3 * Copyright (C) 2009 Red Hat Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 (* This script generates a large amount of code and documentation for
21 * all the daemon actions.
23 * To add a new action there are only two files you need to change,
24 * this one to describe the interface (see the big table below), and
25 * daemon/<somefile>.c to write the implementation.
27 * After editing this file, run it (./src/generator.ml) to regenerate
28 * all the output files.
30 * IMPORTANT: This script should NOT print any warnings. If it prints
31 * warnings, you should treat them as errors.
32 * [Need to add -warn-error to ocaml command line]
40 type style = ret * args
42 (* "RErr" as a return value means an int used as a simple error
43 * indication, ie. 0 or -1.
46 (* "RInt" as a return value means an int which is -1 for error
47 * or any value >= 0 on success. Only use this for smallish
48 * positive ints (0 <= i < 2^30).
51 (* "RInt64" is the same as RInt, but is guaranteed to be able
52 * to return a full 64 bit value, _except_ that -1 means error
53 * (so -1 cannot be a valid, non-error return value).
56 (* "RBool" is a bool return value which can be true/false or
60 (* "RConstString" is a string that refers to a constant value.
61 * Try to avoid using this. In particular you cannot use this
62 * for values returned from the daemon, because there is no
63 * thread-safe way to return them in the C API.
65 | RConstString of string
66 (* "RString" and "RStringList" are caller-frees. *)
68 | RStringList of string
69 (* Some limited tuples are possible: *)
70 | RIntBool of string * string
71 (* LVM PVs, VGs and LVs. *)
78 (* Key-value pairs of untyped strings. Turns into a hashtable or
79 * dictionary in languages which support it. DON'T use this as a
80 * general "bucket" for results. Prefer a stronger typed return
81 * value if one is available, or write a custom struct. Don't use
82 * this if the list could potentially be very long, since it is
83 * inefficient. Keys should be unique. NULLs are not permitted.
85 | RHashtable of string
86 (* List of directory entries (the result of readdir(3)). *)
87 | RDirentList of string
89 and args = argt list (* Function parameters, guestfs handle is implicit. *)
91 (* Note in future we should allow a "variable args" parameter as
92 * the final parameter, to allow commands like
93 * chmod mode file [file(s)...]
94 * This is not implemented yet, but many commands (such as chmod)
95 * are currently defined with the argument order keeping this future
96 * possibility in mind.
99 | String of string (* const char *name, cannot be NULL *)
100 | OptString of string (* const char *name, may be NULL *)
101 | StringList of string(* list of strings (each string cannot be NULL) *)
102 | Bool of string (* boolean *)
103 | Int of string (* int (smallish ints, signed, <= 31 bits) *)
104 (* These are treated as filenames (simple string parameters) in
105 * the C API and bindings. But in the RPC protocol, we transfer
106 * the actual file content up to or down from the daemon.
107 * FileIn: local machine -> daemon (in request)
108 * FileOut: daemon -> local machine (in reply)
109 * In guestfish (only), the special name "-" means read from
110 * stdin or write to stdout.
116 | ProtocolLimitWarning (* display warning about protocol size limits *)
117 | DangerWillRobinson (* flags particularly dangerous commands *)
118 | FishAlias of string (* provide an alias for this cmd in guestfish *)
119 | FishAction of string (* call this function in guestfish *)
120 | NotInFish (* do not export via guestfish *)
121 | NotInDocs (* do not add this function to documentation *)
123 let protocol_limit_warning =
124 "Because of the message protocol, there is a transfer limit
125 of somewhere between 2MB and 4MB. To transfer large files you should use
128 let danger_will_robinson =
129 "B<This command is dangerous. Without careful use you
130 can easily destroy all your data>."
132 (* You can supply zero or as many tests as you want per API call.
134 * Note that the test environment has 3 block devices, of size 500MB,
135 * 50MB and 10MB (respectively /dev/sda, /dev/sdb, /dev/sdc), and
136 * a fourth squashfs block device with some known files on it (/dev/sdd).
138 * Note for partitioning purposes, the 500MB device has 1015 cylinders.
139 * Number of cylinders was 63 for IDE emulated disks with precisely
140 * the same size. How exactly this is calculated is a mystery.
142 * The squashfs block device (/dev/sdd) comes from images/test.sqsh.
144 * To be able to run the tests in a reasonable amount of time,
145 * the virtual machine and block devices are reused between tests.
146 * So don't try testing kill_subprocess :-x
148 * Between each test we blockdev-setrw, umount-all, lvm-remove-all.
150 * Don't assume anything about the previous contents of the block
151 * devices. Use 'Init*' to create some initial scenarios.
153 * You can add a prerequisite clause to any individual test. This
154 * is a run-time check, which, if it fails, causes the test to be
155 * skipped. Useful if testing a command which might not work on
156 * all variations of libguestfs builds. A test that has prerequisite
157 * of 'Always' is run unconditionally.
159 * In addition, packagers can skip individual tests by setting the
160 * environment variables: eg:
161 * SKIP_TEST_<CMD>_<NUM>=1 SKIP_TEST_COMMAND_3=1 (skips test #3 of command)
162 * SKIP_TEST_<CMD>=1 SKIP_TEST_ZEROFREE=1 (skips all zerofree tests)
164 type tests = (test_init * test_prereq * test) list
166 (* Run the command sequence and just expect nothing to fail. *)
168 (* Run the command sequence and expect the output of the final
169 * command to be the string.
171 | TestOutput of seq * string
172 (* Run the command sequence and expect the output of the final
173 * command to be the list of strings.
175 | TestOutputList of seq * string list
176 (* Run the command sequence and expect the output of the final
177 * command to be the list of block devices (could be either
178 * "/dev/sd.." or "/dev/hd.." form - we don't check the 5th
179 * character of each string).
181 | TestOutputListOfDevices of seq * string list
182 (* Run the command sequence and expect the output of the final
183 * command to be the integer.
185 | TestOutputInt of seq * int
186 (* Run the command sequence and expect the output of the final
187 * command to be a true value (!= 0 or != NULL).
189 | TestOutputTrue of seq
190 (* Run the command sequence and expect the output of the final
191 * command to be a false value (== 0 or == NULL, but not an error).
193 | TestOutputFalse of seq
194 (* Run the command sequence and expect the output of the final
195 * command to be a list of the given length (but don't care about
198 | TestOutputLength of seq * int
199 (* Run the command sequence and expect the output of the final
200 * command to be a structure.
202 | TestOutputStruct of seq * test_field_compare list
203 (* Run the command sequence and expect the final command (only)
206 | TestLastFail of seq
208 and test_field_compare =
209 | CompareWithInt of string * int
210 | CompareWithString of string * string
211 | CompareFieldsIntEq of string * string
212 | CompareFieldsStrEq of string * string
214 (* Test prerequisites. *)
216 (* Test always runs. *)
218 (* Test is currently disabled - eg. it fails, or it tests some
219 * unimplemented feature.
222 (* 'string' is some C code (a function body) that should return
223 * true or false. The test will run if the code returns true.
226 (* As for 'If' but the test runs _unless_ the code returns true. *)
229 (* Some initial scenarios for testing. *)
231 (* Do nothing, block devices could contain random stuff including
232 * LVM PVs, and some filesystems might be mounted. This is usually
236 (* Block devices are empty and no filesystems are mounted. *)
238 (* /dev/sda contains a single partition /dev/sda1, which is formatted
239 * as ext2, empty [except for lost+found] and mounted on /.
240 * /dev/sdb and /dev/sdc may have random content.
245 * /dev/sda1 (is a PV):
246 * /dev/VG/LV (size 8MB):
247 * formatted as ext2, empty [except for lost+found], mounted on /
248 * /dev/sdb and /dev/sdc may have random content.
252 (* Sequence of commands for testing. *)
254 and cmd = string list
256 (* Note about long descriptions: When referring to another
257 * action, use the format C<guestfs_other> (ie. the full name of
258 * the C function). This will be replaced as appropriate in other
261 * Apart from that, long descriptions are just perldoc paragraphs.
264 (* These test functions are used in the language binding tests. *)
266 let test_all_args = [
269 StringList "strlist";
276 let test_all_rets = [
277 (* except for RErr, which is tested thoroughly elsewhere *)
278 "test0rint", RInt "valout";
279 "test0rint64", RInt64 "valout";
280 "test0rbool", RBool "valout";
281 "test0rconststring", RConstString "valout";
282 "test0rstring", RString "valout";
283 "test0rstringlist", RStringList "valout";
284 "test0rintbool", RIntBool ("valout", "valout");
285 "test0rpvlist", RPVList "valout";
286 "test0rvglist", RVGList "valout";
287 "test0rlvlist", RLVList "valout";
288 "test0rstat", RStat "valout";
289 "test0rstatvfs", RStatVFS "valout";
290 "test0rhashtable", RHashtable "valout";
293 let test_functions = [
294 ("test0", (RErr, test_all_args), -1, [NotInFish; NotInDocs],
296 "internal test function - do not use",
298 This is an internal test function which is used to test whether
299 the automatically generated bindings can handle every possible
300 parameter type correctly.
302 It echos the contents of each parameter to stdout.
304 You probably don't want to call this function.");
308 [(name, (ret, [String "val"]), -1, [NotInFish; NotInDocs],
310 "internal test function - do not use",
312 This is an internal test function which is used to test whether
313 the automatically generated bindings can handle every possible
314 return type correctly.
316 It converts string C<val> to the return type.
318 You probably don't want to call this function.");
319 (name ^ "err", (ret, []), -1, [NotInFish; NotInDocs],
321 "internal test function - do not use",
323 This is an internal test function which is used to test whether
324 the automatically generated bindings can handle every possible
325 return type correctly.
327 This function always returns an error.
329 You probably don't want to call this function.")]
333 (* non_daemon_functions are any functions which don't get processed
334 * in the daemon, eg. functions for setting and getting local
335 * configuration values.
338 let non_daemon_functions = test_functions @ [
339 ("launch", (RErr, []), -1, [FishAlias "run"; FishAction "launch"],
341 "launch the qemu subprocess",
343 Internally libguestfs is implemented by running a virtual machine
346 You should call this after configuring the handle
347 (eg. adding drives) but before performing any actions.");
349 ("wait_ready", (RErr, []), -1, [NotInFish],
351 "wait until the qemu subprocess launches",
353 Internally libguestfs is implemented by running a virtual machine
356 You should call this after C<guestfs_launch> to wait for the launch
359 ("kill_subprocess", (RErr, []), -1, [],
361 "kill the qemu subprocess",
363 This kills the qemu subprocess. You should never need to call this.");
365 ("add_drive", (RErr, [String "filename"]), -1, [FishAlias "add"],
367 "add an image to examine or modify",
369 This function adds a virtual machine disk image C<filename> to the
370 guest. The first time you call this function, the disk appears as IDE
371 disk 0 (C</dev/sda>) in the guest, the second time as C</dev/sdb>, and
374 You don't necessarily need to be root when using libguestfs. However
375 you obviously do need sufficient permissions to access the filename
376 for whatever operations you want to perform (ie. read access if you
377 just want to read the image or write access if you want to modify the
380 This is equivalent to the qemu parameter
381 C<-drive file=filename,cache=off,if=virtio>.
383 Note that this call checks for the existence of C<filename>. This
384 stops you from specifying other types of drive which are supported
385 by qemu such as C<nbd:> and C<http:> URLs. To specify those, use
386 the general C<guestfs_config> call instead.");
388 ("add_cdrom", (RErr, [String "filename"]), -1, [FishAlias "cdrom"],
390 "add a CD-ROM disk image to examine",
392 This function adds a virtual CD-ROM disk image to the guest.
394 This is equivalent to the qemu parameter C<-cdrom filename>.
396 Note that this call checks for the existence of C<filename>. This
397 stops you from specifying other types of drive which are supported
398 by qemu such as C<nbd:> and C<http:> URLs. To specify those, use
399 the general C<guestfs_config> call instead.");
401 ("add_drive_ro", (RErr, [String "filename"]), -1, [FishAlias "add-ro"],
403 "add a drive in snapshot mode (read-only)",
405 This adds a drive in snapshot mode, making it effectively
408 Note that writes to the device are allowed, and will be seen for
409 the duration of the guestfs handle, but they are written
410 to a temporary file which is discarded as soon as the guestfs
411 handle is closed. We don't currently have any method to enable
412 changes to be committed, although qemu can support this.
414 This is equivalent to the qemu parameter
415 C<-drive file=filename,snapshot=on,if=virtio>.
417 Note that this call checks for the existence of C<filename>. This
418 stops you from specifying other types of drive which are supported
419 by qemu such as C<nbd:> and C<http:> URLs. To specify those, use
420 the general C<guestfs_config> call instead.");
422 ("config", (RErr, [String "qemuparam"; OptString "qemuvalue"]), -1, [],
424 "add qemu parameters",
426 This can be used to add arbitrary qemu command line parameters
427 of the form C<-param value>. Actually it's not quite arbitrary - we
428 prevent you from setting some parameters which would interfere with
429 parameters that we use.
431 The first character of C<param> string must be a C<-> (dash).
433 C<value> can be NULL.");
435 ("set_qemu", (RErr, [String "qemu"]), -1, [FishAlias "qemu"],
437 "set the qemu binary",
439 Set the qemu binary that we will use.
441 The default is chosen when the library was compiled by the
444 You can also override this by setting the C<LIBGUESTFS_QEMU>
445 environment variable.
447 Setting C<qemu> to C<NULL> restores the default qemu binary.");
449 ("get_qemu", (RConstString "qemu", []), -1, [],
451 "get the qemu binary",
453 Return the current qemu binary.
455 This is always non-NULL. If it wasn't set already, then this will
456 return the default qemu binary name.");
458 ("set_path", (RErr, [String "path"]), -1, [FishAlias "path"],
460 "set the search path",
462 Set the path that libguestfs searches for kernel and initrd.img.
464 The default is C<$libdir/guestfs> unless overridden by setting
465 C<LIBGUESTFS_PATH> environment variable.
467 Setting C<path> to C<NULL> restores the default path.");
469 ("get_path", (RConstString "path", []), -1, [],
471 "get the search path",
473 Return the current search path.
475 This is always non-NULL. If it wasn't set already, then this will
476 return the default path.");
478 ("set_append", (RErr, [String "append"]), -1, [FishAlias "append"],
480 "add options to kernel command line",
482 This function is used to add additional options to the
483 guest kernel command line.
485 The default is C<NULL> unless overridden by setting
486 C<LIBGUESTFS_APPEND> environment variable.
488 Setting C<append> to C<NULL> means I<no> additional options
489 are passed (libguestfs always adds a few of its own).");
491 ("get_append", (RConstString "append", []), -1, [],
493 "get the additional kernel options",
495 Return the additional kernel options which are added to the
496 guest kernel command line.
498 If C<NULL> then no options are added.");
500 ("set_autosync", (RErr, [Bool "autosync"]), -1, [FishAlias "autosync"],
504 If C<autosync> is true, this enables autosync. Libguestfs will make a
505 best effort attempt to run C<guestfs_umount_all> followed by
506 C<guestfs_sync> when the handle is closed
507 (also if the program exits without closing handles).
509 This is disabled by default (except in guestfish where it is
510 enabled by default).");
512 ("get_autosync", (RBool "autosync", []), -1, [],
516 Get the autosync flag.");
518 ("set_verbose", (RErr, [Bool "verbose"]), -1, [FishAlias "verbose"],
522 If C<verbose> is true, this turns on verbose messages (to C<stderr>).
524 Verbose messages are disabled unless the environment variable
525 C<LIBGUESTFS_DEBUG> is defined and set to C<1>.");
527 ("get_verbose", (RBool "verbose", []), -1, [],
531 This returns the verbose messages flag.");
533 ("is_ready", (RBool "ready", []), -1, [],
535 "is ready to accept commands",
537 This returns true iff this handle is ready to accept commands
538 (in the C<READY> state).
540 For more information on states, see L<guestfs(3)>.");
542 ("is_config", (RBool "config", []), -1, [],
544 "is in configuration state",
546 This returns true iff this handle is being configured
547 (in the C<CONFIG> state).
549 For more information on states, see L<guestfs(3)>.");
551 ("is_launching", (RBool "launching", []), -1, [],
553 "is launching subprocess",
555 This returns true iff this handle is launching the subprocess
556 (in the C<LAUNCHING> state).
558 For more information on states, see L<guestfs(3)>.");
560 ("is_busy", (RBool "busy", []), -1, [],
562 "is busy processing a command",
564 This returns true iff this handle is busy processing a command
565 (in the C<BUSY> state).
567 For more information on states, see L<guestfs(3)>.");
569 ("get_state", (RInt "state", []), -1, [],
571 "get the current state",
573 This returns the current state as an opaque integer. This is
574 only useful for printing debug and internal error messages.
576 For more information on states, see L<guestfs(3)>.");
578 ("set_busy", (RErr, []), -1, [NotInFish],
582 This sets the state to C<BUSY>. This is only used when implementing
583 actions using the low-level API.
585 For more information on states, see L<guestfs(3)>.");
587 ("set_ready", (RErr, []), -1, [NotInFish],
589 "set state to ready",
591 This sets the state to C<READY>. This is only used when implementing
592 actions using the low-level API.
594 For more information on states, see L<guestfs(3)>.");
596 ("end_busy", (RErr, []), -1, [NotInFish],
598 "leave the busy state",
600 This sets the state to C<READY>, or if in C<CONFIG> then it leaves the
601 state as is. This is only used when implementing
602 actions using the low-level API.
604 For more information on states, see L<guestfs(3)>.");
606 ("set_memsize", (RErr, [Int "memsize"]), -1, [FishAlias "memsize"],
608 "set memory allocated to the qemu subprocess",
610 This sets the memory size in megabytes allocated to the
611 qemu subprocess. This only has any effect if called before
614 You can also change this by setting the environment
615 variable C<LIBGUESTFS_MEMSIZE> before the handle is
618 For more information on the architecture of libguestfs,
619 see L<guestfs(3)>.");
621 ("get_memsize", (RInt "memsize", []), -1, [],
623 "get memory allocated to the qemu subprocess",
625 This gets the memory size in megabytes allocated to the
628 If C<guestfs_set_memsize> was not called
629 on this handle, and if C<LIBGUESTFS_MEMSIZE> was not set,
630 then this returns the compiled-in default value for memsize.
632 For more information on the architecture of libguestfs,
633 see L<guestfs(3)>.");
637 (* daemon_functions are any functions which cause some action
638 * to take place in the daemon.
641 let daemon_functions = [
642 ("mount", (RErr, [String "device"; String "mountpoint"]), 1, [],
643 [InitEmpty, Always, TestOutput (
644 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
645 ["mkfs"; "ext2"; "/dev/sda1"];
646 ["mount"; "/dev/sda1"; "/"];
647 ["write_file"; "/new"; "new file contents"; "0"];
648 ["cat"; "/new"]], "new file contents")],
649 "mount a guest disk at a position in the filesystem",
651 Mount a guest disk at a position in the filesystem. Block devices
652 are named C</dev/sda>, C</dev/sdb> and so on, as they were added to
653 the guest. If those block devices contain partitions, they will have
654 the usual names (eg. C</dev/sda1>). Also LVM C</dev/VG/LV>-style
657 The rules are the same as for L<mount(2)>: A filesystem must
658 first be mounted on C</> before others can be mounted. Other
659 filesystems can only be mounted on directories which already
662 The mounted filesystem is writable, if we have sufficient permissions
663 on the underlying device.
665 The filesystem options C<sync> and C<noatime> are set with this
666 call, in order to improve reliability.");
668 ("sync", (RErr, []), 2, [],
669 [ InitEmpty, Always, TestRun [["sync"]]],
670 "sync disks, writes are flushed through to the disk image",
672 This syncs the disk, so that any writes are flushed through to the
673 underlying disk image.
675 You should always call this if you have modified a disk image, before
676 closing the handle.");
678 ("touch", (RErr, [String "path"]), 3, [],
679 [InitBasicFS, Always, TestOutputTrue (
681 ["exists"; "/new"]])],
682 "update file timestamps or create a new file",
684 Touch acts like the L<touch(1)> command. It can be used to
685 update the timestamps on a file, or, if the file does not exist,
686 to create a new zero-length file.");
688 ("cat", (RString "content", [String "path"]), 4, [ProtocolLimitWarning],
689 [InitBasicFS, Always, TestOutput (
690 [["write_file"; "/new"; "new file contents"; "0"];
691 ["cat"; "/new"]], "new file contents")],
692 "list the contents of a file",
694 Return the contents of the file named C<path>.
696 Note that this function cannot correctly handle binary files
697 (specifically, files containing C<\\0> character which is treated
698 as end of string). For those you need to use the C<guestfs_download>
699 function which has a more complex interface.");
701 ("ll", (RString "listing", [String "directory"]), 5, [],
702 [], (* XXX Tricky to test because it depends on the exact format
703 * of the 'ls -l' command, which changes between F10 and F11.
705 "list the files in a directory (long format)",
707 List the files in C<directory> (relative to the root directory,
708 there is no cwd) in the format of 'ls -la'.
710 This command is mostly useful for interactive sessions. It
711 is I<not> intended that you try to parse the output string.");
713 ("ls", (RStringList "listing", [String "directory"]), 6, [],
714 [InitBasicFS, Always, TestOutputList (
717 ["touch"; "/newest"];
718 ["ls"; "/"]], ["lost+found"; "new"; "newer"; "newest"])],
719 "list the files in a directory",
721 List the files in C<directory> (relative to the root directory,
722 there is no cwd). The '.' and '..' entries are not returned, but
723 hidden files are shown.
725 This command is mostly useful for interactive sessions. Programs
726 should probably use C<guestfs_readdir> instead.");
728 ("list_devices", (RStringList "devices", []), 7, [],
729 [InitEmpty, Always, TestOutputListOfDevices (
730 [["list_devices"]], ["/dev/sda"; "/dev/sdb"; "/dev/sdc"; "/dev/sdd"])],
731 "list the block devices",
733 List all the block devices.
735 The full block device names are returned, eg. C</dev/sda>");
737 ("list_partitions", (RStringList "partitions", []), 8, [],
738 [InitBasicFS, Always, TestOutputListOfDevices (
739 [["list_partitions"]], ["/dev/sda1"]);
740 InitEmpty, Always, TestOutputListOfDevices (
741 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",200 ,400 ,"];
742 ["list_partitions"]], ["/dev/sda1"; "/dev/sda2"; "/dev/sda3"])],
743 "list the partitions",
745 List all the partitions detected on all block devices.
747 The full partition device names are returned, eg. C</dev/sda1>
749 This does not return logical volumes. For that you will need to
750 call C<guestfs_lvs>.");
752 ("pvs", (RStringList "physvols", []), 9, [],
753 [InitBasicFSonLVM, Always, TestOutputListOfDevices (
754 [["pvs"]], ["/dev/sda1"]);
755 InitEmpty, Always, TestOutputListOfDevices (
756 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",200 ,400 ,"];
757 ["pvcreate"; "/dev/sda1"];
758 ["pvcreate"; "/dev/sda2"];
759 ["pvcreate"; "/dev/sda3"];
760 ["pvs"]], ["/dev/sda1"; "/dev/sda2"; "/dev/sda3"])],
761 "list the LVM physical volumes (PVs)",
763 List all the physical volumes detected. This is the equivalent
764 of the L<pvs(8)> command.
766 This returns a list of just the device names that contain
767 PVs (eg. C</dev/sda2>).
769 See also C<guestfs_pvs_full>.");
771 ("vgs", (RStringList "volgroups", []), 10, [],
772 [InitBasicFSonLVM, Always, TestOutputList (
774 InitEmpty, Always, TestOutputList (
775 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",200 ,400 ,"];
776 ["pvcreate"; "/dev/sda1"];
777 ["pvcreate"; "/dev/sda2"];
778 ["pvcreate"; "/dev/sda3"];
779 ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"];
780 ["vgcreate"; "VG2"; "/dev/sda3"];
781 ["vgs"]], ["VG1"; "VG2"])],
782 "list the LVM volume groups (VGs)",
784 List all the volumes groups detected. This is the equivalent
785 of the L<vgs(8)> command.
787 This returns a list of just the volume group names that were
788 detected (eg. C<VolGroup00>).
790 See also C<guestfs_vgs_full>.");
792 ("lvs", (RStringList "logvols", []), 11, [],
793 [InitBasicFSonLVM, Always, TestOutputList (
794 [["lvs"]], ["/dev/VG/LV"]);
795 InitEmpty, Always, TestOutputList (
796 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",200 ,400 ,"];
797 ["pvcreate"; "/dev/sda1"];
798 ["pvcreate"; "/dev/sda2"];
799 ["pvcreate"; "/dev/sda3"];
800 ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"];
801 ["vgcreate"; "VG2"; "/dev/sda3"];
802 ["lvcreate"; "LV1"; "VG1"; "50"];
803 ["lvcreate"; "LV2"; "VG1"; "50"];
804 ["lvcreate"; "LV3"; "VG2"; "50"];
805 ["lvs"]], ["/dev/VG1/LV1"; "/dev/VG1/LV2"; "/dev/VG2/LV3"])],
806 "list the LVM logical volumes (LVs)",
808 List all the logical volumes detected. This is the equivalent
809 of the L<lvs(8)> command.
811 This returns a list of the logical volume device names
812 (eg. C</dev/VolGroup00/LogVol00>).
814 See also C<guestfs_lvs_full>.");
816 ("pvs_full", (RPVList "physvols", []), 12, [],
817 [], (* XXX how to test? *)
818 "list the LVM physical volumes (PVs)",
820 List all the physical volumes detected. This is the equivalent
821 of the L<pvs(8)> command. The \"full\" version includes all fields.");
823 ("vgs_full", (RVGList "volgroups", []), 13, [],
824 [], (* XXX how to test? *)
825 "list the LVM volume groups (VGs)",
827 List all the volumes groups detected. This is the equivalent
828 of the L<vgs(8)> command. The \"full\" version includes all fields.");
830 ("lvs_full", (RLVList "logvols", []), 14, [],
831 [], (* XXX how to test? *)
832 "list the LVM logical volumes (LVs)",
834 List all the logical volumes detected. This is the equivalent
835 of the L<lvs(8)> command. The \"full\" version includes all fields.");
837 ("read_lines", (RStringList "lines", [String "path"]), 15, [],
838 [InitBasicFS, Always, TestOutputList (
839 [["write_file"; "/new"; "line1\r\nline2\nline3"; "0"];
840 ["read_lines"; "/new"]], ["line1"; "line2"; "line3"]);
841 InitBasicFS, Always, TestOutputList (
842 [["write_file"; "/new"; ""; "0"];
843 ["read_lines"; "/new"]], [])],
844 "read file as lines",
846 Return the contents of the file named C<path>.
848 The file contents are returned as a list of lines. Trailing
849 C<LF> and C<CRLF> character sequences are I<not> returned.
851 Note that this function cannot correctly handle binary files
852 (specifically, files containing C<\\0> character which is treated
853 as end of line). For those you need to use the C<guestfs_read_file>
854 function which has a more complex interface.");
856 ("aug_init", (RErr, [String "root"; Int "flags"]), 16, [],
857 [], (* XXX Augeas code needs tests. *)
858 "create a new Augeas handle",
860 Create a new Augeas handle for editing configuration files.
861 If there was any previous Augeas handle associated with this
862 guestfs session, then it is closed.
864 You must call this before using any other C<guestfs_aug_*>
867 C<root> is the filesystem root. C<root> must not be NULL,
870 The flags are the same as the flags defined in
871 E<lt>augeas.hE<gt>, the logical I<or> of the following
876 =item C<AUG_SAVE_BACKUP> = 1
878 Keep the original file with a C<.augsave> extension.
880 =item C<AUG_SAVE_NEWFILE> = 2
882 Save changes into a file with extension C<.augnew>, and
883 do not overwrite original. Overrides C<AUG_SAVE_BACKUP>.
885 =item C<AUG_TYPE_CHECK> = 4
887 Typecheck lenses (can be expensive).
889 =item C<AUG_NO_STDINC> = 8
891 Do not use standard load path for modules.
893 =item C<AUG_SAVE_NOOP> = 16
895 Make save a no-op, just record what would have been changed.
897 =item C<AUG_NO_LOAD> = 32
899 Do not load the tree in C<guestfs_aug_init>.
903 To close the handle, you can call C<guestfs_aug_close>.
905 To find out more about Augeas, see L<http://augeas.net/>.");
907 ("aug_close", (RErr, []), 26, [],
908 [], (* XXX Augeas code needs tests. *)
909 "close the current Augeas handle",
911 Close the current Augeas handle and free up any resources
912 used by it. After calling this, you have to call
913 C<guestfs_aug_init> again before you can use any other
916 ("aug_defvar", (RInt "nrnodes", [String "name"; OptString "expr"]), 17, [],
917 [], (* XXX Augeas code needs tests. *)
918 "define an Augeas variable",
920 Defines an Augeas variable C<name> whose value is the result
921 of evaluating C<expr>. If C<expr> is NULL, then C<name> is
924 On success this returns the number of nodes in C<expr>, or
925 C<0> if C<expr> evaluates to something which is not a nodeset.");
927 ("aug_defnode", (RIntBool ("nrnodes", "created"), [String "name"; String "expr"; String "val"]), 18, [],
928 [], (* XXX Augeas code needs tests. *)
929 "define an Augeas node",
931 Defines a variable C<name> whose value is the result of
934 If C<expr> evaluates to an empty nodeset, a node is created,
935 equivalent to calling C<guestfs_aug_set> C<expr>, C<value>.
936 C<name> will be the nodeset containing that single node.
938 On success this returns a pair containing the
939 number of nodes in the nodeset, and a boolean flag
940 if a node was created.");
942 ("aug_get", (RString "val", [String "path"]), 19, [],
943 [], (* XXX Augeas code needs tests. *)
944 "look up the value of an Augeas path",
946 Look up the value associated with C<path>. If C<path>
947 matches exactly one node, the C<value> is returned.");
949 ("aug_set", (RErr, [String "path"; String "val"]), 20, [],
950 [], (* XXX Augeas code needs tests. *)
951 "set Augeas path to value",
953 Set the value associated with C<path> to C<value>.");
955 ("aug_insert", (RErr, [String "path"; String "label"; Bool "before"]), 21, [],
956 [], (* XXX Augeas code needs tests. *)
957 "insert a sibling Augeas node",
959 Create a new sibling C<label> for C<path>, inserting it into
960 the tree before or after C<path> (depending on the boolean
963 C<path> must match exactly one existing node in the tree, and
964 C<label> must be a label, ie. not contain C</>, C<*> or end
965 with a bracketed index C<[N]>.");
967 ("aug_rm", (RInt "nrnodes", [String "path"]), 22, [],
968 [], (* XXX Augeas code needs tests. *)
969 "remove an Augeas path",
971 Remove C<path> and all of its children.
973 On success this returns the number of entries which were removed.");
975 ("aug_mv", (RErr, [String "src"; String "dest"]), 23, [],
976 [], (* XXX Augeas code needs tests. *)
979 Move the node C<src> to C<dest>. C<src> must match exactly
980 one node. C<dest> is overwritten if it exists.");
982 ("aug_match", (RStringList "matches", [String "path"]), 24, [],
983 [], (* XXX Augeas code needs tests. *)
984 "return Augeas nodes which match path",
986 Returns a list of paths which match the path expression C<path>.
987 The returned paths are sufficiently qualified so that they match
988 exactly one node in the current tree.");
990 ("aug_save", (RErr, []), 25, [],
991 [], (* XXX Augeas code needs tests. *)
992 "write all pending Augeas changes to disk",
994 This writes all pending changes to disk.
996 The flags which were passed to C<guestfs_aug_init> affect exactly
997 how files are saved.");
999 ("aug_load", (RErr, []), 27, [],
1000 [], (* XXX Augeas code needs tests. *)
1001 "load files into the tree",
1003 Load files into the tree.
1005 See C<aug_load> in the Augeas documentation for the full gory
1008 ("aug_ls", (RStringList "matches", [String "path"]), 28, [],
1009 [], (* XXX Augeas code needs tests. *)
1010 "list Augeas nodes under a path",
1012 This is just a shortcut for listing C<guestfs_aug_match>
1013 C<path/*> and sorting the resulting nodes into alphabetical order.");
1015 ("rm", (RErr, [String "path"]), 29, [],
1016 [InitBasicFS, Always, TestRun
1019 InitBasicFS, Always, TestLastFail
1021 InitBasicFS, Always, TestLastFail
1026 Remove the single file C<path>.");
1028 ("rmdir", (RErr, [String "path"]), 30, [],
1029 [InitBasicFS, Always, TestRun
1032 InitBasicFS, Always, TestLastFail
1033 [["rmdir"; "/new"]];
1034 InitBasicFS, Always, TestLastFail
1036 ["rmdir"; "/new"]]],
1037 "remove a directory",
1039 Remove the single directory C<path>.");
1041 ("rm_rf", (RErr, [String "path"]), 31, [],
1042 [InitBasicFS, Always, TestOutputFalse
1044 ["mkdir"; "/new/foo"];
1045 ["touch"; "/new/foo/bar"];
1047 ["exists"; "/new"]]],
1048 "remove a file or directory recursively",
1050 Remove the file or directory C<path>, recursively removing the
1051 contents if its a directory. This is like the C<rm -rf> shell
1054 ("mkdir", (RErr, [String "path"]), 32, [],
1055 [InitBasicFS, Always, TestOutputTrue
1057 ["is_dir"; "/new"]];
1058 InitBasicFS, Always, TestLastFail
1059 [["mkdir"; "/new/foo/bar"]]],
1060 "create a directory",
1062 Create a directory named C<path>.");
1064 ("mkdir_p", (RErr, [String "path"]), 33, [],
1065 [InitBasicFS, Always, TestOutputTrue
1066 [["mkdir_p"; "/new/foo/bar"];
1067 ["is_dir"; "/new/foo/bar"]];
1068 InitBasicFS, Always, TestOutputTrue
1069 [["mkdir_p"; "/new/foo/bar"];
1070 ["is_dir"; "/new/foo"]];
1071 InitBasicFS, Always, TestOutputTrue
1072 [["mkdir_p"; "/new/foo/bar"];
1073 ["is_dir"; "/new"]];
1074 (* Regression tests for RHBZ#503133: *)
1075 InitBasicFS, Always, TestRun
1077 ["mkdir_p"; "/new"]];
1078 InitBasicFS, Always, TestLastFail
1080 ["mkdir_p"; "/new"]]],
1081 "create a directory and parents",
1083 Create a directory named C<path>, creating any parent directories
1084 as necessary. This is like the C<mkdir -p> shell command.");
1086 ("chmod", (RErr, [Int "mode"; String "path"]), 34, [],
1087 [], (* XXX Need stat command to test *)
1090 Change the mode (permissions) of C<path> to C<mode>. Only
1091 numeric modes are supported.");
1093 ("chown", (RErr, [Int "owner"; Int "group"; String "path"]), 35, [],
1094 [], (* XXX Need stat command to test *)
1095 "change file owner and group",
1097 Change the file owner to C<owner> and group to C<group>.
1099 Only numeric uid and gid are supported. If you want to use
1100 names, you will need to locate and parse the password file
1101 yourself (Augeas support makes this relatively easy).");
1103 ("exists", (RBool "existsflag", [String "path"]), 36, [],
1104 [InitBasicFS, Always, TestOutputTrue (
1106 ["exists"; "/new"]]);
1107 InitBasicFS, Always, TestOutputTrue (
1109 ["exists"; "/new"]])],
1110 "test if file or directory exists",
1112 This returns C<true> if and only if there is a file, directory
1113 (or anything) with the given C<path> name.
1115 See also C<guestfs_is_file>, C<guestfs_is_dir>, C<guestfs_stat>.");
1117 ("is_file", (RBool "fileflag", [String "path"]), 37, [],
1118 [InitBasicFS, Always, TestOutputTrue (
1120 ["is_file"; "/new"]]);
1121 InitBasicFS, Always, TestOutputFalse (
1123 ["is_file"; "/new"]])],
1124 "test if file exists",
1126 This returns C<true> if and only if there is a file
1127 with the given C<path> name. Note that it returns false for
1128 other objects like directories.
1130 See also C<guestfs_stat>.");
1132 ("is_dir", (RBool "dirflag", [String "path"]), 38, [],
1133 [InitBasicFS, Always, TestOutputFalse (
1135 ["is_dir"; "/new"]]);
1136 InitBasicFS, Always, TestOutputTrue (
1138 ["is_dir"; "/new"]])],
1139 "test if file exists",
1141 This returns C<true> if and only if there is a directory
1142 with the given C<path> name. Note that it returns false for
1143 other objects like files.
1145 See also C<guestfs_stat>.");
1147 ("pvcreate", (RErr, [String "device"]), 39, [],
1148 [InitEmpty, Always, TestOutputListOfDevices (
1149 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",200 ,400 ,"];
1150 ["pvcreate"; "/dev/sda1"];
1151 ["pvcreate"; "/dev/sda2"];
1152 ["pvcreate"; "/dev/sda3"];
1153 ["pvs"]], ["/dev/sda1"; "/dev/sda2"; "/dev/sda3"])],
1154 "create an LVM physical volume",
1156 This creates an LVM physical volume on the named C<device>,
1157 where C<device> should usually be a partition name such
1160 ("vgcreate", (RErr, [String "volgroup"; StringList "physvols"]), 40, [],
1161 [InitEmpty, Always, TestOutputList (
1162 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",200 ,400 ,"];
1163 ["pvcreate"; "/dev/sda1"];
1164 ["pvcreate"; "/dev/sda2"];
1165 ["pvcreate"; "/dev/sda3"];
1166 ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"];
1167 ["vgcreate"; "VG2"; "/dev/sda3"];
1168 ["vgs"]], ["VG1"; "VG2"])],
1169 "create an LVM volume group",
1171 This creates an LVM volume group called C<volgroup>
1172 from the non-empty list of physical volumes C<physvols>.");
1174 ("lvcreate", (RErr, [String "logvol"; String "volgroup"; Int "mbytes"]), 41, [],
1175 [InitEmpty, Always, TestOutputList (
1176 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",200 ,400 ,"];
1177 ["pvcreate"; "/dev/sda1"];
1178 ["pvcreate"; "/dev/sda2"];
1179 ["pvcreate"; "/dev/sda3"];
1180 ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"];
1181 ["vgcreate"; "VG2"; "/dev/sda3"];
1182 ["lvcreate"; "LV1"; "VG1"; "50"];
1183 ["lvcreate"; "LV2"; "VG1"; "50"];
1184 ["lvcreate"; "LV3"; "VG2"; "50"];
1185 ["lvcreate"; "LV4"; "VG2"; "50"];
1186 ["lvcreate"; "LV5"; "VG2"; "50"];
1188 ["/dev/VG1/LV1"; "/dev/VG1/LV2";
1189 "/dev/VG2/LV3"; "/dev/VG2/LV4"; "/dev/VG2/LV5"])],
1190 "create an LVM volume group",
1192 This creates an LVM volume group called C<logvol>
1193 on the volume group C<volgroup>, with C<size> megabytes.");
1195 ("mkfs", (RErr, [String "fstype"; String "device"]), 42, [],
1196 [InitEmpty, Always, TestOutput (
1197 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1198 ["mkfs"; "ext2"; "/dev/sda1"];
1199 ["mount"; "/dev/sda1"; "/"];
1200 ["write_file"; "/new"; "new file contents"; "0"];
1201 ["cat"; "/new"]], "new file contents")],
1202 "make a filesystem",
1204 This creates a filesystem on C<device> (usually a partition
1205 or LVM logical volume). The filesystem type is C<fstype>, for
1208 ("sfdisk", (RErr, [String "device";
1209 Int "cyls"; Int "heads"; Int "sectors";
1210 StringList "lines"]), 43, [DangerWillRobinson],
1212 "create partitions on a block device",
1214 This is a direct interface to the L<sfdisk(8)> program for creating
1215 partitions on block devices.
1217 C<device> should be a block device, for example C</dev/sda>.
1219 C<cyls>, C<heads> and C<sectors> are the number of cylinders, heads
1220 and sectors on the device, which are passed directly to sfdisk as
1221 the I<-C>, I<-H> and I<-S> parameters. If you pass C<0> for any
1222 of these, then the corresponding parameter is omitted. Usually for
1223 'large' disks, you can just pass C<0> for these, but for small
1224 (floppy-sized) disks, sfdisk (or rather, the kernel) cannot work
1225 out the right geometry and you will need to tell it.
1227 C<lines> is a list of lines that we feed to C<sfdisk>. For more
1228 information refer to the L<sfdisk(8)> manpage.
1230 To create a single partition occupying the whole disk, you would
1231 pass C<lines> as a single element list, when the single element being
1232 the string C<,> (comma).
1234 See also: C<guestfs_sfdisk_l>, C<guestfs_sfdisk_N>");
1236 ("write_file", (RErr, [String "path"; String "content"; Int "size"]), 44, [ProtocolLimitWarning],
1237 [InitBasicFS, Always, TestOutput (
1238 [["write_file"; "/new"; "new file contents"; "0"];
1239 ["cat"; "/new"]], "new file contents");
1240 InitBasicFS, Always, TestOutput (
1241 [["write_file"; "/new"; "\nnew file contents\n"; "0"];
1242 ["cat"; "/new"]], "\nnew file contents\n");
1243 InitBasicFS, Always, TestOutput (
1244 [["write_file"; "/new"; "\n\n"; "0"];
1245 ["cat"; "/new"]], "\n\n");
1246 InitBasicFS, Always, TestOutput (
1247 [["write_file"; "/new"; ""; "0"];
1248 ["cat"; "/new"]], "");
1249 InitBasicFS, Always, TestOutput (
1250 [["write_file"; "/new"; "\n\n\n"; "0"];
1251 ["cat"; "/new"]], "\n\n\n");
1252 InitBasicFS, Always, TestOutput (
1253 [["write_file"; "/new"; "\n"; "0"];
1254 ["cat"; "/new"]], "\n")],
1257 This call creates a file called C<path>. The contents of the
1258 file is the string C<content> (which can contain any 8 bit data),
1259 with length C<size>.
1261 As a special case, if C<size> is C<0>
1262 then the length is calculated using C<strlen> (so in this case
1263 the content cannot contain embedded ASCII NULs).
1265 I<NB.> Owing to a bug, writing content containing ASCII NUL
1266 characters does I<not> work, even if the length is specified.
1267 We hope to resolve this bug in a future version. In the meantime
1268 use C<guestfs_upload>.");
1270 ("umount", (RErr, [String "pathordevice"]), 45, [FishAlias "unmount"],
1271 [InitEmpty, Always, TestOutputListOfDevices (
1272 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1273 ["mkfs"; "ext2"; "/dev/sda1"];
1274 ["mount"; "/dev/sda1"; "/"];
1275 ["mounts"]], ["/dev/sda1"]);
1276 InitEmpty, Always, TestOutputList (
1277 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1278 ["mkfs"; "ext2"; "/dev/sda1"];
1279 ["mount"; "/dev/sda1"; "/"];
1282 "unmount a filesystem",
1284 This unmounts the given filesystem. The filesystem may be
1285 specified either by its mountpoint (path) or the device which
1286 contains the filesystem.");
1288 ("mounts", (RStringList "devices", []), 46, [],
1289 [InitBasicFS, Always, TestOutputListOfDevices (
1290 [["mounts"]], ["/dev/sda1"])],
1291 "show mounted filesystems",
1293 This returns the list of currently mounted filesystems. It returns
1294 the list of devices (eg. C</dev/sda1>, C</dev/VG/LV>).
1296 Some internal mounts are not shown.");
1298 ("umount_all", (RErr, []), 47, [FishAlias "unmount-all"],
1299 [InitBasicFS, Always, TestOutputList (
1302 (* check that umount_all can unmount nested mounts correctly: *)
1303 InitEmpty, Always, TestOutputList (
1304 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",200 ,400 ,"];
1305 ["mkfs"; "ext2"; "/dev/sda1"];
1306 ["mkfs"; "ext2"; "/dev/sda2"];
1307 ["mkfs"; "ext2"; "/dev/sda3"];
1308 ["mount"; "/dev/sda1"; "/"];
1310 ["mount"; "/dev/sda2"; "/mp1"];
1311 ["mkdir"; "/mp1/mp2"];
1312 ["mount"; "/dev/sda3"; "/mp1/mp2"];
1313 ["mkdir"; "/mp1/mp2/mp3"];
1316 "unmount all filesystems",
1318 This unmounts all mounted filesystems.
1320 Some internal mounts are not unmounted by this call.");
1322 ("lvm_remove_all", (RErr, []), 48, [DangerWillRobinson],
1324 "remove all LVM LVs, VGs and PVs",
1326 This command removes all LVM logical volumes, volume groups
1327 and physical volumes.");
1329 ("file", (RString "description", [String "path"]), 49, [],
1330 [InitBasicFS, Always, TestOutput (
1332 ["file"; "/new"]], "empty");
1333 InitBasicFS, Always, TestOutput (
1334 [["write_file"; "/new"; "some content\n"; "0"];
1335 ["file"; "/new"]], "ASCII text");
1336 InitBasicFS, Always, TestLastFail (
1337 [["file"; "/nofile"]])],
1338 "determine file type",
1340 This call uses the standard L<file(1)> command to determine
1341 the type or contents of the file. This also works on devices,
1342 for example to find out whether a partition contains a filesystem.
1344 The exact command which runs is C<file -bsL path>. Note in
1345 particular that the filename is not prepended to the output
1346 (the C<-b> option).");
1348 ("command", (RString "output", [StringList "arguments"]), 50, [ProtocolLimitWarning],
1349 [InitBasicFS, Always, TestOutput (
1350 [["upload"; "test-command"; "/test-command"];
1351 ["chmod"; "0o755"; "/test-command"];
1352 ["command"; "/test-command 1"]], "Result1");
1353 InitBasicFS, Always, TestOutput (
1354 [["upload"; "test-command"; "/test-command"];
1355 ["chmod"; "0o755"; "/test-command"];
1356 ["command"; "/test-command 2"]], "Result2\n");
1357 InitBasicFS, Always, TestOutput (
1358 [["upload"; "test-command"; "/test-command"];
1359 ["chmod"; "0o755"; "/test-command"];
1360 ["command"; "/test-command 3"]], "\nResult3");
1361 InitBasicFS, Always, TestOutput (
1362 [["upload"; "test-command"; "/test-command"];
1363 ["chmod"; "0o755"; "/test-command"];
1364 ["command"; "/test-command 4"]], "\nResult4\n");
1365 InitBasicFS, Always, TestOutput (
1366 [["upload"; "test-command"; "/test-command"];
1367 ["chmod"; "0o755"; "/test-command"];
1368 ["command"; "/test-command 5"]], "\nResult5\n\n");
1369 InitBasicFS, Always, TestOutput (
1370 [["upload"; "test-command"; "/test-command"];
1371 ["chmod"; "0o755"; "/test-command"];
1372 ["command"; "/test-command 6"]], "\n\nResult6\n\n");
1373 InitBasicFS, Always, TestOutput (
1374 [["upload"; "test-command"; "/test-command"];
1375 ["chmod"; "0o755"; "/test-command"];
1376 ["command"; "/test-command 7"]], "");
1377 InitBasicFS, Always, TestOutput (
1378 [["upload"; "test-command"; "/test-command"];
1379 ["chmod"; "0o755"; "/test-command"];
1380 ["command"; "/test-command 8"]], "\n");
1381 InitBasicFS, Always, TestOutput (
1382 [["upload"; "test-command"; "/test-command"];
1383 ["chmod"; "0o755"; "/test-command"];
1384 ["command"; "/test-command 9"]], "\n\n");
1385 InitBasicFS, Always, TestOutput (
1386 [["upload"; "test-command"; "/test-command"];
1387 ["chmod"; "0o755"; "/test-command"];
1388 ["command"; "/test-command 10"]], "Result10-1\nResult10-2\n");
1389 InitBasicFS, Always, TestOutput (
1390 [["upload"; "test-command"; "/test-command"];
1391 ["chmod"; "0o755"; "/test-command"];
1392 ["command"; "/test-command 11"]], "Result11-1\nResult11-2");
1393 InitBasicFS, Always, TestLastFail (
1394 [["upload"; "test-command"; "/test-command"];
1395 ["chmod"; "0o755"; "/test-command"];
1396 ["command"; "/test-command"]])],
1397 "run a command from the guest filesystem",
1399 This call runs a command from the guest filesystem. The
1400 filesystem must be mounted, and must contain a compatible
1401 operating system (ie. something Linux, with the same
1402 or compatible processor architecture).
1404 The single parameter is an argv-style list of arguments.
1405 The first element is the name of the program to run.
1406 Subsequent elements are parameters. The list must be
1407 non-empty (ie. must contain a program name). Note that
1408 the command runs directly, and is I<not> invoked via
1409 the shell (see C<guestfs_sh>).
1411 The return value is anything printed to I<stdout> by
1414 If the command returns a non-zero exit status, then
1415 this function returns an error message. The error message
1416 string is the content of I<stderr> from the command.
1418 The C<$PATH> environment variable will contain at least
1419 C</usr/bin> and C</bin>. If you require a program from
1420 another location, you should provide the full path in the
1423 Shared libraries and data files required by the program
1424 must be available on filesystems which are mounted in the
1425 correct places. It is the caller's responsibility to ensure
1426 all filesystems that are needed are mounted at the right
1429 ("command_lines", (RStringList "lines", [StringList "arguments"]), 51, [ProtocolLimitWarning],
1430 [InitBasicFS, Always, TestOutputList (
1431 [["upload"; "test-command"; "/test-command"];
1432 ["chmod"; "0o755"; "/test-command"];
1433 ["command_lines"; "/test-command 1"]], ["Result1"]);
1434 InitBasicFS, Always, TestOutputList (
1435 [["upload"; "test-command"; "/test-command"];
1436 ["chmod"; "0o755"; "/test-command"];
1437 ["command_lines"; "/test-command 2"]], ["Result2"]);
1438 InitBasicFS, Always, TestOutputList (
1439 [["upload"; "test-command"; "/test-command"];
1440 ["chmod"; "0o755"; "/test-command"];
1441 ["command_lines"; "/test-command 3"]], ["";"Result3"]);
1442 InitBasicFS, Always, TestOutputList (
1443 [["upload"; "test-command"; "/test-command"];
1444 ["chmod"; "0o755"; "/test-command"];
1445 ["command_lines"; "/test-command 4"]], ["";"Result4"]);
1446 InitBasicFS, Always, TestOutputList (
1447 [["upload"; "test-command"; "/test-command"];
1448 ["chmod"; "0o755"; "/test-command"];
1449 ["command_lines"; "/test-command 5"]], ["";"Result5";""]);
1450 InitBasicFS, Always, TestOutputList (
1451 [["upload"; "test-command"; "/test-command"];
1452 ["chmod"; "0o755"; "/test-command"];
1453 ["command_lines"; "/test-command 6"]], ["";"";"Result6";""]);
1454 InitBasicFS, Always, TestOutputList (
1455 [["upload"; "test-command"; "/test-command"];
1456 ["chmod"; "0o755"; "/test-command"];
1457 ["command_lines"; "/test-command 7"]], []);
1458 InitBasicFS, Always, TestOutputList (
1459 [["upload"; "test-command"; "/test-command"];
1460 ["chmod"; "0o755"; "/test-command"];
1461 ["command_lines"; "/test-command 8"]], [""]);
1462 InitBasicFS, Always, TestOutputList (
1463 [["upload"; "test-command"; "/test-command"];
1464 ["chmod"; "0o755"; "/test-command"];
1465 ["command_lines"; "/test-command 9"]], ["";""]);
1466 InitBasicFS, Always, TestOutputList (
1467 [["upload"; "test-command"; "/test-command"];
1468 ["chmod"; "0o755"; "/test-command"];
1469 ["command_lines"; "/test-command 10"]], ["Result10-1";"Result10-2"]);
1470 InitBasicFS, Always, TestOutputList (
1471 [["upload"; "test-command"; "/test-command"];
1472 ["chmod"; "0o755"; "/test-command"];
1473 ["command_lines"; "/test-command 11"]], ["Result11-1";"Result11-2"])],
1474 "run a command, returning lines",
1476 This is the same as C<guestfs_command>, but splits the
1477 result into a list of lines.
1479 See also: C<guestfs_sh_lines>");
1481 ("stat", (RStat "statbuf", [String "path"]), 52, [],
1482 [InitBasicFS, Always, TestOutputStruct (
1484 ["stat"; "/new"]], [CompareWithInt ("size", 0)])],
1485 "get file information",
1487 Returns file information for the given C<path>.
1489 This is the same as the C<stat(2)> system call.");
1491 ("lstat", (RStat "statbuf", [String "path"]), 53, [],
1492 [InitBasicFS, Always, TestOutputStruct (
1494 ["lstat"; "/new"]], [CompareWithInt ("size", 0)])],
1495 "get file information for a symbolic link",
1497 Returns file information for the given C<path>.
1499 This is the same as C<guestfs_stat> except that if C<path>
1500 is a symbolic link, then the link is stat-ed, not the file it
1503 This is the same as the C<lstat(2)> system call.");
1505 ("statvfs", (RStatVFS "statbuf", [String "path"]), 54, [],
1506 [InitBasicFS, Always, TestOutputStruct (
1507 [["statvfs"; "/"]], [CompareWithInt ("namemax", 255);
1508 CompareWithInt ("bsize", 1024)])],
1509 "get file system statistics",
1511 Returns file system statistics for any mounted file system.
1512 C<path> should be a file or directory in the mounted file system
1513 (typically it is the mount point itself, but it doesn't need to be).
1515 This is the same as the C<statvfs(2)> system call.");
1517 ("tune2fs_l", (RHashtable "superblock", [String "device"]), 55, [],
1519 "get ext2/ext3/ext4 superblock details",
1521 This returns the contents of the ext2, ext3 or ext4 filesystem
1522 superblock on C<device>.
1524 It is the same as running C<tune2fs -l device>. See L<tune2fs(8)>
1525 manpage for more details. The list of fields returned isn't
1526 clearly defined, and depends on both the version of C<tune2fs>
1527 that libguestfs was built against, and the filesystem itself.");
1529 ("blockdev_setro", (RErr, [String "device"]), 56, [],
1530 [InitEmpty, Always, TestOutputTrue (
1531 [["blockdev_setro"; "/dev/sda"];
1532 ["blockdev_getro"; "/dev/sda"]])],
1533 "set block device to read-only",
1535 Sets the block device named C<device> to read-only.
1537 This uses the L<blockdev(8)> command.");
1539 ("blockdev_setrw", (RErr, [String "device"]), 57, [],
1540 [InitEmpty, Always, TestOutputFalse (
1541 [["blockdev_setrw"; "/dev/sda"];
1542 ["blockdev_getro"; "/dev/sda"]])],
1543 "set block device to read-write",
1545 Sets the block device named C<device> to read-write.
1547 This uses the L<blockdev(8)> command.");
1549 ("blockdev_getro", (RBool "ro", [String "device"]), 58, [],
1550 [InitEmpty, Always, TestOutputTrue (
1551 [["blockdev_setro"; "/dev/sda"];
1552 ["blockdev_getro"; "/dev/sda"]])],
1553 "is block device set to read-only",
1555 Returns a boolean indicating if the block device is read-only
1556 (true if read-only, false if not).
1558 This uses the L<blockdev(8)> command.");
1560 ("blockdev_getss", (RInt "sectorsize", [String "device"]), 59, [],
1561 [InitEmpty, Always, TestOutputInt (
1562 [["blockdev_getss"; "/dev/sda"]], 512)],
1563 "get sectorsize of block device",
1565 This returns the size of sectors on a block device.
1566 Usually 512, but can be larger for modern devices.
1568 (Note, this is not the size in sectors, use C<guestfs_blockdev_getsz>
1571 This uses the L<blockdev(8)> command.");
1573 ("blockdev_getbsz", (RInt "blocksize", [String "device"]), 60, [],
1574 [InitEmpty, Always, TestOutputInt (
1575 [["blockdev_getbsz"; "/dev/sda"]], 4096)],
1576 "get blocksize of block device",
1578 This returns the block size of a device.
1580 (Note this is different from both I<size in blocks> and
1581 I<filesystem block size>).
1583 This uses the L<blockdev(8)> command.");
1585 ("blockdev_setbsz", (RErr, [String "device"; Int "blocksize"]), 61, [],
1587 "set blocksize of block device",
1589 This sets the block size of a device.
1591 (Note this is different from both I<size in blocks> and
1592 I<filesystem block size>).
1594 This uses the L<blockdev(8)> command.");
1596 ("blockdev_getsz", (RInt64 "sizeinsectors", [String "device"]), 62, [],
1597 [InitEmpty, Always, TestOutputInt (
1598 [["blockdev_getsz"; "/dev/sda"]], 1024000)],
1599 "get total size of device in 512-byte sectors",
1601 This returns the size of the device in units of 512-byte sectors
1602 (even if the sectorsize isn't 512 bytes ... weird).
1604 See also C<guestfs_blockdev_getss> for the real sector size of
1605 the device, and C<guestfs_blockdev_getsize64> for the more
1606 useful I<size in bytes>.
1608 This uses the L<blockdev(8)> command.");
1610 ("blockdev_getsize64", (RInt64 "sizeinbytes", [String "device"]), 63, [],
1611 [InitEmpty, Always, TestOutputInt (
1612 [["blockdev_getsize64"; "/dev/sda"]], 524288000)],
1613 "get total size of device in bytes",
1615 This returns the size of the device in bytes.
1617 See also C<guestfs_blockdev_getsz>.
1619 This uses the L<blockdev(8)> command.");
1621 ("blockdev_flushbufs", (RErr, [String "device"]), 64, [],
1622 [InitEmpty, Always, TestRun
1623 [["blockdev_flushbufs"; "/dev/sda"]]],
1624 "flush device buffers",
1626 This tells the kernel to flush internal buffers associated
1629 This uses the L<blockdev(8)> command.");
1631 ("blockdev_rereadpt", (RErr, [String "device"]), 65, [],
1632 [InitEmpty, Always, TestRun
1633 [["blockdev_rereadpt"; "/dev/sda"]]],
1634 "reread partition table",
1636 Reread the partition table on C<device>.
1638 This uses the L<blockdev(8)> command.");
1640 ("upload", (RErr, [FileIn "filename"; String "remotefilename"]), 66, [],
1641 [InitBasicFS, Always, TestOutput (
1642 (* Pick a file from cwd which isn't likely to change. *)
1643 [["upload"; "../COPYING.LIB"; "/COPYING.LIB"];
1644 ["checksum"; "md5"; "/COPYING.LIB"]], "e3eda01d9815f8d24aae2dbd89b68b06")],
1645 "upload a file from the local machine",
1647 Upload local file C<filename> to C<remotefilename> on the
1650 C<filename> can also be a named pipe.
1652 See also C<guestfs_download>.");
1654 ("download", (RErr, [String "remotefilename"; FileOut "filename"]), 67, [],
1655 [InitBasicFS, Always, TestOutput (
1656 (* Pick a file from cwd which isn't likely to change. *)
1657 [["upload"; "../COPYING.LIB"; "/COPYING.LIB"];
1658 ["download"; "/COPYING.LIB"; "testdownload.tmp"];
1659 ["upload"; "testdownload.tmp"; "/upload"];
1660 ["checksum"; "md5"; "/upload"]], "e3eda01d9815f8d24aae2dbd89b68b06")],
1661 "download a file to the local machine",
1663 Download file C<remotefilename> and save it as C<filename>
1664 on the local machine.
1666 C<filename> can also be a named pipe.
1668 See also C<guestfs_upload>, C<guestfs_cat>.");
1670 ("checksum", (RString "checksum", [String "csumtype"; String "path"]), 68, [],
1671 [InitBasicFS, Always, TestOutput (
1672 [["write_file"; "/new"; "test\n"; "0"];
1673 ["checksum"; "crc"; "/new"]], "935282863");
1674 InitBasicFS, Always, TestLastFail (
1675 [["checksum"; "crc"; "/new"]]);
1676 InitBasicFS, Always, TestOutput (
1677 [["write_file"; "/new"; "test\n"; "0"];
1678 ["checksum"; "md5"; "/new"]], "d8e8fca2dc0f896fd7cb4cb0031ba249");
1679 InitBasicFS, Always, TestOutput (
1680 [["write_file"; "/new"; "test\n"; "0"];
1681 ["checksum"; "sha1"; "/new"]], "4e1243bd22c66e76c2ba9eddc1f91394e57f9f83");
1682 InitBasicFS, Always, TestOutput (
1683 [["write_file"; "/new"; "test\n"; "0"];
1684 ["checksum"; "sha224"; "/new"]], "52f1bf093f4b7588726035c176c0cdb4376cfea53819f1395ac9e6ec");
1685 InitBasicFS, Always, TestOutput (
1686 [["write_file"; "/new"; "test\n"; "0"];
1687 ["checksum"; "sha256"; "/new"]], "f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2");
1688 InitBasicFS, Always, TestOutput (
1689 [["write_file"; "/new"; "test\n"; "0"];
1690 ["checksum"; "sha384"; "/new"]], "109bb6b5b6d5547c1ce03c7a8bd7d8f80c1cb0957f50c4f7fda04692079917e4f9cad52b878f3d8234e1a170b154b72d");
1691 InitBasicFS, Always, TestOutput (
1692 [["write_file"; "/new"; "test\n"; "0"];
1693 ["checksum"; "sha512"; "/new"]], "0e3e75234abc68f4378a86b3f4b32a198ba301845b0cd6e50106e874345700cc6663a86c1ea125dc5e92be17c98f9a0f85ca9d5f595db2012f7cc3571945c123");
1694 InitBasicFS, Always, TestOutput (
1695 (* RHEL 5 thinks this is an HFS+ filesystem unless we give
1696 * the type explicitly.
1698 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
1699 ["checksum"; "md5"; "/known-3"]], "46d6ca27ee07cdc6fa99c2e138cc522c")],
1700 "compute MD5, SHAx or CRC checksum of file",
1702 This call computes the MD5, SHAx or CRC checksum of the
1705 The type of checksum to compute is given by the C<csumtype>
1706 parameter which must have one of the following values:
1712 Compute the cyclic redundancy check (CRC) specified by POSIX
1713 for the C<cksum> command.
1717 Compute the MD5 hash (using the C<md5sum> program).
1721 Compute the SHA1 hash (using the C<sha1sum> program).
1725 Compute the SHA224 hash (using the C<sha224sum> program).
1729 Compute the SHA256 hash (using the C<sha256sum> program).
1733 Compute the SHA384 hash (using the C<sha384sum> program).
1737 Compute the SHA512 hash (using the C<sha512sum> program).
1741 The checksum is returned as a printable string.");
1743 ("tar_in", (RErr, [FileIn "tarfile"; String "directory"]), 69, [],
1744 [InitBasicFS, Always, TestOutput (
1745 [["tar_in"; "../images/helloworld.tar"; "/"];
1746 ["cat"; "/hello"]], "hello\n")],
1747 "unpack tarfile to directory",
1749 This command uploads and unpacks local file C<tarfile> (an
1750 I<uncompressed> tar file) into C<directory>.
1752 To upload a compressed tarball, use C<guestfs_tgz_in>.");
1754 ("tar_out", (RErr, [String "directory"; FileOut "tarfile"]), 70, [],
1756 "pack directory into tarfile",
1758 This command packs the contents of C<directory> and downloads
1759 it to local file C<tarfile>.
1761 To download a compressed tarball, use C<guestfs_tgz_out>.");
1763 ("tgz_in", (RErr, [FileIn "tarball"; String "directory"]), 71, [],
1764 [InitBasicFS, Always, TestOutput (
1765 [["tgz_in"; "../images/helloworld.tar.gz"; "/"];
1766 ["cat"; "/hello"]], "hello\n")],
1767 "unpack compressed tarball to directory",
1769 This command uploads and unpacks local file C<tarball> (a
1770 I<gzip compressed> tar file) into C<directory>.
1772 To upload an uncompressed tarball, use C<guestfs_tar_in>.");
1774 ("tgz_out", (RErr, [String "directory"; FileOut "tarball"]), 72, [],
1776 "pack directory into compressed tarball",
1778 This command packs the contents of C<directory> and downloads
1779 it to local file C<tarball>.
1781 To download an uncompressed tarball, use C<guestfs_tar_out>.");
1783 ("mount_ro", (RErr, [String "device"; String "mountpoint"]), 73, [],
1784 [InitBasicFS, Always, TestLastFail (
1786 ["mount_ro"; "/dev/sda1"; "/"];
1787 ["touch"; "/new"]]);
1788 InitBasicFS, Always, TestOutput (
1789 [["write_file"; "/new"; "data"; "0"];
1791 ["mount_ro"; "/dev/sda1"; "/"];
1792 ["cat"; "/new"]], "data")],
1793 "mount a guest disk, read-only",
1795 This is the same as the C<guestfs_mount> command, but it
1796 mounts the filesystem with the read-only (I<-o ro>) flag.");
1798 ("mount_options", (RErr, [String "options"; String "device"; String "mountpoint"]), 74, [],
1800 "mount a guest disk with mount options",
1802 This is the same as the C<guestfs_mount> command, but it
1803 allows you to set the mount options as for the
1804 L<mount(8)> I<-o> flag.");
1806 ("mount_vfs", (RErr, [String "options"; String "vfstype"; String "device"; String "mountpoint"]), 75, [],
1808 "mount a guest disk with mount options and vfstype",
1810 This is the same as the C<guestfs_mount> command, but it
1811 allows you to set both the mount options and the vfstype
1812 as for the L<mount(8)> I<-o> and I<-t> flags.");
1814 ("debug", (RString "result", [String "subcmd"; StringList "extraargs"]), 76, [],
1816 "debugging and internals",
1818 The C<guestfs_debug> command exposes some internals of
1819 C<guestfsd> (the guestfs daemon) that runs inside the
1822 There is no comprehensive help for this command. You have
1823 to look at the file C<daemon/debug.c> in the libguestfs source
1824 to find out what you can do.");
1826 ("lvremove", (RErr, [String "device"]), 77, [],
1827 [InitEmpty, Always, TestOutputList (
1828 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1829 ["pvcreate"; "/dev/sda1"];
1830 ["vgcreate"; "VG"; "/dev/sda1"];
1831 ["lvcreate"; "LV1"; "VG"; "50"];
1832 ["lvcreate"; "LV2"; "VG"; "50"];
1833 ["lvremove"; "/dev/VG/LV1"];
1834 ["lvs"]], ["/dev/VG/LV2"]);
1835 InitEmpty, Always, TestOutputList (
1836 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1837 ["pvcreate"; "/dev/sda1"];
1838 ["vgcreate"; "VG"; "/dev/sda1"];
1839 ["lvcreate"; "LV1"; "VG"; "50"];
1840 ["lvcreate"; "LV2"; "VG"; "50"];
1841 ["lvremove"; "/dev/VG"];
1843 InitEmpty, Always, TestOutputList (
1844 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1845 ["pvcreate"; "/dev/sda1"];
1846 ["vgcreate"; "VG"; "/dev/sda1"];
1847 ["lvcreate"; "LV1"; "VG"; "50"];
1848 ["lvcreate"; "LV2"; "VG"; "50"];
1849 ["lvremove"; "/dev/VG"];
1851 "remove an LVM logical volume",
1853 Remove an LVM logical volume C<device>, where C<device> is
1854 the path to the LV, such as C</dev/VG/LV>.
1856 You can also remove all LVs in a volume group by specifying
1857 the VG name, C</dev/VG>.");
1859 ("vgremove", (RErr, [String "vgname"]), 78, [],
1860 [InitEmpty, Always, TestOutputList (
1861 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1862 ["pvcreate"; "/dev/sda1"];
1863 ["vgcreate"; "VG"; "/dev/sda1"];
1864 ["lvcreate"; "LV1"; "VG"; "50"];
1865 ["lvcreate"; "LV2"; "VG"; "50"];
1868 InitEmpty, Always, TestOutputList (
1869 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1870 ["pvcreate"; "/dev/sda1"];
1871 ["vgcreate"; "VG"; "/dev/sda1"];
1872 ["lvcreate"; "LV1"; "VG"; "50"];
1873 ["lvcreate"; "LV2"; "VG"; "50"];
1876 "remove an LVM volume group",
1878 Remove an LVM volume group C<vgname>, (for example C<VG>).
1880 This also forcibly removes all logical volumes in the volume
1883 ("pvremove", (RErr, [String "device"]), 79, [],
1884 [InitEmpty, Always, TestOutputListOfDevices (
1885 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1886 ["pvcreate"; "/dev/sda1"];
1887 ["vgcreate"; "VG"; "/dev/sda1"];
1888 ["lvcreate"; "LV1"; "VG"; "50"];
1889 ["lvcreate"; "LV2"; "VG"; "50"];
1891 ["pvremove"; "/dev/sda1"];
1893 InitEmpty, Always, TestOutputListOfDevices (
1894 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1895 ["pvcreate"; "/dev/sda1"];
1896 ["vgcreate"; "VG"; "/dev/sda1"];
1897 ["lvcreate"; "LV1"; "VG"; "50"];
1898 ["lvcreate"; "LV2"; "VG"; "50"];
1900 ["pvremove"; "/dev/sda1"];
1902 InitEmpty, Always, TestOutputListOfDevices (
1903 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1904 ["pvcreate"; "/dev/sda1"];
1905 ["vgcreate"; "VG"; "/dev/sda1"];
1906 ["lvcreate"; "LV1"; "VG"; "50"];
1907 ["lvcreate"; "LV2"; "VG"; "50"];
1909 ["pvremove"; "/dev/sda1"];
1911 "remove an LVM physical volume",
1913 This wipes a physical volume C<device> so that LVM will no longer
1916 The implementation uses the C<pvremove> command which refuses to
1917 wipe physical volumes that contain any volume groups, so you have
1918 to remove those first.");
1920 ("set_e2label", (RErr, [String "device"; String "label"]), 80, [],
1921 [InitBasicFS, Always, TestOutput (
1922 [["set_e2label"; "/dev/sda1"; "testlabel"];
1923 ["get_e2label"; "/dev/sda1"]], "testlabel")],
1924 "set the ext2/3/4 filesystem label",
1926 This sets the ext2/3/4 filesystem label of the filesystem on
1927 C<device> to C<label>. Filesystem labels are limited to
1930 You can use either C<guestfs_tune2fs_l> or C<guestfs_get_e2label>
1931 to return the existing label on a filesystem.");
1933 ("get_e2label", (RString "label", [String "device"]), 81, [],
1935 "get the ext2/3/4 filesystem label",
1937 This returns the ext2/3/4 filesystem label of the filesystem on
1940 ("set_e2uuid", (RErr, [String "device"; String "uuid"]), 82, [],
1941 [InitBasicFS, Always, TestOutput (
1942 [["set_e2uuid"; "/dev/sda1"; "a3a61220-882b-4f61-89f4-cf24dcc7297d"];
1943 ["get_e2uuid"; "/dev/sda1"]], "a3a61220-882b-4f61-89f4-cf24dcc7297d");
1944 InitBasicFS, Always, TestOutput (
1945 [["set_e2uuid"; "/dev/sda1"; "clear"];
1946 ["get_e2uuid"; "/dev/sda1"]], "");
1947 (* We can't predict what UUIDs will be, so just check the commands run. *)
1948 InitBasicFS, Always, TestRun (
1949 [["set_e2uuid"; "/dev/sda1"; "random"]]);
1950 InitBasicFS, Always, TestRun (
1951 [["set_e2uuid"; "/dev/sda1"; "time"]])],
1952 "set the ext2/3/4 filesystem UUID",
1954 This sets the ext2/3/4 filesystem UUID of the filesystem on
1955 C<device> to C<uuid>. The format of the UUID and alternatives
1956 such as C<clear>, C<random> and C<time> are described in the
1957 L<tune2fs(8)> manpage.
1959 You can use either C<guestfs_tune2fs_l> or C<guestfs_get_e2uuid>
1960 to return the existing UUID of a filesystem.");
1962 ("get_e2uuid", (RString "uuid", [String "device"]), 83, [],
1964 "get the ext2/3/4 filesystem UUID",
1966 This returns the ext2/3/4 filesystem UUID of the filesystem on
1969 ("fsck", (RInt "status", [String "fstype"; String "device"]), 84, [],
1970 [InitBasicFS, Always, TestOutputInt (
1971 [["umount"; "/dev/sda1"];
1972 ["fsck"; "ext2"; "/dev/sda1"]], 0);
1973 InitBasicFS, Always, TestOutputInt (
1974 [["umount"; "/dev/sda1"];
1975 ["zero"; "/dev/sda1"];
1976 ["fsck"; "ext2"; "/dev/sda1"]], 8)],
1977 "run the filesystem checker",
1979 This runs the filesystem checker (fsck) on C<device> which
1980 should have filesystem type C<fstype>.
1982 The returned integer is the status. See L<fsck(8)> for the
1983 list of status codes from C<fsck>.
1991 Multiple status codes can be summed together.
1995 A non-zero return code can mean \"success\", for example if
1996 errors have been corrected on the filesystem.
2000 Checking or repairing NTFS volumes is not supported
2005 This command is entirely equivalent to running C<fsck -a -t fstype device>.");
2007 ("zero", (RErr, [String "device"]), 85, [],
2008 [InitBasicFS, Always, TestOutput (
2009 [["umount"; "/dev/sda1"];
2010 ["zero"; "/dev/sda1"];
2011 ["file"; "/dev/sda1"]], "data")],
2012 "write zeroes to the device",
2014 This command writes zeroes over the first few blocks of C<device>.
2016 How many blocks are zeroed isn't specified (but it's I<not> enough
2017 to securely wipe the device). It should be sufficient to remove
2018 any partition tables, filesystem superblocks and so on.
2020 See also: C<guestfs_scrub_device>.");
2022 ("grub_install", (RErr, [String "root"; String "device"]), 86, [],
2023 (* Test disabled because grub-install incompatible with virtio-blk driver.
2024 * See also: https://bugzilla.redhat.com/show_bug.cgi?id=479760
2026 [InitBasicFS, Disabled, TestOutputTrue (
2027 [["grub_install"; "/"; "/dev/sda1"];
2028 ["is_dir"; "/boot"]])],
2031 This command installs GRUB (the Grand Unified Bootloader) on
2032 C<device>, with the root directory being C<root>.");
2034 ("cp", (RErr, [String "src"; String "dest"]), 87, [],
2035 [InitBasicFS, Always, TestOutput (
2036 [["write_file"; "/old"; "file content"; "0"];
2037 ["cp"; "/old"; "/new"];
2038 ["cat"; "/new"]], "file content");
2039 InitBasicFS, Always, TestOutputTrue (
2040 [["write_file"; "/old"; "file content"; "0"];
2041 ["cp"; "/old"; "/new"];
2042 ["is_file"; "/old"]]);
2043 InitBasicFS, Always, TestOutput (
2044 [["write_file"; "/old"; "file content"; "0"];
2046 ["cp"; "/old"; "/dir/new"];
2047 ["cat"; "/dir/new"]], "file content")],
2050 This copies a file from C<src> to C<dest> where C<dest> is
2051 either a destination filename or destination directory.");
2053 ("cp_a", (RErr, [String "src"; String "dest"]), 88, [],
2054 [InitBasicFS, Always, TestOutput (
2055 [["mkdir"; "/olddir"];
2056 ["mkdir"; "/newdir"];
2057 ["write_file"; "/olddir/file"; "file content"; "0"];
2058 ["cp_a"; "/olddir"; "/newdir"];
2059 ["cat"; "/newdir/olddir/file"]], "file content")],
2060 "copy a file or directory recursively",
2062 This copies a file or directory from C<src> to C<dest>
2063 recursively using the C<cp -a> command.");
2065 ("mv", (RErr, [String "src"; String "dest"]), 89, [],
2066 [InitBasicFS, Always, TestOutput (
2067 [["write_file"; "/old"; "file content"; "0"];
2068 ["mv"; "/old"; "/new"];
2069 ["cat"; "/new"]], "file content");
2070 InitBasicFS, Always, TestOutputFalse (
2071 [["write_file"; "/old"; "file content"; "0"];
2072 ["mv"; "/old"; "/new"];
2073 ["is_file"; "/old"]])],
2076 This moves a file from C<src> to C<dest> where C<dest> is
2077 either a destination filename or destination directory.");
2079 ("drop_caches", (RErr, [Int "whattodrop"]), 90, [],
2080 [InitEmpty, Always, TestRun (
2081 [["drop_caches"; "3"]])],
2082 "drop kernel page cache, dentries and inodes",
2084 This instructs the guest kernel to drop its page cache,
2085 and/or dentries and inode caches. The parameter C<whattodrop>
2086 tells the kernel what precisely to drop, see
2087 L<http://linux-mm.org/Drop_Caches>
2089 Setting C<whattodrop> to 3 should drop everything.
2091 This automatically calls L<sync(2)> before the operation,
2092 so that the maximum guest memory is freed.");
2094 ("dmesg", (RString "kmsgs", []), 91, [],
2095 [InitEmpty, Always, TestRun (
2097 "return kernel messages",
2099 This returns the kernel messages (C<dmesg> output) from
2100 the guest kernel. This is sometimes useful for extended
2101 debugging of problems.
2103 Another way to get the same information is to enable
2104 verbose messages with C<guestfs_set_verbose> or by setting
2105 the environment variable C<LIBGUESTFS_DEBUG=1> before
2106 running the program.");
2108 ("ping_daemon", (RErr, []), 92, [],
2109 [InitEmpty, Always, TestRun (
2110 [["ping_daemon"]])],
2111 "ping the guest daemon",
2113 This is a test probe into the guestfs daemon running inside
2114 the qemu subprocess. Calling this function checks that the
2115 daemon responds to the ping message, without affecting the daemon
2116 or attached block device(s) in any other way.");
2118 ("equal", (RBool "equality", [String "file1"; String "file2"]), 93, [],
2119 [InitBasicFS, Always, TestOutputTrue (
2120 [["write_file"; "/file1"; "contents of a file"; "0"];
2121 ["cp"; "/file1"; "/file2"];
2122 ["equal"; "/file1"; "/file2"]]);
2123 InitBasicFS, Always, TestOutputFalse (
2124 [["write_file"; "/file1"; "contents of a file"; "0"];
2125 ["write_file"; "/file2"; "contents of another file"; "0"];
2126 ["equal"; "/file1"; "/file2"]]);
2127 InitBasicFS, Always, TestLastFail (
2128 [["equal"; "/file1"; "/file2"]])],
2129 "test if two files have equal contents",
2131 This compares the two files C<file1> and C<file2> and returns
2132 true if their content is exactly equal, or false otherwise.
2134 The external L<cmp(1)> program is used for the comparison.");
2136 ("strings", (RStringList "stringsout", [String "path"]), 94, [ProtocolLimitWarning],
2137 [InitBasicFS, Always, TestOutputList (
2138 [["write_file"; "/new"; "hello\nworld\n"; "0"];
2139 ["strings"; "/new"]], ["hello"; "world"]);
2140 InitBasicFS, Always, TestOutputList (
2142 ["strings"; "/new"]], [])],
2143 "print the printable strings in a file",
2145 This runs the L<strings(1)> command on a file and returns
2146 the list of printable strings found.");
2148 ("strings_e", (RStringList "stringsout", [String "encoding"; String "path"]), 95, [ProtocolLimitWarning],
2149 [InitBasicFS, Always, TestOutputList (
2150 [["write_file"; "/new"; "hello\nworld\n"; "0"];
2151 ["strings_e"; "b"; "/new"]], []);
2152 InitBasicFS, Disabled, TestOutputList (
2153 [["write_file"; "/new"; "\000h\000e\000l\000l\000o\000\n\000w\000o\000r\000l\000d\000\n"; "24"];
2154 ["strings_e"; "b"; "/new"]], ["hello"; "world"])],
2155 "print the printable strings in a file",
2157 This is like the C<guestfs_strings> command, but allows you to
2158 specify the encoding.
2160 See the L<strings(1)> manpage for the full list of encodings.
2162 Commonly useful encodings are C<l> (lower case L) which will
2163 show strings inside Windows/x86 files.
2165 The returned strings are transcoded to UTF-8.");
2167 ("hexdump", (RString "dump", [String "path"]), 96, [ProtocolLimitWarning],
2168 [InitBasicFS, Always, TestOutput (
2169 [["write_file"; "/new"; "hello\nworld\n"; "12"];
2170 ["hexdump"; "/new"]], "00000000 68 65 6c 6c 6f 0a 77 6f 72 6c 64 0a |hello.world.|\n0000000c\n");
2171 (* Test for RHBZ#501888c2 regression which caused large hexdump
2172 * commands to segfault.
2174 InitBasicFS, Always, TestRun (
2175 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2176 ["hexdump"; "/100krandom"]])],
2177 "dump a file in hexadecimal",
2179 This runs C<hexdump -C> on the given C<path>. The result is
2180 the human-readable, canonical hex dump of the file.");
2182 ("zerofree", (RErr, [String "device"]), 97, [],
2183 [InitNone, Always, TestOutput (
2184 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
2185 ["mkfs"; "ext3"; "/dev/sda1"];
2186 ["mount"; "/dev/sda1"; "/"];
2187 ["write_file"; "/new"; "test file"; "0"];
2188 ["umount"; "/dev/sda1"];
2189 ["zerofree"; "/dev/sda1"];
2190 ["mount"; "/dev/sda1"; "/"];
2191 ["cat"; "/new"]], "test file")],
2192 "zero unused inodes and disk blocks on ext2/3 filesystem",
2194 This runs the I<zerofree> program on C<device>. This program
2195 claims to zero unused inodes and disk blocks on an ext2/3
2196 filesystem, thus making it possible to compress the filesystem
2199 You should B<not> run this program if the filesystem is
2202 It is possible that using this program can damage the filesystem
2203 or data on the filesystem.");
2205 ("pvresize", (RErr, [String "device"]), 98, [],
2207 "resize an LVM physical volume",
2209 This resizes (expands or shrinks) an existing LVM physical
2210 volume to match the new size of the underlying device.");
2212 ("sfdisk_N", (RErr, [String "device"; Int "partnum";
2213 Int "cyls"; Int "heads"; Int "sectors";
2214 String "line"]), 99, [DangerWillRobinson],
2216 "modify a single partition on a block device",
2218 This runs L<sfdisk(8)> option to modify just the single
2219 partition C<n> (note: C<n> counts from 1).
2221 For other parameters, see C<guestfs_sfdisk>. You should usually
2222 pass C<0> for the cyls/heads/sectors parameters.");
2224 ("sfdisk_l", (RString "partitions", [String "device"]), 100, [],
2226 "display the partition table",
2228 This displays the partition table on C<device>, in the
2229 human-readable output of the L<sfdisk(8)> command. It is
2230 not intended to be parsed.");
2232 ("sfdisk_kernel_geometry", (RString "partitions", [String "device"]), 101, [],
2234 "display the kernel geometry",
2236 This displays the kernel's idea of the geometry of C<device>.
2238 The result is in human-readable format, and not designed to
2241 ("sfdisk_disk_geometry", (RString "partitions", [String "device"]), 102, [],
2243 "display the disk geometry from the partition table",
2245 This displays the disk geometry of C<device> read from the
2246 partition table. Especially in the case where the underlying
2247 block device has been resized, this can be different from the
2248 kernel's idea of the geometry (see C<guestfs_sfdisk_kernel_geometry>).
2250 The result is in human-readable format, and not designed to
2253 ("vg_activate_all", (RErr, [Bool "activate"]), 103, [],
2255 "activate or deactivate all volume groups",
2257 This command activates or (if C<activate> is false) deactivates
2258 all logical volumes in all volume groups.
2259 If activated, then they are made known to the
2260 kernel, ie. they appear as C</dev/mapper> devices. If deactivated,
2261 then those devices disappear.
2263 This command is the same as running C<vgchange -a y|n>");
2265 ("vg_activate", (RErr, [Bool "activate"; StringList "volgroups"]), 104, [],
2267 "activate or deactivate some volume groups",
2269 This command activates or (if C<activate> is false) deactivates
2270 all logical volumes in the listed volume groups C<volgroups>.
2271 If activated, then they are made known to the
2272 kernel, ie. they appear as C</dev/mapper> devices. If deactivated,
2273 then those devices disappear.
2275 This command is the same as running C<vgchange -a y|n volgroups...>
2277 Note that if C<volgroups> is an empty list then B<all> volume groups
2278 are activated or deactivated.");
2280 ("lvresize", (RErr, [String "device"; Int "mbytes"]), 105, [],
2281 [InitNone, Always, TestOutput (
2282 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
2283 ["pvcreate"; "/dev/sda1"];
2284 ["vgcreate"; "VG"; "/dev/sda1"];
2285 ["lvcreate"; "LV"; "VG"; "10"];
2286 ["mkfs"; "ext2"; "/dev/VG/LV"];
2287 ["mount"; "/dev/VG/LV"; "/"];
2288 ["write_file"; "/new"; "test content"; "0"];
2290 ["lvresize"; "/dev/VG/LV"; "20"];
2291 ["e2fsck_f"; "/dev/VG/LV"];
2292 ["resize2fs"; "/dev/VG/LV"];
2293 ["mount"; "/dev/VG/LV"; "/"];
2294 ["cat"; "/new"]], "test content")],
2295 "resize an LVM logical volume",
2297 This resizes (expands or shrinks) an existing LVM logical
2298 volume to C<mbytes>. When reducing, data in the reduced part
2301 ("resize2fs", (RErr, [String "device"]), 106, [],
2302 [], (* lvresize tests this *)
2303 "resize an ext2/ext3 filesystem",
2305 This resizes an ext2 or ext3 filesystem to match the size of
2306 the underlying device.
2308 I<Note:> It is sometimes required that you run C<guestfs_e2fsck_f>
2309 on the C<device> before calling this command. For unknown reasons
2310 C<resize2fs> sometimes gives an error about this and sometimes not.
2311 In any case, it is always safe to call C<guestfs_e2fsck_f> before
2312 calling this function.");
2314 ("find", (RStringList "names", [String "directory"]), 107, [],
2315 [InitBasicFS, Always, TestOutputList (
2316 [["find"; "/"]], ["lost+found"]);
2317 InitBasicFS, Always, TestOutputList (
2321 ["find"; "/"]], ["a"; "b"; "b/c"; "lost+found"]);
2322 InitBasicFS, Always, TestOutputList (
2323 [["mkdir_p"; "/a/b/c"];
2324 ["touch"; "/a/b/c/d"];
2325 ["find"; "/a/b/"]], ["c"; "c/d"])],
2326 "find all files and directories",
2328 This command lists out all files and directories, recursively,
2329 starting at C<directory>. It is essentially equivalent to
2330 running the shell command C<find directory -print> but some
2331 post-processing happens on the output, described below.
2333 This returns a list of strings I<without any prefix>. Thus
2334 if the directory structure was:
2340 then the returned list from C<guestfs_find> C</tmp> would be
2348 If C<directory> is not a directory, then this command returns
2351 The returned list is sorted.");
2353 ("e2fsck_f", (RErr, [String "device"]), 108, [],
2354 [], (* lvresize tests this *)
2355 "check an ext2/ext3 filesystem",
2357 This runs C<e2fsck -p -f device>, ie. runs the ext2/ext3
2358 filesystem checker on C<device>, noninteractively (C<-p>),
2359 even if the filesystem appears to be clean (C<-f>).
2361 This command is only needed because of C<guestfs_resize2fs>
2362 (q.v.). Normally you should use C<guestfs_fsck>.");
2364 ("sleep", (RErr, [Int "secs"]), 109, [],
2365 [InitNone, Always, TestRun (
2367 "sleep for some seconds",
2369 Sleep for C<secs> seconds.");
2371 ("ntfs_3g_probe", (RInt "status", [Bool "rw"; String "device"]), 110, [],
2372 [InitNone, Always, TestOutputInt (
2373 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
2374 ["mkfs"; "ntfs"; "/dev/sda1"];
2375 ["ntfs_3g_probe"; "true"; "/dev/sda1"]], 0);
2376 InitNone, Always, TestOutputInt (
2377 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
2378 ["mkfs"; "ext2"; "/dev/sda1"];
2379 ["ntfs_3g_probe"; "true"; "/dev/sda1"]], 12)],
2380 "probe NTFS volume",
2382 This command runs the L<ntfs-3g.probe(8)> command which probes
2383 an NTFS C<device> for mountability. (Not all NTFS volumes can
2384 be mounted read-write, and some cannot be mounted at all).
2386 C<rw> is a boolean flag. Set it to true if you want to test
2387 if the volume can be mounted read-write. Set it to false if
2388 you want to test if the volume can be mounted read-only.
2390 The return value is an integer which C<0> if the operation
2391 would succeed, or some non-zero value documented in the
2392 L<ntfs-3g.probe(8)> manual page.");
2394 ("sh", (RString "output", [String "command"]), 111, [],
2395 [], (* XXX needs tests *)
2396 "run a command via the shell",
2398 This call runs a command from the guest filesystem via the
2401 This is like C<guestfs_command>, but passes the command to:
2403 /bin/sh -c \"command\"
2405 Depending on the guest's shell, this usually results in
2406 wildcards being expanded, shell expressions being interpolated
2409 All the provisos about C<guestfs_command> apply to this call.");
2411 ("sh_lines", (RStringList "lines", [String "command"]), 112, [],
2412 [], (* XXX needs tests *)
2413 "run a command via the shell returning lines",
2415 This is the same as C<guestfs_sh>, but splits the result
2416 into a list of lines.
2418 See also: C<guestfs_command_lines>");
2420 ("glob_expand", (RStringList "paths", [String "pattern"]), 113, [],
2421 [InitBasicFS, Always, TestOutputList (
2422 [["mkdir_p"; "/a/b/c"];
2423 ["touch"; "/a/b/c/d"];
2424 ["touch"; "/a/b/c/e"];
2425 ["glob_expand"; "/a/b/c/*"]], ["/a/b/c/d"; "/a/b/c/e"]);
2426 InitBasicFS, Always, TestOutputList (
2427 [["mkdir_p"; "/a/b/c"];
2428 ["touch"; "/a/b/c/d"];
2429 ["touch"; "/a/b/c/e"];
2430 ["glob_expand"; "/a/*/c/*"]], ["/a/b/c/d"; "/a/b/c/e"]);
2431 InitBasicFS, Always, TestOutputList (
2432 [["mkdir_p"; "/a/b/c"];
2433 ["touch"; "/a/b/c/d"];
2434 ["touch"; "/a/b/c/e"];
2435 ["glob_expand"; "/a/*/x/*"]], [])],
2436 "expand a wildcard path",
2438 This command searches for all the pathnames matching
2439 C<pattern> according to the wildcard expansion rules
2442 If no paths match, then this returns an empty list
2443 (note: not an error).
2445 It is just a wrapper around the C L<glob(3)> function
2446 with flags C<GLOB_MARK|GLOB_BRACE>.
2447 See that manual page for more details.");
2449 ("scrub_device", (RErr, [String "device"]), 114, [DangerWillRobinson],
2450 [InitNone, Always, TestRun ( (* use /dev/sdc because it's smaller *)
2451 [["scrub_device"; "/dev/sdc"]])],
2452 "scrub (securely wipe) a device",
2454 This command writes patterns over C<device> to make data retrieval
2457 It is an interface to the L<scrub(1)> program. See that
2458 manual page for more details.");
2460 ("scrub_file", (RErr, [String "file"]), 115, [],
2461 [InitBasicFS, Always, TestRun (
2462 [["write_file"; "/file"; "content"; "0"];
2463 ["scrub_file"; "/file"]])],
2464 "scrub (securely wipe) a file",
2466 This command writes patterns over a file to make data retrieval
2469 The file is I<removed> after scrubbing.
2471 It is an interface to the L<scrub(1)> program. See that
2472 manual page for more details.");
2474 ("scrub_freespace", (RErr, [String "dir"]), 116, [],
2475 [], (* XXX needs testing *)
2476 "scrub (securely wipe) free space",
2478 This command creates the directory C<dir> and then fills it
2479 with files until the filesystem is full, and scrubs the files
2480 as for C<guestfs_scrub_file>, and deletes them.
2481 The intention is to scrub any free space on the partition
2484 It is an interface to the L<scrub(1)> program. See that
2485 manual page for more details.");
2487 ("mkdtemp", (RString "dir", [String "template"]), 117, [],
2488 [InitBasicFS, Always, TestRun (
2490 ["mkdtemp"; "/tmp/tmpXXXXXX"]])],
2491 "create a temporary directory",
2493 This command creates a temporary directory. The
2494 C<template> parameter should be a full pathname for the
2495 temporary directory name with the final six characters being
2498 For example: \"/tmp/myprogXXXXXX\" or \"/Temp/myprogXXXXXX\",
2499 the second one being suitable for Windows filesystems.
2501 The name of the temporary directory that was created
2504 The temporary directory is created with mode 0700
2505 and is owned by root.
2507 The caller is responsible for deleting the temporary
2508 directory and its contents after use.
2510 See also: L<mkdtemp(3)>");
2512 ("wc_l", (RInt "lines", [String "path"]), 118, [],
2513 [InitBasicFS, Always, TestOutputInt (
2514 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2515 ["wc_l"; "/10klines"]], 10000)],
2516 "count lines in a file",
2518 This command counts the lines in a file, using the
2519 C<wc -l> external command.");
2521 ("wc_w", (RInt "words", [String "path"]), 119, [],
2522 [InitBasicFS, Always, TestOutputInt (
2523 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2524 ["wc_w"; "/10klines"]], 10000)],
2525 "count words in a file",
2527 This command counts the words in a file, using the
2528 C<wc -w> external command.");
2530 ("wc_c", (RInt "chars", [String "path"]), 120, [],
2531 [InitBasicFS, Always, TestOutputInt (
2532 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2533 ["wc_c"; "/100kallspaces"]], 102400)],
2534 "count characters in a file",
2536 This command counts the characters in a file, using the
2537 C<wc -c> external command.");
2539 ("head", (RStringList "lines", [String "path"]), 121, [ProtocolLimitWarning],
2540 [InitBasicFS, Always, TestOutputList (
2541 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2542 ["head"; "/10klines"]], ["0abcdefghijklmnopqrstuvwxyz";"1abcdefghijklmnopqrstuvwxyz";"2abcdefghijklmnopqrstuvwxyz";"3abcdefghijklmnopqrstuvwxyz";"4abcdefghijklmnopqrstuvwxyz";"5abcdefghijklmnopqrstuvwxyz";"6abcdefghijklmnopqrstuvwxyz";"7abcdefghijklmnopqrstuvwxyz";"8abcdefghijklmnopqrstuvwxyz";"9abcdefghijklmnopqrstuvwxyz"])],
2543 "return first 10 lines of a file",
2545 This command returns up to the first 10 lines of a file as
2546 a list of strings.");
2548 ("head_n", (RStringList "lines", [Int "nrlines"; String "path"]), 122, [ProtocolLimitWarning],
2549 [InitBasicFS, Always, TestOutputList (
2550 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2551 ["head_n"; "3"; "/10klines"]], ["0abcdefghijklmnopqrstuvwxyz";"1abcdefghijklmnopqrstuvwxyz";"2abcdefghijklmnopqrstuvwxyz"]);
2552 InitBasicFS, Always, TestOutputList (
2553 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2554 ["head_n"; "-9997"; "/10klines"]], ["0abcdefghijklmnopqrstuvwxyz";"1abcdefghijklmnopqrstuvwxyz";"2abcdefghijklmnopqrstuvwxyz"]);
2555 InitBasicFS, Always, TestOutputList (
2556 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2557 ["head_n"; "0"; "/10klines"]], [])],
2558 "return first N lines of a file",
2560 If the parameter C<nrlines> is a positive number, this returns the first
2561 C<nrlines> lines of the file C<path>.
2563 If the parameter C<nrlines> is a negative number, this returns lines
2564 from the file C<path>, excluding the last C<nrlines> lines.
2566 If the parameter C<nrlines> is zero, this returns an empty list.");
2568 ("tail", (RStringList "lines", [String "path"]), 123, [ProtocolLimitWarning],
2569 [InitBasicFS, Always, TestOutputList (
2570 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2571 ["tail"; "/10klines"]], ["9990abcdefghijklmnopqrstuvwxyz";"9991abcdefghijklmnopqrstuvwxyz";"9992abcdefghijklmnopqrstuvwxyz";"9993abcdefghijklmnopqrstuvwxyz";"9994abcdefghijklmnopqrstuvwxyz";"9995abcdefghijklmnopqrstuvwxyz";"9996abcdefghijklmnopqrstuvwxyz";"9997abcdefghijklmnopqrstuvwxyz";"9998abcdefghijklmnopqrstuvwxyz";"9999abcdefghijklmnopqrstuvwxyz"])],
2572 "return last 10 lines of a file",
2574 This command returns up to the last 10 lines of a file as
2575 a list of strings.");
2577 ("tail_n", (RStringList "lines", [Int "nrlines"; String "path"]), 124, [ProtocolLimitWarning],
2578 [InitBasicFS, Always, TestOutputList (
2579 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2580 ["tail_n"; "3"; "/10klines"]], ["9997abcdefghijklmnopqrstuvwxyz";"9998abcdefghijklmnopqrstuvwxyz";"9999abcdefghijklmnopqrstuvwxyz"]);
2581 InitBasicFS, Always, TestOutputList (
2582 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2583 ["tail_n"; "-9998"; "/10klines"]], ["9997abcdefghijklmnopqrstuvwxyz";"9998abcdefghijklmnopqrstuvwxyz";"9999abcdefghijklmnopqrstuvwxyz"]);
2584 InitBasicFS, Always, TestOutputList (
2585 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2586 ["tail_n"; "0"; "/10klines"]], [])],
2587 "return last N lines of a file",
2589 If the parameter C<nrlines> is a positive number, this returns the last
2590 C<nrlines> lines of the file C<path>.
2592 If the parameter C<nrlines> is a negative number, this returns lines
2593 from the file C<path>, starting with the C<-nrlines>th line.
2595 If the parameter C<nrlines> is zero, this returns an empty list.");
2597 ("df", (RString "output", []), 125, [],
2598 [], (* XXX Tricky to test because it depends on the exact format
2599 * of the 'df' command and other imponderables.
2601 "report file system disk space usage",
2603 This command runs the C<df> command to report disk space used.
2605 This command is mostly useful for interactive sessions. It
2606 is I<not> intended that you try to parse the output string.
2607 Use C<statvfs> from programs.");
2609 ("df_h", (RString "output", []), 126, [],
2610 [], (* XXX Tricky to test because it depends on the exact format
2611 * of the 'df' command and other imponderables.
2613 "report file system disk space usage (human readable)",
2615 This command runs the C<df -h> command to report disk space used
2616 in human-readable format.
2618 This command is mostly useful for interactive sessions. It
2619 is I<not> intended that you try to parse the output string.
2620 Use C<statvfs> from programs.");
2622 ("du", (RInt64 "sizekb", [String "path"]), 127, [],
2623 [InitBasicFS, Always, TestOutputInt (
2625 ["du"; "/p"]], 1 (* ie. 1 block, so depends on ext3 blocksize *))],
2626 "estimate file space usage",
2628 This command runs the C<du -s> command to estimate file space
2631 C<path> can be a file or a directory. If C<path> is a directory
2632 then the estimate includes the contents of the directory and all
2633 subdirectories (recursively).
2635 The result is the estimated size in I<kilobytes>
2636 (ie. units of 1024 bytes).");
2638 ("initrd_list", (RStringList "filenames", [String "path"]), 128, [],
2639 [InitBasicFS, Always, TestOutputList (
2640 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2641 ["initrd_list"; "/initrd"]], ["empty";"known-1";"known-2";"known-3"])],
2642 "list files in an initrd",
2644 This command lists out files contained in an initrd.
2646 The files are listed without any initial C</> character. The
2647 files are listed in the order they appear (not necessarily
2648 alphabetical). Directory names are listed as separate items.
2650 Old Linux kernels (2.4 and earlier) used a compressed ext2
2651 filesystem as initrd. We I<only> support the newer initramfs
2652 format (compressed cpio files).");
2654 ("mount_loop", (RErr, [String "file"; String "mountpoint"]), 129, [],
2656 "mount a file using the loop device",
2658 This command lets you mount C<file> (a filesystem image
2659 in a file) on a mount point. It is entirely equivalent to
2660 the command C<mount -o loop file mountpoint>.");
2662 ("mkswap", (RErr, [String "device"]), 130, [],
2663 [InitEmpty, Always, TestRun (
2664 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
2665 ["mkswap"; "/dev/sda1"]])],
2666 "create a swap partition",
2668 Create a swap partition on C<device>.");
2670 ("mkswap_L", (RErr, [String "label"; String "device"]), 131, [],
2671 [InitEmpty, Always, TestRun (
2672 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
2673 ["mkswap_L"; "hello"; "/dev/sda1"]])],
2674 "create a swap partition with a label",
2676 Create a swap partition on C<device> with label C<label>.");
2678 ("mkswap_U", (RErr, [String "uuid"; String "device"]), 132, [],
2679 [InitEmpty, Always, TestRun (
2680 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
2681 ["mkswap_U"; "a3a61220-882b-4f61-89f4-cf24dcc7297d"; "/dev/sda1"]])],
2682 "create a swap partition with an explicit UUID",
2684 Create a swap partition on C<device> with UUID C<uuid>.");
2686 ("mknod", (RErr, [Int "mode"; Int "devmajor"; Int "devminor"; String "path"]), 133, [],
2687 [InitBasicFS, Always, TestOutputStruct (
2688 [["mknod"; "0o10777"; "0"; "0"; "/node"];
2689 (* NB: default umask 022 means 0777 -> 0755 in these tests *)
2690 ["stat"; "/node"]], [CompareWithInt ("mode", 0o10755)]);
2691 InitBasicFS, Always, TestOutputStruct (
2692 [["mknod"; "0o60777"; "66"; "99"; "/node"];
2693 ["stat"; "/node"]], [CompareWithInt ("mode", 0o60755)])],
2694 "make block, character or FIFO devices",
2696 This call creates block or character special devices, or
2697 named pipes (FIFOs).
2699 The C<mode> parameter should be the mode, using the standard
2700 constants. C<devmajor> and C<devminor> are the
2701 device major and minor numbers, only used when creating block
2702 and character special devices.");
2704 ("mkfifo", (RErr, [Int "mode"; String "path"]), 134, [],
2705 [InitBasicFS, Always, TestOutputStruct (
2706 [["mkfifo"; "0o777"; "/node"];
2707 ["stat"; "/node"]], [CompareWithInt ("mode", 0o10755)])],
2708 "make FIFO (named pipe)",
2710 This call creates a FIFO (named pipe) called C<path> with
2711 mode C<mode>. It is just a convenient wrapper around
2712 C<guestfs_mknod>.");
2714 ("mknod_b", (RErr, [Int "mode"; Int "devmajor"; Int "devminor"; String "path"]), 135, [],
2715 [InitBasicFS, Always, TestOutputStruct (
2716 [["mknod_b"; "0o777"; "99"; "66"; "/node"];
2717 ["stat"; "/node"]], [CompareWithInt ("mode", 0o60755)])],
2718 "make block device node",
2720 This call creates a block device node called C<path> with
2721 mode C<mode> and device major/minor C<devmajor> and C<devminor>.
2722 It is just a convenient wrapper around C<guestfs_mknod>.");
2724 ("mknod_c", (RErr, [Int "mode"; Int "devmajor"; Int "devminor"; String "path"]), 136, [],
2725 [InitBasicFS, Always, TestOutputStruct (
2726 [["mknod_c"; "0o777"; "99"; "66"; "/node"];
2727 ["stat"; "/node"]], [CompareWithInt ("mode", 0o20755)])],
2728 "make char device node",
2730 This call creates a char device node called C<path> with
2731 mode C<mode> and device major/minor C<devmajor> and C<devminor>.
2732 It is just a convenient wrapper around C<guestfs_mknod>.");
2734 ("umask", (RInt "oldmask", [Int "mask"]), 137, [],
2735 [], (* XXX umask is one of those stateful things that we should
2736 * reset between each test.
2738 "set file mode creation mask (umask)",
2740 This function sets the mask used for creating new files and
2741 device nodes to C<mask & 0777>.
2743 Typical umask values would be C<022> which creates new files
2744 with permissions like \"-rw-r--r--\" or \"-rwxr-xr-x\", and
2745 C<002> which creates new files with permissions like
2746 \"-rw-rw-r--\" or \"-rwxrwxr-x\".
2748 The default umask is C<022>. This is important because it
2749 means that directories and device nodes will be created with
2750 C<0644> or C<0755> mode even if you specify C<0777>.
2752 See also L<umask(2)>, C<guestfs_mknod>, C<guestfs_mkdir>.
2754 This call returns the previous umask.");
2756 ("readdir", (RDirentList "entries", [String "dir"]), 138, [],
2758 "read directories entries",
2760 This returns the list of directory entries in directory C<dir>.
2762 All entries in the directory are returned, including C<.> and
2763 C<..>. The entries are I<not> sorted, but returned in the same
2764 order as the underlying filesystem.
2766 This function is primarily intended for use by programs. To
2767 get a simple list of names, use C<guestfs_ls>. To get a printable
2768 directory for human consumption, use C<guestfs_ll>.");
2772 let all_functions = non_daemon_functions @ daemon_functions
2774 (* In some places we want the functions to be displayed sorted
2775 * alphabetically, so this is useful:
2777 let all_functions_sorted =
2778 List.sort (fun (n1,_,_,_,_,_,_) (n2,_,_,_,_,_,_) ->
2779 compare n1 n2) all_functions
2781 (* Column names and types from LVM PVs/VGs/LVs. *)
2790 "pv_attr", `String (* XXX *);
2791 "pv_pe_count", `Int;
2792 "pv_pe_alloc_count", `Int;
2795 "pv_mda_count", `Int;
2796 "pv_mda_free", `Bytes;
2797 (* Not in Fedora 10:
2798 "pv_mda_size", `Bytes;
2805 "vg_attr", `String (* XXX *);
2808 "vg_sysid", `String;
2809 "vg_extent_size", `Bytes;
2810 "vg_extent_count", `Int;
2811 "vg_free_count", `Int;
2819 "vg_mda_count", `Int;
2820 "vg_mda_free", `Bytes;
2821 (* Not in Fedora 10:
2822 "vg_mda_size", `Bytes;
2828 "lv_attr", `String (* XXX *);
2831 "lv_kernel_major", `Int;
2832 "lv_kernel_minor", `Int;
2836 "snap_percent", `OptPercent;
2837 "copy_percent", `OptPercent;
2840 "mirror_log", `String;
2844 (* Column names and types from stat structures.
2845 * NB. Can't use things like 'st_atime' because glibc header files
2846 * define some of these as macros. Ugh.
2863 let statvfs_cols = [
2877 (* Column names in dirent structure. *)
2880 "ftyp", `Char; (* 'b' 'c' 'd' 'f' (FIFO) 'l' 'r' (regular file) 's' 'u' '?' *)
2884 (* Used for testing language bindings. *)
2886 | CallString of string
2887 | CallOptString of string option
2888 | CallStringList of string list
2892 (* Used to memoize the result of pod2text. *)
2893 let pod2text_memo_filename = "src/.pod2text.data"
2894 let pod2text_memo : ((int * string * string), string list) Hashtbl.t =
2896 let chan = open_in pod2text_memo_filename in
2897 let v = input_value chan in
2901 _ -> Hashtbl.create 13
2903 (* Useful functions.
2904 * Note we don't want to use any external OCaml libraries which
2905 * makes this a bit harder than it should be.
2907 let failwithf fs = ksprintf failwith fs
2909 let replace_char s c1 c2 =
2910 let s2 = String.copy s in
2911 let r = ref false in
2912 for i = 0 to String.length s2 - 1 do
2913 if String.unsafe_get s2 i = c1 then (
2914 String.unsafe_set s2 i c2;
2918 if not !r then s else s2
2922 (* || c = '\f' *) || c = '\n' || c = '\r' || c = '\t' (* || c = '\v' *)
2924 let triml ?(test = isspace) str =
2926 let n = ref (String.length str) in
2927 while !n > 0 && test str.[!i]; do
2932 else String.sub str !i !n
2934 let trimr ?(test = isspace) str =
2935 let n = ref (String.length str) in
2936 while !n > 0 && test str.[!n-1]; do
2939 if !n = String.length str then str
2940 else String.sub str 0 !n
2942 let trim ?(test = isspace) str =
2943 trimr ~test (triml ~test str)
2945 let rec find s sub =
2946 let len = String.length s in
2947 let sublen = String.length sub in
2949 if i <= len-sublen then (
2951 if j < sublen then (
2952 if s.[i+j] = sub.[j] then loop2 (j+1)
2958 if r = -1 then loop (i+1) else r
2964 let rec replace_str s s1 s2 =
2965 let len = String.length s in
2966 let sublen = String.length s1 in
2967 let i = find s s1 in
2970 let s' = String.sub s 0 i in
2971 let s'' = String.sub s (i+sublen) (len-i-sublen) in
2972 s' ^ s2 ^ replace_str s'' s1 s2
2975 let rec string_split sep str =
2976 let len = String.length str in
2977 let seplen = String.length sep in
2978 let i = find str sep in
2979 if i = -1 then [str]
2981 let s' = String.sub str 0 i in
2982 let s'' = String.sub str (i+seplen) (len-i-seplen) in
2983 s' :: string_split sep s''
2986 let files_equal n1 n2 =
2987 let cmd = sprintf "cmp -s %s %s" (Filename.quote n1) (Filename.quote n2) in
2988 match Sys.command cmd with
2991 | i -> failwithf "%s: failed with error code %d" cmd i
2993 let rec find_map f = function
2994 | [] -> raise Not_found
2998 | None -> find_map f xs
3001 let rec loop i = function
3003 | x :: xs -> f i x; loop (i+1) xs
3008 let rec loop i = function
3010 | x :: xs -> let r = f i x in r :: loop (i+1) xs
3014 let name_of_argt = function
3015 | String n | OptString n | StringList n | Bool n | Int n
3016 | FileIn n | FileOut n -> n
3018 let seq_of_test = function
3019 | TestRun s | TestOutput (s, _) | TestOutputList (s, _)
3020 | TestOutputListOfDevices (s, _)
3021 | TestOutputInt (s, _) | TestOutputTrue s | TestOutputFalse s
3022 | TestOutputLength (s, _) | TestOutputStruct (s, _)
3023 | TestLastFail s -> s
3025 (* Check function names etc. for consistency. *)
3026 let check_functions () =
3027 let contains_uppercase str =
3028 let len = String.length str in
3030 if i >= len then false
3033 if c >= 'A' && c <= 'Z' then true
3040 (* Check function names. *)
3042 fun (name, _, _, _, _, _, _) ->
3043 if String.length name >= 7 && String.sub name 0 7 = "guestfs" then
3044 failwithf "function name %s does not need 'guestfs' prefix" name;
3046 failwithf "function name is empty";
3047 if name.[0] < 'a' || name.[0] > 'z' then
3048 failwithf "function name %s must start with lowercase a-z" name;
3049 if String.contains name '-' then
3050 failwithf "function name %s should not contain '-', use '_' instead."
3054 (* Check function parameter/return names. *)
3056 fun (name, style, _, _, _, _, _) ->
3057 let check_arg_ret_name n =
3058 if contains_uppercase n then
3059 failwithf "%s param/ret %s should not contain uppercase chars"
3061 if String.contains n '-' || String.contains n '_' then
3062 failwithf "%s param/ret %s should not contain '-' or '_'"
3065 failwithf "%s has a param/ret called 'value', which causes conflicts in the OCaml bindings, use something like 'val' or a more descriptive name" name;
3066 if n = "int" || n = "char" || n = "short" || n = "long" then
3067 failwithf "%s has a param/ret which conflicts with a C type (eg. 'int', 'char' etc.)" name;
3068 if n = "i" || n = "n" then
3069 failwithf "%s has a param/ret called 'i' or 'n', which will cause some conflicts in the generated code" name;
3070 if n = "argv" || n = "args" then
3071 failwithf "%s has a param/ret called 'argv' or 'args', which will cause some conflicts in the generated code" name
3074 (match fst style with
3076 | RInt n | RInt64 n | RBool n | RConstString n | RString n
3077 | RStringList n | RPVList n | RVGList n | RLVList n
3078 | RStat n | RStatVFS n
3081 check_arg_ret_name n
3083 check_arg_ret_name n;
3084 check_arg_ret_name m
3086 List.iter (fun arg -> check_arg_ret_name (name_of_argt arg)) (snd style)
3089 (* Check short descriptions. *)
3091 fun (name, _, _, _, _, shortdesc, _) ->
3092 if shortdesc.[0] <> Char.lowercase shortdesc.[0] then
3093 failwithf "short description of %s should begin with lowercase." name;
3094 let c = shortdesc.[String.length shortdesc-1] in
3095 if c = '\n' || c = '.' then
3096 failwithf "short description of %s should not end with . or \\n." name
3099 (* Check long dscriptions. *)
3101 fun (name, _, _, _, _, _, longdesc) ->
3102 if longdesc.[String.length longdesc-1] = '\n' then
3103 failwithf "long description of %s should not end with \\n." name
3106 (* Check proc_nrs. *)
3108 fun (name, _, proc_nr, _, _, _, _) ->
3109 if proc_nr <= 0 then
3110 failwithf "daemon function %s should have proc_nr > 0" name
3114 fun (name, _, proc_nr, _, _, _, _) ->
3115 if proc_nr <> -1 then
3116 failwithf "non-daemon function %s should have proc_nr -1" name
3117 ) non_daemon_functions;
3120 List.map (fun (name, _, proc_nr, _, _, _, _) -> name, proc_nr)
3123 List.sort (fun (_,nr1) (_,nr2) -> compare nr1 nr2) proc_nrs in
3124 let rec loop = function
3127 | (name1,nr1) :: ((name2,nr2) :: _ as rest) when nr1 < nr2 ->
3129 | (name1,nr1) :: (name2,nr2) :: _ ->
3130 failwithf "%s and %s have conflicting procedure numbers (%d, %d)"
3138 (* Ignore functions that have no tests. We generate a
3139 * warning when the user does 'make check' instead.
3141 | name, _, _, _, [], _, _ -> ()
3142 | name, _, _, _, tests, _, _ ->
3146 match seq_of_test test with
3148 failwithf "%s has a test containing an empty sequence" name
3149 | cmds -> List.map List.hd cmds
3151 let funcs = List.flatten funcs in
3153 let tested = List.mem name funcs in
3156 failwithf "function %s has tests but does not test itself" name
3159 (* 'pr' prints to the current output file. *)
3160 let chan = ref stdout
3161 let pr fs = ksprintf (output_string !chan) fs
3163 (* Generate a header block in a number of standard styles. *)
3164 type comment_style = CStyle | HashStyle | OCamlStyle | HaskellStyle
3165 type license = GPLv2 | LGPLv2
3167 let generate_header comment license =
3168 let c = match comment with
3169 | CStyle -> pr "/* "; " *"
3170 | HashStyle -> pr "# "; "#"
3171 | OCamlStyle -> pr "(* "; " *"
3172 | HaskellStyle -> pr "{- "; " " in
3173 pr "libguestfs generated file\n";
3174 pr "%s WARNING: THIS FILE IS GENERATED BY 'src/generator.ml'.\n" c;
3175 pr "%s ANY CHANGES YOU MAKE TO THIS FILE WILL BE LOST.\n" c;
3177 pr "%s Copyright (C) 2009 Red Hat Inc.\n" c;
3181 pr "%s This program is free software; you can redistribute it and/or modify\n" c;
3182 pr "%s it under the terms of the GNU General Public License as published by\n" c;
3183 pr "%s the Free Software Foundation; either version 2 of the License, or\n" c;
3184 pr "%s (at your option) any later version.\n" c;
3186 pr "%s This program is distributed in the hope that it will be useful,\n" c;
3187 pr "%s but WITHOUT ANY WARRANTY; without even the implied warranty of\n" c;
3188 pr "%s MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" c;
3189 pr "%s GNU General Public License for more details.\n" c;
3191 pr "%s You should have received a copy of the GNU General Public License along\n" c;
3192 pr "%s with this program; if not, write to the Free Software Foundation, Inc.,\n" c;
3193 pr "%s 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\n" c;
3196 pr "%s This library is free software; you can redistribute it and/or\n" c;
3197 pr "%s modify it under the terms of the GNU Lesser General Public\n" c;
3198 pr "%s License as published by the Free Software Foundation; either\n" c;
3199 pr "%s version 2 of the License, or (at your option) any later version.\n" c;
3201 pr "%s This library is distributed in the hope that it will be useful,\n" c;
3202 pr "%s but WITHOUT ANY WARRANTY; without even the implied warranty of\n" c;
3203 pr "%s MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n" c;
3204 pr "%s Lesser General Public License for more details.\n" c;
3206 pr "%s You should have received a copy of the GNU Lesser General Public\n" c;
3207 pr "%s License along with this library; if not, write to the Free Software\n" c;
3208 pr "%s Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n" c;
3211 | CStyle -> pr " */\n"
3213 | OCamlStyle -> pr " *)\n"
3214 | HaskellStyle -> pr "-}\n"
3218 (* Start of main code generation functions below this line. *)
3220 (* Generate the pod documentation for the C API. *)
3221 let rec generate_actions_pod () =
3223 fun (shortname, style, _, flags, _, _, longdesc) ->
3224 if not (List.mem NotInDocs flags) then (
3225 let name = "guestfs_" ^ shortname in
3226 pr "=head2 %s\n\n" name;
3228 generate_prototype ~extern:false ~handle:"handle" name style;
3230 pr "%s\n\n" longdesc;
3231 (match fst style with
3233 pr "This function returns 0 on success or -1 on error.\n\n"
3235 pr "On error this function returns -1.\n\n"
3237 pr "On error this function returns -1.\n\n"
3239 pr "This function returns a C truth value on success or -1 on error.\n\n"
3241 pr "This function returns a string, or NULL on error.
3242 The string is owned by the guest handle and must I<not> be freed.\n\n"
3244 pr "This function returns a string, or NULL on error.
3245 I<The caller must free the returned string after use>.\n\n"
3247 pr "This function returns a NULL-terminated array of strings
3248 (like L<environ(3)>), or NULL if there was an error.
3249 I<The caller must free the strings and the array after use>.\n\n"
3251 pr "This function returns a C<struct guestfs_int_bool *>,
3252 or NULL if there was an error.
3253 I<The caller must call C<guestfs_free_int_bool> after use>.\n\n"
3255 pr "This function returns a C<struct guestfs_lvm_pv_list *>
3256 (see E<lt>guestfs-structs.hE<gt>),
3257 or NULL if there was an error.
3258 I<The caller must call C<guestfs_free_lvm_pv_list> after use>.\n\n"
3260 pr "This function returns a C<struct guestfs_lvm_vg_list *>
3261 (see E<lt>guestfs-structs.hE<gt>),
3262 or NULL if there was an error.
3263 I<The caller must call C<guestfs_free_lvm_vg_list> after use>.\n\n"
3265 pr "This function returns a C<struct guestfs_lvm_lv_list *>
3266 (see E<lt>guestfs-structs.hE<gt>),
3267 or NULL if there was an error.
3268 I<The caller must call C<guestfs_free_lvm_lv_list> after use>.\n\n"
3270 pr "This function returns a C<struct guestfs_stat *>
3271 (see L<stat(2)> and E<lt>guestfs-structs.hE<gt>),
3272 or NULL if there was an error.
3273 I<The caller must call C<free> after use>.\n\n"
3275 pr "This function returns a C<struct guestfs_statvfs *>
3276 (see L<statvfs(2)> and E<lt>guestfs-structs.hE<gt>),
3277 or NULL if there was an error.
3278 I<The caller must call C<free> after use>.\n\n"
3280 pr "This function returns a NULL-terminated array of
3281 strings, or NULL if there was an error.
3282 The array of strings will always have length C<2n+1>, where
3283 C<n> keys and values alternate, followed by the trailing NULL entry.
3284 I<The caller must free the strings and the array after use>.\n\n"
3286 pr "This function returns a C<struct guestfs_dirent_list *>
3287 (see E<lt>guestfs-structs.hE<gt>),
3288 or NULL if there was an error.
3289 I<The caller must call C<guestfs_free_dirent_list> after use>.\n\n"
3291 if List.mem ProtocolLimitWarning flags then
3292 pr "%s\n\n" protocol_limit_warning;
3293 if List.mem DangerWillRobinson flags then
3294 pr "%s\n\n" danger_will_robinson
3296 ) all_functions_sorted
3298 and generate_structs_pod () =
3299 (* LVM structs documentation. *)
3302 pr "=head2 guestfs_lvm_%s\n" typ;
3304 pr " struct guestfs_lvm_%s {\n" typ;
3307 | name, `String -> pr " char *%s;\n" name
3309 pr " /* The next field is NOT nul-terminated, be careful when printing it: */\n";
3310 pr " char %s[32];\n" name
3311 | name, `Bytes -> pr " uint64_t %s;\n" name
3312 | name, `Int -> pr " int64_t %s;\n" name
3313 | name, `OptPercent ->
3314 pr " /* The next field is [0..100] or -1 meaning 'not present': */\n";
3315 pr " float %s;\n" name
3318 pr " struct guestfs_lvm_%s_list {\n" typ;
3319 pr " uint32_t len; /* Number of elements in list. */\n";
3320 pr " struct guestfs_lvm_%s *val; /* Elements. */\n" typ;
3323 pr " void guestfs_free_lvm_%s_list (struct guestfs_free_lvm_%s_list *);\n"
3326 ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols];
3331 pr "=head2 guestfs_%s\n" typ;
3333 pr " struct guestfs_%s {\n" typ;
3336 | name, `Int -> pr " int64_t %s;\n" name
3340 ) [ "stat", stat_cols; "statvfs", statvfs_cols ];
3343 pr "=head2 guestfs_dirent\n";
3345 pr " struct guestfs_dirent {\n";
3348 | name, `String -> pr " char *%s;\n" name
3349 | name, `Int -> pr " int64_t %s;\n" name
3350 | name, `Char -> pr " char %s;\n" name
3354 pr " struct guestfs_dirent_list {\n";
3355 pr " uint32_t len; /* Number of elements in list. */\n";
3356 pr " struct guestfs_dirent *val; /* Elements. */\n";
3359 pr " void guestfs_free_dirent_list (struct guestfs_free_dirent_list *);\n";
3362 (* Generate the protocol (XDR) file, 'guestfs_protocol.x' and
3363 * indirectly 'guestfs_protocol.h' and 'guestfs_protocol.c'.
3365 * We have to use an underscore instead of a dash because otherwise
3366 * rpcgen generates incorrect code.
3368 * This header is NOT exported to clients, but see also generate_structs_h.
3370 and generate_xdr () =
3371 generate_header CStyle LGPLv2;
3373 (* This has to be defined to get around a limitation in Sun's rpcgen. *)
3374 pr "typedef string str<>;\n";
3377 (* LVM internal structures. *)
3381 pr "struct guestfs_lvm_int_%s {\n" typ;
3383 | name, `String -> pr " string %s<>;\n" name
3384 | name, `UUID -> pr " opaque %s[32];\n" name
3385 | name, `Bytes -> pr " hyper %s;\n" name
3386 | name, `Int -> pr " hyper %s;\n" name
3387 | name, `OptPercent -> pr " float %s;\n" name
3391 pr "typedef struct guestfs_lvm_int_%s guestfs_lvm_int_%s_list<>;\n" typ typ;
3393 ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols];
3395 (* Stat internal structures. *)
3399 pr "struct guestfs_int_%s {\n" typ;
3401 | name, `Int -> pr " hyper %s;\n" name
3405 ) ["stat", stat_cols; "statvfs", statvfs_cols];
3407 (* Dirent structures. *)
3408 pr "struct guestfs_int_dirent {\n";
3410 | name, `Int -> pr " hyper %s;\n" name
3411 | name, `Char -> pr " char %s;\n" name
3412 | name, `String -> pr " string %s<>;\n" name
3416 pr "typedef struct guestfs_int_dirent guestfs_int_dirent_list<>;\n";
3420 fun (shortname, style, _, _, _, _, _) ->
3421 let name = "guestfs_" ^ shortname in
3423 (match snd style with
3426 pr "struct %s_args {\n" name;
3429 | String n -> pr " string %s<>;\n" n
3430 | OptString n -> pr " str *%s;\n" n
3431 | StringList n -> pr " str %s<>;\n" n
3432 | Bool n -> pr " bool %s;\n" n
3433 | Int n -> pr " int %s;\n" n
3434 | FileIn _ | FileOut _ -> ()
3438 (match fst style with
3441 pr "struct %s_ret {\n" name;
3445 pr "struct %s_ret {\n" name;
3446 pr " hyper %s;\n" n;
3449 pr "struct %s_ret {\n" name;
3453 failwithf "RConstString cannot be returned from a daemon function"
3455 pr "struct %s_ret {\n" name;
3456 pr " string %s<>;\n" n;
3459 pr "struct %s_ret {\n" name;
3460 pr " str %s<>;\n" n;
3463 pr "struct %s_ret {\n" name;
3468 pr "struct %s_ret {\n" name;
3469 pr " guestfs_lvm_int_pv_list %s;\n" n;
3472 pr "struct %s_ret {\n" name;
3473 pr " guestfs_lvm_int_vg_list %s;\n" n;
3476 pr "struct %s_ret {\n" name;
3477 pr " guestfs_lvm_int_lv_list %s;\n" n;
3480 pr "struct %s_ret {\n" name;
3481 pr " guestfs_int_stat %s;\n" n;
3484 pr "struct %s_ret {\n" name;
3485 pr " guestfs_int_statvfs %s;\n" n;
3488 pr "struct %s_ret {\n" name;
3489 pr " str %s<>;\n" n;
3492 pr "struct %s_ret {\n" name;
3493 pr " guestfs_int_dirent_list %s;\n" n;
3498 (* Table of procedure numbers. *)
3499 pr "enum guestfs_procedure {\n";
3501 fun (shortname, _, proc_nr, _, _, _, _) ->
3502 pr " GUESTFS_PROC_%s = %d,\n" (String.uppercase shortname) proc_nr
3504 pr " GUESTFS_PROC_NR_PROCS\n";
3508 (* Having to choose a maximum message size is annoying for several
3509 * reasons (it limits what we can do in the API), but it (a) makes
3510 * the protocol a lot simpler, and (b) provides a bound on the size
3511 * of the daemon which operates in limited memory space. For large
3512 * file transfers you should use FTP.
3514 pr "const GUESTFS_MESSAGE_MAX = %d;\n" (4 * 1024 * 1024);
3517 (* Message header, etc. *)
3519 /* The communication protocol is now documented in the guestfs(3)
3523 const GUESTFS_PROGRAM = 0x2000F5F5;
3524 const GUESTFS_PROTOCOL_VERSION = 1;
3526 /* These constants must be larger than any possible message length. */
3527 const GUESTFS_LAUNCH_FLAG = 0xf5f55ff5;
3528 const GUESTFS_CANCEL_FLAG = 0xffffeeee;
3530 enum guestfs_message_direction {
3531 GUESTFS_DIRECTION_CALL = 0, /* client -> daemon */
3532 GUESTFS_DIRECTION_REPLY = 1 /* daemon -> client */
3535 enum guestfs_message_status {
3536 GUESTFS_STATUS_OK = 0,
3537 GUESTFS_STATUS_ERROR = 1
3540 const GUESTFS_ERROR_LEN = 256;
3542 struct guestfs_message_error {
3543 string error_message<GUESTFS_ERROR_LEN>;
3546 struct guestfs_message_header {
3547 unsigned prog; /* GUESTFS_PROGRAM */
3548 unsigned vers; /* GUESTFS_PROTOCOL_VERSION */
3549 guestfs_procedure proc; /* GUESTFS_PROC_x */
3550 guestfs_message_direction direction;
3551 unsigned serial; /* message serial number */
3552 guestfs_message_status status;
3555 const GUESTFS_MAX_CHUNK_SIZE = 8192;
3557 struct guestfs_chunk {
3558 int cancel; /* if non-zero, transfer is cancelled */
3559 /* data size is 0 bytes if the transfer has finished successfully */
3560 opaque data<GUESTFS_MAX_CHUNK_SIZE>;
3564 (* Generate the guestfs-structs.h file. *)
3565 and generate_structs_h () =
3566 generate_header CStyle LGPLv2;
3568 (* This is a public exported header file containing various
3569 * structures. The structures are carefully written to have
3570 * exactly the same in-memory format as the XDR structures that
3571 * we use on the wire to the daemon. The reason for creating
3572 * copies of these structures here is just so we don't have to
3573 * export the whole of guestfs_protocol.h (which includes much
3574 * unrelated and XDR-dependent stuff that we don't want to be
3575 * public, or required by clients).
3577 * To reiterate, we will pass these structures to and from the
3578 * client with a simple assignment or memcpy, so the format
3579 * must be identical to what rpcgen / the RFC defines.
3582 (* guestfs_int_bool structure. *)
3583 pr "struct guestfs_int_bool {\n";
3589 (* LVM public structures. *)
3593 pr "struct guestfs_lvm_%s {\n" typ;
3596 | name, `String -> pr " char *%s;\n" name
3597 | name, `UUID -> pr " char %s[32]; /* this is NOT nul-terminated, be careful when printing */\n" name
3598 | name, `Bytes -> pr " uint64_t %s;\n" name
3599 | name, `Int -> pr " int64_t %s;\n" name
3600 | name, `OptPercent -> pr " float %s; /* [0..100] or -1 */\n" name
3604 pr "struct guestfs_lvm_%s_list {\n" typ;
3605 pr " uint32_t len;\n";
3606 pr " struct guestfs_lvm_%s *val;\n" typ;
3609 ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols];
3611 (* Stat structures. *)
3615 pr "struct guestfs_%s {\n" typ;
3618 | name, `Int -> pr " int64_t %s;\n" name
3622 ) ["stat", stat_cols; "statvfs", statvfs_cols];
3624 (* Dirent structures. *)
3625 pr "struct guestfs_dirent {\n";
3628 | name, `Int -> pr " int64_t %s;\n" name
3629 | name, `Char -> pr " char %s;\n" name
3630 | name, `String -> pr " char *%s;\n" name
3634 pr "struct guestfs_dirent_list {\n";
3635 pr " uint32_t len;\n";
3636 pr " struct guestfs_dirent *val;\n";
3640 (* Generate the guestfs-actions.h file. *)
3641 and generate_actions_h () =
3642 generate_header CStyle LGPLv2;
3644 fun (shortname, style, _, _, _, _, _) ->
3645 let name = "guestfs_" ^ shortname in
3646 generate_prototype ~single_line:true ~newline:true ~handle:"handle"
3650 (* Generate the client-side dispatch stubs. *)
3651 and generate_client_actions () =
3652 generate_header CStyle LGPLv2;
3658 #include \"guestfs.h\"
3659 #include \"guestfs_protocol.h\"
3661 #define error guestfs_error
3662 #define perrorf guestfs_perrorf
3663 #define safe_malloc guestfs_safe_malloc
3664 #define safe_realloc guestfs_safe_realloc
3665 #define safe_strdup guestfs_safe_strdup
3666 #define safe_memdup guestfs_safe_memdup
3668 /* Check the return message from a call for validity. */
3670 check_reply_header (guestfs_h *g,
3671 const struct guestfs_message_header *hdr,
3672 int proc_nr, int serial)
3674 if (hdr->prog != GUESTFS_PROGRAM) {
3675 error (g, \"wrong program (%%d/%%d)\", hdr->prog, GUESTFS_PROGRAM);
3678 if (hdr->vers != GUESTFS_PROTOCOL_VERSION) {
3679 error (g, \"wrong protocol version (%%d/%%d)\",
3680 hdr->vers, GUESTFS_PROTOCOL_VERSION);
3683 if (hdr->direction != GUESTFS_DIRECTION_REPLY) {
3684 error (g, \"unexpected message direction (%%d/%%d)\",
3685 hdr->direction, GUESTFS_DIRECTION_REPLY);
3688 if (hdr->proc != proc_nr) {
3689 error (g, \"unexpected procedure number (%%d/%%d)\", hdr->proc, proc_nr);
3692 if (hdr->serial != serial) {
3693 error (g, \"unexpected serial (%%d/%%d)\", hdr->serial, serial);
3700 /* Check we are in the right state to run a high-level action. */
3702 check_state (guestfs_h *g, const char *caller)
3704 if (!guestfs_is_ready (g)) {
3705 if (guestfs_is_config (g))
3706 error (g, \"%%s: call launch() before using this function\",
3708 else if (guestfs_is_launching (g))
3709 error (g, \"%%s: call wait_ready() before using this function\",
3712 error (g, \"%%s called from the wrong state, %%d != READY\",
3713 caller, guestfs_get_state (g));
3721 (* Client-side stubs for each function. *)
3723 fun (shortname, style, _, _, _, _, _) ->
3724 let name = "guestfs_" ^ shortname in
3726 (* Generate the context struct which stores the high-level
3727 * state between callback functions.
3729 pr "struct %s_ctx {\n" shortname;
3730 pr " /* This flag is set by the callbacks, so we know we've done\n";
3731 pr " * the callbacks as expected, and in the right sequence.\n";
3732 pr " * 0 = not called, 1 = reply_cb called.\n";
3734 pr " int cb_sequence;\n";
3735 pr " struct guestfs_message_header hdr;\n";
3736 pr " struct guestfs_message_error err;\n";
3737 (match fst style with
3740 failwithf "RConstString cannot be returned from a daemon function"
3742 | RBool _ | RString _ | RStringList _
3744 | RPVList _ | RVGList _ | RLVList _
3745 | RStat _ | RStatVFS _
3748 pr " struct %s_ret ret;\n" name
3753 (* Generate the reply callback function. *)
3754 pr "static void %s_reply_cb (guestfs_h *g, void *data, XDR *xdr)\n" shortname;
3756 pr " guestfs_main_loop *ml = guestfs_get_main_loop (g);\n";
3757 pr " struct %s_ctx *ctx = (struct %s_ctx *) data;\n" shortname shortname;
3759 pr " /* This should definitely not happen. */\n";
3760 pr " if (ctx->cb_sequence != 0) {\n";
3761 pr " ctx->cb_sequence = 9999;\n";
3762 pr " error (g, \"%%s: internal error: reply callback called twice\", \"%s\");\n" name;
3766 pr " ml->main_loop_quit (ml, g);\n";
3768 pr " if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {\n";
3769 pr " error (g, \"%%s: failed to parse reply header\", \"%s\");\n" name;
3772 pr " if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {\n";
3773 pr " if (!xdr_guestfs_message_error (xdr, &ctx->err)) {\n";
3774 pr " error (g, \"%%s: failed to parse reply error\", \"%s\");\n"
3781 (match fst style with
3784 failwithf "RConstString cannot be returned from a daemon function"
3786 | RBool _ | RString _ | RStringList _
3788 | RPVList _ | RVGList _ | RLVList _
3789 | RStat _ | RStatVFS _
3792 pr " if (!xdr_%s_ret (xdr, &ctx->ret)) {\n" name;
3793 pr " error (g, \"%%s: failed to parse reply\", \"%s\");\n" name;
3799 pr " ctx->cb_sequence = 1;\n";
3802 (* Generate the action stub. *)
3803 generate_prototype ~extern:false ~semicolon:false ~newline:true
3804 ~handle:"g" name style;
3807 match fst style with
3808 | RErr | RInt _ | RInt64 _ | RBool _ -> "-1"
3810 failwithf "RConstString cannot be returned from a daemon function"
3811 | RString _ | RStringList _ | RIntBool _
3812 | RPVList _ | RVGList _ | RLVList _
3813 | RStat _ | RStatVFS _
3820 (match snd style with
3822 | _ -> pr " struct %s_args args;\n" name
3825 pr " struct %s_ctx ctx;\n" shortname;
3826 pr " guestfs_main_loop *ml = guestfs_get_main_loop (g);\n";
3827 pr " int serial;\n";
3829 pr " if (check_state (g, \"%s\") == -1) return %s;\n" name error_code;
3830 pr " guestfs_set_busy (g);\n";
3832 pr " memset (&ctx, 0, sizeof ctx);\n";
3835 (* Send the main header and arguments. *)
3836 (match snd style with
3838 pr " serial = guestfs__send_sync (g, GUESTFS_PROC_%s, NULL, NULL);\n"
3839 (String.uppercase shortname)
3844 pr " args.%s = (char *) %s;\n" n n
3846 pr " args.%s = %s ? (char **) &%s : NULL;\n" n n n
3848 pr " args.%s.%s_val = (char **) %s;\n" n n n;
3849 pr " for (args.%s.%s_len = 0; %s[args.%s.%s_len]; args.%s.%s_len++) ;\n" n n n n n n n;
3851 pr " args.%s = %s;\n" n n
3853 pr " args.%s = %s;\n" n n
3854 | FileIn _ | FileOut _ -> ()
3856 pr " serial = guestfs__send_sync (g, GUESTFS_PROC_%s,\n"
3857 (String.uppercase shortname);
3858 pr " (xdrproc_t) xdr_%s_args, (char *) &args);\n"
3861 pr " if (serial == -1) {\n";
3862 pr " guestfs_end_busy (g);\n";
3863 pr " return %s;\n" error_code;
3867 (* Send any additional files (FileIn) requested. *)
3868 let need_read_reply_label = ref false in
3875 pr " r = guestfs__send_file_sync (g, %s);\n" n;
3876 pr " if (r == -1) {\n";
3877 pr " guestfs_end_busy (g);\n";
3878 pr " return %s;\n" error_code;
3880 pr " if (r == -2) /* daemon cancelled */\n";
3881 pr " goto read_reply;\n";
3882 need_read_reply_label := true;
3888 (* Wait for the reply from the remote end. *)
3889 if !need_read_reply_label then pr " read_reply:\n";
3890 pr " guestfs__switch_to_receiving (g);\n";
3891 pr " ctx.cb_sequence = 0;\n";
3892 pr " guestfs_set_reply_callback (g, %s_reply_cb, &ctx);\n" shortname;
3893 pr " (void) ml->main_loop_run (ml, g);\n";
3894 pr " guestfs_set_reply_callback (g, NULL, NULL);\n";
3895 pr " if (ctx.cb_sequence != 1) {\n";
3896 pr " error (g, \"%%s reply failed, see earlier error messages\", \"%s\");\n" name;
3897 pr " guestfs_end_busy (g);\n";
3898 pr " return %s;\n" error_code;
3902 pr " if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_%s, serial) == -1) {\n"
3903 (String.uppercase shortname);
3904 pr " guestfs_end_busy (g);\n";
3905 pr " return %s;\n" error_code;
3909 pr " if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {\n";
3910 pr " error (g, \"%%s\", ctx.err.error_message);\n";
3911 pr " free (ctx.err.error_message);\n";
3912 pr " guestfs_end_busy (g);\n";
3913 pr " return %s;\n" error_code;
3917 (* Expecting to receive further files (FileOut)? *)
3921 pr " if (guestfs__receive_file_sync (g, %s) == -1) {\n" n;
3922 pr " guestfs_end_busy (g);\n";
3923 pr " return %s;\n" error_code;
3929 pr " guestfs_end_busy (g);\n";
3931 (match fst style with
3932 | RErr -> pr " return 0;\n"
3933 | RInt n | RInt64 n | RBool n ->
3934 pr " return ctx.ret.%s;\n" n
3936 failwithf "RConstString cannot be returned from a daemon function"
3938 pr " return ctx.ret.%s; /* caller will free */\n" n
3939 | RStringList n | RHashtable n ->
3940 pr " /* caller will free this, but we need to add a NULL entry */\n";
3941 pr " ctx.ret.%s.%s_val =\n" n n;
3942 pr " safe_realloc (g, ctx.ret.%s.%s_val,\n" n n;
3943 pr " sizeof (char *) * (ctx.ret.%s.%s_len + 1));\n"
3945 pr " ctx.ret.%s.%s_val[ctx.ret.%s.%s_len] = NULL;\n" n n n n;
3946 pr " return ctx.ret.%s.%s_val;\n" n n
3948 pr " /* caller with free this */\n";
3949 pr " return safe_memdup (g, &ctx.ret, sizeof (ctx.ret));\n"
3950 | RPVList n | RVGList n | RLVList n
3951 | RStat n | RStatVFS n
3953 pr " /* caller will free this */\n";
3954 pr " return safe_memdup (g, &ctx.ret.%s, sizeof (ctx.ret.%s));\n" n n
3960 (* Generate daemon/actions.h. *)
3961 and generate_daemon_actions_h () =
3962 generate_header CStyle GPLv2;
3964 pr "#include \"../src/guestfs_protocol.h\"\n";
3968 fun (name, style, _, _, _, _, _) ->
3970 ~single_line:true ~newline:true ~in_daemon:true ~prefix:"do_"
3974 (* Generate the server-side stubs. *)
3975 and generate_daemon_actions () =
3976 generate_header CStyle GPLv2;
3978 pr "#include <config.h>\n";
3980 pr "#include <stdio.h>\n";
3981 pr "#include <stdlib.h>\n";
3982 pr "#include <string.h>\n";
3983 pr "#include <inttypes.h>\n";
3984 pr "#include <ctype.h>\n";
3985 pr "#include <rpc/types.h>\n";
3986 pr "#include <rpc/xdr.h>\n";
3988 pr "#include \"daemon.h\"\n";
3989 pr "#include \"../src/guestfs_protocol.h\"\n";
3990 pr "#include \"actions.h\"\n";
3994 fun (name, style, _, _, _, _, _) ->
3995 (* Generate server-side stubs. *)
3996 pr "static void %s_stub (XDR *xdr_in)\n" name;
3999 match fst style with
4000 | RErr | RInt _ -> pr " int r;\n"; "-1"
4001 | RInt64 _ -> pr " int64_t r;\n"; "-1"
4002 | RBool _ -> pr " int r;\n"; "-1"
4004 failwithf "RConstString cannot be returned from a daemon function"
4005 | RString _ -> pr " char *r;\n"; "NULL"
4006 | RStringList _ | RHashtable _ -> pr " char **r;\n"; "NULL"
4007 | RIntBool _ -> pr " guestfs_%s_ret *r;\n" name; "NULL"
4008 | RPVList _ -> pr " guestfs_lvm_int_pv_list *r;\n"; "NULL"
4009 | RVGList _ -> pr " guestfs_lvm_int_vg_list *r;\n"; "NULL"
4010 | RLVList _ -> pr " guestfs_lvm_int_lv_list *r;\n"; "NULL"
4011 | RStat _ -> pr " guestfs_int_stat *r;\n"; "NULL"
4012 | RStatVFS _ -> pr " guestfs_int_statvfs *r;\n"; "NULL"
4013 | RDirentList _ -> pr " guestfs_int_dirent_list *r;\n"; "NULL" in
4015 (match snd style with
4018 pr " struct guestfs_%s_args args;\n" name;
4021 (* Note we allow the string to be writable, in order to
4022 * allow device name translation. This is safe because
4023 * we can modify the string (passed from RPC).
4026 | OptString n -> pr " char *%s;\n" n
4027 | StringList n -> pr " char **%s;\n" n
4028 | Bool n -> pr " int %s;\n" n
4029 | Int n -> pr " int %s;\n" n
4030 | FileIn _ | FileOut _ -> ()
4035 (match snd style with
4038 pr " memset (&args, 0, sizeof args);\n";
4040 pr " if (!xdr_guestfs_%s_args (xdr_in, &args)) {\n" name;
4041 pr " reply_with_error (\"%%s: daemon failed to decode procedure arguments\", \"%s\");\n" name;
4046 | String n -> pr " %s = args.%s;\n" n n
4047 | OptString n -> pr " %s = args.%s ? *args.%s : NULL;\n" n n n
4049 pr " %s = realloc (args.%s.%s_val,\n" n n n;
4050 pr " sizeof (char *) * (args.%s.%s_len+1));\n" n n;
4051 pr " if (%s == NULL) {\n" n;
4052 pr " reply_with_perror (\"realloc\");\n";
4055 pr " %s[args.%s.%s_len] = NULL;\n" n n n;
4056 pr " args.%s.%s_val = %s;\n" n n n;
4057 | Bool n -> pr " %s = args.%s;\n" n n
4058 | Int n -> pr " %s = args.%s;\n" n n
4059 | FileIn _ | FileOut _ -> ()
4064 (* Don't want to call the impl with any FileIn or FileOut
4065 * parameters, since these go "outside" the RPC protocol.
4068 List.filter (function FileIn _ | FileOut _ -> false | _ -> true)
4070 pr " r = do_%s " name;
4071 generate_call_args argsnofile;
4074 pr " if (r == %s)\n" error_code;
4075 pr " /* do_%s has already called reply_with_error */\n" name;
4079 (* If there are any FileOut parameters, then the impl must
4080 * send its own reply.
4083 List.exists (function FileOut _ -> true | _ -> false) (snd style) in
4085 pr " /* do_%s has already sent a reply */\n" name
4087 match fst style with
4088 | RErr -> pr " reply (NULL, NULL);\n"
4089 | RInt n | RInt64 n | RBool n ->
4090 pr " struct guestfs_%s_ret ret;\n" name;
4091 pr " ret.%s = r;\n" n;
4092 pr " reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
4095 failwithf "RConstString cannot be returned from a daemon function"
4097 pr " struct guestfs_%s_ret ret;\n" name;
4098 pr " ret.%s = r;\n" n;
4099 pr " reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
4102 | RStringList n | RHashtable n ->
4103 pr " struct guestfs_%s_ret ret;\n" name;
4104 pr " ret.%s.%s_len = count_strings (r);\n" n n;
4105 pr " ret.%s.%s_val = r;\n" n n;
4106 pr " reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
4108 pr " free_strings (r);\n"
4110 pr " reply ((xdrproc_t) xdr_guestfs_%s_ret, (char *) r);\n"
4112 pr " xdr_free ((xdrproc_t) xdr_guestfs_%s_ret, (char *) r);\n" name
4113 | RPVList n | RVGList n | RLVList n
4114 | RStat n | RStatVFS n
4116 pr " struct guestfs_%s_ret ret;\n" name;
4117 pr " ret.%s = *r;\n" n;
4118 pr " reply ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
4120 pr " xdr_free ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
4124 (* Free the args. *)
4125 (match snd style with
4130 pr " xdr_free ((xdrproc_t) xdr_guestfs_%s_args, (char *) &args);\n"
4137 (* Dispatch function. *)
4138 pr "void dispatch_incoming_message (XDR *xdr_in)\n";
4140 pr " switch (proc_nr) {\n";
4143 fun (name, style, _, _, _, _, _) ->
4144 pr " case GUESTFS_PROC_%s:\n" (String.uppercase name);
4145 pr " %s_stub (xdr_in);\n" name;
4150 pr " reply_with_error (\"dispatch_incoming_message: unknown procedure number %%d, set LIBGUESTFS_PATH to point to the matching libguestfs appliance directory\", proc_nr);\n";
4155 (* LVM columns and tokenization functions. *)
4156 (* XXX This generates crap code. We should rethink how we
4162 pr "static const char *lvm_%s_cols = \"%s\";\n"
4163 typ (String.concat "," (List.map fst cols));
4166 pr "static int lvm_tokenize_%s (char *str, struct guestfs_lvm_int_%s *r)\n" typ typ;
4168 pr " char *tok, *p, *next;\n";
4172 pr " fprintf (stderr, \"%%s: <<%%s>>\\n\", __func__, str);\n";
4175 pr " if (!str) {\n";
4176 pr " fprintf (stderr, \"%%s: failed: passed a NULL string\\n\", __func__);\n";
4179 pr " if (!*str || isspace (*str)) {\n";
4180 pr " fprintf (stderr, \"%%s: failed: passed a empty string or one beginning with whitespace\\n\", __func__);\n";
4185 fun (name, coltype) ->
4186 pr " if (!tok) {\n";
4187 pr " fprintf (stderr, \"%%s: failed: string finished early, around token %%s\\n\", __func__, \"%s\");\n" name;
4190 pr " p = strchrnul (tok, ',');\n";
4191 pr " if (*p) next = p+1; else next = NULL;\n";
4192 pr " *p = '\\0';\n";
4195 pr " r->%s = strdup (tok);\n" name;
4196 pr " if (r->%s == NULL) {\n" name;
4197 pr " perror (\"strdup\");\n";
4201 pr " for (i = j = 0; i < 32; ++j) {\n";
4202 pr " if (tok[j] == '\\0') {\n";
4203 pr " fprintf (stderr, \"%%s: failed to parse UUID from '%%s'\\n\", __func__, tok);\n";
4205 pr " } else if (tok[j] != '-')\n";
4206 pr " r->%s[i++] = tok[j];\n" name;
4209 pr " if (sscanf (tok, \"%%\"SCNu64, &r->%s) != 1) {\n" name;
4210 pr " fprintf (stderr, \"%%s: failed to parse size '%%s' from token %%s\\n\", __func__, tok, \"%s\");\n" name;
4214 pr " if (sscanf (tok, \"%%\"SCNi64, &r->%s) != 1) {\n" name;
4215 pr " fprintf (stderr, \"%%s: failed to parse int '%%s' from token %%s\\n\", __func__, tok, \"%s\");\n" name;
4219 pr " if (tok[0] == '\\0')\n";
4220 pr " r->%s = -1;\n" name;
4221 pr " else if (sscanf (tok, \"%%f\", &r->%s) != 1) {\n" name;
4222 pr " fprintf (stderr, \"%%s: failed to parse float '%%s' from token %%s\\n\", __func__, tok, \"%s\");\n" name;
4226 pr " tok = next;\n";
4229 pr " if (tok != NULL) {\n";
4230 pr " fprintf (stderr, \"%%s: failed: extra tokens at end of string\\n\", __func__);\n";
4237 pr "guestfs_lvm_int_%s_list *\n" typ;
4238 pr "parse_command_line_%ss (void)\n" typ;
4240 pr " char *out, *err;\n";
4241 pr " char *p, *pend;\n";
4243 pr " guestfs_lvm_int_%s_list *ret;\n" typ;
4244 pr " void *newp;\n";
4246 pr " ret = malloc (sizeof *ret);\n";
4247 pr " if (!ret) {\n";
4248 pr " reply_with_perror (\"malloc\");\n";
4249 pr " return NULL;\n";
4252 pr " ret->guestfs_lvm_int_%s_list_len = 0;\n" typ;
4253 pr " ret->guestfs_lvm_int_%s_list_val = NULL;\n" typ;
4255 pr " r = command (&out, &err,\n";
4256 pr " \"/sbin/lvm\", \"%ss\",\n" typ;
4257 pr " \"-o\", lvm_%s_cols, \"--unbuffered\", \"--noheadings\",\n" typ;
4258 pr " \"--nosuffix\", \"--separator\", \",\", \"--units\", \"b\", NULL);\n";
4259 pr " if (r == -1) {\n";
4260 pr " reply_with_error (\"%%s\", err);\n";
4261 pr " free (out);\n";
4262 pr " free (err);\n";
4263 pr " free (ret);\n";
4264 pr " return NULL;\n";
4267 pr " free (err);\n";
4269 pr " /* Tokenize each line of the output. */\n";
4272 pr " while (p) {\n";
4273 pr " pend = strchr (p, '\\n'); /* Get the next line of output. */\n";
4274 pr " if (pend) {\n";
4275 pr " *pend = '\\0';\n";
4279 pr " while (*p && isspace (*p)) /* Skip any leading whitespace. */\n";
4282 pr " if (!*p) { /* Empty line? Skip it. */\n";
4287 pr " /* Allocate some space to store this next entry. */\n";
4288 pr " newp = realloc (ret->guestfs_lvm_int_%s_list_val,\n" typ;
4289 pr " sizeof (guestfs_lvm_int_%s) * (i+1));\n" typ;
4290 pr " if (newp == NULL) {\n";
4291 pr " reply_with_perror (\"realloc\");\n";
4292 pr " free (ret->guestfs_lvm_int_%s_list_val);\n" typ;
4293 pr " free (ret);\n";
4294 pr " free (out);\n";
4295 pr " return NULL;\n";
4297 pr " ret->guestfs_lvm_int_%s_list_val = newp;\n" typ;
4299 pr " /* Tokenize the next entry. */\n";
4300 pr " r = lvm_tokenize_%s (p, &ret->guestfs_lvm_int_%s_list_val[i]);\n" typ typ;
4301 pr " if (r == -1) {\n";
4302 pr " reply_with_error (\"failed to parse output of '%ss' command\");\n" typ;
4303 pr " free (ret->guestfs_lvm_int_%s_list_val);\n" typ;
4304 pr " free (ret);\n";
4305 pr " free (out);\n";
4306 pr " return NULL;\n";
4313 pr " ret->guestfs_lvm_int_%s_list_len = i;\n" typ;
4315 pr " free (out);\n";
4316 pr " return ret;\n";
4319 ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols]
4321 (* Generate a list of function names, for debugging in the daemon.. *)
4322 and generate_daemon_names () =
4323 generate_header CStyle GPLv2;
4325 pr "#include <config.h>\n";
4327 pr "#include \"daemon.h\"\n";
4330 pr "/* This array is indexed by proc_nr. See guestfs_protocol.x. */\n";
4331 pr "const char *function_names[] = {\n";
4333 fun (name, _, proc_nr, _, _, _, _) -> pr " [%d] = \"%s\",\n" proc_nr name
4337 (* Generate the tests. *)
4338 and generate_tests () =
4339 generate_header CStyle GPLv2;
4346 #include <sys/types.h>
4349 #include \"guestfs.h\"
4351 static guestfs_h *g;
4352 static int suppress_error = 0;
4354 static void print_error (guestfs_h *g, void *data, const char *msg)
4356 if (!suppress_error)
4357 fprintf (stderr, \"%%s\\n\", msg);
4360 static void print_strings (char * const * const argv)
4364 for (argc = 0; argv[argc] != NULL; ++argc)
4365 printf (\"\\t%%s\\n\", argv[argc]);
4369 static void print_table (char * const * const argv)
4373 for (i = 0; argv[i] != NULL; i += 2)
4374 printf (\"%%s: %%s\\n\", argv[i], argv[i+1]);
4378 static void no_test_warnings (void)
4384 | name, _, _, _, [], _, _ ->
4385 pr " fprintf (stderr, \"warning: \\\"guestfs_%s\\\" has no tests\\n\");\n" name
4386 | name, _, _, _, tests, _, _ -> ()
4392 (* Generate the actual tests. Note that we generate the tests
4393 * in reverse order, deliberately, so that (in general) the
4394 * newest tests run first. This makes it quicker and easier to
4399 fun (name, _, _, _, tests, _, _) ->
4400 mapi (generate_one_test name) tests
4401 ) (List.rev all_functions) in
4402 let test_names = List.concat test_names in
4403 let nr_tests = List.length test_names in
4406 int main (int argc, char *argv[])
4410 const char *filename;
4412 int nr_tests, test_num = 0;
4414 setbuf (stdout, NULL);
4416 no_test_warnings ();
4418 g = guestfs_create ();
4420 printf (\"guestfs_create FAILED\\n\");
4424 guestfs_set_error_handler (g, print_error, NULL);
4426 guestfs_set_path (g, \"../appliance\");
4428 filename = \"test1.img\";
4429 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
4434 if (lseek (fd, %d, SEEK_SET) == -1) {
4440 if (write (fd, &c, 1) == -1) {
4446 if (close (fd) == -1) {
4451 if (guestfs_add_drive (g, filename) == -1) {
4452 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
4456 filename = \"test2.img\";
4457 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
4462 if (lseek (fd, %d, SEEK_SET) == -1) {
4468 if (write (fd, &c, 1) == -1) {
4474 if (close (fd) == -1) {
4479 if (guestfs_add_drive (g, filename) == -1) {
4480 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
4484 filename = \"test3.img\";
4485 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
4490 if (lseek (fd, %d, SEEK_SET) == -1) {
4496 if (write (fd, &c, 1) == -1) {
4502 if (close (fd) == -1) {
4507 if (guestfs_add_drive (g, filename) == -1) {
4508 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
4512 if (guestfs_add_drive_ro (g, \"../images/test.sqsh\") == -1) {
4513 printf (\"guestfs_add_drive_ro ../images/test.sqsh FAILED\\n\");
4517 if (guestfs_launch (g) == -1) {
4518 printf (\"guestfs_launch FAILED\\n\");
4522 /* Set a timeout in case qemu hangs during launch (RHBZ#505329). */
4525 if (guestfs_wait_ready (g) == -1) {
4526 printf (\"guestfs_wait_ready FAILED\\n\");
4530 /* Cancel previous alarm. */
4535 " (500 * 1024 * 1024) (50 * 1024 * 1024) (10 * 1024 * 1024) nr_tests;
4539 pr " test_num++;\n";
4540 pr " printf (\"%%3d/%%3d %s\\n\", test_num, nr_tests);\n" test_name;
4541 pr " if (%s () == -1) {\n" test_name;
4542 pr " printf (\"%s FAILED\\n\");\n" test_name;
4548 pr " guestfs_close (g);\n";
4549 pr " unlink (\"test1.img\");\n";
4550 pr " unlink (\"test2.img\");\n";
4551 pr " unlink (\"test3.img\");\n";
4554 pr " if (failed > 0) {\n";
4555 pr " printf (\"***** %%d / %%d tests FAILED *****\\n\", failed, nr_tests);\n";
4563 and generate_one_test name i (init, prereq, test) =
4564 let test_name = sprintf "test_%s_%d" name i in
4567 static int %s_skip (void)
4571 str = getenv (\"TEST_ONLY\");
4573 return strstr (str, \"%s\") == NULL;
4574 str = getenv (\"SKIP_%s\");
4575 if (str && strcmp (str, \"1\") == 0) return 1;
4576 str = getenv (\"SKIP_TEST_%s\");
4577 if (str && strcmp (str, \"1\") == 0) return 1;
4581 " test_name name (String.uppercase test_name) (String.uppercase name);
4584 | Disabled | Always -> ()
4585 | If code | Unless code ->
4586 pr "static int %s_prereq (void)\n" test_name;
4594 static int %s (void)
4597 printf (\" %%s skipped (reason: environment variable set)\\n\", \"%s\");
4601 " test_name test_name test_name;
4605 pr " printf (\" %%s skipped (reason: test disabled in generator)\\n\", \"%s\");\n" test_name
4607 pr " if (! %s_prereq ()) {\n" test_name;
4608 pr " printf (\" %%s skipped (reason: test prerequisite)\\n\", \"%s\");\n" test_name;
4612 generate_one_test_body name i test_name init test;
4614 pr " if (%s_prereq ()) {\n" test_name;
4615 pr " printf (\" %%s skipped (reason: test prerequisite)\\n\", \"%s\");\n" test_name;
4619 generate_one_test_body name i test_name init test;
4621 generate_one_test_body name i test_name init test
4629 and generate_one_test_body name i test_name init test =
4633 pr " /* InitNone|InitEmpty for %s */\n" test_name;
4634 List.iter (generate_test_command_call test_name)
4635 [["blockdev_setrw"; "/dev/sda"];
4639 pr " /* InitBasicFS for %s: create ext2 on /dev/sda1 */\n" test_name;
4640 List.iter (generate_test_command_call test_name)
4641 [["blockdev_setrw"; "/dev/sda"];
4644 ["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
4645 ["mkfs"; "ext2"; "/dev/sda1"];
4646 ["mount"; "/dev/sda1"; "/"]]
4647 | InitBasicFSonLVM ->
4648 pr " /* InitBasicFSonLVM for %s: create ext2 on /dev/VG/LV */\n"
4650 List.iter (generate_test_command_call test_name)
4651 [["blockdev_setrw"; "/dev/sda"];
4654 ["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
4655 ["pvcreate"; "/dev/sda1"];
4656 ["vgcreate"; "VG"; "/dev/sda1"];
4657 ["lvcreate"; "LV"; "VG"; "8"];
4658 ["mkfs"; "ext2"; "/dev/VG/LV"];
4659 ["mount"; "/dev/VG/LV"; "/"]]
4662 let get_seq_last = function
4664 failwithf "%s: you cannot use [] (empty list) when expecting a command"
4667 let seq = List.rev seq in
4668 List.rev (List.tl seq), List.hd seq
4673 pr " /* TestRun for %s (%d) */\n" name i;
4674 List.iter (generate_test_command_call test_name) seq
4675 | TestOutput (seq, expected) ->
4676 pr " /* TestOutput for %s (%d) */\n" name i;
4677 pr " char expected[] = \"%s\";\n" (c_quote expected);
4678 let seq, last = get_seq_last seq in
4680 pr " if (strcmp (r, expected) != 0) {\n";
4681 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name;
4685 List.iter (generate_test_command_call test_name) seq;
4686 generate_test_command_call ~test test_name last
4687 | TestOutputList (seq, expected) ->
4688 pr " /* TestOutputList for %s (%d) */\n" name i;
4689 let seq, last = get_seq_last seq in
4693 pr " if (!r[%d]) {\n" i;
4694 pr " fprintf (stderr, \"%s: short list returned from command\\n\");\n" test_name;
4695 pr " print_strings (r);\n";
4699 pr " char expected[] = \"%s\";\n" (c_quote str);
4700 pr " if (strcmp (r[%d], expected) != 0) {\n" i;
4701 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r[%d]);\n" test_name i;
4706 pr " if (r[%d] != NULL) {\n" (List.length expected);
4707 pr " fprintf (stderr, \"%s: extra elements returned from command\\n\");\n"
4709 pr " print_strings (r);\n";
4713 List.iter (generate_test_command_call test_name) seq;
4714 generate_test_command_call ~test test_name last
4715 | TestOutputListOfDevices (seq, expected) ->
4716 pr " /* TestOutputListOfDevices for %s (%d) */\n" name i;
4717 let seq, last = get_seq_last seq in
4721 pr " if (!r[%d]) {\n" i;
4722 pr " fprintf (stderr, \"%s: short list returned from command\\n\");\n" test_name;
4723 pr " print_strings (r);\n";
4727 pr " char expected[] = \"%s\";\n" (c_quote str);
4728 pr " r[%d][5] = 's';\n" i;
4729 pr " if (strcmp (r[%d], expected) != 0) {\n" i;
4730 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r[%d]);\n" test_name i;
4735 pr " if (r[%d] != NULL) {\n" (List.length expected);
4736 pr " fprintf (stderr, \"%s: extra elements returned from command\\n\");\n"
4738 pr " print_strings (r);\n";
4742 List.iter (generate_test_command_call test_name) seq;
4743 generate_test_command_call ~test test_name last
4744 | TestOutputInt (seq, expected) ->
4745 pr " /* TestOutputInt for %s (%d) */\n" name i;
4746 let seq, last = get_seq_last seq in
4748 pr " if (r != %d) {\n" expected;
4749 pr " fprintf (stderr, \"%s: expected %d but got %%d\\n\","
4755 List.iter (generate_test_command_call test_name) seq;
4756 generate_test_command_call ~test test_name last
4757 | TestOutputTrue seq ->
4758 pr " /* TestOutputTrue for %s (%d) */\n" name i;
4759 let seq, last = get_seq_last seq in
4762 pr " fprintf (stderr, \"%s: expected true, got false\\n\");\n"
4767 List.iter (generate_test_command_call test_name) seq;
4768 generate_test_command_call ~test test_name last
4769 | TestOutputFalse seq ->
4770 pr " /* TestOutputFalse for %s (%d) */\n" name i;
4771 let seq, last = get_seq_last seq in
4774 pr " fprintf (stderr, \"%s: expected false, got true\\n\");\n"
4779 List.iter (generate_test_command_call test_name) seq;
4780 generate_test_command_call ~test test_name last
4781 | TestOutputLength (seq, expected) ->
4782 pr " /* TestOutputLength for %s (%d) */\n" name i;
4783 let seq, last = get_seq_last seq in
4786 pr " for (j = 0; j < %d; ++j)\n" expected;
4787 pr " if (r[j] == NULL) {\n";
4788 pr " fprintf (stderr, \"%s: short list returned\\n\");\n"
4790 pr " print_strings (r);\n";
4793 pr " if (r[j] != NULL) {\n";
4794 pr " fprintf (stderr, \"%s: long list returned\\n\");\n"
4796 pr " print_strings (r);\n";
4800 List.iter (generate_test_command_call test_name) seq;
4801 generate_test_command_call ~test test_name last
4802 | TestOutputStruct (seq, checks) ->
4803 pr " /* TestOutputStruct for %s (%d) */\n" name i;
4804 let seq, last = get_seq_last seq in
4808 | CompareWithInt (field, expected) ->
4809 pr " if (r->%s != %d) {\n" field expected;
4810 pr " fprintf (stderr, \"%s: %s was %%d, expected %d\\n\",\n"
4811 test_name field expected;
4812 pr " (int) r->%s);\n" field;
4815 | CompareWithString (field, expected) ->
4816 pr " if (strcmp (r->%s, \"%s\") != 0) {\n" field expected;
4817 pr " fprintf (stderr, \"%s: %s was \"%%s\", expected \"%s\"\\n\",\n"
4818 test_name field expected;
4819 pr " r->%s);\n" field;
4822 | CompareFieldsIntEq (field1, field2) ->
4823 pr " if (r->%s != r->%s) {\n" field1 field2;
4824 pr " fprintf (stderr, \"%s: %s (%%d) <> %s (%%d)\\n\",\n"
4825 test_name field1 field2;
4826 pr " (int) r->%s, (int) r->%s);\n" field1 field2;
4829 | CompareFieldsStrEq (field1, field2) ->
4830 pr " if (strcmp (r->%s, r->%s) != 0) {\n" field1 field2;
4831 pr " fprintf (stderr, \"%s: %s (\"%%s\") <> %s (\"%%s\")\\n\",\n"
4832 test_name field1 field2;
4833 pr " r->%s, r->%s);\n" field1 field2;
4838 List.iter (generate_test_command_call test_name) seq;
4839 generate_test_command_call ~test test_name last
4840 | TestLastFail seq ->
4841 pr " /* TestLastFail for %s (%d) */\n" name i;
4842 let seq, last = get_seq_last seq in
4843 List.iter (generate_test_command_call test_name) seq;
4844 generate_test_command_call test_name ~expect_error:true last
4846 (* Generate the code to run a command, leaving the result in 'r'.
4847 * If you expect to get an error then you should set expect_error:true.
4849 and generate_test_command_call ?(expect_error = false) ?test test_name cmd =
4851 | [] -> assert false
4853 (* Look up the command to find out what args/ret it has. *)
4856 let _, style, _, _, _, _, _ =
4857 List.find (fun (n, _, _, _, _, _, _) -> n = name) all_functions in
4860 failwithf "%s: in test, command %s was not found" test_name name in
4862 if List.length (snd style) <> List.length args then
4863 failwithf "%s: in test, wrong number of args given to %s"
4870 | OptString n, "NULL" -> ()
4872 | OptString n, arg ->
4873 pr " char %s[] = \"%s\";\n" n (c_quote arg);
4876 | FileIn _, _ | FileOut _, _ -> ()
4877 | StringList n, arg ->
4878 let strs = string_split " " arg in
4881 pr " char %s_%d[] = \"%s\";\n" n i (c_quote str);
4883 pr " char *%s[] = {\n" n;
4885 fun i _ -> pr " %s_%d,\n" n i
4889 ) (List.combine (snd style) args);
4892 match fst style with
4893 | RErr | RInt _ | RBool _ -> pr " int r;\n"; "-1"
4894 | RInt64 _ -> pr " int64_t r;\n"; "-1"
4895 | RConstString _ -> pr " const char *r;\n"; "NULL"
4896 | RString _ -> pr " char *r;\n"; "NULL"
4897 | RStringList _ | RHashtable _ ->
4902 pr " struct guestfs_int_bool *r;\n"; "NULL"
4904 pr " struct guestfs_lvm_pv_list *r;\n"; "NULL"
4906 pr " struct guestfs_lvm_vg_list *r;\n"; "NULL"
4908 pr " struct guestfs_lvm_lv_list *r;\n"; "NULL"
4910 pr " struct guestfs_stat *r;\n"; "NULL"
4912 pr " struct guestfs_statvfs *r;\n"; "NULL"
4914 pr " struct guestfs_dirent_list *r;\n"; "NULL" in
4916 pr " suppress_error = %d;\n" (if expect_error then 1 else 0);
4917 pr " r = guestfs_%s (g" name;
4919 (* Generate the parameters. *)
4922 | OptString _, "NULL" -> pr ", NULL"
4926 | FileIn _, arg | FileOut _, arg ->
4927 pr ", \"%s\"" (c_quote arg)
4928 | StringList n, _ ->
4932 try int_of_string arg
4933 with Failure "int_of_string" ->
4934 failwithf "%s: expecting an int, but got '%s'" test_name arg in
4937 let b = bool_of_string arg in pr ", %d" (if b then 1 else 0)
4938 ) (List.combine (snd style) args);
4941 if not expect_error then
4942 pr " if (r == %s)\n" error_code
4944 pr " if (r != %s)\n" error_code;
4947 (* Insert the test code. *)
4953 (match fst style with
4954 | RErr | RInt _ | RInt64 _ | RBool _ | RConstString _ -> ()
4955 | RString _ -> pr " free (r);\n"
4956 | RStringList _ | RHashtable _ ->
4957 pr " for (i = 0; r[i] != NULL; ++i)\n";
4958 pr " free (r[i]);\n";
4961 pr " guestfs_free_int_bool (r);\n"
4963 pr " guestfs_free_lvm_pv_list (r);\n"
4965 pr " guestfs_free_lvm_vg_list (r);\n"
4967 pr " guestfs_free_lvm_lv_list (r);\n"
4968 | RStat _ | RStatVFS _ ->
4971 pr " guestfs_free_dirent_list (r);\n"
4977 let str = replace_str str "\r" "\\r" in
4978 let str = replace_str str "\n" "\\n" in
4979 let str = replace_str str "\t" "\\t" in
4980 let str = replace_str str "\000" "\\0" in
4983 (* Generate a lot of different functions for guestfish. *)
4984 and generate_fish_cmds () =
4985 generate_header CStyle GPLv2;
4989 fun (_, _, _, flags, _, _, _) -> not (List.mem NotInFish flags)
4991 let all_functions_sorted =
4993 fun (_, _, _, flags, _, _, _) -> not (List.mem NotInFish flags)
4994 ) all_functions_sorted in
4996 pr "#include <stdio.h>\n";
4997 pr "#include <stdlib.h>\n";
4998 pr "#include <string.h>\n";
4999 pr "#include <inttypes.h>\n";
5001 pr "#include <guestfs.h>\n";
5002 pr "#include \"fish.h\"\n";
5005 (* list_commands function, which implements guestfish -h *)
5006 pr "void list_commands (void)\n";
5008 pr " printf (\" %%-16s %%s\\n\", \"Command\", \"Description\");\n";
5009 pr " list_builtin_commands ();\n";
5011 fun (name, _, _, flags, _, shortdesc, _) ->
5012 let name = replace_char name '_' '-' in
5013 pr " printf (\"%%-20s %%s\\n\", \"%s\", \"%s\");\n"
5015 ) all_functions_sorted;
5016 pr " printf (\" Use -h <cmd> / help <cmd> to show detailed help for a command.\\n\");\n";
5020 (* display_command function, which implements guestfish -h cmd *)
5021 pr "void display_command (const char *cmd)\n";
5024 fun (name, style, _, flags, _, shortdesc, longdesc) ->
5025 let name2 = replace_char name '_' '-' in
5027 try find_map (function FishAlias n -> Some n | _ -> None) flags
5028 with Not_found -> name in
5029 let longdesc = replace_str longdesc "C<guestfs_" "C<" in
5031 match snd style with
5035 name2 (String.concat "> <" (List.map name_of_argt args)) in
5038 if List.mem ProtocolLimitWarning flags then
5039 ("\n\n" ^ protocol_limit_warning)
5042 (* For DangerWillRobinson commands, we should probably have
5043 * guestfish prompt before allowing you to use them (especially
5044 * in interactive mode). XXX
5048 if List.mem DangerWillRobinson flags then
5049 ("\n\n" ^ danger_will_robinson)
5052 let describe_alias =
5053 if name <> alias then
5054 sprintf "\n\nYou can use '%s' as an alias for this command." alias
5058 pr "strcasecmp (cmd, \"%s\") == 0" name;
5059 if name <> name2 then
5060 pr " || strcasecmp (cmd, \"%s\") == 0" name2;
5061 if name <> alias then
5062 pr " || strcasecmp (cmd, \"%s\") == 0" alias;
5064 pr " pod2text (\"%s - %s\", %S);\n"
5066 (" " ^ synopsis ^ "\n\n" ^ longdesc ^ warnings ^ describe_alias);
5069 pr " display_builtin_command (cmd);\n";
5073 (* print_{pv,vg,lv}_list functions *)
5077 pr "static void print_%s (struct guestfs_lvm_%s *%s)\n" typ typ typ;
5084 pr " printf (\"%s: %%s\\n\", %s->%s);\n" name typ name
5086 pr " printf (\"%s: \");\n" name;
5087 pr " for (i = 0; i < 32; ++i)\n";
5088 pr " printf (\"%%c\", %s->%s[i]);\n" typ name;
5089 pr " printf (\"\\n\");\n"
5091 pr " printf (\"%s: %%\" PRIu64 \"\\n\", %s->%s);\n" name typ name
5093 pr " printf (\"%s: %%\" PRIi64 \"\\n\", %s->%s);\n" name typ name
5094 | name, `OptPercent ->
5095 pr " if (%s->%s >= 0) printf (\"%s: %%g %%%%\\n\", %s->%s);\n"
5096 typ name name typ name;
5097 pr " else printf (\"%s: \\n\");\n" name
5101 pr "static void print_%s_list (struct guestfs_lvm_%s_list *%ss)\n"
5106 pr " for (i = 0; i < %ss->len; ++i)\n" typ;
5107 pr " print_%s (&%ss->val[i]);\n" typ typ;
5110 ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols];
5112 (* print_{stat,statvfs} functions *)
5116 pr "static void print_%s (struct guestfs_%s *%s)\n" typ typ typ;
5121 pr " printf (\"%s: %%\" PRIi64 \"\\n\", %s->%s);\n" name typ name
5125 ) ["stat", stat_cols; "statvfs", statvfs_cols];
5127 (* print_dirent_list function *)
5128 pr "static void print_dirent (struct guestfs_dirent *dirent)\n";
5133 pr " printf (\"%s: %%s\\n\", dirent->%s);\n" name name
5135 pr " printf (\"%s: %%\" PRIi64 \"\\n\", dirent->%s);\n" name name
5137 pr " printf (\"%s: %%c\\n\", dirent->%s);\n" name name
5141 pr "static void print_dirent_list (struct guestfs_dirent_list *dirents)\n";
5145 pr " for (i = 0; i < dirents->len; ++i)\n";
5146 pr " print_dirent (&dirents->val[i]);\n";
5150 (* run_<action> actions *)
5152 fun (name, style, _, flags, _, _, _) ->
5153 pr "static int run_%s (const char *cmd, int argc, char *argv[])\n" name;
5155 (match fst style with
5158 | RBool _ -> pr " int r;\n"
5159 | RInt64 _ -> pr " int64_t r;\n"
5160 | RConstString _ -> pr " const char *r;\n"
5161 | RString _ -> pr " char *r;\n"
5162 | RStringList _ | RHashtable _ -> pr " char **r;\n"
5163 | RIntBool _ -> pr " struct guestfs_int_bool *r;\n"
5164 | RPVList _ -> pr " struct guestfs_lvm_pv_list *r;\n"
5165 | RVGList _ -> pr " struct guestfs_lvm_vg_list *r;\n"
5166 | RLVList _ -> pr " struct guestfs_lvm_lv_list *r;\n"
5167 | RStat _ -> pr " struct guestfs_stat *r;\n"
5168 | RStatVFS _ -> pr " struct guestfs_statvfs *r;\n"
5169 | RDirentList _ -> pr " struct guestfs_dirent_list *r;\n"
5176 | FileOut n -> pr " const char *%s;\n" n
5177 | StringList n -> pr " char **%s;\n" n
5178 | Bool n -> pr " int %s;\n" n
5179 | Int n -> pr " int %s;\n" n
5182 (* Check and convert parameters. *)
5183 let argc_expected = List.length (snd style) in
5184 pr " if (argc != %d) {\n" argc_expected;
5185 pr " fprintf (stderr, \"%%s should have %d parameter(s)\\n\", cmd);\n"
5187 pr " fprintf (stderr, \"type 'help %%s' for help on %%s\\n\", cmd, cmd);\n";
5193 | String name -> pr " %s = argv[%d];\n" name i
5195 pr " %s = strcmp (argv[%d], \"\") != 0 ? argv[%d] : NULL;\n"
5198 pr " %s = strcmp (argv[%d], \"-\") != 0 ? argv[%d] : \"/dev/stdin\";\n"
5201 pr " %s = strcmp (argv[%d], \"-\") != 0 ? argv[%d] : \"/dev/stdout\";\n"
5203 | StringList name ->
5204 pr " %s = parse_string_list (argv[%d]);\n" name i
5206 pr " %s = is_true (argv[%d]) ? 1 : 0;\n" name i
5208 pr " %s = atoi (argv[%d]);\n" name i
5211 (* Call C API function. *)
5213 try find_map (function FishAction n -> Some n | _ -> None) flags
5214 with Not_found -> sprintf "guestfs_%s" name in
5216 generate_call_args ~handle:"g" (snd style);
5219 (* Check return value for errors and display command results. *)
5220 (match fst style with
5221 | RErr -> pr " return r;\n"
5223 pr " if (r == -1) return -1;\n";
5224 pr " printf (\"%%d\\n\", r);\n";
5227 pr " if (r == -1) return -1;\n";
5228 pr " printf (\"%%\" PRIi64 \"\\n\", r);\n";
5231 pr " if (r == -1) return -1;\n";
5232 pr " if (r) printf (\"true\\n\"); else printf (\"false\\n\");\n";
5235 pr " if (r == NULL) return -1;\n";
5236 pr " printf (\"%%s\\n\", r);\n";
5239 pr " if (r == NULL) return -1;\n";
5240 pr " printf (\"%%s\\n\", r);\n";
5244 pr " if (r == NULL) return -1;\n";
5245 pr " print_strings (r);\n";
5246 pr " free_strings (r);\n";
5249 pr " if (r == NULL) return -1;\n";
5250 pr " printf (\"%%d, %%s\\n\", r->i,\n";
5251 pr " r->b ? \"true\" : \"false\");\n";
5252 pr " guestfs_free_int_bool (r);\n";
5255 pr " if (r == NULL) return -1;\n";
5256 pr " print_pv_list (r);\n";
5257 pr " guestfs_free_lvm_pv_list (r);\n";
5260 pr " if (r == NULL) return -1;\n";
5261 pr " print_vg_list (r);\n";
5262 pr " guestfs_free_lvm_vg_list (r);\n";
5265 pr " if (r == NULL) return -1;\n";
5266 pr " print_lv_list (r);\n";
5267 pr " guestfs_free_lvm_lv_list (r);\n";
5270 pr " if (r == NULL) return -1;\n";
5271 pr " print_stat (r);\n";
5275 pr " if (r == NULL) return -1;\n";
5276 pr " print_statvfs (r);\n";
5280 pr " if (r == NULL) return -1;\n";
5281 pr " print_table (r);\n";
5282 pr " free_strings (r);\n";
5285 pr " if (r == NULL) return -1;\n";
5286 pr " print_dirent_list (r);\n";
5287 pr " guestfs_free_dirent_list (r);\n";
5294 (* run_action function *)
5295 pr "int run_action (const char *cmd, int argc, char *argv[])\n";
5298 fun (name, _, _, flags, _, _, _) ->
5299 let name2 = replace_char name '_' '-' in
5301 try find_map (function FishAlias n -> Some n | _ -> None) flags
5302 with Not_found -> name in
5304 pr "strcasecmp (cmd, \"%s\") == 0" name;
5305 if name <> name2 then
5306 pr " || strcasecmp (cmd, \"%s\") == 0" name2;
5307 if name <> alias then
5308 pr " || strcasecmp (cmd, \"%s\") == 0" alias;
5310 pr " return run_%s (cmd, argc, argv);\n" name;
5314 pr " fprintf (stderr, \"%%s: unknown command\\n\", cmd);\n";
5321 (* Readline completion for guestfish. *)
5322 and generate_fish_completion () =
5323 generate_header CStyle GPLv2;
5327 fun (_, _, _, flags, _, _, _) -> not (List.mem NotInFish flags)
5337 #ifdef HAVE_LIBREADLINE
5338 #include <readline/readline.h>
5343 #ifdef HAVE_LIBREADLINE
5345 static const char *const commands[] = {
5346 BUILTIN_COMMANDS_FOR_COMPLETION,
5349 (* Get the commands, including the aliases. They don't need to be
5350 * sorted - the generator() function just does a dumb linear search.
5354 fun (name, _, _, flags, _, _, _) ->
5355 let name2 = replace_char name '_' '-' in
5357 try find_map (function FishAlias n -> Some n | _ -> None) flags
5358 with Not_found -> name in
5360 if name <> alias then [name2; alias] else [name2]
5362 let commands = List.flatten commands in
5364 List.iter (pr " \"%s\",\n") commands;
5370 generator (const char *text, int state)
5372 static int index, len;
5377 len = strlen (text);
5380 rl_attempted_completion_over = 1;
5382 while ((name = commands[index]) != NULL) {
5384 if (strncasecmp (name, text, len) == 0)
5385 return strdup (name);
5391 #endif /* HAVE_LIBREADLINE */
5393 char **do_completion (const char *text, int start, int end)
5395 char **matches = NULL;
5397 #ifdef HAVE_LIBREADLINE
5398 rl_completion_append_character = ' ';
5401 matches = rl_completion_matches (text, generator);
5402 else if (complete_dest_paths)
5403 matches = rl_completion_matches (text, complete_dest_paths_generator);
5410 (* Generate the POD documentation for guestfish. *)
5411 and generate_fish_actions_pod () =
5412 let all_functions_sorted =
5414 fun (_, _, _, flags, _, _, _) ->
5415 not (List.mem NotInFish flags || List.mem NotInDocs flags)
5416 ) all_functions_sorted in
5418 let rex = Str.regexp "C<guestfs_\\([^>]+\\)>" in
5421 fun (name, style, _, flags, _, _, longdesc) ->
5423 Str.global_substitute rex (
5426 try Str.matched_group 1 s
5428 failwithf "error substituting C<guestfs_...> in longdesc of function %s" name in
5429 "C<" ^ replace_char sub '_' '-' ^ ">"
5431 let name = replace_char name '_' '-' in
5433 try find_map (function FishAlias n -> Some n | _ -> None) flags
5434 with Not_found -> name in
5436 pr "=head2 %s" name;
5437 if name <> alias then
5444 | String n -> pr " %s" n
5445 | OptString n -> pr " %s" n
5446 | StringList n -> pr " '%s ...'" n
5447 | Bool _ -> pr " true|false"
5448 | Int n -> pr " %s" n
5449 | FileIn n | FileOut n -> pr " (%s|-)" n
5453 pr "%s\n\n" longdesc;
5455 if List.exists (function FileIn _ | FileOut _ -> true
5456 | _ -> false) (snd style) then
5457 pr "Use C<-> instead of a filename to read/write from stdin/stdout.\n\n";
5459 if List.mem ProtocolLimitWarning flags then
5460 pr "%s\n\n" protocol_limit_warning;
5462 if List.mem DangerWillRobinson flags then
5463 pr "%s\n\n" danger_will_robinson
5464 ) all_functions_sorted
5466 (* Generate a C function prototype. *)
5467 and generate_prototype ?(extern = true) ?(static = false) ?(semicolon = true)
5468 ?(single_line = false) ?(newline = false) ?(in_daemon = false)
5470 ?handle name style =
5471 if extern then pr "extern ";
5472 if static then pr "static ";
5473 (match fst style with
5475 | RInt _ -> pr "int "
5476 | RInt64 _ -> pr "int64_t "
5477 | RBool _ -> pr "int "
5478 | RConstString _ -> pr "const char *"
5479 | RString _ -> pr "char *"
5480 | RStringList _ | RHashtable _ -> pr "char **"
5482 if not in_daemon then pr "struct guestfs_int_bool *"
5483 else pr "guestfs_%s_ret *" name
5485 if not in_daemon then pr "struct guestfs_lvm_pv_list *"
5486 else pr "guestfs_lvm_int_pv_list *"
5488 if not in_daemon then pr "struct guestfs_lvm_vg_list *"
5489 else pr "guestfs_lvm_int_vg_list *"
5491 if not in_daemon then pr "struct guestfs_lvm_lv_list *"
5492 else pr "guestfs_lvm_int_lv_list *"
5494 if not in_daemon then pr "struct guestfs_stat *"
5495 else pr "guestfs_int_stat *"
5497 if not in_daemon then pr "struct guestfs_statvfs *"
5498 else pr "guestfs_int_statvfs *"
5500 if not in_daemon then pr "struct guestfs_dirent_list *"
5501 else pr "guestfs_int_dirent_list *"
5503 pr "%s%s (" prefix name;
5504 if handle = None && List.length (snd style) = 0 then
5507 let comma = ref false in
5510 | Some handle -> pr "guestfs_h *%s" handle; comma := true
5514 if single_line then pr ", " else pr ",\n\t\t"
5523 if not in_daemon then pr "const char *%s" n
5524 else pr "char *%s" n
5527 if not in_daemon then pr "char * const* const %s" n
5528 else pr "char **%s" n
5529 | Bool n -> next (); pr "int %s" n
5530 | Int n -> next (); pr "int %s" n
5533 if not in_daemon then (next (); pr "const char *%s" n)
5537 if semicolon then pr ";";
5538 if newline then pr "\n"
5540 (* Generate C call arguments, eg "(handle, foo, bar)" *)
5541 and generate_call_args ?handle args =
5543 let comma = ref false in
5546 | Some handle -> pr "%s" handle; comma := true
5550 if !comma then pr ", ";
5552 pr "%s" (name_of_argt arg)
5556 (* Generate the OCaml bindings interface. *)
5557 and generate_ocaml_mli () =
5558 generate_header OCamlStyle LGPLv2;
5561 (** For API documentation you should refer to the C API
5562 in the guestfs(3) manual page. The OCaml API uses almost
5563 exactly the same calls. *)
5566 (** A [guestfs_h] handle. *)
5568 exception Error of string
5569 (** This exception is raised when there is an error. *)
5571 val create : unit -> t
5573 val close : t -> unit
5574 (** Handles are closed by the garbage collector when they become
5575 unreferenced, but callers can also call this in order to
5576 provide predictable cleanup. *)
5579 generate_ocaml_lvm_structure_decls ();
5581 generate_ocaml_stat_structure_decls ();
5583 generate_ocaml_dirent_structure_decls ();
5587 fun (name, style, _, _, _, shortdesc, _) ->
5588 generate_ocaml_prototype name style;
5589 pr "(** %s *)\n" shortdesc;
5593 (* Generate the OCaml bindings implementation. *)
5594 and generate_ocaml_ml () =
5595 generate_header OCamlStyle LGPLv2;
5599 exception Error of string
5600 external create : unit -> t = \"ocaml_guestfs_create\"
5601 external close : t -> unit = \"ocaml_guestfs_close\"
5604 Callback.register_exception \"ocaml_guestfs_error\" (Error \"\")
5608 generate_ocaml_lvm_structure_decls ();
5610 generate_ocaml_stat_structure_decls ();
5612 generate_ocaml_dirent_structure_decls ();
5616 fun (name, style, _, _, _, shortdesc, _) ->
5617 generate_ocaml_prototype ~is_external:true name style;
5620 (* Generate the OCaml bindings C implementation. *)
5621 and generate_ocaml_c () =
5622 generate_header CStyle LGPLv2;
5629 #include <caml/config.h>
5630 #include <caml/alloc.h>
5631 #include <caml/callback.h>
5632 #include <caml/fail.h>
5633 #include <caml/memory.h>
5634 #include <caml/mlvalues.h>
5635 #include <caml/signals.h>
5637 #include <guestfs.h>
5639 #include \"guestfs_c.h\"
5641 /* Copy a hashtable of string pairs into an assoc-list. We return
5642 * the list in reverse order, but hashtables aren't supposed to be
5645 static CAMLprim value
5646 copy_table (char * const * argv)
5649 CAMLlocal5 (rv, pairv, kv, vv, cons);
5653 for (i = 0; argv[i] != NULL; i += 2) {
5654 kv = caml_copy_string (argv[i]);
5655 vv = caml_copy_string (argv[i+1]);
5656 pairv = caml_alloc (2, 0);
5657 Store_field (pairv, 0, kv);
5658 Store_field (pairv, 1, vv);
5659 cons = caml_alloc (2, 0);
5660 Store_field (cons, 1, rv);
5662 Store_field (cons, 0, pairv);
5670 (* LVM struct copy functions. *)
5673 let has_optpercent_col =
5674 List.exists (function (_, `OptPercent) -> true | _ -> false) cols in
5676 pr "static CAMLprim value\n";
5677 pr "copy_lvm_%s (const struct guestfs_lvm_%s *%s)\n" typ typ typ;
5679 pr " CAMLparam0 ();\n";
5680 if has_optpercent_col then
5681 pr " CAMLlocal3 (rv, v, v2);\n"
5683 pr " CAMLlocal2 (rv, v);\n";
5685 pr " rv = caml_alloc (%d, 0);\n" (List.length cols);
5690 pr " v = caml_copy_string (%s->%s);\n" typ name
5692 pr " v = caml_alloc_string (32);\n";
5693 pr " memcpy (String_val (v), %s->%s, 32);\n" typ name
5696 pr " v = caml_copy_int64 (%s->%s);\n" typ name
5697 | name, `OptPercent ->
5698 pr " if (%s->%s >= 0) { /* Some %s */\n" typ name name;
5699 pr " v2 = caml_copy_double (%s->%s);\n" typ name;
5700 pr " v = caml_alloc (1, 0);\n";
5701 pr " Store_field (v, 0, v2);\n";
5702 pr " } else /* None */\n";
5703 pr " v = Val_int (0);\n";
5705 pr " Store_field (rv, %d, v);\n" i
5707 pr " CAMLreturn (rv);\n";
5711 pr "static CAMLprim value\n";
5712 pr "copy_lvm_%s_list (const struct guestfs_lvm_%s_list *%ss)\n"
5715 pr " CAMLparam0 ();\n";
5716 pr " CAMLlocal2 (rv, v);\n";
5719 pr " if (%ss->len == 0)\n" typ;
5720 pr " CAMLreturn (Atom (0));\n";
5722 pr " rv = caml_alloc (%ss->len, 0);\n" typ;
5723 pr " for (i = 0; i < %ss->len; ++i) {\n" typ;
5724 pr " v = copy_lvm_%s (&%ss->val[i]);\n" typ typ;
5725 pr " caml_modify (&Field (rv, i), v);\n";
5727 pr " CAMLreturn (rv);\n";
5731 ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols];
5733 (* Stat copy functions. *)
5736 pr "static CAMLprim value\n";
5737 pr "copy_%s (const struct guestfs_%s *%s)\n" typ typ typ;
5739 pr " CAMLparam0 ();\n";
5740 pr " CAMLlocal2 (rv, v);\n";
5742 pr " rv = caml_alloc (%d, 0);\n" (List.length cols);
5747 pr " v = caml_copy_int64 (%s->%s);\n" typ name
5749 pr " Store_field (rv, %d, v);\n" i
5751 pr " CAMLreturn (rv);\n";
5754 ) ["stat", stat_cols; "statvfs", statvfs_cols];
5756 (* Dirent copy functions. *)
5757 pr "static CAMLprim value\n";
5758 pr "copy_dirent (const struct guestfs_dirent *dirent)\n";
5760 pr " CAMLparam0 ();\n";
5761 pr " CAMLlocal2 (rv, v);\n";
5763 pr " rv = caml_alloc (%d, 0);\n" (List.length dirent_cols);
5768 pr " v = caml_copy_string (dirent->%s);\n" name
5770 pr " v = caml_copy_int64 (dirent->%s);\n" name
5772 pr " v = Val_int (dirent->%s);\n" name
5774 pr " Store_field (rv, %d, v);\n" i
5776 pr " CAMLreturn (rv);\n";
5780 pr "static CAMLprim value\n";
5781 pr "copy_dirent_list (const struct guestfs_dirent_list *dirents)\n";
5783 pr " CAMLparam0 ();\n";
5784 pr " CAMLlocal2 (rv, v);\n";
5787 pr " if (dirents->len == 0)\n";
5788 pr " CAMLreturn (Atom (0));\n";
5790 pr " rv = caml_alloc (dirents->len, 0);\n";
5791 pr " for (i = 0; i < dirents->len; ++i) {\n";
5792 pr " v = copy_dirent (&dirents->val[i]);\n";
5793 pr " caml_modify (&Field (rv, i), v);\n";
5795 pr " CAMLreturn (rv);\n";
5802 fun (name, style, _, _, _, _, _) ->
5804 "gv" :: List.map (fun arg -> name_of_argt arg ^ "v") (snd style) in
5806 pr "CAMLprim value\n";
5807 pr "ocaml_guestfs_%s (value %s" name (List.hd params);
5808 List.iter (pr ", value %s") (List.tl params);
5813 | [p1; p2; p3; p4; p5] ->
5814 pr " CAMLparam5 (%s);\n" (String.concat ", " params)
5815 | p1 :: p2 :: p3 :: p4 :: p5 :: rest ->
5816 pr " CAMLparam5 (%s);\n" (String.concat ", " [p1; p2; p3; p4; p5]);
5817 pr " CAMLxparam%d (%s);\n"
5818 (List.length rest) (String.concat ", " rest)
5820 pr " CAMLparam%d (%s);\n" (List.length ps) (String.concat ", " ps)
5822 pr " CAMLlocal1 (rv);\n";
5825 pr " guestfs_h *g = Guestfs_val (gv);\n";
5826 pr " if (g == NULL)\n";
5827 pr " caml_failwith (\"%s: used handle after closing it\");\n" name;
5835 pr " const char *%s = String_val (%sv);\n" n n
5837 pr " const char *%s =\n" n;
5838 pr " %sv != Val_int (0) ? String_val (Field (%sv, 0)) : NULL;\n"
5841 pr " char **%s = ocaml_guestfs_strings_val (g, %sv);\n" n n
5843 pr " int %s = Bool_val (%sv);\n" n n
5845 pr " int %s = Int_val (%sv);\n" n n
5848 match fst style with
5849 | RErr -> pr " int r;\n"; "-1"
5850 | RInt _ -> pr " int r;\n"; "-1"
5851 | RInt64 _ -> pr " int64_t r;\n"; "-1"
5852 | RBool _ -> pr " int r;\n"; "-1"
5853 | RConstString _ -> pr " const char *r;\n"; "NULL"
5854 | RString _ -> pr " char *r;\n"; "NULL"
5860 pr " struct guestfs_int_bool *r;\n"; "NULL"
5862 pr " struct guestfs_lvm_pv_list *r;\n"; "NULL"
5864 pr " struct guestfs_lvm_vg_list *r;\n"; "NULL"
5866 pr " struct guestfs_lvm_lv_list *r;\n"; "NULL"
5868 pr " struct guestfs_stat *r;\n"; "NULL"
5870 pr " struct guestfs_statvfs *r;\n"; "NULL"
5876 pr " struct guestfs_dirent_list *r;\n"; "NULL" in
5879 pr " caml_enter_blocking_section ();\n";
5880 pr " r = guestfs_%s " name;
5881 generate_call_args ~handle:"g" (snd style);
5883 pr " caml_leave_blocking_section ();\n";
5888 pr " ocaml_guestfs_free_strings (%s);\n" n;
5889 | String _ | OptString _ | Bool _ | Int _ | FileIn _ | FileOut _ -> ()
5892 pr " if (r == %s)\n" error_code;
5893 pr " ocaml_guestfs_raise_error (g, \"%s\");\n" name;
5896 (match fst style with
5897 | RErr -> pr " rv = Val_unit;\n"
5898 | RInt _ -> pr " rv = Val_int (r);\n"
5900 pr " rv = caml_copy_int64 (r);\n"
5901 | RBool _ -> pr " rv = Val_bool (r);\n"
5902 | RConstString _ -> pr " rv = caml_copy_string (r);\n"
5904 pr " rv = caml_copy_string (r);\n";
5907 pr " rv = caml_copy_string_array ((const char **) r);\n";
5908 pr " for (i = 0; r[i] != NULL; ++i) free (r[i]);\n";
5911 pr " rv = caml_alloc (2, 0);\n";
5912 pr " Store_field (rv, 0, Val_int (r->i));\n";
5913 pr " Store_field (rv, 1, Val_bool (r->b));\n";
5914 pr " guestfs_free_int_bool (r);\n";
5916 pr " rv = copy_lvm_pv_list (r);\n";
5917 pr " guestfs_free_lvm_pv_list (r);\n";
5919 pr " rv = copy_lvm_vg_list (r);\n";
5920 pr " guestfs_free_lvm_vg_list (r);\n";
5922 pr " rv = copy_lvm_lv_list (r);\n";
5923 pr " guestfs_free_lvm_lv_list (r);\n";
5925 pr " rv = copy_stat (r);\n";
5928 pr " rv = copy_statvfs (r);\n";
5931 pr " rv = copy_table (r);\n";
5932 pr " for (i = 0; r[i] != NULL; ++i) free (r[i]);\n";
5935 pr " rv = copy_dirent_list (r);\n";
5936 pr " guestfs_free_dirent_list (r);\n";
5939 pr " CAMLreturn (rv);\n";
5943 if List.length params > 5 then (
5944 pr "CAMLprim value\n";
5945 pr "ocaml_guestfs_%s_byte (value *argv, int argn)\n" name;
5947 pr " return ocaml_guestfs_%s (argv[0]" name;
5948 iteri (fun i _ -> pr ", argv[%d]" i) (List.tl params);
5955 and generate_ocaml_lvm_structure_decls () =
5958 pr "type lvm_%s = {\n" typ;
5961 | name, `String -> pr " %s : string;\n" name
5962 | name, `UUID -> pr " %s : string;\n" name
5963 | name, `Bytes -> pr " %s : int64;\n" name
5964 | name, `Int -> pr " %s : int64;\n" name
5965 | name, `OptPercent -> pr " %s : float option;\n" name
5969 ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols]
5971 and generate_ocaml_stat_structure_decls () =
5974 pr "type %s = {\n" typ;
5977 | name, `Int -> pr " %s : int64;\n" name
5981 ) ["stat", stat_cols; "statvfs", statvfs_cols]
5983 and generate_ocaml_dirent_structure_decls () =
5984 pr "type dirent = {\n";
5987 | name, `Int -> pr " %s : int64;\n" name
5988 | name, `Char -> pr " %s : char;\n" name
5989 | name, `String -> pr " %s : string;\n" name
5994 and generate_ocaml_prototype ?(is_external = false) name style =
5995 if is_external then pr "external " else pr "val ";
5996 pr "%s : t -> " name;
5999 | String _ | FileIn _ | FileOut _ -> pr "string -> "
6000 | OptString _ -> pr "string option -> "
6001 | StringList _ -> pr "string array -> "
6002 | Bool _ -> pr "bool -> "
6003 | Int _ -> pr "int -> "
6005 (match fst style with
6006 | RErr -> pr "unit" (* all errors are turned into exceptions *)
6007 | RInt _ -> pr "int"
6008 | RInt64 _ -> pr "int64"
6009 | RBool _ -> pr "bool"
6010 | RConstString _ -> pr "string"
6011 | RString _ -> pr "string"
6012 | RStringList _ -> pr "string array"
6013 | RIntBool _ -> pr "int * bool"
6014 | RPVList _ -> pr "lvm_pv array"
6015 | RVGList _ -> pr "lvm_vg array"
6016 | RLVList _ -> pr "lvm_lv array"
6017 | RStat _ -> pr "stat"
6018 | RStatVFS _ -> pr "statvfs"
6019 | RHashtable _ -> pr "(string * string) list"
6020 | RDirentList _ -> pr "dirent array"
6022 if is_external then (
6024 if List.length (snd style) + 1 > 5 then
6025 pr "\"ocaml_guestfs_%s_byte\" " name;
6026 pr "\"ocaml_guestfs_%s\"" name
6030 (* Generate Perl xs code, a sort of crazy variation of C with macros. *)
6031 and generate_perl_xs () =
6032 generate_header CStyle LGPLv2;
6035 #include \"EXTERN.h\"
6039 #include <guestfs.h>
6042 #define PRId64 \"lld\"
6046 my_newSVll(long long val) {
6047 #ifdef USE_64_BIT_ALL
6048 return newSViv(val);
6052 len = snprintf(buf, 100, \"%%\" PRId64, val);
6053 return newSVpv(buf, len);
6058 #define PRIu64 \"llu\"
6062 my_newSVull(unsigned long long val) {
6063 #ifdef USE_64_BIT_ALL
6064 return newSVuv(val);
6068 len = snprintf(buf, 100, \"%%\" PRIu64, val);
6069 return newSVpv(buf, len);
6073 /* http://www.perlmonks.org/?node_id=680842 */
6075 XS_unpack_charPtrPtr (SV *arg) {
6080 if (!arg || !SvOK (arg) || !SvROK (arg) || SvTYPE (SvRV (arg)) != SVt_PVAV)
6081 croak (\"array reference expected\");
6083 av = (AV *)SvRV (arg);
6084 ret = malloc ((av_len (av) + 1 + 1) * sizeof (char *));
6086 croak (\"malloc failed\");
6088 for (i = 0; i <= av_len (av); i++) {
6089 SV **elem = av_fetch (av, i, 0);
6091 if (!elem || !*elem)
6092 croak (\"missing element in list\");
6094 ret[i] = SvPV_nolen (*elem);
6102 MODULE = Sys::Guestfs PACKAGE = Sys::Guestfs
6109 RETVAL = guestfs_create ();
6111 croak (\"could not create guestfs handle\");
6112 guestfs_set_error_handler (RETVAL, NULL, NULL);
6125 fun (name, style, _, _, _, _, _) ->
6126 (match fst style with
6127 | RErr -> pr "void\n"
6128 | RInt _ -> pr "SV *\n"
6129 | RInt64 _ -> pr "SV *\n"
6130 | RBool _ -> pr "SV *\n"
6131 | RConstString _ -> pr "SV *\n"
6132 | RString _ -> pr "SV *\n"
6135 | RPVList _ | RVGList _ | RLVList _
6136 | RStat _ | RStatVFS _
6139 pr "void\n" (* all lists returned implictly on the stack *)
6141 (* Call and arguments. *)
6143 generate_call_args ~handle:"g" (snd style);
6145 pr " guestfs_h *g;\n";
6149 | String n | FileIn n | FileOut n -> pr " char *%s;\n" n
6151 (* http://www.perlmonks.org/?node_id=554277
6152 * Note that the implicit handle argument means we have
6153 * to add 1 to the ST(x) operator.
6155 pr " char *%s = SvOK(ST(%d)) ? SvPV_nolen(ST(%d)) : NULL;\n" n (i+1) (i+1)
6156 | StringList n -> pr " char **%s;\n" n
6157 | Bool n -> pr " int %s;\n" n
6158 | Int n -> pr " int %s;\n" n
6161 let do_cleanups () =
6164 | String _ | OptString _ | Bool _ | Int _
6165 | FileIn _ | FileOut _ -> ()
6166 | StringList n -> pr " free (%s);\n" n
6171 (match fst style with
6176 pr " r = guestfs_%s " name;
6177 generate_call_args ~handle:"g" (snd style);
6180 pr " if (r == -1)\n";
6181 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
6187 pr " %s = guestfs_%s " n name;
6188 generate_call_args ~handle:"g" (snd style);
6191 pr " if (%s == -1)\n" n;
6192 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
6193 pr " RETVAL = newSViv (%s);\n" n;
6198 pr " int64_t %s;\n" n;
6200 pr " %s = guestfs_%s " n name;
6201 generate_call_args ~handle:"g" (snd style);
6204 pr " if (%s == -1)\n" n;
6205 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
6206 pr " RETVAL = my_newSVll (%s);\n" n;
6211 pr " const char *%s;\n" n;
6213 pr " %s = guestfs_%s " n name;
6214 generate_call_args ~handle:"g" (snd style);
6217 pr " if (%s == NULL)\n" n;
6218 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
6219 pr " RETVAL = newSVpv (%s, 0);\n" n;
6224 pr " char *%s;\n" n;
6226 pr " %s = guestfs_%s " n name;
6227 generate_call_args ~handle:"g" (snd style);
6230 pr " if (%s == NULL)\n" n;
6231 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
6232 pr " RETVAL = newSVpv (%s, 0);\n" n;
6233 pr " free (%s);\n" n;
6236 | RStringList n | RHashtable n ->
6238 pr " char **%s;\n" n;
6241 pr " %s = guestfs_%s " n name;
6242 generate_call_args ~handle:"g" (snd style);
6245 pr " if (%s == NULL)\n" n;
6246 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
6247 pr " for (n = 0; %s[n] != NULL; ++n) /**/;\n" n;
6248 pr " EXTEND (SP, n);\n";
6249 pr " for (i = 0; i < n; ++i) {\n";
6250 pr " PUSHs (sv_2mortal (newSVpv (%s[i], 0)));\n" n;
6251 pr " free (%s[i]);\n" n;
6253 pr " free (%s);\n" n;
6256 pr " struct guestfs_int_bool *r;\n";
6258 pr " r = guestfs_%s " name;
6259 generate_call_args ~handle:"g" (snd style);
6262 pr " if (r == NULL)\n";
6263 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
6264 pr " EXTEND (SP, 2);\n";
6265 pr " PUSHs (sv_2mortal (newSViv (r->i)));\n";
6266 pr " PUSHs (sv_2mortal (newSViv (r->b)));\n";
6267 pr " guestfs_free_int_bool (r);\n";
6269 generate_perl_lvm_code "pv" pv_cols name style n do_cleanups
6271 generate_perl_lvm_code "vg" vg_cols name style n do_cleanups
6273 generate_perl_lvm_code "lv" lv_cols name style n do_cleanups
6275 generate_perl_stat_code "stat" stat_cols name style n do_cleanups
6277 generate_perl_stat_code
6278 "statvfs" statvfs_cols name style n do_cleanups
6280 generate_perl_dirent_code
6281 "dirent" dirent_cols name style n do_cleanups
6287 and generate_perl_lvm_code typ cols name style n do_cleanups =
6289 pr " struct guestfs_lvm_%s_list *%s;\n" typ n;
6293 pr " %s = guestfs_%s " n name;
6294 generate_call_args ~handle:"g" (snd style);
6297 pr " if (%s == NULL)\n" n;
6298 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
6299 pr " EXTEND (SP, %s->len);\n" n;
6300 pr " for (i = 0; i < %s->len; ++i) {\n" n;
6301 pr " hv = newHV ();\n";
6305 pr " (void) hv_store (hv, \"%s\", %d, newSVpv (%s->val[i].%s, 0), 0);\n"
6306 name (String.length name) n name
6308 pr " (void) hv_store (hv, \"%s\", %d, newSVpv (%s->val[i].%s, 32), 0);\n"
6309 name (String.length name) n name
6311 pr " (void) hv_store (hv, \"%s\", %d, my_newSVull (%s->val[i].%s), 0);\n"
6312 name (String.length name) n name
6314 pr " (void) hv_store (hv, \"%s\", %d, my_newSVll (%s->val[i].%s), 0);\n"
6315 name (String.length name) n name
6316 | name, `OptPercent ->
6317 pr " (void) hv_store (hv, \"%s\", %d, newSVnv (%s->val[i].%s), 0);\n"
6318 name (String.length name) n name
6320 pr " PUSHs (sv_2mortal (newRV ((SV *) hv)));\n";
6322 pr " guestfs_free_lvm_%s_list (%s);\n" typ n
6324 and generate_perl_stat_code typ cols name style n do_cleanups =
6326 pr " struct guestfs_%s *%s;\n" typ n;
6328 pr " %s = guestfs_%s " n name;
6329 generate_call_args ~handle:"g" (snd style);
6332 pr " if (%s == NULL)\n" n;
6333 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
6334 pr " EXTEND (SP, %d);\n" (List.length cols);
6338 pr " PUSHs (sv_2mortal (my_newSVll (%s->%s)));\n" n name
6340 pr " free (%s);\n" n
6342 and generate_perl_dirent_code typ cols name style n do_cleanups =
6344 pr " struct guestfs_%s_list *%s;\n" typ n;
6348 pr " %s = guestfs_%s " n name;
6349 generate_call_args ~handle:"g" (snd style);
6352 pr " if (%s == NULL)\n" n;
6353 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
6354 pr " EXTEND (SP, %s->len);\n" n;
6355 pr " for (i = 0; i < %s->len; ++i) {\n" n;
6356 pr " hv = newHV ();\n";
6360 pr " (void) hv_store (hv, \"%s\", %d, newSVpv (%s->val[i].%s, 0), 0);\n"
6361 name (String.length name) n name
6363 pr " (void) hv_store (hv, \"%s\", %d, my_newSVull (%s->val[i].%s), 0);\n"
6364 name (String.length name) n name
6366 pr " (void) hv_store (hv, \"%s\", %d, newSVpv (&%s->val[i].%s, 1), 0);\n"
6367 name (String.length name) n name
6369 pr " PUSHs (newRV (sv_2mortal ((SV *) hv)));\n";
6371 pr " guestfs_free_%s_list (%s);\n" typ n
6373 (* Generate Sys/Guestfs.pm. *)
6374 and generate_perl_pm () =
6375 generate_header HashStyle LGPLv2;
6382 Sys::Guestfs - Perl bindings for libguestfs
6388 my $h = Sys::Guestfs->new ();
6389 $h->add_drive ('guest.img');
6392 $h->mount ('/dev/sda1', '/');
6393 $h->touch ('/hello');
6398 The C<Sys::Guestfs> module provides a Perl XS binding to the
6399 libguestfs API for examining and modifying virtual machine
6402 Amongst the things this is good for: making batch configuration
6403 changes to guests, getting disk used/free statistics (see also:
6404 virt-df), migrating between virtualization systems (see also:
6405 virt-p2v), performing partial backups, performing partial guest
6406 clones, cloning guests and changing registry/UUID/hostname info, and
6409 Libguestfs uses Linux kernel and qemu code, and can access any type of
6410 guest filesystem that Linux and qemu can, including but not limited
6411 to: ext2/3/4, btrfs, FAT and NTFS, LVM, many different disk partition
6412 schemes, qcow, qcow2, vmdk.
6414 Libguestfs provides ways to enumerate guest storage (eg. partitions,
6415 LVs, what filesystem is in each LV, etc.). It can also run commands
6416 in the context of the guest. Also you can access filesystems over FTP.
6420 All errors turn into calls to C<croak> (see L<Carp(3)>).
6428 package Sys::Guestfs;
6434 XSLoader::load ('Sys::Guestfs');
6436 =item $h = Sys::Guestfs->new ();
6438 Create a new guestfs handle.
6444 my $class = ref ($proto) || $proto;
6446 my $self = Sys::Guestfs::_create ();
6447 bless $self, $class;
6453 (* Actions. We only need to print documentation for these as
6454 * they are pulled in from the XS code automatically.
6457 fun (name, style, _, flags, _, _, longdesc) ->
6458 if not (List.mem NotInDocs flags) then (
6459 let longdesc = replace_str longdesc "C<guestfs_" "C<$h-E<gt>" in
6461 generate_perl_prototype name style;
6463 pr "%s\n\n" longdesc;
6464 if List.mem ProtocolLimitWarning flags then
6465 pr "%s\n\n" protocol_limit_warning;
6466 if List.mem DangerWillRobinson flags then
6467 pr "%s\n\n" danger_will_robinson
6469 ) all_functions_sorted;
6481 Copyright (C) 2009 Red Hat Inc.
6485 Please see the file COPYING.LIB for the full license.
6489 L<guestfs(3)>, L<guestfish(1)>.
6494 and generate_perl_prototype name style =
6495 (match fst style with
6501 | RString n -> pr "$%s = " n
6502 | RIntBool (n, m) -> pr "($%s, $%s) = " n m
6507 | RDirentList n -> pr "@%s = " n
6510 | RHashtable n -> pr "%%%s = " n
6513 let comma = ref false in
6516 if !comma then pr ", ";
6519 | String n | OptString n | Bool n | Int n | FileIn n | FileOut n ->
6526 (* Generate Python C module. *)
6527 and generate_python_c () =
6528 generate_header CStyle LGPLv2;
6537 #include \"guestfs.h\"
6545 get_handle (PyObject *obj)
6548 assert (obj != Py_None);
6549 return ((Pyguestfs_Object *) obj)->g;
6553 put_handle (guestfs_h *g)
6557 PyCObject_FromVoidPtrAndDesc ((void *) g, (char *) \"guestfs_h\", NULL);
6560 /* This list should be freed (but not the strings) after use. */
6561 static const char **
6562 get_string_list (PyObject *obj)
6569 if (!PyList_Check (obj)) {
6570 PyErr_SetString (PyExc_RuntimeError, \"expecting a list parameter\");
6574 len = PyList_Size (obj);
6575 r = malloc (sizeof (char *) * (len+1));
6577 PyErr_SetString (PyExc_RuntimeError, \"get_string_list: out of memory\");
6581 for (i = 0; i < len; ++i)
6582 r[i] = PyString_AsString (PyList_GetItem (obj, i));
6589 put_string_list (char * const * const argv)
6594 for (argc = 0; argv[argc] != NULL; ++argc)
6597 list = PyList_New (argc);
6598 for (i = 0; i < argc; ++i)
6599 PyList_SetItem (list, i, PyString_FromString (argv[i]));
6605 put_table (char * const * const argv)
6607 PyObject *list, *item;
6610 for (argc = 0; argv[argc] != NULL; ++argc)
6613 list = PyList_New (argc >> 1);
6614 for (i = 0; i < argc; i += 2) {
6615 item = PyTuple_New (2);
6616 PyTuple_SetItem (item, 0, PyString_FromString (argv[i]));
6617 PyTuple_SetItem (item, 1, PyString_FromString (argv[i+1]));
6618 PyList_SetItem (list, i >> 1, item);
6625 free_strings (char **argv)
6629 for (argc = 0; argv[argc] != NULL; ++argc)
6635 py_guestfs_create (PyObject *self, PyObject *args)
6639 g = guestfs_create ();
6641 PyErr_SetString (PyExc_RuntimeError,
6642 \"guestfs.create: failed to allocate handle\");
6645 guestfs_set_error_handler (g, NULL, NULL);
6646 return put_handle (g);
6650 py_guestfs_close (PyObject *self, PyObject *args)
6655 if (!PyArg_ParseTuple (args, (char *) \"O:guestfs_close\", &py_g))
6657 g = get_handle (py_g);
6661 Py_INCREF (Py_None);
6667 (* LVM structures, turned into Python dictionaries. *)
6670 pr "static PyObject *\n";
6671 pr "put_lvm_%s (struct guestfs_lvm_%s *%s)\n" typ typ typ;
6673 pr " PyObject *dict;\n";
6675 pr " dict = PyDict_New ();\n";
6679 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
6680 pr " PyString_FromString (%s->%s));\n"
6683 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
6684 pr " PyString_FromStringAndSize (%s->%s, 32));\n"
6687 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
6688 pr " PyLong_FromUnsignedLongLong (%s->%s));\n"
6691 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
6692 pr " PyLong_FromLongLong (%s->%s));\n"
6694 | name, `OptPercent ->
6695 pr " if (%s->%s >= 0)\n" typ name;
6696 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
6697 pr " PyFloat_FromDouble ((double) %s->%s));\n"
6700 pr " Py_INCREF (Py_None);\n";
6701 pr " PyDict_SetItemString (dict, \"%s\", Py_None);" name;
6704 pr " return dict;\n";
6708 pr "static PyObject *\n";
6709 pr "put_lvm_%s_list (struct guestfs_lvm_%s_list *%ss)\n" typ typ typ;
6711 pr " PyObject *list;\n";
6714 pr " list = PyList_New (%ss->len);\n" typ;
6715 pr " for (i = 0; i < %ss->len; ++i)\n" typ;
6716 pr " PyList_SetItem (list, i, put_lvm_%s (&%ss->val[i]));\n" typ typ;
6717 pr " return list;\n";
6720 ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols];
6722 (* Stat structures, turned into Python dictionaries. *)
6725 pr "static PyObject *\n";
6726 pr "put_%s (struct guestfs_%s *%s)\n" typ typ typ;
6728 pr " PyObject *dict;\n";
6730 pr " dict = PyDict_New ();\n";
6734 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
6735 pr " PyLong_FromLongLong (%s->%s));\n"
6738 pr " return dict;\n";
6741 ) ["stat", stat_cols; "statvfs", statvfs_cols];
6743 (* Dirent structures, turned into Python dictionaries. *)
6744 pr "static PyObject *\n";
6745 pr "put_dirent (struct guestfs_dirent *dirent)\n";
6747 pr " PyObject *dict;\n";
6749 pr " dict = PyDict_New ();\n";
6753 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
6754 pr " PyLong_FromLongLong (dirent->%s));\n" name
6756 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
6757 pr " PyString_FromStringAndSize (&dirent->%s, 1));\n" name
6759 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
6760 pr " PyString_FromString (dirent->%s));\n" name
6762 pr " return dict;\n";
6766 pr "static PyObject *\n";
6767 pr "put_dirent_list (struct guestfs_dirent_list *dirents)\n";
6769 pr " PyObject *list;\n";
6772 pr " list = PyList_New (dirents->len);\n";
6773 pr " for (i = 0; i < dirents->len; ++i)\n";
6774 pr " PyList_SetItem (list, i, put_dirent (&dirents->val[i]));\n";
6775 pr " return list;\n";
6779 (* Python wrapper functions. *)
6781 fun (name, style, _, _, _, _, _) ->
6782 pr "static PyObject *\n";
6783 pr "py_guestfs_%s (PyObject *self, PyObject *args)\n" name;
6786 pr " PyObject *py_g;\n";
6787 pr " guestfs_h *g;\n";
6788 pr " PyObject *py_r;\n";
6791 match fst style with
6792 | RErr | RInt _ | RBool _ -> pr " int r;\n"; "-1"
6793 | RInt64 _ -> pr " int64_t r;\n"; "-1"
6794 | RConstString _ -> pr " const char *r;\n"; "NULL"
6795 | RString _ -> pr " char *r;\n"; "NULL"
6796 | RStringList _ | RHashtable _ -> pr " char **r;\n"; "NULL"
6797 | RIntBool _ -> pr " struct guestfs_int_bool *r;\n"; "NULL"
6798 | RPVList n -> pr " struct guestfs_lvm_pv_list *r;\n"; "NULL"
6799 | RVGList n -> pr " struct guestfs_lvm_vg_list *r;\n"; "NULL"
6800 | RLVList n -> pr " struct guestfs_lvm_lv_list *r;\n"; "NULL"
6801 | RStat n -> pr " struct guestfs_stat *r;\n"; "NULL"
6802 | RStatVFS n -> pr " struct guestfs_statvfs *r;\n"; "NULL"
6803 | RDirentList n -> pr " struct guestfs_dirent_list *r;\n"; "NULL" in
6807 | String n | FileIn n | FileOut n -> pr " const char *%s;\n" n
6808 | OptString n -> pr " const char *%s;\n" n
6810 pr " PyObject *py_%s;\n" n;
6811 pr " const char **%s;\n" n
6812 | Bool n -> pr " int %s;\n" n
6813 | Int n -> pr " int %s;\n" n
6818 (* Convert the parameters. *)
6819 pr " if (!PyArg_ParseTuple (args, (char *) \"O";
6822 | String _ | FileIn _ | FileOut _ -> pr "s"
6823 | OptString _ -> pr "z"
6824 | StringList _ -> pr "O"
6825 | Bool _ -> pr "i" (* XXX Python has booleans? *)
6828 pr ":guestfs_%s\",\n" name;
6832 | String n | FileIn n | FileOut n -> pr ", &%s" n
6833 | OptString n -> pr ", &%s" n
6834 | StringList n -> pr ", &py_%s" n
6835 | Bool n -> pr ", &%s" n
6836 | Int n -> pr ", &%s" n
6840 pr " return NULL;\n";
6842 pr " g = get_handle (py_g);\n";
6845 | String _ | FileIn _ | FileOut _ | OptString _ | Bool _ | Int _ -> ()
6847 pr " %s = get_string_list (py_%s);\n" n n;
6848 pr " if (!%s) return NULL;\n" n
6853 pr " r = guestfs_%s " name;
6854 generate_call_args ~handle:"g" (snd style);
6859 | String _ | FileIn _ | FileOut _ | OptString _ | Bool _ | Int _ -> ()
6861 pr " free (%s);\n" n
6864 pr " if (r == %s) {\n" error_code;
6865 pr " PyErr_SetString (PyExc_RuntimeError, guestfs_last_error (g));\n";
6866 pr " return NULL;\n";
6870 (match fst style with
6872 pr " Py_INCREF (Py_None);\n";
6873 pr " py_r = Py_None;\n"
6875 | RBool _ -> pr " py_r = PyInt_FromLong ((long) r);\n"
6876 | RInt64 _ -> pr " py_r = PyLong_FromLongLong (r);\n"
6877 | RConstString _ -> pr " py_r = PyString_FromString (r);\n"
6879 pr " py_r = PyString_FromString (r);\n";
6882 pr " py_r = put_string_list (r);\n";
6883 pr " free_strings (r);\n"
6885 pr " py_r = PyTuple_New (2);\n";
6886 pr " PyTuple_SetItem (py_r, 0, PyInt_FromLong ((long) r->i));\n";
6887 pr " PyTuple_SetItem (py_r, 1, PyInt_FromLong ((long) r->b));\n";
6888 pr " guestfs_free_int_bool (r);\n"
6890 pr " py_r = put_lvm_pv_list (r);\n";
6891 pr " guestfs_free_lvm_pv_list (r);\n"
6893 pr " py_r = put_lvm_vg_list (r);\n";
6894 pr " guestfs_free_lvm_vg_list (r);\n"
6896 pr " py_r = put_lvm_lv_list (r);\n";
6897 pr " guestfs_free_lvm_lv_list (r);\n"
6899 pr " py_r = put_stat (r);\n";
6902 pr " py_r = put_statvfs (r);\n";
6905 pr " py_r = put_table (r);\n";
6906 pr " free_strings (r);\n"
6908 pr " py_r = put_dirent_list (r);\n";
6909 pr " guestfs_free_dirent_list (r);\n"
6912 pr " return py_r;\n";
6917 (* Table of functions. *)
6918 pr "static PyMethodDef methods[] = {\n";
6919 pr " { (char *) \"create\", py_guestfs_create, METH_VARARGS, NULL },\n";
6920 pr " { (char *) \"close\", py_guestfs_close, METH_VARARGS, NULL },\n";
6922 fun (name, _, _, _, _, _, _) ->
6923 pr " { (char *) \"%s\", py_guestfs_%s, METH_VARARGS, NULL },\n"
6926 pr " { NULL, NULL, 0, NULL }\n";
6930 (* Init function. *)
6933 initlibguestfsmod (void)
6935 static int initialized = 0;
6937 if (initialized) return;
6938 Py_InitModule ((char *) \"libguestfsmod\", methods);
6943 (* Generate Python module. *)
6944 and generate_python_py () =
6945 generate_header HashStyle LGPLv2;
6948 u\"\"\"Python bindings for libguestfs
6951 g = guestfs.GuestFS ()
6952 g.add_drive (\"guest.img\")
6955 parts = g.list_partitions ()
6957 The guestfs module provides a Python binding to the libguestfs API
6958 for examining and modifying virtual machine disk images.
6960 Amongst the things this is good for: making batch configuration
6961 changes to guests, getting disk used/free statistics (see also:
6962 virt-df), migrating between virtualization systems (see also:
6963 virt-p2v), performing partial backups, performing partial guest
6964 clones, cloning guests and changing registry/UUID/hostname info, and
6967 Libguestfs uses Linux kernel and qemu code, and can access any type of
6968 guest filesystem that Linux and qemu can, including but not limited
6969 to: ext2/3/4, btrfs, FAT and NTFS, LVM, many different disk partition
6970 schemes, qcow, qcow2, vmdk.
6972 Libguestfs provides ways to enumerate guest storage (eg. partitions,
6973 LVs, what filesystem is in each LV, etc.). It can also run commands
6974 in the context of the guest. Also you can access filesystems over FTP.
6976 Errors which happen while using the API are turned into Python
6977 RuntimeError exceptions.
6979 To create a guestfs handle you usually have to perform the following
6982 # Create the handle, call add_drive at least once, and possibly
6983 # several times if the guest has multiple block devices:
6984 g = guestfs.GuestFS ()
6985 g.add_drive (\"guest.img\")
6987 # Launch the qemu subprocess and wait for it to become ready:
6991 # Now you can issue commands, for example:
6996 import libguestfsmod
6999 \"\"\"Instances of this class are libguestfs API handles.\"\"\"
7001 def __init__ (self):
7002 \"\"\"Create a new libguestfs handle.\"\"\"
7003 self._o = libguestfsmod.create ()
7006 libguestfsmod.close (self._o)
7011 fun (name, style, _, flags, _, _, longdesc) ->
7013 generate_call_args ~handle:"self" (snd style);
7016 if not (List.mem NotInDocs flags) then (
7017 let doc = replace_str longdesc "C<guestfs_" "C<g." in
7019 match fst style with
7020 | RErr | RInt _ | RInt64 _ | RBool _ | RConstString _
7023 doc ^ "\n\nThis function returns a list of strings."
7025 doc ^ "\n\nThis function returns a tuple (int, bool).\n"
7027 doc ^ "\n\nThis function returns a list of PVs. Each PV is represented as a dictionary."
7029 doc ^ "\n\nThis function returns a list of VGs. Each VG is represented as a dictionary."
7031 doc ^ "\n\nThis function returns a list of LVs. Each LV is represented as a dictionary."
7033 doc ^ "\n\nThis function returns a dictionary, with keys matching the various fields in the stat structure."
7035 doc ^ "\n\nThis function returns a dictionary, with keys matching the various fields in the statvfs structure."
7037 doc ^ "\n\nThis function returns a dictionary."
7039 doc ^ "\n\nThis function returns a list of directory entries. Each directory entry is represented as a dictionary." in
7041 if List.mem ProtocolLimitWarning flags then
7042 doc ^ "\n\n" ^ protocol_limit_warning
7045 if List.mem DangerWillRobinson flags then
7046 doc ^ "\n\n" ^ danger_will_robinson
7048 let doc = pod2text ~width:60 name doc in
7049 let doc = List.map (fun line -> replace_str line "\\" "\\\\") doc in
7050 let doc = String.concat "\n " doc in
7051 pr " u\"\"\"%s\"\"\"\n" doc;
7053 pr " return libguestfsmod.%s " name;
7054 generate_call_args ~handle:"self._o" (snd style);
7059 (* Useful if you need the longdesc POD text as plain text. Returns a
7062 * Because this is very slow (the slowest part of autogeneration),
7063 * we memoize the results.
7065 and pod2text ~width name longdesc =
7066 let key = width, name, longdesc in
7067 try Hashtbl.find pod2text_memo key
7069 let filename, chan = Filename.open_temp_file "gen" ".tmp" in
7070 fprintf chan "=head1 %s\n\n%s\n" name longdesc;
7072 let cmd = sprintf "pod2text -w %d %s" width (Filename.quote filename) in
7073 let chan = Unix.open_process_in cmd in
7074 let lines = ref [] in
7076 let line = input_line chan in
7077 if i = 1 then (* discard the first line of output *)
7080 let line = triml line in
7081 lines := line :: !lines;
7084 let lines = try loop 1 with End_of_file -> List.rev !lines in
7085 Unix.unlink filename;
7086 (match Unix.close_process_in chan with
7087 | Unix.WEXITED 0 -> ()
7089 failwithf "pod2text: process exited with non-zero status (%d)" i
7090 | Unix.WSIGNALED i | Unix.WSTOPPED i ->
7091 failwithf "pod2text: process signalled or stopped by signal %d" i
7093 Hashtbl.add pod2text_memo key lines;
7094 let chan = open_out pod2text_memo_filename in
7095 output_value chan pod2text_memo;
7099 (* Generate ruby bindings. *)
7100 and generate_ruby_c () =
7101 generate_header CStyle LGPLv2;
7109 #include \"guestfs.h\"
7111 #include \"extconf.h\"
7113 /* For Ruby < 1.9 */
7115 #define RARRAY_LEN(r) (RARRAY((r))->len)
7118 static VALUE m_guestfs; /* guestfs module */
7119 static VALUE c_guestfs; /* guestfs_h handle */
7120 static VALUE e_Error; /* used for all errors */
7122 static void ruby_guestfs_free (void *p)
7125 guestfs_close ((guestfs_h *) p);
7128 static VALUE ruby_guestfs_create (VALUE m)
7132 g = guestfs_create ();
7134 rb_raise (e_Error, \"failed to create guestfs handle\");
7136 /* Don't print error messages to stderr by default. */
7137 guestfs_set_error_handler (g, NULL, NULL);
7139 /* Wrap it, and make sure the close function is called when the
7142 return Data_Wrap_Struct (c_guestfs, NULL, ruby_guestfs_free, g);
7145 static VALUE ruby_guestfs_close (VALUE gv)
7148 Data_Get_Struct (gv, guestfs_h, g);
7150 ruby_guestfs_free (g);
7151 DATA_PTR (gv) = NULL;
7159 fun (name, style, _, _, _, _, _) ->
7160 pr "static VALUE ruby_guestfs_%s (VALUE gv" name;
7161 List.iter (fun arg -> pr ", VALUE %sv" (name_of_argt arg)) (snd style);
7164 pr " guestfs_h *g;\n";
7165 pr " Data_Get_Struct (gv, guestfs_h, g);\n";
7167 pr " rb_raise (rb_eArgError, \"%%s: used handle after closing it\", \"%s\");\n"
7173 | String n | FileIn n | FileOut n ->
7174 pr " Check_Type (%sv, T_STRING);\n" n;
7175 pr " const char *%s = StringValueCStr (%sv);\n" n n;
7177 pr " rb_raise (rb_eTypeError, \"expected string for parameter %%s of %%s\",\n";
7178 pr " \"%s\", \"%s\");\n" n name
7180 pr " const char *%s = !NIL_P (%sv) ? StringValueCStr (%sv) : NULL;\n" n n n
7182 pr " char **%s;\n" n;
7183 pr " Check_Type (%sv, T_ARRAY);\n" n;
7185 pr " int i, len;\n";
7186 pr " len = RARRAY_LEN (%sv);\n" n;
7187 pr " %s = guestfs_safe_malloc (g, sizeof (char *) * (len+1));\n"
7189 pr " for (i = 0; i < len; ++i) {\n";
7190 pr " VALUE v = rb_ary_entry (%sv, i);\n" n;
7191 pr " %s[i] = StringValueCStr (v);\n" n;
7193 pr " %s[len] = NULL;\n" n;
7196 pr " int %s = RTEST (%sv);\n" n n
7198 pr " int %s = NUM2INT (%sv);\n" n n
7203 match fst style with
7204 | RErr | RInt _ | RBool _ -> pr " int r;\n"; "-1"
7205 | RInt64 _ -> pr " int64_t r;\n"; "-1"
7206 | RConstString _ -> pr " const char *r;\n"; "NULL"
7207 | RString _ -> pr " char *r;\n"; "NULL"
7208 | RStringList _ | RHashtable _ -> pr " char **r;\n"; "NULL"
7209 | RIntBool _ -> pr " struct guestfs_int_bool *r;\n"; "NULL"
7210 | RPVList n -> pr " struct guestfs_lvm_pv_list *r;\n"; "NULL"
7211 | RVGList n -> pr " struct guestfs_lvm_vg_list *r;\n"; "NULL"
7212 | RLVList n -> pr " struct guestfs_lvm_lv_list *r;\n"; "NULL"
7213 | RStat n -> pr " struct guestfs_stat *r;\n"; "NULL"
7214 | RStatVFS n -> pr " struct guestfs_statvfs *r;\n"; "NULL"
7215 | RDirentList n -> pr " struct guestfs_dirent_list *r;\n"; "NULL" in
7218 pr " r = guestfs_%s " name;
7219 generate_call_args ~handle:"g" (snd style);
7224 | String _ | FileIn _ | FileOut _ | OptString _ | Bool _ | Int _ -> ()
7226 pr " free (%s);\n" n
7229 pr " if (r == %s)\n" error_code;
7230 pr " rb_raise (e_Error, \"%%s\", guestfs_last_error (g));\n";
7233 (match fst style with
7235 pr " return Qnil;\n"
7236 | RInt _ | RBool _ ->
7237 pr " return INT2NUM (r);\n"
7239 pr " return ULL2NUM (r);\n"
7241 pr " return rb_str_new2 (r);\n";
7243 pr " VALUE rv = rb_str_new2 (r);\n";
7247 pr " int i, len = 0;\n";
7248 pr " for (i = 0; r[i] != NULL; ++i) len++;\n";
7249 pr " VALUE rv = rb_ary_new2 (len);\n";
7250 pr " for (i = 0; r[i] != NULL; ++i) {\n";
7251 pr " rb_ary_push (rv, rb_str_new2 (r[i]));\n";
7252 pr " free (r[i]);\n";
7257 pr " VALUE rv = rb_ary_new2 (2);\n";
7258 pr " rb_ary_push (rv, INT2NUM (r->i));\n";
7259 pr " rb_ary_push (rv, INT2NUM (r->b));\n";
7260 pr " guestfs_free_int_bool (r);\n";
7263 generate_ruby_lvm_code "pv" pv_cols
7265 generate_ruby_lvm_code "vg" vg_cols
7267 generate_ruby_lvm_code "lv" lv_cols
7269 pr " VALUE rv = rb_hash_new ();\n";
7273 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), ULL2NUM (r->%s));\n" name name
7278 pr " VALUE rv = rb_hash_new ();\n";
7282 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), ULL2NUM (r->%s));\n" name name
7287 pr " VALUE rv = rb_hash_new ();\n";
7289 pr " for (i = 0; r[i] != NULL; i+=2) {\n";
7290 pr " rb_hash_aset (rv, rb_str_new2 (r[i]), rb_str_new2 (r[i+1]));\n";
7291 pr " free (r[i]);\n";
7292 pr " free (r[i+1]);\n";
7297 generate_ruby_dirent_code "dirent" dirent_cols
7305 /* Initialize the module. */
7306 void Init__guestfs ()
7308 m_guestfs = rb_define_module (\"Guestfs\");
7309 c_guestfs = rb_define_class_under (m_guestfs, \"Guestfs\", rb_cObject);
7310 e_Error = rb_define_class_under (m_guestfs, \"Error\", rb_eStandardError);
7312 rb_define_module_function (m_guestfs, \"create\", ruby_guestfs_create, 0);
7313 rb_define_method (c_guestfs, \"close\", ruby_guestfs_close, 0);
7316 (* Define the rest of the methods. *)
7318 fun (name, style, _, _, _, _, _) ->
7319 pr " rb_define_method (c_guestfs, \"%s\",\n" name;
7320 pr " ruby_guestfs_%s, %d);\n" name (List.length (snd style))
7325 (* Ruby code to return an LVM struct list. *)
7326 and generate_ruby_lvm_code typ cols =
7327 pr " VALUE rv = rb_ary_new2 (r->len);\n";
7329 pr " for (i = 0; i < r->len; ++i) {\n";
7330 pr " VALUE hv = rb_hash_new ();\n";
7334 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_str_new2 (r->val[i].%s));\n" name name
7336 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_str_new (r->val[i].%s, 32));\n" name name
7339 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), ULL2NUM (r->val[i].%s));\n" name name
7340 | name, `OptPercent ->
7341 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_dbl2big (r->val[i].%s));\n" name name
7343 pr " rb_ary_push (rv, hv);\n";
7345 pr " guestfs_free_lvm_%s_list (r);\n" typ;
7348 (* Ruby code to return a dirent struct list. *)
7349 and generate_ruby_dirent_code typ cols =
7350 pr " VALUE rv = rb_ary_new2 (r->len);\n";
7352 pr " for (i = 0; i < r->len; ++i) {\n";
7353 pr " VALUE hv = rb_hash_new ();\n";
7357 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_str_new2 (r->val[i].%s));\n" name name
7358 | name, (`Char|`Int) ->
7359 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), ULL2NUM (r->val[i].%s));\n" name name
7361 pr " rb_ary_push (rv, hv);\n";
7363 pr " guestfs_free_%s_list (r);\n" typ;
7366 (* Generate Java bindings GuestFS.java file. *)
7367 and generate_java_java () =
7368 generate_header CStyle LGPLv2;
7371 package com.redhat.et.libguestfs;
7373 import java.util.HashMap;
7374 import com.redhat.et.libguestfs.LibGuestFSException;
7375 import com.redhat.et.libguestfs.PV;
7376 import com.redhat.et.libguestfs.VG;
7377 import com.redhat.et.libguestfs.LV;
7378 import com.redhat.et.libguestfs.Stat;
7379 import com.redhat.et.libguestfs.StatVFS;
7380 import com.redhat.et.libguestfs.IntBool;
7381 import com.redhat.et.libguestfs.Dirent;
7384 * The GuestFS object is a libguestfs handle.
7388 public class GuestFS {
7389 // Load the native code.
7391 System.loadLibrary (\"guestfs_jni\");
7395 * The native guestfs_h pointer.
7400 * Create a libguestfs handle.
7402 * @throws LibGuestFSException
7404 public GuestFS () throws LibGuestFSException
7408 private native long _create () throws LibGuestFSException;
7411 * Close a libguestfs handle.
7413 * You can also leave handles to be collected by the garbage
7414 * collector, but this method ensures that the resources used
7415 * by the handle are freed up immediately. If you call any
7416 * other methods after closing the handle, you will get an
7419 * @throws LibGuestFSException
7421 public void close () throws LibGuestFSException
7427 private native void _close (long g) throws LibGuestFSException;
7429 public void finalize () throws LibGuestFSException
7437 fun (name, style, _, flags, _, shortdesc, longdesc) ->
7438 if not (List.mem NotInDocs flags); then (
7439 let doc = replace_str longdesc "C<guestfs_" "C<g." in
7441 if List.mem ProtocolLimitWarning flags then
7442 doc ^ "\n\n" ^ protocol_limit_warning
7445 if List.mem DangerWillRobinson flags then
7446 doc ^ "\n\n" ^ danger_will_robinson
7448 let doc = pod2text ~width:60 name doc in
7449 let doc = List.map ( (* RHBZ#501883 *)
7452 | nonempty -> nonempty
7454 let doc = String.concat "\n * " doc in
7457 pr " * %s\n" shortdesc;
7460 pr " * @throws LibGuestFSException\n";
7464 generate_java_prototype ~public:true ~semicolon:false name style;
7467 pr " if (g == 0)\n";
7468 pr " throw new LibGuestFSException (\"%s: handle is closed\");\n"
7471 if fst style <> RErr then pr "return ";
7473 generate_call_args ~handle:"g" (snd style);
7477 generate_java_prototype ~privat:true ~native:true name style;
7484 and generate_java_prototype ?(public=false) ?(privat=false) ?(native=false)
7485 ?(semicolon=true) name style =
7486 if privat then pr "private ";
7487 if public then pr "public ";
7488 if native then pr "native ";
7491 (match fst style with
7492 | RErr -> pr "void ";
7493 | RInt _ -> pr "int ";
7494 | RInt64 _ -> pr "long ";
7495 | RBool _ -> pr "boolean ";
7496 | RConstString _ | RString _ -> pr "String ";
7497 | RStringList _ -> pr "String[] ";
7498 | RIntBool _ -> pr "IntBool ";
7499 | RPVList _ -> pr "PV[] ";
7500 | RVGList _ -> pr "VG[] ";
7501 | RLVList _ -> pr "LV[] ";
7502 | RStat _ -> pr "Stat ";
7503 | RStatVFS _ -> pr "StatVFS ";
7504 | RHashtable _ -> pr "HashMap<String,String> ";
7505 | RDirentList _ -> pr "Dirent[] ";
7508 if native then pr "_%s " name else pr "%s " name;
7510 let needs_comma = ref false in
7519 if !needs_comma then pr ", ";
7520 needs_comma := true;
7537 pr " throws LibGuestFSException";
7538 if semicolon then pr ";"
7540 and generate_java_struct typ cols =
7541 generate_header CStyle LGPLv2;
7544 package com.redhat.et.libguestfs;
7547 * Libguestfs %s structure.
7558 | name, `UUID -> pr " public String %s;\n" name
7560 | name, `Int -> pr " public long %s;\n" name
7561 | name, `Char -> pr " public char %s;\n" name
7562 | name, `OptPercent ->
7563 pr " /* The next field is [0..100] or -1 meaning 'not present': */\n";
7564 pr " public float %s;\n" name
7569 and generate_java_c () =
7570 generate_header CStyle LGPLv2;
7577 #include \"com_redhat_et_libguestfs_GuestFS.h\"
7578 #include \"guestfs.h\"
7580 /* Note that this function returns. The exception is not thrown
7581 * until after the wrapper function returns.
7584 throw_exception (JNIEnv *env, const char *msg)
7587 cl = (*env)->FindClass (env,
7588 \"com/redhat/et/libguestfs/LibGuestFSException\");
7589 (*env)->ThrowNew (env, cl, msg);
7592 JNIEXPORT jlong JNICALL
7593 Java_com_redhat_et_libguestfs_GuestFS__1create
7594 (JNIEnv *env, jobject obj)
7598 g = guestfs_create ();
7600 throw_exception (env, \"GuestFS.create: failed to allocate handle\");
7603 guestfs_set_error_handler (g, NULL, NULL);
7604 return (jlong) (long) g;
7607 JNIEXPORT void JNICALL
7608 Java_com_redhat_et_libguestfs_GuestFS__1close
7609 (JNIEnv *env, jobject obj, jlong jg)
7611 guestfs_h *g = (guestfs_h *) (long) jg;
7618 fun (name, style, _, _, _, _, _) ->
7620 (match fst style with
7621 | RErr -> pr "void ";
7622 | RInt _ -> pr "jint ";
7623 | RInt64 _ -> pr "jlong ";
7624 | RBool _ -> pr "jboolean ";
7625 | RConstString _ | RString _ -> pr "jstring ";
7626 | RIntBool _ | RStat _ | RStatVFS _ | RHashtable _ ->
7628 | RStringList _ | RPVList _ | RVGList _ | RLVList _ | RDirentList _ ->
7632 pr "Java_com_redhat_et_libguestfs_GuestFS_";
7633 pr "%s" (replace_str ("_" ^ name) "_" "_1");
7635 pr " (JNIEnv *env, jobject obj, jlong jg";
7642 pr ", jstring j%s" n
7644 pr ", jobjectArray j%s" n
7646 pr ", jboolean j%s" n
7652 pr " guestfs_h *g = (guestfs_h *) (long) jg;\n";
7653 let error_code, no_ret =
7654 match fst style with
7655 | RErr -> pr " int r;\n"; "-1", ""
7657 | RInt _ -> pr " int r;\n"; "-1", "0"
7658 | RInt64 _ -> pr " int64_t r;\n"; "-1", "0"
7659 | RConstString _ -> pr " const char *r;\n"; "NULL", "NULL"
7661 pr " jstring jr;\n";
7662 pr " char *r;\n"; "NULL", "NULL"
7664 pr " jobjectArray jr;\n";
7667 pr " jstring jstr;\n";
7668 pr " char **r;\n"; "NULL", "NULL"
7670 pr " jobject jr;\n";
7672 pr " jfieldID fl;\n";
7673 pr " struct guestfs_int_bool *r;\n"; "NULL", "NULL"
7675 pr " jobject jr;\n";
7677 pr " jfieldID fl;\n";
7678 pr " struct guestfs_stat *r;\n"; "NULL", "NULL"
7680 pr " jobject jr;\n";
7682 pr " jfieldID fl;\n";
7683 pr " struct guestfs_statvfs *r;\n"; "NULL", "NULL"
7685 pr " jobjectArray jr;\n";
7687 pr " jfieldID fl;\n";
7688 pr " jobject jfl;\n";
7689 pr " struct guestfs_lvm_pv_list *r;\n"; "NULL", "NULL"
7691 pr " jobjectArray jr;\n";
7693 pr " jfieldID fl;\n";
7694 pr " jobject jfl;\n";
7695 pr " struct guestfs_lvm_vg_list *r;\n"; "NULL", "NULL"
7697 pr " jobjectArray jr;\n";
7699 pr " jfieldID fl;\n";
7700 pr " jobject jfl;\n";
7701 pr " struct guestfs_lvm_lv_list *r;\n"; "NULL", "NULL"
7702 | RHashtable _ -> pr " char **r;\n"; "NULL", "NULL"
7704 pr " jobjectArray jr;\n";
7706 pr " jfieldID fl;\n";
7707 pr " jobject jfl;\n";
7708 pr " struct guestfs_dirent_list *r;\n"; "NULL", "NULL" in
7715 pr " const char *%s;\n" n
7717 pr " int %s_len;\n" n;
7718 pr " const char **%s;\n" n
7725 (match fst style with
7726 | RStringList _ | RPVList _ | RVGList _ | RLVList _
7727 | RDirentList _ -> true
7728 | RErr | RBool _ | RInt _ | RInt64 _ | RConstString _
7729 | RString _ | RIntBool _ | RStat _ | RStatVFS _
7730 | RHashtable _ -> false) ||
7731 List.exists (function StringList _ -> true | _ -> false) (snd style) in
7737 (* Get the parameters. *)
7743 pr " %s = (*env)->GetStringUTFChars (env, j%s, NULL);\n" n n
7745 (* This is completely undocumented, but Java null becomes
7748 pr " %s = j%s ? (*env)->GetStringUTFChars (env, j%s, NULL) : NULL;\n" n n n
7750 pr " %s_len = (*env)->GetArrayLength (env, j%s);\n" n n;
7751 pr " %s = guestfs_safe_malloc (g, sizeof (char *) * (%s_len+1));\n" n n;
7752 pr " for (i = 0; i < %s_len; ++i) {\n" n;
7753 pr " jobject o = (*env)->GetObjectArrayElement (env, j%s, i);\n"
7755 pr " %s[i] = (*env)->GetStringUTFChars (env, o, NULL);\n" n;
7757 pr " %s[%s_len] = NULL;\n" n n;
7760 pr " %s = j%s;\n" n n
7763 (* Make the call. *)
7764 pr " r = guestfs_%s " name;
7765 generate_call_args ~handle:"g" (snd style);
7768 (* Release the parameters. *)
7774 pr " (*env)->ReleaseStringUTFChars (env, j%s, %s);\n" n n
7777 pr " (*env)->ReleaseStringUTFChars (env, j%s, %s);\n" n n
7779 pr " for (i = 0; i < %s_len; ++i) {\n" n;
7780 pr " jobject o = (*env)->GetObjectArrayElement (env, j%s, i);\n"
7782 pr " (*env)->ReleaseStringUTFChars (env, o, %s[i]);\n" n;
7784 pr " free (%s);\n" n
7789 (* Check for errors. *)
7790 pr " if (r == %s) {\n" error_code;
7791 pr " throw_exception (env, guestfs_last_error (g));\n";
7792 pr " return %s;\n" no_ret;
7796 (match fst style with
7798 | RInt _ -> pr " return (jint) r;\n"
7799 | RBool _ -> pr " return (jboolean) r;\n"
7800 | RInt64 _ -> pr " return (jlong) r;\n"
7801 | RConstString _ -> pr " return (*env)->NewStringUTF (env, r);\n"
7803 pr " jr = (*env)->NewStringUTF (env, r);\n";
7807 pr " for (r_len = 0; r[r_len] != NULL; ++r_len) ;\n";
7808 pr " cl = (*env)->FindClass (env, \"java/lang/String\");\n";
7809 pr " jstr = (*env)->NewStringUTF (env, \"\");\n";
7810 pr " jr = (*env)->NewObjectArray (env, r_len, cl, jstr);\n";
7811 pr " for (i = 0; i < r_len; ++i) {\n";
7812 pr " jstr = (*env)->NewStringUTF (env, r[i]);\n";
7813 pr " (*env)->SetObjectArrayElement (env, jr, i, jstr);\n";
7814 pr " free (r[i]);\n";
7819 pr " cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/IntBool\");\n";
7820 pr " jr = (*env)->AllocObject (env, cl);\n";
7821 pr " fl = (*env)->GetFieldID (env, cl, \"i\", \"I\");\n";
7822 pr " (*env)->SetIntField (env, jr, fl, r->i);\n";
7823 pr " fl = (*env)->GetFieldID (env, cl, \"i\", \"Z\");\n";
7824 pr " (*env)->SetBooleanField (env, jr, fl, r->b);\n";
7825 pr " guestfs_free_int_bool (r);\n";
7828 pr " cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/Stat\");\n";
7829 pr " jr = (*env)->AllocObject (env, cl);\n";
7833 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"J\");\n"
7835 pr " (*env)->SetLongField (env, jr, fl, r->%s);\n" name;
7840 pr " cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/StatVFS\");\n";
7841 pr " jr = (*env)->AllocObject (env, cl);\n";
7845 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"J\");\n"
7847 pr " (*env)->SetLongField (env, jr, fl, r->%s);\n" name;
7852 generate_java_lvm_return "pv" "PV" pv_cols
7854 generate_java_lvm_return "vg" "VG" vg_cols
7856 generate_java_lvm_return "lv" "LV" lv_cols
7859 pr " throw_exception (env, \"%s: internal error: please let us know how to make a Java HashMap from JNI bindings!\");\n" name;
7860 pr " return NULL;\n"
7862 generate_java_dirent_return "dirent" "Dirent" dirent_cols
7869 and generate_java_lvm_return typ jtyp cols =
7870 pr " cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/%s\");\n" jtyp;
7871 pr " jr = (*env)->NewObjectArray (env, r->len, cl, NULL);\n";
7872 pr " for (i = 0; i < r->len; ++i) {\n";
7873 pr " jfl = (*env)->AllocObject (env, cl);\n";
7877 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
7878 pr " (*env)->SetObjectField (env, jfl, fl, (*env)->NewStringUTF (env, r->val[i].%s));\n" name;
7881 pr " char s[33];\n";
7882 pr " memcpy (s, r->val[i].%s, 32);\n" name;
7884 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
7885 pr " (*env)->SetObjectField (env, jfl, fl, (*env)->NewStringUTF (env, s));\n";
7887 | name, (`Bytes|`Int) ->
7888 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"J\");\n" name;
7889 pr " (*env)->SetLongField (env, jfl, fl, r->val[i].%s);\n" name;
7890 | name, `OptPercent ->
7891 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"F\");\n" name;
7892 pr " (*env)->SetFloatField (env, jfl, fl, r->val[i].%s);\n" name;
7894 pr " (*env)->SetObjectArrayElement (env, jfl, i, jfl);\n";
7896 pr " guestfs_free_lvm_%s_list (r);\n" typ;
7899 and generate_java_dirent_return typ jtyp cols =
7900 pr " cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/%s\");\n" jtyp;
7901 pr " jr = (*env)->NewObjectArray (env, r->len, cl, NULL);\n";
7902 pr " for (i = 0; i < r->len; ++i) {\n";
7903 pr " jfl = (*env)->AllocObject (env, cl);\n";
7907 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
7908 pr " (*env)->SetObjectField (env, jfl, fl, (*env)->NewStringUTF (env, r->val[i].%s));\n" name;
7909 | name, (`Char|`Int) ->
7910 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"J\");\n" name;
7911 pr " (*env)->SetLongField (env, jfl, fl, r->val[i].%s);\n" name;
7913 pr " (*env)->SetObjectArrayElement (env, jfl, i, jfl);\n";
7915 pr " guestfs_free_%s_list (r);\n" typ;
7918 and generate_haskell_hs () =
7919 generate_header HaskellStyle LGPLv2;
7921 (* XXX We only know how to generate partial FFI for Haskell
7922 * at the moment. Please help out!
7924 let can_generate style =
7928 | RInt64 _, _ -> true
7940 | RDirentList _, _ -> false in
7943 {-# INCLUDE <guestfs.h> #-}
7944 {-# LANGUAGE ForeignFunctionInterface #-}
7949 (* List out the names of the actions we want to export. *)
7951 fun (name, style, _, _, _, _, _) ->
7952 if can_generate style then pr ",\n %s" name
7959 import Foreign.C.Types
7961 import Control.Exception
7962 import Data.Typeable
7964 data GuestfsS = GuestfsS -- represents the opaque C struct
7965 type GuestfsP = Ptr GuestfsS -- guestfs_h *
7966 type GuestfsH = ForeignPtr GuestfsS -- guestfs_h * with attached finalizer
7968 -- XXX define properly later XXX
7972 data IntBool = IntBool
7974 data StatVFS = StatVFS
7975 data Hashtable = Hashtable
7977 foreign import ccall unsafe \"guestfs_create\" c_create
7979 foreign import ccall unsafe \"&guestfs_close\" c_close
7980 :: FunPtr (GuestfsP -> IO ())
7981 foreign import ccall unsafe \"guestfs_set_error_handler\" c_set_error_handler
7982 :: GuestfsP -> Ptr CInt -> Ptr CInt -> IO ()
7984 create :: IO GuestfsH
7987 c_set_error_handler p nullPtr nullPtr
7988 h <- newForeignPtr c_close p
7991 foreign import ccall unsafe \"guestfs_last_error\" c_last_error
7992 :: GuestfsP -> IO CString
7994 -- last_error :: GuestfsH -> IO (Maybe String)
7995 -- last_error h = do
7996 -- str <- withForeignPtr h (\\p -> c_last_error p)
7997 -- maybePeek peekCString str
7999 last_error :: GuestfsH -> IO (String)
8001 str <- withForeignPtr h (\\p -> c_last_error p)
8003 then return \"no error\"
8004 else peekCString str
8008 (* Generate wrappers for each foreign function. *)
8010 fun (name, style, _, _, _, _, _) ->
8011 if can_generate style then (
8012 pr "foreign import ccall unsafe \"guestfs_%s\" c_%s\n" name name;
8014 generate_haskell_prototype ~handle:"GuestfsP" style;
8018 generate_haskell_prototype ~handle:"GuestfsH" ~hs:true style;
8020 pr "%s %s = do\n" name
8021 (String.concat " " ("h" :: List.map name_of_argt (snd style)));
8023 (* Convert pointer arguments using with* functions. *)
8028 | String n -> pr "withCString %s $ \\%s -> " n n
8029 | OptString n -> pr "maybeWith withCString %s $ \\%s -> " n n
8030 | StringList n -> pr "withMany withCString %s $ \\%s -> withArray0 nullPtr %s $ \\%s -> " n n n n
8031 | Bool _ | Int _ -> ()
8033 (* Convert integer arguments. *)
8037 | Bool n -> sprintf "(fromBool %s)" n
8038 | Int n -> sprintf "(fromIntegral %s)" n
8039 | FileIn n | FileOut n | String n | OptString n | StringList n -> n
8041 pr "withForeignPtr h (\\p -> c_%s %s)\n" name
8042 (String.concat " " ("p" :: args));
8043 (match fst style with
8044 | RErr | RInt _ | RInt64 _ | RBool _ ->
8045 pr " if (r == -1)\n";
8047 pr " err <- last_error h\n";
8049 | RConstString _ | RString _ | RStringList _ | RIntBool _
8050 | RPVList _ | RVGList _ | RLVList _ | RStat _ | RStatVFS _
8051 | RHashtable _ | RDirentList _ ->
8052 pr " if (r == nullPtr)\n";
8054 pr " err <- last_error h\n";
8057 (match fst style with
8059 pr " else return ()\n"
8061 pr " else return (fromIntegral r)\n"
8063 pr " else return (fromIntegral r)\n"
8065 pr " else return (toBool r)\n"
8077 pr " else return ()\n" (* XXXXXXXXXXXXXXXXXXXX *)
8083 and generate_haskell_prototype ~handle ?(hs = false) style =
8085 let string = if hs then "String" else "CString" in
8086 let int = if hs then "Int" else "CInt" in
8087 let bool = if hs then "Bool" else "CInt" in
8088 let int64 = if hs then "Integer" else "Int64" in
8092 | String _ -> pr "%s" string
8093 | OptString _ -> if hs then pr "Maybe String" else pr "CString"
8094 | StringList _ -> if hs then pr "[String]" else pr "Ptr CString"
8095 | Bool _ -> pr "%s" bool
8096 | Int _ -> pr "%s" int
8097 | FileIn _ -> pr "%s" string
8098 | FileOut _ -> pr "%s" string
8103 (match fst style with
8104 | RErr -> if not hs then pr "CInt"
8105 | RInt _ -> pr "%s" int
8106 | RInt64 _ -> pr "%s" int64
8107 | RBool _ -> pr "%s" bool
8108 | RConstString _ -> pr "%s" string
8109 | RString _ -> pr "%s" string
8110 | RStringList _ -> pr "[%s]" string
8111 | RIntBool _ -> pr "IntBool"
8112 | RPVList _ -> pr "[PV]"
8113 | RVGList _ -> pr "[VG]"
8114 | RLVList _ -> pr "[LV]"
8115 | RStat _ -> pr "Stat"
8116 | RStatVFS _ -> pr "StatVFS"
8117 | RHashtable _ -> pr "Hashtable"
8118 | RDirentList _ -> pr "[Dirent]"
8122 and generate_bindtests () =
8123 generate_header CStyle LGPLv2;
8128 #include <inttypes.h>
8131 #include \"guestfs.h\"
8132 #include \"guestfs_protocol.h\"
8134 #define error guestfs_error
8137 print_strings (char * const* const argv)
8142 for (argc = 0; argv[argc] != NULL; ++argc) {
8143 if (argc > 0) printf (\", \");
8144 printf (\"\\\"%%s\\\"\", argv[argc]);
8149 /* The test0 function prints its parameters to stdout. */
8153 match test_functions with
8154 | [] -> assert false
8155 | test0 :: tests -> test0, tests in
8158 let (name, style, _, _, _, _, _) = test0 in
8159 generate_prototype ~extern:false ~semicolon:false ~newline:true
8160 ~handle:"g" ~prefix:"guestfs_" name style;
8166 | FileOut n -> pr " printf (\"%%s\\n\", %s);\n" n
8167 | OptString n -> pr " printf (\"%%s\\n\", %s ? %s : \"null\");\n" n n
8168 | StringList n -> pr " print_strings (%s);\n" n
8169 | Bool n -> pr " printf (\"%%s\\n\", %s ? \"true\" : \"false\");\n" n
8170 | Int n -> pr " printf (\"%%d\\n\", %s);\n" n
8172 pr " /* Java changes stdout line buffering so we need this: */\n";
8173 pr " fflush (stdout);\n";
8179 fun (name, style, _, _, _, _, _) ->
8180 if String.sub name (String.length name - 3) 3 <> "err" then (
8181 pr "/* Test normal return. */\n";
8182 generate_prototype ~extern:false ~semicolon:false ~newline:true
8183 ~handle:"g" ~prefix:"guestfs_" name style;
8185 (match fst style with
8190 pr " sscanf (val, \"%%d\", &r);\n";
8194 pr " sscanf (val, \"%%\" SCNi64, &r);\n";
8197 pr " return strcmp (val, \"true\") == 0;\n"
8199 (* Can't return the input string here. Return a static
8200 * string so we ensure we get a segfault if the caller
8203 pr " return \"static string\";\n"
8205 pr " return strdup (val);\n"
8207 pr " char **strs;\n";
8209 pr " sscanf (val, \"%%d\", &n);\n";
8210 pr " strs = malloc ((n+1) * sizeof (char *));\n";
8211 pr " for (i = 0; i < n; ++i) {\n";
8212 pr " strs[i] = malloc (16);\n";
8213 pr " snprintf (strs[i], 16, \"%%d\", i);\n";
8215 pr " strs[n] = NULL;\n";
8216 pr " return strs;\n"
8218 pr " struct guestfs_int_bool *r;\n";
8219 pr " r = malloc (sizeof (struct guestfs_int_bool));\n";
8220 pr " sscanf (val, \"%%\" SCNi32, &r->i);\n";
8224 pr " struct guestfs_lvm_pv_list *r;\n";
8226 pr " r = malloc (sizeof (struct guestfs_lvm_pv_list));\n";
8227 pr " sscanf (val, \"%%d\", &r->len);\n";
8228 pr " r->val = calloc (r->len, sizeof (struct guestfs_lvm_pv));\n";
8229 pr " for (i = 0; i < r->len; ++i) {\n";
8230 pr " r->val[i].pv_name = malloc (16);\n";
8231 pr " snprintf (r->val[i].pv_name, 16, \"%%d\", i);\n";
8235 pr " struct guestfs_lvm_vg_list *r;\n";
8237 pr " r = malloc (sizeof (struct guestfs_lvm_vg_list));\n";
8238 pr " sscanf (val, \"%%d\", &r->len);\n";
8239 pr " r->val = calloc (r->len, sizeof (struct guestfs_lvm_vg));\n";
8240 pr " for (i = 0; i < r->len; ++i) {\n";
8241 pr " r->val[i].vg_name = malloc (16);\n";
8242 pr " snprintf (r->val[i].vg_name, 16, \"%%d\", i);\n";
8246 pr " struct guestfs_lvm_lv_list *r;\n";
8248 pr " r = malloc (sizeof (struct guestfs_lvm_lv_list));\n";
8249 pr " sscanf (val, \"%%d\", &r->len);\n";
8250 pr " r->val = calloc (r->len, sizeof (struct guestfs_lvm_lv));\n";
8251 pr " for (i = 0; i < r->len; ++i) {\n";
8252 pr " r->val[i].lv_name = malloc (16);\n";
8253 pr " snprintf (r->val[i].lv_name, 16, \"%%d\", i);\n";
8257 pr " struct guestfs_stat *r;\n";
8258 pr " r = calloc (1, sizeof (*r));\n";
8259 pr " sscanf (val, \"%%\" SCNi64, &r->dev);\n";
8262 pr " struct guestfs_statvfs *r;\n";
8263 pr " r = calloc (1, sizeof (*r));\n";
8264 pr " sscanf (val, \"%%\" SCNi64, &r->bsize);\n";
8267 pr " char **strs;\n";
8269 pr " sscanf (val, \"%%d\", &n);\n";
8270 pr " strs = malloc ((n*2+1) * sizeof (char *));\n";
8271 pr " for (i = 0; i < n; ++i) {\n";
8272 pr " strs[i*2] = malloc (16);\n";
8273 pr " strs[i*2+1] = malloc (16);\n";
8274 pr " snprintf (strs[i*2], 16, \"%%d\", i);\n";
8275 pr " snprintf (strs[i*2+1], 16, \"%%d\", i);\n";
8277 pr " strs[n*2] = NULL;\n";
8278 pr " return strs;\n"
8280 pr " struct guestfs_dirent_list *r;\n";
8282 pr " r = malloc (sizeof (struct guestfs_dirent_list));\n";
8283 pr " sscanf (val, \"%%d\", &r->len);\n";
8284 pr " r->val = calloc (r->len, sizeof (struct guestfs_dirent));\n";
8285 pr " for (i = 0; i < r->len; ++i)\n";
8286 pr " r->val[i].ino = i;\n";
8292 pr "/* Test error return. */\n";
8293 generate_prototype ~extern:false ~semicolon:false ~newline:true
8294 ~handle:"g" ~prefix:"guestfs_" name style;
8296 pr " error (g, \"error\");\n";
8297 (match fst style with
8298 | RErr | RInt _ | RInt64 _ | RBool _ ->
8301 | RString _ | RStringList _ | RIntBool _
8302 | RPVList _ | RVGList _ | RLVList _ | RStat _ | RStatVFS _
8305 pr " return NULL;\n"
8312 and generate_ocaml_bindtests () =
8313 generate_header OCamlStyle GPLv2;
8317 let g = Guestfs.create () in
8324 | CallString s -> "\"" ^ s ^ "\""
8325 | CallOptString None -> "None"
8326 | CallOptString (Some s) -> sprintf "(Some \"%s\")" s
8327 | CallStringList xs ->
8328 "[|" ^ String.concat ";" (List.map (sprintf "\"%s\"") xs) ^ "|]"
8329 | CallInt i when i >= 0 -> string_of_int i
8330 | CallInt i (* when i < 0 *) -> "(" ^ string_of_int i ^ ")"
8331 | CallBool b -> string_of_bool b
8336 generate_lang_bindtests (
8337 fun f args -> pr " Guestfs.%s g %s;\n" f (mkargs args)
8340 pr "print_endline \"EOF\"\n"
8342 and generate_perl_bindtests () =
8343 pr "#!/usr/bin/perl -w\n";
8344 generate_header HashStyle GPLv2;
8351 my $g = Sys::Guestfs->new ();
8355 String.concat ", " (
8358 | CallString s -> "\"" ^ s ^ "\""
8359 | CallOptString None -> "undef"
8360 | CallOptString (Some s) -> sprintf "\"%s\"" s
8361 | CallStringList xs ->
8362 "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
8363 | CallInt i -> string_of_int i
8364 | CallBool b -> if b then "1" else "0"
8369 generate_lang_bindtests (
8370 fun f args -> pr "$g->%s (%s);\n" f (mkargs args)
8373 pr "print \"EOF\\n\"\n"
8375 and generate_python_bindtests () =
8376 generate_header HashStyle GPLv2;
8381 g = guestfs.GuestFS ()
8385 String.concat ", " (
8388 | CallString s -> "\"" ^ s ^ "\""
8389 | CallOptString None -> "None"
8390 | CallOptString (Some s) -> sprintf "\"%s\"" s
8391 | CallStringList xs ->
8392 "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
8393 | CallInt i -> string_of_int i
8394 | CallBool b -> if b then "1" else "0"
8399 generate_lang_bindtests (
8400 fun f args -> pr "g.%s (%s)\n" f (mkargs args)
8403 pr "print \"EOF\"\n"
8405 and generate_ruby_bindtests () =
8406 generate_header HashStyle GPLv2;
8411 g = Guestfs::create()
8415 String.concat ", " (
8418 | CallString s -> "\"" ^ s ^ "\""
8419 | CallOptString None -> "nil"
8420 | CallOptString (Some s) -> sprintf "\"%s\"" s
8421 | CallStringList xs ->
8422 "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
8423 | CallInt i -> string_of_int i
8424 | CallBool b -> string_of_bool b
8429 generate_lang_bindtests (
8430 fun f args -> pr "g.%s(%s)\n" f (mkargs args)
8433 pr "print \"EOF\\n\"\n"
8435 and generate_java_bindtests () =
8436 generate_header CStyle GPLv2;
8439 import com.redhat.et.libguestfs.*;
8441 public class Bindtests {
8442 public static void main (String[] argv)
8445 GuestFS g = new GuestFS ();
8449 String.concat ", " (
8452 | CallString s -> "\"" ^ s ^ "\""
8453 | CallOptString None -> "null"
8454 | CallOptString (Some s) -> sprintf "\"%s\"" s
8455 | CallStringList xs ->
8457 String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "}"
8458 | CallInt i -> string_of_int i
8459 | CallBool b -> string_of_bool b
8464 generate_lang_bindtests (
8465 fun f args -> pr " g.%s (%s);\n" f (mkargs args)
8469 System.out.println (\"EOF\");
8471 catch (Exception exn) {
8472 System.err.println (exn);
8479 and generate_haskell_bindtests () =
8480 generate_header HaskellStyle GPLv2;
8483 module Bindtests where
8484 import qualified Guestfs
8494 | CallString s -> "\"" ^ s ^ "\""
8495 | CallOptString None -> "Nothing"
8496 | CallOptString (Some s) -> sprintf "(Just \"%s\")" s
8497 | CallStringList xs ->
8498 "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
8499 | CallInt i when i < 0 -> "(" ^ string_of_int i ^ ")"
8500 | CallInt i -> string_of_int i
8501 | CallBool true -> "True"
8502 | CallBool false -> "False"
8507 generate_lang_bindtests (
8508 fun f args -> pr " Guestfs.%s g %s\n" f (mkargs args)
8511 pr " putStrLn \"EOF\"\n"
8513 (* Language-independent bindings tests - we do it this way to
8514 * ensure there is parity in testing bindings across all languages.
8516 and generate_lang_bindtests call =
8517 call "test0" [CallString "abc"; CallOptString (Some "def");
8518 CallStringList []; CallBool false;
8519 CallInt 0; CallString "123"; CallString "456"];
8520 call "test0" [CallString "abc"; CallOptString None;
8521 CallStringList []; CallBool false;
8522 CallInt 0; CallString "123"; CallString "456"];
8523 call "test0" [CallString ""; CallOptString (Some "def");
8524 CallStringList []; CallBool false;
8525 CallInt 0; CallString "123"; CallString "456"];
8526 call "test0" [CallString ""; CallOptString (Some "");
8527 CallStringList []; CallBool false;
8528 CallInt 0; CallString "123"; CallString "456"];
8529 call "test0" [CallString "abc"; CallOptString (Some "def");
8530 CallStringList ["1"]; CallBool false;
8531 CallInt 0; CallString "123"; CallString "456"];
8532 call "test0" [CallString "abc"; CallOptString (Some "def");
8533 CallStringList ["1"; "2"]; CallBool false;
8534 CallInt 0; CallString "123"; CallString "456"];
8535 call "test0" [CallString "abc"; CallOptString (Some "def");
8536 CallStringList ["1"]; CallBool true;
8537 CallInt 0; CallString "123"; CallString "456"];
8538 call "test0" [CallString "abc"; CallOptString (Some "def");
8539 CallStringList ["1"]; CallBool false;
8540 CallInt (-1); CallString "123"; CallString "456"];
8541 call "test0" [CallString "abc"; CallOptString (Some "def");
8542 CallStringList ["1"]; CallBool false;
8543 CallInt (-2); CallString "123"; CallString "456"];
8544 call "test0" [CallString "abc"; CallOptString (Some "def");
8545 CallStringList ["1"]; CallBool false;
8546 CallInt 1; CallString "123"; CallString "456"];
8547 call "test0" [CallString "abc"; CallOptString (Some "def");
8548 CallStringList ["1"]; CallBool false;
8549 CallInt 2; CallString "123"; CallString "456"];
8550 call "test0" [CallString "abc"; CallOptString (Some "def");
8551 CallStringList ["1"]; CallBool false;
8552 CallInt 4095; CallString "123"; CallString "456"];
8553 call "test0" [CallString "abc"; CallOptString (Some "def");
8554 CallStringList ["1"]; CallBool false;
8555 CallInt 0; CallString ""; CallString ""]
8557 (* XXX Add here tests of the return and error functions. *)
8559 (* This is used to generate the src/MAX_PROC_NR file which
8560 * contains the maximum procedure number, a surrogate for the
8561 * ABI version number. See src/Makefile.am for the details.
8563 and generate_max_proc_nr () =
8564 let proc_nrs = List.map (
8565 fun (_, _, proc_nr, _, _, _, _) -> proc_nr
8566 ) daemon_functions in
8568 let max_proc_nr = List.fold_left max 0 proc_nrs in
8570 pr "%d\n" max_proc_nr
8572 let output_to filename =
8573 let filename_new = filename ^ ".new" in
8574 chan := open_out filename_new;
8579 (* Is the new file different from the current file? *)
8580 if Sys.file_exists filename && files_equal filename filename_new then
8581 Unix.unlink filename_new (* same, so skip it *)
8583 (* different, overwrite old one *)
8584 (try Unix.chmod filename 0o644 with Unix.Unix_error _ -> ());
8585 Unix.rename filename_new filename;
8586 Unix.chmod filename 0o444;
8587 printf "written %s\n%!" filename;
8596 if not (Sys.file_exists "configure.ac") then (
8598 You are probably running this from the wrong directory.
8599 Run it from the top source directory using the command
8605 let close = output_to "src/guestfs_protocol.x" in
8609 let close = output_to "src/guestfs-structs.h" in
8610 generate_structs_h ();
8613 let close = output_to "src/guestfs-actions.h" in
8614 generate_actions_h ();
8617 let close = output_to "src/guestfs-actions.c" in
8618 generate_client_actions ();
8621 let close = output_to "daemon/actions.h" in
8622 generate_daemon_actions_h ();
8625 let close = output_to "daemon/stubs.c" in
8626 generate_daemon_actions ();
8629 let close = output_to "daemon/names.c" in
8630 generate_daemon_names ();
8633 let close = output_to "capitests/tests.c" in
8637 let close = output_to "src/guestfs-bindtests.c" in
8638 generate_bindtests ();
8641 let close = output_to "fish/cmds.c" in
8642 generate_fish_cmds ();
8645 let close = output_to "fish/completion.c" in
8646 generate_fish_completion ();
8649 let close = output_to "guestfs-structs.pod" in
8650 generate_structs_pod ();
8653 let close = output_to "guestfs-actions.pod" in
8654 generate_actions_pod ();
8657 let close = output_to "guestfish-actions.pod" in
8658 generate_fish_actions_pod ();
8661 let close = output_to "ocaml/guestfs.mli" in
8662 generate_ocaml_mli ();
8665 let close = output_to "ocaml/guestfs.ml" in
8666 generate_ocaml_ml ();
8669 let close = output_to "ocaml/guestfs_c_actions.c" in
8670 generate_ocaml_c ();
8673 let close = output_to "ocaml/bindtests.ml" in
8674 generate_ocaml_bindtests ();
8677 let close = output_to "perl/Guestfs.xs" in
8678 generate_perl_xs ();
8681 let close = output_to "perl/lib/Sys/Guestfs.pm" in
8682 generate_perl_pm ();
8685 let close = output_to "perl/bindtests.pl" in
8686 generate_perl_bindtests ();
8689 let close = output_to "python/guestfs-py.c" in
8690 generate_python_c ();
8693 let close = output_to "python/guestfs.py" in
8694 generate_python_py ();
8697 let close = output_to "python/bindtests.py" in
8698 generate_python_bindtests ();
8701 let close = output_to "ruby/ext/guestfs/_guestfs.c" in
8705 let close = output_to "ruby/bindtests.rb" in
8706 generate_ruby_bindtests ();
8709 let close = output_to "java/com/redhat/et/libguestfs/GuestFS.java" in
8710 generate_java_java ();
8713 let close = output_to "java/com/redhat/et/libguestfs/PV.java" in
8714 generate_java_struct "PV" pv_cols;
8717 let close = output_to "java/com/redhat/et/libguestfs/VG.java" in
8718 generate_java_struct "VG" vg_cols;
8721 let close = output_to "java/com/redhat/et/libguestfs/LV.java" in
8722 generate_java_struct "LV" lv_cols;
8725 let close = output_to "java/com/redhat/et/libguestfs/Stat.java" in
8726 generate_java_struct "Stat" stat_cols;
8729 let close = output_to "java/com/redhat/et/libguestfs/StatVFS.java" in
8730 generate_java_struct "StatVFS" statvfs_cols;
8733 let close = output_to "java/com/redhat/et/libguestfs/Dirent.java" in
8734 generate_java_struct "Dirent" dirent_cols;
8737 let close = output_to "java/com_redhat_et_libguestfs_GuestFS.c" in
8741 let close = output_to "java/Bindtests.java" in
8742 generate_java_bindtests ();
8745 let close = output_to "haskell/Guestfs.hs" in
8746 generate_haskell_hs ();
8749 let close = output_to "haskell/Bindtests.hs" in
8750 generate_haskell_bindtests ();
8753 let close = output_to "src/MAX_PROC_NR" in
8754 generate_max_proc_nr ();
8757 (* Always generate this file last, and unconditionally. It's used
8758 * by the Makefile to know when we must re-run the generator.
8760 let chan = open_out "src/stamp-generator" in