Sunday, 4 December 2011

How-to: Adjust Virtual Disk Size using VBoxManage

It's a common problem: you run VirtualBox, create a virtual machine, probably with a dynamic disk, at your best guess at an adequate disk size, run it for a while, then find you're running out of space.

You can easily expand the hard disk size VirtualBox using the command: VBoxManage modifyhd --resize.
The --resize option allows you to change the capacity of an existing image; this adjusts the logical size of a virtual disk without affecting the physical size. It  works only for expanding the capacity of VDI and VHD  virtual disk formats, and only  dynamically allocated not fixed size disks.  But this is a way to add more space to a virtual disk without needing to create a new image, attach and copy all the data across within a virtual machine.

This is a command-line application, so you need to open a Terminal session in in order to run the command and change directory to the path where your virtual disk is stored.

Two important notes:
  1. Shutdown the running virtual machine before you mess with the disk!
  2. Backup the existing disk image, or at least any volatile data, before you alter it!
The command you need follows this pattern:

VBoxManage modifyhd XP_newdisk.vdi --resize 10240
  • VBoxManage is the utility suite that comes with VirtualBox:
  • modifyhd is the command to resize the virtualdisk
  • XP_new.vdi is my example disk name; you'll need to change it to match your disk name and don't forget the  .vdi suffix. Under Linux it's also case sensitive.
  • --resize 10240 Next is the desired new size for the virtual disk. That's two dashes before the word "resize" followed by a number in kilobytes; 10240 being equivalent to 10Gb.
Also remember that you'll need to increase the size of your guest partition on the disk, or create an additional partition in the new space. In this respect, you'll need to do the work with disk utilities appropriate to your guest operating system just as if it were a real PC with real spinning rust.

Note that .vmdk disks cannot be expanded by modifyhd commands. Try this command line and you'll get the error:

VBoxManage: error: Resize hard disk operation for this format is not implemented yet!

You can find the manual for Oracle VirtualBox, indexed by each topic on the website. VBoxManage is Chapter 8:
where the full syntax of the command looks like this:

VBoxManage modifyhd         <uuid>|<filename>
                            [--type normal|writethrough|immutable|shareable|
                            [--autoreset on|off]
                            [--resize <megabytes>|--resizebyte <bytes>]

The --compact option can be used to shrink disk images, that is, remove blocks of empty space (containing zeroes). This will shrink a dynamically allocated image by reducing the physical size of the image without affecting the logical size of the virtual disk.

However,  free space in the guest system must first be zeroed. For Windows guests, you can use the sdelete tool provided by Microsoft; running sdelete -c in the guest will zero the free disk space. Under Linux, the zerofree utility supports ext2/ext3 filesystems to do the same. You can then compact (compress) the virtual disk image.  RC