Monday, January 12, 2009

Resize a VirtualBox VDI

Recently we wanted to create a series of vdi images. Each image would be built with a minimum disk size and increase. The issue we discovered however is increasing the size of the vdi files as we migrate to each successive image.

Here is a simple solution from the Crooked Spoke blog.

Since the blog is now offline swiped the original post from the Wayback machine.

There are quite a few advantages to using VirtualBox, an open source virtualization software, over the industry-standard VMWare, not the least of which is that the former doesn’t nag you for serials.

Unfortunately, VirtualBox is lacking in some functionalities I’d like - one of them is to resize a VDI (virtual disk image) after you’ve created it. I ran into a problem when I once created a 6GB disk image for a virtual install of Fedora 8 on a Windows XP host. I got to the point where it needed more space. Unfortunately, this is not a built-in functionality in VirtualBox.

Fortunately, there is a way around it. What you’ll need:

•A Gparted LiveCD, or some similar rescue CD or LiveCD

◦VirtualBox (and to be fair some other virtualization software) allows you to treat raw ISOs stored on your hard drive as virtual optical disks for the guests. This is very flexible and convenient - the Guest VM will “see” a regular CD or DVD. Further, this is probably faster than running through an optical drive.

◦Specifically, any LiveCD with the Linux utility dd will suffice for the first part (copying the existing data from one VDI to another), but you’ll need some sort of partition resizing software for the second part (setting your virtual machine’s OS to use all of the new, bigger virtual disk). For that reason, I suggest Gparted or any other rescue disk with both dd and partition resizing software.

•Two virtual disk images

◦The old one (the one that you’ve found is too small) with an operating system

◦The new one (made with a larger size); it should be “raw” (unpartitioned)

The general steps are fairly simple if you’re already familiar with Linux and VirtualBox. Sorry - if you’re looking for lots of screenshots and hand-holding, look elsewhere.

1. If you want, burn the Gparted LiveCD to a CD - it’s a nice tool to have around in general, but not strictly needed if you plan to mount the raw ISO as a virtual optical disk.

2. Back up the old VDI and Machine (in case Murphy’s Law kicks in), unless your VM is expendable.

3. Using VirtualBox, create a new VDI with a larger size that you want to expand the virtual machine to

4. In VirtualBox set the “virtual bios” for the machine to boot from CD before the virtual hard disk

5. Leave your existing VDI as the primary IDE master. Set the new VDI to be the Primary IDE slave for the machine.

6. Put in the LiveCD and let VirtualBox know that it is to “present” your physical optical drive to the virtual machine, or point VBox to the raw .iso of Gparted on your hard disk. (The idea is to boot into the virtual machine with the LiveCD.)

7. run fdisk -l (that’s fdisk-space-dash-lowercase-l) to view your partitions. You should get /dev/hda and /dev/hdb. /dev/hdb shouldn’t be partitioned at this point. /dev/hda should be your primary master (the original VDI that is too small for your needs), and /dev/hdb should be the new image (virtual primary IDE slave).

8. Do not mount those. You don’t need to for this procedure. Instead, use dd to copy the old image to the new one:

dd if=/dev/hda of=/dev/hdb

This will make a copy of the original image (the one that’s too small) on the new one that’s bigger. This step may take awhile.

9. For good measure, you may wish to run fsck on the newly created partition.

10. Launch gparted, and resize the partition on /dev/hdb (the new image) to take up the entire disk. This may take awhile.

11. Poweroff with the GParted LiveCD once this has completed.

12. Finally, set the new image (resized) as the primary master for the virtual machine. Uncheck the primary slave. Boot into the machine to verify that it worked.

This worked for me in expanding a ~5GB virtual disk image to ~15GB (Fedora 8 Guest, Windows XP Host - the steps should be pretty much the same for a Linux host). Your mileage may vary. If all went well, you should now have your working install from the smaller VDI on a larger VDI. I did have problems when I first booted up the virtual machine using the new VDI as the primary master, but it was fixed by running fsck on the file system that the Fedora 8 VM complained about (hence step #9). If it does complain, boot up using the LiveCD/.iso image again and use that to run fsck on the dirty file system.

I’m not sure if this is doable with a virtual disk image with multiple partitions.

Once you’re done and have confirmed your guest OS sees the new disk image at its proper size, take your old VDI and do whatever you want with it - keep it for a backup, delete it, shred it, or whatever.

UPDATE: I have tried the above procedure - modified slightly - with VMWare virtual machines (specifically Server 1.0.6) and can confirm that I’ve managed to use a similar process with success with vmware.

To those of you who have expressed problems with the above procedure, I have been unable to duplicate the issues. Keep in mind that, as always, this guide comes with no warranty and that, as always, YMMV.