On Thu, Oct 22, 2020 at 2:40 PM Peter Krempa <pkrempa@redhat.com> wrote:
On Thu, Oct 22, 2020 at 10:57:05 +0800, Han Han wrote:
> Hello,
> I find "discard":"unmap" is defaultly enabled in  qemu cmdline(libvirt
> v6.6, qemu v5.1):
> XML:
> <disk type="file" device="disk">
>       <driver name="qemu" type="qcow2"/>
>       <source file="/var/lib/libvirt/images/new.qcow2" index="2"/>
>       <backingStore/>
>       <target dev="sda" bus="scsi"/>
>       <alias name="scsi0-0-0-0"/>
>       <address type="drive" controller="0" bus="0" target="0" unit="0"/>

[...]

> QEMU cmdline:

The -blockdev definition of storage splits up a storage into a protocol
node, which handles storage access:

In this case it's a 'file':

> ... -blockdev
> {"driver":"file","filename":"/var/lib/libvirt/images/new.qcow2","node-name":"libvirt-2-storage","auto-read-only":true,
> *"discard":"unmap"*}

You can see, that 'discard: unmap' is enabled only here.


The second layer is a 'format' node which handles the actual qcow2
format:

>-blockdev
> {"node-name":"libvirt-2-format","read-only":false,"driver":"qcow2","file":"libvirt-2-storage","backing":null}

Here you can see that there's no 'discard' setting, since you didn't
configure any setting in the disk.

Now the above configuration is used as we want to allow the QCOW2 driver
to discard it's unused internal sectors/structures. This can happen for
example when you delete an internal snapshot. All the unused sectors get
unref'd and discarded. This wouldn't be possible without enabling unmap
for the protocol node and the qcow2 image would not decrease size
otherwise.

The user setting is configured only for the 'format' node since it
handles whether the guest discard gets honoured or not.

The 'format' node is then installed as the disk backend:

Thx
> -device
> scsi-hd,bus=scsi0.0,channel=0,scsi-id=0,lun=0,device_id=drive-scsi0-0-0-0,drive=libvirt-2-format,id=scsi0-0-0-0,bootindex=1

[...]


> I think it's from
> https://gitlab.com/libvirt/libvirt/-/blob/master/src/qemu/qemu_block.c#L1211
> But I cannot find the reason from commit msgs or documents.
> Could you please explain it?

See above. I'm curious though why are you asking? You didn't mention
that there's any problem with that.

Yeah. No problems here. I was just a little surprised to find discard in qemu cmdline when no such configurations in libvirt domain XML.