System Recovery

From LQWiki
Jump to navigation Jump to search

System Recovery allows you to make changes to your system when you can't start or boot the computer into it.

You start off by booting the system with a copy of the same Linux distribution on CD/DVD or USB that is installed on the hard drive, mounting your hard drive, entering your local environment, making necessary changes, then unmounting and restarting.

First, boot into the Linux distribution, and get to a root or superuser prompt. Then we'll mount the storage of the damaged system. We assume the primary disk is /dev/sda1 in this example.

cd /
mount -t /dev/sda1 /mnt
mount -t proc proc /mnt/proc
mount -t sysfs sys /mnt/sys
mount -o bind /dev /mnt/dev

If you have other partitions, such as a separate /boot, /home, or other partitions, you must mount these as well. For example, boot is shown as /dev/sda2, and home is shown as /dev/sda3. Make sure you mount all systems necessary before the next step.

mount /dev/sda2 /mnt/boot
mount /dev/sda3 /mnt/home
# (etc)

If you need to perform network access, you'll want to replace the resolv.conf stored in your damaged system with the fresh copy from the recovery system.

cp -L /etc/resolv.conf /mnt/etc/resolv.conf

Then, use the chroot command to spawn a new shell within your mounts.

chroot /mnt /bin/bash

If you'll be working with the bootloader, you can do the following to update the mtab file to make this possible.

grep -v rootfs /proc/mounts > /etc/mtab

At this point, you can rerun your /etc/profile script to set up the environment, and put a reminder that you're in a recovery environment.

source /etc/profile
export PS1="(chroot) $PS1"  # recovery reminder

Now it's possible to complete tasks such as reloading your boot loader, resetting a password, install packages, and other things you would normally need to do to recover a system.

Once you're done, you need to clean up.

exit
umount /mnt/proc
umount /mnt/dev
umount /mnt/sys
umount /mnt/boot
umount /mnt/home
# and any others, then
unmount /mnt

If you get an error, you might want to see if you forgot to unmount another partition or volume.

mount | grep sda

Should show what's still mounted on the internal drive.

Then

reboot