How To: Shrink/Compress a VirtualBox Windows Guest Machine

When you create a VirtualBox virtual-machine, you get the option to choose either a fixed sized hard drive (in which case the entire amount of space is allocated and fixed immediately), or a dynamically expanding hard drive for the virtual machines OS and file storage (i.e. you specify a maximum size for the hard drive, it starts at 0 bytes, and increases as necessary up to the maximum you gave it on creation – at which point the drive is full).

Linux and Windows via VirtualBox

This is all well and good, but the problem with dynamic storage is that although it’s more than happy to increase in size, it doesn’t come down in size again when data is removed. So, to give an example, if you created a 10GB dynamic disk for a virtual machine it starts off at 0 bytes, you install a 1GB operating system and the drive is now 1GB in size (and hence taking up 1GB of space on your actual hard drive), you rip a 4.7GB DVD to the virtual drive which makes its size now 5.7GB, you delete the DVD rip so only the OS remains – and you might think that the “dynamic” drive will automatically shrink back down to 1GB, only it doesn’t. You’re holding on to 4.7GB of unrecoverable* bloat. Lucky you… =P

You could rip another DVD and re-use that space without the drive expanding any further, but really, it’s just going to increase and increase, and you’ll know in your heart of hearts that when you’re running low on disk space you could really do with that space back to your real hard drive. In VMWare you can compact the drive image as a menu option, but in VB we have to do a three step process… So, shall we?

* = unrecoverable unless you jump through the below 3 hoops, or create a new dynamic drive image, expand it to a size just over the size of your data on your original drive, then raw-copy the data across, that is… IMHO the steps below are easier!

A-B-C, Easy As…

  1. Defrag your Windows guest machine
  2. Now we need to replace all the “blank” (but still taking up space!) areas of our drive with zeros so we can recognise them to be stripped out later. Thankfully, this is really easy. Just download free (and tiny – 47KB) command-line utility SDelete from Microsoft and run it from within your virtual Windows guest machine with the following command (the -c switch is important!):
  3. Once that’s finished running shut down your virtual machine, navigate to the folder where your virtual machine hard drive is (such as ~/.VirtualBox/HardDisks) then from your host system run the following command to compress the hard drive down to a more reasonable size:

    So if your virtual machine name (and thus by default the hard drive name) was “XP_Client_1”, then you’d use:

With that done I trimmed down an excessively bloated 25GB .VDI of Windows 7 into a still excessively bloated 15GB – but that’s just in the nature of Microsoft OS’s… =P

Update: If you get an error stating things along the lines of:

then you can fix it like this:

  1. Detach the drive from your virtual machine,
  2. Edit the file ~/.VirtualBox/VirtualBox.xml and remove all lines with the drive you want to compact mentioned in the HardDrives section (Note: be careful you don’t delete the virtual machine entry itself from the MachineRegistry section! Only remove the drive from the HardDrives section.),
  3. Now you’ll be able to compact the drive, and when it’s done you can re-attach the drive to your virtual machine. Good as old! =D

Snap Happy

Another thing you can do to decrease the disk usage of VirtualBox machines is get rid of all your snapshots if you don’t need them anymore. Each snapshot is basically an entire disk image which you can roll back to, so if you have Windows 7 installed it’s about 7GB or so after a fresh install, if you then put on 500MB of patches and take another snapshot you’re storing another 7.5GB. If you then install Office or something and that takes up 2GB and take yet another snapshot you’re burning through yet another 9.5GB, so we’re up to 23.5GB already for a single 9GB drive!

You should definitely be careful when merging snapshots into the main image (basically getting rid of the snapshots), as it has the potential to break, but more likely it has the potential to confuse and cause you to throw away data you didn’t mean to. This is because of some particularly ambiguous and misleading phrasing used in VirtualBox circles – the crux of the matter being:

  • When you restore a snapshot, it will throw the current state and/or any subsequent snapshots away and leave the machine in the state defined by the snapshot you’re restoring. This can mean a lot of changes which currently exist in the image being undone, and lot of files disappear, for example – the only copy of important documents created since the snapshot you’re restoring was made. Use with care.
  • When you delete a snapshot, it will actually merge the current state of the machine into the snapshot before removing that snapshot and leaving the machine at it’s current merged state but without the snapshot existing…

Yeah, I know it’s confusing, so just be careful, okay? If you’ve got the space available just take a copy of the .VDI file from the HardDisks folder AS WELL AS a copy of the snapshots by copying the folder with the name of your VM from the Machines folder, and then merge in the snapshots – this way if it all goes nuts you can’t throw away the knackered copy and replace it with your pristine pre-merge copies.

Cheers!

Credits: Thanks to Damien for his article at MakeTechEasier for the initial information (you can also find out how to compress Linux guests there too, but just be aware that the technique he outlines involves cloning the drives then shrinking and re-importing them) and to Alphatek’s article for the simplification!

10 thoughts on “How To: Shrink/Compress a VirtualBox Windows Guest Machine”

  1. Hey there,

    when you get the error saying that the disk can not be registered, then you have to pass the UUID of the disk instead of the filename of the disk. In that case you do not have to edit any xml file.

    Cheers!

  2. I think your parameter is wrong. You should be using “sdelete -z”, which specifically zeroes free space. The -c parameter securely deletes content on free space by overwriting it, but does not guarantee to zero it.

    1. I think you’re absolutely right!

      Good spot – thanks, Mike!

      I’ll update the article accordingly.

      I did think you were absolutely right, and then I went to compress a virtualbox hard drive today, and the sdelete -z command pushed the dynamic drive size up to its size limit, and then it wouldn’t compress! Stick with sdelete -c!

      Cheers,
      r3dux

      1. I tried sdelete -c and the compact function actually grew my image by 2Gig. I went back with sdelete -z, and that did the trick. I’ve seen other references to -c as well. Maybe you just have to hold your mouth right…?

        Thanks for the great writeup!

        1. How curious!

          -z grew my dynamic drive to its maximum possible size and then –compact didn’t shrink it a single bit in my case – but using -c then –compact took multiple GB’s off the dynamic drive image.

          In my case I’m using a NTFS formatted virtual drive (i.e. guest) on an EXT4 formatted host drive – I wonder if the formatting configuration affects whether the -c or -z switches should be used with sdelete?

          Anyhoo – cheers for your input! =D

          1. Well -c did not work for me as expected (max sized vdi), but -z did, even though it still takes up more space than windos claims to.
            I used it on Windos XP with NTFS on Ext4, VirtualBox 4.1.12

            Another Mistery in the land of the microsofts…

            1. Hi, i think the problem is the version of sdelete.

              In 1.51 the help says -c = zero and -z = clean, in 1.6 it chance so -c = clean and -z = zero (i think in the older version it was wrong and is now corrected)
              This leads in a different bahavior in differen versions.

              You can see this change when you hit sdelete -?

              1. Great find! That looks like a very plausible explanation for why different switches are having different effects!

                Will update the article accordingly.

  3. Hi,

    I think your understanding of how snapshots work is a bit off! (Unless VDI files work differently to VHDs that is; I’ve only ever used VHDs)

    When you create a snapshot, it creates a new virtual hard drive file but that file only contains the changes, not a full copy. So if you install 500Mb of patches, the new file only stores those 500Mb. There’s lots of info out there about it so I’ll leave you to check it out and revise your article.

    Good advice about the restore and delete though, very misleading names!

    Regards,
    John

Leave a Reply

Your email address will not be published.