cc8e44f48413df426d95af657e2cb62d80d4fdda
[libguestfs.git] / clone / virt-sysprep.pod
1 =encoding utf8
2
3 =head1 NAME
4
5 virt-sysprep - Reset or unconfigure a virtual machine so clones can be made
6
7 =head1 SYNOPSIS
8
9  virt-sysprep [--options] -d domname
10
11  virt-sysprep [--options] -a disk.img [-a disk.img ...]
12
13 =head1 DESCRIPTION
14
15 Virt-sysprep "resets" or "unconfigures" a virtual machine so that
16 clones can be made from it.  Steps in this process include removing
17 SSH host keys, removing persistent network MAC configuration, and
18 removing user accounts.  Each step can be enabled or disabled as
19 required.
20
21 Virt-sysprep is a simple shell script, allowing easy inspection or
22 customization by the system administrator.
23
24 Virt-sysprep modifies the guest or disk image I<in place>.  The guest
25 must be shut down.  If you want to preserve the existing contents of
26 the guest, you I<must copy or clone the disk first>.
27 See L</COPYING AND CLONING> below.
28
29 "Sysprep" stands for "system preparation" tool.  The name comes from
30 the Microsoft program C<sysprep.exe> which is used to unconfigure
31 Windows machines in preparation for cloning them.  Having said that,
32 virt-sysprep does I<not> currently work on Microsoft Windows guests.
33 We plan to support Windows sysprepping in a future version, and we
34 already have code to do it.
35
36 =head1 OPTIONS
37
38 =over 4
39
40 =item B<--help>
41
42 Display brief help.
43
44 =item B<-a> file
45
46 =item B<--add> file
47
48 Add I<file> which should be a disk image from a virtual machine.
49
50 The format of the disk image is auto-detected.  To override this and
51 force a particular format use the I<--format=..> option.
52
53 =item B<-c> URI
54
55 =item B<--connect> URI
56
57 If using libvirt, connect to the given I<URI>.  If omitted, then we
58 connect to the default libvirt hypervisor.
59
60 If you specify guest block devices directly (I<-a>), then libvirt is
61 not used at all.
62
63 =item B<-d> guest
64
65 =item B<--domain> guest
66
67 Add all the disks from the named libvirt guest.  Domain UUIDs can be
68 used instead of names.
69
70 =item B<--enable=...>
71
72 Choose which sysprep operations to perform.  Give a comma-separated
73 list of operations, for example:
74
75  --enable=ssh-hostkeys,udev-persistent-net
76
77 would enable ONLY C<ssh-hostkeys> and C<udev-persistent-net> operations.
78
79 If the I<--enable> option is not given, then we default to trying all
80 possible sysprep operations.  But some sysprep operations are skipped
81 for some guest types.
82
83 Use I<--list-operations> to list operations supported by a particular
84 version of virt-sysprep.
85
86 See L</OPERATIONS> below for a list and an explanation of each
87 operation.
88
89 =item B<--format=raw|qcow2|..>
90
91 =item B<--format>
92
93 The default for the I<-a> option is to auto-detect the format of the
94 disk image.  Using this forces the disk format for I<-a> options which
95 follow on the command line.  Using I<--format> with no argument
96 switches back to auto-detection for subsequent I<-a> options.
97
98 For example:
99
100  virt-sysprep --format=raw -a disk.img
101
102 forces raw format (no auto-detection) for C<disk.img>.
103
104  virt-sysprep --format=raw -a disk.img --format -a another.img
105
106 forces raw format (no auto-detection) for C<disk.img> and reverts to
107 auto-detection for C<another.img>.
108
109 If you have untrusted raw-format guest disk images, you should use
110 this option to specify the disk format.  This avoids a possible
111 security problem with malicious guests (CVE-2010-3851).
112
113 =item B<--hostname> newhostname
114
115 Change the hostname.  See the L</hostname> operation below.
116 If not given, defaults to C<localhost.localdomain>.
117
118 =item B<--list-operations>
119
120 List the operations supported by the virt-sysprep program.
121
122 =item B<-v>
123
124 =item B<--verbose>
125
126 Enable verbose messages for debugging.
127
128 =item B<-V>
129
130 =item B<--version>
131
132 Display version number and exit.
133
134 =item B<-x>
135
136 Enable tracing of libguestfs API calls.
137
138 =back
139
140 =head1 OPERATIONS
141
142 If the I<--enable> option is I<not> given, then all sysprep operations
143 in the list below are enabled, although some are skipped depending on
144 the type of guest.
145
146 Operations can be individually enabled using the I<--enable> option.
147 Use a comma-separated list, for example:
148
149  virt-sysprep --enable=ssh-hostkeys,udev-persistent-net [etc..]
150
151 To list the operations supported by the current version of
152 virt-sysprep, use I<--list-operations>.
153
154 =head2 hostname
155
156 This changes the hostname of the guest to the value given in the
157 I<--hostname> parameter.
158
159 If the I<--hostname> parameter is not given, then the hostname is
160 changed to C<localhost.localdomain>.
161
162 =head2 net-hwaddr
163
164 Remove HWADDR (hard-coded MAC address) configuration.  For Fedora and
165 Red Hat Enterprise Linux, this is removed from C<ifcfg-*> files.
166
167 =head2 ssh-hostkeys
168
169 This erases the SSH host keys in the guest.
170
171 The SSH host keys are regenerated (differently) next time the guest is
172 booted.
173
174 If, after cloning, the guest gets the same IP address, ssh will give
175 you a stark warning about the host key changing:
176
177  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
178  @    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
179  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
180  IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
181
182 =head2 udev-persistent-net
183
184 This erases udev persistent net rules which map the guest's existing
185 MAC address to a fixed ethernet device (eg. eth0).
186
187 After a guest is cloned, the MAC address usually changes.  Since the
188 old MAC address occupies the old name (eg. eth0), this means the fresh
189 MAC address is assigned to a new name (eg. eth1) and this is usually
190 undesirable.  Erasing the udev persistent net rules avoids this.
191
192 =head1 COPYING AND CLONING
193
194 Virt-sysprep can be used as part of a process of cloning guests, or to
195 prepare a template from which guests can be cloned.  There are many
196 different ways to achieve this using the virt tools, and this section
197 is just an introduction.
198
199 A virtual machine (when switched off) consists of two parts:
200
201 =over 4
202
203 =item I<configuration>
204
205 The configuration or description of the guest.  eg. The libvirt
206 XML (see C<virsh dumpxml>), the running configuration of the guest,
207 or another external format like OVF.
208
209 Some configuration items that might need to be changed:
210
211 =over 4
212
213 =item *
214
215 name
216
217 =item *
218
219 UUID
220
221 =item *
222
223 path to block device(s)
224
225 =item *
226
227 network card MAC address
228
229 =back
230
231 =item I<block device(s)>
232
233 One or more hard disk images, themselves containing files,
234 directories, applications, kernels, configuration, etc.
235
236 Some things inside the block devices that might need to be changed:
237
238 =over 4
239
240 =item *
241
242 hostname and other net configuration
243
244 =item *
245
246 UUID
247
248 =item *
249
250 SSH host keys
251
252 =item *
253
254 Windows unique security ID (SID)
255
256 =item *
257
258 Puppet registration
259
260 =back
261
262 =back
263
264 =head2 COPYING THE BLOCK DEVICE
265
266 Starting with an original guest, you probably wish to copy the guest
267 block device and its configuration to make a template.  Then once you
268 are happy with the template, you will want to make many clones from
269 it.
270
271                         virt-sysprep
272                              |
273                              v
274  original guest --------> template ---------->
275                                       \------> cloned
276                                        \-----> guests
277                                         \---->
278
279 You can, of course, just copy the block device on the host using
280 L<cp(1)> or L<dd(1)>.
281
282                    dd                 dd
283  original guest --------> template ---------->
284                                       \------> cloned
285                                        \-----> guests
286                                         \---->
287
288 There are some smarter (and faster) ways too:
289
290 =over 4
291
292 =item *
293
294                           snapshot
295                 template ---------->
296                             \------> cloned
297                              \-----> guests
298                               \---->
299
300 Use the block device as a backing file and create a snapshot on top
301 for each guest.  The advantage is that you don't need to copy the
302 block device (very fast) and only changes are stored (less storage
303 required).
304
305 Note that writing to the backing file once you have created guests on
306 top of it is not possible: you will corrupt the guests.
307
308 Tools that can do this include:
309 L<qemu-img(1)> (with the I<create -f qcow2 -o backing_file> option),
310 L<lvcreate(8)> (I<--snapshot> option).  Some filesystems (such as
311 btrfs) and most Network Attached Storage devices can also create cheap
312 snapshots from files or LUNs.
313
314 =item *
315
316 Get your NAS to snapshot and/or duplicate the LUN.
317
318 =item *
319
320 Prepare your template using L<virt-sparsify(1)>.  See below.
321
322 =back
323
324 =head2 VIRT-CLONE
325
326 A separate tool, L<virt-clone(1)>, can be used to duplicate the block
327 device and/or modify the external libvirt configuration of a guest.
328 It will reset the name, UUID and MAC address of the guest in the
329 libvirt XML.
330
331 L<virt-clone(1)> does not use libguestfs and cannot look inside the
332 disk image.  This was the original motivation to write virt-sysprep.
333
334 =head2 SPARSIFY
335
336               virt-sparsify
337  original guest --------> template
338
339 L<virt-sparsify(1)> can be used to make the cloning template smaller,
340 making it easier to compress and/or faster to copy.
341
342 Notice that since virt-sparsify also copies the image, you can use it
343 to make the initial copy (instead of C<dd>).
344
345 =head2 RESIZE
346
347                          virt-resize
348                 template ---------->
349                             \------> cloned
350                              \-----> guests
351                               \---->
352
353 If you want to give people cloned guests, but let them pick the size
354 of the guest themselves (eg. depending on how much they are prepared
355 to pay for disk space), then instead of copying the template, you can
356 run L<virt-resize(1)>.  Virt-resize performs a copy and resize, and
357 thus is ideal for cloning guests from a template.
358
359 =head1 SHELL QUOTING
360
361 Libvirt guest names can contain arbitrary characters, some of which
362 have meaning to the shell such as C<#> and space.  You may need to
363 quote or escape these characters on the command line.  See the shell
364 manual page L<sh(1)> for details.
365
366 =head1 EXIT STATUS
367
368 This program returns 0 on success, or 1 if there was an error.
369
370 =head1 SEE ALSO
371
372 L<guestfs(3)>,
373 L<guestfish(1)>,
374 L<virt-clone(1)>,
375 L<virt-rescue(1)>,
376 L<virt-resize(1)>,
377 L<virt-sparsify(1)>,
378 L<virsh(1)>,
379 L<qemu-img(1)>,
380 L<lvcreate(8)>,
381 L<http://libguestfs.org/>,
382 L<http://libvirt.org/>.
383
384 =head1 AUTHOR
385
386 Richard W.M. Jones L<http://people.redhat.com/~rjones/>
387
388 =head1 COPYRIGHT
389
390 Copyright (C) 2011 Red Hat Inc.
391
392 This program is free software; you can redistribute it and/or modify
393 it under the terms of the GNU General Public License as published by
394 the Free Software Foundation; either version 2 of the License, or
395 (at your option) any later version.
396
397 This program is distributed in the hope that it will be useful,
398 but WITHOUT ANY WARRANTY; without even the implied warranty of
399 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
400 GNU General Public License for more details.
401
402 You should have received a copy of the GNU General Public License
403 along with this program; if not, write to the Free Software
404 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.