daemon: Move TLS initialization to virInitialize
[libvirt/zwu.git] / docs / hooks.html.in
blob890359e64ccf88caaf39fde638489cccc0cd1572
1 <?xml version="1.0"?>
2 <html>
3 <body>
4 <h1>Hooks for specific system management</h1>
6 <ul id="toc"></ul>
8 <h2><a name="intro">Custom event scripts</a></h2>
9 <p>Beginning with libvirt 0.8.0, specific events on a host system will
10 trigger custom scripts.</p>
11 <p>These custom <b>hook</b> scripts are executed when any of the following
12 actions occur:</p>
13 <ul>
14 <li>The libvirt daemon starts, stops, or reloads its
15 configuration<br/><br/></li>
16 <li>A QEMU guest is started or stopped<br/><br/></li>
17 <li>An LXC guest is started or stopped<br/><br/></li>
18 </ul>
20 <h2><a name="location">Script location</a></h2>
21 <p>The libvirt hook scripts are located in the directory
22 <code>$SYSCONFDIR/libvirt/hooks/</code>.</p>
23 <ul>
24 <li>In Linux distributions such as Fedora and RHEL, this is
25 <code>/etc/libvirt/hooks/</code>. Other Linux distributions may do
26 this differently.</li>
27 <li>If your installation of libvirt has instead been compiled from
28 source, it is likely to be
29 <code>/usr/local/etc/libvirt/hooks/</code>.</li>
30 </ul>
31 <p>To use hook scripts, you will need to create this <code>hooks</code>
32 directory manually, place the desired hook scripts inside, then make
33 them executable.</p>
34 <br/>
36 <h2><a name="names">Script names</a></h2>
37 <p>At present, there are three hook scripts that can be called:</p>
38 <ul>
39 <li><code>/etc/libvirt/hooks/daemon</code><br/><br/>
40 Executed when the libvirt daemon is started, stopped, or reloads
41 its configuration<br/><br/></li>
42 <li><code>/etc/libvirt/hooks/qemu</code><br/><br/>
43 Executed when a QEMU guest is started, stopped, or migrated<br/><br/></li>
44 <li><code>/etc/libvirt/hooks/lxc</code><br /><br/>
45 Executed when an LXC guest is started or stopped</li>
46 </ul>
47 <br/>
49 <h2><a name="structure">Script structure</a></h2>
50 <p>The hook scripts are executed using standard Linux process creation
51 functions. Therefore, they must begin with the declaration of the
52 command interpreter to use.</p>
53 <p>For example:</p>
54 <pre>#!/bin/bash</pre>
55 <p>or:</p>
56 <pre>#!/usr/bin/python</pre>
57 <p>Other command interpreters are equally valid, as is any executable
58 binary, so you are welcome to use your favourite languages.</p>
59 <br/>
61 <h2><a name="arguments">Script arguments</a></h2>
62 <p>The hook scripts are called with specific command line arguments,
63 depending upon the script, and the operation being performed.</p>
64 <p>The guest hook scripts, qemu and lxc, are also given the <b>full</b>
65 XML description for the domain on their stdin. This includes items
66 such the UUID of the domain and its storage information, and is
67 intended to provide all the libvirt information the script needs.</p>
69 <p>The command line arguments take this approach:</p>
70 <ol>
71 <li>The first argument is the name of the <b>object</b> involved in the
72 operation, or '-' if there is none.<br/><br/>
73 For example, the name of a guest being started.<br/><br/></li>
74 <li>The second argument is the name of the <b>operation</b> being
75 performed.<br/><br/>
76 For example, "start" if a guest is being started.<br/><br/></li>
77 <li>The third argument is a <b>sub-operation</b> indication, or '-' if there
78 is none.<br/><br/></li>
79 <li>The last argument is an <b>extra argument</b> string, or '-' if there is
80 none.</li>
81 </ol>
83 <h4><a name="arguments_specifics">Specifics</a></h4>
84 <p>This translates to the following specifics for each hook script:</p>
86 <h5><a name="daemon">/etc/libvirt/hooks/daemon</a></h5>
87 <ul>
88 <li>When the libvirt daemon is started, this script is called as:<br/>
89 <pre>/etc/libvirt/hooks/daemon - start - start</pre></li>
90 <li>When the libvirt daemon is shut down, this script is called as:<br/>
91 <pre>/etc/libvirt/hooks/daemon - shutdown - shutdown</pre></li>
92 <li>When the libvirt daemon receives the SIGHUP signal, it reloads its
93 configuration and triggers the hook script as:<br/>
94 <pre>/etc/libvirt/hooks/daemon - reload begin SIGHUP</pre></li>
95 </ul>
96 <p>Please note that when the libvirt daemon is restarted, the <i>daemon</i>
97 hook script is called once with the "shutdown" operation, and then once
98 with the "start" operation. There is no specific operation to indicate
99 a "restart" is occurring.</p>
101 <h5><a name="qemu">/etc/libvirt/hooks/qemu</a></h5>
102 <ul>
103 <li>Before a QEMU guest is started, the qemu hook script is
104 called in two locations; if either location fails, the guest
105 is not started. The first location, <span class="since">since
106 0.9.0</span>, is before libvirt performs any resource
107 labeling, and the hook can allocate resources not managed by
108 libvirt such as DRBD or missing bridges. This is called as:<br/>
109 <pre>/etc/libvirt/hooks/qemu guest_name prepare begin -</pre>
110 The second location, available <span class="since">Since
111 0.8.0</span>, occurs after libvirt has finished labeling
112 all resources, but has not yet started the guest, called as:<br/>
113 <pre>/etc/libvirt/hooks/qemu guest_name start begin -</pre></li>
114 <li>When a QEMU guest is stopped, the qemu hook script is called
115 in two locations, to match the startup.
116 First, <span class="since">since 0.8.0</span>, the hook is
117 called before libvirt restores any labels:<br/>
118 <pre>/etc/libvirt/hooks/qemu guest_name stopped end -</pre>
119 Then, after libvirt has released all resources, the hook is
120 called again, <span class="since">since 0.9.0</span>, to allow
121 any additional resource cleanup:<br/>
122 <pre>/etc/libvirt/hooks/qemu guest_name release end -</pre></li>
123 </ul>
125 <h5><a name="lxc">/etc/libvirt/hooks/lxc</a></h5>
126 <ul>
127 <li>When an LXC guest is started, the lxc hook script is called as:<br/>
128 <pre>/etc/libvirt/hooks/lxc guest_name start begin -</pre></li>
129 <li>When a LXC guest is stopped, the lxc hook script is called
130 as:<br/>
131 <pre>/etc/libvirt/hooks/lxc guest_name stopped end -</pre></li>
132 </ul>
133 <br/>
135 <h2><a name="execution">Script execution</a></h2>
136 <ul>
137 <li>The "start" operation for the guest hook scripts, qemu and lxc,
138 executes <b>prior</b> to the guest being created. This allows the
139 guest start operation to be aborted if the script returns indicating
140 failure.<br/><br/></li>
141 <li>The "shutdown" operation for the guest hook scripts, qemu and lxc,
142 executes <b>after</b> the guest has stopped. If the hook script
143 indicates failure in its return, the shut down of the guest cannot
144 be aborted because it has already been performed.<br/><br/></li>
145 <li>Hook scripts execute in a synchronous fashion. Libvirt waits
146 for them to return before continuing the given operation.<br/><br/>
147 This is most noticeable with the guest start operation, as a lengthy
148 operation in the hook script can mean an extended wait for the guest
149 to be available to end users.<br/><br/></li>
150 <li>For a hook script to be utilised, it must have its execute bit set
151 (ie. chmod o+rx <i>qemu</i>), and must be present when the libvirt
152 daemon is started.<br/><br/></li>
153 <li>If a hook script is added to a host after the libvirt daemon is
154 already running, it won't be used until the libvirt daemon
155 next starts.</li>
156 </ul>
157 <br/>
159 <h2><a name="qemu_migration">QEMU guest migration</a></h2>
160 <p>Migration of a QEMU guest involves running hook scripts on both the
161 source and destination hosts:</p>
162 <ol>
163 <li>At the beginning of the migration, the <i>qemu</i> hook script on
164 the <b>destination</b> host is executed with the "start"
165 operation.<br/><br/></li>
166 <li>If this hook script returns indicating success (error code 0), the
167 migration continues. Any other return code indicates failure, and
168 the migration is aborted.<br/><br/></li>
169 <li>The QEMU guest is then migrated to the destination host.<br/>
170 <br/></li>
171 <li>Unless an error occurs during the migration process, the <i>qemu</i>
172 hook script on the <b>source</b> host is then executed with the "stopped"
173 operation, to indicate it is no longer running on this
174 host.<br/><br/>
175 Regardless of the return code from this hook script, the migration
176 is not aborted as it has already been performed.</li>
177 </ol>
178 <br/>
180 <h2><a name="recursive">Calling libvirt functions from within a hook script</a></h2>
181 <p><b>DO NOT DO THIS!</b></p>
182 <p>A hook script must not call back into libvirt, as the libvirt daemon
183 is already waiting for the script to exit.</p>
184 <p>A deadlock is likely to occur.</p>
185 <br/>
187 <h2><a name="return_codes">Return codes and logging</a></h2>
188 <p>If a hook script returns with an exit code of 0, the libvirt daemon
189 regards this as successful and performs no logging of it.</p>
190 <p>However, if a hook script returns with a non zero exit code, the libvirt
191 daemon regards this as a failure, logs it with return code 256, and
192 additionally logs anything on stderr the hook script returns.</p>
193 <p>For example, a hook script might use this code to indicate failure,
194 and send a text string to stderr:</p>
195 <pre>echo "Could not find required XYZZY" &gt;&amp;2
196 exit 1</pre>
197 <p>The resulting entry in the libvirt log will appear as:</p>
198 <pre>20:02:40.297: error : virHookCall:416 : Hook script execution failed: Hook script /etc/libvirt/hooks/qemu qemu failed with error code 256:Could not find required XYZZY</pre>
199 </body>
200 </html>