Linux Annoyances for Geeks: Getting the Most Flexible System in the World Just the Way You Want It

8.3. A Windows User Reinstalled Windows on a Dual-Boot System

Microsoft Windows power users may reinstall that operating system periodically. Reinstallation can provide fresh, unfragmented copies of key utilities, driver libraries, and more. Furthermore, Microsoft Windows power users work hard to deal with the fragmentation associated with Microsoft filesystems. While Microsoft provides defragmentation utilities, they are not completely reliable, in my opinion. Reinstallation is often the easiest solution. Unfortunately, if a user reinstalls Windows on a dual-boot computer, it overwrites any existing Linux bootloader such as GRUB or LILO, and possibly the Linux portion of the dual boot.

This annoyance assumes you know the basics of configuring and formatting hard drives and associated partitions with the fdisk utility. More information is available on any standard Linux book, including Running Linux by Matthias Kalle Dalheimer and Matt Welsh (O'Reilly).

8.3.1. Restoring the Bootloader

The first thing to check in this situation is whether Linux is still there. You won't be able to boot Linux from a Microsoft Windows bootloader, at least just after Windows is reinstalled. So you'll need a rescue disk of some sort to boot into Linux. There are three basic scenarios:

  • You have a rescue disk customized for your configuration. In this case, it should include a bootloader, and you should be able to boot Linux and proceed directly to step 7 in the following list.

  • You have the first installation CD associated with Red Hat/Fedora. If you boot from the CD, and type linux rescue at the boot: prompt, it searches through the existing partitions for your Linux installation. Once you reach the Rescue screen, click Continue. If it finds an existing system, you'll be taken to a prompt. At that point, the following command puts the system in a state where it treats the old Linux partition as the root of the filesystem, after which you can restore your bootloader. (If you need more information on this process, see the "My Server Is So Secure I Can't Log In as Root" annoyance in Chapter 6.)

    chroot /mnt/sysimage

    Afterward, you can proceed to step 7 in the following list.

  • You need to boot from a different rescue CD, such as the Debian from Scratch CD, the first SUSE installation CD, or the Knoppix CD. I'll detail the procedure here, based on the Knoppix CD. The procedure with the other CDs is similar.

The steps for recovering Linux are as follows:

  1. Set your system to boot from the CD/DVD drive, and start your computer.

  2. If you're using a Knoppix CD, it should normally boot directly into the Knoppix KDE desktop environment, where you can start a command-line console.

  3. At the command line, use the su command to log in as the root user. The rescue CDs do not require a password for root access.

  4. Take stock and find the partitions on your hard drive with the fdisk -l command. If you use more than one partition for your Linux system, some trial and error may be required. Sometimes you can make intelligent guesses based on the output. For example, I can surmise from the system, the devices, and the sizes of the partitions that my /boot directory is associated with partition /dev/hda6 and my top-level root (/) directory with /dev/hda7:

    Device Boot Start End Blocks Id System /dev/hda1 * 1 2676 21494938+ 7 HPFS/NTFS /dev/hda2 2677 4136 11727450 c W95 FAT32 (LBA) /dev/hda3 4137 7296 25382700 5 Extended /dev/hda5 4137 4259 987966 82 Linux swap /dev/hda6 4260 4273 112423+ 83 Linux /dev/hda7 4274 5247 7823623+ 83 Linux

  5. Mount the partitions associated with the top-level root (/) directory. At this point, if you're uncertain about other partitions, you'll be able to find them in your normal fstab configuration file. If you mount your root (/) directory on /mnt/hda7, for instance, you'll find that file in /mnt/hda7/etc/fstab:

    mount /dev/hda7 /mnt/hda7

    If you can't find your fstab, you've probably mounted the wrong partition as the root (/) directory and need to try again.

  6. Set up your filesystems. The following commands are based on the output from step 4, and assume that the contents of your /boot directory are actually located on /dev/hda6:

    mount /dev/hda6 /mnt/hda7/boot chroot /mnt/hda7

  7. Now restore your original bootloader to the MBR with one of the following two commands; the command that you use depends on whether you use GRUB or LILO:

    grub-install /dev/hda lilo

  8. Finally, reboot the computer. If the bootloader pointer is now on the MBR, you'll be able to boot into either operating system.

8.3.2. Recovering from Backup

If the steps in the previous section failed because you couldn't find your Linux partitions, it means the Windows installation might have wiped out the partitions in addition to the bootloader. In any case, at this point you need to restore Linux from a backup. What you need to do depends on the backup and the status of the partitions. Because Linux isn't available on the local computer, you'll need to boot with a CD-based distribution. We'll use a Knoppix CD for this purpose:

  1. Set your system to boot from the CD/DVD drive, and start your computer. If you're restoring from an external drive connected via IEEE1394 or USB2, and are using an older Knoppix CD/DVD, use the knoppix26 command at the boot: prompt.

  2. If you're using a Knoppix CD, it should normally boot directly into the Knoppix KDE desktop environment, where you can start a command-line console.

  3. At the command line, use the su command to log in as the root user. No password is required.

  4. Check the current state of your partitions with the fdisk -l command. If you see partitions still configured for Linux, as shown in the previous section, skip to step 7.

  5. If you no longer have Linux partitions, you'll need to back up the current Microsoft Windows operating system. Use the techniques described in "I'm Afraid of Losing Data" in Chapter 2.

    If you have a single partition with Microsoft Windows, you may be able to save it using the parted utility without restoring it from backup. parted can even resize NTFS partitions. For more information, see http://www.gnu.org/software/parted/parted.html. Alternatively, Knoppix includes the QTParted utility, which is a GUI frontend to parted.

  6. Now re-create the partitions that you need. For example, if the drive is /dev/hda, use the fdisk /dev/hda command. Make sure that the partition types correspond to the required filesystems, such as Linux, W95 FAT32 (LBA), or HPFS/NTFS. The size depends on the amount of data in your backup.

  7. Format each partition in the desired filesystem format. For example, if you're formatting partition /dev/hda7 for the ext3 filesystem, use the following command:

    mkfs.ext3 /dev/hda7

  8. Restore Linux from your backup. If possible, use the techniques described in Chapter 2. If you had to use the dd_rescue command to create an image, you can restore your system with the dd command, as described in the previous annoyance.

  9. Don't forget to restore your bootloader; we described one technique for that in the previous section.

Категории