Saturday, June 13, 2009

Clone and Resize Multiple Windows/Linux Partitions for the new Harddisk

One of my old laptop harddisk (250GB) was nearly full, and its partitions were structured like this:
- MBR: grub
- /dev/sda1: Windows XP (fat32, windows boot)
- /dev/sda2: data (ntfs)
- /dev/sda3: Suse 11.0 (ext3)
- /dev/sda4: Linux swap

Getting new harddisk with 500GB, I would like to copy the whole old HD data into the new one with the same structure like this:
- MBR: grub
- /dev/sdb1: Windows XP (fat32, windows boot, with bigger size)
- /dev/sdb2: data (ntfs, with bigger size)
- /dev/sdb3: Suse 11.0 (ext3, with bigger size)
- /dev/sdb4: Linux swap

I am too lazy to wait for the cloning if I have to use dd to read raw image of the entire 250GB of /dev/sda into /dev/sdb. After dd-ing, I will still have to rearrange and resize the partitions to fit them into 500GB new size with partitioner tool like gparted (resizing the partitions in the gparted will need another long time I have to wait as I have 90% data of 250GB size to be re-arranged for /dev/sdb1,2,3,4).

So, in order to speed up the imaging, I prefer to do some manual work like these:
1) Attach the new HD to the laptop as USB storage.
2) Clone the Windows boot partition with dd from /dev/sda1 to /dev/sdb1 (new sdb1 has to be created with same size using Linux fdisk)
3) Resize /dev/sdb1 only (with gparted)
4) Create new empty partition of /dev/sdb2,3,4 manually (using Linux fdisk)
5) Copy the rest of the files manually from old partitions to the newly created partitions (/dev/sdb2,3,4)
   - On /dev/sdb2, use manual dragging from Windows explorer (can also be done from Linux)
   - On /dev/sdb3, use rsync from localbind mount to the target
6) Remove the old HD and attach the new one as SATA.
7) Revive back the grub in the new HD.

Okay done. After doing step #1-5, I could remove my old HD and attached the new one as SATA. With one single drive, my new HD should be seen as "/dev/sda", and the last step to do would be to revive back the grub on the newly cloned HD.

Afterwards, everything was fine (my Suse Linux booted nicely) except the main Windows XP got stuck at blue login welcome screen!

What happened?

Oh, after digging around the web, this seems to be caused by the so called "Windows feature" on memorizing the partition signatures :-(( . It must be due to my itchy hand on step #1, attaching the USB HD from Windows.

So bad. When the Windows assigned wrong drive letter during the bootup, it also created junk files on my new D: drive with folders like "My Documents" and "Program Files".

What could I do next? Do I need to boot my old Windows and clear the partition signature and re-clone again?

I am a lazy guy to wait for another imaging for the whole 250GB, and luckilly I could still boot my linux from the new /dev/sda3 partition.

After booting into Linux, life became easier and I could do the fix with some file cleanup:
- Clear the partition signature: dd if=/dev/zero of=[my new harddisk] bs=446 count=1
   (be extra careful on running this command as it can wipe out your entire disk if it points to the wrong target!)
   (Take note 446 is the safe bootstrap code area to be zero-ed, while 447-512 contain master partition table info and the 0xaa55 signature
   (If you are not familiar with dd, you can also use "fdisk /mbr" from Win98 as per described by the previous link)
- Install back the grub (grub-install /dev/sda)
- Delete all junk folders/files on my new D: drive (/dev/sda2)
- Also, I don't like the folder/files like "System Volume Informations", pagefile.sys, hiberfil.sys (from my previous old HD at /dev/sda1,2), so I deleted them too :-))

What a pain when we have to deal with Windows.
Linux makes our life simpler ;-)