Expanding VirtualBox VM Disk Space

Posted by: on

Problem

You have a Virtualbox VM that you have been using for a while but have now run out of space on the primary drive and you want to get more disk space allocated to that you can continue using the VM.

Solution

This is the simplest method I have discovered to expand a virtual disk on VirtualBox.

  1. Shut down the VM.
  2. Ensure the disk is in VDI format.
  3. Expand the virtual disk.
  4. Expand the filesystem of the VM.
  5. Replace the original disk with the new expanded disk.

Solution steps

See discussion for details, here I present only the steps.

Shutdown VM

If you’re using vagrant just vagrant halt <machineID> (<machineID> being the name you assigned in your Vagantfile, omit this if referring to the default or only machine in your Vagrantfile). Using vagrant halt is not strictly necessary but it keeps everything square with vagrant.

If not using vagrant just power off the machine in VirtualBox.

Ensure disk is in VDI format

Assuming a VM named mybox in the default ~/VirtualBox\ VMs directory, obviously the machine and disk name will be different for you.

1
2
cd ~/VirtualBox\ VMs/mybox
VBoxManage clonehd "disk1.vmdk" "clone-disk1.vdi" --format vdi

Expand virtual disk

1
VBoxManage modifyhd "clone-disk1.vdi" --resize 102400

Expand the filesystem of the VM

Create a VM with gparted installed. This is simple enough, I use a basic Debian VM with Gnome installed and this comes with gparted installed.

Shutdown the gparted VM and attached the cloned-disk1.vdi as a second disk.

Start the gparted VM, open gparted, switch to the second attached disk.

Assuming you have a basic default Debian install as an example, use gparted to:

  1. Move the swap partition to the end of the free space.
  2. Expand the primary partition to fill the resulting free space.
  3. Commit the changes.

Once the partitions have been modified, close the gparted VM and disconnect the cloned-disk1.vdi.

Replace the original disk with the new expanded disk

This is simply done through the VirtualBox GUI.

With the VM halted, detach the original VMDK disk and attach the new VDI disk.

Start the VM as normal (either directly or, if using vagrant via vagrant up <machineID>).

Alternative Approaches

There are several tutorials on differing approaches to expanding the VM’s primary disk.

Use vagrant-disksize plugin

If you use vagrant then you may find the vagrant-disksize plugin useful.

1
vagrant plugin install vagrant-disksize

Then in your Vagrantfile add (by way of example):

1
config.disksize.size = '50GB'

I found this to be somewhat hit-and-miss on my vagrant boxes but YMMV.

Resize A Vagrant VMDK Drive

Although the title specifies vagrant the principals hold for an VirtualBox VM. That said, this blog post assumes that your VM is using Logical Volumes, which, in my experience, is fairly uncommon on third-party vagrant boxes so YMMV.

Resize Your Vagrant / VirtualBox Disk

Similar to the above blog post this one assumes use of Logical Volumes but I think is easier to follow. Again YMMV.

Discussion

Shutdown VM

You cannot resize a virtual disk that is attached to a running machine.

Ensure disk is in VDI format

It is common to find VMDK format (initially a VMWare format but now widely available; QEMU, VMWare, and VirtualBox all support VMDK) disks attached to VirtualBox VMs. Unfortunately VirtualBox cannot resize VMDK disks, so we need to convert the disk to VDI format (VirtualBox native format), which can be resized.

Expand virtual disk

Expanding the disk only changes the size of the virtual disk, it does not change the size of any file system on the disk. In this step we are only changing the amount of virtual disk space available.

Expand the file system of the VM

Okay, in this step we are actually moving bits around on the virtual disk to make the file system larger.

I’m assuming in this example you have a simple setup (a common one at that) where you have one virtual disk attached to the machine and have selected a default Linux OS install, which typically creates one large primary partition to hold the main file system and one small logical partition to contain the swap file system.

In order to expand the primary partition (the one we’re interested in) you need to move the swap partition up to the end of the new free disk space created when we expanded the disk. gparted is a simple to use partition management tool (that’s why I’m using it here). In order to use gparted we need to make the expanded disk available to the VM with gparted on it. (It may be tempting to try this on the original VM but if you’re OS is on the primary partition—and it will be in this simple setup—you will not be able to resize it, you have to be able to dismount it and access it, hence we attach it to another machine.)

Once you have gparted open, change the disk it’s looking at and then move the swap partition to the top of the free space. To do this expand the logical partition, then move the swap partition up, then move the bottom of the logical partition up to make a large block of free space available immediately after the primary partition. Finally, expand the primary partition into the free space. Then apply the changes. This is a fairly quick and painless process and gparted will move modestly sized swap partitions fairly quickly (only a few second).

Once done, shut down the gparted VM, detach the expanded disk.

We now have an expanded virtual disk cloned-disk1.vdi on which the file system will have been expanded thanks to gparted.

Replace original disk with the new expanded disk

This is a simple process of shuffling the attached disks (I use the VirtualBox GUI). Detach the old VMDK disk, attach the new VDI disk, and boot the machine. Voilà you’re done.