These days I’ve been playing for a while with VirtualBox 3.0. Though usually I use quite a lot KVM, I’ve found VirtualBox very interested too.
But as probably other users have suffered, I made a miscalculation when creating a machine, and after installing the whole operating system I realized that assigned disk size was not enough.
So how to resize a VirtualBox Disk Image (from now on, VDI)?. VirtualBox doesn’t come with appropriate tools, and after googling for a while, all solutions proposed to use a live CD to resize the partition.
Well, actually resizing can be done with VirtualBox tools, but using some workarounds. I don’t know if this is new in version 3.0, but really I don’t care.
Steps to resize are described below. In this example, current VDI has an ext3 partition with 4 Gb, and the aim is to extend it up to 6 Gb.
$ VBoxManage clonehd image.vdi image.raw --format RAW
$ rm image.vdi
$ dd if=/dev/zero of=extension.raw bs=1G count=2
$ cat extension.raw >> image.raw
$ rm extension.raw
Now we have a disk of 6 Gb, but internal partition is still of 4 Gb. We’ll use a KVM tool to mount the partition in order to resize it.
$ qemu-nbd image.raw &
$ sudo nbd-client localhost 1024 /dev/nbd0
$ sudo fdisk /dev/nbd0
At this point, we need to delete the current partition and create a new one that starts in the same cylinder and ends in the last one, keeping the same format. Don’t forget to mark it as bootable. Then, save and exit. This doesn’t destroy partition content, but re-creates it. Now we can extend it.
$ sudo resize2fs /dev/nbd0p1
$ sudo fsck.ext3 /dev/nbd0p1
$ sudo nbd-client -d /dev/nbd0
Finally, we’ll convert back the image.
$ VBoxManage convertfromraw image.raw image.vdi
$ rm image.raw
And that’s all. Requirements? Free space in disk and free time!