Shrinking qcow2 images


This post is written as a recipe containing a set of commands to shrink the real and virtual size of a qcow2 image. There are different alternatives to achieve a similar result and probably some of them will be better, so if you know one of them, please post it as comment ;).

The starting point is a debian jessie qcow2 image. This image is based on the official debian Openstack image (available on the Debian CD image site) with some additional packages installed. The virtual size of this image is 10GB, but the file size is 2.1G:

$ ls -hl debian-redes.qcow2
-rw-r--r-- 1 user user 2.1G Dec  2 17:33 debian-redes.qcow2

$ qemu-img info debian-redes.qcow2 
image: debian-redes.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 4.0G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false

The virtual size is 10GB, but only around 8% of the disk size is used (virt-df is a tool included in the libguestfs-tools package):

$ virt-df -h debian-redes.qcow2 
Filesystem                                Size       Used  Available  Use%
debian-redes.qcow2:/dev/sda1              9.8G       770M       8.6G    8%

The first step is convert the qcow2 image format to raw because resize2fs-size option in guestfish doesn’t support the qcow2 format:

$ qemu-img convert -f qcow2 -O raw debian-redes.qcow2 debian-redes.raw
$ ls -hl debian-redes.raw
-rw-r--r-- 1 user user 10G Dec  2 17:38 debian-redes.raw

Now, the /dev/sda1 partition is going to be shrunk to 2 GB with guestfish:

$ guestfish 

Welcome to guestfish, the guest filesystem shell for
editing virtual machine filesystems and disk images.

Type: 'help' for help on commands
      'man' to read the manual
      'quit' to quit the shell

> <fs> add debian-redes.raw
> <fs> run
> <fs> list-filesystems 
/dev/sda1: ext3
> <fs> e2fsck-f /dev/sda1
> <fs> resize2fs-size /dev/sda1 2G
> <fs> e2fsck-f /dev/sda1
> <fs> quit

The last step doesn’t modify the raw image size:

$ ls -hl debian-redes.raw 
-rw-r--r-- 1 user user 10G Dec  2 17:39 debian-redes.raw

But the partition /dev/sda1 has been shrunk:

$ virt-df -h debian-redes.raw 
Filesystem                                Size       Used  Available  Use%
debian-redes.raw:/dev/sda1                1.9G       769M       1.1G   39%

Now we can use qemu-img to resize the image size (this step cannot be done previously because the size of /dev/sda1 was around 10 GB):

$ qemu-img resize -f raw debian-redes.raw 2.1G
Image resized.
$ qemu-img info debian-redes.raw 
image: debian-redes.raw
file format: raw
virtual size: 2.1G (2254858240 bytes)
disk size: 1.6G

And now convert it back to qcow2:

$ qemu-img convert -f raw -O qcow2 debian-redes.raw debian-redes.qcow2

Finally we can use virt-sparsify to make a sparse image that reduces the size of the image file:

$ virt-sparsify --compress debian-redes.qcow2 debian-redes-sparse.qcow2
[   0.0] Create overlay file in /tmp to protect source disk
[   0.0] Examine source disk
[   3.2] Fill free space in /dev/sda1 with zero
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ --:--
[   6.3] Copy to destination and make sparse
[  68.7] Sparsify operation completed with no errors.
virt-sparsify: Before deleting the old disk, carefully check that the 
target disk boots and works correctly

And the disk size and virtual size of the sparse image are:

$ ls -hl debian-redes-sparse.qcow2 
-rw-r--r-- 1 user user 322M Dec  2 17:43 debian-redes-sparse.qcow2

$ qemu-img info debian-redes-sparse.qcow2 
image: debian-redes-sparse.qcow2
file format: qcow2
virtual size: 2.1G (2254858240 bytes)
disk size: 321M
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false
Anuncios

, , , , ,

  1. Deja un comentario

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

A %d blogueros les gusta esto: