macOS: swap disk controller from virtio-blk-pci to ahci

Currently, the virtio specification does not include provision for the TRIM (aka DISCARD) command that allows a guest operating system to signal the disk hardware that blocks have become unused so that the underlying device may clear the physical data.

The TRIM/DISCARD command was introduced for SSD disks as an extension to the AHCI specification that is used in SATA systems.

With Virtual Machines we can use this command to tell QEMU's Qcow2 driver to reclaim unused space in the disk image. This ensures the disk image file is kept to the smallest size possible where without the TRIM/DISCARD command it grows to it's maximum configured size and never shrinks again when data is deleted.

Let's swap our default disk driver from `virtio-blk-pci` which does not support TRIM to `ahci` which does.

(We cannot use `virtio-scsi-pci` when running macOS, like we do in our default disk device,  because macOS does not support SCSI disks at all on x86_64 systems.)
This commit is contained in:
Dani Llewellyn 2022-10-17 20:25:42 +01:00 committed by Martin Wimpress
parent fb8deb10e8
commit 881adb289a

View File

@ -542,13 +542,13 @@ function vm_boot() {
case ${macos_release} in case ${macos_release} in
catalina) catalina)
BALLOON="" BALLOON=""
MAC_DISK_DEV="virtio-blk-pci" MAC_DISK_DEV="ide-hd,bus=ahci.2"
NET_DEVICE="vmxnet3" NET_DEVICE="vmxnet3"
USB_HOST_PASSTHROUGH_CONTROLLER="usb-ehci" USB_HOST_PASSTHROUGH_CONTROLLER="usb-ehci"
;; ;;
big-sur|monterey|ventura) big-sur|monterey|ventura)
BALLOON="-device virtio-balloon" BALLOON="-device virtio-balloon"
MAC_DISK_DEV="virtio-blk-pci" MAC_DISK_DEV="ide-hd,bus=ahci.2"
NET_DEVICE="virtio-net" NET_DEVICE="virtio-net"
USB_HOST_PASSTHROUGH_CONTROLLER="nec-usb-xhci" USB_HOST_PASSTHROUGH_CONTROLLER="nec-usb-xhci"
GUEST_TWEAKS="${GUEST_TWEAKS} -global nec-usb-xhci.msi=off" GUEST_TWEAKS="${GUEST_TWEAKS} -global nec-usb-xhci.msi=off"
@ -1071,18 +1071,19 @@ function vm_boot() {
if [ "${guest_os}" == "macos" ]; then if [ "${guest_os}" == "macos" ]; then
# shellcheck disable=SC2054 # shellcheck disable=SC2054
args+=(-device ahci,id=ahci args+=(-device ahci,id=ahci
-device ide-hd,bus=ahci.0,drive=BootLoader,bootindex=0 -device ide-hd,bus=ahci.0,drive=BootLoader,bootindex=0,rotation_rate=1
-drive id=BootLoader,if=none,format=qcow2,file="${MAC_BOOTLOADER}") -drive id=BootLoader,if=none,format=qcow2,discard=unmap,file="${MAC_BOOTLOADER}")
if [ -n "${img}" ]; then if [ -n "${img}" ]; then
# shellcheck disable=SC2054 # shellcheck disable=SC2054
args+=(-device ide-hd,bus=ahci.1,drive=RecoveryImage args+=(-device ide-hd,bus=ahci.1,drive=RecoveryImage,rotation_rate=1
-drive id=RecoveryImage,if=none,format=raw,file="${img}") -drive id=RecoveryImage,if=none,format=raw,discard=unmap,file="${img}")
fi fi
# shellcheck disable=SC2054,SC2206 # shellcheck disable=SC2054,SC2206
args+=(-device ${MAC_DISK_DEV},drive=SystemDisk args+=(-device ${MAC_DISK_DEV},drive=SystemDisk,rotation_rate=1
-drive id=SystemDisk,if=none,format=qcow2,file="${disk_img}" ${STATUS_QUO}) -drive id=SystemDisk,if=none,format=qcow2,discard=unmap,file="${disk_img}" ${STATUS_QUO})
elif [ "${guest_os}" == "kolibrios" ]; then elif [ "${guest_os}" == "kolibrios" ]; then
# shellcheck disable=SC2054,SC2206 # shellcheck disable=SC2054,SC2206
args+=(-device ahci,id=ahci args+=(-device ahci,id=ahci