backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / docs / hooks.html.in
blob7c9d3ef7f3e81603e46b04ca83009877147cd53f
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE html>
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <body>
5 <h1>Hooks for specific system management</h1>
7 <ul id="toc"></ul>
9 <h2><a id="intro">Custom event scripts</a></h2>
10 <p>Beginning with libvirt 0.8.0, specific events on a host system will
11 trigger custom scripts.</p>
12 <p>These custom <b>hook</b> scripts are executed when any of the following
13 actions occur:</p>
14 <ul>
15 <li>The libvirt daemon starts, stops, or reloads its
16 configuration
17 (<span class="since">since 0.8.0</span>)<br/><br/></li>
18 <li>A QEMU guest is started or stopped
19 (<span class="since">since 0.8.0</span>)<br/><br/></li>
20 <li>An LXC guest is started or stopped
21 (<span class="since">since 0.8.0</span>)<br/><br/></li>
22 <li>A libxl-handled Xen guest is started or stopped
23 (<span class="since">since 2.1.0</span>)<br/><br/></li>
24 <li>A network is started or stopped or an interface is
25 plugged/unplugged to/from the network
26 (<span class="since">since 1.2.2</span>)<br/><br/></li>
27 </ul>
29 <h2><a id="location">Script location</a></h2>
30 <p>The libvirt hook scripts are located in the directory
31 <code>$SYSCONFDIR/libvirt/hooks/</code>.</p>
32 <ul>
33 <li>In Linux distributions such as Fedora and RHEL, this is
34 <code>/etc/libvirt/hooks/</code>. Other Linux distributions may do
35 this differently.</li>
36 <li>If your installation of libvirt has instead been compiled from
37 source, it is likely to be
38 <code>/usr/local/etc/libvirt/hooks/</code>.</li>
39 </ul>
40 <p>To use hook scripts, you will need to create this <code>hooks</code>
41 directory manually, place the desired hook scripts inside, then make
42 them executable.</p>
43 <br/>
45 <h2><a id="names">Script names</a></h2>
46 <p>At present, there are five hook scripts that can be called:</p>
47 <ul>
48 <li><code>/etc/libvirt/hooks/daemon</code><br/><br/>
49 Executed when the libvirt daemon is started, stopped, or reloads
50 its configuration<br/><br/></li>
51 <li><code>/etc/libvirt/hooks/qemu</code><br/><br/>
52 Executed when a QEMU guest is started, stopped, or migrated<br/><br/></li>
53 <li><code>/etc/libvirt/hooks/lxc</code><br /><br/>
54 Executed when an LXC guest is started or stopped</li>
55 <li><code>/etc/libvirt/hooks/libxl</code><br/><br/>
56 Executed when a libxl-handled Xen guest is started, stopped, or
57 migrated<br/><br/></li>
58 <li><code>/etc/libvirt/hooks/network</code><br/><br/>
59 Executed when a network is started or stopped or an
60 interface is plugged/unplugged to/from the network</li>
61 </ul>
62 <br/>
64 <h2><a id="structure">Script structure</a></h2>
65 <p>The hook scripts are executed using standard Linux process creation
66 functions. Therefore, they must begin with the declaration of the
67 command interpreter to use.</p>
68 <p>For example:</p>
69 <pre>#!/bin/bash</pre>
70 <p>or:</p>
71 <pre>#!/usr/bin/python</pre>
72 <p>Other command interpreters are equally valid, as is any executable
73 binary, so you are welcome to use your favourite languages.</p>
74 <br/>
76 <h2><a id="arguments">Script arguments</a></h2>
77 <p>The hook scripts are called with specific command line arguments,
78 depending upon the script, and the operation being performed.</p>
79 <p>The guest hook scripts, qemu and lxc, are also given the <b>full</b>
80 XML description for the domain on their stdin. This includes items
81 such the UUID of the domain and its storage information, and is
82 intended to provide all the libvirt information the script needs.</p>
83 <p>For all cases, stdin of the network hook script is provided with the
84 full XML description of the network status in the following form:</p>
86 <pre>&lt;hookData&gt;
87 &lt;network&gt;
88 &lt;name&gt;$network_name&lt;/name&gt;
89 &lt;uuid&gt;afca425a-2c3a-420c-b2fb-dd7b4950d722&lt;/uuid&gt;
90 ...
91 &lt;/network&gt;
92 &lt;/hookData&gt;</pre>
94 <p>In the case of an network port being created / deleted, the network
95 XML will be followed with the full XML description of the port:</p>
97 <pre>&lt;hookData&gt;
98 &lt;network&gt;
99 &lt;name&gt;$network_name&lt;/name&gt;
100 &lt;uuid&gt;afca425a-2c3a-420c-b2fb-dd7b4950d722&lt;/uuid&gt;
102 &lt;/network&gt;
103 &lt;networkport&gt;
104 &lt;uuid&gt;5d744f21-ba4a-4d6e-bdb2-30a35ff3207d&lt;/uuid&gt;
106 &lt;plug type='direct' dev='ens3' mode='vepa'/&gt;
107 &lt;/networkport&gt;
108 &lt;/hookData&gt;</pre>
110 <p>Please note that this approach is different from other cases such as
111 <code>daemon</code>, <code>qemu</code> or <code>lxc</code> hook scripts,
112 because two XMLs may be passed here, while in the other cases only a single
113 XML is passed.</p>
115 <p>The command line arguments take this approach:</p>
116 <ol>
117 <li>The first argument is the name of the <b>object</b> involved in the
118 operation, or '-' if there is none.<br/><br/>
119 For example, the name of a guest being started.<br/><br/></li>
120 <li>The second argument is the name of the <b>operation</b> being
121 performed.<br/><br/>
122 For example, "start" if a guest is being started.<br/><br/></li>
123 <li>The third argument is a <b>sub-operation</b> indication, or '-' if there
124 is none.<br/><br/></li>
125 <li>The last argument is an <b>extra argument</b> string, or '-' if there is
126 none.</li>
127 </ol>
129 <h4><a id="arguments_specifics">Specifics</a></h4>
130 <p>This translates to the following specifics for each hook script:</p>
132 <h5><a id="daemon">/etc/libvirt/hooks/daemon</a></h5>
133 <ul>
134 <li>When the libvirt daemon is started, this script is called as:<br/>
135 <pre>/etc/libvirt/hooks/daemon - start - start</pre></li>
136 <li>When the libvirt daemon is shut down, this script is called as:<br/>
137 <pre>/etc/libvirt/hooks/daemon - shutdown - shutdown</pre></li>
138 <li>When the libvirt daemon receives the SIGHUP signal, it reloads its
139 configuration and triggers the hook script as:<br/>
140 <pre>/etc/libvirt/hooks/daemon - reload begin SIGHUP</pre></li>
141 </ul>
142 <p>Please note that when the libvirt daemon is restarted, the <i>daemon</i>
143 hook script is called once with the "shutdown" operation, and then once
144 with the "start" operation. There is no specific operation to indicate
145 a "restart" is occurring.</p>
147 <h5><a id="qemu">/etc/libvirt/hooks/qemu</a></h5>
148 <ul>
149 <li>Before a QEMU guest is started, the qemu hook script is
150 called in three locations; if any location fails, the guest
151 is not started. The first location, <span class="since">since
152 0.9.0</span>, is before libvirt performs any resource
153 labeling, and the hook can allocate resources not managed by
154 libvirt such as DRBD or missing bridges. This is called as:<br/>
155 <pre>/etc/libvirt/hooks/qemu guest_name prepare begin -</pre>
156 The second location, available <span class="since">Since
157 0.8.0</span>, occurs after libvirt has finished labeling
158 all resources, but has not yet started the guest, called as:<br/>
159 <pre>/etc/libvirt/hooks/qemu guest_name start begin -</pre>
160 The third location, <span class="since">0.9.13</span>,
161 occurs after the QEMU process has successfully started up:<br/>
162 <pre>/etc/libvirt/hooks/qemu guest_name started begin -</pre>
163 </li>
164 <li>When a QEMU guest is stopped, the qemu hook script is called
165 in two locations, to match the startup.
166 First, <span class="since">since 0.8.0</span>, the hook is
167 called before libvirt restores any labels:<br/>
168 <pre>/etc/libvirt/hooks/qemu guest_name stopped end -</pre>
169 Then, after libvirt has released all resources, the hook is
170 called again, <span class="since">since 0.9.0</span>, to allow
171 any additional resource cleanup:<br/>
172 <pre>/etc/libvirt/hooks/qemu guest_name release end -</pre></li>
173 <li><span class="since">Since 0.9.11</span>, the qemu hook script
174 is also called at the beginning of incoming migration. It is called
175 as: <pre>/etc/libvirt/hooks/qemu guest_name migrate begin -</pre>
176 with domain XML sent to standard input of the script. In this case,
177 the script acts as a filter and is supposed to modify the domain
178 XML and print it out on its standard output. Empty output is
179 identical to copying the input XML without changing it. In case the
180 script returns failure or the output XML is not valid, incoming
181 migration will be canceled. This hook may be used, e.g., to change
182 location of disk images for incoming domains.</li>
183 <li><span class="since">Since 1.2.9</span>, the qemu hook script is
184 also called when restoring a saved image either via the API or
185 automatically when restoring a managed save machine. It is called
186 as: <pre>/etc/libvirt/hooks/qemu guest_name restore begin -</pre>
187 with domain XML sent to standard input of the script. In this case,
188 the script acts as a filter and is supposed to modify the domain
189 XML and print it out on its standard output. Empty output is
190 identical to copying the input XML without changing it. In case the
191 script returns failure or the output XML is not valid, restore of the
192 image will be aborted. This hook may be used, e.g., to change
193 location of disk images for restored domains.</li>
194 <li><span class="since">Since 0.9.13</span>, the qemu hook script
195 is also called when the libvirtd daemon restarts and reconnects
196 to previously running QEMU processes. If the script fails, the
197 existing QEMU process will be killed off. It is called as:
198 <pre>/etc/libvirt/hooks/qemu guest_name reconnect begin -</pre>
199 </li>
200 <li><span class="since">Since 0.9.13</span>, the qemu hook script
201 is also called when the QEMU driver is told to attach to an
202 externally launched QEMU process. It is called as:
203 <pre>/etc/libvirt/hooks/qemu guest_name attach begin -</pre>
204 </li>
205 </ul>
207 <h5><a id="lxc">/etc/libvirt/hooks/lxc</a></h5>
208 <ul>
209 <li>Before a LXC guest is started, the lxc hook script is
210 called in three locations; if any location fails, the guest
211 is not started. The first location, <span class="since">since
212 0.9.13</span>, is before libvirt performs any resource
213 labeling, and the hook can allocate resources not managed by
214 libvirt such as DRBD or missing bridges. This is called as:<br/>
215 <pre>/etc/libvirt/hooks/lxc guest_name prepare begin -</pre>
216 The second location, available <span class="since">Since
217 0.8.0</span>, occurs after libvirt has finished labeling
218 all resources, but has not yet started the guest, called as:<br/>
219 <pre>/etc/libvirt/hooks/lxc guest_name start begin -</pre>
220 The third location, <span class="since">0.9.13</span>,
221 occurs after the LXC process has successfully started up:<br/>
222 <pre>/etc/libvirt/hooks/lxc guest_name started begin -</pre>
223 </li>
224 <li>When a LXC guest is stopped, the lxc hook script is called
225 in two locations, to match the startup.
226 First, <span class="since">since 0.8.0</span>, the hook is
227 called before libvirt restores any labels:<br/>
228 <pre>/etc/libvirt/hooks/lxc guest_name stopped end -</pre>
229 Then, after libvirt has released all resources, the hook is
230 called again, <span class="since">since 0.9.0</span>, to allow
231 any additional resource cleanup:<br/>
232 <pre>/etc/libvirt/hooks/lxc guest_name release end -</pre></li>
233 <li><span class="since">Since 0.9.13</span>, the lxc hook script
234 is also called when the libvirtd daemon restarts and reconnects
235 to previously running LXC processes. If the script fails, the
236 existing LXC process will be killed off. It is called as:
237 <pre>/etc/libvirt/hooks/lxc guest_name reconnect begin -</pre>
238 </li>
239 </ul>
241 <h5><a id="libxl">/etc/libvirt/hooks/libxl</a></h5>
242 <ul>
243 <li>Before a Xen guest is started using libxl driver, the libxl hook
244 script is called in three locations; if any location fails, the guest
245 is not started. The first location, <span class="since">since
246 2.1.0</span>, is before libvirt performs any resource
247 labeling, and the hook can allocate resources not managed by
248 libvirt. This is called as:<br/>
249 <pre>/etc/libvirt/hooks/libxl guest_name prepare begin -</pre>
250 The second location, available <span class="since">Since
251 2.1.0</span>, occurs after libvirt has finished labeling
252 all resources, but has not yet started the guest, called as:<br/>
253 <pre>/etc/libvirt/hooks/libxl guest_name start begin -</pre>
254 The third location, <span class="since">2.1.0</span>,
255 occurs after the domain has successfully started up:<br/>
256 <pre>/etc/libvirt/hooks/libxl guest_name started begin -</pre>
257 </li>
258 <li>When a libxl-handled Xen guest is stopped, the libxl hook script
259 is called in two locations, to match the startup.
260 First, <span class="since">since 2.1.0</span>, the hook is
261 called before libvirt restores any labels:<br/>
262 <pre>/etc/libvirt/hooks/libxl guest_name stopped end -</pre>
263 Then, after libvirt has released all resources, the hook is
264 called again, <span class="since">since 2.1.0</span>, to allow
265 any additional resource cleanup:<br/>
266 <pre>/etc/libvirt/hooks/libxl guest_name release end -</pre></li>
267 <li><span class="since">Since 2.1.0</span>, the libxl hook script
268 is also called at the beginning of incoming migration. It is called
269 as: <pre>/etc/libvirt/hooks/libxl guest_name migrate begin -</pre>
270 with domain XML sent to standard input of the script. In this case,
271 the script acts as a filter and is supposed to modify the domain
272 XML and print it out on its standard output. Empty output is
273 identical to copying the input XML without changing it. In case the
274 script returns failure or the output XML is not valid, incoming
275 migration will be canceled. This hook may be used, e.g., to change
276 location of disk images for incoming domains.</li>
277 <li><span class="since">Since 2.1.0</span>, the libxl hook script
278 is also called when the libvirtd daemon restarts and reconnects
279 to previously running Xen domains. If the script fails, the
280 existing Xen domains will be killed off. It is called as:
281 <pre>/etc/libvirt/hooks/libxl guest_name reconnect begin -</pre>
282 </li>
283 </ul>
285 <h5><a id="network">/etc/libvirt/hooks/network</a></h5>
286 <ul>
287 <li><span class="since">Since 1.2.2</span>, before a network is started,
288 this script is called as:<br/>
289 <pre>/etc/libvirt/hooks/network network_name start begin -</pre></li>
290 <li>After the network is started, up &amp; running, the script is
291 called as:<br/>
292 <pre>/etc/libvirt/hooks/network network_name started begin -</pre></li>
293 <li>When a network is shut down, this script is called as:<br/>
294 <pre>/etc/libvirt/hooks/network network_name stopped end -</pre></li>
295 <li>Later, when network is started and there's an interface from a
296 domain to be plugged into the network, the hook script is called as:<br/>
297 <pre>/etc/libvirt/hooks/network network_name port-created begin -</pre>
298 Please note, that in this case, the script is passed both network and
299 port XMLs on its stdin.</li>
300 <li>When network is updated, the hook script is called as:<br/>
301 <pre>/etc/libvirt/hooks/network network_name updated begin -</pre></li>
302 <li>When the domain from previous case is shutting down, the interface
303 is unplugged. This leads to another script invocation:<br/>
304 <pre>/etc/libvirt/hooks/network network_name port-deleted begin -</pre>
305 And again, as in previous case, both network and port XMLs are passed
306 onto script's stdin.</li>
307 </ul>
309 <br/>
311 <h2><a id="execution">Script execution</a></h2>
312 <ul>
313 <li>The "start" operation for the guest and network hook scripts,
314 executes <b>prior</b> to the object (guest or network) being created.
315 This allows the object start operation to be aborted if the script
316 returns indicating failure.<br/><br/></li>
317 <li>The "stopped" operation for the guest and network hook scripts,
318 executes <b>after</b> the object (guest or network) has stopped. If
319 the hook script indicates failure in its return, the shut down of the
320 object cannot be aborted because it has already been performed.
321 <br/><br/></li>
322 <li>Hook scripts execute in a synchronous fashion. Libvirt waits
323 for them to return before continuing the given operation.<br/><br/>
324 This is most noticeable with the guest or network start operation,
325 as a lengthy operation in the hook script can mean an extended wait
326 for the guest or network to be available to end users.<br/><br/></li>
327 <li>For a hook script to be utilised, it must have its execute bit set
328 (e.g. chmod o+rx <i>qemu</i>), and must be present when the libvirt
329 daemon is started.<br/><br/></li>
330 <li>If a hook script is added to a host after the libvirt daemon is
331 already running, it won't be used until the libvirt daemon
332 next starts.</li>
333 </ul>
334 <br/>
336 <h2><a id="qemu_migration">QEMU guest migration</a></h2>
337 <p>Migration of a QEMU guest involves running hook scripts on both the
338 source and destination hosts:</p>
339 <ol>
340 <li>At the beginning of the migration, the <i>qemu</i> hook script on
341 the <b>destination</b> host is executed with the "migrate"
342 operation.</li>
343 <li>Before QEMU process is spawned, the two operations ("prepare" and
344 "start") called for domain start are executed on
345 <b>destination</b> host.</li>
346 <li>If both of these hook script executions exit successfully (exit
347 status 0), the migration continues. Any other exit code indicates
348 failure, and the migration is aborted.</li>
349 <li>The QEMU guest is then migrated to the destination host.</li>
350 <li>Unless an error occurs during the migration process, the <i>qemu</i>
351 hook script on the <b>source</b> host is then executed with the
352 "stopped" and "release" operations to indicate it is no longer
353 running on this host. Regardless of the return codes, the
354 migration is not aborted as it has already been performed.</li>
355 </ol>
356 <br/>
358 <h2><a id="recursive">Calling libvirt functions from within a hook script</a></h2>
359 <p><b>DO NOT DO THIS!</b></p>
360 <p>A hook script must not call back into libvirt, as the libvirt daemon
361 is already waiting for the script to exit.</p>
362 <p>A deadlock is likely to occur.</p>
363 <br/>
365 <h2><a id="return_codes">Return codes and logging</a></h2>
366 <p>If a hook script returns with an exit code of 0, the libvirt daemon
367 regards this as successful and performs no logging of it.</p>
368 <p>However, if a hook script returns with a non zero exit code, the libvirt
369 daemon regards this as a failure, logs its return code, and
370 additionally logs anything on stderr the hook script returns.</p>
371 <p>For example, a hook script might use this code to indicate failure,
372 and send a text string to stderr:</p>
373 <pre>echo "Could not find required XYZZY" &gt;&amp;2
374 exit 1</pre>
375 <p>The resulting entry in the libvirt log will appear as:</p>
376 <pre>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
377 HOME=/root USER=root LOGNAME=root /etc/libvirt/hooks/qemu qemu prepare begin -) unexpected exit status 1: Could not find required XYZZY</pre>
378 </body>
379 </html>