-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsbox-mgt
More file actions
executable file
·371 lines (323 loc) · 9.32 KB
/
Copy pathsbox-mgt
File metadata and controls
executable file
·371 lines (323 loc) · 9.32 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/bin/bash
#
# sbox
# Sandbox instance management.
#
# Copyright: 2019 Shawwwn, shawwwn1@gmail.com
# License: MIT
#
# Usage: sbox-mgt ACTION [ARGS...] [OPTION]
#
function help {
local executable=$(basename $0)
echo "usage: $executable stop [-n NAME|-c|-m|-v]"
echo " $executable join [-n NAME|-v]"
echo " $executable list [-n NAME|-a|-v]"
echo " $executable [-h|--help]"
echo "Sandbox instance management."
echo ""
echo " stop:"
echo " Kill the sandbox'ed process."
echo " Specify whether to commit/merge changes with flags -c/-m."
echo ""
echo " join:"
echo " Send process into a running container."
echo ""
echo " list:"
echo " List *running* containers. Use with -v for more details."
echo ""
echo " remove:"
echo " Remove an inactive container."
echo ""
echo " -n, --container-name Name of the target sandbox."
echo " -c, --auto-commit Auto commit file system changes"
echo " to snapshot when exit"
echo " -m, --auto-merge Auto merge snapshot to rootfs"
echo " when exit."
echo " -a, --list-all List all containers, including"
echo " ones that are not running."
echo " -v, --verbose Print more infomation."
echo " -h, --help Display this help and exit."
}
# process command line arguments
VERBOSE=false
AUTO_COMMIT=false
AUTO_MERGE=false
LIST_ALL=false
CONTAINER_NAME="default"
NAME_SET=false
ARGS=$(getopt -q \
-o "n:cmavh" \
-l "container-name:,auto-commit,auto-merge,list-all,verbose,help" \
-n "sbox-mgt" -- "$@")
if [ $? -ne 0 ]; then
echo "Invalid Arguments!"
exit 2
fi
eval set -- "$ARGS"
while true; do
case "$1" in
-n|--container-name)
CONTAINER_NAME="$2"
NAME_SET=true
shift 2 ;;
-c|--auto-commit)
AUTO_COMMIT=true
shift ;;
-m|--auto-merge)
AUTO_MERGE=true
shift ;;
-a|--list-all)
LIST_ALL=true
shift ;;
-v|--verbose)
VERBOSE=true
shift ;;
-h|--help)
help
shift
exit ;;
--)
shift
break ;;
esac
done
# globals
CMD=$(echo "$1" | awk '{print tolower($0)}')
shift
#############################################################
# Get the nth parent pid, start from given pid
#############################################################
get_nth_ppid() {
local pid=$1
local n=$2
local ppid=$(ps --no-headers -o ppid "$pid" | xargs) # trim result from 'ps'
[ -z n ] && n=1
[ $n -eq 0 ] && echo "$pid" && return
if (( $n > 1 )); then
get_nth_ppid "$ppid" $((n-1))
else
echo "$ppid"
fi
}
#############################################################
# Set pid's environment variable
#############################################################
set_pid_env() {
local pid=$1
local var=$2
local val="$3"
if [ -z "$3" ]; then
# unset $var
gdb -n -q -p $pid <<<"""
call (int) unsetenv(\"$var\")
set confirm off
quit
""" >/dev/null
else
# set $var
gdb -n -q -p $pid <<<"""
call (int) setenv(\"$var\", \"$val\")
set confirm off
quit
""" >/dev/null
fi
}
#############################################################
# Trying to terminate a process nicely
#############################################################
kill_proc_nice() {
local pid=$1
if ! ps -p $pid >/dev/null; then return 0; fi
local killed=false
local i=0
kill -SIGINT $pid
for ((i=0; i<20; i++)); do # 2s timeout
if ! ps -p $pid >/dev/null; then
killed=true
break
fi
sleep 0.1
done
if $killed; then return 0; fi
kill -SIGTERM $pid
for ((i=0; i<30; i++)); do # 3s timeout
if ! ps -p $pid >/dev/null; then
killed=true
break
fi
sleep 0.1
done
if $killed; then return 0; fi
kill -SIGKILL $pid
}
#############################################################
# Trying to terminate a process tree nicely
#############################################################
kill_tree_nice() {
local pid=$1
for child in $(ps -o "pid" --no-headers --ppid $pid); do
kill_tree_nice $child
done
kill_proc_nice $pid
}
#############################################################
# Return container pid if running
#############################################################
get_container_pid() {
local name="$1"
local pid=$(cat "/tmp/sbox/$name/container.pid" 2>/dev/null)
if [ ! -z "$pid" ] && (ps -p "$pid" >/dev/null 2>&1); then
echo "$pid"
return 0
fi
return 1;
}
#############################################################
# Print error message & exit
#############################################################
error_exit() {
echo -e "$1" >&2
exit 1
}
#############################################################
# Return a list of containers under base directory
#############################################################
container_list() {
local base_dir=$(realpath "$1") # /tmp/sbox
pushd "$base_dir" >/dev/null
local name
find * -maxdepth 0 -type d | \
while IFS= read -r name; do
if [ -f "$base_dir/$name/config" ]; then
echo "$name"
fi
done
popd >/dev/null
}
#############################################################
# Return a list of containers under base directory
#############################################################
show_container_info() {
local name="$1"
if get_container_pid "$CONTAINER_NAME" >/dev/null; then
printf "\e[32m$1\e[0m" # active container, print in green
$VERBOSE && echo ": running" || echo ""
else
printf "\e[31m$1\e[0m" # inactive container, print in red
$VERBOSE && echo ": inactive" || echo ""
fi
# source configuration file in a subshell
$VERBOSE && (
source "/tmp/sbox/$CONTAINER_NAME/config";
echo -e "\e[33mlocation\e[0m=$SBOX_LOC";
echo -e "\e[33msnapshot_dir\e[0m=$SBOX_SNAPSHOT_DIR";
echo -e "\e[33mdirty_dir\e[0m=$SBOX_DIRTY_DIR";
echo -e "\e[33mwork_dir\e[0m=$SBOX_WORK_DIR";
echo -e "\e[33mrootfs\e[0m=$SBOX_ROOTFS_DIR";
echo -e "\e[33mmapped_user\e[0m=$SBOX_USER";
echo -e "\e[33mexec\e[0m=$SBOX_EXEC";
echo -e "\e[33mppid\e[0m=$SBOX_PID";
echo -e "\e[33mpid\e[0m=$SBOX_PPID";
echo "";
)
}
#############################################################
# Prompt user to enter Y/N
#############################################################
prompt_yes_no() {
local prompt="$1"
local default="$2" # true|false
[ -z "$default" ] && default=false # default is 'no'
local yn
while true; do
read -p "$prompt" -e yn
case $yn in
[Yy]* ) return 0 ;;
[Nn]* ) return 1 ;;
"" ) if $default; then return 0; else return 1; fi ;;
* ) return 2 ;;
esac
done
return 3
}
#############################################################
# Remove a sandbox's internal directory
#############################################################
remove_dir() {
local tgt_dir="$1" # dir to delete
local root_dir="$2" # sandbox's root dir
local default="$3" # true|false
[ -z "$default" ] && default=false # default is 'no'
# manual remove if internal dir is not under sbox's root dir
if [ -d "$tgt_dir" ] && ! (echo "$tgt_dir" | grep -q "$root_dir"); then
if prompt_yes_no "> delete '$tgt_dir'? [Y/n] " $default; then
$VERBOSE && echo "Removing $tgt_dir";
rm -r "$tgt_dir";
else
$VERBOSE && echo "skip";
fi
fi
}
# main()
spid=$(get_container_pid "$CONTAINER_NAME") # pid of sandbox'ed program
$NAME_SET && $VERBOSE && echo "container name: $CONTAINER_NAME"
if [ "$CMD" = "stop" ]; then
$VERBOSE && echo "container pid: $spid"
[ -z $spid ] && error_exit "Container \"$CONTAINER_NAME\" is not running."
cpid=$(get_nth_ppid $spid 3) # container pid
# set target process' environment variable
set_pid_env $cpid AUTO_COMMIT "$AUTO_COMMIT"
set_pid_env $cpid AUTO_MERGE "$AUTO_MERGE"
# kill target process
kill_tree_nice $spid
elif [ "$CMD" = "join" ]; then
$VERBOSE && echo "container pid: $spid"
[ -z $spid ] && error_exit "Container \"$CONTAINER_NAME\" is not running."
[ -z $1 ] && error_exit "Nothing to run."
# launch process inside namespace
local killed=false
echo "Joining container \"$CONTAINER_NAME\":"
nsenter -a -t $spid --no-fork \
bash -c """$(cat <<-EOF
ppid=\$(cat /proc/self/status | grep ^PPid: | awk '{print \$2}')
cd "/tmp/sbox/$CONTAINER_NAME"
exec chroot rootfs \
sudo -u $USER \
bash -c 'cd ~; exec $@'
EOF
)"""
[ $? == 137 ] && killed=true
# fix shell's strange behavior after sandbox's main process was killed(-9)
# thus bringing down joint process
if $killed; then
echo "sandbox is being torn down, exiting ..."
stty sane
# $killed && kill -9 $$
fi
elif [ "$CMD" = "list" ]; then
if $NAME_SET; then
show_container_info "$CONTAINER_NAME"
else
container_list "/tmp/sbox" | while IFS= read -r CONTAINER_NAME; do
# filter inactive containers if list-all flag is not present
if ! ($LIST_ALL || get_container_pid "$CONTAINER_NAME" >/dev/null); then
continue
fi
show_container_info "$CONTAINER_NAME"
done
fi
elif [ "$CMD" = "remove" ]; then
[ ! -z $spid ] && error_exit "Container \"$CONTAINER_NAME\" is running.\n\nRun 'sbox-mgt stop -n $CONTAINER_NAME' first."
[ ! -f "/tmp/sbox/$CONTAINER_NAME/config" ] && error_exit "Container \"$CONTAINER_NAME\" has already been deleted."
(
source "/tmp/sbox/$CONTAINER_NAME/config";
remove_dir "$SBOX_SNAPSHOT_DIR" "$SBOX_LOC" false
remove_dir "$SBOX_DIRTY_DIR" "$SBOX_LOC" true
remove_dir "$SBOX_WORK_DIR" "$SBOX_LOC" true
$VERBOSE && echo "Removing $SBOX_LOC";
rm -r "$SBOX_LOC";
)
else
help
fi