secondary_vm.sh
· 1.9 KiB · Bash
Raw
#!/bin/bash
# Set a secondary VM to use second NVIDIA card as PCI Passthrough
# And the bottom USB card. And REMOVE those 2 PCI devices from any other VM
# mReschke 2025-03-16
# Input
secondary_vmid="$1"
#primary=109
# Validate
if [ "$secondary_vmid" == "" ]; then
echo "You must provide a VMID you want to be the seconday"
exit 1
fi
# Validate secondary is NOT our primary VM
#if [ "$secondary_vmid" == "$primary" ]; then
# echo "You cannot use 109 as a secondary VM, that is your primary"
# exit 1
#fi
# Define PCI devices to add/remove
hostpci0="0000:01:00,pcie=1,x-vga=1"
hostpci1="0000:82:00,pcie=1"
# Get all VMID from qm list
vmids=($(qm list | tail -n +2 | cut -d\ -f8))
# Loop each VM and remove our pci devices if exist
found_secondary=0
for vmid in "${vmids[@]}"
do
echo "Analyzing VMID $vmid"
if [ "$vmid" == "$secondary_vmid" ]; then
found_secondary=1
fi
# TODO check status
# qm status 102 | cut -d: -f2 | xargs
# will be running or stopped
# if running and PCI device exist, do NOT remove it
# and if I can't remove it, do NOT add it second VM
# Remove hostpci0 if matches our desired device
if result=$(qm config $vmid | grep $hostpci0); then
echo " VMID $vmid has option $result"
qm set $vmid --delete hostpci0
echo " VMID $vmid removed hostpci0"
fi
# Remove hostpci1 is matches our desired device
if result=$(qm config $vmid | grep $hostpci1); then
echo " VMID $vmid has option $result"
qm set $vmid --delete hostpci1
echo " VMID $vmid removed hostpci1"
fi
done
# Finally add our PIC devices to the desired VMID
if [ "$found_secondary" == 1 ]; then
echo
echo "Setting secondary VM to $secondary_vmid"
qm set $secondary_vmid -hostpci0 $hostpci0
qm set $secondary_vmid -hostpci1 $hostpci1
else
echo "The secondary VMID $secondary_vmid was not found in qm list, not setting secondary"
exit 1
fi
#root@dynepyc:/usr/local/bin# qm config 107|grep hostpci
#hostpci0: 0000:01:00,pcie=1,x-vga=1
#hostpci1: 0000:82:00,pcie=1
| 1 | #!/bin/bash |
| 2 | |
| 3 | # Set a secondary VM to use second NVIDIA card as PCI Passthrough |
| 4 | # And the bottom USB card. And REMOVE those 2 PCI devices from any other VM |
| 5 | # mReschke 2025-03-16 |
| 6 | |
| 7 | # Input |
| 8 | secondary_vmid="$1" |
| 9 | #primary=109 |
| 10 | |
| 11 | # Validate |
| 12 | if [ "$secondary_vmid" == "" ]; then |
| 13 | echo "You must provide a VMID you want to be the seconday" |
| 14 | exit 1 |
| 15 | fi |
| 16 | |
| 17 | # Validate secondary is NOT our primary VM |
| 18 | #if [ "$secondary_vmid" == "$primary" ]; then |
| 19 | # echo "You cannot use 109 as a secondary VM, that is your primary" |
| 20 | # exit 1 |
| 21 | #fi |
| 22 | |
| 23 | |
| 24 | # Define PCI devices to add/remove |
| 25 | hostpci0="0000:01:00,pcie=1,x-vga=1" |
| 26 | hostpci1="0000:82:00,pcie=1" |
| 27 | |
| 28 | # Get all VMID from qm list |
| 29 | vmids=($(qm list | tail -n +2 | cut -d\ -f8)) |
| 30 | |
| 31 | # Loop each VM and remove our pci devices if exist |
| 32 | found_secondary=0 |
| 33 | for vmid in "${vmids[@]}" |
| 34 | do |
| 35 | echo "Analyzing VMID $vmid" |
| 36 | if [ "$vmid" == "$secondary_vmid" ]; then |
| 37 | found_secondary=1 |
| 38 | fi |
| 39 | |
| 40 | # TODO check status |
| 41 | # qm status 102 | cut -d: -f2 | xargs |
| 42 | # will be running or stopped |
| 43 | # if running and PCI device exist, do NOT remove it |
| 44 | # and if I can't remove it, do NOT add it second VM |
| 45 | |
| 46 | # Remove hostpci0 if matches our desired device |
| 47 | if result=$(qm config $vmid | grep $hostpci0); then |
| 48 | echo " VMID $vmid has option $result" |
| 49 | qm set $vmid --delete hostpci0 |
| 50 | echo " VMID $vmid removed hostpci0" |
| 51 | fi |
| 52 | |
| 53 | # Remove hostpci1 is matches our desired device |
| 54 | if result=$(qm config $vmid | grep $hostpci1); then |
| 55 | echo " VMID $vmid has option $result" |
| 56 | qm set $vmid --delete hostpci1 |
| 57 | echo " VMID $vmid removed hostpci1" |
| 58 | fi |
| 59 | |
| 60 | done |
| 61 | |
| 62 | # Finally add our PIC devices to the desired VMID |
| 63 | if [ "$found_secondary" == 1 ]; then |
| 64 | echo |
| 65 | echo "Setting secondary VM to $secondary_vmid" |
| 66 | qm set $secondary_vmid -hostpci0 $hostpci0 |
| 67 | qm set $secondary_vmid -hostpci1 $hostpci1 |
| 68 | else |
| 69 | echo "The secondary VMID $secondary_vmid was not found in qm list, not setting secondary" |
| 70 | exit 1 |
| 71 | fi |
| 72 | |
| 73 | |
| 74 | #root@dynepyc:/usr/local/bin# qm config 107|grep hostpci |
| 75 | #hostpci0: 0000:01:00,pcie=1,x-vga=1 |
| 76 | #hostpci1: 0000:82:00,pcie=1 |