The first few times I used VirtualBox it wasn’t clear why you would ever want to create a virtual disk that is dynamic. I could only envision having three machines each with a disk that wasn’t fully expanded only to run out of real storage when I really needed it.
However, just like real machines it is possible to full up the hard disk and want just a tiny bit more space. Of course you can add a second virtual disk to your machine but there is another option. It is actually possible to add to a small extra bit of space to your virtual hard disk, but only under once situation. It is not possible to expand a fixed size disk beyond its initial size but it is possible to extend a dynamic disk to a larger size.
The steps to extend a virtual harddisk is actually pretty simple.
- extend the virtual disk image (vdi)
- mount gparted inside the virtual machine
- select and extend the partition
Original state
The disk drive associated with my virtual machine was only 10GB. This is actually more than enough for small tests.
Extend the virtual disk image
This first step needs to be performed on a disk image that is not currently being used by virtualbox. This step is done by running the command line program vboxmanage. Simply give the total size of the disk image and the disk is extended.
The command both gives feedback while running and runs really fast.
vboxmanage modifyhd <qualified path to disk> --resize <# of megabytes>
Virtualbox can display the disk size as soon as the command has finished running.
Mount Gparted inside the VM
Simply resizing the virtual disk image is not actually enough. This will make the disk itself larger but it will not make any more space available to the client operating system inside of the virtual machine. This space can be allocated to the partition by using some tool to extend the partition.
Gparted is one of the easiest tools to use to enlarge the partition. However, just like many other Linux tools it needs to be run on a volume that is not currently mounted. In order to do that simply download the gparted live disk and mount it to the virtual machine.
The Gparted live disk will boot up just like on any physical machine, it will bring up a grub menu and let you start the Gparted live installation.
Actual extension of file system
The actual extensino of the file system in this particular case is trivial. Simply select /dev/sda5 and resize this to use the extra two gigabytes that have been added.
This particular case was trivial by design. I didn’t want to move the swap partition system to the end of the disk and move the free space to the end of the partition.
You can see that the space has been allocated to the partition /dev/sda2 but not yet being used by the file system. The next step is to expand the actual filesystem on /dev/sda5
It may seem like a lot of effort to simply add a few gigabytes or more space to a virtual disk image but the entire process can be done in a few minutes.