My Linux VirtualBox guest OS often runs out of space – I never learn that to build anything in Linux, you need about 10 times the amount of space that you think you need. Also, VirtualBox recommends very small default values, so it is easy to be caught out.

Anyway, here are the current steps to re-size a VirtualBox disk, where Linux is the guest OS and Windows is the host OS.

In this example I am using VirtualBox 4.2.6 (The approach is valid with more recent versions also). The host OS (the one that is running VirtualBox) is Windows 7 and the guest OS that I wish to re-size is Ubuntu. Please backup everything before continuing as something could always go wrong.

Step 1. (Optional) Move the VDI file in Windows

If you need to move the VDI file to another location/physical drive with more space, you can do the following.

  • With VirtualBox shut down, using Windows Explorer move your vdi file (e.g., “c:\MyLinux.vdi“) to its new location (e.g., “d:\MyLinux.vdi“)
  • Start  Oracle VM VirtualBox Manager -> choose File-> “Virtual Media Manager”
  • Under the “Hard drives” tab, choose your disk (e.g. “MyLinux.vdi“) and press the “Release” button at the top.
  • Now with the disk selected press “Release” (this should remove the last location)
  • Close “Virtual Media Manager”, choose your VirtualBox instance (e.g. MyLinux which should say “Powered Off” underneath) and press the “Settings” button at the top.
  • Press”Storage” on the left-hand side, and under Controller remove the current drive by pressing the red minus. Then Press the + with the hard drive platters.
  • It should ask “You are about to add a virtual hard disk to the controller IDE Controller”, pick “Choose existing disk” and browse to the location to which you moved your VDI image (e.g. “d:\MyLinux.vdi“). Press “OK”
  • Check that your image boots before you go any further.

Step 2. Resize the VDI file

  • Shut down VirtualBox again.
  • Make a copy of the VDI file – just in case (“MyLinux.vdi” -> “MyLinuxCopy.vdi“)
  • Go into the Windows command prompt (Start->type “cmd” into the box)
  • cd to the location of the VDI file that you wish to resize, e.g., “d:\MyLinux.vdi
  • At the Windows command prompt, type:

D:\>VBoxmanage modifyhd MyLinux.vdi --resize 100000
this will re-size the drive to 100 GB. Pick a value that suits you. Note, your vdi file will not change in size at this point. The output should look like:
D:\>VBoxmanage modifyhd MyLinux.vdi --resize 100000
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
D:\>

Unfortunately that was the easy part!

Step 3. Resize the Linux Partition

At this point you need to grow the Linux partition to take up the space of the newly resized vdi file:

  • Go into “Oracle VM VirtualBox Manager”, choose your Linux image “MyLinux (Powered Off)”. Press “Settings” and under “Storage” when you select your disk you should see “Virtual Size: 97.66GB” for the 100GB that I set and “Actual Size: 18.52GB” if your previous disk was 20GB and now almost out of space!
  • Unfortunately, if you boot this image at this point the space will not have been allocated to your Linux drive. A “df -k” will make that clear!
  • You need to use GParted for this. Go to http://gparted.sourceforge.net/livecd.php and download a GParted Live on CD ISO file (i.e., GParted Live CD/USB/HD/PXE Bootable Image ISO File Use the latest version for your architecture – e.g., gparted-live-0.22.0-1-i586.iso). Save this ISO on your hard disk.
  • In “VirtualBox” Create a new virtual machine, as in Figure 1, and call it “GParted” with Type: “Linux” and Version “Linux 3.8” or whatever version. Choose “Do not add a Hard Drive” and ignore the warning.

Figure 1: Creating the GParted Virtual Machine

  • Pick your “GParted (Powered Off)” virtual machine and press “Settings”. Choose “Storage” and under “Controller: IDE Controller” add a new CD/DVD device. Browse to the location of your GParted ISO file and select it. Your first item under “Controller: IDE” should be “gparted-live-XXX”. If you have a second “Empty” disk you can remove it. Then add the disk that you wish to resize under “Controller: SATA Controller”. At this point it should look like Figure 2, where HomeOfficeUbuntu is the “MyLinux.vdi” discussed above.

Figure 2: Add the SATA Controller and Disk

  • Press OK and start your GParted Virtual Machine and you should see it boot (very quickly). Choose Gparted Live (Default settings). Choose all the default settings and your language of choice. Press 0 to start X and you should end up with a virtual machine running that looks something like the output in Figure 3.

Figure 3: GParted Virtual Machine Running in VirtualBox

  • You can see that there are 78.12GB unallocated and this is the space that we wish to allocate in my case to /dev/sda1 which is an ext4 filesystem and is currently 18.53GB in size.
  • Now, you should have made a backup of your vdi at this point. If you haven’t go back and do that – so many things can go wrong here and you are on your own!
  • If it is any partition other than /dev/sda1 you can right-click the partition you wish to resize and choose “Resize/Move” (as in Figure 4) – not in my case!

Figure 4: Resize/Move the Partition

  • However, my problem is that the extended partition and my linux-swap are blocking me from changing the size of my /dev/sda1/. So, unfortunately I have to delete and re-create them again. So, select the linux-swap and press “Delete” and /dev/sda2 (or whatever your one is!) and press “Delete”. The press “Apply” as in Figure 5 below.

Figure 5: Delete Partitions

  • After applying the changes choose the /dev/sda1 partition and choose resize as in Figure 6, allowing enough space (“free space following” of 1023) for a new swap partition. It should say “Grow /dev/sda1 from 18.53GiB to 96.66GiB” and it may take a short while (~1 min). Hopefully you should get a “All operations successfully completed” message, as in Figure 6.

Figure 6: Resizing the Partition

  • Now, re-add your Linux Swap Space. Press on the unallocated space and right-click to “Created new Partition” -> choosing “Create as: Extended partition” as in Figure 7 below.

Figure 7: Output after changes are applied

  • Press “+Add” and right-click the new “unallocated”  to Create a new partition. Choose as “Create as: Logical Partition” and underneath “File System: linux-swap” as in Figure 8.

Figure 8: Creating the Swap Partition

  • Now press “+Add” and then “Apply” in the main window. Hopefully all changes are successfully applied and now my output looks like Figure 9 below.

Figure 9: A successful partitioning

  • You can now safely shut down this Live CD Virtual Machine.
  • Finally, boot your original “MyLinux” Virtual Machine and all should be in order, so that when you do a “df -k” you get the output as in Figure 10 below.

Figure 10: A successful outcome

So, now the disk is only 21% utilised in my case and I am free to work away with my new found space!

Good luck with this… remember to back up your VDI in advance and be careful when you are working with GParted.