KVM (Kernel Virtualization Module) is an open source, popular and efficient virtualization technology provided by Linux kernel. Virtualization creates virtual ram, devices, disks, CPU’s, networks etc. Qemu is the user space tool used to provide these to the kernel level virtualization tool KVM. Disk is one of the most important device in the virtual systems. Qemu-img is the tool used to create, manage, convert shrink etc. the disk images of virtual machines.
Help
We can get help about the qemu-img
command with the -h
option.
$ qemu-img -h

Syntax
Syntax of the qemu-img
command is like below.
qemu-img create -f fmt -o options fname size
Create Disk Image
Virtual Machines or simply VM’s runs operating system and user level tools like normal PC. Operating system and user level tools are stored in disk image files like physical disks. Generally all VM’s uses one disk image file to read and write data. We will use create
command to create a disk image. In the example the disk image name will be ubuntu.img
. We will provide disk size after disk name which is 10G
in gigabyte.
$ qemu-img create ubuntu.img 10G

After disk image create some information is provided. The disk image format is raw
and size is expressed as byte. We will look image formats below. Disk image actual size is 0 because there is no data in it but the vm will see disk image as 10G disk and will be able to use up to 10G.
Disk Image Types
As stated before qemu supports different type of disk image formats. Disk image format is different than file systems. Disk image format is related with the host system.
Raw
Raw is default format if no specific format is specified while creating disk images. Raw disk images do not have special features like compression, snapshot etc. Best thing about raw disk image is performance. Raw disk images are faster than other disk image types.
Qcow2
Qcow2 is opensource format developed against Vmdk and Vdi. Qcow2 provides features like compression, snapshot, backing file etc. It is popular in the Kvm, Qemu community.
Qed
Qed is a disk format provided by Qemu. It provides support for overlay and sparse images. Performance of Qed is better than Qcow2 .
Qcow
Qcow is predecessor of the Qcow2.
Vmdk
Vmdk is default and popular disk image format developed and user by VMware. Vmware products like Player, Workstation, ESXi, uses vmdk disk image type. It is rival of Qcow2 format
Vdi
Vdi is popular format developed Virtual Box. It has similar features to the Vmdk and Qcow2
Vpc
Vps is format used by first generation Microsoft Virtualization tool named Virtual PC. It is not actively developed right now.
Create Qcow2 Disk Image
We can create qcow image with the -f
option. We will also provide the disk size as an option. As you remember in the first example we have specified the disk size at the and of the command. Now we will set disk size with -o size
option. In the example we create a qcow2 formatted disk image named ubuntu.img
with the size of 10GB
.
$ qemu-img create -f qcow2 -o size=10G ubuntu.img

Create VMDK Disk Image
In this example we will create an vmdk disk image. We will set disk file name as ubuntu.vmdk
. But the extension vmdk
do not have an effect on the disk image type. We will also set disk image size 20GB
with -o size
option.
$ qemu-img create -f vmdk -o size=20G ubuntu.img

Get Information About Disk Image
Information about a disk file can be needed. As we learned previously the extension is not a sole disk image type shower. To get detailed information we will use info
command by providing disk image name. In the example we will list information about ubuntu.vmdk
$ qemu-img info ubuntu.img

image
shows the disk image file namevirtual size
shows the disk size of the image which will be read by VMdisk size
show the real size which hold in the host file system. It is 16K because there is no data on the disk image file
Shrink Disk Image
After some usage disk image file sizes grows. Even we delete files in the vm the size in the host system remains the same. Because the host system do not know how much and which part of the disk image file is unused. Shrink is done with convert
command and copying existing disk image in to new one. In the example we will shrink disk image file named ubuntu.qcow2
into ubuntu_s.qcow2
.
$ qemu-img convert -O qcow2 ubuntu.qcow2 ubuntu_s.qcow2
Compress Disk Image
Another useful feature of the qemu-img is disk compression. Compressing disk images will make more space for other VMs. But keep in mind compression will little performance loss. We will use convert
command to compress. In the example we will compress disk image file ubuntu.qcow2
into new file named ubuntu_c.qcow2
with -c
option. This may take some time according to disk image file size and disk performance of the host system.
$ qemu-img convert -O qcow2 -c ubuntu.qcow2 ubuntu_c.qcow2
Check Disk Image For Errors
Sometime while working with VM’s and disk images there can be errors. With qemu-img command these errors can be fixed. We will use check
command for this operation. In this example we will provide disk image file name with the check
command. Disk image format can be omitted because in most situations it is detected automatically.
$ qemu-img check ubuntu.qcow2

Increase Disk Image Size
While creating disk image files we generally give small amount of disk. But this may become problem in the feature as unexpectedly. We may need to increase the disk image file size. We will use resize
command in order to increase disk size.In the example we will add extra 5GB
to the disk image file named ubuntu.qcow2
.
$ qemu-img resize ubuntu.qcow2 +5GB

We can see from screenshot that new size is 25GB
1 thought on “Linux Qemu-img Command Tutorial With Examples to Create, Change, Shrink Disk Images”