Skip to content
View in the app

A better way to browse. Learn more.

Unraid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Libvirt Hook Scrips

Featured Replies

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

  • 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

  • 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

  • 1 year later...

hookscript to kill/ start nvidia-persistenced when VM starts/ stops

assume you have "nvidia-persistenced" in go file

 

 

  • 3 months later...
On 1/29/2025 at 5:38 PM, fka said:

hookscript to kill/ start nvidia-persistenced when VM starts/ stops

assume you have "nvidia-persistenced" in go file

 

 

this is exactly what I came for, thanks

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...

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.