Posts Tagged Linux VirtualBox Clonezilla

How to shrink a disc on VirtualBox

First off I have to admit that this article has a tricky title, it should say someting like “Copying  the content of a big disk into a small one on VirtualBox using rsync”, but because my problem arose from that need I decided to leave it as it is.

The problem

I needed a 600 GB disk virtual machine image cloned with Clonezilla, as shown in the above picture, but the destination was only a 200GB of hard disk. Clonezilla can’t modify the disk’s hardware information, and I could’t find a way to shrink a disk on VirtualBox, even though about 15 GB out og 600 GB were used.  With Clonezilla you can create images from partitions, but when I tried to recover the image, it recognized that the source system had a 600 GB disk and the destination just 200 GB. On the other hand,  the operating system (CentOS 6.0) had LVM, so restoring from partitions didn’t solve the problem.

The solution

Because the generated image was done from a 600 GB hard disk, I came up with the idea of making a smaller disk of 200 GB and install the operating system there, then copy the needed files from the original disk into the smal disk. So on the virtual machine I created a dynamic 200 GB hard disk :

Shrink disk on VirtualBox - Adding small disk

Then I remove the 600 GB disk (on the example, CentOS Server.dvi), leaving the 200 GB disk (SmallDisk.dvi). Later I installed CentOS as I normally would. It’s a good idea to check the installation, I mean, check that the virtual machine boots with the 200 GB hard disk normally.

Once the operating systmen on the 200 GB hard disk has been checked, turn off the virtual machine and add the 600 GB hard disk, but as primary disk (select SATA Port 0 instead of SATA Port 1). The small disk must be set as secondary by choosing SATA Port 1, as shown on the above picture. Then turn on the virtual machine, and as root mount the 200 GB disk:

mount /dev/mapper/VolGroup-lv_root /mnt/lvm2/root/

And now you can copy the content of the big disk into the small one:

rsync -arpz --progress --numeric-ids --exclude=/dev --exclude=/proc --exclude=/tmp --exclude=/boot --exclude=/home --exclude=/etc/fstab  --exclude=/mnt --exclude=/sys / /mnt/lvm2/root/

Note that  /dev, /proc, /sys, /tmp, /boot, /home, /mnt directories and the /etc/fstab file were excluded, because you won’t want to copy that information on the small disk (for instance, you won’t want to copy the  boot loader from the big hard disk into the small one).

Once  the files have been copied, you can turn off the virtual machine, detach the big hard dsik, leaving the small one on SATA Port 0.

As a final note, all were made with CentOS but it will works on Debian and spin-off as well.

No Comments