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 referrred 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 narative performances.
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. This working with qemu-img cheatsheet will show you a couple examples.
Commonly used qemu-img command options
qemu-img uses the following general syntax:
qemu-img subcommand [options]
The following list shows common subcommands for working with qemu-img:
|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.|
|- 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:
|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.|
Below are some of the examples of working with qemu-img .
Create a new disk image with
qemu-img create -f fmt fname size
Create raw disk image of size
# 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
vmdk to create such formats. Have a look at below for
# 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: : virtual size: 10737418240 filename: ubuntu.vmdk cluster size: 65536 format:
Convert disk image with
qemu-img convert -O out_fmt fname out_fname
vmdk image to
# 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 -f fmt fname
# qemu-img check ubuntu.vmdk No errors were found on the image.
Resize disk image with
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!
resize filename [+ | -]size
Example: Increase disk image by
# 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 note that the size was increased from
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.
Managing snapshots with
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)
- 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.
Example: Create of current state of VM with a name
# 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
Installing Operating system 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,