Virtualbox: How to resize a VDI disk

Resizing virtual disks (VDI, Virtual Disk Image) in Virtualbox is still not possible*). There are several rather long tutorials out there how to do this, that's the short version of it:

  • Create a new VDI disk of desired size. We've created a 2GB deb02.vdi, as our 1GB deb01.vdi was too small.

  • Create a new VM, attach both the old (too small) and new (bigger, but still empty) disk to the VM, boot from a bootable CD, e.g. grml.

  • Once booted, we transfer the old disk (sda) to the new, bigger one (sdb):
      $ dd if=/dev/sda of=/dev/sdb bs=1M
      $ sfdisk -R /dev/sdb
    Yes, that's right. We're just copying the whole disk (with its partition table!) to the new disk. I tried to just copy the partition and make it larger with GNU/parted, but it kept barking about unsupported flags on the ext4 partiton (sdb1) and whatnot and I gave up quickly. Anyway, now we have a 2GB sdb with the partitiontable from sda, that is: sdb1 is still 1GB in size, 1GB is unallocated space.

  • Luckily our disklayout was easy enough (and we had a simple MS-DOS partition-table). Thus, we just started cfdisk, deleted sdb1 and created a new sdb1, but filling out the whole disk (2GB).

  • $ sfdisk -R /dev/sdb again to re-read the partition-table.

  • Now that our partition is in good shape, we need to enlarge the peni^W filesystem as well:
       $ e2fsck -vf /dev/sdb1
       $ resize2fs -p /dev/sdb1
    We might have to mount /dev/sdb1 for this, I don't remember.

If all goes well, we should now have a perfectly good sdb, so we could go on and replace the small deb0.vdi VDI disk with the bigger one, deb1.vdi. I've done this a few days ago and I already forgot wether I had to re-install the bootloader. But I'm sure you'll find out if you have to :-)

*) as opposed to e.g. VMware, where it should be possible to resize a virtual disk. I've even done it once :-)