Fix 'file(1)' command to work on /dev devices.
[libguestfs.git] / perl / lib / Sys / Guestfs.pm
1 # libguestfs generated file
2 # WARNING: THIS FILE IS GENERATED BY 'src/generator.ml'.
3 # ANY CHANGES YOU MAKE TO THIS FILE WILL BE LOST.
4 #
5 # Copyright (C) 2009 Red Hat Inc.
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2 of the License, or (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
21 =pod
22
23 =head1 NAME
24
25 Sys::Guestfs - Perl bindings for libguestfs
26
27 =head1 SYNOPSIS
28
29  use Sys::Guestfs;
30  
31  my $h = Sys::Guestfs->new ();
32  $h->add_drive ('guest.img');
33  $h->launch ();
34  $h->wait_ready ();
35  $h->mount ('/dev/sda1', '/');
36  $h->touch ('/hello');
37  $h->sync ();
38
39 =head1 DESCRIPTION
40
41 The C<Sys::Guestfs> module provides a Perl XS binding to the
42 libguestfs API for examining and modifying virtual machine
43 disk images.
44
45 Amongst the things this is good for: making batch configuration
46 changes to guests, getting disk used/free statistics (see also:
47 virt-df), migrating between virtualization systems (see also:
48 virt-p2v), performing partial backups, performing partial guest
49 clones, cloning guests and changing registry/UUID/hostname info, and
50 much else besides.
51
52 Libguestfs uses Linux kernel and qemu code, and can access any type of
53 guest filesystem that Linux and qemu can, including but not limited
54 to: ext2/3/4, btrfs, FAT and NTFS, LVM, many different disk partition
55 schemes, qcow, qcow2, vmdk.
56
57 Libguestfs provides ways to enumerate guest storage (eg. partitions,
58 LVs, what filesystem is in each LV, etc.).  It can also run commands
59 in the context of the guest.  Also you can access filesystems over FTP.
60
61 =head1 ERRORS
62
63 All errors turn into calls to C<croak> (see L<Carp(3)>).
64
65 =head1 METHODS
66
67 =over 4
68
69 =cut
70
71 package Sys::Guestfs;
72
73 use strict;
74 use warnings;
75
76 require XSLoader;
77 XSLoader::load ('Sys::Guestfs');
78
79 =item $h = Sys::Guestfs->new ();
80
81 Create a new guestfs handle.
82
83 =cut
84
85 sub new {
86   my $proto = shift;
87   my $class = ref ($proto) || $proto;
88
89   my $self = Sys::Guestfs::_create ();
90   bless $self, $class;
91   return $self;
92 }
93
94 =item $h->add_cdrom ($filename);
95
96 This function adds a virtual CD-ROM disk image to the guest.
97
98 This is equivalent to the qemu parameter C<-cdrom filename>.
99
100 =item $h->add_drive ($filename);
101
102 This function adds a virtual machine disk image C<filename> to the
103 guest.  The first time you call this function, the disk appears as IDE
104 disk 0 (C</dev/sda>) in the guest, the second time as C</dev/sdb>, and
105 so on.
106
107 You don't necessarily need to be root when using libguestfs.  However
108 you obviously do need sufficient permissions to access the filename
109 for whatever operations you want to perform (ie. read access if you
110 just want to read the image or write access if you want to modify the
111 image).
112
113 This is equivalent to the qemu parameter C<-drive file=filename>.
114
115 =item $h->aug_close ();
116
117 Close the current Augeas handle and free up any resources
118 used by it.  After calling this, you have to call
119 C<$h-E<gt>aug_init> again before you can use any other
120 Augeas functions.
121
122 =item ($nrnodes, $created) = $h->aug_defnode ($name, $expr, $val);
123
124 Defines a variable C<name> whose value is the result of
125 evaluating C<expr>.
126
127 If C<expr> evaluates to an empty nodeset, a node is created,
128 equivalent to calling C<$h-E<gt>aug_set> C<expr>, C<value>.
129 C<name> will be the nodeset containing that single node.
130
131 On success this returns a pair containing the
132 number of nodes in the nodeset, and a boolean flag
133 if a node was created.
134
135 =item $nrnodes = $h->aug_defvar ($name, $expr);
136
137 Defines an Augeas variable C<name> whose value is the result
138 of evaluating C<expr>.  If C<expr> is NULL, then C<name> is
139 undefined.
140
141 On success this returns the number of nodes in C<expr>, or
142 C<0> if C<expr> evaluates to something which is not a nodeset.
143
144 =item $val = $h->aug_get ($path);
145
146 Look up the value associated with C<path>.  If C<path>
147 matches exactly one node, the C<value> is returned.
148
149 =item $h->aug_init ($root, $flags);
150
151 Create a new Augeas handle for editing configuration files.
152 If there was any previous Augeas handle associated with this
153 guestfs session, then it is closed.
154
155 You must call this before using any other C<$h-E<gt>aug_*>
156 commands.
157
158 C<root> is the filesystem root.  C<root> must not be NULL,
159 use C</> instead.
160
161 The flags are the same as the flags defined in
162 E<lt>augeas.hE<gt>, the logical I<or> of the following
163 integers:
164
165 =over 4
166
167 =item C<AUG_SAVE_BACKUP> = 1
168
169 Keep the original file with a C<.augsave> extension.
170
171 =item C<AUG_SAVE_NEWFILE> = 2
172
173 Save changes into a file with extension C<.augnew>, and
174 do not overwrite original.  Overrides C<AUG_SAVE_BACKUP>.
175
176 =item C<AUG_TYPE_CHECK> = 4
177
178 Typecheck lenses (can be expensive).
179
180 =item C<AUG_NO_STDINC> = 8
181
182 Do not use standard load path for modules.
183
184 =item C<AUG_SAVE_NOOP> = 16
185
186 Make save a no-op, just record what would have been changed.
187
188 =item C<AUG_NO_LOAD> = 32
189
190 Do not load the tree in C<$h-E<gt>aug_init>.
191
192 =back
193
194 To close the handle, you can call C<$h-E<gt>aug_close>.
195
196 To find out more about Augeas, see L<http://augeas.net/>.
197
198 =item $h->aug_insert ($path, $label, $before);
199
200 Create a new sibling C<label> for C<path>, inserting it into
201 the tree before or after C<path> (depending on the boolean
202 flag C<before>).
203
204 C<path> must match exactly one existing node in the tree, and
205 C<label> must be a label, ie. not contain C</>, C<*> or end
206 with a bracketed index C<[N]>.
207
208 =item $h->aug_load ();
209
210 Load files into the tree.
211
212 See C<aug_load> in the Augeas documentation for the full gory
213 details.
214
215 =item @matches = $h->aug_ls ($path);
216
217 This is just a shortcut for listing C<$h-E<gt>aug_match>
218 C<path/*> and sorting the resulting nodes into alphabetical order.
219
220 =item @matches = $h->aug_match ($path);
221
222 Returns a list of paths which match the path expression C<path>.
223 The returned paths are sufficiently qualified so that they match
224 exactly one node in the current tree.
225
226 =item $h->aug_mv ($src, $dest);
227
228 Move the node C<src> to C<dest>.  C<src> must match exactly
229 one node.  C<dest> is overwritten if it exists.
230
231 =item $nrnodes = $h->aug_rm ($path);
232
233 Remove C<path> and all of its children.
234
235 On success this returns the number of entries which were removed.
236
237 =item $h->aug_save ();
238
239 This writes all pending changes to disk.
240
241 The flags which were passed to C<$h-E<gt>aug_init> affect exactly
242 how files are saved.
243
244 =item $h->aug_set ($path, $val);
245
246 Set the value associated with C<path> to C<value>.
247
248 =item $content = $h->cat ($path);
249
250 Return the contents of the file named C<path>.
251
252 Note that this function cannot correctly handle binary files
253 (specifically, files containing C<\0> character which is treated
254 as end of string).  For those you need to use the C<$h-E<gt>read_file>
255 function which has a more complex interface.
256
257 Because of the message protocol, there is a transfer limit 
258 of somewhere between 2MB and 4MB.  To transfer large files you should use
259 FTP.
260
261 =item $h->chmod ($mode, $path);
262
263 Change the mode (permissions) of C<path> to C<mode>.  Only
264 numeric modes are supported.
265
266 =item $h->chown ($owner, $group, $path);
267
268 Change the file owner to C<owner> and group to C<group>.
269
270 Only numeric uid and gid are supported.  If you want to use
271 names, you will need to locate and parse the password file
272 yourself (Augeas support makes this relatively easy).
273
274 =item $h->config ($qemuparam, $qemuvalue);
275
276 This can be used to add arbitrary qemu command line parameters
277 of the form C<-param value>.  Actually it's not quite arbitrary - we
278 prevent you from setting some parameters which would interfere with
279 parameters that we use.
280
281 The first character of C<param> string must be a C<-> (dash).
282
283 C<value> can be NULL.
284
285 =item $existsflag = $h->exists ($path);
286
287 This returns C<true> if and only if there is a file, directory
288 (or anything) with the given C<path> name.
289
290 See also C<$h-E<gt>is_file>, C<$h-E<gt>is_dir>, C<$h-E<gt>stat>.
291
292 =item $description = $h->file ($path);
293
294 This call uses the standard L<file(1)> command to determine
295 the type or contents of the file.  This also works on devices,
296 for example to find out whether a partition contains a filesystem.
297
298 The exact command which runs is C<file -bsL path>.  Note in
299 particular that the filename is not prepended to the output
300 (the C<-b> option).
301
302 =item $autosync = $h->get_autosync ();
303
304 Get the autosync flag.
305
306 =item $path = $h->get_path ();
307
308 Return the current search path.
309
310 This is always non-NULL.  If it wasn't set already, then this will
311 return the default path.
312
313 =item $verbose = $h->get_verbose ();
314
315 This returns the verbose messages flag.
316
317 =item $dirflag = $h->is_dir ($path);
318
319 This returns C<true> if and only if there is a directory
320 with the given C<path> name.  Note that it returns false for
321 other objects like files.
322
323 See also C<$h-E<gt>stat>.
324
325 =item $fileflag = $h->is_file ($path);
326
327 This returns C<true> if and only if there is a file
328 with the given C<path> name.  Note that it returns false for
329 other objects like directories.
330
331 See also C<$h-E<gt>stat>.
332
333 =item $h->kill_subprocess ();
334
335 This kills the qemu subprocess.  You should never need to call this.
336
337 =item $h->launch ();
338
339 Internally libguestfs is implemented by running a virtual machine
340 using L<qemu(1)>.
341
342 You should call this after configuring the handle
343 (eg. adding drives) but before performing any actions.
344
345 =item @devices = $h->list_devices ();
346
347 List all the block devices.
348
349 The full block device names are returned, eg. C</dev/sda>
350
351 =item @partitions = $h->list_partitions ();
352
353 List all the partitions detected on all block devices.
354
355 The full partition device names are returned, eg. C</dev/sda1>
356
357 This does not return logical volumes.  For that you will need to
358 call C<$h-E<gt>lvs>.
359
360 =item $listing = $h->ll ($directory);
361
362 List the files in C<directory> (relative to the root directory,
363 there is no cwd) in the format of 'ls -la'.
364
365 This command is mostly useful for interactive sessions.  It
366 is I<not> intended that you try to parse the output string.
367
368 =item @listing = $h->ls ($directory);
369
370 List the files in C<directory> (relative to the root directory,
371 there is no cwd).  The '.' and '..' entries are not returned, but
372 hidden files are shown.
373
374 This command is mostly useful for interactive sessions.  Programs
375 should probably use C<$h-E<gt>readdir> instead.
376
377 =item $h->lvcreate ($logvol, $volgroup, $mbytes);
378
379 This creates an LVM volume group called C<logvol>
380 on the volume group C<volgroup>, with C<size> megabytes.
381
382 =item $h->lvm_remove_all ();
383
384 This command removes all LVM logical volumes, volume groups
385 and physical volumes.
386
387 B<This command is dangerous.  Without careful use you
388 can easily destroy all your data>.
389
390 =item @logvols = $h->lvs ();
391
392 List all the logical volumes detected.  This is the equivalent
393 of the L<lvs(8)> command.
394
395 This returns a list of the logical volume device names
396 (eg. C</dev/VolGroup00/LogVol00>).
397
398 See also C<$h-E<gt>lvs_full>.
399
400 =item @logvols = $h->lvs_full ();
401
402 List all the logical volumes detected.  This is the equivalent
403 of the L<lvs(8)> command.  The "full" version includes all fields.
404
405 =item $h->mkdir ($path);
406
407 Create a directory named C<path>.
408
409 =item $h->mkdir_p ($path);
410
411 Create a directory named C<path>, creating any parent directories
412 as necessary.  This is like the C<mkdir -p> shell command.
413
414 =item $h->mkfs ($fstype, $device);
415
416 This creates a filesystem on C<device> (usually a partition
417 of LVM logical volume).  The filesystem type is C<fstype>, for
418 example C<ext3>.
419
420 =item $h->mount ($device, $mountpoint);
421
422 Mount a guest disk at a position in the filesystem.  Block devices
423 are named C</dev/sda>, C</dev/sdb> and so on, as they were added to
424 the guest.  If those block devices contain partitions, they will have
425 the usual names (eg. C</dev/sda1>).  Also LVM C</dev/VG/LV>-style
426 names can be used.
427
428 The rules are the same as for L<mount(2)>:  A filesystem must
429 first be mounted on C</> before others can be mounted.  Other
430 filesystems can only be mounted on directories which already
431 exist.
432
433 The mounted filesystem is writable, if we have sufficient permissions
434 on the underlying device.
435
436 The filesystem options C<sync> and C<noatime> are set with this
437 call, in order to improve reliability.
438
439 =item @devices = $h->mounts ();
440
441 This returns the list of currently mounted filesystems.  It returns
442 the list of devices (eg. C</dev/sda1>, C</dev/VG/LV>).
443
444 Some internal mounts are not shown.
445
446 =item $h->pvcreate ($device);
447
448 This creates an LVM physical volume on the named C<device>,
449 where C<device> should usually be a partition name such
450 as C</dev/sda1>.
451
452 =item @physvols = $h->pvs ();
453
454 List all the physical volumes detected.  This is the equivalent
455 of the L<pvs(8)> command.
456
457 This returns a list of just the device names that contain
458 PVs (eg. C</dev/sda2>).
459
460 See also C<$h-E<gt>pvs_full>.
461
462 =item @physvols = $h->pvs_full ();
463
464 List all the physical volumes detected.  This is the equivalent
465 of the L<pvs(8)> command.  The "full" version includes all fields.
466
467 =item @lines = $h->read_lines ($path);
468
469 Return the contents of the file named C<path>.
470
471 The file contents are returned as a list of lines.  Trailing
472 C<LF> and C<CRLF> character sequences are I<not> returned.
473
474 Note that this function cannot correctly handle binary files
475 (specifically, files containing C<\0> character which is treated
476 as end of line).  For those you need to use the C<$h-E<gt>read_file>
477 function which has a more complex interface.
478
479 =item $h->rm ($path);
480
481 Remove the single file C<path>.
482
483 =item $h->rm_rf ($path);
484
485 Remove the file or directory C<path>, recursively removing the
486 contents if its a directory.  This is like the C<rm -rf> shell
487 command.
488
489 =item $h->rmdir ($path);
490
491 Remove the single directory C<path>.
492
493 =item $h->set_autosync ($autosync);
494
495 If C<autosync> is true, this enables autosync.  Libguestfs will make a
496 best effort attempt to run C<$h-E<gt>sync> when the handle is closed
497 (also if the program exits without closing handles).
498
499 =item $h->set_path ($path);
500
501 Set the path that libguestfs searches for kernel and initrd.img.
502
503 The default is C<$libdir/guestfs> unless overridden by setting
504 C<LIBGUESTFS_PATH> environment variable.
505
506 The string C<path> is stashed in the libguestfs handle, so the caller
507 must make sure it remains valid for the lifetime of the handle.
508
509 Setting C<path> to C<NULL> restores the default path.
510
511 =item $h->set_verbose ($verbose);
512
513 If C<verbose> is true, this turns on verbose messages (to C<stderr>).
514
515 Verbose messages are disabled unless the environment variable
516 C<LIBGUESTFS_DEBUG> is defined and set to C<1>.
517
518 =item $h->sfdisk ($device, $cyls, $heads, $sectors, \@lines);
519
520 This is a direct interface to the L<sfdisk(8)> program for creating
521 partitions on block devices.
522
523 C<device> should be a block device, for example C</dev/sda>.
524
525 C<cyls>, C<heads> and C<sectors> are the number of cylinders, heads
526 and sectors on the device, which are passed directly to sfdisk as
527 the I<-C>, I<-H> and I<-S> parameters.  If you pass C<0> for any
528 of these, then the corresponding parameter is omitted.  Usually for
529 'large' disks, you can just pass C<0> for these, but for small
530 (floppy-sized) disks, sfdisk (or rather, the kernel) cannot work
531 out the right geometry and you will need to tell it.
532
533 C<lines> is a list of lines that we feed to C<sfdisk>.  For more
534 information refer to the L<sfdisk(8)> manpage.
535
536 To create a single partition occupying the whole disk, you would
537 pass C<lines> as a single element list, when the single element being
538 the string C<,> (comma).
539
540 B<This command is dangerous.  Without careful use you
541 can easily destroy all your data>.
542
543 =item $h->sync ();
544
545 This syncs the disk, so that any writes are flushed through to the
546 underlying disk image.
547
548 You should always call this if you have modified a disk image, before
549 closing the handle.
550
551 =item $h->touch ($path);
552
553 Touch acts like the L<touch(1)> command.  It can be used to
554 update the timestamps on a file, or, if the file does not exist,
555 to create a new zero-length file.
556
557 =item $h->umount ($pathordevice);
558
559 This unmounts the given filesystem.  The filesystem may be
560 specified either by its mountpoint (path) or the device which
561 contains the filesystem.
562
563 =item $h->umount_all ();
564
565 This unmounts all mounted filesystems.
566
567 Some internal mounts are not unmounted by this call.
568
569 =item $h->vgcreate ($volgroup, \@physvols);
570
571 This creates an LVM volume group called C<volgroup>
572 from the non-empty list of physical volumes C<physvols>.
573
574 =item @volgroups = $h->vgs ();
575
576 List all the volumes groups detected.  This is the equivalent
577 of the L<vgs(8)> command.
578
579 This returns a list of just the volume group names that were
580 detected (eg. C<VolGroup00>).
581
582 See also C<$h-E<gt>vgs_full>.
583
584 =item @volgroups = $h->vgs_full ();
585
586 List all the volumes groups detected.  This is the equivalent
587 of the L<vgs(8)> command.  The "full" version includes all fields.
588
589 =item $h->wait_ready ();
590
591 Internally libguestfs is implemented by running a virtual machine
592 using L<qemu(1)>.
593
594 You should call this after C<$h-E<gt>launch> to wait for the launch
595 to complete.
596
597 =item $h->write_file ($path, $content, $size);
598
599 This call creates a file called C<path>.  The contents of the
600 file is the string C<content> (which can contain any 8 bit data),
601 with length C<size>.
602
603 As a special case, if C<size> is C<0>
604 then the length is calculated using C<strlen> (so in this case
605 the content cannot contain embedded ASCII NULs).
606
607 Because of the message protocol, there is a transfer limit 
608 of somewhere between 2MB and 4MB.  To transfer large files you should use
609 FTP.
610
611 =cut
612
613 1;
614
615 =back
616
617 =head1 COPYRIGHT
618
619 Copyright (C) 2009 Red Hat Inc.
620
621 =head1 LICENSE
622
623 Please see the file COPYING.LIB for the full license.
624
625 =head1 SEE ALSO
626
627 L<guestfs(3)>, L<guestfish(1)>.
628
629 =cut