Still waiting for a medication that treats Attention Surplus Disorder.

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.

13 feedbacks Follow me on Twitter for an update every time this blog gets a post.
Stumble it Digg this Reddit this add to Delicious share on Facebook

Comments:

Comment from: andrew [Visitor]
isn't it "menu.lst" (the letter "l")
Comment from: Penguin Pete [Member] · http://www.penguinpetes.com/
@ andrew

And so it is! Corrected and acknowledged!
Comment from: VonSkippy [Visitor] · http://www.hormel.com
Easier to use either a commercial of open source drive clone and partition utility then go thru all that.

Plus who uses IDE any more? It's all SATA now, and they're jumperless (at least for location - some have a 1.5G vs 3.0G jumper - and they're usually itty bitty tiny jumpers).
Comment from: rcjhawk [Visitor] · http://hawknotes.blogspot.com/
I know it's a graphical front-end and thus not truly geek, but gparted lays the whole disk out in front of you and even does on-the-fly resizing of Windows partitions (back them up first).

Of course, the best thing about a brand new disk is that it's very hard to destroy it, so if you goof up the partitioning you can always start over.

As for swap, I've always heard 2 X memory, which might have made sense when the maximum memory was 256 MB, but seems kind of silly when we routinely have gigabytes of memory just sitting around idle. I suppose if you're running a large code like pwscf you'd want more swap, but I'm the only person I know who does such a thing on an old home computer.
Comment from: Kaboosh [Visitor]
That's funny. All this time I've been doing it this way because it's logical (and over time you learn which shortcuts don't work, i.e. things like dd if=/dev/hda of=/dev/hdb) but I always thought it was too lengthy to be the "right" way. This kind of confirms that the steps are more or less the "best way" to do it. Of course one shortcut that's worked for me is to use tar and ssh instead of cp so I can do it over the network (i.e. tar -C /original/drive/mount/point -cf - | gzip -9 | ssh targethostname 'tar -C /target/drive/mount/point -xpzf -). I've also found that you can more or less safely copy a live system while in use if you do a "bind" mount of the root directory (i.e. mount --bind / /root/clone/mount/point). This mounts without any additional submounts that screw up the process (i.e. /dev, /proc, etc...).
Comment from: Jeff [Visitor]
most linux distributions don't use /dev/hd[a-h] anymore, hard drives now are /dev/sd[a-h]
Comment from: Pikhq [Visitor] · http://pikhq.nonlogic.org/
Something else you can do: make a partition on the new drive for each partition on the old drive. Make these partitions at least as large as (or larger) than the ones on the old drive. Then, for each partition, do:
dd if=/dev/old_partition of=/dev/new_partition
resize2fs /dev/new_partition

Of course, this only applies if you use partitions, instead of the wonderful flexibility that LVM provides. I'd explain LVM, but honestly, that merits a fairly large article to cover well.
Comment from: Bittersweet [Visitor]
I just went through a similar exercise to finally purge my system of Gentoo and Ubuntu and convert to LVM (switching to Arch, plus want some flexibility to do some distro sampling). I tried an alternative to the venerable 'cp'. Using rsync locally to copy the contents of a partition worked quite nicely.

rsync -avHSxP /oldpartition/ /newpartition/

(trailing slashes significant).

rsync FTW!
Comment from: T [Visitor] · http://visio159.com
bookmarked, will come handy when my sucky HDD dies :p

thanks
Comment from: man_iii [Visitor]
Hey ... isnt that ogg comment from "Stig of the Dump" ?

:-) I must be one more cave-beast roams the nights and sleeping in my cave ... banging rocks and living in the bedrock :-P
Comment from: Eric Mesa [Visitor] · http://www.ericsbinaryworld.com
awesome, Pete. I've always wanted to know how to do this.
Comment from: kaylee [Visitor] · http://www.thinkpadonline.info
I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.

kaylee


http://www.thinkpadonline.info
Comment from: Knight [Visitor]
Thanks a lot man.


Your URL will be displayed.
Allowed XHTML tags:
p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, a,
span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite,
abbr, acronym, q, sub, sup, tt, i, b, big, small, pre
Options:
 
(Line breaks become <br />)
(Set cookies for name & url)
images required for CAPTCHA viewing

Alphabet letters and digits 2,3,4,6,7,8,9. The digits 0,1, and 5 are NOT used.

Enter the letters/numbers in the CAPTCHA picture above:
My apology to readers for the hassle. I don't like CAPTCHAs any more
than you do. But we all hate spam even more, and this seems to be an
effective way to stop it.
suddenly the moon