Szymon Niedźwiedź 2023/11/12

Manual qemu hooks configuration

  1. Creating cpu hook which passes access to CPUs, 3 cores are disabled, because core with index 3 is pinned in router configuration.

/etc/libvirt/hooks/qemu-cpu [755]

#!/bin/sh

domain="$1"
if [ "$domain" != "win-lvm" ] && [ "$domain" != "mmpc" ]; then
    exit 0
fi

command="$2"
if [ "$command" = "started" ]; then
    systemctl set-property --runtime -- system.slice AllowedCPUs=0-2
    systemctl set-property --runtime -- user.slice AllowedCPUs=0-2
    systemctl set-property --runtime -- init.scope AllowedCPUs=0-2
elif [ "$command" = "release" ]; then
    systemctl set-property --runtime -- system.slice AllowedCPUs=0-15
    systemctl set-property --runtime -- user.slice AllowedCPUs=0-15
    systemctl set-property --runtime -- init.scope AllowedCPUs=0-15
fi
  1. Creating a hook to prevent host OS from sleeping when VM is running.

/etc/libvirt/hooks/qemu-nosleep [755]

#!/bin/sh

OBJECT="$1"
OPERATION="$2"
SUBOPERATION="$3"
EXTRA_ARG="$4"

if  [ "$OBJECT" != "win-lvm" ] && [ "$OBJECT" != "mmpc" ]; then
    exit 0
fi


case "$OPERATION" in
        "prepare")
                systemctl start libvirt-nosleep@"$OBJECT"
                ;;
        "release")
                systemctl stop libvirt-nosleep@"$OBJECT"
                ;;
esac

Proper service needs also to be created.

/etc/systemd/system/libvirt-nosleep@.service [644]

[Unit]
Description=Preventing sleep while libvirt domain "%i" is running

[Service]
Type=simple
ExecStart=/usr/bin/systemd-inhibit --what=sleep --why="Libvirt domain \"%i\" is running" --who=%U --mode=block sleep infinity
  1. Create a hook to start router VM with Windows VM

/etc/libvirt/hooks/qemu-router [755]

#!/bin/sh -x

domain="$1"
if [ "$domain" = "router" ]; then
    exit 0
fi

action="$2"

if [ "$action" != "started" ]; then
    exit 0
fi

systemctl start router-start

A service is used to start router, because it was easiest way to execute virsh start command externally.

/etc/systemd/system/router-start.service

[Unit]
Description=Start router

[Service]
Type=simple
ExecStart=virsh --connect qemu:///system start "router"

[Install]
WantedBy=multi-user.target

When running virsh restart internally without a systemd service, router machine could not be started as it probably waited for passthrough machine to finish startup process, but that did not happen as it was stuck inside this script.

  1. Reload systemd service to make them useful
# systemctl daemon-reload