As a frequent maker and, occasionally, breaker of Oracle VirtualBox virtual disk images, I am constantly creating and maintaining many virtual machines. Some are Ubuntu or Linux variants, others are Windows, and I even have a Mac OS X virtual machine as well. Speed is my number one priority in these VMs, so I almost always set the disk size to static. However, at times I find myself desiring a bit more disk space, or perhaps an update comes along and I realize I don't have enough space to download and install it. Frustrated with the lack of guides on the inter-webs, I have decided to create my own step-by-step guide to resizing a static disk image.
This guide will allow you to convert your VDI disk image from static to dynamic, expand your disk image to a larger size, and convert back to a fixed-size disk image. For the sake of simplicity, this guide will be aimed at Windows hosts and Ubuntu guests, though the commands I use will be the same or similar for other hosts/guest combos. In addition, I will detail the expansion process, though the reduction mechanism is one and the same. With the exception of the last step, the only thing that changes is the directory the VirtualBox files are located.
Step One
Open the command line prompt. To do this, type cmd
into your start menu or start screen, and hit enter.
Once open, navigate to the folder where your virtual disk image is stored. If on Windows, your command will most likely be:
cd /Users/Your-Username-Here/"VirtualBox VMs"
Type dir
(ls
for *nix) into the command line prompt to see your virtual machine folders in that directory. Then, navigate into one of them to see your actual VDI file (the virtual machine disk we want to expand). If my virtual machine were named Ubuntu, I would type:
cd Ubuntu
Step Two
Type dir
again to see the files within the directory. The file I'm looking for will be named my-virtual-machine.vdi
. Let's say, for consistency, it's named Ubuntu.vdi
.
Our next step is to convert the disk image from being a fixed-size disk image to being a dynamically-allocated-size disk image. While there's no real way to do this directly, we can get the same effect by cloning the existing disk image into a new one. In our case, we can do this with the following command:
VBoxManage clonehd [Ubuntu.vdi] [UbuntuToo.vdi] --variant Standard
Where Ubuntu.vdi
is my original disk, UbuntuToo.vdi
is my newly-created disk, and --variant Standard
tells VirtualBox to make the image dynamic. Note that absolutely nothing changes in your original disk via this process—your original data and disk will be unharmed. If something were to break in the cloning process, your original disk image is unchanged and intact.
Step Three
We now have a dynamically-resizable disk image, which means we can expand our disk to, say, 500 GB via the following command:
VBoxManage modifyhd UbuntuToo.vdi --resize 512000
Where 512000
is the MB equivalent of 500GB. You can choose whatever size you like, of course, as long as you're sure it fits in your hard drive. This resizing process may take a couple minutes, depending on the size. Note that this command only works because we have a dynamic disk image. A fixed-size disk image would give us an error, and if you do get an error here, that's most likely it.
You now have a choice: if all you want to keep your newly-dynamic disk image, you can stop here. If you want to go back to a fixed-size disk image for the speed benefits (and I wouldn't blame you if you did), keep reading.
Step Four (optional)
To go back to a fixed-size virtual machine, we must follow a similar process to what we did earlier, just in the reverse direction. We can type:
VBoxManage clonehd [UbuntuToo.vdi] [UbuntuAlso.vdi] --variant Fixed
Where UbuntuToo.vdi
is your new dynamic disk, UbuntuAlso.vdi
is what your final disk will be named (though you can rename always it later), and --variant Fixed
declares it as a fixed-size disk. This will give us a fixed-size disk of the size we allocated for our dynamic disk. It's important to note that while a 500GB dynamically-allocated disk may only take up 20GB on your hard drive, a fixed-size 500GB disk will take up 500GB of your hard drive space. Also keep in mind you will now have three VDI files in your folder: the original, the dynamic, and the final, larger virtual disk. If nothing went wrong, you can feel free to delete the other two files, just keep track of which is your final disk.
Step Five
We're done with the command line. The next step we have to take is within the VirtualBox GUI. Open up VirtualBox. Select the virtual machine you resized and go to "Settings". Now go to "Storage" on the left-hand side. Within the "Storage Tree" box, highlight "Controller: SATA" and click on the small "Add Hard Disk" icon (it should be on the right). Now click on "Choose an existing disk" and browse to the location of your newly-created virtual drive. Select it and click "Open". You should see it appear under the "Controller: SATA" label. If there are any other drives there, be sure to remove them by highlighting them and clicking the red "Remove Attachment" icon at the bottom. You're done!
Step Six
...Almost. The last step is very operating system dependent. Our newly-resized virtual machine will now start up, but it won't automatically expand its main partition to encompass the new space. We have to manually extend the partition so that it can grow to the new size. On a Windows guest, we would right-click on "My Computer", go to "Manage", and select "Disk Management"; on an OS X guest, we would use the Disk Utility app. Continuing with the the idea of a Ubuntu guest OS, we need to check if gparted
(or your favorite partition editor) is installed. If it is, open it; otherwise, go to the terminal and type in:
sudo apt-get install gparted
Now open gparted
. It's a relatively simple interface, and the only thing we have to do now is grow the main partition to the entire disk space. Click the main partition on the bar displaying the disk space and click the "Resize" icon at the top. In this new window, drag the slider to the end of the disk so there is a 0 in the "Free Space Following" on the disk (or as little as possible), hit "Resize", and click the check mark icon back in the main window to apply the operation. After this completes, you have a larger disk at your disposal!