Sunday, January 30, 2011

Resizing a VirtualBox Virtual Hard Disk

Prior to version 4.0, expanding the size of a virtual disk was not a task that was directly supported by VirtualBox. After some trial and error, I've cobbled together a solution documented below which consolidates and amends information found elsewhere online.

For VirtualBox versions before 4.0, the procedure, in broad strokes, involves the following steps.

  1. Create a new virtual disk.
  2. Clone the existing virtual disk into the new virtual disk.
  3. Replace the old virtual disk on your virtual machine with the new one.
  4. Expand the partition in the new virtual disk clone.

As of VirtualBox 4.0 (released December 22, 2010), the process has gotten a lot simpler. You may replace the first three steps with a single VBoxManage command. [Thanks to Przemysław for pointing this out.] You will still need to perform the final step of expanding the partition in your newly resized virtual disk image.

Prerequisites

Before you start this procedure you'll need to do the following.

  • Make sure you have the VBoxManage command-line tool installed on your host system.
  • Download the ISO for a GParted Live CD or else a Linux Live CD using a Linux distribution that includes the GParted partition editor utility.
  • IMPORTANT: If the virtual disk you want to resize is attached to a virtual machine with snapshots, you will need to delete these snapshots so that all disk state information is merged into the base virtual disk VDI file.

0. Resize the virtual disk (VirtualBox version 4.0+ only)

If you are using VirtualBox version 4.0 or later, you can resize the logical capacity of a virtual disk using the VBoxManage modifyhd --resize command.

On the host system, run the following command:

cd /path/to/vbox/disks

VBoxManage modifyhd OldDisk.vdi –-resize 30000

where OldDisk.vdi is the filename of the virtual disk VDI file you want enlarge and 30000 is the new maximum size (in megabytes) of the virtual disk.

If your host OS is Windows, then the commands you need to enter at the Command Prompt will look more like the following.

cd "C:\Documents and Settings\myusername\.VirtualBox\HardDisks"

"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifyhd OldDisk.vdi --resize 30000

Once you've resized your existing virtual disk, you should skip to Step 4: Expand the partition on the larger disk.

1. Create a new virtual disk

You can create a new virtual disk of the desired storage size in two ways: using the VirtualBox GUI or using the VirtualBox command-line utility VBoxManage.

1a. Using the VirtualBox GUI

On the host system, launch VirtualBox, and navigate to File → Virtual Media Manager… → Hard Disks. Click Add to add a new virtual hard disk, and step through the wizard, making sure to create a new maximum disk size that accommodates your needs.

1b. Using the VirtualBox command-line utility

On the host system, run the following command:

cd /path/to/vbox/disks

VBoxManage createhd –-filename NewDisk.vdi --size 30000 --remember

where NewDisk.vdi is the filename of the new virtual disk VDI file and 30000 is the maximum size (in megabytes) of the virtual disk.

Note that on Windows you may need to designate the full path to the VBoxManage command. For example, on a Windows host system at the Command Prompt, the above commands will instead look something like the following.

cd "C:\Documents and Settings\myusername\.VirtualBox\HardDisks"

"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" createhd –-filename NewDisk.vdi --size 30000 --remember

2. Clone the existing virtual disk into the new virtual disk

Now clone the old, small virtual disk into the new, large virtual disk.

cd /path/to/vbox/disks

VBoxManage clonehd OldDisk.vdi NewDisk.vdi --existing

If your host OS is Windows, then the commands you need to enter at the Command Prompt will look more like the following.

cd "C:\Documents and Settings\myusername\.VirtualBox\HardDisks"

"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" clonehd OldDisk.vdi NewDisk.vdi --existing

3. Replace the old virtual disk on your virtual machine with the new one

3a. Using the VirtualBox GUI

On the host system, launch VirtualBox, and select the virtual machine whose hard disk you are expanding, and navigate to Settings → Storage. Change the SATA Controller so that it uses the new virtual disk file instead of the old one.

3b. Using the VirtualBox command-line utility

VBoxManage modifyvm "My Virtual Machine" --hda none

VBoxManage modifyvm "My Virtual Machine" --hda NewDisk.vdi

Or for Windows host systems:

"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifyvm "My Virtual Machine" --hda none

"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" modifyvm "My Virtual Machine" --hda NewDisk.vdi

where My Virtual Machine is the name of the virtual machine whose disk you are expanding.

(At this point you may want to boot your virtual machine to test whether it works. If you do, remember to shut it down before continuing.)

4. Expand the partition in the larger virtual disk

Now that you have an enlarged version of your virtual disk, you will want to expand the new disk's partitions to take advantage of the increased disk size. To do so we'll use the GParted partition editor.

If you have created a GParted Live CD or Linux Live CD, place it in the CD/DVD drive on your host machine, and change the settings of your virtual machine to use the host drive as its CD/DVD drive. If you have the ISO for the Live CD, simply configure your virtual machine so that it uses the Live CD ISO file for its CD/DVD drive. Navigate to Settings → Storage to configure the CD/DVD settings for your virtual machine.

Make sure that the virtual machine is configured to include CD/DVD-ROM in the boot order. To check the boot order, navigate to Settings → System → Motherboard.

Start the virtual machine. The virtual machine should boot from the Live CD.

If you are using the GParted Live CD, then you should see the GParted utility. If you are using a Linux Live CD, you may need to click on a menu item to indicate you want to try the Linux distribution without installing it.

Once the Gnome or KDE desktop environment is finished loading, open the Terminal application, and then enter sudo gparted to launch the GParted utility.

Now that you have launched GParted, in the upper right of the GParted window, make sure the correct disk has been selected — e.g. /dev/sda (30.00GB). Select the partition you want to expand — e.g. /dev/sda1 — and click the Resize/Move button.

In the pop-up window, expand the partition to use the maximum size. You can do so by dragging the right edge of the partition block all the way to the left. Click the Resize/Move button.

Note: If there is a swap partition, you may need to take a few extra steps to ensure that the unallocated space is immediately after the partition you want to grow. See the addendum below for more information on how to move the unallocated space.

Click the Apply button to apply the partition changes.

Quit GParted, and shutdown the virtual machine. Eject the CD from the host machine's CD/DVD drive or modify the virtual machine settings to unmount the ISO from the virtual machine CD/DVD drive.

Start the virtual machine again, and at the command line, type df -h to validate that the partition has been resized properly.

References

ADDENDUM: Special Notes Regarding Swap Partitions

If virtual disk you are editing via GParted has a swap partition, you may find that the newly available free space on the disk is separated from the partition you want to expand by the swap partition. This presents a problem because in order to expand a partition, the free, unallocated disk space must be located immediately after the partition.

One way to deal with the problem is to move the swap partition so that the unallocated space is before the swap partition rather than after it. Another is to delete the swap partition altogether.

Moving the swap partition

Right-click the swap partition, and select the Swapoff item in the context menu to disable the swap partition. Doing so will unlock the swap partition so that you may modify it.

Select the extended partition that contains the swap partition, and click on the Resize/Move button.

In the pop-up window, take note of the partition size (e.g. 486 MB).

Drag the right edge of the partition all the way to the right, and then drag the left edge of the partition toward the right until the new partition size is back to what it was — in this example, 486 MB. Click the Resize/Move button.

Click the Apply button. Re-enable the swap partition by right-clicking on it and selecting Swapon.

You should now be able to expand the main partition into the unallocated space.

Deleting the swap partition

Right-click the swap partition, and select the Swapoff item in the context menu to disable the swap partition. Doing so will unlock the swap partition so that you may modify it.

Select the extended partition that contains the swap partition, and then click the Delete button.

You should now be able to expand the main partition into the unallocated space.