You did know that blogging is vanity, didn't you? Just checking.

How To Move Linux to a New Hard Drive

Date/Time Permalink: 07/26/08 05:49:59 pm
Category: HOWTOs and Guides

It's been a busy week for me. It's been one of those weeks where all the machines in the house gang up on me and demand attention at once. Like the Twilight Zone episode "A Thing About Machines", I've had to carefully negotiate with them, bargaining to keep them patched so I don't have to repair them for just a bit longer.

While I'm at it, let me just mention that I also switched over my ISP. And a big, whooping shout goes out to Iowa Telecom, our new DSL provider, for unflinchingly providing comprehensive tech support to set up my network with their service even after I admitted to openly running Linux! We're set up with their DSL now, which has had no problems at all.

Back to the point of this post, one of the computers ran out of hard drive space. So I had a bigger hard drive with four times the disk space handy, and swapped them. The steps:

1. You'll need to connect the new drive onto the IDE cable. Most IDE cables have two plugs for hard drives. The headache comes with determining which drive is a master and which a slave, so the computer knows which one to boot to. No two hard drive manufacturers have the same standard for setting those tiny little jumper pins on the unit, so you'll have to read whatever diagram they have and fiddle with tweezers moving them around and such.

Also, most PCs allow you to select boot order from the BIOS, or with what order you go by on the cables. Our troubles begin when we have a drive with the pins in the wrong place and a BIOS that disagrees with it. You'll know that you have it all straightened out when you have your original drive as "/dev/hda" and your new drive as "/dev/hdb". Start it up and test for this, then shut it back down.

2. Disconnect the original drive (so you don't accidentally mess it up!) and boot the machine from your choice of any live CD distro or "Tom's root-boot floppy". Tomsrtbt has the benefit of being fast, having all the tools you need for disk maintenance, and virtually guaranteed to run on any old toaster you have. It's saved my bacon dozens of times, so another big, whooping shout out to Tom.

3. If it's a used drive, use "fdisk /dev/hdb" to delete partitions. fdisk is really simple. The commands are all one-letter.

  • m - The "manual", it will print all the commands available.
  • p - Prints the partition table.
  • d - Deletes a partition. It will prompt you for a number, if there's more than one.
  • w - Will write the changes to the drive permanently.
  • q - Quits fdisk.

After this, I always use "dd if=/dev/zero of=/dev/hdb" to wipe the new hard drive completely. If it's factory-new, you can skip these steps. Using "dd" will take a while for a large drive. Figure roughly one hour per ten Gigabytes. Leave it run while you go play Sudoku.

4. Now you will want to set up the new drive's partitions with fdisk. Once again, use "fdisk /dev/hdb" and type your letters; Linux distros will usually need partition #1 to be the boot section, #2 to be swap, and #3 to be the "/" root directory. But these differ. Some distos want to split up the root partition into multiple sub-partitions, with "/usr/" or "/home/" going on a separate partition.

  • n - New partition. You will be prompted to answer whether it's to be a primary or extended partition, enter a partition number, and specify the beginning and ending blocks. For anything less than four partitions per disk, make them all primary.
  • a - make a partition "bootAble". Usually only the first partition - boot, naturally!
  • t - Change a partition's system Type. By default, fdisk on Linux will make every partition type #83 - native Linux. The only one you will have to change is the swap one, to #82.

Here is a typical Linux setup from a different machine:

Disk /dev/hda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hda1   *           1           9       72261   83  Linux
/dev/hda2              10         140     1052257+  82  Linux swap
/dev/hda3             141        1550    11325825   83  Linux
/dev/hda4            1551        9729    65697817+  83  Linux

Check the start and end cylinders for each partition. The boot partition (#1) only needs to go from 1 to about 9, since the boot section only needs a few MBs. The swap partition... is the subject of controversy. Nobody ever knows exactly what to make it. Just take a guess close to what most systems seem to install by default and go from there. The rule of thumb is "twice your RAM size", but in real life I've found that the minimum you need should be about 256MB (1/4th of a Gig) and the most about a Gig. For a home system, anyway.

Because I have to have huge amounts of disk space for home (backing up the website, archiving my graphics art and writing work, emulating systems), I have 62 Gigabytes here for /home/ and only 11 Gigs for the "/" partition, which is extremely generous for most Linux installs even with more stuff on it than anybody would need.

5. Anyway. Now that you have the disk partitioned, make a file system on all partitions with type #83. "mke2fs /dev/hda1" and do the same for /dev/hda3... etc.

6. Now you're ready to shut down, reconnect the main hard drive, and boot back to your main drive. Mount each partition from both disks in tandem - "mount -t ext2 /dev/hda1 /mnt/hda1" and "mount -t ext2 /dev/hdb1 /mnt/hdb1" for instance. Then copy everything from one disk to the other, one set of partitions at a time, repeating for hda3, hda4, etc. If you use "cp -pR /mnt/hda1 /mnt/hdb1", that will copy everything Recursively (R) (all folders, subfolders, and files for the whole file tree), and preserving permissions (p). Do not copy the swap partition, of course!

7. And... one last step. You need to use either Grub or Lilo to write to the Master Boot Record of the new hard drive. I use Grub, since... well, I dunno, I like it! And it's flexible. The only problem with grub is that its syntax is so alien. Here's a good tutorial on Grub. My only habit is to mount the old boot partition and make a copy of the menu.lst file to somewhere else, then after I use Grub to create the bootable record on the new disk, I copy over the menu.lst file to it. Providing, that is, that the old system also used Grub and I'm not changing anything.

8. Finally, shut down yet again, remove the old hard drive, change the new hard drive so it will be the master disk instead of the slave, checking jumper pin arrangement on the drive, cable position on the IDE cable, and BIOS boot order. Congratulations, you did it! But do check to make sure the system is stable and no file data has been lost before you go wiping the old hard drive... this is why we copied files instead of moving them!

This was just a quick overview of the process. There's a lot of details for each of the steps. Consult documentation for specifics, and if you're not at least competent with the tools involved, try practicing on a scratch monkey computer until you get the hang of it without risking your valuable data.

And be sure to check the comments for all of the corrections left by visitors pointing out where I goofed this up!

Another Linux shield

corrections: Commenter andrew scored one point. It's "menu.lst" not "menu.1st". I was thinking "this menu gets shown first" instead of "this is the menu list", I guess?

And I might add... Seeing some of the other comments, *yes*, I am an ancient caveman using stone knives and rock hammers to chip computers out of wood. Of course, wonderful new programs, hard drive platforms, and space-age file systems are out there now. This is just one way to do it. Ogg go back to bang rocks together now.

Follow me on Twitter for an update every time this blog gets a post.
Stumble it Reddit this share on Facebook

suddenly the moon