Defrag and compact .vdi images

If you have a lot of Virtualbox images (.vdi) disk space starts to become an issue (dynamically allocated images). So to reduce the size of the images there is a couple of tools we can use, and scripting to get the job done faster.

You might be familiar with "VBoxManage.exe modifyvdi my_vm.vdi --compact", this command and arguments will re-compact a .vdi image. But the way it works is by finding the end of data in the .vdi image file and saying from here an onwards is dynamic. Unfortunatly junk data (deleted files) in the image would still be considered as being data because it's not "empty".

This article will focused around running a Windows based operating system inside VirtualBox.

Tools we are gone need

To clear the junk data after defragmenting we are gone use the tool SDelete v1.61 by Mark Russinovich.

Visit/Download at http://technet.microsoft.com/en-au/sysinternals/bb897443.aspx

And we are gone use a different defragmenting tool: JkDefrag 3.36 by Jeroen Kessels. (Note that the latest version is now called MyDefrag, but because of the simple command line options we will use JkDefrag.)

Visit/Download at http://www.kessels.com/jkdefrag/

(VBoxManage.exe comes with VirtualBox)

Compacting .vdi images on the host system

Let's start with a simple batch script to compact our .vdi files. The script below will use the command mentioned above to compact all the images in the same directory as it resides (You do not need to alter the script).

Put this script where your VirtualBox .vdi files are stored, and you run the script standalone whenever you need to compact the image(s) in that folder.

compact_vdi_images.bat

@ECHO OFF
SET VBOXMANAGE=%ProgramFiles%\Oracle\VirtualBox\VBoxManage.exe
ECHO Compacting VirtualBox .vdi image(s) using VBoxManage...
FOR /f "delims=." %%a in ('dir *.vdi /b') do "%VBOXMANAGE%" modifyvdi "%%a.vdi" --compact

If you have many images, and want compact all images in one go then add the script below to the parent directory. It will simply run thru all the folders and call compact_vdi_images.bat if it exists.

compact_all_vdi_images.bat

@ECHO OFF
FOR /d %%i IN (*.*) DO (
  IF EXIST "%%i\compact_vdi_images.bat" (
     CD "%%i"
     ECHO Processing: %%i
     CMD /C compact_vdi_images.bat
     CD ..
  )
)

Your directory structure should end up look something like this:

Running the compact_all_vdi_images.bat script is gone take some time if you have a lot of images, or big ones.

C:\Documents and Settings\example\VirtualBox VMs>compact_all_vdi_images.bat
Processing: windows_xp_sp1_test
Compacting VirtualBox .vdi image(s) using VBoxManage...
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Processing: win_xp_legacy_games
Compacting VirtualBox .vdi image(s) using VBoxManage...
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Processing: my_cool_machine
Compacting VirtualBox .vdi image(s) using VBoxManage...
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Processing: win_xp_sp3_test
Compacting VirtualBox .vdi image(s) using VBoxManage...
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

C:\Documents and Settings\example\VirtualBox VMs>

Defragmenting and zeroing empty space

In order to get a higher compact ratio we need to defragment, and zero the empty data on the virtual machine before compact them on the host machine.

We start by downloading JkDefrag and SDelete, and create a folder on the VM called c:\scripts. Extract the downloaded zip files in here (including folder names).

And we create a small batch script, defrag_drive_and_optimze.bat and place it in the same folder. JkDefrag will run with "fast defrag and optimize" parameters first, then SDelete with -z option for "zero free space".

defrag_drive_and_optimze.bat

@ECHO ON
JkDefrag-3.36\JkDefragCmd.exe -a 3
SDelete\sdelete.exe -z c:
C:\scripts>JkDefrag-3.36\JkDefragCmd.exe -a 3
JkDefrag v3.36
Date: 2014/05/17
Windows version: v5.1 build 2600 Service Pack 3
Commandline argument '-a' accepted, optimizemode = 3
...
Phase 1: Analyze
This is an NTFS disk.
Phase 2: Defragment
Phase 3: Fixup
Zone 1: Fast Optimize
Zone 2: Fast Optimize
Zone 3: Fast Optimize
Phase 3: Fixup
Finished.

C:\scripts>SDelete\sdelete.exe -z c:

SDelete - Secure Delete v1.61
Copyright (C) 1999-2012 Mark Russinovich
Sysinternals - www.sysinternals.com

Free space cleaned on C:\
1 drives zapped

C:\scripts>

And finally we create a shortcut to the script(s) on the desktop.

Download the scripts used in this article:

defrag_and_compact_vdi_images.zip

In this article VirtualBox Version 4.3.10 was used to run Windows XP SP3 virtually, and test the scripts mentioned above.

Site maintained by tommy@trapv.com.
Page generated in: 0.0041 seconds.
Memory usage: 428904 bytes (peak: 475328 bytes)