How To

Create, Resize and convert VM images in KVM using Qemu-img

qemu-img is a powerful command-line tool that lets you create, convert, and inspect virtual machine disk images with ease.

Original content from computingforgeeks.com - post 88444

It supports a wide range of image formats, including:

  • QCOW2 – the most common format with snapshot support
  • RAW – simple and fast, great for performance
  • VDI – used by VirtualBox
  • VMDK – VMware’s disk format
  • …and many more

With qemu-img, you can:

  • Create new virtual machine disks
  • Convert images between different formats
  • Check and repair disk images
  • Resize or modify virtual machine disks

Perfect for anyone working with KVM, Proxmox, VirtualBox, or VMware environments.

Example 1: Create a New Image

Let us create an image in the default libvirt storage pool path.

sudo qemu-img create -f qcow2 /var/lib/libvirt/images/deb12.qcow2 20G
  • -f qcow2: Specifies the image format (QCOW2 is a common format for QEMU/KVM).
  • 20G: Creates a 20GB virtual disk.

A virtual machine that uses the disk image can be created using virt-install

sudo virt-install \
--name debian12 \
--ram 4096 \
--vcpus 2 \
--disk path=/var/lib/libvirt/images/deb12.qcow2 \
--os-variant debian12 \
--network bridge=virbr0 \
--graphics none \
--console pty,target_type=serial \
--location /var/lib/libvirt/images/debian-12.7.0-amd64-DVD-1.iso \
--extra-args 'console=ttyS0,115200n8'

Installation will start in text mode.

...
┌───────────────────────┤ [!!] Select a language ├────────────────────────┐
│ │
│ Choose the language to be used for the installation process. The │
│ selected language will also be the default language for the installed │
│ system. │
│ │
│ Language: │
│ │
│ C │
│ English │
│ │
│ <Go Back> │
│ │
└─────────────────────────────────────────────────────────────────────────┘

<Tab> moves; <Space> selects; <Enter> activates buttons

Example 2: Resize an Image

We can resize an image by adding extra 10GB to it using resize command option:

qemu-img resize /var/lib/libvirt/images/deb12.qcow2 +10G

Example 3: Inspect an Image

The qemu-img info command provides detailed information about a virtual machine disk image, including its format, size, and metadata.

qemu-img info /var/lib/libvirt/images/deb12.qcow2

Example 4: Converting between image formats

It is generally straightforward to convert images from one format to another using qemu-img:

Image formatArgument to qemu-img
QCOW2 (KVM, Xen)qcow2
QED (KVM)qed
rawraw
VDI (VirtualBox)vdi
VHD (Hyper-V)vpc
VMDK (VMware)vmdk

Refer to the examples below to learn how image conversion can be done from one format to the other.

Example 5: Grow image / filesystem

Get default filesystem disk size using qemu-img:

IMAGE=ubuntu-24.04-amd64.img
qemu-img info $IMAGE

In the output, we can confirm the disk size before growing it.

image: ubuntu-24.04-amd64.img
file format: qcow2
virtual size: 3.5 GiB (3758096384 bytes)
disk size: 581 MiB
cluster_size: 65536
Format specific information:
compat: 1.1
compression type: zlib
lazy refcounts: false
refcount bits: 16
corrupt: false
extended l2: false
Child node '/file':
filename: ubuntu-24.04-amd64.img
protocol type: file
file length: 581 MiB (608755712 bytes)
disk size: 581 MiB

Suppose we want to grow disk size by 20GB

qemu-img resize $IMAGE +20G

Confirmation after the change:

$ qemu-img info $IMAGE
image: ubuntu-24.04-amd64.img
file format: qcow2
virtual size: 23.5 GiB (25232932864 bytes)
...

The -f format flag is optional. If omitted, qemu-img will try to infer the image format.

  • Convert a raw image file to a qcow2 image file.
qemu-img convert -f raw -O qcow2 image.img image.qcow2
  • convert a vmdk image file to a raw image file.
qemu-img convert -f vmdk -O raw image.vmdk image.img
  • Convert a vmdk image file to a qcow2 image file.
qemu-img convert -f vmdk -O qcow2 image.vmdk image.qcow2
  • Convert qcow2 to vdi
qemu-img convert -O vdi test.qcow2 test.vdi
  • Convert qcow2 to raw
qemu-img convert -O raw test.qcow2 test.raw
  • Convert qcow2 to vmdk
qemu-img convert -O vmdk test.qcow2 test.vmdk

Related Articles

Vagrant How To run Kali Linux on VirtualBox with Vagrant Virtualization How To Enable IOMMU in KVM host system CentOS Install oVirt Compute Node on CentOS Stream 9 / Rocky 9 KVM Prepare Guest Image For Cloning with virt-sysprep

Leave a Comment

Press ESC to close