Skip to content

Instantly share code, notes, and snippets.

@stvhay
Last active September 23, 2025 10:40
Show Gist options
  • Select an option

  • Save stvhay/3ed0966dcadc1e58e7f3213121a26918 to your computer and use it in GitHub Desktop.

Select an option

Save stvhay/3ed0966dcadc1e58e7f3213121a26918 to your computer and use it in GitHub Desktop.
Proxmox Update PCI pass through based on PCI ID
#!/usr/bin/env bash

vm_id="$1"
phase="$2"


[ $# -ne 2 ] || exit 1
case "$phase" in
        pre-start)  ;;
        post-start) exit 0 ;;
        pre-stop)   exit 0 ;;
        post-stop)  exit 0 ;;
        *)          exit 1 ;;
esac


pci_id=8086:1521
vm_pci_n=0
conf_dir="/etc/pve/qemu-server"

pci_address()
{
        local id="$1"
        local -a output
        if output=$(/usr/bin/lspci -m | /usr/bin/grep "$id" | /usr/bin/head -n 1)
        then
                printf "0000:%s" "${output[0]%%.*}"
        else
                return 1
        fi
}

conf_address()
{
        local conf="$conf_dir/${1}.conf"
        local n="$2"
        if local line=/usr/bin/grep "hostpci${n}:" "$conf"
        then
                printf "%s" "$line" | /usr/bin/tr -s ' ' | /usr/bin/cut -d' ' -f2
        else
                return 1
        fi
}


conf_addr=$(conf_address $vm_id $vm_pci_n) || exit 1
current_addr=$(pci_address "$pci_id")      || exit 1
if [ "$conf_addr" != "$current_addr" ]
then
        /usr/bin/sed -i "s/^hostpci0:.*/hostpci0: 0000:$(current_addr)/" "$conf_dir/${1}.conf"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment