#!/bin/bash target="${1%/}" [ -z "$target" ] && { echo "Wrapper for chroot to set up variables and after use cleanup." echo "Usage: $0 target [script]" 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 } script="${2%/}" [ ! -z "$script" ] && { if [ -e "$script" ] ; then if [ -x "$script" ] ; then echo "Using script: $script" [ -L "$script" ] && { echo "Warning: Script $script is a symlink. Proper symlink handling is not yet implemented" } else echo "Error: Script $script is not an executable" exit 1 fi else echo "Error: Script $script does not exist" exit 1 fi } echo Setting up chroot... mount -v --rbind /dev $target/dev mount -v --make-rslave $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 [ ! -z "$script" ] && cp -L "$script" "$target/root/tmp/$(basename "$script")" echo Entering chroot... chroot $target /bin/bash -c "echo Initial cleanup find /run -not -path /run -print -delete find /proc -not -path /proc -print -delete find /sys -not -path /sys -print -delete find /tmp -not -path /tmp -print -delete [ -d /var/tmp ] && find /var/tmp -not -path /var/tmp -print -delete echo Setting up mounts 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 echo Setting up variables 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 [ ! -e \"/etc/resolv.conf\" ] ; then echo Creating new resolv.conf file if [ -L \"/etc/resolv.conf\" ] ; then file=\$(readlink \"/etc/resolv.conf\") echo Creating dir \$(dirname \$file) mkdir -p \$(dirname \$file) echo Creating file \$file touch \$file fi 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 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 if [ ! -z \"$script\" -a -e \"/root/tmp/\$(basename \"$script\")\" ] ; then script=\$(basename \"$script\") echo \"\$(basename \"$script\")\" > /tmp/chroot_script echo Running /root/tmp/\$script /root/tmp/\$script echo Cleaning temporary files... rm /tmp/chroot_script rm /root/tmp/ -rf else echo Cleaning temporary files... rm /root/tmp/ -rf #if [ -n \"$user\" ]; then # echo Running shell for user $user... # su - $user #else echo Running root shell... su - #fi 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 }