3. Exporting VMs

Linux

Zero out the free space

Install zerofree and boot to single user mode

sudo apt-get install zerofree

Press and hold the Shift key during the boot process to bring up the GNU GRUB menu. (If you see the Ubuntu splash screen you missed it)
Select the line which ends with "recovery mode” and in the following menu after the boot, "Drop to root shell prompt”

zerofree /dev/sda1

(and repeat for other partitions as needed)

Alternatively you can use the secure_delete package or sfill utilities, though they take more time
 
   sudo apt-get install secure_delete
 
   sfill -llzv /

Zero out the swap file

    cat /proc/swaps

    swapoff /dev/mapper/VolGroup00-LogVol01
    sswap -llzv /dev/mapper/VolGroup00-LogVol01
    swapon /dev/mapper/VolGroup00-LogVol01

Compress the image

VBoxManage modifyhd someDisk.vdi --compact





Windows



  • SID. This used to be a problem, or was it?...read this post you can add a /generalize if you fee the need
c:\Windows\System32\sysprep\sysprep.exe /oobe /unattend:C:\Windows\System32\sysprep\unattend.xml


  • Reduce the page file size (or turn it off).  Hint: you cannot set the initial size to 0. You can set it to 16 though, and use a small max size, say of half the vm's RAM
  • Set the pagefile to clear at shutdown.  - this writes zeros to the page file, allowing for better compression when handling virtual disk files
  • Clear the C:\Windows\SoftwareDistribution\Download folder
  • Turn off system restore points
  • Delete temporary files (from the drive properties)
  • Defrag - can't think of what this does exactly, other than allow some only partially filled clusters to be marked as empty.
  • Zero out the unused space, so it can be compressed
    • Downlaod the SDelete too from http://www.microsoft.com/technet/sysinternals/Security/SDelete.mspx
    • issue the command  "sdelete -c c:"  (and for any other drive you need as well)
    • This will expand the disk file to the full size if you are using a dynamic file (though you can shrink it back later for a net gain)



Sysprep the image (if you plan to distribute it)
  • Make sure the system is activated (or you will limit the number of types sysprep can be run)
  • Create and copy an xml file to the c:\Windows\System32\sysprep directory. More info and a sample here
  • Issue the sysprep with the "activate oobe", but leaving out the "generalize" pass. This saves a lot of time, but doesn't generate a new SID. This used to be a problem, or was it?...read this post you can add a /generalize if you fee the need
c:\Windows\System32\sysprep\sysprep.exe /oobe /unattend:C:\Windows\System32\sysprep\unattend.xml

(For more details see the sysprep page.)






A good list of other optimizations

http://vmetc.com/wp-content/plugins/download-monitor/download.php?id=1

Clearing the pagefile
http://technet.microsoft.com/en-us/library/cc740219%28WS.10%29.aspx







Sources


http://superuser.com/questions/529149/how-to-compact-virtualboxs-vdi-file-size
Comments