Introduction to Linux Rescue and Recovery
I’ve seen this go wrong when a Linux system becomes unbootable - it can be a real nightmare. Whether it’s a failed update, a misconfigured kernel, or a corrupted filesystem, having a reliable method for recovery is crucial. One of the most effective ways to rescue a borked Linux boot is by using a USB rescue drive in combination with the chroot command. This approach allows you to access and repair your system from a safe environment.
Preparing the USB Rescue Drive
The first step in the recovery process is to create a USB rescue drive. I usually start with a lightweight Linux distribution that includes essential tools for rescue and recovery operations. Some popular choices include SystemRescue and Ubuntu Server.
To create the USB drive, you can use tools like dd or a graphical utility such as Rufus on Windows or Etcher on Linux and macOS. The command to create a bootable USB drive using dd is as follows:
sudo dd if=/path/to/your/linux.iso of=/dev/sdX bs=4M status=progress oflag=sync
Replace /path/to/your/linux.iso with the path to your Linux ISO file, and /dev/sdX with the device identifier of your USB drive (e.g., /dev/sdb). Don’t bother with the dd command if you’re not comfortable with it - the graphical tools are often easier to use. Be cautious when using dd, as specifying the wrong device can result in data loss.
Booting from the USB Drive
Once the USB drive is prepared, insert it into the computer with the borked Linux installation and boot from it. You may need to enter the BIOS or UEFI settings to set the USB drive as the first boot device. The process for accessing these settings varies by motherboard but typically involves pressing a key like F2, F12, or Del during boot. In practice, this is usually the easiest part of the process.
Identifying and Mounting the System Partitions
After booting from the USB drive, you’ll need to identify and mount the partitions of your Linux system. Use the lsblk or fdisk command to list the available disks and partitions:
lsblk
or
sudo fdisk -l
Once you’ve identified the partitions you want to mount (typically the root / partition and possibly a separate /boot or /home partition), you can mount them using the mount command. For example:
sudo mount /dev/sda1 /mnt
This command mounts the first partition of the first SATA disk (/dev/sda1) to the /mnt directory. If your system has a separate /boot partition, you’ll need to mount it as well:
sudo mount /dev/sda2 /mnt/boot
Replace /dev/sda1 and /dev/sda2 with the appropriate device identifiers for your system’s root and boot partitions. This is where people usually get burned - make sure you’re mounting the right partitions.
Chrooting into the System
With the necessary partitions mounted, you can use the chroot command to change the root directory of your session to the mounted system. This allows you to run commands as if you were logged into the system normally:
sudo chroot /mnt /bin/bash
This command changes the root directory to /mnt (where your system is mounted) and opens a new bash shell. From this point, you can perform repairs, such as reinstalling the bootloader (e.g., GRUB), fixing package issues with apt or dnf, or correcting configuration files. The real trick is to remember that you’re now operating within the mounted system, not the live environment.
Reinstalling the Bootloader
One common reason for a system becoming unbootable is a malfunctioning or missing bootloader. To reinstall GRUB, the most commonly used bootloader on Linux systems, you can use the following command within the chroot environment:
grub-install --recheck /dev/sda
Replace /dev/sda with the device identifier of the disk where you want GRUB installed (not the partition, e.g., /dev/sda1). After installing GRUB, update the GRUB configuration:
update-grub
Troubleshooting and Additional Steps
The steps outlined above cover the basic process for recovering a Linux system using a USB rescue drive and chroot. However, the specifics can vary depending on your system’s configuration and the nature of the problem. It’s essential to be prepared to troubleshoot and adapt these steps as necessary. For example, if your system uses LVM (Logical Volume Management) or encryption, you’ll need to take additional steps to unlock and mount these volumes before you can chroot into the system. The cryptsetup command is used for encrypted volumes:
sudo cryptsetup luksOpen /dev/sda2 cryptroot
And then you can mount the decrypted volume.
Further Reading
Recovering a borked Linux boot with a USB rescue drive and chroot is a powerful technique that can save you from a lot of trouble. By understanding how to prepare a rescue drive, boot from it, identify and mount system partitions, and chroot into the system, you gain the ability to fix a wide range of problems that might otherwise require a full system reinstall. For more detailed information on Linux system administration and recovery, consider visiting the official Debian documentation or the Arch Linux Wiki.
See also
- Taming Noisy Systemd Logs with Journalctl Filters and Log Rotation Tweaks
- Taming Log Noise with journalctl: Filtering Out the Chaff to Find Real Issues
- Taming the initramfs: How to Debug and Optimize Your Linux Boot Process
- Taming Disk Usage with btrfs Snapshots and Automatic Pruning
- Taming Resource-Hungry Containers with cgroups and Podman