-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtempenv.sh
executable file
·70 lines (57 loc) · 1.2 KB
/
tempenv.sh
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
#!/bin/bash
function showhelp() {
echo "Usage: $0 [setup|start|help]"
}
function setup() {
mkdir -p tempenv/{chroot,persist,files,dev/pts}
mknod -m 622 tempenv/dev/console c 5 1
mknod -m 666 tempenv/dev/null c 1 3
mknod -m 666 tempenv/dev/zero c 1 5
mknod -m 666 tempenv/dev/ptmx c 5 2
mknod -m 666 tempenv/dev/tty c 5 0
mknod -m 444 tempenv/dev/random c 1 8
mknod -m 444 tempenv/dev/urandom c 1 9
chown root:tty tempenv/dev/{console,ptmx,tty}
}
function verifysetup() {
for i in tempenv/{chroot,persist,files,dev/pts}; do
if [[ ! -d $i ]]; then
return 1
fi
done
return 0
}
function start() {
if ! verifysetup; then
echo "Environment wasn't set up."
return 1
fi
unionfs-fuse -o cow,allow_other tempenv/files=RW:tempenv/persist:/ tempenv/chroot
mount --bind tempenv/dev tempenv/chroot/dev
mount --bind /dev/pts tempenv/chroot/dev/pts
set +e
chroot tempenv/chroot
umount tempenv/chroot/{dev/pts,dev} tempenv/chroot
set -e
}
set -e
if [[ -z $1 ]] || [[ $1 == "help" ]]; then
showhelp
exit 0
fi
cd "$(dirname "$0")"
if [[ $EUID -ne 0 ]]; then
echo "Must be run as root"
exit 1
fi
case "$1" in
"start")
start
;;
"setup")
setup
;;
*)
echo "Unknown command: $1"
;;
esac