Expanding VirtualBox VM Disk Space
Posted by: Mark Bools on 2019-08-09 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. This is the simplest method I have discovered to expand a virtual disk on VirtualBox. See discussion for details, here I present only the steps. If you’re using If not using Assuming a VM named Create a VM with Shutdown the Start the Assuming you have a basic default Debian install as an example, use Once the partitions have been modified, close the 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 There are several tutorials on differing approaches to expanding the VM’s primary disk. If you use Then in your Vagrantfile add (by way of example): I found this to be somewhat hit-and-miss on my Although the title specifies Similar to the above blog post this one assumes use of Logical Volumes but I think is easier to follow. Again YMMV. You cannot resize a virtual disk that is attached to a running machine. 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. 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. 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. Once you have Once done, shut down the We now have an expanded virtual 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.Problem
Solution
Solution steps
Shutdown VM
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
.vagrant
just power off the machine in VirtualBox.Ensure disk is in VDI format
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
gparted
installed. This is simple enough, I use a basic Debian VM with Gnome installed and this comes with gparted
installed.gparted
VM and attached the cloned-disk1.vdi
as a second disk.gparted
VM, open gparted
, switch to the second attached disk.gparted
to:
gparted
VM and disconnect the cloned-disk1.vdi
.Replace the original disk with the new expanded disk
vagrant
via vagrant up <machineID>
).Alternative Approaches
Use
vagrant-disksize
pluginvagrant
then you may find the vagrant-disksize
plugin useful.1
vagrant plugin install vagrant-disksize
1
config.disksize.size = '50GB'
vagrant
boxes but YMMV.Resize A Vagrant VMDK Drive
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
Discussion
Shutdown VM
Ensure disk is in VDI format
Expand virtual disk
Expand the file system of the VM
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.)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).gparted
VM, detach the expanded disk.cloned-disk1.vdi
on which the file system will have been expanded thanks to gparted
.Replace original disk with the new expanded disk