"qemu-img resize -f qcow2" produces broken disk images
Host environment
- Operating system: Ubuntu 22.04
- OS/kernel version: Linux 5.15.0-91-generic x86_64
- Architecture: x86_64
- QEMU flavor: qemu-img
- QEMU version: 8.2.0
- QEMU command line:
qemu-img resize -f qcow2 arm64-cr.qcow2 10G
Emulated/Virtualized environment
- Operating system:
- OS/kernel version:
- Architecture:
Description of problem
The documentation of qemu-img
at
https://www.qemu.org/docs/master/tools/qemu-img.html
makes it sound like qemu-img resize
supports various image formats
(raw, qcow2, etc.) in the same way.
But it doesn't. While qemu-img resize -f raw
works as expected,
qemu-img resize -f qcow2
produces broken disk images.
Steps to reproduce
$ wget http://nycdn.netbsd.org/pub/NetBSD-daily/netbsd-9/latest/evbarm-aarch64/binary/gzimg/arm64.img.gz
$ gunzip arm64.img
First resize, then convert:
$ cp arm64.img arm64-rc.img
$ qemu-img resize -f raw arm64-rc.img 10G
$ qemu-img convert -f raw -O qcow2 arm64-rc.img arm64-rc.qcow2
$ rm -f arm64-rc.img
First convert, then resize:
$ qemu-img convert -f raw -O qcow2 arm64.img arm64-cr.qcow2
$ qemu-img resize -f qcow2 arm64-cr.qcow2 10G
Attach to a VM in VirtualBox (as an additional SATA disk) and start that VM.
arm64-rc.qcow2 =>
# fdisk /dev/sdb
=> it has two partitions.
arm64-cr.qcow2 =>
# fdisk /dev/sdb
=> it has no partitions!
And the VM cannot be cleanly shut down. I had to manually kill the VirtualBoxVM
process.
For verification:
$ qemu-img convert -f qcow2 -O raw arm64-rc.qcow2 arm64-rc.img
$ qemu-img convert -f qcow2 -O raw arm64-cr.qcow2 arm64-cr.img
If qemu-img was operating correctly, the two resulting .img files should be identical. But they are not:
$ sha256sum arm64-rc.img arm64-cr.img
0a9e9b6b06b11dcfe8fa055351087329b069bfbc9d5a6cb3032d433c6f9d62e3 arm64-rc.img
732377e7f4a2abdc13ddfa1eb4c9c497fd2a2b294674d056cf51581b47dd586d arm64-cr.img
In fact, arm64-cr.img contains significantly less data, as can be seen by compressing the two images:
$ gzip -9 arm64-rc.img
$ gzip -9 arm64-cr.img
$ ls -l arm64-rc.img.gz; ls -l arm64-cr.img.gz
-rw-r--r-- 1 bruno bruno 302447710 16. Jan 12:56 arm64-rc.img.gz
-rw-r--r-- 1 bruno bruno 10420398 16. Jan 12:56 arm64-cr.img.gz