Expanding VirtualBox Hard Drives on a Mac

I do all my web-development through VirtualBox on my mac. This lets me run Ubuntu Server on my local machine and gets me a little closer to replicating a production server on my mac.
Occasionally though, the VirtualBox hard drive gets full and I need to expand it. This is how I do it.

Disclaimer: back up your virtual machine before attempting this. Nothing bad should happen, but resizing partitions can (though usually doesn't) result in a lose of data.

Step 1

Open Terminal, it's in /Applications/Utilities/Terminal.app

(or just type 'terminal' into Spotlight).

Step 2

Type the following command into terminal to navigate to your VirtualBox VMs folder.

cd ~/VirtualBox\ VMs/{your VM folder}

Replace {your VM folder} with the name of the folder containing the VirtualBox VDI file, in my case, this folder is called 'ubuntu'.

Step 3

Now we'll use the VirtualBox command line tool to resize the drive image.

VBoxManage modifyhd {yourVDIFile.vdi} --resize {new hd size}

Replace {yourVDIFile.vdi} with the name of the VDI file, and replace {new hd size} with the new hard drive size.

This size is measured in megabytes, but the size you want in probably in gigabytes, so multiply the number of gigabytes you want by 1024, i.e. if you want 15gb, thats 15 * 1024.

After hitting enter, you should see something like this, this is the computer telling you everything is going extremely well, Dave.

Step 4

Now that we've resized the hard drive, we have to resize the partition. This is where GParted, a partition manager for linux, comes in real handy, so download the GParted Live CD from here.

Step 5

Mount GParted onto your VirtualBox image so that it boots up when you start your virtual machine.

  1. Open VirtualBox
  2. Right click on your virtual machine
  3. Click on Settings
  4. Click Storage
  5. Click the CD icon beside the IDE Controller.
  6. Select the GParted file we just download

Now when you start the virtual machine, it will boot into GParted. GParted gives us some settings when it starts, but just clicking enter will give us the defaults.

Once we have GParted up and running, resizing the partition is just a few clicks away.

Select the main partition, and click resize/move . Drag the slider to fill the hard drive with all the available space.

GParted warns us that this could potentially set our house of fire, but we click apply anyway. BTW, if this warning freaks you out, back up your virtual machine folder.

But in the end everything works out just fine.

Now it's safe to shutdown the virtual machine.
The only thing left to do is remove GParted from the virtual machine. We do this in the same way we added the image, except now we just right click 'Remove Attachment' and we're all done!