Libvirt Hook Scrips


Recommended Posts

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:

  1. Which libvirt directory is used - libvirt or libvert- ?
  2. 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 by burkasaurusrex
Link to comment
  • 3 months later...
On 1/7/2020 at 10:05 AM, burkasaurusrex said:
  1. 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 by CaptainSandwich
New info
Link to comment
  • 4 months later...
  • 2 years later...
  • 3 months later...

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 by jch
  • Like 1
Link to comment

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.