-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwith-bind-mount
executable file
·54 lines (45 loc) · 1.02 KB
/
with-bind-mount
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
usage_content=(
"Usage: ${0##*/} [--if-exists] src dest [--] cmd arg..."
)
usage() {
printf '%s\n' "${usage_content[@]}"
}
ignore_missing=0
(( $# > 2 )) || { usage; exit 1; }
while [[ $1 = -* ]]; do
case $1 in
--) shift; break;;
--if-exists)
ignore_missing=1;;
-h|--help) usage; exit;;
*) usage; exit 1;;
esac
shift
done
[[ $EUID = 0 ]] || {
echo "ERROR: ${0##*/} requires root" >&2
exit 1
}
source read-mount-table.bash || exit
source=$1; shift
dest=$1; shift
[[ $1 = -- ]] && shift
(( $# )) || { usage; exit 1; }
if [[ -e $source && -e $dest ]]; then
dest_mountpoint=$(stat -c %m "$dest")
dest_mid=${relroot_to_mid[$dest_mountpoint]}
if [[ ${mid_to_options_str[$dest_mid]} = *" shared:"* ]]; then
echo "ERROR: $dest_mountpoint has not been unshared" >&2
exit 1
fi
mount -o bind -- "$source" "$dest" || exit
else
if (( ignore_missing )); then
echo "WARNING: Either $source or $dest does not exist" >&2
else
echo "ERROR: Either $source or $dest does not exist" >&2
exit 1
fi
fi
exec -- "$@"