Last active
September 7, 2020 11:04
-
-
Save hekmon/bb1dca8daaedd4765edd7d74ebe4a63e to your computer and use it in GitHub Desktop.
Quick live rescue chroot script
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 -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