From 4fc4c20bbe10edc95d7d6fe466fca1a6268ab934 Mon Sep 17 00:00:00 2001
From: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
Date: Mon, 20 Dec 2021 21:34:22 +0200
Subject: [PATCH] Place explicitly-declared block device drivers at the end of
 the PCI bus

In 1745eff43d67bf3563310a2910f17908bd10039b we risk reorganizing the
list of PCI devices, due to how raw -drive disks end up placing their
parent devices at the end of the PCI bus.

Instead, we should explicitly place the directives to configure the
block device interfaces at the tail of the PCI bus.

This should hopefully address the final concerns in
https://0xacab.org/dkg/kvm-manager/-/issues/7
---
 kvm-start | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/kvm-start b/kvm-start
index 2f8ed23..9f815f6 100755
--- a/kvm-start
+++ b/kvm-start
@@ -108,6 +108,8 @@ INITRD="$OWNERHOME/vms/$VMNAME/initrd"
 KVMARGS=
 unset KERNEL_CMDLINE
 
+declare -a KVM_DISK_DEVS=()
+
 BOOTCHOICE=c
 
 if [ -e "$KERNEL" -a -e "$INITRD" ] ; then
@@ -150,7 +152,7 @@ for disk in HD{A..Z}; do
   if [ -b "${!disk}" ]; then
     driver_var_name="${disk}_DRIVER"
     if [ -n "${!driver_var_name}" -a  "${!driver_var_name}" = "scsi-hd" ]; then
-      KVMARGS="$KVMARGS -device virtio-scsi-pci,id=scsi"
+      KVM_DISK_DEVS+=(-device virtio-scsi-pci,id=scsi)
       break
     fi
   fi
@@ -159,7 +161,8 @@ done
 if [ -n "$HDA" ]; then
    build_disk_io_params HDA
    driver=${HDA_DRIVER:-virtio-blk-pci}
-   KVMARGS="$KVMARGS -drive file=$HDA,if=none,cache=none,id=disk-${index},format=raw${first_disk_extra_args}${disk_io_params} -device ${driver},drive=disk-${index},bootindex=1"
+   KVMARGS="$KVMARGS -drive file=$HDA,if=none,cache=none,id=disk-${index},format=raw${first_disk_extra_args}${disk_io_params}"
+   KVM_DISK_DEVS+=(-device "${driver},drive=disk-${index},bootindex=1")
 fi
 # loop here on everything after HDA:
 for disk in HD{B..Z}; do
@@ -169,7 +172,8 @@ for disk in HD{B..Z}; do
     driver_var_name="${disk}_DRIVER"
     [ -n "${!driver_var_name}" ] && driver=${!driver_var_name}
     build_disk_io_params "${disk}"
-    KVMARGS="$KVMARGS -drive file=${!disk},if=none,cache=none,id=disk-${index},format=raw${disk_io_params} -device ${driver},drive=disk-${index}"
+    KVMARGS="$KVMARGS -drive file=${!disk},if=none,cache=none,id=disk-${index},format=raw${disk_io_params}"
+    KVM_DISK_DEVS+=(-device "${driver},drive=disk-${index}")
   fi
 done
 
@@ -203,5 +207,5 @@ exec /usr/bin/kvm $KVMARGS \
     -chardev "socket,id=serial0,path=$CONSOLENAME,server" -device isa-serial,chardev=serial0 \
     -smp "${SMP:-1},maxcpus=${MAXCPUS:-8}" \
     -device "virtio-net-pci,netdev=n1,id=net0,mac=$MAC,bus=pci.0" \
-    -netdev "tap,id=n1,ifname=$TAP,script=no,downscript=no"
-
+    -netdev "tap,id=n1,ifname=$TAP,script=no,downscript=no" \
+    "${KVM_DISK_DEVS[@]}"
-- 
GitLab