Bug 2174700

Summary: [snapshot] when deleting external snapshot with new disk attached, error popped by snapshot actually deleted
Product: Red Hat Enterprise Linux 9 Reporter: yisun
Component: libvirtAssignee: Pavel Hrdina <phrdina>
libvirt sub component: General QA Contact: yisun
Status: CLOSED ERRATA Docs Contact:
Severity: medium    
Priority: medium CC: jdenemar, lmen, pkrempa, virt-maint, ymankad
Version: 9.2Keywords: AutomationTriaged, Triaged
Target Milestone: rcFlags: pm-rhel: mirror+
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: libvirt-9.3.0-1.el9 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2023-11-07 08:30:47 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version: 9.3.0
Embargoed:
Bug Depends On:    
Bug Blocks: 1519002    

Description yisun 2023-03-02 07:37:55 UTC
Description of problem:
[snapshot] when deleting external snapshot with new disk attached, error popped by snapshot actually deleted

Version-Release number of selected component (if applicable):
libvirt-9.0.0-7.el9.x86_64

How reproducible:
100%

Steps to Reproduce:
Pls note, this only happens with at least 4 snapshots generated.

1. Prepare a running vm with only one disk
# virsh domblklist vm1
 Target   Source
---------------------------------------------
 vda      /var/lib/libvirt/images/vm1.qcow2

2. Create 2 external snapshots for it
# virsh snapshot-create-as vm1 s1 --diskspec vda,snapshot=external --disk-only
Domain snapshot s1 created
# virsh snapshot-create-as vm1 s2 --diskspec vda,snapshot=external --disk-only
Domain snapshot s2 created

3. Attach a disk to the vm
# cat vdb.xml 
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/var/lib/libvirt/images/vdb.qcow2'/>
      <backingStore/>
      <target dev='vdb' bus='virtio'/>
    </disk>

# virsh attach-device vm1 vdb.xml 
Device attached successfully

# virsh domblklist vm1
 Target   Source
---------------------------------------------
 vda      /var/lib/libvirt/images/vm1.s2
 vdb      /var/lib/libvirt/images/vdb.qcow2

4. create 3rd snapshot for it (here mem snapshot involved for future check, but mem snapshot is not necessary)
# virsh snapshot-create-as vm1 s3 --memspec /tmp/mem.s3,snapshot=external --diskspec vda,snapshot=external vdb,snapshot=external
Domain snapshot s3 created

# ll -h /tmp/mem.s3 
-rw-------. 1 root root 744M Mar  2 02:19 /tmp/mem.s3

# virsh domblklist vm1
 Target   Source
------------------------------------------
 vda      /var/lib/libvirt/images/vm1.s3
 vdb      /var/lib/libvirt/images/vdb.s3

(5. generate some data in vm's vdb for future check, this is not a necessary step)
vm# mkfs.ext4 /dev/vdb -F
vm# mount /dev/vdb /mnt/
vm# dd if=/dev/urandom of=/mnt/test_data bs=1M count=100; sync
vm# md5sum /mnt/test_data 
32d7c3b497ce32be440318edd743be81  /mnt/test_data

6. Create 4th snapshot for vm
# virsh snapshot-create-as vm1 s4 --diskspec vda,snapshot=external --disk-only
Domain snapshot s4 created

# virsh domblklist vm1
 Target   Source
------------------------------------------
 vda      /var/lib/libvirt/images/vm1.s4
 vdb      /var/lib/libvirt/images/vdb.s4

7. try to delete the 3rd snapshot, error happens
# virsh snapshot-delete vm1 s3
error: Failed to delete snapshot s3
error: invalid argument: disk 'vdb' not found in domain

Actual results:
When delete the 3rd snapshot, error happens

Expected results:
No error happens

Additional info:
Actually after step 7, the snapshot=s3 is removed
1. metadata removed
# virsh snapshot-list vm1
 Name   Creation Time               State
---------------------------------------------------
 s1     2023-03-02 02:18:29 -0500   disk-snapshot
 s2     2023-03-02 02:18:45 -0500   disk-snapshot
 s4     2023-03-02 02:22:30 -0500   disk-snapshot

2. vdb.s3 not in vdb's backing store
# virsh dumpxml vm1 | awk '/<disk/,/<\/disk/'
...
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/var/lib/libvirt/images/vdb.s4' index='8'/>
      <backingStore type='file' index='4'>
        <format type='qcow2'/>
        <source file='/var/lib/libvirt/images/vdb.qcow2'/>
        <backingStore/>
      </backingStore>
      <target dev='vdb' bus='virtio'/>
      <alias name='virtio-disk1'/>
      <address type='pci' domain='0x0000' bus='0x07' slot='0x00' function='0x0'/>
    </disk>

3. mem snapshot of snapshot=s3 removed
# ll -h /tmp/mem.s3 
ls: cannot access '/tmp/mem.s3': No such file or directory

4. s3's parent s2 has no <snapshotdeleteinprogress> tag
# virsh snapshot-dumpxml vm1 s2| grep -i inprogress
<=== nothing

5. data in vdb is correctedly commited
vm# md5sum /mnt/test_data 
32d7c3b497ce32be440318edd743be81  /mnt/test_data

Comment 2 Pavel Hrdina 2023-03-08 13:17:53 UTC
I did some other testing and when the disks are plugged or unplugged between snapshots it is not in same cases properly handled.

In this specific case the issue is that we do the deletion correctly but when updating metadata we are not taking into account that the disk doesn't have to be listed in inactive domain definition.

Comment 3 Pavel Hrdina 2023-04-03 12:37:03 UTC
Upstream commit:

commit d292ddf1cc268bdd8a494f8e7ce76dc3445c26ab
Author: Pavel Hrdina <phrdina>
Date:   Tue Mar 21 17:42:10 2023 +0100

    qemu_snapshot: external: don't error out when updating metadata

Comment 4 yisun 2023-04-27 10:26:15 UTC
Preverified with libvirt-9.3.0-1.fc37.x86_64
Result is PASSED

1. have a running vm with one disk
[root@yisun-prevbug ~]# virsh domblklist vm1
 Target   Source
---------------------------------------------
 vda      /var/lib/libvirt/images/vda.qcow2

2. create 2 disk only snapshots
[root@yisun-prevbug ~]# virsh snapshot-create-as vm1 s1 --diskspec vda,snapshot=external --disk-only
Domain snapshot s1 created
[root@yisun-prevbug ~]# virsh snapshot-create-as vm1 s2 --diskspec vda,snapshot=external --disk-only
Domain snapshot s2 created

3. attach vdb to it
[root@yisun-prevbug ~]# cat vdb.xml 
<disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/var/lib/libvirt/images/vdb.qcow2'/>
      <backingStore/>
      <target dev='vdb' bus='virtio'/>
</disk>

[root@yisun-prevbug ~]# virsh attach-device vm1 vdb.xml 
Device attached successfully

[root@yisun-prevbug ~]# virsh domblklist vm1
 Target   Source
---------------------------------------------
 vda      /var/lib/libvirt/images/vda.s2
 vdb      /var/lib/libvirt/images/vdb.qcow2

4. create disk+mem snapshot
[root@yisun-prevbug ~]# virsh snapshot-create-as vm1 s3 --memspec /tmp/mem.s3,snapshot=external --diskspec vda,snapshot=external vdb,snapshot=external
Domain snapshot s3 created

[root@yisun-prevbug ~]# ll -h /tmp/mem.s3 
-rw-------. 1 root root 48M Apr 27 10:05 /tmp/mem.s3

[root@yisun-prevbug ~]# virsh domblklist vm1
 Target   Source
------------------------------------------
 vda      /var/lib/libvirt/images/vda.s3
 vdb      /var/lib/libvirt/images/vdb.s3

5. create 4th disk only snapshot
[root@yisun-prevbug ~]# virsh snapshot-create-as vm1 s4 --diskspec vda,snapshot=external --disk-only
Domain snapshot s4 created

[root@yisun-prevbug ~]# virsh domblklist vm1
 Target   Source
------------------------------------------
 vda      /var/lib/libvirt/images/vda.s4
 vdb      /var/lib/libvirt/images/vdb.s4

6. delete snapshot=s3, it's OK now, no error produced.
[root@yisun-prevbug ~]# virsh snapshot-delete vm1 s3
Domain snapshot s3 deleted

7. check the snapshot is actually removed
# virsh snapshot-list vm1
 Name   Creation Time               State
---------------------------------------------------
 s1     2023-04-27 10:03:08 +0000   disk-snapshot
 s2     2023-04-27 10:03:13 +0000   disk-snapshot
 s4     2023-04-27 10:06:25 +0000   disk-snapshot

[root@yisun-prevbug ~]# virsh dumpxml vm1 --xpath //disk
<disk type="file" device="disk">
  <driver name="qemu" type="qcow2"/>
  <source file="/var/lib/libvirt/images/vda.s4" index="7"/>
  <backingStore type="file" index="3">
    <format type="qcow2"/>
    <source file="/var/lib/libvirt/images/vda.s2"/>
    <backingStore type="file" index="2">
      <format type="qcow2"/>
      <source file="/var/lib/libvirt/images/vda.s1"/>
      <backingStore type="file" index="1">
        <format type="qcow2"/>
        <source file="/var/lib/libvirt/images/vda.qcow2"/>
        <backingStore/>
      </backingStore>
    </backingStore>
  </backingStore>
  <target dev="vda" bus="virtio"/>
  <alias name="virtio-disk0"/>
  <address type="pci" domain="0x0000" bus="0x04" slot="0x00" function="0x0"/>
</disk>
<disk type="file" device="disk">
  <driver name="qemu" type="qcow2"/>
  <source file="/var/lib/libvirt/images/vdb.s4" index="8"/>
  <backingStore type="file" index="4">
    <format type="qcow2"/>
    <source file="/var/lib/libvirt/images/vdb.qcow2"/>
    <backingStore/>
  </backingStore>
  <target dev="vdb" bus="virtio"/>
  <alias name="virtio-disk1"/>
  <address type="pci" domain="0x0000" bus="0x07" slot="0x00" function="0x0"/>
</disk>


[root@yisun-prevbug ~]# ll -h /tmp/mem.s3 
ls: cannot access '/tmp/mem.s3': No such file or directory

[root@yisun-prevbug ~]# virsh snapshot-dumpxml vm1 s2| grep -i inprogress
[root@yisun-prevbug ~]#

Comment 8 yisun 2023-05-19 08:29:36 UTC
test with libvirt-9.3.0-2.el9.x86_64 and result is PASS.

Comment 10 errata-xmlrpc 2023-11-07 08:30:47 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory (Moderate: libvirt security, bug fix, and enhancement update), and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHSA-2023:6409