Hack 3. Bypass the Boot Manager
Save your system from a broken or missing kernel.
Of all the fantastic things you can do with your Linux box, one of the most nerve-wracking activities is changing or upgrading a kernel. This not only involves the archaic process of compiling the kernel itself [Hack #88], but it also requires you to update your bootloader so that you can actually use the new kernel. Updating the bootloader often involves editing a complex configuration file in which one wrong change can lead to a system that won't boot. Perhaps nothing is more worrisome in the Linux world than a computer containing all your important work and files that doesn't boot, because you screwed up the bootloader. Worrisome as it is, there is a useful hack you can use to get around the bootloader to boot a working kernel, fix the broken configuration file, and restart a healthy, working system.
1.4.1. Bypassing the Installer
The key to restoring your sanity when confronted with a broken kernel is to use the installation disk you used to install Linux on your computer in the first place. Every installation disk boots a generic kernel from the CD-ROM to perform the installation routine. Many of the install disks for distributions such as Debian, Ubuntu, Mandrake, Red Hat, and others include a slim-line shell called BusyBox that you can use to sneak behind the installer and fix your system.
When you run the CD-ROM-based installation program, nothing on your hard disk is affected until you get to the partitioning part of the program. Just before this section, jump to another virtual terminal by pressing Ctrl-Alt-F(n) (such as Ctrl-Alt-F2). If you are installing Linux on a Mac, this can be a little trickier, because you need to press the Apple Option Key-F(n)-F(n). These key combinations switch you to a blank screen where you see a root shell. This shell is a substantially cut-down version of a normal shell, but it has the essential commands to navigate directories, mount disks, and use an editor to change files.
1.4.2. Mounting Disks
Before you can fix the bootloader, you need to identify the problem that is preventing your system from booting. In many cases, the problem occurs because the kernel files in /boot do not have the same name/location as specified in the bootloader configuration. This can occur if, for example, you specify in your bootloader configuration that your kernel is called vmlinux, but when you installed a new kernel, the original was automatically renamed by the installer to vmlinux.old, thus breaking the bootloader configuration because the bootloader no longer contains the correct kernel name. When this occurs, first you need to access /boot and check the names of your kernel and associated files.
To do this, mount /boot. First, create a new directory for the mount point on your hard disk:
foo@bar:~# mkdir /bootmnt
Now you can mount your hard disk's boot partition using the notation /dev/discs/disc[disk number]/part[partition number]. For example, mount the third partition on the first disk with the following:
foo@bar:~# mount /dev/discs/disc0/part3 /bootmnt
With your boot partition mounted to /bootmnt you can note the names of your kernels and associated files (such as initrd.img files). Now you need to mount the main root partition so that you can modify your bootloader configuration file. First, create the mount point:
foo@bar:~# mkdir /rootmnt
Now mount it:
foo@bar:~# mount /dev/discs/disc0/ part4 /rootmnt
Of course, your root partition might be something other than part4 as specified in this command. With the partition mounted in /rootmnt, you need to change the root partition to that mount point so that you can specify a different root partition from the one the installer is running. This will enable you to access your root partition and run commands from it. To do this, use the chroot command on /rootmnt:
foo@bar:~# chroot /rootmnt
When you have run this command, take a look at the directory structure with ls, and you will see that your normal root partition is visible.
At this point in this process, you have access to your normal range of applications, and if you have a network connection, you can use this opportunity to copy important files and directories to another computer, just in case you can't work out how to fix your kernel or bootloader problem. You can find more information on making network backups with scp and rsync in [Hack #83] .
To restore your system to its previous working form, you can enter /etc and edit the configuration file for the bootloader using the notes of the filenames that you took from the /boot partition earlier. Then run the command to re-create the bootloader (such as lilo or ybin) and reboot the machine.
1.4.3. Hacking the Hack
If you system is failing to boot the kernel, it could be that your kernel has become corrupted or is incorrectly installed. At this point, you should try to reinstall the kernel from a package if one is available. As root, you can do this with an RPM file using the following code:
foo@bar:~# rpm -Uvh kernel.rpm
You can reinstall a Debian package using this code:
foo@bar:~# dpkg -i kernel.deb
Instructions to compile a kernel are in [Hack #88] . You can compile a new kernel from the chrooted environment you used earlier in this hack.