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.




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

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') {

$strXML = file_get_contents('php://stdin');

$doc = new DOMDocument();

$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') {

        foreach ($arg_list as $arg) {
                $keypair = explode('=', $arg);

                if ($keypair[0] == 'host' && !empty($keypair[1])) {


Edited by CaptainSandwich
New info
Link to comment
  • 4 months later...

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.

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.