Skip to content

Compress Virtual OS Image

by drphrozen on June 20th, 2010

Here is a simple guide for those who need to export an “Virtual Appliance” from VirtualBox, Xen, VMware Workstation/Player or others. With this guide it is possible to compress images even further, by cleaning the targets partitions. This guide is for Ubuntu, but should work for any Linux machine.
I use zerofree instead of dd for ext partitions because it is faster, and dd for swap. A simple way to do this could be booting from a Ubuntu live disk in the Virtual Appliance instance, apt-get zerofree (universe) and then follow these steps:

$ sudo fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0009e93c

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        2497    20051968   83  Linux
/dev/sda2            2497        2611      916481    5  Extended
/dev/sda5            2497        2611      916480   82  Linux swap / Solaris
$ sudo zerofree /dev/sda1
$ sudo swapoff /dev/sda5
$ sudo dd if=/dev/zero of=/dev/sda5
dd: writing to `/dev/sda5': No space left on device
1832961+0 records in
1832960+0 records out
938475520 bytes (938 MB) copied, 25.5471 s, 36.7 MB/s
$ sudo mkswap /dev/sda5
Setting up swapspace version 1, size = 916476 KiB
no label, UUID=fb59e480-e471-405e-98e9-37fd16fe5ac6

From → Uncategorized

One Comment
  1. drphrozen permalink

    http://partedmagic.com/ could be used as an alternative to Ubuntu Live CD, also this dist should contain zerofree.

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS