Pardon my stupid question about unbinding the Nvidia driver/GPU from a VM

alpha754293

Member
Jan 8, 2023
88
18
8
I ran the search here for "unbind" and found some examples for how to unbind it for AMD GPUs.

I am not a programmer nor a developer, so I am not sure how I would "convert"/"translate"/"adapt" that for Nvidia GPUs.

Here is the hookscript that I have (which someone else wrote using Perl), that I am using to bind the GPU to a VM:

Code:
#!/usr/bin/perl
# Exmple hook script for PVE guests (hookscript config option)
# You can set this via pct/qm with
# pct set <vmid> -hookscript <volume-id>
# qm set <vmid> -hookscript <volume-id>
# where <volume-id> has to be an executable file in the snippets folder
# of any storage with directories e.g.:
# qm set 100 -hookscript local:snippets/hookscript.pl
use strict;
use warnings;
print "GUEST HOOK: " . join(' ', @ARGV). "\n";
# First argument is the vmid
my $vmid = shift;
# Second argument is the phase
my $phase = shift;
if ($phase eq 'pre-start') {
# First phase 'pre-start' will be executed before the guest
    # ist started. Exiting with a code != 0 will abort the start
print "$vmid is starting, doing preparations.\n";
system('echo 1 > /sys/bus/pci/devices/0000\:81\:00.0/remove');
system('echo 1 > /sys/bus/pci/rescan');
# print "preparations failed, aborting."
    # exit(1);
} elsif ($phase eq 'post-start') {
# Second phase 'post-start' will be executed after the guest
    # successfully started.
print "$vmid started successfully.\n";
} elsif ($phase eq 'pre-stop') {
# Third phase 'pre-stop' will be executed before stopping the guest
    # via the API. Will not be executed if the guest is stopped from
    # within e.g., with a 'poweroff'
print "$vmid will be stopped.\n";
} elsif ($phase eq 'post-stop') {
# Last phase 'post-stop' will be executed after the guest stopped.
    # This should even be executed in case the guest crashes or stopped
    # unexpectedly.
print "$vmid stopped. Doing cleanup.\n";
} else {
    die "got unknown phase '$phase'\n";
}
exit(0);

The two questions that I have are:

1) How do I adapt this so that it will unbind the Nvidia GPU from said VM?

and 2), it says in the script there that if I shutdown the VM from inside the VM, that it will not unbind the GPU from said VM.

So given that, is there another way that I can re-write this Perl script such that it will unbind the GPU from the VM, even if the VM is shut down from inside the VM?

Your help is greatly appreciated.

(P.S. I am using Proxmox 7.4-17 still. I assume that the procedure will still be valid for Proxmox 8 as even with a mapped device (or even a raw device, in Proxmox 8), it still won't unbind the device automatically, even though the Proxmox GUI in Proxmox 8 helps with adding a PCI device, but doesn't really help to unbind the PCI device from the VM, correct? Please educate me if my understanding about Proxmox 8 PCIe device unbinding is incorrect. Thank you.)
 
Also, is it possible for me to share a GPU between LXCs and a VM?

(i.e. such that when the VM isn't running, the LXCs are able to use the GPU, but if the VM is running, then the LXCs will relinquish said GPU until the VM stops?)
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!