Skip to content

Instantly share code, notes, and snippets.

@hekmon
Last active September 7, 2020 11:04
Show Gist options
  • Save hekmon/bb1dca8daaedd4765edd7d74ebe4a63e to your computer and use it in GitHub Desktop.
Save hekmon/bb1dca8daaedd4765edd7d74ebe4a63e to your computer and use it in GitHub Desktop.
Quick live rescue chroot script
#!/bin/bash -e
ROOT=$1
HOME=$2
if [ ! -b "$ROOT" ]
then
echo "First parameter (/) is not a block device" >&2
exit 1
fi
if [ ! -b "$HOME" ]
then
echo "Second parameter (/home) is not a block device" >&2
exit 2
fi
echo "* Mounting root device"
mount "$ROOT" /mnt
echo "* Mounting /dev"
mount --bind /dev /mnt/dev
echo "* Mounting /proc"
mount -t proc /proc /mnt/proc
echo "* Mounting /run"
mount --bind /run /mnt/run
echo "* Mounting /sys"
mount -t sysfs /sys /mnt/sys
echo "* Mounting /home"
mount "$HOME" /mnt/home
echo "* Temporarily superseeding resolv.conf"
mv /mnt/etc/resolv.conf /mnt/etc/resolv.conf.bak
cp /etc/resolv.conf /mnt/etc/resolv.conf
echo "* Changing root"
echo
chroot /mnt /bin/bash
echo
echo "* Restoring resolv.conf"
rm /mnt/etc/resolv.conf
mv /mnt/etc/resolv.conf.bak /mnt/etc/resolv.conf
echo "* Unmounting /home"
umount /mnt/home
echo "* Unmounting /sys"
umount /mnt/sys
echo "* Unmounting /run"
umount /mnt/run
echo "* Unmounting /proc"
umount /mnt/proc
echo "* Unmounting /dev"
umount /mnt/dev
echo "* Unmounting root device"
umount /mnt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment