Last active
December 3, 2020 13:34
-
-
Save juozaspo/68f55bfd699930f84812913ca9341a75 to your computer and use it in GitHub Desktop.
Wrapper for automating various common jobs while chrooting offline debian based system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
target="${1%/}" | |
[ -z "$target" ] && { | |
echo "Wrapper for chroot to set up variables and after use cleanup." | |
echo "Usage: $0 target" | |
exit 1 | |
} | |
target="$(realpath "$target")" | |
[ -d "$target" ] || { | |
echo "Target $target does not exist or is not a directory"; | |
exit 1 | |
} | |
[ -d "$target/dev" ] && [ -d "$target/run" ] && [ -d "$target/proc" ] && [ -d "$target/sys" ] && [ -d "$target/tmp" ] || { | |
echo "Required directories (dev,run,proc,sys,tmp) missing in $target" | |
exit 1 | |
} | |
echo Setting up chroot... | |
mount -v --bind /dev $target/dev | |
#mount -v --bind /dev/tty $target/dev/tty | |
echo Copying files... | |
mkdir -p $target/root/tmp | |
cp -L /etc/resolv.conf $target/root/tmp/resolv.conf | |
[ -e "/run/systemd/resolve/stub-resolv.conf" ] && cp -L /run/systemd/resolve/stub-resolv.conf $target/root/tmp/stub-resolv.conf | |
[ -e "/run/resolvconf/interface/systemd-resolved" ] && cp -L /run/resolvconf/interface/systemd-resolved $target/root/tmp/systemd-resolved.interface | |
echo Entering chroot... | |
chroot $target /bin/bash -c "mount -vt tmpfs none /run | |
mount -vt proc none /proc | |
mount -vt sysfs none /sys | |
mount -vt devpts devpts /dev/pts | |
mount -vt tmpfs none /dev/shm | |
mount -vt tmpfs none /tmp | |
[ -d /var/tmp ] && mount -vt tmpfs none /var/tmp | |
export LC_ALL=C | |
echo $target > /etc/debian_chroot | |
[ -h /var/lock ] && mkdir -vp \"\$(readlink -f /var/lock)\" | |
if ( type dbus-uuidgen >/dev/null 2>&1 ) ; then | |
if [ ! -f \"/var/lib/dbus/machine-id\" ] ; then | |
echo Generating dbus machine id. | |
dbus-uuidgen --ensure | |
else | |
echo Skipping dbus machine id generation. | |
fi | |
else | |
echo No dbus-uuidgen on this system. | |
fi | |
if ( type systemd-machine-id-setup >/dev/null 2>&1 ) ; then | |
if [ ! -f \"/etc/machine-id\" ] ; then | |
systemd-machine-id-setup | |
else | |
echo Skipping systemd machine id setup. | |
fi | |
else | |
echo No systemd-machine-id-setup on this system. | |
fi | |
if ( type resolvconf >/dev/null 2>&1 ) ; then | |
echo Setting up resolvconf | |
mkdir /run/resolvconf | |
#ln -s /run/resolvconf /etc/resolvconf/run | |
resolvconf --enable-updates | |
if [ \"\$(ls -A /run/resolvconf/interface/)\" == \"\" ] ; then | |
echo No interfaces found: Adding one | |
if [ -f /root/tmp/systemd-resolved.interface ] ; then | |
cp -v /root/tmp/systemd-resolved.interface /run/resolvconf/interface/systemd-resolved | |
else | |
cat /root/tmp/resolv.conf > /run/resolvconf/interface/systemd-resolved | |
fi | |
fi | |
resolvconf -u | |
elif ( type systemd-resolve >/dev/null 2>&1 ) ; then | |
echo Running workaround for systemd-resolve | |
mkdir -p /run/systemd/resolve/ | |
cp /root/tmp/stub-resolv.conf /run/systemd/resolve/ | |
else | |
if [ ! -e \"/etc/resolv.conf\" ] ; then | |
echo Creating new resolv.conf file | |
cp /root/tmp/resolv.conf -v /etc/resolv.conf | |
elif [ -f /etc/resolv.conf ] ; then | |
echo *Found an old resolv.conf on $target* | |
du -h /etc/resolv.conf | |
cat /etc/resolv.conf | |
echo *To be replaced with* | |
du -h /root/tmp/resolv.conf | |
cat /root/tmp/resolv.conf | |
cp -vi /root/tmp/resolv.conf /etc/resolv.conf | |
fi | |
fi | |
if ( type dhclient >/dev/null 2>&1 ) ; then | |
read -p \"Dhcp client detected. Enter \\\"y\\\" to run: \" ch | |
[ \"\$ch\" == \"y\" ] && dhclient -v | |
else | |
echo No dhclient on this system | |
fi | |
echo Cleaning temporary files... | |
rm /root/tmp/ -rf | |
if [ -n \"$2\" ]; then | |
echo Running shell for user $2... | |
su - $2 | |
else | |
echo Running root shell... | |
su - | |
fi | |
echo Cleaning up... | |
if [ -f \"/var/lib/dbus/machine-id\" ] ; then | |
read -p \"Dbus machine id file present. Enter \\\"y\\\" to delete it: \" ch | |
[ \"\$ch\" == \"y\" ] && rm -vf /var/lib/dbus/machine-id | |
fi | |
if [ -f \"/etc/machine-id\" ] ; then | |
read -p \"Systemd machine id file is present. Enter \\\"y\\\" to delete it: \" ch | |
[ \"\$ch\" == \"y\" ] && rm -vf /etc/machine-id | |
fi | |
if [ -f \"\$HOME/.bash_history\" ] ; then | |
read -p \"Bash history file is present. Enter \\\"y\\\" to delete it: \" ch | |
[ \"\$ch\" == \"y\" ] && rm -vf \$HOME/.bash_history | |
fi | |
rm /etc/debian_chroot | |
[ -d /var/tmp ] && umount -lfv /var/tmp | |
umount -lfv /tmp | |
umount -lfv /dev/shm | |
umount -lfv /dev/pts | |
umount -lfv /sys | |
umount -lfv /run | |
umount -lfv /proc | |
echo Leaving chroot..." | |
#umount -lfv $target/dev/tty | |
umount -lfv $target/dev | |
rm -rfv $target/run/* | |
pstokill="$(lsof 2>/dev/null | grep $target | tr -s "[:space:]" | grep -v ^chroot\- | cut -d\ -f2 | uniq )" | |
[ -z "$pstokill" ] || { | |
echo Terminating remaining processes... | |
kill $pstokill | |
} | |
pstokill="$(lsof 2>/dev/null | grep $target | tr -s "[:space:]" | grep -v ^chroot\- | cut -d\ -f2 | uniq )" | |
[ -z "$pstokill" ] || { | |
echo Killing remaining processes... | |
kill -9 $pstokill | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment