# Hooks for specific system management

- Custom event scripts
- Script location
- Script names
- Script structure
- Script arguments
- Script execution
- QEMU guest migration
- Calling libvirt functions from within a hook script
- Return codes and logging

## Custom event scripts

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 daemon starts, stops, or reloads its configuration
- A QEMU guest is started or stopped
- An LXC guest is started or stopped

## Script location

The libvirt hook scripts are located in the directory `$SYSCONFDIR/libvirt/hooks/`.

- In Linux distributions such as Fedora and RHEL, this is `/etc/libvirt/hooks/`. Other Linux distributions may do this differently.
- If your installation of libvirt has instead been compiled from source, it is likely to be `/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.

## Script names

At present, there are three hook scripts that can be called:

- `/etc/libvirt/hooks/daemon`
  Executed when the libvirt daemon is started, stopped, or reloads its configuration
- `/etc/libvirt/hooks/qemu`
  Executed when a QEMU guest is started, stopped, or migrated
- `/etc/libvirt/hooks/lxc`
  Executed when an LXC guest is started or stopped

## Script structure

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.

## Script arguments

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.

The command line arguments take this approach:

1. The first argument is the name of the object involved in the operation, or '-' if there is none.
   For example, the name of a guest being started.
2. The second argument is the name of the operation being performed.
   For example, "start" if a guest is being started.
3. The third argument is a sub-operation indication, or '-' if there is none.
4. The last argument is an extra argument string, or '-' if there is none.

#### Specifics

This translates to the following specifics for each hook script:

##### /etc/libvirt/hooks/daemon

- When the libvirt daemon is started, this script is called as:

      /etc/libvirt/hooks/daemon - start - start

- When the libvirt daemon is shut down, this script is called as:

      /etc/libvirt/hooks/daemon - shutdown - shutdown

- When the libvirt daemon receives the SIGHUP signal, it reloads its configuration and triggers the hook script as:

      /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

- Before a QEMU guest is started, the qemu hook script is called in three locations; if any location fails, the guest is not started. The first location, since 0.9.0, is before libvirt performs any resource labeling, and the hook can allocate resources not managed by libvirt such as DRBD or missing bridges. This is called as:

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

- When a QEMU guest is stopped, the qemu hook script is called in two locations, to match the startup. First, since 0.8.0, the hook is called before libvirt restores any labels:

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

- Since 0.9.11, the qemu hook script is also called at the beginning of incoming migration. It is called as:

      /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.
- Since 0.9.13, the qemu hook script is also called when the libvirtd daemon restarts and reconnects to previously running QEMU processes. If the script fails, the existing QEMU process will be killed off. It is called as:

      /etc/libvirt/hooks/qemu guest_name reconnect begin -

- Since 0.9.13, the qemu hook script is also called when the QEMU driver is told to attach to an externally launched QEMU process. It is called as:

      /etc/libvirt/hooks/qemu guest_name attach begin -

##### /etc/libvirt/hooks/lxc

- Before a LXC guest is started, the lxc hook script is called in three locations; if any location fails, the guest is not started. The first location, since 0.9.13, is before libvirt performs any resource labeling, and the hook can allocate resources not managed by libvirt such as DRBD or missing bridges. This is called as:

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

- When a LXC guest is stopped, the lxc hook script is called in two locations, to match the startup. First, since 0.8.0, the hook is called before libvirt restores any labels:

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

- Since 0.9.13, the lxc hook script is also called when the libvirtd daemon restarts and reconnects to previously running LXC processes. If the script fails, the existing LXC process will be killed off. It is called as:

      /etc/libvirt/hooks/lxc guest_name reconnect begin -

## Script execution

- The "start" operation for the guest hook scripts, qemu and lxc, executes prior to the guest being created. This allows the guest start operation to be aborted if the script returns indicating failure.
- The "shutdown" operation for the guest hook scripts, qemu and lxc, executes after the guest has stopped. If the hook script indicates failure in its return, the shut down of the guest cannot be aborted because it has already been performed.
- Hook scripts execute in a synchronous fashion. Libvirt waits for them to return before continuing the given operation.
  This is most noticeable with the guest start operation, as a lengthy operation in the hook script can mean an extended wait for the guest to be available to end users.
- For a hook script to be utilised, it must have its execute bit set (ie. chmod o+rx qemu), and must be present when the libvirt daemon is started.
- If a hook script is added to a host after the libvirt daemon is already running, it won't be used until the libvirt daemon next starts.

## QEMU guest migration

Migration of a QEMU guest involves running hook scripts on both the source and destination hosts:

1. At the beginning of the migration, the qemu hook script on the destination host is executed with the "migrate" operation.
2. Before QEMU process is spawned, the two operations ("prepare" and "start") called for domain start are executed on destination host.
3. If both of these hook script executions exit successfully (exit status 0), the migration continues. Any other exit code indicates failure, and the migration is aborted.
4. The QEMU guest is then migrated to the destination host.
5. Unless an error occurs during the migration process, the qemu hook script on the source host is then executed with the "stopped" and "release" operations to indicate it is no longer running on this host. Regardless of the return codes, the migration is not aborted as it has already been performed.

## Calling libvirt functions from within a hook script

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.

## Return codes and logging

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