From 4571b04b1b69baed3a54a164a2911577bcf0de83 Mon Sep 17 00:00:00 2001 From: Albrecht Lohofener Date: Fri, 9 May 2025 14:36:24 +0200 Subject: [PATCH] Add option for second PCI pass-through device --- docker-compose.yml | 5 +++-- src/cleanup_container.sh | 13 ++++++++++++- src/helpers.sh | 1 + src/run_openwrt.sh | 13 ++++++++++++- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 95d184f..f8babd2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -41,7 +41,7 @@ services: # USB pass-through # Format : like 'lsusb' is given - # Currently only two USB devices are supported + # Currently, up to two USB devices are supported # If unset -> disabled #USB_1: "0bda:8153" #USB_2: "0e8d:7961" @@ -54,9 +54,10 @@ services: # - Ubuntu 24.04 is working by default # Format like 'lspci' is given # optional like 'lspci' is given + # Currently, up to two PCI devices are supported # If unset -> disabled #PCI_1: "00:1f.6" - #PCI_1: "0000:00:1f.6" + #PCI_2: "0000:00:1f.6" # Define number of VM CPUs # If unset -> 1 diff --git a/src/cleanup_container.sh b/src/cleanup_container.sh index 5274338..33c788c 100644 --- a/src/cleanup_container.sh +++ b/src/cleanup_container.sh @@ -18,6 +18,17 @@ cleanup() { echo 1 > /sys/bus/pci/rescan fi + if [ -n "$PCI_2" ]; then + if [[ "$PCI_2" =~ ^[0-9]{4}: ]]; then + PCI_SLOT="$PCI_2" + else + PCI_SLOT="0000:$PCI_2" + fi + echo "Attaching PCI device $PCI_SLOT back to the host" + echo 1 > /sys/bus/pci/devices/$PCI_SLOT/remove + echo 1 > /sys/bus/pci/rescan + fi + echo "Cleanup complete." exit 0 @@ -38,4 +49,4 @@ if [[ $DEBUG = "true" ]]; then done & wait fi -sleep infinity & wait \ No newline at end of file +sleep infinity & wait diff --git a/src/helpers.sh b/src/helpers.sh index 29b695c..5e99dcc 100644 --- a/src/helpers.sh +++ b/src/helpers.sh @@ -15,6 +15,7 @@ trap 'error "Status $? while: $BASH_COMMAND (line $LINENO/$BASH_LINENO)"' ERR : "${USB_1:=""}" # USB 1 vendor and device ID : "${USB_2:=""}" # USB 2 vendor and device ID : "${PCI_1:=""}" # PCI 1 slot (first number in lspci) +: "${PCI_2:=""}" # PCI 2 slot (first number in lspci) : "${FORWARD_LUCI:=""}" # Make LuCI OpenWrt web interface accessible via host LAN : "${LUCI_WEB_BUTTON_JSON:=""}" # Adapt the "OpenWrt LuCI web interface" button to your needs. : "${OPENWRT_AFTER_BOOT_CMD:=""}" # User command or script to run after OpenWrt is booted diff --git a/src/run_openwrt.sh b/src/run_openwrt.sh index 4cead58..19a125c 100755 --- a/src/run_openwrt.sh +++ b/src/run_openwrt.sh @@ -253,6 +253,7 @@ USB_ARGS="${USB_1_ARGS} ${USB_2_ARGS}" # Attaching PCI devices PCI_ARGS="" PCI_1_ARGS="" +PCI_2_ARGS="" if [[ -n "$PCI_1" ]]; then if [[ "$PCI_1" =~ ^[0-9]{4}: ]]; then PCI_SLOT="$PCI_1" @@ -263,7 +264,17 @@ if [[ -n "$PCI_1" ]]; then PCI_1_ARGS="-device vfio-pci,host=$PCI_SLOT" fi -PCI_ARGS="${PCI_1_ARGS}" +if [[ -n "$PCI_2" ]]; then + if [[ "$PCI_2" =~ ^[0-9]{4}: ]]; then + PCI_SLOT="$PCI_2" + else + PCI_SLOT="0000:$PCI_2" + fi + attach_pci_device "$PCI_SLOT" + PCI_2_ARGS="-device vfio-pci,host=$PCI_SLOT" +fi + +PCI_ARGS="${PCI_1_ARGS} ${PCI_2_ARGS}" info "Booting image using $VERS..."