burkasaurusrex Posted January 7, 2020 Share Posted January 7, 2020 (edited) I'm having a bit of trouble passing through a GPU so I wanted to write a libvirt hook script to adjust a few things before guest startup and after guest shutdown. It looks like there are already scripts at both /etc/libvirt/hooks/qemu and /etc/libvirt-/hooks/qemu. So two questions: Which libvirt directory is used - libvirt or libvert- ? If I edit the qemu script, will the changes persist between boots or will they be lost like other Unraid changes? Or I'm guessing I could symlink it to somewhere that persists? Separately the PassthroughPOST/VFIO-Tools repo is pretty helpful. I'll most likely port this qemu script to PHP to easily run new scripts if anybody else is interested. Edited January 7, 2020 by burkasaurusrex Quote Link to comment
CaptainSandwich Posted April 18, 2020 Share Posted April 18, 2020 (edited) On 1/7/2020 at 10:05 AM, burkasaurusrex said: Which libvirt directory is used - libvirt or libvert- ? Did you ever find out the answer to this question? I'm looking to setup a script to set nvidia driver persistence mode when a VM shuts down, so the gpu can enter low power state. Thanks. EDIT - looks like /etc/libvirt/hooks/qemu is the one you want to edit. I modified the existing script to the below to set nvidia persistence mode when a VM releases all resources. The script update appears to persist between boots. #!/usr/bin/env php <?php if ($argv[2] == 'release' && $argv[3] == 'end'){ shell_exec('date +"%b %d %H:%M:%S libvirt hook: Setting nVidia Promiscuous mode to 1" >> /var/log/syslog'); shell_exec('nvidia-smi --persistence-mode=1'); } if (!isset($argv[2]) || $argv[2] != 'start') { exit(0); } $strXML = file_get_contents('php://stdin'); $doc = new DOMDocument(); $doc->loadXML($strXML); $xpath = new DOMXpath($doc); $args = $xpath->evaluate("//domain/*[name()='qemu:commandline']/*[name()='qemu:arg']/@value"); for ($i = 0; $i < $args->length; $i++){ $arg_list = explode(',', $args->item($i)->nodeValue); if ($arg_list[0] !== 'vfio-pci') { continue; } foreach ($arg_list as $arg) { $keypair = explode('=', $arg); if ($keypair[0] == 'host' && !empty($keypair[1])) { vfio_bind($keypair[1]); break; } Edited April 19, 2020 by CaptainSandwich New info Quote Link to comment
segator Posted September 13, 2020 Share Posted September 13, 2020 I modified the script for my needs, set VM nice at -20, nvidia-persistance mode on stop. it will be nice if we have something like this https://passthroughpo.st/simple-per-vm-libvirt-hooks-with-the-vfio-tools-hook-helper/ to unraid so we don't need to modify limetech scripts... Quote Link to comment
Vodros Posted May 17, 2023 Share Posted May 17, 2023 On 9/13/2020 at 10:48 AM, segator said: I modified the script for my needs, set VM nice at -20, nvidia-persistance mode on stop. it will be nice if we have something like this https://passthroughpo.st/simple-per-vm-libvirt-hooks-with-the-vfio-tools-hook-helper/ to unraid so we don't need to modify limetech scripts... Would you mind sharing your modified script? Quote Link to comment
jch Posted August 30, 2023 Share Posted August 30, 2023 (edited) If anybody finds this topic like I did, I hope they find this useful. I added this at the top of my `/etc/libvirt/hooks/qemu` file (just under the `<?php` tag: // https://github.com/PassthroughPOST/VFIO-Tools $vfio_args = join(" ", array_map("escapeshellarg", array_slice($argv, 1))); shell_exec("/etc/libvirt/hooks/vfio_tools.sh {$vfio_args}"); Then I put the attached `vfio_tools.sh` in the same directory and `chmod +x vfio_tools.sh`. I modified the original script slightly so that you see the hook executions in the syslog. Then I created the `qemu.d` directory and followed the steps from https://github.com/PassthroughPOST/VFIO-Tools/tree/master to install various hooks. The directory structure is `/etc/libvirt/hooks/qem.d/<VM_NAME>/<HOOK_NAME>/<STATE_NAME>/<SCRIPTS>`. All the events can be found here: https://libvirt.org/hooks.html. I believe you need to make the script file executable as well. You probably need to repeat these steps every time you do an upgrade because unRAID controls the `qemu` script. vfio_tools.sh Edited August 30, 2023 by jch 1 Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.