KVM: Run a script when a guest is shutdown from within?

I am running KVM as virtualization engine.

Is there a way to make QEMU/libvirt run a script on the host when a guest is shutdown from within (shutdown -h now)?

I want to be able to run some custom clean-up tasks when a guest is stopped.


libvirt provides the possibility to use hooks for specific system management:

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