# qcow2 image usage
###### tags: `linux` `qcow2`
#### create qcow2 image
```
/usr/bin/qemu-img create -f qcow2 -o preallocation=metadata /export/vmimgs/glacier.qcow2 8G
```
> NOTE: At this point in time, preallocation=metadata option is the best we can do to extract max. possible (near RAW) I/O performance out of QCOW2 format.
#### show actual usage of image
```
[root@moon tmp]# ls -lash /export/vmimgs/glacier.qcow2
970M -rw-r--r--. 1 qemu qemu 8.1G Sep 24 23:45 /export/vmimgs/glacier.qcow2
```
#### resize image
```
qemu-img resize ubuntu-server.qcow2 +5GB
```
#### mount qcow2 image
load NBD kernel module
```
root@NUC10:~# modprobe nbd max_part=8 nbds_max=4
root@NUC10:~# ls -l /dev/nbd*
brw-rw---- 1 root disk 43, 0 May 28 20:49 /dev/nbd0
brw-rw---- 1 root disk 43, 4 May 28 20:49 /dev/nbd1
brw-rw---- 1 root disk 43, 8 May 28 20:49 /dev/nbd2
brw-rw---- 1 root disk 43, 12 May 28 20:49 /dev/nbd3
```
> max_part - Number of partitions per device (default: 0).
> nbds_max - Number of block devices that should be initialized (default: 16).
connect image as a NBD
```
root@NUC10:~# qemu-nbd --connect=/dev/nbd0 /mnt/data/storage-pool/lab/lab-test.qcow2
root@NUC10:~# fdisk -l /dev/nbd0 -l
Disk /dev/nbd0: 16 GiB, 17179869184 bytes, 33554432 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
Disklabel type: gpt
Disk identifier: 029F65E0-4F19-4249-A6C1-3D3D3B82F0CF
Device Start End Sectors Size Type
/dev/nbd0p1 2048 4095 2048 1M BIOS boot
/dev/nbd0p2 4096 33552383 33548288 16G Linux filesystem
root@NUC10:~# ls -l /dev/nbd*
brw-rw---- 1 root disk 43, 0 May 28 20:51 /dev/nbd0
brw-rw---- 1 root disk 43, 1 May 28 20:51 /dev/nbd0p1
brw-rw---- 1 root disk 43, 2 May 28 20:51 /dev/nbd0p2
brw-rw---- 1 root disk 43, 4 May 28 20:49 /dev/nbd1
brw-rw---- 1 root disk 43, 8 May 28 20:49 /dev/nbd2
brw-rw---- 1 root disk 43, 12 May 28 20:49 /dev/nbd3
```
> The linux filesystem partion is on /dev/nbd0p2
mount the filesystem partition on the image
```
root@NUC10:~# mkdir /tmp/qcow2-image
root@NUC10:~# mount /dev/nbd0p2 /tmp/qcow2-image/
root@NUC10:~# ls -l /tmp/qcow2-image/
total 2097240
lrwxrwxrwx 1 root root 7 Feb 2 2021 bin -> usr/bin
drwxr-xr-x 3 root root 4096 Mar 23 00:25 boot
drwxr-xr-x 2 root root 4096 Sep 12 2021 cdrom
drwxr-xr-x 5 root root 4096 Feb 2 2021 dev
drwxr-xr-x 94 root root 4096 Mar 23 00:24 etc
drwxr-xr-x 3 root root 4096 Sep 12 2021 home
lrwxrwxrwx 1 root root 7 Feb 2 2021 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Feb 2 2021 lib32 -> usr/lib32
lrwxrwxrwx 1 root root 9 Feb 2 2021 lib64 -> usr/lib64
lrwxrwxrwx 1 root root 10 Feb 2 2021 libx32 -> usr/libx32
drwx------ 2 root root 16384 Sep 12 2021 lost+found
drwxr-xr-x 2 root root 4096 Feb 2 2021 media
drwxr-xr-x 2 root root 4096 Feb 2 2021 mnt
drwxr-xr-x 2 root root 4096 Feb 2 2021 opt
drwxr-xr-x 2 root root 4096 Apr 15 2020 proc
drwx------ 4 root root 4096 Mar 27 17:48 root
drwxr-xr-x 11 root root 4096 Feb 2 2021 run
lrwxrwxrwx 1 root root 8 Feb 2 2021 sbin -> usr/sbin
drwxr-xr-x 7 root root 4096 Sep 18 2021 snap
drwxr-xr-x 2 root root 4096 Feb 2 2021 srv
-rw------- 1 root root 2147483648 Sep 12 2021 swap.img
drwxr-xr-x 2 root root 4096 Apr 15 2020 sys
drwxrwxrwt 8 root root 4096 Mar 27 17:49 tmp
drwxr-xr-x 14 root root 4096 Feb 2 2021 usr
drwxr-xr-x 13 root root 4096 Feb 2 2021 var
```
To unmount
```
root@NUC10:~# umount /tmp/qcow2-image
root@NUC10:~# qemu-nbd --disconnect /dev/nbd0
/dev/nbd0 disconnected
root@NUC10:~# rmmod nbd
```
#### Reference
https://www.kernel.org/doc/Documentation/blockdev/nbd.txt