-
Notifications
You must be signed in to change notification settings - Fork 0
/
chroot_x86_64
executable file
·345 lines (334 loc) · 19.2 KB
/
chroot_x86_64
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
#!/bin/zsh
# ============================================================================ #
# ==== Version 1.1.4 31mar2024 ============================================= #
# ==== (c) IB ============================================= #
# ============================================================================ #
# check if the user is /root/, or exit with error ============================ #
####
[[ $EUID -ne 0 ]] &&
{ echo "This script must be run as root!" ; exit 1; }
# display /help/ ============================================================= #
####
[[ $1 == *"-h"* ]] &&
{ echo "type $(readlink -f "$0") --install to install the chroot environment";
echo "type $(readlink -f "$0") --setup to setup the chroot environment" ;
echo "type $(readlink -f "$0") --upgrade to upgrade the chroot environment";
echo "type $(readlink -f "$0") --backup to backup unsynced folders/files" ;
echo "type $(readlink -f "$0") --restore to restore the chroot environment";
echo "type $(readlink -f "$0") <cmd> to run <cmd> under chroot"; exit 0; }
# an extra line to even the total number
# ============================================================================ #
# ==== Configuration ========================================================= #
# ============================================================================ #
# /general configuration/ (general variables) -------------------------------- #
####
user____________="$(whoami)" `# "..." in Raspberry Pi`
user_chroot_____="$(whoami)" `# same as in the VM `
display_________=0 `# 0 by default `
path_filesystem_="$(ls -d /media/${user____________}/*USB*/*RPi/_filesystem/)"
path_software___="$(ls -d /media/${user____________}/*USB*/*RPi/_software/ )"
path_backup_file="${path_filesystem_}/non_qcow2.tar.gz"
qemu_image_url__="https://cdimage.debian.org/debian-cd/current/amd64"
qemu_image_url__="${qemu_image_url__}/iso-cd/debian-12.6.0-amd64-netinst.iso"
qemu_image_size_="64G" `# 16G or more `
qemu_smp________=2 `# 50% of host CPU `
qemu_m__________="4G" `# 50% of host RAM `
qemu_ssh_port___=22000
path_local_sw___="/opt"
path_mountpoint_="/mnt"
path_chroot_____="/var/chroot_x86_64"
path_user_chroot="/home/user.${user_chroot_____}" `# ~ in the VM `
vm_on_upgrade___=1 `# 0 or 1 `
rsync_ignore____="/home/user.${user_chroot_____}/" `# no patterns allowed! `
rsync_ignore____="${rsync_ignore____} ${path_local_sw___}/stata18/"
rsync_ignore____="${rsync_ignore____} ${path_local_sw___}/zotero/ "
# NB if upgrade is interrupted, unsynced files can be found in ~/.rsync_ignore/
# /user-defined functions/ --------------------------------------------------- #
####
chroot_backup() {
[[ ! -e "$(dirname "${path_backup_file}")" ]] &&
{
echo "The backups folder was not found! Aborting chroot backup..."
exit 0
}
# unmount user folder(s)
for i in "$(eval echo ~"${user____________}")"/*/ ; do
i="$(basename "$i")"
umount -lf "${path_chroot_____}${path_user_chroot}"/"$i" &> /dev/null; done
sleep 2
[[ "$(mount | grep ${path_user_chroot})" != "" ]] &&
{
echo "Unmounting failed, please unmount the filesystem(s) manually!"
exit 0
}
# backup the ${rsync_ignore____} folders/files
echo "Chroot backup initiated. Compressing unsynced folders/files..."
chroot "${path_chroot_____}" \
sh -c "tar czvf \"${path_backup_file}\" ${rsync_ignore____}"
# finish
echo "Backup of unsynced folders/files is complete! Setting up chroot..."
chroot_setup 0 1
}
chroot_install() {
# display information and ask for confirmation
echo "Script requirements:" `# sudo apt install ... `
echo -e " udisks2\n libguestfs-tools\n qemu (with x86_64 support)"
echo -e " qemu-user-static\n samba\n"
echo "This script will"
echo -n " 1. create a QEMU VM qcow2 file of ${qemu_image_size_} "
echo -n "(dynamically increasing size) at "
echo "'${path_filesystem_}$(basename "${path_chroot_____}").qcow2';"
echo -n " 2. help you install the image "
echo -n "'$(basename "${qemu_image_url__}")' "
echo "into the qcow2 file;"
echo -n " 3. run the QEMU VM for software installation with a CPU of "
echo "${qemu_smp________} core(s) and a RAM of ${qemu_m__________};"
echo -n " 4. on VM's shutdown, mount the qcow2 file at "
echo "'${path_mountpoint_}';"
echo " 5. rsync the QEMU VM's filesystem to '${path_chroot_____}'."
echo ""
read "ans?$(echo $'\n')Do you wish to continue? [y/n]$(echo $'\n: ')"
[[ $(echo ${ans} | tr -d '[:space:]') != [Yy]* ]] &&
exit 0
# set up the qcow2 file
[[ -e "${path_filesystem_}$(basename "${path_chroot_____}").qcow2" ]] &&
{
echo "The QEMU VM qcow2 file already exists! Aborting chroot install..."
exit 0
}
qemu-img create \
-f qcow2 \
"${path_filesystem_}$(basename "${path_chroot_____}").qcow2" \
${qemu_image_size_} &&
rm -rf /tmp/image.iso &&
wget -O /tmp/image.iso "${qemu_image_url__}" &&
qemu-system-x86_64 \
-smp ${qemu_smp________} \
-m ${qemu_m__________} \
-cdrom /tmp/image.iso \
-hda "${path_filesystem_}$(basename "${path_chroot_____}").qcow2" \
-nic user,smb="${path_software___}" \
-nic user,hostfwd=tcp::${qemu_ssh_port___}-:22
# perform chroot upgrade
echo "The QEMU VM qcow2 file is configured! Commencing filesystem sync..."
chroot_upgrade 0 1
}
chroot_restore() {
[[ ! -e "${path_filesystem_}$(basename "${path_chroot_____}").qcow2" ]] &&
{
echo "The QEMU VM qcow2 file was not found! Aborting chroot restore..."
exit 0
}
# perform chroot upgrade
echo "Chroot restore initiated. Commencing filesystem sync..."
chroot_upgrade 0 0
# restore the ${rsync_ignore____} folders/files
echo "Uncompressing backed up unsynced folders/files..."
sh -c "tar xzvf \"${path_backup_file}\" -C \"${path_chroot_____}\""
# finish
echo "Chroot restore is complete! Rebooting (120 seconds)..." &&
sleep 120 &&
shutdown -r now
}
chroot_setup() {
# $1 == 0 do not mount the chroot filesystem(s) on start
# $2 == 0 do not print message on finish
[[ $1 -eq 1 ]] &&
[[ "$(mount | grep ${path_chroot_____} | grep -v /media)" != "" ]] &&
{
echo "The chroot is already (partly) mounted! Aborting chroot setup..."
exit 0
}
# mount the chroot filesystem(s)
[[ $1 -eq 1 ]] &&
mount -t proc proc "${path_chroot_____}"/proc &&
mount -t sysfs sys "${path_chroot_____}"/sys &&
mount --rbind /run "${path_chroot_____}"/run &&
mount --rbind /dev "${path_chroot_____}"/dev &&
mount --rbind /dev/pts "${path_chroot_____}"/dev/pts &&
chmod 0777 "${path_chroot_____}"/tmp
# mount user folder(s)
for i in "$(eval echo ~"${user____________}")"/*/ ; do
i="$(basename "$i")"
[[ "$(echo "$i" | grep conda)" != "" ]] &&
continue
mkdir -p "${path_chroot_____}${path_user_chroot}"/"$i"
mount --rbind "$i" "${path_chroot_____}${path_user_chroot}"/"$i" ; done
# mount media (to chroot)
[[ "$(mount | grep ${path_chroot_____} | grep /media)" == "" ]] &&
chroot "${path_chroot_____}" mkdir -p /media &&
mount --rbind /media "${path_chroot_____}"/media
# share DNS servers and X11
cp -R /etc/host* "${path_chroot_____}"/etc/
ln -f /run/resolvconf/resolv.conf \
"${path_chroot_____}"/etc/
ln -f /tmp/.X11-unix/X${display_________} \
"${path_chroot_____}"/tmp/.X11-unix/
cp "$(eval echo ~"${user____________}")"/.Xauthority \
"${path_chroot_____}${path_user_chroot}"/
# configure RSA key for chroot -> host SSH and individual apps
[[ ! -e "${path_chroot_____}${path_user_chroot}"/.ssh/id_rsa ]] &&
{
cd "$(eval echo ~"${user____________}")"/.ssh/
su "${user____________}" -c 'echo -e "\n\n\n" | ssh-keygen'
echo "# chroot_x86_64" | tee -a authorized_keys &> /dev/null &&
cat id_rsa.pub | tee -a authorized_keys &> /dev/null
mkdir -p "${path_chroot_____}${path_user_chroot}"/.ssh
mv id_rsa* "${path_chroot_____}${path_user_chroot}"/.ssh/
chmod 0600 "${path_chroot_____}${path_user_chroot}"/.ssh/id_rsa*
}
# SoS Jupyter Lab
echo "#!/bin/bash" |
tee "${path_chroot_____}"/usr/local/bin/firefox &> /dev/null &&
echo "chmod 0600 ~/.ssh/id_rsa; ssh -oAddressFamily=inet" \
"-oStrictHostKeyChecking=no -i ~/.ssh/id_rsa" \
"${user____________}@localhost 'DISPLAY=:${display_________}" \
"nohup firefox --new-tab http://localhost:8888/'" |
tee -a "${path_chroot_____}"/usr/local/bin/firefox &> /dev/null &&
chmod 0755 "${path_chroot_____}"/usr/local/bin/firefox
# Stata SE
rm -f /usr/lib64 /usr/lib32 /lib/x86_64-linux-gnu \
/usr/lib/i386-linux-gnu /usr/lib/ld-linux.so.2 \
"$(eval echo ~"${user____________}")"/ado \
$(find "${path_local_sw___}"/ -type l) &> /dev/null
ln -s "${path_chroot_____}"/usr/lib64 /usr/
ln -s "${path_chroot_____}"/usr/lib32 /usr/
ln -s "${path_chroot_____}"/usr/lib/x86_64-linux-gnu /usr/lib/
ln -s "${path_chroot_____}"/usr/lib/i386-linux-gnu /usr/lib/
ln -s "${path_chroot_____}"/lib/i386-linux-gnu/ld-2.28.so \
/lib/ld-linux.so.2
ln -s /usr/lib64 / &> /dev/null
ln -s /usr/lib32 / &> /dev/null
ln -s "${path_chroot_____}${path_user_chroot}"/ado \
"$(eval echo ~"${user____________}")"/ &> /dev/null
for i in "${path_chroot_____}${path_local_sw___}"/* ; do
ln -s "$i" "${path_local_sw___}"/ ; done
[[ "${user_chroot_____}" != "${user____________}" ]] &&
{
useradd "${user_chroot_____}" &> /dev/null
usermod -m -d "${path_user_chroot}" \
"${user_chroot_____}" &> /dev/null
mkdir -p "${path_user_chroot}"
for i in "${path_chroot_____}${path_user_chroot}"/*/ ; do
ln -s "$i" "${path_user_chroot}"/ &> /dev/null; done
stata="$(basename "$(ls -d "${path_local_sw___}"/stata*)")"
ln -s "${path_chroot_____}${path_user_chroot}"/."${stata}" \
"${path_user_chroot}" &> /dev/null
}
for i in "stata" "stata-se" "xstata" "xstata-se" ; do
rm -f "${path_chroot_____}"/usr/local/bin/"$i" &> /dev/null &&
echo "#!/bin/bash" |
tee "${path_chroot_____}"/usr/local/bin/"$i" &> /dev/null &&
echo "${path_local_sw___}/stata*/$i" |
tee -a "${path_chroot_____}"/usr/local/bin/"$i" &> /dev/null &&
chmod 0755 "${path_chroot_____}"/usr/local/bin/"$i" ; done
# configure permissions
chown -R "${user_chroot_____}":"${user_chroot_____}" "${path_user_chroot}"
chroot "${path_chroot_____}" \
chown -R "${user_chroot_____}":"${user_chroot_____}" "${path_user_chroot}"
for i in "${path_local_sw___}" $(echo ${rsync_ignore____}) ; do
setfacl -R -m u:"${user_chroot_____}":rwx "${path_chroot_____}"/"$i" \
&> /dev/null
chroot "${path_chroot_____}" \
setfacl -R -m u:"${user_chroot_____}":rwx "$i" &> /dev/null; done
[[ $2 -eq 1 ]] &&
echo "Chroot setup is complete! Type $(readlink -f "$0") <cmd> to use."
exit 0
}
chroot_upgrade() {
# $1 == 0 do not run the QEMU VM for software installation before rsync
# $2 == 0 do not run reboot on finish
# find media mounted to host (not chroot)
declare -a media
while read i; do media=( "${media[@]}" "$i"); done \
< <(mount | grep -v ${path_chroot_____} | grep /media | awk '{print $1}')
# unmount the chroot filesystem(s)
for i in "/proc" "/sys" "/run" "/dev/pts" "/dev" ; do
umount -lf "${path_chroot_____}$i" &> /dev/null; done
sleep 2
# unmount user folder(s)
for i in "$(eval echo ~"${user____________}")"/*/ ; do
i="$(basename "$i")"
umount -lf "${path_chroot_____}${path_user_chroot}"/"$i" &> /dev/null; done
sleep 2
[[ "$(mount | grep ${path_chroot_____} | grep -v /media)" != "" ]] &&
{
echo "Unmounting failed, please unmount the filesystem(s) manually!"
exit 0
}
# unmount media (from chroot)
for i in "${media[@]}" ; do
su "${user____________}" -c "udisksctl unmount -b $i" &> /dev/null; done
umount -lf "${path_chroot_____}"/media &> /dev/null
sleep 2
for i in "${media[@]}" ; do
su "${user____________}" -c "udisksctl mount -b $i" &> /dev/null; done
# run the QEMU VM and sync chroot with its filesystem
[[ ! -e "${path_filesystem_}$(basename "${path_chroot_____}").qcow2" ]] &&
{
echo "The QEMU VM qcow2 file was not found! Aborting chroot upgrade..."
exit 0
}
[[ $1 -eq 1 ]] &&
qemu-system-x86_64 \
-smp ${qemu_smp________} \
-m ${qemu_m__________} \
-hda "${path_filesystem_}$(basename "${path_chroot_____}").qcow2" \
-nic user,smb="${path_software___}" \
-nic user,hostfwd=tcp::${qemu_ssh_port___}-:22
echo "Chroot upgrade initiated. Commencing filesystem sync..."
umount -lf "${path_mountpoint_}" &> /dev/null
guestmount \
-a "${path_filesystem_}$(basename "${path_chroot_____}").qcow2" \
-i \
"${path_mountpoint_}"
{
tmp_path="$(eval echo ~"${user____________}")"/.rsync_ignore
for i in $(echo ${rsync_ignore____}) ; do
mkdir -p "$(dirname "${tmp_path}$i")"
rm -rf "${tmp_path}$i"
mv "${path_chroot_____}$i" "${tmp_path}$i" &> /dev/null; done
}
rsync -arzv --delete \
--include="*" \
"${path_mountpoint_}"/ "${path_chroot_____}"/
{
for i in $(echo ${rsync_ignore____}) ; do
mkdir -p "$(dirname "${path_chroot_____}$i")"
rm -rf "${path_chroot_____}$i"
mv "${tmp_path}$i" "$(dirname "${path_chroot_____}$i")" \
&> /dev/null; done
}
rm -rf "${tmp_path}"
umount -lf "${path_mountpoint_}" &> /dev/null
# finish
[[ $2 -eq 1 ]] &&
echo "Chroot install/upgrade is complete! Rebooting (120 seconds)..." &&
sleep 120 &&
shutdown -r now
}
# ============================================================================ #
# ==== Chroot ================================================================ #
# ============================================================================ #
# perform /chroot_*/ operations or run a command ----------------------------- #
####
# chroot operations ####################
[[ $@ == *"--backup"* ]] &&
{ chroot_backup ; exit 0; }
[[ $@ == *"--install"* ]] &&
{ chroot_install ; exit 0; }
[[ $@ == *"--restore"* ]] &&
{ chroot_restore ; exit 0; }
[[ $@ == *"--setup"* ]] &&
{ chroot_setup 1 0 ; exit 0; }
[[ $@ == *"--upgrade"* ]] &&
{ chroot_upgrade ${vm_on_upgrade___} 1 ; exit 0; }
# chroot command #######################
chroot "${path_chroot_____}" \
su "${user_chroot_____}" -c "export DISPLAY=:${display_________}; $@"
# ============================================================================ #
# ==== Finish ================================================================ #
# ============================================================================ #
exit 0
####
####