mreschke revised this gist 4 hours ago. Go to revision
No changes
mreschke revised this gist 4 hours ago. Go to revision
1 file changed, 76 insertions
secondary_vm.sh(file created)
| @@ -0,0 +1,76 @@ | |||
| 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 | |
Newer
Older