Aug 10 2017 0

Shrinking VirtualBox disk images

Since moving to an SSD drive space is a limited resource. For several development tasks I am using a virtual machine whose disk image (dynamically allocated storage) has grown to approximately 45.5 GB. To free up some space quickly (relative term) I decided to shrink this particular disk image.

At the time of this writing the VirtualBox tool modifyhd’ of VBoxManage only supports the disk format VDI.

!! Before continuing ensure you have a (verified) backup of any disk image to be shrunk.

Preparations within the virtual machine

Defrag disks

For the best result make sure all of the disks/drives within the virtual machine have been defragmented.

Disk Defragmenter

Wipe out free space

Next step is to overwrite the free space with zero, actually wiping out the data (not just marking the blocks within the FAT tables as free’). You can use the command line tool sdelete and run it on all drives within the virtual machine using the option -z.

sdelete -z c:
Running sdelete in the virtual box

Shrinking the disk image

And the last step is to physically shrink the Virtual Disk Image using the tool VBoxManage using the following command

VBoxManage.exe modifyhd c:\path\to\thedisk.vdi --compact
Running VBoxManage to shrink the Virtual Disk Image

Before

D:\VirtualBox VMs\OHI_WIN7>dir
 Volume in drive D is Data
 Volume Serial Number is BC22-D10F

 Directory of D:\VirtualBox VMs\OHI_WIN7

25-07-2017  16:16    <DIR>          .
25-07-2017  16:16    <DIR>          ..
01-08-2017  10:14    <DIR>          Logs
25-07-2017  16:16            10.849 OHI_WIN7.vbox
25-07-2017  12:30            10.849 OHI_WIN7.vbox-prev
01-08-2017  11:04    49.321.869.312 OHI_WIN7_disk1.vdi
25-07-2017  11:35    <DIR>          Snapshots
               3 File(s) 49.321.891.010 bytes
               4 Dir(s)  29.253.611.520 bytes free

After

D:\VirtualBox VMs\OHI_WIN7>dir
 Volume in drive D is Data
 Volume Serial Number is BC22-D10F

 Directory of D:\VirtualBox VMs\OHI_WIN7

01-08-2017  11:32    <DIR>          .
01-08-2017  11:32    <DIR>          ..
01-08-2017  10:14    <DIR>          Logs
01-08-2017  11:32            10.849 OHI_WIN7.vbox
25-07-2017  16:16            10.849 OHI_WIN7.vbox-prev
01-08-2017  12:29    42.115.006.464 OHI_WIN7_disk1.vdi
25-07-2017  11:35    <DIR>          Snapshots
               3 File(s) 42.115.028.162 bytes
               4 Dir(s)  36.460.265.472 bytes free

The disk image went from 45.5 GB down to 39.2 GB in size


Previous post
Why do programmers wear headphones
Next post
Helping out the old man (revisited) Last year July I wrote about how I connected some internet services to help my dad make it easier to get access to his electronic news paper and
This blog is powered by Blot