Prevent qcow2 image ballooning with discard=unmap,detect-zeroes=unmap

To prevent the `qcow2` image file from inevitably ballooning or expanding on the
host, we need to tell qcow2 image format block driver to unmap on discard/TRIM.

Due to how `qcow2` sparse images integrate with SSD TRIM (a.k.a. "`discard`"),
we also must set `discard=unmap`, and `detect-zeroes=unmap`.

However, these parameters are usually specified on the block device, so we must
fallback to the more declarative `-blockdev ...` + `-device ...` form of QEMU
CLI args to specify them [^1].  For example:

```console
  # need to specify 'discard=unmap, detect-zeroes=unmap' on blockdev
  -blockdev driver=qcow2,node-name=MacHDD,file.driver=file,file.filename="$REPO_PATH/mac_hdd_ng.img",file.aio=threads,discard=unmap,detect-zeroes=unmap
  -device ide-hd,bus=sata.4,drive=MacHDD,id=macssd,rotation_rate=1
```

[^1]: See: [_QEMU User Documentation: Block device options_][1] or `man qemu` "_Block device options_" section:

> The  most  explicit  way  to describe disks is to use a combination of
> `-device` to specify the hardware device and `-blockdev` to describe the
> backend. The device defines what the guest sees and the backend describes how
> QEMU handles the data. _**It is the only guaranteed stable interface for
> describing block devices and as such is recommended for management tools and
scripting**_.
> The `-drive` option combines the device and backend into a single command line
> option which is a more human friendly. There is however
> _no interface stability guarantee_ although some older board models still need
> updating to work with the modern `blockdev` forms.

Signed-off-by: James Cuzella <james.cuzella@lyraphase.com>
Suggested-by: Daniel Collins <solemnwarning@solemnwarning.net>
This commit is contained in:
James Cuzella 2025-05-05 20:54:48 -06:00
parent 63b9cc496a
commit 9d3deb0222
No known key found for this signature in database
GPG Key ID: 2689A459B1568D09
3 changed files with 11 additions and 7 deletions

Binary file not shown.

View File

@ -52,12 +52,16 @@ args=(
-smbios type=2
-device ich9-intel-hda -device hda-duplex
-device ich9-ahci,id=sata
-drive id=OpenCoreBoot,if=none,snapshot=on,format=qcow2,file="$REPO_PATH/OpenCore/OpenCore.qcow2"
-device ide-hd,bus=sata.2,drive=OpenCoreBoot
# Setup SSD trim support to keep qcow2 image sparse
-blockdev driver=qcow2,node-name=OpenCoreBoot,file.filename="$REPO_PATH/OpenCore/OpenCore.qcow2",file.aio=threads,file.driver=file,discard=unmap,detect-zeroes=unmap
-device ide-hd,bus=sata.2,drive=OpenCoreBoot,id=oc
-set device.oc.rotation_rate=1
-device ide-hd,bus=sata.3,drive=InstallMedia
-drive id=InstallMedia,if=none,file="$REPO_PATH/BaseSystem.img",format=raw
-drive id=MacHDD,if=none,file="$REPO_PATH/mac_hdd_ng.img",format=qcow2
-device ide-hd,bus=sata.4,drive=MacHDD,rotation_rate=1
# Again, setup SSD trim so macOS disk qcow2 image remains sparse
-blockdev driver=qcow2,node-name=MacHDD,file.driver=file,file.filename="$REPO_PATH/mac_hdd_ng.img",file.aio=threads,discard=unmap,detect-zeroes=unmap
-device ide-hd,bus=sata.4,drive=MacHDD,id=macssd
-set device.macssd.rotation_rate=1
# -netdev tap,id=net0,ifname=tap0,script=no,downscript=no -device virtio-net-pci,netdev=net0,id=net0,mac=52:54:00:c9:18:27
-netdev user,id=net0,hostfwd=tcp::2222-:22 -device virtio-net-pci,netdev=net0,id=net0,mac=52:54:00:c9:18:27
# -netdev user,id=net0 -device vmxnet3,netdev=net0,id=net0,mac=52:54:00:c9:18:27 # Note: Use this line for High Sierra

View File

@ -61,14 +61,14 @@
<devices>
<emulator>/usr/bin/qemu-system-x86_64</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='writeback' io='threads'/>
<driver name='qemu' type='qcow2' cache='writeback' io='threads' discard='unmap' detect_zeroes='unmap'/>
<source file='/home/CHANGEME/OSX-KVM/OpenCore/OpenCore.qcow2'/>
<target dev='sda' bus='sata'/>
<target dev='sda' bus='sata' rotation_rate='1'/>
<boot order='2'/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='writeback' io='threads'/>
<driver name='qemu' type='qcow2' cache='writeback' io='threads' discard='unmap' detect_zeroes='unmap'/>
<source file='/home/CHANGEME/OSX-KVM/mac_hdd_ng.img'/>
<target dev='sdb' bus='sata' rotation_rate='1'/>
<boot order='1'/>