-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdo-chroot
executable file
·195 lines (174 loc) · 5.66 KB
/
do-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
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
#!/bin/bash
# (c) 2020 Leif Sawyer
# License: GPL 3.0 (see https://github.com/akhepcat/)
# Permanent home: https://github.com/akhepcat/Miscellaneous/
# Direct download: https://raw.githubusercontent.com/akhepcat/Miscellaneous/master/do-chroot
#
ROOT=${1}
trap cleanup SIGINT SIGTERM SIGKILL SIGQUIT SIGABRT SIGSTOP SIGSEGV
cleanup() {
echo "bailing out..."
[[ ${MOUNTED} -eq 1 ]] && do_umounts
exit 0
}
resolv_conf() {
if [ -L "${ROOT:-undefined_rootfs}/etc/resolv.conf" ]
then
if [ -n "$(ls -l ${ROOT:-undefined_rootfs}/etc/resolv.conf 2>&1 | egrep -i 'run/.*(resolvconf|networkmanager)')" ]
then
# it's a symbolic link to a dynamic resolv file, so we can overwrite it with impunity
# first get the actual location
LRC=${ROOT:-undefined_rootfs}$(ls -l ${ROOT:-undefined_rootfs}/etc/resolv.conf | sed 's/.*-> //g;')
# check for /var/run -> /run, since that breaks chrooting (use relative links, dists!)
VRR=$(ls -dl ${ROOT:-undefined_rootfs}/var/run | sed 's/.*-> //g;')
if [ -L "${ROOT:-undefined_rootfs}/var/run" -a "${VRR}" = "/run" ]
then
# remove the 'var' part
LRC=${LRC//\/var/}
fi
# touch it so that we know the full path exists
mkdir -p "${LRC//resolv.conf/}"
touch -a "${LRC}"
cat /etc/resolv.conf > "${LRC}"
else
echo "resolv.conf may need manually updating for full network connectivity"
fi
else
# it's a real file
echo "resolv.conf may need manually updating for full network connectivity"
fi
}
do_mounts() {
# grab all the mountpoints, and submountpoints...
[[ ! -e ${ROOT:-undefined_rootfs}/dev/tty0 ]] && ${DEBUG:+echo} mount --rbind /dev ${ROOT:-undefined_rootfs}/dev
[[ ! -e ${ROOT:-undefined_rootfs}/dev/pts/ptmx ]] && ${DEBUG:+echo} mount --rbind /dev/pts ${ROOT:-undefined_rootfs}/dev/pts
[[ ! -e ${ROOT:-undefined_rootfs}/proc/version ]] && ${DEBUG:+echo} mount --rbind /proc ${ROOT:-undefined_rootfs}/proc
# don't rbind /sys, because we don't want the cgroups!
[[ ! -e ${ROOT:-undefined_rootfs}/sys/devices/cpu ]] && ${DEBUG:+echo} mount --bind /sys ${ROOT:-undefined_rootfs}/sys
MOUNTED=1
}
do_umounts() {
if [ ${MOUNTED:-0} -eq 1 -a -n "${ROOT:-undefined_rootfs}" ]
then
${DEBUG:+echo} umount --force --lazy \
${ROOT:-undefined_rootfs}/sys \
${ROOT:-undefined_rootfs}/proc \
${ROOT:-undefined_rootfs}/dev/pts \
${ROOT:-undefined_rootfs}/dev
echo "Sometimes umounting kills the permissions on the directory /dev/shm, and umounts both /dev/pts and /proc/sys/fs/binfmt_misc"
echo "Attempting to detect and fix"
if [ "$(stat --format='%a' /dev/shm)" != "1777" ]
then
chmod 1777 /dev/shm
fi
[[ ! -e /dev/pts/ptmx ]] && mount -t devpts none /dev/pts
[[ ! -e /proc/sys/fs/binfmt_misc/status ]] && mount -t binfmt_misc none /proc/sys/fs/binfmt_misc
else
echo "not mounted, or ${ROOT:-chrootfs} not accessible"
fi
}
check_emu() {
HOST=$(uname -m)
if [ "${HOST}" != "${HOST##*arm*}" ]
then
HOST=arm
elif [ "${HOST}" != "${HOST##*x86_64*}" -o "${HOST}" != "${HOST##*x86-64*}" ]
then
HOST=x86_64
fi
if [ -z "$(ls /proc/sys/fs/binfmt_misc/qemu-* 2>/dev/null)" ]
then
echo "qemu binfmt_misc support not registered. You may have problems with foreign archs"
HOST=""
fi
# this should only find one, but compatibility libs will show up first, so only use the last found
LDLIB=$(find -L ${ROOT:-undefined_rootfs}/lib ${ROOT:-undefined_rootfs}/lib64 -maxdepth 1 -type f -iname 'ld-uclibc*' -o -iname 'ld-linux*' 2>/dev/null | tail -1)
TEMU=$(file -L "${LDLIB}" )
# we can see the library, so we can figure out the emulation
if [ -z "${TEMU##*x86_64*}" -o -z "${TEMU##*x86-64*}" -o -z "${TEMU##*32-bit*80386*}" ]
then
# Intel 32-bit and 64-bit are handled by aarch64
EMU=aarch64
elif [ -n "$(ls -l ${ROOT:-undefined_rootfs}/lib/ld-linux* 2>/dev/null | grep armeb )" ]
then
EMU=armeb
elif [ -n "$(ls -l ${ROOT:-undefined_rootfs}/lib/ld-linux* 2>/dev/null | grep arm-)" ]
then
EMU=arm
elif [ -z "${TEMU##*32*ARM*}" ]
then
EMU=armeb
elif [ -z "${TEMU##*64*ARM*}" -o -z "${TEMU##*aarch64*}" ]
then
EMU=aarch64
fi
if [ -z "${HOST}" -a -z "${EMU}" ]
then
echo "Can't reliably determine host/chroot architecture. Aborting."
cleanup
fi
if [ -n "${EMU}" -a ! -r "/proc/sys/fs/binfmt_misc/qemu-${EMU}" ]
then
EMU=""
if [ "${EMU}" = "${HOST}" ]
then
echo "attempting native chroot without binfmt_misc support"
else
echo "no qemu emulator for architecture ${EMU}"
cleanup
fi
fi
}
get_shell() {
if [ -r ${ROOT:-undefined_rootfs}/bin/bash ];
then
SHELL=/bin/bash
elif [ -r ${ROOT:-undefined_rootfs}/sbin/bash ];
then
SHELL=/sbin/bash
elif [ -r ${ROOT:-undefined_rootfs}/sbin/ash ];
then
SHELL=/sbin/ash
elif [ -r ${ROOT:-undefined_rootfs}/bin/ash ];
then
SHELL=/bin/ash
elif [ -r ${ROOT:-undefined_rootfs}/sbin/sh ];
then
SHELL=/sbin/sh
elif [ -r ${ROOT:-undefined_rootfs}/sbin/sh ];
then
SHELL=/sbin/sh
else
#HOPE!
SHELL=sh
fi
}
# Does it look like a mounted filesystem?
if [ -n "${ROOT}" ]
then
ROOT=${ROOT%/} # strip off a trailing '/'
if [ -d "${ROOT:-undefined_rootfs}/etc" ]
then
check_emu
do_mounts
resolv_conf
[[ -r "${ROOT:-undefined_rootfs}/etc/hostname" ]] && export debian_chroot=$(cat ${ROOT:-undefined_rootfs}/etc/hostname)
if [ -n "${EMU}" -a ! -r "${ROOT:-undefined_rootfs}/usr/bin/qemu-${EMU}-static" ]
then
cp /usr/bin/qemu-${EMU}-static ${ROOT:-undefined_rootfs}/usr/bin/ 2>/dev/null
if [ $? -ne 0 ];
then
echo "can't cp emulation binary; check filesystem"
cleanup
fi
fi
get_shell
chroot ${ROOT:-undefined_rootfs} ${SHELL}
echo "clean to umount ${ROOT:-undefined_rootfs}"
else
echo "Can't chroot to dir ${ROOT:-undefined_rootfs} - is it a valid filesystem?"
fi
else
echo "chroot to where?"
fi
cleanup