migration corrupts qcow2 metadata when "backing file: json:{" is involve
Host environment
- Operating system: Debian 10, Centos 7
- OS/kernel version: 4.19, 5.4.70
- Architecture: x86
- QEMU flavor: qemu-system-x86_64
- QEMU version: 4.1.1, 7.0.0
- QEMU command line:
/opt/qemu-7.0.0/bin/qemu-img create -f qcow2 0.qcow2 64G /opt/qemu-7.0.0/bin/qemu-img create -F qcow2 -f qcow2 -b 'json:{"driver": "qcow2", "file": { "driver": "file", "filename": "0.qcow2"}}' 1.qcow2 /opt/qemu-7.0.0/bin/qemu-img create -F qcow2 -f qcow2 -b 'json:{"driver": "qcow2", "file": { "driver": "file", "filename": "1.qcow2"}}' 2.qcow2
#VM1 /opt/qemu-7.0.0/bin/qemu-system-x86_64 -enable-kvm -drive if=virtio,file=2.qcow2,node-name=drive0 -qmp stdio -display none { "execute": "qmp_capabilities" } { "execute": "migrate", "arguments": { "uri": "tcp:localhost:8082" } } {"timestamp": {"seconds": 1658491019, "microseconds": 233177}, "event": "STOP"}
#VM2 /opt/qemu-7.0.0/bin/qemu-system-x86_64 -enable-kvm -drive if=virtio,file=2.qcow2,node-name=drive0 -qmp stdio -display none -incoming tcp::8082 { "execute": "qmp_capabilities" } {"timestamp": {"seconds": 1658491019, "microseconds": 249760}, "event": "RESUME"} { "execute": "blockdev-snapshot-sync", "arguments": { "format": "qcow2", "snapshot-file": "3.qcow2", "node-name": "drive0", "snapshot-node-name": "drive0-snap" }}
/opt/qemu-7.0.0/bin/qemu-img info --backing-chain 3.qcow2 qemu-img: Could not open 'json:{"backing": {"backing": {"driver": "qcow2", "file": {"driver": "file", "filename": "0.qcow2"}}, "driver": "qcow2", "file": {"driver": "file", "filename": "1.qcow2"}}, "driver": "qcow2", "file": {"driver": "file", "filename": "2.qcow2"}}': Block format 'qcow2' does not support the option 'backing.backing.driver'
Emulated/Virtualized environment
- Operating system: N/A
- OS/kernel version: N/A
- Architecture: x86
Description of problem
the bug happens when you have a qcow2 with backing file in json format image: 2.qcow2 [...] backing file: json:{"driver": "qcow2", "file": { "driver": "file", "filename": "1.qcow2"}} backing file format: qcow2 [...] if you want to migrate a VM that have that kind of qcow2 attached, the migration is gonna corrupted qcow2 metadata in memory and info block will look like this json:{"backing": {"backing": {"driver": "qcow2", "file": {"driver": "file", "filename": "0.qcow2"}}, "driver": "qcow2", "file": {"driver": "file", "filename": "1.qcow2"}}, "driver": "qcow2", "file": {"driver": "file", "filename": "2.qcow2"}} later if you execute blockdev-snapshot-sync, the corrupt json will be write to the new qcow2 resulting with a unusable qcow2
Steps to reproduce
/opt/qemu-7.0.0/bin/qemu-img create -f qcow2 0.qcow2 64G /opt/qemu-7.0.0/bin/qemu-img create -F qcow2 -f qcow2 -b 'json:{"driver": "qcow2", "file": { "driver": "file", "filename": "0.qcow2"}}' 1.qcow2 /opt/qemu-7.0.0/bin/qemu-img create -F qcow2 -f qcow2 -b 'json:{"driver": "qcow2", "file": { "driver": "file", "filename": "1.qcow2"}}' 2.qcow2
#VM1 /opt/qemu-7.0.0/bin/qemu-system-x86_64 -enable-kvm -drive if=virtio,file=2.qcow2,node-name=drive0 -qmp stdio -display none
#VM2 /opt/qemu-7.0.0/bin/qemu-system-x86_64 -enable-kvm -drive if=virtio,file=2.qcow2,node-name=drive0 -qmp stdio -display none -incoming tcp::8082
#VM1 INFO BLOCK {"QMP": {"version": {"qemu": {"micro": 0, "minor": 0, "major": 7}, "package": ""}, "capabilities": ["oob"]}} { "execute": "qmp_capabilities" } {"return": {}} { "execute": "human-monitor-command", "arguments": {'command-line': 'info block'} } {"return": "virtio0 (drive0): 2.qcow2 (qcow2)\r\n Attached to: /machine/peripheral-anon/device[0]/virtio-backend\r\n Cache mode: writethrough\r\n Backing file: 1.qcow2 (chain depth: 2)\r\n\r\nide1-cd0: [not inserted]\r\n Attached to: /machine/unattached/device[24]\r\n Removable device: not locked, tray closed\r\n\r\nfloppy0: [not inserted]\r\n Attached to: /machine/unattached/device[17]\r\n Removable device: not locked, tray closed\r\n\r\nsd0: [not inserted]\r\n Removable device: not locked, tray closed\r\n"}
#VM1 MIGRATE { "execute": "migrate", "arguments": { "uri": "tcp:localhost:8082" } } {"return": {}} {"timestamp": {"seconds": 1658491019, "microseconds": 233177}, "event": "STOP"}
#VM2 INFO BLOCK {"QMP": {"version": {"qemu": {"micro": 0, "minor": 0, "major": 7}, "package": ""}, "capabilities": ["oob"]}} { "execute": "qmp_capabilities" } {"return": {}} { "execute": "human-monitor-command", "arguments": {'command-line': 'info block'} } {"return": "virtio0 (drive0): 2.qcow2 (qcow2)\r\n Attached to: /machine/peripheral-anon/device[0]/virtio-backend\r\n Cache mode: writeback\r\n Backing file: 1.qcow2 (chain depth: 2)\r\n\r\nide1-cd0: [not inserted]\r\n Attached to: /machine/unattached/device[24]\r\n Removable device: not locked, tray closed\r\n\r\nfloppy0: [not inserted]\r\n Attached to: /machine/unattached/device[17]\r\n Removable device: not locked, tray closed\r\n\r\nsd0: [not inserted]\r\n Removable device: not locked, tray closed\r\n"}
#VM2 MIGRATE {"timestamp": {"seconds": 1658491019, "microseconds": 249760}, "event": "RESUME"}
#VM2 MIGRATION DONE, INFO BLOCK { "execute": "human-monitor-command", "arguments": {'command-line': 'info block'} } {"return": "virtio0 (drive0): json:{"backing": {"backing": {"driver": "qcow2", "file": {"driver": "file", "filename": "0.qcow2"}}, "driver": "qcow2", "file": {"driver": "file", "filename": "1.qcow2"}}, "driver": "qcow2", "file": {"driver": "file", "filename": "2.qcow2"}} (qcow2)\r\n Attached to: /machine/peripheral-anon/device[0]/virtio-backend\r\n Cache mode: writethrough\r\n Backing file: json:{"backing": {"driver": "qcow2", "file": {"driver": "file", "filename": "0.qcow2"}}, "driver": "qcow2", "file": {"driver": "file", "filename": "1.qcow2"}} (chain depth: 2)\r\n\r\nide1-cd0: [not inserted]\r\n Attached to: /machine/unattached/device[24]\r\n Removable device: not locked, tray closed\r\n\r\nfloppy0: [not inserted]\r\n Attached to: /machine/unattached/device[17]\r\n Removable device: not locked, tray closed\r\n\r\nsd0: [not inserted]\r\n Removable device: not locked, tray closed\r\n"}
#VM2 SNAPSHOT AFTER MIGRATION { "execute": "blockdev-snapshot-sync", "arguments": { "format": "qcow2", "snapshot-file": "3.qcow2", "node-name": "drive0", "snapshot-node-name": "drive0-snap" }} Formatting '3.qcow2', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=68719476736 backing_file=json:{"backing": {"backing": {"driver": "qcow2",, "file": {"driver": "file",, "filename": "0.qcow2"}},, "driver": "qcow2",, "file": {"driver": "file",, "filename": "1.qcow2"}},, "driver": "qcow2",, "file": {"driver": "file",, "filename": "2.qcow2"}} backing_fmt=qcow2 lazy_refcounts=off refcount_bits=16 {"return": {}}
#VM2 INFO BLOCK AFTER SNAPSHOT { "execute": "human-monitor-command", "arguments": {'command-line': 'info block'} } {"return": "virtio0 (drive0-snap): 3.qcow2 (qcow2)\r\n Attached to: /machine/peripheral-anon/device[0]/virtio-backend\r\n Cache mode: writethrough\r\n Backing file: json:{"backing": {"backing": {"driver": "qcow2", "file": {"driver": "file", "filename": "0.qcow2"}}, "driver": "qcow2", "file": {"driver": "file", "filename": "1.qcow2"}}, "driver": "qcow2", "file": {"driver": "file", "filename": "2.qcow2"}} (chain depth: 3)\r\n\r\nide1-cd0: [not inserted]\r\n Attached to: /machine/unattached/device[24]\r\n Removable device: not locked, tray closed\r\n\r\nfloppy0: [not inserted]\r\n Attached to: /machine/unattached/device[17]\r\n Removable device: not locked, tray closed\r\n\r\nsd0: [not inserted]\r\n Removable device: not locked, tray closed\r\n"}
#INFO /opt/qemu-7.0.0/bin/qemu-img info --backing-chain 3.qcow2 qemu-img: Could not open 'json:{"backing": {"backing": {"driver": "qcow2", "file": {"driver": "file", "filename": "0.qcow2"}}, "driver": "qcow2", "file": {"driver": "file", "filename": "1.qcow2"}}, "driver": "qcow2", "file": {"driver": "file", "filename": "2.qcow2"}}': Block format 'qcow2' does not support the option 'backing.backing.driver'
Additional information
Even if the bug is scary it's very simple to fix it
/opt/qemu-7.0.0/bin/qemu-img info --backing-chain 3.qcow2 qemu-img: Could not open 'json:{"backing": {"backing": {"driver": "qcow2", "file": {"driver": "file", "filename": "0.qcow2"}}, "driver": "qcow2", "file": {"driver": "file", "filename": "1.qcow2"}}, "driver": "qcow2", "file": {"driver": "file", "filename": "2.qcow2"}}': Block format 'qcow2' does not support the option 'backing.backing.driver'
root@lenovo2:/data# /opt/qemu-7.0.0/bin/qemu-img rebase -f qcow2 -F qcow2 -u -b 2.qcow2 3.qcow2 root@lenovo2:/data# /opt/qemu-7.0.0/bin/qemu-img info --backing-chain 3.qcow2 image: 3.qcow2 file format: qcow2 virtual size: 64 GiB (68719476736 bytes) disk size: 24 KiB cluster_size: 65536 backing file: 2.qcow2 backing file format: qcow2 Format specific information: compat: 1.1 compression type: zlib lazy refcounts: false refcount bits: 16 corrupt: false extended l2: false
image: 2.qcow2 file format: qcow2 virtual size: 64 GiB (68719476736 bytes) disk size: 24 KiB cluster_size: 65536 [..........]