Beginning with libvirt 0.8.0, specific events on a host system will trigger custom scripts.
These custom hook scripts are executed when any of the following actions occur:
The libvirt hook scripts are located in the directory
$SYSCONFDIR/libvirt/hooks/
.
/etc/libvirt/hooks/
. Other Linux distributions may do
this differently./usr/local/etc/libvirt/hooks/
.To use hook scripts, you will need to create this hooks
directory manually, place the desired hook scripts inside, then make
them executable.
At present, there are three hook scripts that can be called:
/etc/libvirt/hooks/daemon
/etc/libvirt/hooks/qemu
/etc/libvirt/hooks/lxc
/etc/libvirt/hooks/network
The hook scripts are executed using standard Linux process creation functions. Therefore, they must begin with the declaration of the command interpreter to use.
For example:
#!/bin/bash
or:
#!/usr/bin/python
Other command interpreters are equally valid, as is any executable binary, so you are welcome to use your favourite languages.
The hook scripts are called with specific command line arguments, depending upon the script, and the operation being performed.
The guest hook scripts, qemu and lxc, are also given the full XML description for the domain on their stdin. This includes items such the UUID of the domain and its storage information, and is intended to provide all the libvirt information the script needs.
For all cases, stdin of the network hook script is provided with the full XML description of the network status in the following form:
<hookData> <network> <name>$network_name</name> <uuid>afca425a-2c3a-420c-b2fb-dd7b4950d722</uuid> ... </network> </hookData>
In the case of an interface being plugged/unplugged to/from the network, the network XML will be followed with the full XML description of the domain containing the interface that is being plugged/unplugged:
<hookData> <network> <name>$network_name</name> <uuid>afca425a-2c3a-420c-b2fb-dd7b4950d722</uuid> ... </network> <domain type='$domain_type' id='$domain_id'> <name>$domain_name</name> <uuid>afca425a-2c3a-420c-b2fb-dd7b4950d722</uuid> ... </domain> </hookData>
Please note that this approach is different from other cases such as
daemon
, qemu
or lxc
hook scripts,
because two XMLs may be passed here, while in the other cases only a single
XML is passed.
The command line arguments take this approach:
This translates to the following specifics for each hook script:
/etc/libvirt/hooks/daemon - start - start
/etc/libvirt/hooks/daemon - shutdown - shutdown
/etc/libvirt/hooks/daemon - reload begin SIGHUP
Please note that when the libvirt daemon is restarted, the daemon hook script is called once with the "shutdown" operation, and then once with the "start" operation. There is no specific operation to indicate a "restart" is occurring.
/etc/libvirt/hooks/qemu guest_name prepare begin -The second location, available Since 0.8.0, occurs after libvirt has finished labeling all resources, but has not yet started the guest, called as:
/etc/libvirt/hooks/qemu guest_name start begin -The third location, 0.9.13, occurs after the QEMU process has successfully started up:
/etc/libvirt/hooks/qemu guest_name started begin -
/etc/libvirt/hooks/qemu guest_name stopped end -Then, after libvirt has released all resources, the hook is called again, since 0.9.0, to allow any additional resource cleanup:
/etc/libvirt/hooks/qemu guest_name release end -
/etc/libvirt/hooks/qemu guest_name migrate begin -with domain XML sent to standard input of the script. In this case, the script acts as a filter and is supposed to modify the domain XML and print it out on its standard output. Empty output is identical to copying the input XML without changing it. In case the script returns failure or the output XML is not valid, incoming migration will be canceled. This hook may be used, e.g., to change location of disk images for incoming domains.
/etc/libvirt/hooks/qemu guest_name restore begin -with domain XML sent to standard input of the script. In this case, the script acts as a filter and is supposed to modify the domain XML and print it out on its standard output. Empty output is identical to copying the input XML without changing it. In case the script returns failure or the output XML is not valid, restore of the image will be aborted. This hook may be used, e.g., to change location of disk images for restored domains.
/etc/libvirt/hooks/qemu guest_name reconnect begin -
/etc/libvirt/hooks/qemu guest_name attach begin -
/etc/libvirt/hooks/lxc guest_name prepare begin -The second location, available Since 0.8.0, occurs after libvirt has finished labeling all resources, but has not yet started the guest, called as:
/etc/libvirt/hooks/lxc guest_name start begin -The third location, 0.9.13, occurs after the LXC process has successfully started up:
/etc/libvirt/hooks/lxc guest_name started begin -
/etc/libvirt/hooks/lxc guest_name stopped end -Then, after libvirt has released all resources, the hook is called again, since 0.9.0, to allow any additional resource cleanup:
/etc/libvirt/hooks/lxc guest_name release end -
/etc/libvirt/hooks/lxc guest_name reconnect begin -
/etc/libvirt/hooks/network network_name start begin -
/etc/libvirt/hooks/network network_name started begin -
/etc/libvirt/hooks/network network_name stopped end -
/etc/libvirt/hooks/network network_name plugged begin -Please note, that in this case, the script is passed both network and domain XMLs on its stdin.
/etc/libvirt/hooks/network network_name unplugged begin -And again, as in previous case, both network and domain XMLs are passed onto script's stdin.
Migration of a QEMU guest involves running hook scripts on both the source and destination hosts:
DO NOT DO THIS!
A hook script must not call back into libvirt, as the libvirt daemon is already waiting for the script to exit.
A deadlock is likely to occur.
If a hook script returns with an exit code of 0, the libvirt daemon regards this as successful and performs no logging of it.
However, if a hook script returns with a non zero exit code, the libvirt daemon regards this as a failure, logs its return code, and additionally logs anything on stderr the hook script returns.
For example, a hook script might use this code to indicate failure, and send a text string to stderr:
echo "Could not find required XYZZY" >&2 exit 1
The resulting entry in the libvirt log will appear as:
20:02:40.297: error : virHookCall:285 : Hook script execution failed: internal error Child process (LC_ALL=C PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOME=/root USER=root LOGNAME=root /etc/libvirt/hooks/qemu qemu prepare begin -) unexpected exit status 1: Could not find required XYZZY