How to shrink/expand disks in VirtualBox - Tutorial

Updated: November 16, 2009

We have already had two tutorials on VirtualBox disk management. We learned how to clone disks and how to add disks to virtual machines. Today, we will learn how to shrink disks taking too much storage or expand those running dangerously low of free space.

Shrink disks

This is our first task. Let's say you've created a 40GB virtual disk and pre-allocated it. Indeed, when you're creating a new virtual storage device, you have the choice between creating a dynamically expanding disk and a fixed-size one.

If you create a fixed-size disk, then you will be able to shrink it. If you create a dynamically expanding disk, the chance you'll be able to shrink it are lower. Now, let's first see what we need to do before changing the disk size.

Tasks before shrinking

Defragment

This is a useful task, as it may shift some of the dispersed chunks of data into continuous blocks, allowing you to resize the disk. The defragmentation may not be applicable to some filesystems used by the virtual machine.

Remove junk

This is also quite useful. Cleaning temporary files and temporary Internet files, the trash bin, the installation leftovers, orphaned packaged, etc. Each operating system requires a different approach.

Shrink command

Once again, we will use the omnipotent VBoxManage utility:

VBoxManage modifyhd disk.vdi compact

What do we have here?

modifyhd command tells VBoxManage to try to alter the disk.

disk.vdi is the target disks you want to work on. Now, before you do try to change the disk, please make sure any critical data stored inside the virtual machine is safely backed up. In fact, you may probably want to clone the disk and test the shrinking operation on the copy, before you commit the changes to your production setup.

compact is the function that tells VBoxManage to try to shrink the disk. This may or may not succeed, depending on what data your disk contains and how it's spread on the filesystem. For example if your virtual machine contains a 6GB TrueCrypt container, which is essentially seen as raw, random data by encapsulating filesystems, this segment of the disk space cannot be compacted.

There's one more think you should be aware of ...

Dynamically expanding disks are sparse files, where the empty storage takes virtually no space. Converting virtual disks to other formats may inflate them to their original size. For example, if you convert your disks to RAW format, using an utility like QEMU, as we've demonstrated in the Amazon EC2 tutorial, they will grow back to their full size.

Keep this mind, because even though your virtual disk may only take 1MB while it is supposed to be 100GB in size, manipulations and conversions will open the file to its full size. That would be all. I don't have any exciting screenshots, I'm afraid.

Expand disks

Expanding disks is an easier, although more complicated task. It is easier, because you will succeed if you put your hands to it, whereas the shrinking procedure is more sort of a gamble. On the other hand, expanding the disk does not happen as magically as the shrinking.

To expand the disk, you will have to image it first. This means creating an image of the operating system. In most cases, this cannot be done in-vivo and requires an external, third-party software like CloneZilla running live session.

The next step is to create a new, larger disk and add it to your virtual machine. Luckily, we learned how to do that in the last article.

The third step is to restore the image onto the larger disk. Eventually, you will have to set the new disk as the boot device. The next time you power your virtual machine, depending on your imaging software, the image will have been expanded to accommodate the new disk or you will have extra free space at the end of the new disk. But wait!

Important! Please note that you will have to store the image somewhere! So effectively, you will need the THIRD storage device, where the image will be backup to and later restored from. It's a bit complicated, I know.

Mini summary

To recap, for a successful expansion of virtual disks:

Create new disk at least the size of the original.

Attach the new disk to the virtual machine.

Boot into the virtual machine and partition/format the new disk.

Use your preferred imaging software to create the image of the original disk. Most imaging software works from live CD, although some can take snapshots of the system while in use. This means you will have to boot the virtual machine from live CD or an ISO image of the imaging software.

Create an image; store it on our temporary disk.

Power down the virtual machine.

Attach a third disk to the virtual machine, larger than the original, as we want to expand the virtual machine disk.

Boot again into the live session and this time restore the image from the temporary disk to the third, larger disk. Please note that all imaging software require require that the operating system being restored be turned off, so you will require the live session. Optionally, a dual boot system might also work, but let's not complicate things.

After the image is restored, power off the virtual machine.

Detach the temporary storage disk.

Change the boot priority between the original, filled disk and the new, larger disk with the image just recently restored to it, so that the new disk is booted first.

Boot into your virtual machine and make sure everything works. Once you are satisfied, you can also detach the first disk or maybe format it and use it for data storage.

As you can see, expanding disks is a very serious, complicated task that new users should not attempt to try unless they are familiar with partitioning and really comfortable with working with imaging software. I hope my explanation was clear enough.

VirtualBox snapshots

VirtualBox snapshots may interfere with the shrinking/expanding of disks. If you encounter problems with these two operations, consider removing the snapshots. Again, while you are still testing the setup, perform all your operations on a cloned machine.

Conclusion

Shrinking and expanding disks is a complicated task that involves a lot of risks, since botched operations can result in data loss. This kind of work requires knowledge and experience and is equally daunting whether you're working with physical or virtual disks.

The best thing you can do is plan ahead. Try to make your setup modular and scalable, so you can easily exchange bits of the whole without interrupting the daily operation. For example, always use separate partitions for system and data, possibly even span them across a number of (virtual disks).

Consider leaving some free space after the system partition so that you may not need image it, but merely extend it using a partitioning software like GParted. Likewise, always allocate some extra space above the minimum recommended requirements by the vendor. For example, if your OS requires 4GB to be installed, go for 8GB or more. You can never be sure what new large programs you will have to install. And if you're using dynamically expanding disks, then the extra free space really won't matter.

I hope this tutorial was useful. Unlike most, it's quite thin on images, but I could not think of a simple formula that would present all of the mentioned material easily without exploding this article into a huge thing. If you have questions, feel free to contact me.

In the fourth article, we will discuss VirtualBox networking & sharing.

Cheers.