-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwithout-mounts
executable file
·176 lines (152 loc) · 3.81 KB
/
without-mounts
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash -e
name=${0##*/}
printf -v name_pad "%${#name}s" ''
usage_content=(
"Usage: ${0##*/} [--except]"
" ${name_pad} --fs-type=* [--fs-type=*]..."
" ${name_pad} -- cmd args..."
''
"Usage: ${0##*/} [--except]"
" ${name_pad} [--subtree=dir]..."
" ${name_pad} [mount-point] ..."
" ${name_pad} -- cmd arg1 arg2 ..."
''
' Removes mount table entries matching (or not matching) a'
' specification from the current namespace, and runs a specified'
' command. Refuses to operate in a shared namespace.'
''
' Intended for use with the util-linux unshare utility.'
' Example:'
''
" unshare --mount -- \\"
" without-mounts --except /usr/bin /var/log /proc -- \\"
" runuser -u log -- \\"
" syslogd"
)
usage() {
printf '%s\n' "${usage_content[@]}"
}
safe_umount() {
local relroot options_str retval
relroot=$1
mid=${relroot_to_mid[$relroot]}
[[ ${done_mids[$mid]} ]] && return
[[ $mid ]] || {
echo "ERROR: $relroot not recognized as a mount point" >&2
return 1
}
options_str=${mid_to_options_str[$mid]}
if [[ $options_str = *" shared:"* ]]; then
echo "ERROR: $relroot has not been unshared" >&2
return 1
fi
{ (( dry_run )) || umount -l -R "$relroot"; }; retval=$?
flag_done "$mid"
return "$retval"
}
# mark a MID as a target
declare -A flagged_mids=( )
flag_mid() {
local mid=$1
[[ ${flagged_mids[$mid]} ]] && return
flagged_mids[$mid]=1
if (( except )); then
local pmid=${mid_to_pmid[$mid]}
if [[ $pmid ]] && ! [[ ${flagged_mids[$pmid]} ]]; then
flag_mid "$pmid"
fi
fi
}
# mark a mid as unmounted
declare -A done_mids=( )
flag_done() {
local mid cmid cmids_str
local -a cmids
mid=$1
[[ ${done_mids[$mid]} ]] && return
done_mids[$mid]=1
cmids_str=${pmid_to_mids[$mid]}
read -r -a cmids <<<"$cmids_str"
for cmid in "${cmids[@]}"; do
flag_done "$cmid"
done
}
source read-mount-table.bash || exit
declare -A fs_types=( )
declare -A subtrees=( )
except=0
dry_run=0
while [[ $1 = -* ]]; do
case $1 in
--) break;;
--dry-run) dry_run=1;;
--fs-type=*) fs_types[${1#*=}]=1;;
--except) except=1;;
--subtree=*) subtrees[${1#*=}]=1;;
-h|--help) usage; exit;;
*) usage; exit 1;;
esac
shift
done
(( dry_run )) || [[ $EUID = 0 ]] || {
echo "ERROR: ${0##*/} requires root" >&2
exit 1
}
targets=( )
while (( $# )) && [[ $1 != -- ]]; do
targets+=( "$1" )
shift
done
[[ $1 = -- ]] && shift
cmd=( "$@" )
(( ${#cmd[@]} )) || { usage; exit 1; }
if (( ${#fs_types[@]} )); then
if (( ${#subtrees[@]} )); then
echo "ERROR: Subtree support is not enabled when filtering by fs type" >&2
exit 1
fi
if (( ${#targets[@]} )); then
echo "ERROR: Explicit target support is not enabled when filtering by fs type" >&2
exit 1
fi
fi
# flag mount IDs matching given fs types
for mid in "${!mid_to_fstype[@]}"; do
fstype=${mid_to_fstype[$mid]}
if [[ ${fs_types[$fstype]} || ${fs_types[${fstype%.*}]} ]]; then
flag_mid "$mid"
fi
done
# flag mount IDs given in explicit targets
for target in "${targets[@]}" "${!subtrees[@]}"; do
target_mid=${relroot_to_mid[$target]}
if [[ $target_mid ]]; then
flag_mid "$target_mid"
elif (( except )); then
# when in whitelist mode, find and whitelist the mount point
target_mnt=$(stat -c %m "$target")
target_mid=${relroot_to_mid[$target_mnt]}
flag_mid "$target_mid"
fi
if [[ ${subtrees[$target]} ]] && (( except )); then
for relroot in "${!relroot_to_mid[@]}"; do
if [[ $relroot = "${target%/}"/* ]]; then
subtree_mid=${relroot_to_mid[$relroot]}
flagged_mids[$subtree_mid]=1
fi
done
fi
done
if (( except )); then
for mid in "${!mid_to_relroot[@]}"; do
[[ ${flagged_mids[$mid]} ]] && continue
relroot=${mid_to_relroot[$mid]}
safe_umount "$relroot" || exit
done
else
for mid in "${!flagged_mids[@]}"; do
relroot=${mid_to_relroot[$mid]}
safe_umount "$relroot"
done
fi
exec -- "${cmd[@]}"