Working with qemu-img in Linux: This is a brief qemu-img cheatsheet for working with qemu-img command on Linux and Unix systems supporting qemu. I made this qemu-img cheatsheet for my own reference and saw a need to share with you guys. Before working with qemu-img command to perform any disk operation, It’s good to first understand what qemu is and how it is used in Linux and Virtualization world.

What is Qemu?

According to the Qemu about page, QEMU is defined as a generic and open source machine emulator and virtualizer. This means that Qemu can be used as machine emulator to run Operating systems and programs for one machine on a different machine. Example is running ARM program on x86 PC. In this case, QEMU can use other hypervisors like Xen or KVM for CPU extensions, to achieve what is commonly referred to as Hardware Assisted Virtualization

When QEMU is used as a virtualizer, it executes the guest code directly on the host machine CPU to achieve near narrative performances.  QEMU image formats are supported by most VPS cloud provides like Amazon, Digital Ocean, Linode, OVH and many others. I had earlier written a tutorial on how to install Qemu in Arch Linux and Manjaro:

Complete Installation of KVM,QEMU and Virt Manager on Arch Linux and Manjaro

What is qemu-img?

qemu-img is the command line utility that’s used to convert various file systems used by hypervisors like Xen, KVM, VMware, VirtualBox. qemu-img is used to format guest images, add additional storage devices and network storage e.t.c. The output image format can be imported to VPS cloud providers like Openstack and other providers like Cloudstack. This working with qemu-img cheatsheet will show you a couple examples.

Commonly used qemu-img command options

The qemu-img uses the following general syntax:

qemu-img subcommand [options]

The following list shows common subcommands for working with qemu-img:

 Subcommand  Description
create Used to create a new disk image on the file system.
check Used to check an existing disk image for errors.
convert Used to converts an existing disk image from one format to another.
info Used to display information about a disk image.
snapshot Used to manage snapshots of an existing disk image.
commit Use this to apply changes made to an existing disk image.
rebase Used to create a new base image based on an existing disk image.
resize Used to increase or decrease the size of an existing disk image.

Common options

 Option  Explanation
O output format
 -f specify disk format

Supported disk formats

By default, the format of an image is usually guessed automatically. The following formats are supported though:

 Format  Description
raw This is the default image format. Can be exported to all other emulators.
qcow2 QEMU image format which is most versatile format. Support AES encryption, zlib compression and multiple VM snapshots. It generates smaller images.
vmdk VMware compatible image format
cloop Useful only to reuse directly compressed CD-ROM images present e.g in Knoppix CD-ROMs. It is Linux compressed Loop image.

Working with qemu-img: Examples

Below are some of the examples of working with qemu-img .

Create a new disk image with qemu-img create

Syntax:

qemu-img create -f fmt fname size Create raw disk image of size 10 GB:

# qemu-img create -f raw ubuntu.img 10G
Formatting 'ubuntu.img', fmt=raw size=10737418240

# qemu-img info ubuntu.img 
image: ubuntu.img
file format: raw
virtual size: 10G (10737418240 bytes)
disk size: 0

Replace raw with qcow2, vdk, vmdk to create such formats. Have a look at below for vmdk:

# qemu-img create -f vmdk ubuntu.vmdk 10G
Formatting 'ubuntu.vmdk', fmt=vmdk size=10737418240 compat6=off hwversion=undefined

# qemu-img info ubuntu.vmdk            
image: ubuntu.vmdk
file format: vmdk
virtual size: 10G (10737418240 bytes)
disk size: 12K
cluster_size: 65536
Format specific information:
    cid: 1484281290
    parent cid: 4294967295
    create type: monolithicSparse
    extents:
        [0]:
            virtual size: 10737418240
            filename: ubuntu.vmdk
            cluster size: 65536
            format: 

Convert disk image with qemu-img convert

Syntax:

qemu-img convert -O out_fmt fname out_fname

Example: Convert vmdk image to qcow2

# qemu-img create -f vmdk ~/ubuntu.vmdk 10G
Formatting '/home/josepy/ubuntu.vmdk', fmt=vmdk size=10737418240 compat6=off hwversion=undefined

# qemu-img convert -O qcow2 ~/ubuntu.vmdk ~/ubuntu.qcow2

# qemu-img info ~/ubuntu.qcow2 
image: /home/josepy/ubuntu.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false

Check for disk with qemu-img check

Syntax:

qemu-img check -f fmt fname Example:

# qemu-img check ubuntu.vmdk 
No errors were found on the image.

You can also convert vmdk to vhd. Virt install can use a converted image for new installation.

Resize disk image with qemu-img resize

WARNING: Before using this command to shrink a disk image, you MUST use file system and partitioning tools inside the VM to reduce allocated file systems and partition sizes accordingly. Failure to do so will result in data loss!

Syntax: resize filename [+ | -]size

Example: Increase disk image by 3GB

# qemu-img create -f qcow2  ubuntu.qcow2 10G
Formatting 'ubuntu.qcow2', fmt=qcow2 size=10737418240 encryption=off cluster_size=65536 lazy_refcounts=off refcount_bits=16

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

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

# qemu-img resize ubuntu.qcow2 +3G          
Image resized.

# qemu-img info ubuntu.qcow2                
image: ubuntu.qcow2
file format: qcow2
virtual size: 13G (13958643712 bytes)
disk size: 200K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false

You should notice that the size was increased from 10GB to 13GB

NOTE: After enlarging the disk image, you must use file system and partitioning tools inside the virtual machine to actually begin using the new space.

Working with qemu-img: Managing snapshots with qemu-img snapshot

Snapshots are often used to save virtual machine in a particular state. It is a snapshot of the complete environment in which a VM Guest is running. A snapshot normally includes the state of:

  • memory (RAM)
  • the processor (CPU)
  • devices
  • And all writable disks.

In order to use snapshots, your VM Guest must contain at least one writable hard disk image in qcow2 format. This device is usually the first virtual hard disk. Working with qemu-img snapshots is illustrated below.

Example: Create of current state of VM with a name ubuntu_fresh

# qemu-img snapshot -c ubuntu_fresh ubuntu.qcow2

To list snapshots of VM , use:

# qemu-img snapshot -l ubuntu.qcow2 
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         ubuntu_fresh              0 2017-03-06 18:41:21   00:00:00.000

If something breaks in your Virtual machine and you need to restore the state of the saved snapshot, you the id given by command above:

# qemu-img snapshot -a 1  ubuntu.qcow2

To delete snapshot:

# qemu-img snapshot -d 1 ubuntu.qcow2

Rebasing derived images

# qemu-img rebase -b /new/ubuntu.raw ubuntu.qcow2

Working with qemu-img: Install OS to created disk image

To install an operating system into your disk image, you need the installation medium such as cdrom or ISO image file. The installation medium should not be mounted because QEMU accesses the media directly.

# qemu-system-x86_64 \
-m 512\
-hda ~/Desktop/virt/arch.qcow2  \
-cdrom ~/iso/CentOS-7-x86_64-Everything-1611.iso \
-enable-kvm

You can specify multiple images: -hda ubuntu.qcow2 -hdb files.img -hdc container.qcow2. ISO image location can be cdrom device, -cdrom dev/cdrom

Further reading:

Mastering KVM Virtualization

Virtualization Essentials