Friday, March 04, 2011

Copying Windows to a new hard drive with dd

Based on the existence of disk cloning software, one might assume that specialized software is needed to move Windows to a new hard drive. However, that's not true; the raw data can simply be copied. My normal personal setup has a small OS partition and a large data partition. I did the copying from Linux (SystemRescueCd) using dd. Here's the procedure I followed, with sda being the new drive and sdb being the old one (don't mix them up; that could cause data loss):
  1. Copy the start sectors, before the first partition: "dd if=/dev/sdb of=/dev/sda bs=512 count=63". I could have just copied the MBR with "count=1".
  2. Run fdisk and delete the data partition from the new drive. As a side effect, this causes Linux to recognize the newly written partition table.
  3. Copy the system partition to the new drive: "dd if=/dev/sdb1 of=/dev/sda1 bs=1M".
  4. Run fdisk and change the ID of the old drive. This is one of the advanced functions. There's a bug in fdisk: it doesn't save the partition table after this. To work around the bug, make another change. I set the type of the old system partition to NTFS hidden.
  5. Reboot into Windows. It should boot off the new drive and mount the partition there as the system partition.
  6. In Windows Disk Management, extend the new partition. Windows can always use more space, and new drives are usually bigger.
  7. Still in Disk Management, create the new data partition.
  8. Copy data to the new data partition.
  9. Remove the old hard drive. It is no longer needed.
If there's no need to resize the system partition and the new drive is the same size or bigger than the old drive, the entire drive can be copied, simply using "dd if=/dev/sdb of=/dev/sda bs=1M".

When working with modern large hard drives and not moving partitions relative to the start of the disk, the NT boot sector doesn't need to be altered. If moving a partition (for example, to align it for Advanced Format on the new drive) it would be necessary to change "Number of Hidden Sectors" at offset 0x1C in the boot sector.

If the new drive fails, and the old drive has to be used again, the procedure is simple: just change the disk ID to its former ID and unhide the system partition.

2 comments:

Unknown said...

i'm using linux and i don't want to install windows to my hard drive. but i want to install windows to external harddrive for games. at normal, we can't use windows to external usb drive. can i use this method to copy windows to external hard drive?

Boris Gjenero said...

You can use this method to copy Windows anywhere. However, it might not boot there. I've successfully booted Windows off an eSATA external drive, but not from USB. You could try some of the stuff I wrote about in my post on moving a Vista partition.