Hardware Installation Notes

Installing a New Disk on Linux

Installing a new, additional disk or hard drive on your Linux system can be quite easy. Unfortunately, there are any number of little glitches that can occur along the way that can, quite literally, render your system unbootable. So, what should be a simple process can become a nightmare.

Herein, we outline the steps to follow to ensure a disaster-free installation of a new, additional disk (i.e. in addition to your current system disk).

  1. Do yourself a huge, huge favaor and change the boot loader to not use the volume label to boot from. This is a stupid feature, at best. And, under many circumstances, it can be an unmitigated disaster. Essentially, someone decided that it would be a really clever idea if the boot loader could decide which file system to mount on root, based on the partition's label (a.k.a. the volume label). It isn't. Its a dumb, dumb, dumb idea.

    The boot loader must boot from the fixed device that it is told to boot from (e.g. sda or hda). Presumably, the partitions on it are also fixed so why not always mount the same, fixed partition on root as well? Sadly, most OS distros don't come configured this way anymore. Instead, they boot from the fixed device and then go looking for a partition named "/". If you accidentally add a new drive that has a partition labeled "/" (e.g. a drive you are reusing from somewhere else) or if you inadvertently label a partition on the new drive as "/", you are screwed. You'd think that the loader would first look for the partition labeled "/" on the device it booted from. Not happening. For some reason, it has the uncanny knack of picking the wrong disk with 100% accuracy. Quite impressive but your boot sequence is going nowhere, since the loader will try to load the rest of the OS from the partition on the new disk. Even if you do luck out and hit a working OS, it will be on the wrong disk.

    If you are using GRUB, the change is quite easy to make. Just open up either /boot/grub/grub.conf or /etc/grub.conf (its a symlink to /boot/grub/grub.conf) and add a few lines for the new boot choice. You can copy what's there (I make the copy the first choice and, hence, the default). Change its name and replace the "LABEL=/" string with "/dev/sda3" or whatever your root patition is. Here's an example of how the GRUB config for CentOS was hacked:

    title NoLabel
        root (hd0,0)
        kernel /vmlinuz-2.6.18-92.1.22.el5 ro root=/dev/hda3 \
          rhgb quiet crashkernel=128M@16M
        initrd /initrd-2.6.18-92.1.22.el5.img
    title CentOS (2.6.18-92.1.22.el5)
        root (hd0,0)
        kernel /vmlinuz-2.6.18-92.1.22.el5 ro root=LABEL=/ \
          rhgb quiet crashkernel=128M@16M
        initrd /initrd-2.6.18-92.1.22.el5.img

    I added the "NoLabel" choice to allow me to boot without the stupid partition label matching feature. You might also want to change the "timeout" parameter to give you time to make a choice. Most OS distros set it to five seconds, which may be too short.

    For GRUB, that's all there is to it. GRUB reads the config file on startup and does the rest. If you are still using LILO, you'll have to figure out how to hack its config file and then rebuild the boot sector that contains the config information. You're on your own.

  2. Install the new disk in your machine. Be aware that some SATA controllers carry over the idiotic notion of master and slave from IDE days. If your motherboard has such a controller, you might find that SAT0 = sda, SATA1 = sdc, SATA2 = sdb, and SATA3 = sdd or some other screwy ordering like that. Also, the SATA drives might just collide with IDE devices such as a DVD or CD drive.
  3. Boot the system and make sure that you have the new disk installed in the right spot. You can double check (for example /dev/sdb) with:

    /sbin/fdisk -l /dev/sdb

  4. Create the partitons on the new disk. To do this, use fdisk in interactive mode (you could also use cfdisk, although you're on your own there):

    /sbin/fdisk /dev/sdb

    There are number of simple commands that you can use: "m" - gives help; "d" - deletes a partition; "n" - creates a new one. Presumably, you are just going to need one partition but you may have to delete old ones, if you are reusing the disk. The "n" command will use the first available cylinder for the start of the partition and the last available cylinder for the end, by default.

    When you are done partitioning the disk, the "w" command writes the new partition table and exits. If you change your mind, "q" exits without saving.

  5. The new partiton must be formatted for the file system of your choice. Here, we're assuming ext3 but, if you do "man mkfs" you can read about the others. To create a new ext3 file system, do:

    /sbin/mkfs.ext3 -j -T news /dev/sdb1

    You'll want to think very carefully about using the "-L" (label) parameter. If you make a mistake and add the wrong label to the partition, you can remove it using the "-L" parameter of tune2fs but you'll have to boot the system without mounting the partition (which may require the use of a standalone system such as Knoppix) and there doesn't seem to be any percentage to adding a label in the first place. And, under no circumstances should you use something like "-L /" as this can easily result in the boot loader trying to boot or mount the wrong partition.

    Incidentally, if you are not enamored of the fact that fsck, by default, is going to check the file system every so often, thereby delaying your boot, usually when its least convenient, you should change the check parameters like this:

    /sbin/tune2fs -c 0 -i 0 /dev/sdb1

    Once you are all done, you can verify the new file system with:

    /sbin/dumpe2fs -h /dev/sdb1

  6. At this point, after the new file system has been built, I boot the system to make sure I haven't screwed anything up (i.e. it will still boot). You don't have to take this step but, if the new disk prevents you from booting, you can just unplug it and try booting again. Once you add it to fstab, it had better be working or your system won't be booting.
  7. In order to add the new disk, you must create a mount point somewhere in the existing file system. The mount point is simply an empty directory. For example, to mount it under a new "/users" directory:

    mkdir /users

  8. The last step is to add the new disk to /etc/fstab so that the system will mount it. Here is an example of the entry for a new sdb:
    /dev/sdb1     /users     ext3     defaults     0 2

    After you've added the new drive to fstab, you can check that it mounts OK with:

    mount -a

  9. A reboot of the system, to make sure that everything works on autopilot is now in order. You shouldn't experience any problems if "mount -a" worked OK.
  10. And, to put things back exactly the way they were, once you are sure that the system boots OK with the new drive in place, you should hack the GRUB configuration (in /etc/grub.conf or /boot/grub/grub.conf) to remove the special, no-label boot choice that you added in Step 1.

So, what do you do if you screw something up and the system won't boot? Well, despite the fact that it says "Kernel Panic", you shouldn't. The most common problem is that the loader boots from the wrong root partition. And, this is caused by the "LABEL=" parameter. Here's how to recover from it.

  1. To get rid of the "Kernel Panic", simply unplug the new device. The loader can't erroneously load from it if its not plugged in.
  2. If you added the disk to the fstab, your system will now fail to boot because it can't mount the new drive. Instead it will boot into single user mode. Supply the system password to enter single user mode.
  3. The root file system is cleverly mounted read only so you can't fix it. Remount it read/write so you can:

    mount -o remount,rw /

  4. Fire up your favorite text editor and remove the offending line from /etc/fstab.
  5. Reboot the system and you should be fine. Go back and reread the part about disabling the stupid boot-from-labeled-partition feature and/or the part about not labeling the disk "/". Fix your mistakes and try again.

An indispensible tool that can be used for creating new partitions and formatting them (or recovering from mistakes) is the standalone, single-CD bootable Linux variant called Knoppix. Using Knoppix, you can set up and initialize a new disk before it is ever installed in the target system. You can even use it to copy all of the data off of an original, system disk to the new disk without worrying about whether the data is changing while the system is running.

With the new and old disks installed in a machine that has a CD drive, the Knoppix CD is booted. You can use Knoppix to create the new partition(s) and initialize them. Once this is done, the new partition(s) can be populated using dd (if their size isn't to be changed) or they can be mounted, along with the original partition and copied as follows:

cp -R --preserve=all /media/sda1 /media/sdb1

In this case, the first partions on the sda and sdb devices are being copied. It may take a long time for this copy to proceed but it always works, including when the file system type is being changed (e.g. from resierfs to ext3) or the partition size is being altered (e.g. from smaller to larger on an XFS partition, which is not supported by many disk reorganization programs).