Archive for July, 2011

How to resize your virtualbox harddrive

Tuesday, July 26th, 2011

This was my situation: Ubuntu 10.04 i686 Host, Ubuntu 10.04 i686 Guest, VirtualBox 4.0.12, dynamic virtual guest hard drive with 8GB, which were simply too few.

Searching the VBox-Forums, it seems there are a couple of ways to increase the disc space of your dynamic virtual harddrive, the way also depends on your host/guest combination.

You might want to look into CloneVDI, a tool that lets you do many things with your VDI. The Post on the VBox-Forums can be found here: CloneVDI Discussion and Support.

I followed the instructions from this post, which seemed to fit my combination just fine. All you need is another guest linux, which happened to be another Ubuntu 10.04 for me. I will call this guest tmp-guest.

First, shut down the guest with the too-small harddrive, which i will call original.vdi. Then goto the vbox settings of your tmp-guest and select the storage tab. There you can add another harddrive to the controller, which will be a new harddrive. Follow the guide to create a harddrive with the desired size (e.g. 15GB for me). I will call this the new.vdi.

After that, stay in this section and also add the original.vdi harddrive (by choosing “select existing harddisk” in the guide). You should now have the harddisk of the tmp-guest, the newly created big new.vdi and the original.vdi attached to your tmp-guest.

Now boot up your tmp-guest and install e.g. “gparted” by doing:

sudo apt-get install gparted

The operations done by gparted could also be done via the LiveCD, where the .iso can be mounted by virtualbox, if you dont want to install gparted to your tmp-guest.

Before we can resize the virtual harddrives, we need to clone the original.vdi to the new.vdi. This can be achieved with the following command:

dd if=/dev/[device of original.vdi] of=/dev/[device of new.vdi] bs=1M conf=noerror

The device will probably be /dev/sdb for the original.vdi and /dev/sdc for the new.vdi, /dev/sda is the harddrive of your tmp-guest. Depending on your configuration this could be totally different though, so its better to verify that you copy the right devices.

ALL YOUR DATA COULD BE LOST.

If you know the harddrives UUID’s, you could use the command

blkid

to identify the devices. Another option is to use

fdisk /dev/sdb

and hit the “p” option to show the details of this device. Pay attention to quit fdisk with “q” so that no changes are written. The new.vdi will not display proper information, since now partitions were created in it.

One could also install “smartmontools” (sudo apt-get install smartmontools) and use the command:

sudo smartctl –all /dev/sdc

to get information on that device.

Assuming the dd command was successfull, you will see the transfer stats like

8589934592 bytes (8.6 GB) copied, 416.04 s, 20.6 MB/s

Now new.vdi contains an exact mirror of original.vdi, but the extra space is not yet available. This will be fixed by gparted.

Start gparted and select the new.vdi device from the top right (should be 15GB or whatever your new size is). Right click on the 8.6GB of partition (left) and select resize. Draw the border all the way to the right (this uses all the available space) and click OK. These changes have to be applied (green arrow on middle).

Now your new.vdi contains the data of your original.vdi but has some extra space.

Shut down your tmp-guest, go back to the settings, detach the new.vdi and original.vdi from your tmp-guest, remove the original.vdi from your original guest and add the new.vdi instead. You should be able to boot the new.vdi without problems and TADA, more space!

One note, if you have different snapshots and want to merge them into a .vdi, you have to delete the previous stages from the snapshowview. Otherwise, snapshots are stored as a difference to the vdi and you will have an old snapshot in your new.vdi.

See this post on how to merge a vdi with a snapshot.

If you have trouble, removing your original.vdi, e.g. because of the error

“… has more than one child hard disk”

it might still be attached somewhere. This can be resolved in the Virtual Media Manager, which shows the machines, an image is attached to. For further guidance, please see this post or this blog entry.

For windows guests, this blogpost might be helpful.