Clone and Shrink Linux CentOS 6 installation

At long last I was able to clone and shrink my Linux installation. This all came about when, originally, I created a gigantic virtual drive and later realized I did not need it all. It was excessive.

I did this on a CentOS 6 virtual machine sitting on Microsoft Hyper-V. My original VHD was 50Gb and I was using 3Gb of that. So, I created another VHD that was 20Gb. Incidentally, the original, 50Gb VHD was static and the replacement, 20Gb VHD was dynamic.

Also, in order to get started, I downloaded System Rescue CD 3.8.1 from:
Code:

and inserted it into my VM's optical drive.

Next, I attached my 20Gb dynamic VHD to IDE0 (making it sda) and my original, 50Gb static VHD to IDE1 (making it sdb).

I powered on the VM, booting to System Rescue CD 3.8.1. I had to type "startx" to get the GUI to come up and once it did, I found GPartd in the lower left. I launched it and was defaulted to the 20Gb dynamic VHD/the desired destination for this exercise. I used "Device Menu/Create Partition Table" and answered affirmatively to the prompts.

Continuing with GPartd, I accessed "Partition/New" and created a 15Gb primary partition of type "ext2". I repeated this action for a second primary partition of type "linux-swap" to finish out the drive. I right clicked the 15Gb partition, chose "Manage Flags" and ticked "boot". Then I clicked the green check at the top to commit the changes.

Also near the top, and to the right, I was able to choose the other drive- sdb. This is the source, 50Gb static. GPartd showed this drive's partitions to me, and I right clicked the large data partition I wished to shrink, then left clicked "Resize/Move" from the resulting menu. I had GPartd shrink this partition to the exact same size as I had entered for the destination previously. Again, I clicked the green check at the top to commit the changes. Then I closed GPartd.

Next, in the shell of SystemRescueCD, I entered the following commands:
Code:

cd /mnt
mkdir sda
mount /dev/sda1 sda
fsarchiver savefs /mnt/sda/source.fsa /dev/sdb1

That last command can take some time depending on the scale of what you're doing here. In any event, in my case, the command returned without errors, allowing me to proceed with my next batch of commands:
Code:

mkdir sdb
mount /dev/sdb1 sdb
mv sda/source.fsa sdb/
umount sda
fsarchiver restfs /mnt/sdb/source.fsa id=0,dest=/dev/sda1
poweroff

The gist of this is to mount the destination, output the source's partition on the destination, mount the source, move that output partition across, unmount the destination, and finally write the outputted partition to the destination.

At this point, the original, 50Gb static VHD should exist on the new, 20Gb dynamic VHD. So, the original, 50Gb (IDE1/sdb) can be detached from the VM.

Now there are some final steps, reinstalling grub and setting the swap file up in fstab. To reinstall grub, I booted to the CentOS disc/image and chose "Rescue installed system". I followed the prompts until I could get a shell and I issued:
Code:

chroot /mnt/sysimage
grub-install /dev/sda1

Now we have a booting system. At it's command prompt, I typed:
Code:

blkid

and I meticulously took down the UUID of the swap partition. I edited /etc/fstab to reflect this UUID and I was done.

Now I have successfully cloned and shrunk my install! If only I hadn't created such a monster in the first place...