Edit configuration before starting VM through custom hook script

samsepi0l

Member
Jan 17, 2021
7
1
8
30
First of all I would like to thank the Proxmox team for the great software and the nice community they built.

I'm using a custom hook script (hookscript.pl) to manage VMs on a system with various GPUs passed through.
The first motivation to do so is to avoid a VM (which requires a GPU) being started when another VM is using the same GPU already, because neither Proxmox or qemu or vfio check if the device is already in use and the VM gets started unconditionally (but it doesn't end well).
Without this workaround, the system tries to start the VM but it fails to, while the other VM which was using the GPU loses access to it and I have to restart the latter in order to make recover access.
Up to this point everything in the script works almost fine, I will eventually share it once I'm sure enough it is safe for others to use (please also suggest what would be the correct license).
Anyway, since the system has multiple GPUs of the same type, I tried to enhance the script and make it edit the configuration to do the following:
  1. Check if the GPU is currently in use,
  2. If it's not, the VM can be started normally,
  3. If it's in use, find an alternative GPU which is not being used,
  4. If there isn't one, abort and exit,
  5. If there's one, edit the VM's configuration and replace the GPU with the free one,
  6. Continue and start the VM (hopefully with the new configuration),
The problem begins at points 5 and 6, because despite the configuration gets edited correctly, the VM starts with the previous configuration and not the updated one, throwing an error because the GPU it tries to take is already in use.
So the question is: would it be possible to reload the configuration before starting the VM from the hook script? And what would be the correct API to do so?

Thanks in advance
Best regards

P.S. It's worth mentioning that, when Proxmox starts a backup, it will fail due to the reasons described above, hence the motivations for such a script are many.
 
Last edited:
i think that is currently not possible due to how we interact with the config and hookscript
basically we
1. load the config
2. detect there is a hookscript
3. execute the hookscript at various stages
4. start the vm with the loaded config from (1)

though it would be sensible to reload the config after hook-script execution

you can open an enhancement request for this at https://bugzilla.proxmox.com
there we can discuss that behaviour better
no promises though ;)
 
Thank you so much for the clarification.
So as far as I can see there's nothing more I can do with the hookscript.
Would it be possible instead to temporarily workaround the issue by "intercepting" the qm start command for example?

Shall I open a new enhancement request just for the configuration reloading alone?
I mean, is it appropriate if I open one more for the passthrough check I described?
The latter seems kind of useful and important for anyone using passthrough to me.
 
i think i'd be good if you could open a separate one for the assignment check one

though i wanted to implement some more general pci pass through improvements such as assignment checks, delegating to non root@pam users, gui config for vfio binding etc
i am not sure if (or when) i have time for that though (maybe someone else will pick it up)
 
Both requests opened, but I got another related question if I may bother you some more:

The facts are that using a passed through GPU just for computing acceleration purposes poses no problem for the video output because one can always display the VM's desktop on the virtual GPU in the Proxmox console, but when the VM's desktop needs to be run directly on the physical GPU then other means to connect to the remote desktop must be used (eg. VNC server inside the VM or even TeamViewer).
So the question is, do you think it would ever be possible to replace the virtual display (eg. QXL, Virtio GPU, etc) with the actual VNC server or Spice server running inside the VM itself and connect automatically through Proxmox console instead of setting up manually the connection? Obviously Proxmox console would have to be on the same network as the VM to be able to communicate.

Another idea would be using a shared memory device to copy the framebuffer from guest to host like Looking Glass does, but it seems somewhat more complicated, at least to me. I'm not exacly giving my advice here, just asking if you think it would ever make sense to have something like that on Proxmox someday (I don't know how many users could ever need such a feature). I stumbled across several posts in this forum but there the Looking Glass client is being used on the host with a desktop environment with OpenGL acceleration, so no one plans on using it in the console. Also the server part only works on Windows.

Is my question clear? I know there are lots of requests on this forum and I don't want to hurry anyone, so I would just like to know, although not in the near future, are there any plans about this?

Thanks again
Best regards
 
So the question is, do you think it would ever be possible to replace the virtual display (eg. QXL, Virtio GPU, etc) with the actual VNC server or Spice server running inside the VM itself and connect automatically through Proxmox console instead of setting up manually the connection?
i mean it would be possible, but i am not so sure if there is much demand for such a feature

also we would have to be very careful as for the integration with api in regards to permissions etc..

the only other thing that exists is the spice-streaming-agent, redhat started this some time ago, i am not so sure about its current state
what it does is having a looking-glass type of agent in the vm that sends the data over a special spice channel, which the spice client then can decode

though that would not really help, as you would have to use the spice client instead of novnc

Is my question clear? I know there are lots of requests on this forum and I don't want to hurry anyone, so I would just like to know, although not in the near future, are there any plans about this?
no there are no plans, but you can always open a feature request on the bugtracker, worst case is we decide to not implement it at all, second worst case is nobody got the time to implement it
 
the only other thing that exists is the spice-streaming-agent, redhat started this some time ago, i am not so sure about its current state
what it does is having a looking-glass type of agent in the vm that sends the data over a special spice channel, which the spice client then can decode
I didn't now at all about spice streaming agent, I will give it a try. Thank you so much!
 
i must warn you about that, i think there are a few parts missing in pve for it to work (some libs qemu must be linked against, and some special command line arguments to create that spice channel)
but if you get it to work and its usable, i would appreciate some feedback maybe it would pay off to integrate that
 
I was able to build and try spice streaming agent in an Ubuntu 20.04 VM, on Proxmox 6.3-3, here are the steps I followed (copied from bash history):
  1. Install development libraries and tools: sudo apt install build-essential git dh-autoreconf autoconf-archive libspice-protocol-dev libdrm-dev libx11-dev libxfixes-dev libxrandr-dev libjpeg-dev
  2. Clone spice streaming agent v0.3: git clone https://gitlab.freedesktop.org/spice/spice-streaming-agent.git --recursive --branch=v0.3 spice-streaming-agent-0.3 && cd spice-streaming-agent-0.3
  3. Configure and build: ./autogen.sh && make
  4. Install: sudo make install
  5. Ensure to start spice-streaming-agent at boot and keep it running.
  6. Install graphics drivers (in my case NVIDIA): sudo apt install nvidia-driver-460 nvidia-settings
  7. Use the following /etc/X11/xorg.conf to enable the graphics card without any display attached:
    Code:
    Section "ServerLayout" Identifier "Default Layout"
    Screen 0 "Screen0"
    InputDevice "Keyboard0" "CoreKeyboard"
    InputDevice "Mouse0" "CorePointer"
    EndSection
    
    Section "Module"
    Load "modesetting"
    Load "glx"
    EndSection
    
    Section "InputDevice"
    Identifier "Keyboard0"
    Driver "kbd"
    Option "XkbModel" "pc105"
    Option "XkbLayout" "it"
    EndSection
    
    Section "InputDevice"
    Identifier "Mouse0"
    Driver "mouse"
    Option "Protocol" "auto"
    Option "Device" "/dev/psaux"
    Option "Emulate3Buttons" "no"
    Option "ZAxisMapping" "4 5"
    EndSection
    
    Section "Monitor"
    Identifier "Monitor0"
    VendorName "Unknown"
    ModelName "Unknown"
    Option "DPMS"
    EndSection
    
    Section "Device"
    Identifier "Device0"
    Driver "nvidia"
    BusID "PCI:1:0:0"
    EndSection
    
    Section "Screen"
    Identifier "Screen0"
    Device "Device0"
    Monitor "Monitor0"
    DefaultDepth 24
    Option "AllowEmptyInitialConfiguration" "True"
    Option "UseDisplayDevice" "DFP-0"
    Option "CustomEDID" "DFP-0:/etc/X11/edid.txt"
    Option "ConnectedMonitor" "DFP-0"
    SubSection "Display"
    Virtual 1920 1080
    Depth 24
    EndSubSection
    EndSection
  8. Use the following /etc/X11/edid.txt to simulate an attached display:
    Code:
    00 ff ff ff ff ff ff 00 1e 6d f5 56 71 ca 04 00 05 14 01 03 80 35 1e 78 0a ae c5 a2 57 4a 9c 25 12 50 54 21 08 00 b3 00 81 80 81 40 01 01 01 01 01 01 01 01 01 01 1a 36 80 a0 70 38 1f 40 30 20 35 00 13 2b 21 00 00 1a 02 3a 80 18 71 38 2d 40 58 2c 45 00 13 2b 21 00 00 1e 00 00 00 fd 00 38 3d 1e 53 0f 00 0a 20 20 20 20 20 20 00 00 00 fc 00 57 32 34 35 33 0a 20 20 20 20 20 20 20 01 3d 02 03 21 f1 4e 90 04 03 01 14 12 05 1f 10 13 00 00 00 00 23 09 07 07 83 01 00 00 65 03 0c 00 10 00 02 3a 80 18 71 38 2d 40 58 2c 45 00 13 2b 21 00 00 1e 01 1d 80 18 71 1c 16 20 58 2c 25 00 13 2b 21 00 00 9e 01 1d 00 72 51 d0 1e 20 6e 28 55 00 13 2b 21 00 00 1e 8c 0a d0 8a 20 e0 2d 10 10 3e 96 00 13 2b 21 00 00 18 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 26
Then in Proxmox I removed the virtual display adapter and disabled qemu guest agent and added the discrete GPU, edited the VM configuration to add the VBIOS file, and enabled the "Primary GPU" option. I also added the following to the configuration for spice streaming agent:
Code:
args: -spice port=61000,password=password -device virtio-serial -device virtserialport,nr=1,chardev=charchannel1,id=channel1,name=org.spice-space.stream.0 -chardev spiceport,name=org.spice-space.stream.0,id=charchannel1
After restarting the VM and connecting from a client machine through remote-viewer spice://proxmox_ip_address:61000?password=password I was able to use the VM.
With your help it could become even less tricky, but I don't know where to go next to integrate with PVE's spice proxy.
One more thing, it could be useful to know the exact command line arguments that Proxmox passes to kvm, if there's a way.
Thanks again for your collaboration
Best regards
 
Last edited:
nice that you got it to work
With your help it could become even less tricky, but I don't know where to go next to integrate with PVE's spice proxy.
One more thing, it could be useful to know the exact command line arguments that Proxmox passes to kvm, if there's a way.
yeah just checkout 'qm showcmd ID --pretty'

i'll see if i can find the time to test this setup, maybe we can integrate some option to add the channel
 
I would like to update my post with new information. It seems that someone is developing an interesting script to manage GPUs in a scenario similar to mine, but it's still unclear to me what it does exactly: https://github.com/rosineygp/proxmox-pci-switcher
If I understand correctly it shuts down a VM using a single shared GPU when the user wants to turn on another VM.
 

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!