forked from pikhq/sabotage
-
Notifications
You must be signed in to change notification settings - Fork 68
/
enter-chroot
executable file
·104 lines (91 loc) · 2.45 KB
/
enter-chroot
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
#!/bin/sh
[ -z "$CONFIG" ] && CONFIG=./config
. "$CONFIG"
export H="$PWD"
ret=0
if [ -z "$R" ] ; then
echo 'no R environment var set, your config is broken'
exit 1
elif [ "$R" = / ] ; then
echo "error: R=/ can't be used with this script, as it would make"
echo "your host sys unusable after returning. (umounting /dev /proc...)"
exit 1
fi
mkdir -p "$R"/src/tarballs
linux32=
if [ $(uname -m) = "x86_64" ] ; then
case "$A" in i?86)
echo "trying to get into 32bit rootfs from 64bit host"
[ -x ./linux32 ] && linux32=./linux32 || {
$CC "$K"/linux32.c -static -s -Os -o ./linux32 && linux32=./linux32 || \
echo -ne "warning: trying to chroot into i386 rootfs on x86_64" \
"kernel and building linux32 command failed\n"
} ;;
esac
fi
uid="$(id -u)"
if [ -d /proc/self/ns ] ; then
CHROOT=./super_chroot
if [ ! -x "$CHROOT" ] ; then
if ! $CC "$K"/super_chroot.c -static -s -Os -o "$CHROOT" 2>/dev/null ; then
echo "error: failed to build super_chroot!"
echo "try: musl-gcc KEEP/super_chroot.c -o super_chroot"
echo "or: re-run ./enter-chroot as root."
exit 1
fi
fi
CHROOT="./super_chroot -b $C:./src/tarballs"
else
if [ "$uid" != "0" ]; then
printf -- "need to be root. please enter password.\n"
su -c "$0"
exit $?
fi
# prevent from mounting twice
if [ ! -d "$R"/dev/pts ] ; then
mount --bind /dev "$R"/dev
mount --bind /dev/pts "$R"/dev/pts
mount --bind /dev/shm "$R"/dev/shm
mount --bind /proc "$R"/proc
mount --bind "$C" "$R"/src/tarballs
fi
CHROOT=chroot
fi
set_title() {
[ "$TERM" != "linux" ] && [ "$TERM" != "vt100" ] && \
printf "\033]2;%s\007" "$1"
}
if [ -n "$1" ]; then
$linux32 $CHROOT "$R" /bin/env -i \
HOME=/root PATH="/local/bin:/bin" TERM="$TERM" PS1='\u:\w$ ' \
"$@"
ret=$?
else
set_title "SABOTAGE CHROOT $(basename $R)"
echo "Entering chroot..."
$linux32 $CHROOT "$R" /bin/env -i \
HOME=/root TERM="$TERM" PS1='\u:\w$ ' \
/bin/ash --login
ret=$?
#echo empty line so we get a proper prompt back
echo
fi
tryumount() {
dest="$1"
if ! umount "$dest" 2>/dev/null; then
echo "unmount failure ($dest), retrying in 1 sec..."
sleep 1
if ! umount -f "$dest" 2>/dev/null; then
echo "unmount -f failure ($dest), trying last resort umount -l"
umount -l "$dest"
fi
fi
}
if [ "$CHROOT" = "chroot" ] && [ -d "$R"/dev/pts ] ; then
tryumount "$R"/src/tarballs
tryumount "$R"/dev/pts
tryumount "$R"/dev/shm
tryumount "$R"/dev
tryumount "$R"/proc
fi
exit $ret