Last Updated:

Reducing the size of a Linux VHD image

Andy Linux

Here's a little tip to make your virtual hard drive images zip to as small as possible for distribution. On various projects i have had to distribute Linux images for flashing onto a VHD card. The size of the image is the size of the SD card and when zipped is often not much smaller.

So - before making the image with your SD card reader - Boot the device using the the SD card. Now, from the shell, delete all unused content and then perform the following to make sure that all unused space is filled with zero's - this allows your zip program to make the file much smaller. You can run the command “df” to see how much space is unused. See how many blocks are available


->dd  if=/dev/zero  of=deleteme1  bs=16384 count=20000


This will create a file called “deleteme1” of size 328Mb  filled with zero’s (16384 times 20000 )

You can increase the size by increasing the count.

Run again and create deletme2, deleteme3 etc

Run dd and make sure you have used up most of  the space 
In this way you have effectively filled all the unused space on the CF card with 0’s. This can be compressed very well.

->rm deleteme*  

 to delete  all the files
 
Now pop the SD card into a card reader and use Winimage.exe (or similar) to create a vhd file. Now Zip the vhd file and it will be much smaller.