-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathshocker-exec
executable file
·59 lines (49 loc) · 1.48 KB
/
shocker-exec
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
#!/bin/sh
cgroups='cpu,cpuacct,memory'
btrfs_path='/var/shocker'
dirname=$(dirname "$(readlink -f "$0")")
#shellcheck disable=SC1090
. "$dirname"/utils.sh
usage () {
cat << USAGE
shocker-exec - run a command inside a container
Usage: shocker exec <container> <command>
Options:
-h, --help output usage information
Examples:
$ shocker exec ps_1234 top # run top inside a running container
USAGE
}
[ "$#" -eq 0 ] && { usage; exit 1; }
case "$1" in
-h|--help ) usage && exit 1 ;;
esac
shocker_exec() {
fn_mine () {
xargs ps ho pid,command \
< "/sys/fs/cgroup/cpu/$1/tasks" \
| awk '$2 == "unshare" {print $1}' \
| xargs pgrep -P | head -1
}
cid="$(fn_mine "$1")"
shocker_log_command "$1" "${@:2}"
nsenter -t "$cid" -m -u -i -n -p \
chroot "$btrfs_path/containers/$1" \
/usr/bin/env -i /bin/sh -c "export PS1=\"\x1b[1m${1} \w\x1b[0m # \"; \
${*:2}"
}
cstate=$(get_state "$1")
ctype=$(get_type "$1")
case "$ctype" in
image ) printf "'%s' does not exist as a container\n" "$1" >&2; exit 1 ;;
container)
case "$cstate" in
running) shocker_exec "$@" ;;
stopped) printf "Container '%s' is not running\n" "$1" >&2; exit 1 ;;
crashed) printf "Container '%s' is not running\n" "$1" >&2; exit 1 ;;
missing) printf "Container '%s' does not exist\n" "$1" >&2; exit 1 ;;
* ) usage; exit 1 ;;
esac ;;
unknown ) printf "Can not determine type of '%s'\n" "$1" >&2; exit 1 ;;
* ) usage; exit 1 ;;
esac