Qcow2 could create through qemu-img command but by-default this command don’t create Meta-data on these files, we have to mention it or otherwise we can convert these Qcow2 image files. So in this post we will see how we could create Qcow2 image with Meta-data

Sometime when we attach Qcow2 image disk on kvm virtual machine, these images are not usable on these virtual machine..

SetUP

In this setup we are using KVM virtual machine on Ubuntu 18.04

qemu-img --version
qemu-img version 2.11.1(Debian 1:2.11+dfsg-1ubuntu7.23)
Copyright (c) 2003-2017 Fabrice Bellard and the QEMU Project developers

kvm --version
QEMU emulator version 2.11.1(Debian 1:2.11+dfsg-1ubuntu7.23)
Copyright (c) 2003-2017 Fabrice Bellard and the QEMU Project developers

Ubuntu Version

lsb_release -d
Description:	Ubuntu 18.04.4 LTS

Qcow2 image creation

How to create Qcow2 image on host machine and attach it Virtual Machine.

# qemu-img create -f qcow2 vdisk1 40G
Formatting 'vdisk1', fmt=qcow2 size=42949672960 cluster_size=65536 lazy_refcounts=off refcount_bits=16

# ls -lh vdisk1 vdisk2
-rw-r--r-- 1 ssirohi ssirohi 193K Apr 19 16:30 vdisk1

Above we create 40 GB of file, But this file don’t have meta-data and both have only 193 KB of size.

If we check these files withqemu-img info

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

Attach of Qcow2 Disk on Virtual Machine

Now we attach Qcow2 on Virtual Machine with below command.In previous post we did it earlier

▶ virsh attach-disk centos7_1 --source /data/vm_images/centos7_1/vdisk1 --target vdc --driver qemu
Disk attached successfully

In above command we attach Disk to virtual machine. Now we need to see are we able to detect and use this disk on virtual machine or not

[root@srv70 ~]# sfdisk -s | grep vdc
/dev/vdc:       192

So , we can detect Disk on virtual machine but can we create partitions and use this Disk as normal disk.
We need to use fdisk command to use this Disk like below

[root@srv70 ~]# fdisk /dev/vdc
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xab717b86.
You must set cylinders.
You can do this from the extra functions menu.

Command (m for help): n
You must set cylinders.
You can do this from the extra functions menu.

Command (m for help): q

As we see above we are not able use this Disk inside virtual machine. Now we need to know how can make this available to use in Machine.

Actually this Disk is missing of meta-data which qemu-img command is not create by-default on images, we need to care while creating it or we could covert these images, so let’s take both cases

How to include Meta-Data Qcow2 file

First we need to detach Disk from Virtual machine, as we are not able to use this disk, we can safely detach this disk from virtual machine, in previous post we cover this topic in detail

▶ virsh detach-disk centos7_1 vdc
Disk detached successfully

After this we could convert Qcow2 Disk to Disk that contain Meta-data Disk.

▶ qemu-img convert -f qcow2 -O qcow2 -o preallocation=metadata vdisk1 vdisk3

▶ ls -lh vdisk1 vdisk2
-rw-r--r-- 1 root root 193K Apr 19 18:41 vdisk1
-rw-r--r-- 1 root root  41G Apr 19 19:05 vdisk2

▶ du -sh vdisk2
6.5M	vdisk2

Now we have new disk vdisk2 which has 41 GB space, But in actual its just contain 6.5 MB of space which contain Meta-Data. Let’s attach this disk again to Virtual Machine. Now we have no use of vdisk1, so i am moving vdisk2 to vdisk1

▶ virsh attach-disk centos7_1 --source /data/vm_images/centos7_1/vdisk1 --target vdc --driver qemu
Disk attached successfully

On Machine

[root@srv70 ~]# sfdisk -s | grep vdc
/dev/vdc:  41949696

Now we can see disk is attached and we can see exact size and we can use it through fdisk command

[root@srv70 ~]# fdisk /dev/vdc
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xe8659566.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-83899391, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-83899391, default 83899391): +10G
Partition 1 of type Linux and of size 10 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@srv70 ~]# fdisk -l /dev/vdc

Disk /dev/vdc: 43.0 GB, 42956488704 bytes, 83899392 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xe8659566

   Device Boot      Start         End      Blocks   Id  System
/dev/vdc1            2048    20973567    10485760   83  Linux

Now we know how to create Qcow2 Disk and how we can convert it to usable one. Let’s see how we can create a Qcow2 Disk with Meta-Data on first place. Conversion of Disk type is also covered in another topic in previous post

▶ qemu-img create -f qcow2 vdisk2 -o size=50G,preallocation=metadata 
Formatting 'vdisk2', fmt=qcow2 size=53687091200 cluster_size=65536 preallocation=metadata lazy_refcounts=off refcount_bits=16

Attach with Virtual Machine

▶ virsh attach-disk centos7_1 --source /data/vm_images/centos7_1/vdisk2 --target vdd --driver qemu
Disk attached successfully

Both Disk on Virtual Machine

[root@srv70 ~]# sfdisk -s | grep vd[c,d]
/dev/vdc:  41949696
/dev/vdd:  52437056

With Above discussion we are clear that qemu-img command can create Qcow2 file that we could use in Virtual machine, but these Disk has no use in virtual machines and we need Disk Meta-Data that could covert or we need to care while creating Qcow2 image through qemu-img command.