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
87 and args = argt list (* Function parameters, guestfs handle is implicit. *)
89 (* Note in future we should allow a "variable args" parameter as
90 * the final parameter, to allow commands like
91 * chmod mode file [file(s)...]
92 * This is not implemented yet, but many commands (such as chmod)
93 * are currently defined with the argument order keeping this future
94 * possibility in mind.
97 | String of string (* const char *name, cannot be NULL *)
98 | OptString of string (* const char *name, may be NULL *)
99 | StringList of string(* list of strings (each string cannot be NULL) *)
100 | Bool of string (* boolean *)
101 | Int of string (* int (smallish ints, signed, <= 31 bits) *)
102 (* These are treated as filenames (simple string parameters) in
103 * the C API and bindings. But in the RPC protocol, we transfer
104 * the actual file content up to or down from the daemon.
105 * FileIn: local machine -> daemon (in request)
106 * FileOut: daemon -> local machine (in reply)
107 * In guestfish (only), the special name "-" means read from
108 * stdin or write to stdout.
114 | ProtocolLimitWarning (* display warning about protocol size limits *)
115 | DangerWillRobinson (* flags particularly dangerous commands *)
116 | FishAlias of string (* provide an alias for this cmd in guestfish *)
117 | FishAction of string (* call this function in guestfish *)
118 | NotInFish (* do not export via guestfish *)
119 | NotInDocs (* do not add this function to documentation *)
121 let protocol_limit_warning =
122 "Because of the message protocol, there is a transfer limit
123 of somewhere between 2MB and 4MB. To transfer large files you should use
126 let danger_will_robinson =
127 "B<This command is dangerous. Without careful use you
128 can easily destroy all your data>."
130 (* You can supply zero or as many tests as you want per API call.
132 * Note that the test environment has 3 block devices, of size 500MB,
133 * 50MB and 10MB (respectively /dev/sda, /dev/sdb, /dev/sdc), and
134 * a fourth squashfs block device with some known files on it (/dev/sdd).
136 * Note for partitioning purposes, the 500MB device has 1015 cylinders.
137 * Number of cylinders was 63 for IDE emulated disks with precisely
138 * the same size. How exactly this is calculated is a mystery.
140 * The squashfs block device (/dev/sdd) comes from images/test.sqsh.
142 * To be able to run the tests in a reasonable amount of time,
143 * the virtual machine and block devices are reused between tests.
144 * So don't try testing kill_subprocess :-x
146 * Between each test we blockdev-setrw, umount-all, lvm-remove-all.
148 * Don't assume anything about the previous contents of the block
149 * devices. Use 'Init*' to create some initial scenarios.
151 * You can add a prerequisite clause to any individual test. This
152 * is a run-time check, which, if it fails, causes the test to be
153 * skipped. Useful if testing a command which might not work on
154 * all variations of libguestfs builds. A test that has prerequisite
155 * of 'Always' is run unconditionally.
157 * In addition, packagers can skip individual tests by setting the
158 * environment variables: eg:
159 * SKIP_TEST_<CMD>_<NUM>=1 SKIP_TEST_COMMAND_3=1 (skips test #3 of command)
160 * SKIP_TEST_<CMD>=1 SKIP_TEST_ZEROFREE=1 (skips all zerofree tests)
162 type tests = (test_init * test_prereq * test) list
164 (* Run the command sequence and just expect nothing to fail. *)
166 (* Run the command sequence and expect the output of the final
167 * command to be the string.
169 | TestOutput of seq * string
170 (* Run the command sequence and expect the output of the final
171 * command to be the list of strings.
173 | TestOutputList of seq * string list
174 (* Run the command sequence and expect the output of the final
175 * command to be the list of block devices (could be either
176 * "/dev/sd.." or "/dev/hd.." form - we don't check the 5th
177 * character of each string).
179 | TestOutputListOfDevices of seq * string list
180 (* Run the command sequence and expect the output of the final
181 * command to be the integer.
183 | TestOutputInt of seq * int
184 (* Run the command sequence and expect the output of the final
185 * command to be a true value (!= 0 or != NULL).
187 | TestOutputTrue of seq
188 (* Run the command sequence and expect the output of the final
189 * command to be a false value (== 0 or == NULL, but not an error).
191 | TestOutputFalse of seq
192 (* Run the command sequence and expect the output of the final
193 * command to be a list of the given length (but don't care about
196 | TestOutputLength of seq * int
197 (* Run the command sequence and expect the output of the final
198 * command to be a structure.
200 | TestOutputStruct of seq * test_field_compare list
201 (* Run the command sequence and expect the final command (only)
204 | TestLastFail of seq
206 and test_field_compare =
207 | CompareWithInt of string * int
208 | CompareWithString of string * string
209 | CompareFieldsIntEq of string * string
210 | CompareFieldsStrEq of string * string
212 (* Test prerequisites. *)
214 (* Test always runs. *)
216 (* Test is currently disabled - eg. it fails, or it tests some
217 * unimplemented feature.
220 (* 'string' is some C code (a function body) that should return
221 * true or false. The test will run if the code returns true.
224 (* As for 'If' but the test runs _unless_ the code returns true. *)
227 (* Some initial scenarios for testing. *)
229 (* Do nothing, block devices could contain random stuff including
230 * LVM PVs, and some filesystems might be mounted. This is usually
234 (* Block devices are empty and no filesystems are mounted. *)
236 (* /dev/sda contains a single partition /dev/sda1, which is formatted
237 * as ext2, empty [except for lost+found] and mounted on /.
238 * /dev/sdb and /dev/sdc may have random content.
243 * /dev/sda1 (is a PV):
244 * /dev/VG/LV (size 8MB):
245 * formatted as ext2, empty [except for lost+found], mounted on /
246 * /dev/sdb and /dev/sdc may have random content.
250 (* Sequence of commands for testing. *)
252 and cmd = string list
254 (* Note about long descriptions: When referring to another
255 * action, use the format C<guestfs_other> (ie. the full name of
256 * the C function). This will be replaced as appropriate in other
259 * Apart from that, long descriptions are just perldoc paragraphs.
262 (* These test functions are used in the language binding tests. *)
264 let test_all_args = [
267 StringList "strlist";
274 let test_all_rets = [
275 (* except for RErr, which is tested thoroughly elsewhere *)
276 "test0rint", RInt "valout";
277 "test0rint64", RInt64 "valout";
278 "test0rbool", RBool "valout";
279 "test0rconststring", RConstString "valout";
280 "test0rstring", RString "valout";
281 "test0rstringlist", RStringList "valout";
282 "test0rintbool", RIntBool ("valout", "valout");
283 "test0rpvlist", RPVList "valout";
284 "test0rvglist", RVGList "valout";
285 "test0rlvlist", RLVList "valout";
286 "test0rstat", RStat "valout";
287 "test0rstatvfs", RStatVFS "valout";
288 "test0rhashtable", RHashtable "valout";
291 let test_functions = [
292 ("test0", (RErr, test_all_args), -1, [NotInFish; NotInDocs],
294 "internal test function - do not use",
296 This is an internal test function which is used to test whether
297 the automatically generated bindings can handle every possible
298 parameter type correctly.
300 It echos the contents of each parameter to stdout.
302 You probably don't want to call this function.");
306 [(name, (ret, [String "val"]), -1, [NotInFish; NotInDocs],
308 "internal test function - do not use",
310 This is an internal test function which is used to test whether
311 the automatically generated bindings can handle every possible
312 return type correctly.
314 It converts string C<val> to the return type.
316 You probably don't want to call this function.");
317 (name ^ "err", (ret, []), -1, [NotInFish; NotInDocs],
319 "internal test function - do not use",
321 This is an internal test function which is used to test whether
322 the automatically generated bindings can handle every possible
323 return type correctly.
325 This function always returns an error.
327 You probably don't want to call this function.")]
331 (* non_daemon_functions are any functions which don't get processed
332 * in the daemon, eg. functions for setting and getting local
333 * configuration values.
336 let non_daemon_functions = test_functions @ [
337 ("launch", (RErr, []), -1, [FishAlias "run"; FishAction "launch"],
339 "launch the qemu subprocess",
341 Internally libguestfs is implemented by running a virtual machine
344 You should call this after configuring the handle
345 (eg. adding drives) but before performing any actions.");
347 ("wait_ready", (RErr, []), -1, [NotInFish],
349 "wait until the qemu subprocess launches",
351 Internally libguestfs is implemented by running a virtual machine
354 You should call this after C<guestfs_launch> to wait for the launch
357 ("kill_subprocess", (RErr, []), -1, [],
359 "kill the qemu subprocess",
361 This kills the qemu subprocess. You should never need to call this.");
363 ("add_drive", (RErr, [String "filename"]), -1, [FishAlias "add"],
365 "add an image to examine or modify",
367 This function adds a virtual machine disk image C<filename> to the
368 guest. The first time you call this function, the disk appears as IDE
369 disk 0 (C</dev/sda>) in the guest, the second time as C</dev/sdb>, and
372 You don't necessarily need to be root when using libguestfs. However
373 you obviously do need sufficient permissions to access the filename
374 for whatever operations you want to perform (ie. read access if you
375 just want to read the image or write access if you want to modify the
378 This is equivalent to the qemu parameter
379 C<-drive file=filename,cache=off,if=virtio>.
381 Note that this call checks for the existence of C<filename>. This
382 stops you from specifying other types of drive which are supported
383 by qemu such as C<nbd:> and C<http:> URLs. To specify those, use
384 the general C<guestfs_config> call instead.");
386 ("add_cdrom", (RErr, [String "filename"]), -1, [FishAlias "cdrom"],
388 "add a CD-ROM disk image to examine",
390 This function adds a virtual CD-ROM disk image to the guest.
392 This is equivalent to the qemu parameter C<-cdrom filename>.
394 Note that this call checks for the existence of C<filename>. This
395 stops you from specifying other types of drive which are supported
396 by qemu such as C<nbd:> and C<http:> URLs. To specify those, use
397 the general C<guestfs_config> call instead.");
399 ("add_drive_ro", (RErr, [String "filename"]), -1, [FishAlias "add-ro"],
401 "add a drive in snapshot mode (read-only)",
403 This adds a drive in snapshot mode, making it effectively
406 Note that writes to the device are allowed, and will be seen for
407 the duration of the guestfs handle, but they are written
408 to a temporary file which is discarded as soon as the guestfs
409 handle is closed. We don't currently have any method to enable
410 changes to be committed, although qemu can support this.
412 This is equivalent to the qemu parameter
413 C<-drive file=filename,snapshot=on,if=virtio>.
415 Note that this call checks for the existence of C<filename>. This
416 stops you from specifying other types of drive which are supported
417 by qemu such as C<nbd:> and C<http:> URLs. To specify those, use
418 the general C<guestfs_config> call instead.");
420 ("config", (RErr, [String "qemuparam"; OptString "qemuvalue"]), -1, [],
422 "add qemu parameters",
424 This can be used to add arbitrary qemu command line parameters
425 of the form C<-param value>. Actually it's not quite arbitrary - we
426 prevent you from setting some parameters which would interfere with
427 parameters that we use.
429 The first character of C<param> string must be a C<-> (dash).
431 C<value> can be NULL.");
433 ("set_qemu", (RErr, [String "qemu"]), -1, [FishAlias "qemu"],
435 "set the qemu binary",
437 Set the qemu binary that we will use.
439 The default is chosen when the library was compiled by the
442 You can also override this by setting the C<LIBGUESTFS_QEMU>
443 environment variable.
445 Setting C<qemu> to C<NULL> restores the default qemu binary.");
447 ("get_qemu", (RConstString "qemu", []), -1, [],
449 "get the qemu binary",
451 Return the current qemu binary.
453 This is always non-NULL. If it wasn't set already, then this will
454 return the default qemu binary name.");
456 ("set_path", (RErr, [String "path"]), -1, [FishAlias "path"],
458 "set the search path",
460 Set the path that libguestfs searches for kernel and initrd.img.
462 The default is C<$libdir/guestfs> unless overridden by setting
463 C<LIBGUESTFS_PATH> environment variable.
465 Setting C<path> to C<NULL> restores the default path.");
467 ("get_path", (RConstString "path", []), -1, [],
469 "get the search path",
471 Return the current search path.
473 This is always non-NULL. If it wasn't set already, then this will
474 return the default path.");
476 ("set_append", (RErr, [String "append"]), -1, [FishAlias "append"],
478 "add options to kernel command line",
480 This function is used to add additional options to the
481 guest kernel command line.
483 The default is C<NULL> unless overridden by setting
484 C<LIBGUESTFS_APPEND> environment variable.
486 Setting C<append> to C<NULL> means I<no> additional options
487 are passed (libguestfs always adds a few of its own).");
489 ("get_append", (RConstString "append", []), -1, [],
491 "get the additional kernel options",
493 Return the additional kernel options which are added to the
494 guest kernel command line.
496 If C<NULL> then no options are added.");
498 ("set_autosync", (RErr, [Bool "autosync"]), -1, [FishAlias "autosync"],
502 If C<autosync> is true, this enables autosync. Libguestfs will make a
503 best effort attempt to run C<guestfs_umount_all> followed by
504 C<guestfs_sync> when the handle is closed
505 (also if the program exits without closing handles).
507 This is disabled by default (except in guestfish where it is
508 enabled by default).");
510 ("get_autosync", (RBool "autosync", []), -1, [],
514 Get the autosync flag.");
516 ("set_verbose", (RErr, [Bool "verbose"]), -1, [FishAlias "verbose"],
520 If C<verbose> is true, this turns on verbose messages (to C<stderr>).
522 Verbose messages are disabled unless the environment variable
523 C<LIBGUESTFS_DEBUG> is defined and set to C<1>.");
525 ("get_verbose", (RBool "verbose", []), -1, [],
529 This returns the verbose messages flag.");
531 ("is_ready", (RBool "ready", []), -1, [],
533 "is ready to accept commands",
535 This returns true iff this handle is ready to accept commands
536 (in the C<READY> state).
538 For more information on states, see L<guestfs(3)>.");
540 ("is_config", (RBool "config", []), -1, [],
542 "is in configuration state",
544 This returns true iff this handle is being configured
545 (in the C<CONFIG> state).
547 For more information on states, see L<guestfs(3)>.");
549 ("is_launching", (RBool "launching", []), -1, [],
551 "is launching subprocess",
553 This returns true iff this handle is launching the subprocess
554 (in the C<LAUNCHING> state).
556 For more information on states, see L<guestfs(3)>.");
558 ("is_busy", (RBool "busy", []), -1, [],
560 "is busy processing a command",
562 This returns true iff this handle is busy processing a command
563 (in the C<BUSY> state).
565 For more information on states, see L<guestfs(3)>.");
567 ("get_state", (RInt "state", []), -1, [],
569 "get the current state",
571 This returns the current state as an opaque integer. This is
572 only useful for printing debug and internal error messages.
574 For more information on states, see L<guestfs(3)>.");
576 ("set_busy", (RErr, []), -1, [NotInFish],
580 This sets the state to C<BUSY>. This is only used when implementing
581 actions using the low-level API.
583 For more information on states, see L<guestfs(3)>.");
585 ("set_ready", (RErr, []), -1, [NotInFish],
587 "set state to ready",
589 This sets the state to C<READY>. This is only used when implementing
590 actions using the low-level API.
592 For more information on states, see L<guestfs(3)>.");
594 ("end_busy", (RErr, []), -1, [NotInFish],
596 "leave the busy state",
598 This sets the state to C<READY>, or if in C<CONFIG> then it leaves the
599 state as is. This is only used when implementing
600 actions using the low-level API.
602 For more information on states, see L<guestfs(3)>.");
604 ("set_memsize", (RErr, [Int "memsize"]), -1, [FishAlias "memsize"],
606 "set memory allocated to the qemu subprocess",
608 This sets the memory size in megabytes allocated to the
609 qemu subprocess. This only has any effect if called before
612 You can also change this by setting the environment
613 variable C<LIBGUESTFS_MEMSIZE> before the handle is
616 For more information on the architecture of libguestfs,
617 see L<guestfs(3)>.");
619 ("get_memsize", (RInt "memsize", []), -1, [],
621 "get memory allocated to the qemu subprocess",
623 This gets the memory size in megabytes allocated to the
626 If C<guestfs_set_memsize> was not called
627 on this handle, and if C<LIBGUESTFS_MEMSIZE> was not set,
628 then this returns the compiled-in default value for memsize.
630 For more information on the architecture of libguestfs,
631 see L<guestfs(3)>.");
635 (* daemon_functions are any functions which cause some action
636 * to take place in the daemon.
639 let daemon_functions = [
640 ("mount", (RErr, [String "device"; String "mountpoint"]), 1, [],
641 [InitEmpty, Always, TestOutput (
642 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
643 ["mkfs"; "ext2"; "/dev/sda1"];
644 ["mount"; "/dev/sda1"; "/"];
645 ["write_file"; "/new"; "new file contents"; "0"];
646 ["cat"; "/new"]], "new file contents")],
647 "mount a guest disk at a position in the filesystem",
649 Mount a guest disk at a position in the filesystem. Block devices
650 are named C</dev/sda>, C</dev/sdb> and so on, as they were added to
651 the guest. If those block devices contain partitions, they will have
652 the usual names (eg. C</dev/sda1>). Also LVM C</dev/VG/LV>-style
655 The rules are the same as for L<mount(2)>: A filesystem must
656 first be mounted on C</> before others can be mounted. Other
657 filesystems can only be mounted on directories which already
660 The mounted filesystem is writable, if we have sufficient permissions
661 on the underlying device.
663 The filesystem options C<sync> and C<noatime> are set with this
664 call, in order to improve reliability.");
666 ("sync", (RErr, []), 2, [],
667 [ InitEmpty, Always, TestRun [["sync"]]],
668 "sync disks, writes are flushed through to the disk image",
670 This syncs the disk, so that any writes are flushed through to the
671 underlying disk image.
673 You should always call this if you have modified a disk image, before
674 closing the handle.");
676 ("touch", (RErr, [String "path"]), 3, [],
677 [InitBasicFS, Always, TestOutputTrue (
679 ["exists"; "/new"]])],
680 "update file timestamps or create a new file",
682 Touch acts like the L<touch(1)> command. It can be used to
683 update the timestamps on a file, or, if the file does not exist,
684 to create a new zero-length file.");
686 ("cat", (RString "content", [String "path"]), 4, [ProtocolLimitWarning],
687 [InitBasicFS, Always, TestOutput (
688 [["write_file"; "/new"; "new file contents"; "0"];
689 ["cat"; "/new"]], "new file contents")],
690 "list the contents of a file",
692 Return the contents of the file named C<path>.
694 Note that this function cannot correctly handle binary files
695 (specifically, files containing C<\\0> character which is treated
696 as end of string). For those you need to use the C<guestfs_download>
697 function which has a more complex interface.");
699 ("ll", (RString "listing", [String "directory"]), 5, [],
700 [], (* XXX Tricky to test because it depends on the exact format
701 * of the 'ls -l' command, which changes between F10 and F11.
703 "list the files in a directory (long format)",
705 List the files in C<directory> (relative to the root directory,
706 there is no cwd) in the format of 'ls -la'.
708 This command is mostly useful for interactive sessions. It
709 is I<not> intended that you try to parse the output string.");
711 ("ls", (RStringList "listing", [String "directory"]), 6, [],
712 [InitBasicFS, Always, TestOutputList (
715 ["touch"; "/newest"];
716 ["ls"; "/"]], ["lost+found"; "new"; "newer"; "newest"])],
717 "list the files in a directory",
719 List the files in C<directory> (relative to the root directory,
720 there is no cwd). The '.' and '..' entries are not returned, but
721 hidden files are shown.
723 This command is mostly useful for interactive sessions. Programs
724 should probably use C<guestfs_readdir> instead.");
726 ("list_devices", (RStringList "devices", []), 7, [],
727 [InitEmpty, Always, TestOutputListOfDevices (
728 [["list_devices"]], ["/dev/sda"; "/dev/sdb"; "/dev/sdc"; "/dev/sdd"])],
729 "list the block devices",
731 List all the block devices.
733 The full block device names are returned, eg. C</dev/sda>");
735 ("list_partitions", (RStringList "partitions", []), 8, [],
736 [InitBasicFS, Always, TestOutputListOfDevices (
737 [["list_partitions"]], ["/dev/sda1"]);
738 InitEmpty, Always, TestOutputListOfDevices (
739 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",200 ,400 ,"];
740 ["list_partitions"]], ["/dev/sda1"; "/dev/sda2"; "/dev/sda3"])],
741 "list the partitions",
743 List all the partitions detected on all block devices.
745 The full partition device names are returned, eg. C</dev/sda1>
747 This does not return logical volumes. For that you will need to
748 call C<guestfs_lvs>.");
750 ("pvs", (RStringList "physvols", []), 9, [],
751 [InitBasicFSonLVM, Always, TestOutputListOfDevices (
752 [["pvs"]], ["/dev/sda1"]);
753 InitEmpty, Always, TestOutputListOfDevices (
754 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",200 ,400 ,"];
755 ["pvcreate"; "/dev/sda1"];
756 ["pvcreate"; "/dev/sda2"];
757 ["pvcreate"; "/dev/sda3"];
758 ["pvs"]], ["/dev/sda1"; "/dev/sda2"; "/dev/sda3"])],
759 "list the LVM physical volumes (PVs)",
761 List all the physical volumes detected. This is the equivalent
762 of the L<pvs(8)> command.
764 This returns a list of just the device names that contain
765 PVs (eg. C</dev/sda2>).
767 See also C<guestfs_pvs_full>.");
769 ("vgs", (RStringList "volgroups", []), 10, [],
770 [InitBasicFSonLVM, Always, TestOutputList (
772 InitEmpty, Always, TestOutputList (
773 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",200 ,400 ,"];
774 ["pvcreate"; "/dev/sda1"];
775 ["pvcreate"; "/dev/sda2"];
776 ["pvcreate"; "/dev/sda3"];
777 ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"];
778 ["vgcreate"; "VG2"; "/dev/sda3"];
779 ["vgs"]], ["VG1"; "VG2"])],
780 "list the LVM volume groups (VGs)",
782 List all the volumes groups detected. This is the equivalent
783 of the L<vgs(8)> command.
785 This returns a list of just the volume group names that were
786 detected (eg. C<VolGroup00>).
788 See also C<guestfs_vgs_full>.");
790 ("lvs", (RStringList "logvols", []), 11, [],
791 [InitBasicFSonLVM, Always, TestOutputList (
792 [["lvs"]], ["/dev/VG/LV"]);
793 InitEmpty, Always, TestOutputList (
794 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",200 ,400 ,"];
795 ["pvcreate"; "/dev/sda1"];
796 ["pvcreate"; "/dev/sda2"];
797 ["pvcreate"; "/dev/sda3"];
798 ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"];
799 ["vgcreate"; "VG2"; "/dev/sda3"];
800 ["lvcreate"; "LV1"; "VG1"; "50"];
801 ["lvcreate"; "LV2"; "VG1"; "50"];
802 ["lvcreate"; "LV3"; "VG2"; "50"];
803 ["lvs"]], ["/dev/VG1/LV1"; "/dev/VG1/LV2"; "/dev/VG2/LV3"])],
804 "list the LVM logical volumes (LVs)",
806 List all the logical volumes detected. This is the equivalent
807 of the L<lvs(8)> command.
809 This returns a list of the logical volume device names
810 (eg. C</dev/VolGroup00/LogVol00>).
812 See also C<guestfs_lvs_full>.");
814 ("pvs_full", (RPVList "physvols", []), 12, [],
815 [], (* XXX how to test? *)
816 "list the LVM physical volumes (PVs)",
818 List all the physical volumes detected. This is the equivalent
819 of the L<pvs(8)> command. The \"full\" version includes all fields.");
821 ("vgs_full", (RVGList "volgroups", []), 13, [],
822 [], (* XXX how to test? *)
823 "list the LVM volume groups (VGs)",
825 List all the volumes groups detected. This is the equivalent
826 of the L<vgs(8)> command. The \"full\" version includes all fields.");
828 ("lvs_full", (RLVList "logvols", []), 14, [],
829 [], (* XXX how to test? *)
830 "list the LVM logical volumes (LVs)",
832 List all the logical volumes detected. This is the equivalent
833 of the L<lvs(8)> command. The \"full\" version includes all fields.");
835 ("read_lines", (RStringList "lines", [String "path"]), 15, [],
836 [InitBasicFS, Always, TestOutputList (
837 [["write_file"; "/new"; "line1\r\nline2\nline3"; "0"];
838 ["read_lines"; "/new"]], ["line1"; "line2"; "line3"]);
839 InitBasicFS, Always, TestOutputList (
840 [["write_file"; "/new"; ""; "0"];
841 ["read_lines"; "/new"]], [])],
842 "read file as lines",
844 Return the contents of the file named C<path>.
846 The file contents are returned as a list of lines. Trailing
847 C<LF> and C<CRLF> character sequences are I<not> returned.
849 Note that this function cannot correctly handle binary files
850 (specifically, files containing C<\\0> character which is treated
851 as end of line). For those you need to use the C<guestfs_read_file>
852 function which has a more complex interface.");
854 ("aug_init", (RErr, [String "root"; Int "flags"]), 16, [],
855 [], (* XXX Augeas code needs tests. *)
856 "create a new Augeas handle",
858 Create a new Augeas handle for editing configuration files.
859 If there was any previous Augeas handle associated with this
860 guestfs session, then it is closed.
862 You must call this before using any other C<guestfs_aug_*>
865 C<root> is the filesystem root. C<root> must not be NULL,
868 The flags are the same as the flags defined in
869 E<lt>augeas.hE<gt>, the logical I<or> of the following
874 =item C<AUG_SAVE_BACKUP> = 1
876 Keep the original file with a C<.augsave> extension.
878 =item C<AUG_SAVE_NEWFILE> = 2
880 Save changes into a file with extension C<.augnew>, and
881 do not overwrite original. Overrides C<AUG_SAVE_BACKUP>.
883 =item C<AUG_TYPE_CHECK> = 4
885 Typecheck lenses (can be expensive).
887 =item C<AUG_NO_STDINC> = 8
889 Do not use standard load path for modules.
891 =item C<AUG_SAVE_NOOP> = 16
893 Make save a no-op, just record what would have been changed.
895 =item C<AUG_NO_LOAD> = 32
897 Do not load the tree in C<guestfs_aug_init>.
901 To close the handle, you can call C<guestfs_aug_close>.
903 To find out more about Augeas, see L<http://augeas.net/>.");
905 ("aug_close", (RErr, []), 26, [],
906 [], (* XXX Augeas code needs tests. *)
907 "close the current Augeas handle",
909 Close the current Augeas handle and free up any resources
910 used by it. After calling this, you have to call
911 C<guestfs_aug_init> again before you can use any other
914 ("aug_defvar", (RInt "nrnodes", [String "name"; OptString "expr"]), 17, [],
915 [], (* XXX Augeas code needs tests. *)
916 "define an Augeas variable",
918 Defines an Augeas variable C<name> whose value is the result
919 of evaluating C<expr>. If C<expr> is NULL, then C<name> is
922 On success this returns the number of nodes in C<expr>, or
923 C<0> if C<expr> evaluates to something which is not a nodeset.");
925 ("aug_defnode", (RIntBool ("nrnodes", "created"), [String "name"; String "expr"; String "val"]), 18, [],
926 [], (* XXX Augeas code needs tests. *)
927 "define an Augeas node",
929 Defines a variable C<name> whose value is the result of
932 If C<expr> evaluates to an empty nodeset, a node is created,
933 equivalent to calling C<guestfs_aug_set> C<expr>, C<value>.
934 C<name> will be the nodeset containing that single node.
936 On success this returns a pair containing the
937 number of nodes in the nodeset, and a boolean flag
938 if a node was created.");
940 ("aug_get", (RString "val", [String "path"]), 19, [],
941 [], (* XXX Augeas code needs tests. *)
942 "look up the value of an Augeas path",
944 Look up the value associated with C<path>. If C<path>
945 matches exactly one node, the C<value> is returned.");
947 ("aug_set", (RErr, [String "path"; String "val"]), 20, [],
948 [], (* XXX Augeas code needs tests. *)
949 "set Augeas path to value",
951 Set the value associated with C<path> to C<value>.");
953 ("aug_insert", (RErr, [String "path"; String "label"; Bool "before"]), 21, [],
954 [], (* XXX Augeas code needs tests. *)
955 "insert a sibling Augeas node",
957 Create a new sibling C<label> for C<path>, inserting it into
958 the tree before or after C<path> (depending on the boolean
961 C<path> must match exactly one existing node in the tree, and
962 C<label> must be a label, ie. not contain C</>, C<*> or end
963 with a bracketed index C<[N]>.");
965 ("aug_rm", (RInt "nrnodes", [String "path"]), 22, [],
966 [], (* XXX Augeas code needs tests. *)
967 "remove an Augeas path",
969 Remove C<path> and all of its children.
971 On success this returns the number of entries which were removed.");
973 ("aug_mv", (RErr, [String "src"; String "dest"]), 23, [],
974 [], (* XXX Augeas code needs tests. *)
977 Move the node C<src> to C<dest>. C<src> must match exactly
978 one node. C<dest> is overwritten if it exists.");
980 ("aug_match", (RStringList "matches", [String "path"]), 24, [],
981 [], (* XXX Augeas code needs tests. *)
982 "return Augeas nodes which match path",
984 Returns a list of paths which match the path expression C<path>.
985 The returned paths are sufficiently qualified so that they match
986 exactly one node in the current tree.");
988 ("aug_save", (RErr, []), 25, [],
989 [], (* XXX Augeas code needs tests. *)
990 "write all pending Augeas changes to disk",
992 This writes all pending changes to disk.
994 The flags which were passed to C<guestfs_aug_init> affect exactly
995 how files are saved.");
997 ("aug_load", (RErr, []), 27, [],
998 [], (* XXX Augeas code needs tests. *)
999 "load files into the tree",
1001 Load files into the tree.
1003 See C<aug_load> in the Augeas documentation for the full gory
1006 ("aug_ls", (RStringList "matches", [String "path"]), 28, [],
1007 [], (* XXX Augeas code needs tests. *)
1008 "list Augeas nodes under a path",
1010 This is just a shortcut for listing C<guestfs_aug_match>
1011 C<path/*> and sorting the resulting nodes into alphabetical order.");
1013 ("rm", (RErr, [String "path"]), 29, [],
1014 [InitBasicFS, Always, TestRun
1017 InitBasicFS, Always, TestLastFail
1019 InitBasicFS, Always, TestLastFail
1024 Remove the single file C<path>.");
1026 ("rmdir", (RErr, [String "path"]), 30, [],
1027 [InitBasicFS, Always, TestRun
1030 InitBasicFS, Always, TestLastFail
1031 [["rmdir"; "/new"]];
1032 InitBasicFS, Always, TestLastFail
1034 ["rmdir"; "/new"]]],
1035 "remove a directory",
1037 Remove the single directory C<path>.");
1039 ("rm_rf", (RErr, [String "path"]), 31, [],
1040 [InitBasicFS, Always, TestOutputFalse
1042 ["mkdir"; "/new/foo"];
1043 ["touch"; "/new/foo/bar"];
1045 ["exists"; "/new"]]],
1046 "remove a file or directory recursively",
1048 Remove the file or directory C<path>, recursively removing the
1049 contents if its a directory. This is like the C<rm -rf> shell
1052 ("mkdir", (RErr, [String "path"]), 32, [],
1053 [InitBasicFS, Always, TestOutputTrue
1055 ["is_dir"; "/new"]];
1056 InitBasicFS, Always, TestLastFail
1057 [["mkdir"; "/new/foo/bar"]]],
1058 "create a directory",
1060 Create a directory named C<path>.");
1062 ("mkdir_p", (RErr, [String "path"]), 33, [],
1063 [InitBasicFS, Always, TestOutputTrue
1064 [["mkdir_p"; "/new/foo/bar"];
1065 ["is_dir"; "/new/foo/bar"]];
1066 InitBasicFS, Always, TestOutputTrue
1067 [["mkdir_p"; "/new/foo/bar"];
1068 ["is_dir"; "/new/foo"]];
1069 InitBasicFS, Always, TestOutputTrue
1070 [["mkdir_p"; "/new/foo/bar"];
1071 ["is_dir"; "/new"]];
1072 (* Regression tests for RHBZ#503133: *)
1073 InitBasicFS, Always, TestRun
1075 ["mkdir_p"; "/new"]];
1076 InitBasicFS, Always, TestLastFail
1078 ["mkdir_p"; "/new"]]],
1079 "create a directory and parents",
1081 Create a directory named C<path>, creating any parent directories
1082 as necessary. This is like the C<mkdir -p> shell command.");
1084 ("chmod", (RErr, [Int "mode"; String "path"]), 34, [],
1085 [], (* XXX Need stat command to test *)
1088 Change the mode (permissions) of C<path> to C<mode>. Only
1089 numeric modes are supported.");
1091 ("chown", (RErr, [Int "owner"; Int "group"; String "path"]), 35, [],
1092 [], (* XXX Need stat command to test *)
1093 "change file owner and group",
1095 Change the file owner to C<owner> and group to C<group>.
1097 Only numeric uid and gid are supported. If you want to use
1098 names, you will need to locate and parse the password file
1099 yourself (Augeas support makes this relatively easy).");
1101 ("exists", (RBool "existsflag", [String "path"]), 36, [],
1102 [InitBasicFS, Always, TestOutputTrue (
1104 ["exists"; "/new"]]);
1105 InitBasicFS, Always, TestOutputTrue (
1107 ["exists"; "/new"]])],
1108 "test if file or directory exists",
1110 This returns C<true> if and only if there is a file, directory
1111 (or anything) with the given C<path> name.
1113 See also C<guestfs_is_file>, C<guestfs_is_dir>, C<guestfs_stat>.");
1115 ("is_file", (RBool "fileflag", [String "path"]), 37, [],
1116 [InitBasicFS, Always, TestOutputTrue (
1118 ["is_file"; "/new"]]);
1119 InitBasicFS, Always, TestOutputFalse (
1121 ["is_file"; "/new"]])],
1122 "test if file exists",
1124 This returns C<true> if and only if there is a file
1125 with the given C<path> name. Note that it returns false for
1126 other objects like directories.
1128 See also C<guestfs_stat>.");
1130 ("is_dir", (RBool "dirflag", [String "path"]), 38, [],
1131 [InitBasicFS, Always, TestOutputFalse (
1133 ["is_dir"; "/new"]]);
1134 InitBasicFS, Always, TestOutputTrue (
1136 ["is_dir"; "/new"]])],
1137 "test if file exists",
1139 This returns C<true> if and only if there is a directory
1140 with the given C<path> name. Note that it returns false for
1141 other objects like files.
1143 See also C<guestfs_stat>.");
1145 ("pvcreate", (RErr, [String "device"]), 39, [],
1146 [InitEmpty, Always, TestOutputListOfDevices (
1147 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",200 ,400 ,"];
1148 ["pvcreate"; "/dev/sda1"];
1149 ["pvcreate"; "/dev/sda2"];
1150 ["pvcreate"; "/dev/sda3"];
1151 ["pvs"]], ["/dev/sda1"; "/dev/sda2"; "/dev/sda3"])],
1152 "create an LVM physical volume",
1154 This creates an LVM physical volume on the named C<device>,
1155 where C<device> should usually be a partition name such
1158 ("vgcreate", (RErr, [String "volgroup"; StringList "physvols"]), 40, [],
1159 [InitEmpty, Always, TestOutputList (
1160 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",200 ,400 ,"];
1161 ["pvcreate"; "/dev/sda1"];
1162 ["pvcreate"; "/dev/sda2"];
1163 ["pvcreate"; "/dev/sda3"];
1164 ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"];
1165 ["vgcreate"; "VG2"; "/dev/sda3"];
1166 ["vgs"]], ["VG1"; "VG2"])],
1167 "create an LVM volume group",
1169 This creates an LVM volume group called C<volgroup>
1170 from the non-empty list of physical volumes C<physvols>.");
1172 ("lvcreate", (RErr, [String "logvol"; String "volgroup"; Int "mbytes"]), 41, [],
1173 [InitEmpty, Always, TestOutputList (
1174 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",200 ,400 ,"];
1175 ["pvcreate"; "/dev/sda1"];
1176 ["pvcreate"; "/dev/sda2"];
1177 ["pvcreate"; "/dev/sda3"];
1178 ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"];
1179 ["vgcreate"; "VG2"; "/dev/sda3"];
1180 ["lvcreate"; "LV1"; "VG1"; "50"];
1181 ["lvcreate"; "LV2"; "VG1"; "50"];
1182 ["lvcreate"; "LV3"; "VG2"; "50"];
1183 ["lvcreate"; "LV4"; "VG2"; "50"];
1184 ["lvcreate"; "LV5"; "VG2"; "50"];
1186 ["/dev/VG1/LV1"; "/dev/VG1/LV2";
1187 "/dev/VG2/LV3"; "/dev/VG2/LV4"; "/dev/VG2/LV5"])],
1188 "create an LVM volume group",
1190 This creates an LVM volume group called C<logvol>
1191 on the volume group C<volgroup>, with C<size> megabytes.");
1193 ("mkfs", (RErr, [String "fstype"; String "device"]), 42, [],
1194 [InitEmpty, Always, TestOutput (
1195 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1196 ["mkfs"; "ext2"; "/dev/sda1"];
1197 ["mount"; "/dev/sda1"; "/"];
1198 ["write_file"; "/new"; "new file contents"; "0"];
1199 ["cat"; "/new"]], "new file contents")],
1200 "make a filesystem",
1202 This creates a filesystem on C<device> (usually a partition
1203 or LVM logical volume). The filesystem type is C<fstype>, for
1206 ("sfdisk", (RErr, [String "device";
1207 Int "cyls"; Int "heads"; Int "sectors";
1208 StringList "lines"]), 43, [DangerWillRobinson],
1210 "create partitions on a block device",
1212 This is a direct interface to the L<sfdisk(8)> program for creating
1213 partitions on block devices.
1215 C<device> should be a block device, for example C</dev/sda>.
1217 C<cyls>, C<heads> and C<sectors> are the number of cylinders, heads
1218 and sectors on the device, which are passed directly to sfdisk as
1219 the I<-C>, I<-H> and I<-S> parameters. If you pass C<0> for any
1220 of these, then the corresponding parameter is omitted. Usually for
1221 'large' disks, you can just pass C<0> for these, but for small
1222 (floppy-sized) disks, sfdisk (or rather, the kernel) cannot work
1223 out the right geometry and you will need to tell it.
1225 C<lines> is a list of lines that we feed to C<sfdisk>. For more
1226 information refer to the L<sfdisk(8)> manpage.
1228 To create a single partition occupying the whole disk, you would
1229 pass C<lines> as a single element list, when the single element being
1230 the string C<,> (comma).
1232 See also: C<guestfs_sfdisk_l>, C<guestfs_sfdisk_N>");
1234 ("write_file", (RErr, [String "path"; String "content"; Int "size"]), 44, [ProtocolLimitWarning],
1235 [InitBasicFS, Always, TestOutput (
1236 [["write_file"; "/new"; "new file contents"; "0"];
1237 ["cat"; "/new"]], "new file contents");
1238 InitBasicFS, Always, TestOutput (
1239 [["write_file"; "/new"; "\nnew file contents\n"; "0"];
1240 ["cat"; "/new"]], "\nnew file contents\n");
1241 InitBasicFS, Always, TestOutput (
1242 [["write_file"; "/new"; "\n\n"; "0"];
1243 ["cat"; "/new"]], "\n\n");
1244 InitBasicFS, Always, TestOutput (
1245 [["write_file"; "/new"; ""; "0"];
1246 ["cat"; "/new"]], "");
1247 InitBasicFS, Always, TestOutput (
1248 [["write_file"; "/new"; "\n\n\n"; "0"];
1249 ["cat"; "/new"]], "\n\n\n");
1250 InitBasicFS, Always, TestOutput (
1251 [["write_file"; "/new"; "\n"; "0"];
1252 ["cat"; "/new"]], "\n")],
1255 This call creates a file called C<path>. The contents of the
1256 file is the string C<content> (which can contain any 8 bit data),
1257 with length C<size>.
1259 As a special case, if C<size> is C<0>
1260 then the length is calculated using C<strlen> (so in this case
1261 the content cannot contain embedded ASCII NULs).
1263 I<NB.> Owing to a bug, writing content containing ASCII NUL
1264 characters does I<not> work, even if the length is specified.
1265 We hope to resolve this bug in a future version. In the meantime
1266 use C<guestfs_upload>.");
1268 ("umount", (RErr, [String "pathordevice"]), 45, [FishAlias "unmount"],
1269 [InitEmpty, Always, TestOutputListOfDevices (
1270 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1271 ["mkfs"; "ext2"; "/dev/sda1"];
1272 ["mount"; "/dev/sda1"; "/"];
1273 ["mounts"]], ["/dev/sda1"]);
1274 InitEmpty, Always, TestOutputList (
1275 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1276 ["mkfs"; "ext2"; "/dev/sda1"];
1277 ["mount"; "/dev/sda1"; "/"];
1280 "unmount a filesystem",
1282 This unmounts the given filesystem. The filesystem may be
1283 specified either by its mountpoint (path) or the device which
1284 contains the filesystem.");
1286 ("mounts", (RStringList "devices", []), 46, [],
1287 [InitBasicFS, Always, TestOutputListOfDevices (
1288 [["mounts"]], ["/dev/sda1"])],
1289 "show mounted filesystems",
1291 This returns the list of currently mounted filesystems. It returns
1292 the list of devices (eg. C</dev/sda1>, C</dev/VG/LV>).
1294 Some internal mounts are not shown.");
1296 ("umount_all", (RErr, []), 47, [FishAlias "unmount-all"],
1297 [InitBasicFS, Always, TestOutputList (
1300 (* check that umount_all can unmount nested mounts correctly: *)
1301 InitEmpty, Always, TestOutputList (
1302 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",200 ,400 ,"];
1303 ["mkfs"; "ext2"; "/dev/sda1"];
1304 ["mkfs"; "ext2"; "/dev/sda2"];
1305 ["mkfs"; "ext2"; "/dev/sda3"];
1306 ["mount"; "/dev/sda1"; "/"];
1308 ["mount"; "/dev/sda2"; "/mp1"];
1309 ["mkdir"; "/mp1/mp2"];
1310 ["mount"; "/dev/sda3"; "/mp1/mp2"];
1311 ["mkdir"; "/mp1/mp2/mp3"];
1314 "unmount all filesystems",
1316 This unmounts all mounted filesystems.
1318 Some internal mounts are not unmounted by this call.");
1320 ("lvm_remove_all", (RErr, []), 48, [DangerWillRobinson],
1322 "remove all LVM LVs, VGs and PVs",
1324 This command removes all LVM logical volumes, volume groups
1325 and physical volumes.");
1327 ("file", (RString "description", [String "path"]), 49, [],
1328 [InitBasicFS, Always, TestOutput (
1330 ["file"; "/new"]], "empty");
1331 InitBasicFS, Always, TestOutput (
1332 [["write_file"; "/new"; "some content\n"; "0"];
1333 ["file"; "/new"]], "ASCII text");
1334 InitBasicFS, Always, TestLastFail (
1335 [["file"; "/nofile"]])],
1336 "determine file type",
1338 This call uses the standard L<file(1)> command to determine
1339 the type or contents of the file. This also works on devices,
1340 for example to find out whether a partition contains a filesystem.
1342 The exact command which runs is C<file -bsL path>. Note in
1343 particular that the filename is not prepended to the output
1344 (the C<-b> option).");
1346 ("command", (RString "output", [StringList "arguments"]), 50, [ProtocolLimitWarning],
1347 [InitBasicFS, Always, TestOutput (
1348 [["upload"; "test-command"; "/test-command"];
1349 ["chmod"; "0o755"; "/test-command"];
1350 ["command"; "/test-command 1"]], "Result1");
1351 InitBasicFS, Always, TestOutput (
1352 [["upload"; "test-command"; "/test-command"];
1353 ["chmod"; "0o755"; "/test-command"];
1354 ["command"; "/test-command 2"]], "Result2\n");
1355 InitBasicFS, Always, TestOutput (
1356 [["upload"; "test-command"; "/test-command"];
1357 ["chmod"; "0o755"; "/test-command"];
1358 ["command"; "/test-command 3"]], "\nResult3");
1359 InitBasicFS, Always, TestOutput (
1360 [["upload"; "test-command"; "/test-command"];
1361 ["chmod"; "0o755"; "/test-command"];
1362 ["command"; "/test-command 4"]], "\nResult4\n");
1363 InitBasicFS, Always, TestOutput (
1364 [["upload"; "test-command"; "/test-command"];
1365 ["chmod"; "0o755"; "/test-command"];
1366 ["command"; "/test-command 5"]], "\nResult5\n\n");
1367 InitBasicFS, Always, TestOutput (
1368 [["upload"; "test-command"; "/test-command"];
1369 ["chmod"; "0o755"; "/test-command"];
1370 ["command"; "/test-command 6"]], "\n\nResult6\n\n");
1371 InitBasicFS, Always, TestOutput (
1372 [["upload"; "test-command"; "/test-command"];
1373 ["chmod"; "0o755"; "/test-command"];
1374 ["command"; "/test-command 7"]], "");
1375 InitBasicFS, Always, TestOutput (
1376 [["upload"; "test-command"; "/test-command"];
1377 ["chmod"; "0o755"; "/test-command"];
1378 ["command"; "/test-command 8"]], "\n");
1379 InitBasicFS, Always, TestOutput (
1380 [["upload"; "test-command"; "/test-command"];
1381 ["chmod"; "0o755"; "/test-command"];
1382 ["command"; "/test-command 9"]], "\n\n");
1383 InitBasicFS, Always, TestOutput (
1384 [["upload"; "test-command"; "/test-command"];
1385 ["chmod"; "0o755"; "/test-command"];
1386 ["command"; "/test-command 10"]], "Result10-1\nResult10-2\n");
1387 InitBasicFS, Always, TestOutput (
1388 [["upload"; "test-command"; "/test-command"];
1389 ["chmod"; "0o755"; "/test-command"];
1390 ["command"; "/test-command 11"]], "Result11-1\nResult11-2");
1391 InitBasicFS, Always, TestLastFail (
1392 [["upload"; "test-command"; "/test-command"];
1393 ["chmod"; "0o755"; "/test-command"];
1394 ["command"; "/test-command"]])],
1395 "run a command from the guest filesystem",
1397 This call runs a command from the guest filesystem. The
1398 filesystem must be mounted, and must contain a compatible
1399 operating system (ie. something Linux, with the same
1400 or compatible processor architecture).
1402 The single parameter is an argv-style list of arguments.
1403 The first element is the name of the program to run.
1404 Subsequent elements are parameters. The list must be
1405 non-empty (ie. must contain a program name). Note that
1406 the command runs directly, and is I<not> invoked via
1407 the shell (see C<guestfs_sh>).
1409 The return value is anything printed to I<stdout> by
1412 If the command returns a non-zero exit status, then
1413 this function returns an error message. The error message
1414 string is the content of I<stderr> from the command.
1416 The C<$PATH> environment variable will contain at least
1417 C</usr/bin> and C</bin>. If you require a program from
1418 another location, you should provide the full path in the
1421 Shared libraries and data files required by the program
1422 must be available on filesystems which are mounted in the
1423 correct places. It is the caller's responsibility to ensure
1424 all filesystems that are needed are mounted at the right
1427 ("command_lines", (RStringList "lines", [StringList "arguments"]), 51, [ProtocolLimitWarning],
1428 [InitBasicFS, Always, TestOutputList (
1429 [["upload"; "test-command"; "/test-command"];
1430 ["chmod"; "0o755"; "/test-command"];
1431 ["command_lines"; "/test-command 1"]], ["Result1"]);
1432 InitBasicFS, Always, TestOutputList (
1433 [["upload"; "test-command"; "/test-command"];
1434 ["chmod"; "0o755"; "/test-command"];
1435 ["command_lines"; "/test-command 2"]], ["Result2"]);
1436 InitBasicFS, Always, TestOutputList (
1437 [["upload"; "test-command"; "/test-command"];
1438 ["chmod"; "0o755"; "/test-command"];
1439 ["command_lines"; "/test-command 3"]], ["";"Result3"]);
1440 InitBasicFS, Always, TestOutputList (
1441 [["upload"; "test-command"; "/test-command"];
1442 ["chmod"; "0o755"; "/test-command"];
1443 ["command_lines"; "/test-command 4"]], ["";"Result4"]);
1444 InitBasicFS, Always, TestOutputList (
1445 [["upload"; "test-command"; "/test-command"];
1446 ["chmod"; "0o755"; "/test-command"];
1447 ["command_lines"; "/test-command 5"]], ["";"Result5";""]);
1448 InitBasicFS, Always, TestOutputList (
1449 [["upload"; "test-command"; "/test-command"];
1450 ["chmod"; "0o755"; "/test-command"];
1451 ["command_lines"; "/test-command 6"]], ["";"";"Result6";""]);
1452 InitBasicFS, Always, TestOutputList (
1453 [["upload"; "test-command"; "/test-command"];
1454 ["chmod"; "0o755"; "/test-command"];
1455 ["command_lines"; "/test-command 7"]], []);
1456 InitBasicFS, Always, TestOutputList (
1457 [["upload"; "test-command"; "/test-command"];
1458 ["chmod"; "0o755"; "/test-command"];
1459 ["command_lines"; "/test-command 8"]], [""]);
1460 InitBasicFS, Always, TestOutputList (
1461 [["upload"; "test-command"; "/test-command"];
1462 ["chmod"; "0o755"; "/test-command"];
1463 ["command_lines"; "/test-command 9"]], ["";""]);
1464 InitBasicFS, Always, TestOutputList (
1465 [["upload"; "test-command"; "/test-command"];
1466 ["chmod"; "0o755"; "/test-command"];
1467 ["command_lines"; "/test-command 10"]], ["Result10-1";"Result10-2"]);
1468 InitBasicFS, Always, TestOutputList (
1469 [["upload"; "test-command"; "/test-command"];
1470 ["chmod"; "0o755"; "/test-command"];
1471 ["command_lines"; "/test-command 11"]], ["Result11-1";"Result11-2"])],
1472 "run a command, returning lines",
1474 This is the same as C<guestfs_command>, but splits the
1475 result into a list of lines.
1477 See also: C<guestfs_sh_lines>");
1479 ("stat", (RStat "statbuf", [String "path"]), 52, [],
1480 [InitBasicFS, Always, TestOutputStruct (
1482 ["stat"; "/new"]], [CompareWithInt ("size", 0)])],
1483 "get file information",
1485 Returns file information for the given C<path>.
1487 This is the same as the C<stat(2)> system call.");
1489 ("lstat", (RStat "statbuf", [String "path"]), 53, [],
1490 [InitBasicFS, Always, TestOutputStruct (
1492 ["lstat"; "/new"]], [CompareWithInt ("size", 0)])],
1493 "get file information for a symbolic link",
1495 Returns file information for the given C<path>.
1497 This is the same as C<guestfs_stat> except that if C<path>
1498 is a symbolic link, then the link is stat-ed, not the file it
1501 This is the same as the C<lstat(2)> system call.");
1503 ("statvfs", (RStatVFS "statbuf", [String "path"]), 54, [],
1504 [InitBasicFS, Always, TestOutputStruct (
1505 [["statvfs"; "/"]], [CompareWithInt ("namemax", 255);
1506 CompareWithInt ("bsize", 1024)])],
1507 "get file system statistics",
1509 Returns file system statistics for any mounted file system.
1510 C<path> should be a file or directory in the mounted file system
1511 (typically it is the mount point itself, but it doesn't need to be).
1513 This is the same as the C<statvfs(2)> system call.");
1515 ("tune2fs_l", (RHashtable "superblock", [String "device"]), 55, [],
1517 "get ext2/ext3/ext4 superblock details",
1519 This returns the contents of the ext2, ext3 or ext4 filesystem
1520 superblock on C<device>.
1522 It is the same as running C<tune2fs -l device>. See L<tune2fs(8)>
1523 manpage for more details. The list of fields returned isn't
1524 clearly defined, and depends on both the version of C<tune2fs>
1525 that libguestfs was built against, and the filesystem itself.");
1527 ("blockdev_setro", (RErr, [String "device"]), 56, [],
1528 [InitEmpty, Always, TestOutputTrue (
1529 [["blockdev_setro"; "/dev/sda"];
1530 ["blockdev_getro"; "/dev/sda"]])],
1531 "set block device to read-only",
1533 Sets the block device named C<device> to read-only.
1535 This uses the L<blockdev(8)> command.");
1537 ("blockdev_setrw", (RErr, [String "device"]), 57, [],
1538 [InitEmpty, Always, TestOutputFalse (
1539 [["blockdev_setrw"; "/dev/sda"];
1540 ["blockdev_getro"; "/dev/sda"]])],
1541 "set block device to read-write",
1543 Sets the block device named C<device> to read-write.
1545 This uses the L<blockdev(8)> command.");
1547 ("blockdev_getro", (RBool "ro", [String "device"]), 58, [],
1548 [InitEmpty, Always, TestOutputTrue (
1549 [["blockdev_setro"; "/dev/sda"];
1550 ["blockdev_getro"; "/dev/sda"]])],
1551 "is block device set to read-only",
1553 Returns a boolean indicating if the block device is read-only
1554 (true if read-only, false if not).
1556 This uses the L<blockdev(8)> command.");
1558 ("blockdev_getss", (RInt "sectorsize", [String "device"]), 59, [],
1559 [InitEmpty, Always, TestOutputInt (
1560 [["blockdev_getss"; "/dev/sda"]], 512)],
1561 "get sectorsize of block device",
1563 This returns the size of sectors on a block device.
1564 Usually 512, but can be larger for modern devices.
1566 (Note, this is not the size in sectors, use C<guestfs_blockdev_getsz>
1569 This uses the L<blockdev(8)> command.");
1571 ("blockdev_getbsz", (RInt "blocksize", [String "device"]), 60, [],
1572 [InitEmpty, Always, TestOutputInt (
1573 [["blockdev_getbsz"; "/dev/sda"]], 4096)],
1574 "get blocksize of block device",
1576 This returns the block size of a device.
1578 (Note this is different from both I<size in blocks> and
1579 I<filesystem block size>).
1581 This uses the L<blockdev(8)> command.");
1583 ("blockdev_setbsz", (RErr, [String "device"; Int "blocksize"]), 61, [],
1585 "set blocksize of block device",
1587 This sets the block size of a device.
1589 (Note this is different from both I<size in blocks> and
1590 I<filesystem block size>).
1592 This uses the L<blockdev(8)> command.");
1594 ("blockdev_getsz", (RInt64 "sizeinsectors", [String "device"]), 62, [],
1595 [InitEmpty, Always, TestOutputInt (
1596 [["blockdev_getsz"; "/dev/sda"]], 1024000)],
1597 "get total size of device in 512-byte sectors",
1599 This returns the size of the device in units of 512-byte sectors
1600 (even if the sectorsize isn't 512 bytes ... weird).
1602 See also C<guestfs_blockdev_getss> for the real sector size of
1603 the device, and C<guestfs_blockdev_getsize64> for the more
1604 useful I<size in bytes>.
1606 This uses the L<blockdev(8)> command.");
1608 ("blockdev_getsize64", (RInt64 "sizeinbytes", [String "device"]), 63, [],
1609 [InitEmpty, Always, TestOutputInt (
1610 [["blockdev_getsize64"; "/dev/sda"]], 524288000)],
1611 "get total size of device in bytes",
1613 This returns the size of the device in bytes.
1615 See also C<guestfs_blockdev_getsz>.
1617 This uses the L<blockdev(8)> command.");
1619 ("blockdev_flushbufs", (RErr, [String "device"]), 64, [],
1620 [InitEmpty, Always, TestRun
1621 [["blockdev_flushbufs"; "/dev/sda"]]],
1622 "flush device buffers",
1624 This tells the kernel to flush internal buffers associated
1627 This uses the L<blockdev(8)> command.");
1629 ("blockdev_rereadpt", (RErr, [String "device"]), 65, [],
1630 [InitEmpty, Always, TestRun
1631 [["blockdev_rereadpt"; "/dev/sda"]]],
1632 "reread partition table",
1634 Reread the partition table on C<device>.
1636 This uses the L<blockdev(8)> command.");
1638 ("upload", (RErr, [FileIn "filename"; String "remotefilename"]), 66, [],
1639 [InitBasicFS, Always, TestOutput (
1640 (* Pick a file from cwd which isn't likely to change. *)
1641 [["upload"; "../COPYING.LIB"; "/COPYING.LIB"];
1642 ["checksum"; "md5"; "/COPYING.LIB"]], "e3eda01d9815f8d24aae2dbd89b68b06")],
1643 "upload a file from the local machine",
1645 Upload local file C<filename> to C<remotefilename> on the
1648 C<filename> can also be a named pipe.
1650 See also C<guestfs_download>.");
1652 ("download", (RErr, [String "remotefilename"; FileOut "filename"]), 67, [],
1653 [InitBasicFS, Always, TestOutput (
1654 (* Pick a file from cwd which isn't likely to change. *)
1655 [["upload"; "../COPYING.LIB"; "/COPYING.LIB"];
1656 ["download"; "/COPYING.LIB"; "testdownload.tmp"];
1657 ["upload"; "testdownload.tmp"; "/upload"];
1658 ["checksum"; "md5"; "/upload"]], "e3eda01d9815f8d24aae2dbd89b68b06")],
1659 "download a file to the local machine",
1661 Download file C<remotefilename> and save it as C<filename>
1662 on the local machine.
1664 C<filename> can also be a named pipe.
1666 See also C<guestfs_upload>, C<guestfs_cat>.");
1668 ("checksum", (RString "checksum", [String "csumtype"; String "path"]), 68, [],
1669 [InitBasicFS, Always, TestOutput (
1670 [["write_file"; "/new"; "test\n"; "0"];
1671 ["checksum"; "crc"; "/new"]], "935282863");
1672 InitBasicFS, Always, TestLastFail (
1673 [["checksum"; "crc"; "/new"]]);
1674 InitBasicFS, Always, TestOutput (
1675 [["write_file"; "/new"; "test\n"; "0"];
1676 ["checksum"; "md5"; "/new"]], "d8e8fca2dc0f896fd7cb4cb0031ba249");
1677 InitBasicFS, Always, TestOutput (
1678 [["write_file"; "/new"; "test\n"; "0"];
1679 ["checksum"; "sha1"; "/new"]], "4e1243bd22c66e76c2ba9eddc1f91394e57f9f83");
1680 InitBasicFS, Always, TestOutput (
1681 [["write_file"; "/new"; "test\n"; "0"];
1682 ["checksum"; "sha224"; "/new"]], "52f1bf093f4b7588726035c176c0cdb4376cfea53819f1395ac9e6ec");
1683 InitBasicFS, Always, TestOutput (
1684 [["write_file"; "/new"; "test\n"; "0"];
1685 ["checksum"; "sha256"; "/new"]], "f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2");
1686 InitBasicFS, Always, TestOutput (
1687 [["write_file"; "/new"; "test\n"; "0"];
1688 ["checksum"; "sha384"; "/new"]], "109bb6b5b6d5547c1ce03c7a8bd7d8f80c1cb0957f50c4f7fda04692079917e4f9cad52b878f3d8234e1a170b154b72d");
1689 InitBasicFS, Always, TestOutput (
1690 [["write_file"; "/new"; "test\n"; "0"];
1691 ["checksum"; "sha512"; "/new"]], "0e3e75234abc68f4378a86b3f4b32a198ba301845b0cd6e50106e874345700cc6663a86c1ea125dc5e92be17c98f9a0f85ca9d5f595db2012f7cc3571945c123");
1692 InitBasicFS, Always, TestOutput (
1693 (* RHEL 5 thinks this is an HFS+ filesystem unless we give
1694 * the type explicitly.
1696 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
1697 ["checksum"; "md5"; "/known-3"]], "46d6ca27ee07cdc6fa99c2e138cc522c")],
1698 "compute MD5, SHAx or CRC checksum of file",
1700 This call computes the MD5, SHAx or CRC checksum of the
1703 The type of checksum to compute is given by the C<csumtype>
1704 parameter which must have one of the following values:
1710 Compute the cyclic redundancy check (CRC) specified by POSIX
1711 for the C<cksum> command.
1715 Compute the MD5 hash (using the C<md5sum> program).
1719 Compute the SHA1 hash (using the C<sha1sum> program).
1723 Compute the SHA224 hash (using the C<sha224sum> program).
1727 Compute the SHA256 hash (using the C<sha256sum> program).
1731 Compute the SHA384 hash (using the C<sha384sum> program).
1735 Compute the SHA512 hash (using the C<sha512sum> program).
1739 The checksum is returned as a printable string.");
1741 ("tar_in", (RErr, [FileIn "tarfile"; String "directory"]), 69, [],
1742 [InitBasicFS, Always, TestOutput (
1743 [["tar_in"; "../images/helloworld.tar"; "/"];
1744 ["cat"; "/hello"]], "hello\n")],
1745 "unpack tarfile to directory",
1747 This command uploads and unpacks local file C<tarfile> (an
1748 I<uncompressed> tar file) into C<directory>.
1750 To upload a compressed tarball, use C<guestfs_tgz_in>.");
1752 ("tar_out", (RErr, [String "directory"; FileOut "tarfile"]), 70, [],
1754 "pack directory into tarfile",
1756 This command packs the contents of C<directory> and downloads
1757 it to local file C<tarfile>.
1759 To download a compressed tarball, use C<guestfs_tgz_out>.");
1761 ("tgz_in", (RErr, [FileIn "tarball"; String "directory"]), 71, [],
1762 [InitBasicFS, Always, TestOutput (
1763 [["tgz_in"; "../images/helloworld.tar.gz"; "/"];
1764 ["cat"; "/hello"]], "hello\n")],
1765 "unpack compressed tarball to directory",
1767 This command uploads and unpacks local file C<tarball> (a
1768 I<gzip compressed> tar file) into C<directory>.
1770 To upload an uncompressed tarball, use C<guestfs_tar_in>.");
1772 ("tgz_out", (RErr, [String "directory"; FileOut "tarball"]), 72, [],
1774 "pack directory into compressed tarball",
1776 This command packs the contents of C<directory> and downloads
1777 it to local file C<tarball>.
1779 To download an uncompressed tarball, use C<guestfs_tar_out>.");
1781 ("mount_ro", (RErr, [String "device"; String "mountpoint"]), 73, [],
1782 [InitBasicFS, Always, TestLastFail (
1784 ["mount_ro"; "/dev/sda1"; "/"];
1785 ["touch"; "/new"]]);
1786 InitBasicFS, Always, TestOutput (
1787 [["write_file"; "/new"; "data"; "0"];
1789 ["mount_ro"; "/dev/sda1"; "/"];
1790 ["cat"; "/new"]], "data")],
1791 "mount a guest disk, read-only",
1793 This is the same as the C<guestfs_mount> command, but it
1794 mounts the filesystem with the read-only (I<-o ro>) flag.");
1796 ("mount_options", (RErr, [String "options"; String "device"; String "mountpoint"]), 74, [],
1798 "mount a guest disk with mount options",
1800 This is the same as the C<guestfs_mount> command, but it
1801 allows you to set the mount options as for the
1802 L<mount(8)> I<-o> flag.");
1804 ("mount_vfs", (RErr, [String "options"; String "vfstype"; String "device"; String "mountpoint"]), 75, [],
1806 "mount a guest disk with mount options and vfstype",
1808 This is the same as the C<guestfs_mount> command, but it
1809 allows you to set both the mount options and the vfstype
1810 as for the L<mount(8)> I<-o> and I<-t> flags.");
1812 ("debug", (RString "result", [String "subcmd"; StringList "extraargs"]), 76, [],
1814 "debugging and internals",
1816 The C<guestfs_debug> command exposes some internals of
1817 C<guestfsd> (the guestfs daemon) that runs inside the
1820 There is no comprehensive help for this command. You have
1821 to look at the file C<daemon/debug.c> in the libguestfs source
1822 to find out what you can do.");
1824 ("lvremove", (RErr, [String "device"]), 77, [],
1825 [InitEmpty, Always, TestOutputList (
1826 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1827 ["pvcreate"; "/dev/sda1"];
1828 ["vgcreate"; "VG"; "/dev/sda1"];
1829 ["lvcreate"; "LV1"; "VG"; "50"];
1830 ["lvcreate"; "LV2"; "VG"; "50"];
1831 ["lvremove"; "/dev/VG/LV1"];
1832 ["lvs"]], ["/dev/VG/LV2"]);
1833 InitEmpty, Always, TestOutputList (
1834 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1835 ["pvcreate"; "/dev/sda1"];
1836 ["vgcreate"; "VG"; "/dev/sda1"];
1837 ["lvcreate"; "LV1"; "VG"; "50"];
1838 ["lvcreate"; "LV2"; "VG"; "50"];
1839 ["lvremove"; "/dev/VG"];
1841 InitEmpty, Always, TestOutputList (
1842 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1843 ["pvcreate"; "/dev/sda1"];
1844 ["vgcreate"; "VG"; "/dev/sda1"];
1845 ["lvcreate"; "LV1"; "VG"; "50"];
1846 ["lvcreate"; "LV2"; "VG"; "50"];
1847 ["lvremove"; "/dev/VG"];
1849 "remove an LVM logical volume",
1851 Remove an LVM logical volume C<device>, where C<device> is
1852 the path to the LV, such as C</dev/VG/LV>.
1854 You can also remove all LVs in a volume group by specifying
1855 the VG name, C</dev/VG>.");
1857 ("vgremove", (RErr, [String "vgname"]), 78, [],
1858 [InitEmpty, Always, TestOutputList (
1859 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1860 ["pvcreate"; "/dev/sda1"];
1861 ["vgcreate"; "VG"; "/dev/sda1"];
1862 ["lvcreate"; "LV1"; "VG"; "50"];
1863 ["lvcreate"; "LV2"; "VG"; "50"];
1866 InitEmpty, Always, TestOutputList (
1867 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1868 ["pvcreate"; "/dev/sda1"];
1869 ["vgcreate"; "VG"; "/dev/sda1"];
1870 ["lvcreate"; "LV1"; "VG"; "50"];
1871 ["lvcreate"; "LV2"; "VG"; "50"];
1874 "remove an LVM volume group",
1876 Remove an LVM volume group C<vgname>, (for example C<VG>).
1878 This also forcibly removes all logical volumes in the volume
1881 ("pvremove", (RErr, [String "device"]), 79, [],
1882 [InitEmpty, Always, TestOutputListOfDevices (
1883 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1884 ["pvcreate"; "/dev/sda1"];
1885 ["vgcreate"; "VG"; "/dev/sda1"];
1886 ["lvcreate"; "LV1"; "VG"; "50"];
1887 ["lvcreate"; "LV2"; "VG"; "50"];
1889 ["pvremove"; "/dev/sda1"];
1891 InitEmpty, Always, TestOutputListOfDevices (
1892 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1893 ["pvcreate"; "/dev/sda1"];
1894 ["vgcreate"; "VG"; "/dev/sda1"];
1895 ["lvcreate"; "LV1"; "VG"; "50"];
1896 ["lvcreate"; "LV2"; "VG"; "50"];
1898 ["pvremove"; "/dev/sda1"];
1900 InitEmpty, Always, TestOutputListOfDevices (
1901 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
1902 ["pvcreate"; "/dev/sda1"];
1903 ["vgcreate"; "VG"; "/dev/sda1"];
1904 ["lvcreate"; "LV1"; "VG"; "50"];
1905 ["lvcreate"; "LV2"; "VG"; "50"];
1907 ["pvremove"; "/dev/sda1"];
1909 "remove an LVM physical volume",
1911 This wipes a physical volume C<device> so that LVM will no longer
1914 The implementation uses the C<pvremove> command which refuses to
1915 wipe physical volumes that contain any volume groups, so you have
1916 to remove those first.");
1918 ("set_e2label", (RErr, [String "device"; String "label"]), 80, [],
1919 [InitBasicFS, Always, TestOutput (
1920 [["set_e2label"; "/dev/sda1"; "testlabel"];
1921 ["get_e2label"; "/dev/sda1"]], "testlabel")],
1922 "set the ext2/3/4 filesystem label",
1924 This sets the ext2/3/4 filesystem label of the filesystem on
1925 C<device> to C<label>. Filesystem labels are limited to
1928 You can use either C<guestfs_tune2fs_l> or C<guestfs_get_e2label>
1929 to return the existing label on a filesystem.");
1931 ("get_e2label", (RString "label", [String "device"]), 81, [],
1933 "get the ext2/3/4 filesystem label",
1935 This returns the ext2/3/4 filesystem label of the filesystem on
1938 ("set_e2uuid", (RErr, [String "device"; String "uuid"]), 82, [],
1939 [InitBasicFS, Always, TestOutput (
1940 [["set_e2uuid"; "/dev/sda1"; "a3a61220-882b-4f61-89f4-cf24dcc7297d"];
1941 ["get_e2uuid"; "/dev/sda1"]], "a3a61220-882b-4f61-89f4-cf24dcc7297d");
1942 InitBasicFS, Always, TestOutput (
1943 [["set_e2uuid"; "/dev/sda1"; "clear"];
1944 ["get_e2uuid"; "/dev/sda1"]], "");
1945 (* We can't predict what UUIDs will be, so just check the commands run. *)
1946 InitBasicFS, Always, TestRun (
1947 [["set_e2uuid"; "/dev/sda1"; "random"]]);
1948 InitBasicFS, Always, TestRun (
1949 [["set_e2uuid"; "/dev/sda1"; "time"]])],
1950 "set the ext2/3/4 filesystem UUID",
1952 This sets the ext2/3/4 filesystem UUID of the filesystem on
1953 C<device> to C<uuid>. The format of the UUID and alternatives
1954 such as C<clear>, C<random> and C<time> are described in the
1955 L<tune2fs(8)> manpage.
1957 You can use either C<guestfs_tune2fs_l> or C<guestfs_get_e2uuid>
1958 to return the existing UUID of a filesystem.");
1960 ("get_e2uuid", (RString "uuid", [String "device"]), 83, [],
1962 "get the ext2/3/4 filesystem UUID",
1964 This returns the ext2/3/4 filesystem UUID of the filesystem on
1967 ("fsck", (RInt "status", [String "fstype"; String "device"]), 84, [],
1968 [InitBasicFS, Always, TestOutputInt (
1969 [["umount"; "/dev/sda1"];
1970 ["fsck"; "ext2"; "/dev/sda1"]], 0);
1971 InitBasicFS, Always, TestOutputInt (
1972 [["umount"; "/dev/sda1"];
1973 ["zero"; "/dev/sda1"];
1974 ["fsck"; "ext2"; "/dev/sda1"]], 8)],
1975 "run the filesystem checker",
1977 This runs the filesystem checker (fsck) on C<device> which
1978 should have filesystem type C<fstype>.
1980 The returned integer is the status. See L<fsck(8)> for the
1981 list of status codes from C<fsck>.
1989 Multiple status codes can be summed together.
1993 A non-zero return code can mean \"success\", for example if
1994 errors have been corrected on the filesystem.
1998 Checking or repairing NTFS volumes is not supported
2003 This command is entirely equivalent to running C<fsck -a -t fstype device>.");
2005 ("zero", (RErr, [String "device"]), 85, [],
2006 [InitBasicFS, Always, TestOutput (
2007 [["umount"; "/dev/sda1"];
2008 ["zero"; "/dev/sda1"];
2009 ["file"; "/dev/sda1"]], "data")],
2010 "write zeroes to the device",
2012 This command writes zeroes over the first few blocks of C<device>.
2014 How many blocks are zeroed isn't specified (but it's I<not> enough
2015 to securely wipe the device). It should be sufficient to remove
2016 any partition tables, filesystem superblocks and so on.
2018 See also: C<guestfs_scrub_device>.");
2020 ("grub_install", (RErr, [String "root"; String "device"]), 86, [],
2021 (* Test disabled because grub-install incompatible with virtio-blk driver.
2022 * See also: https://bugzilla.redhat.com/show_bug.cgi?id=479760
2024 [InitBasicFS, Disabled, TestOutputTrue (
2025 [["grub_install"; "/"; "/dev/sda1"];
2026 ["is_dir"; "/boot"]])],
2029 This command installs GRUB (the Grand Unified Bootloader) on
2030 C<device>, with the root directory being C<root>.");
2032 ("cp", (RErr, [String "src"; String "dest"]), 87, [],
2033 [InitBasicFS, Always, TestOutput (
2034 [["write_file"; "/old"; "file content"; "0"];
2035 ["cp"; "/old"; "/new"];
2036 ["cat"; "/new"]], "file content");
2037 InitBasicFS, Always, TestOutputTrue (
2038 [["write_file"; "/old"; "file content"; "0"];
2039 ["cp"; "/old"; "/new"];
2040 ["is_file"; "/old"]]);
2041 InitBasicFS, Always, TestOutput (
2042 [["write_file"; "/old"; "file content"; "0"];
2044 ["cp"; "/old"; "/dir/new"];
2045 ["cat"; "/dir/new"]], "file content")],
2048 This copies a file from C<src> to C<dest> where C<dest> is
2049 either a destination filename or destination directory.");
2051 ("cp_a", (RErr, [String "src"; String "dest"]), 88, [],
2052 [InitBasicFS, Always, TestOutput (
2053 [["mkdir"; "/olddir"];
2054 ["mkdir"; "/newdir"];
2055 ["write_file"; "/olddir/file"; "file content"; "0"];
2056 ["cp_a"; "/olddir"; "/newdir"];
2057 ["cat"; "/newdir/olddir/file"]], "file content")],
2058 "copy a file or directory recursively",
2060 This copies a file or directory from C<src> to C<dest>
2061 recursively using the C<cp -a> command.");
2063 ("mv", (RErr, [String "src"; String "dest"]), 89, [],
2064 [InitBasicFS, Always, TestOutput (
2065 [["write_file"; "/old"; "file content"; "0"];
2066 ["mv"; "/old"; "/new"];
2067 ["cat"; "/new"]], "file content");
2068 InitBasicFS, Always, TestOutputFalse (
2069 [["write_file"; "/old"; "file content"; "0"];
2070 ["mv"; "/old"; "/new"];
2071 ["is_file"; "/old"]])],
2074 This moves a file from C<src> to C<dest> where C<dest> is
2075 either a destination filename or destination directory.");
2077 ("drop_caches", (RErr, [Int "whattodrop"]), 90, [],
2078 [InitEmpty, Always, TestRun (
2079 [["drop_caches"; "3"]])],
2080 "drop kernel page cache, dentries and inodes",
2082 This instructs the guest kernel to drop its page cache,
2083 and/or dentries and inode caches. The parameter C<whattodrop>
2084 tells the kernel what precisely to drop, see
2085 L<http://linux-mm.org/Drop_Caches>
2087 Setting C<whattodrop> to 3 should drop everything.
2089 This automatically calls L<sync(2)> before the operation,
2090 so that the maximum guest memory is freed.");
2092 ("dmesg", (RString "kmsgs", []), 91, [],
2093 [InitEmpty, Always, TestRun (
2095 "return kernel messages",
2097 This returns the kernel messages (C<dmesg> output) from
2098 the guest kernel. This is sometimes useful for extended
2099 debugging of problems.
2101 Another way to get the same information is to enable
2102 verbose messages with C<guestfs_set_verbose> or by setting
2103 the environment variable C<LIBGUESTFS_DEBUG=1> before
2104 running the program.");
2106 ("ping_daemon", (RErr, []), 92, [],
2107 [InitEmpty, Always, TestRun (
2108 [["ping_daemon"]])],
2109 "ping the guest daemon",
2111 This is a test probe into the guestfs daemon running inside
2112 the qemu subprocess. Calling this function checks that the
2113 daemon responds to the ping message, without affecting the daemon
2114 or attached block device(s) in any other way.");
2116 ("equal", (RBool "equality", [String "file1"; String "file2"]), 93, [],
2117 [InitBasicFS, Always, TestOutputTrue (
2118 [["write_file"; "/file1"; "contents of a file"; "0"];
2119 ["cp"; "/file1"; "/file2"];
2120 ["equal"; "/file1"; "/file2"]]);
2121 InitBasicFS, Always, TestOutputFalse (
2122 [["write_file"; "/file1"; "contents of a file"; "0"];
2123 ["write_file"; "/file2"; "contents of another file"; "0"];
2124 ["equal"; "/file1"; "/file2"]]);
2125 InitBasicFS, Always, TestLastFail (
2126 [["equal"; "/file1"; "/file2"]])],
2127 "test if two files have equal contents",
2129 This compares the two files C<file1> and C<file2> and returns
2130 true if their content is exactly equal, or false otherwise.
2132 The external L<cmp(1)> program is used for the comparison.");
2134 ("strings", (RStringList "stringsout", [String "path"]), 94, [ProtocolLimitWarning],
2135 [InitBasicFS, Always, TestOutputList (
2136 [["write_file"; "/new"; "hello\nworld\n"; "0"];
2137 ["strings"; "/new"]], ["hello"; "world"]);
2138 InitBasicFS, Always, TestOutputList (
2140 ["strings"; "/new"]], [])],
2141 "print the printable strings in a file",
2143 This runs the L<strings(1)> command on a file and returns
2144 the list of printable strings found.");
2146 ("strings_e", (RStringList "stringsout", [String "encoding"; String "path"]), 95, [ProtocolLimitWarning],
2147 [InitBasicFS, Always, TestOutputList (
2148 [["write_file"; "/new"; "hello\nworld\n"; "0"];
2149 ["strings_e"; "b"; "/new"]], []);
2150 InitBasicFS, Disabled, TestOutputList (
2151 [["write_file"; "/new"; "\000h\000e\000l\000l\000o\000\n\000w\000o\000r\000l\000d\000\n"; "24"];
2152 ["strings_e"; "b"; "/new"]], ["hello"; "world"])],
2153 "print the printable strings in a file",
2155 This is like the C<guestfs_strings> command, but allows you to
2156 specify the encoding.
2158 See the L<strings(1)> manpage for the full list of encodings.
2160 Commonly useful encodings are C<l> (lower case L) which will
2161 show strings inside Windows/x86 files.
2163 The returned strings are transcoded to UTF-8.");
2165 ("hexdump", (RString "dump", [String "path"]), 96, [ProtocolLimitWarning],
2166 [InitBasicFS, Always, TestOutput (
2167 [["write_file"; "/new"; "hello\nworld\n"; "12"];
2168 ["hexdump"; "/new"]], "00000000 68 65 6c 6c 6f 0a 77 6f 72 6c 64 0a |hello.world.|\n0000000c\n");
2169 (* Test for RHBZ#501888c2 regression which caused large hexdump
2170 * commands to segfault.
2172 InitBasicFS, Always, TestRun (
2173 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2174 ["hexdump"; "/100krandom"]])],
2175 "dump a file in hexadecimal",
2177 This runs C<hexdump -C> on the given C<path>. The result is
2178 the human-readable, canonical hex dump of the file.");
2180 ("zerofree", (RErr, [String "device"]), 97, [],
2181 [InitNone, Always, TestOutput (
2182 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
2183 ["mkfs"; "ext3"; "/dev/sda1"];
2184 ["mount"; "/dev/sda1"; "/"];
2185 ["write_file"; "/new"; "test file"; "0"];
2186 ["umount"; "/dev/sda1"];
2187 ["zerofree"; "/dev/sda1"];
2188 ["mount"; "/dev/sda1"; "/"];
2189 ["cat"; "/new"]], "test file")],
2190 "zero unused inodes and disk blocks on ext2/3 filesystem",
2192 This runs the I<zerofree> program on C<device>. This program
2193 claims to zero unused inodes and disk blocks on an ext2/3
2194 filesystem, thus making it possible to compress the filesystem
2197 You should B<not> run this program if the filesystem is
2200 It is possible that using this program can damage the filesystem
2201 or data on the filesystem.");
2203 ("pvresize", (RErr, [String "device"]), 98, [],
2205 "resize an LVM physical volume",
2207 This resizes (expands or shrinks) an existing LVM physical
2208 volume to match the new size of the underlying device.");
2210 ("sfdisk_N", (RErr, [String "device"; Int "partnum";
2211 Int "cyls"; Int "heads"; Int "sectors";
2212 String "line"]), 99, [DangerWillRobinson],
2214 "modify a single partition on a block device",
2216 This runs L<sfdisk(8)> option to modify just the single
2217 partition C<n> (note: C<n> counts from 1).
2219 For other parameters, see C<guestfs_sfdisk>. You should usually
2220 pass C<0> for the cyls/heads/sectors parameters.");
2222 ("sfdisk_l", (RString "partitions", [String "device"]), 100, [],
2224 "display the partition table",
2226 This displays the partition table on C<device>, in the
2227 human-readable output of the L<sfdisk(8)> command. It is
2228 not intended to be parsed.");
2230 ("sfdisk_kernel_geometry", (RString "partitions", [String "device"]), 101, [],
2232 "display the kernel geometry",
2234 This displays the kernel's idea of the geometry of C<device>.
2236 The result is in human-readable format, and not designed to
2239 ("sfdisk_disk_geometry", (RString "partitions", [String "device"]), 102, [],
2241 "display the disk geometry from the partition table",
2243 This displays the disk geometry of C<device> read from the
2244 partition table. Especially in the case where the underlying
2245 block device has been resized, this can be different from the
2246 kernel's idea of the geometry (see C<guestfs_sfdisk_kernel_geometry>).
2248 The result is in human-readable format, and not designed to
2251 ("vg_activate_all", (RErr, [Bool "activate"]), 103, [],
2253 "activate or deactivate all volume groups",
2255 This command activates or (if C<activate> is false) deactivates
2256 all logical volumes in all volume groups.
2257 If activated, then they are made known to the
2258 kernel, ie. they appear as C</dev/mapper> devices. If deactivated,
2259 then those devices disappear.
2261 This command is the same as running C<vgchange -a y|n>");
2263 ("vg_activate", (RErr, [Bool "activate"; StringList "volgroups"]), 104, [],
2265 "activate or deactivate some volume groups",
2267 This command activates or (if C<activate> is false) deactivates
2268 all logical volumes in the listed volume groups C<volgroups>.
2269 If activated, then they are made known to the
2270 kernel, ie. they appear as C</dev/mapper> devices. If deactivated,
2271 then those devices disappear.
2273 This command is the same as running C<vgchange -a y|n volgroups...>
2275 Note that if C<volgroups> is an empty list then B<all> volume groups
2276 are activated or deactivated.");
2278 ("lvresize", (RErr, [String "device"; Int "mbytes"]), 105, [],
2279 [InitNone, Always, TestOutput (
2280 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
2281 ["pvcreate"; "/dev/sda1"];
2282 ["vgcreate"; "VG"; "/dev/sda1"];
2283 ["lvcreate"; "LV"; "VG"; "10"];
2284 ["mkfs"; "ext2"; "/dev/VG/LV"];
2285 ["mount"; "/dev/VG/LV"; "/"];
2286 ["write_file"; "/new"; "test content"; "0"];
2288 ["lvresize"; "/dev/VG/LV"; "20"];
2289 ["e2fsck_f"; "/dev/VG/LV"];
2290 ["resize2fs"; "/dev/VG/LV"];
2291 ["mount"; "/dev/VG/LV"; "/"];
2292 ["cat"; "/new"]], "test content")],
2293 "resize an LVM logical volume",
2295 This resizes (expands or shrinks) an existing LVM logical
2296 volume to C<mbytes>. When reducing, data in the reduced part
2299 ("resize2fs", (RErr, [String "device"]), 106, [],
2300 [], (* lvresize tests this *)
2301 "resize an ext2/ext3 filesystem",
2303 This resizes an ext2 or ext3 filesystem to match the size of
2304 the underlying device.
2306 I<Note:> It is sometimes required that you run C<guestfs_e2fsck_f>
2307 on the C<device> before calling this command. For unknown reasons
2308 C<resize2fs> sometimes gives an error about this and sometimes not.
2309 In any case, it is always safe to call C<guestfs_e2fsck_f> before
2310 calling this function.");
2312 ("find", (RStringList "names", [String "directory"]), 107, [],
2313 [InitBasicFS, Always, TestOutputList (
2314 [["find"; "/"]], ["lost+found"]);
2315 InitBasicFS, Always, TestOutputList (
2319 ["find"; "/"]], ["a"; "b"; "b/c"; "lost+found"]);
2320 InitBasicFS, Always, TestOutputList (
2321 [["mkdir_p"; "/a/b/c"];
2322 ["touch"; "/a/b/c/d"];
2323 ["find"; "/a/b/"]], ["c"; "c/d"])],
2324 "find all files and directories",
2326 This command lists out all files and directories, recursively,
2327 starting at C<directory>. It is essentially equivalent to
2328 running the shell command C<find directory -print> but some
2329 post-processing happens on the output, described below.
2331 This returns a list of strings I<without any prefix>. Thus
2332 if the directory structure was:
2338 then the returned list from C<guestfs_find> C</tmp> would be
2346 If C<directory> is not a directory, then this command returns
2349 The returned list is sorted.");
2351 ("e2fsck_f", (RErr, [String "device"]), 108, [],
2352 [], (* lvresize tests this *)
2353 "check an ext2/ext3 filesystem",
2355 This runs C<e2fsck -p -f device>, ie. runs the ext2/ext3
2356 filesystem checker on C<device>, noninteractively (C<-p>),
2357 even if the filesystem appears to be clean (C<-f>).
2359 This command is only needed because of C<guestfs_resize2fs>
2360 (q.v.). Normally you should use C<guestfs_fsck>.");
2362 ("sleep", (RErr, [Int "secs"]), 109, [],
2363 [InitNone, Always, TestRun (
2365 "sleep for some seconds",
2367 Sleep for C<secs> seconds.");
2369 ("ntfs_3g_probe", (RInt "status", [Bool "rw"; String "device"]), 110, [],
2370 [InitNone, Always, TestOutputInt (
2371 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
2372 ["mkfs"; "ntfs"; "/dev/sda1"];
2373 ["ntfs_3g_probe"; "true"; "/dev/sda1"]], 0);
2374 InitNone, Always, TestOutputInt (
2375 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
2376 ["mkfs"; "ext2"; "/dev/sda1"];
2377 ["ntfs_3g_probe"; "true"; "/dev/sda1"]], 12)],
2378 "probe NTFS volume",
2380 This command runs the L<ntfs-3g.probe(8)> command which probes
2381 an NTFS C<device> for mountability. (Not all NTFS volumes can
2382 be mounted read-write, and some cannot be mounted at all).
2384 C<rw> is a boolean flag. Set it to true if you want to test
2385 if the volume can be mounted read-write. Set it to false if
2386 you want to test if the volume can be mounted read-only.
2388 The return value is an integer which C<0> if the operation
2389 would succeed, or some non-zero value documented in the
2390 L<ntfs-3g.probe(8)> manual page.");
2392 ("sh", (RString "output", [String "command"]), 111, [],
2393 [], (* XXX needs tests *)
2394 "run a command via the shell",
2396 This call runs a command from the guest filesystem via the
2399 This is like C<guestfs_command>, but passes the command to:
2401 /bin/sh -c \"command\"
2403 Depending on the guest's shell, this usually results in
2404 wildcards being expanded, shell expressions being interpolated
2407 All the provisos about C<guestfs_command> apply to this call.");
2409 ("sh_lines", (RStringList "lines", [String "command"]), 112, [],
2410 [], (* XXX needs tests *)
2411 "run a command via the shell returning lines",
2413 This is the same as C<guestfs_sh>, but splits the result
2414 into a list of lines.
2416 See also: C<guestfs_command_lines>");
2418 ("glob_expand", (RStringList "paths", [String "pattern"]), 113, [],
2419 [InitBasicFS, Always, TestOutputList (
2420 [["mkdir_p"; "/a/b/c"];
2421 ["touch"; "/a/b/c/d"];
2422 ["touch"; "/a/b/c/e"];
2423 ["glob_expand"; "/a/b/c/*"]], ["/a/b/c/d"; "/a/b/c/e"]);
2424 InitBasicFS, Always, TestOutputList (
2425 [["mkdir_p"; "/a/b/c"];
2426 ["touch"; "/a/b/c/d"];
2427 ["touch"; "/a/b/c/e"];
2428 ["glob_expand"; "/a/*/c/*"]], ["/a/b/c/d"; "/a/b/c/e"]);
2429 InitBasicFS, Always, TestOutputList (
2430 [["mkdir_p"; "/a/b/c"];
2431 ["touch"; "/a/b/c/d"];
2432 ["touch"; "/a/b/c/e"];
2433 ["glob_expand"; "/a/*/x/*"]], [])],
2434 "expand a wildcard path",
2436 This command searches for all the pathnames matching
2437 C<pattern> according to the wildcard expansion rules
2440 If no paths match, then this returns an empty list
2441 (note: not an error).
2443 It is just a wrapper around the C L<glob(3)> function
2444 with flags C<GLOB_MARK|GLOB_BRACE>.
2445 See that manual page for more details.");
2447 ("scrub_device", (RErr, [String "device"]), 114, [DangerWillRobinson],
2448 [InitNone, Always, TestRun ( (* use /dev/sdc because it's smaller *)
2449 [["scrub_device"; "/dev/sdc"]])],
2450 "scrub (securely wipe) a device",
2452 This command writes patterns over C<device> to make data retrieval
2455 It is an interface to the L<scrub(1)> program. See that
2456 manual page for more details.");
2458 ("scrub_file", (RErr, [String "file"]), 115, [],
2459 [InitBasicFS, Always, TestRun (
2460 [["write_file"; "/file"; "content"; "0"];
2461 ["scrub_file"; "/file"]])],
2462 "scrub (securely wipe) a file",
2464 This command writes patterns over a file to make data retrieval
2467 The file is I<removed> after scrubbing.
2469 It is an interface to the L<scrub(1)> program. See that
2470 manual page for more details.");
2472 ("scrub_freespace", (RErr, [String "dir"]), 116, [],
2473 [], (* XXX needs testing *)
2474 "scrub (securely wipe) free space",
2476 This command creates the directory C<dir> and then fills it
2477 with files until the filesystem is full, and scrubs the files
2478 as for C<guestfs_scrub_file>, and deletes them.
2479 The intention is to scrub any free space on the partition
2482 It is an interface to the L<scrub(1)> program. See that
2483 manual page for more details.");
2485 ("mkdtemp", (RString "dir", [String "template"]), 117, [],
2486 [InitBasicFS, Always, TestRun (
2488 ["mkdtemp"; "/tmp/tmpXXXXXX"]])],
2489 "create a temporary directory",
2491 This command creates a temporary directory. The
2492 C<template> parameter should be a full pathname for the
2493 temporary directory name with the final six characters being
2496 For example: \"/tmp/myprogXXXXXX\" or \"/Temp/myprogXXXXXX\",
2497 the second one being suitable for Windows filesystems.
2499 The name of the temporary directory that was created
2502 The temporary directory is created with mode 0700
2503 and is owned by root.
2505 The caller is responsible for deleting the temporary
2506 directory and its contents after use.
2508 See also: L<mkdtemp(3)>");
2510 ("wc_l", (RInt "lines", [String "path"]), 118, [],
2511 [InitBasicFS, Always, TestOutputInt (
2512 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2513 ["wc_l"; "/10klines"]], 10000)],
2514 "count lines in a file",
2516 This command counts the lines in a file, using the
2517 C<wc -l> external command.");
2519 ("wc_w", (RInt "words", [String "path"]), 119, [],
2520 [InitBasicFS, Always, TestOutputInt (
2521 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2522 ["wc_w"; "/10klines"]], 10000)],
2523 "count words in a file",
2525 This command counts the words in a file, using the
2526 C<wc -w> external command.");
2528 ("wc_c", (RInt "chars", [String "path"]), 120, [],
2529 [InitBasicFS, Always, TestOutputInt (
2530 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2531 ["wc_c"; "/100kallspaces"]], 102400)],
2532 "count characters in a file",
2534 This command counts the characters in a file, using the
2535 C<wc -c> external command.");
2537 ("head", (RStringList "lines", [String "path"]), 121, [ProtocolLimitWarning],
2538 [InitBasicFS, Always, TestOutputList (
2539 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2540 ["head"; "/10klines"]], ["0abcdefghijklmnopqrstuvwxyz";"1abcdefghijklmnopqrstuvwxyz";"2abcdefghijklmnopqrstuvwxyz";"3abcdefghijklmnopqrstuvwxyz";"4abcdefghijklmnopqrstuvwxyz";"5abcdefghijklmnopqrstuvwxyz";"6abcdefghijklmnopqrstuvwxyz";"7abcdefghijklmnopqrstuvwxyz";"8abcdefghijklmnopqrstuvwxyz";"9abcdefghijklmnopqrstuvwxyz"])],
2541 "return first 10 lines of a file",
2543 This command returns up to the first 10 lines of a file as
2544 a list of strings.");
2546 ("head_n", (RStringList "lines", [Int "nrlines"; String "path"]), 122, [ProtocolLimitWarning],
2547 [InitBasicFS, Always, TestOutputList (
2548 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2549 ["head_n"; "3"; "/10klines"]], ["0abcdefghijklmnopqrstuvwxyz";"1abcdefghijklmnopqrstuvwxyz";"2abcdefghijklmnopqrstuvwxyz"]);
2550 InitBasicFS, Always, TestOutputList (
2551 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2552 ["head_n"; "-9997"; "/10klines"]], ["0abcdefghijklmnopqrstuvwxyz";"1abcdefghijklmnopqrstuvwxyz";"2abcdefghijklmnopqrstuvwxyz"]);
2553 InitBasicFS, Always, TestOutputList (
2554 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2555 ["head_n"; "0"; "/10klines"]], [])],
2556 "return first N lines of a file",
2558 If the parameter C<nrlines> is a positive number, this returns the first
2559 C<nrlines> lines of the file C<path>.
2561 If the parameter C<nrlines> is a negative number, this returns lines
2562 from the file C<path>, excluding the last C<nrlines> lines.
2564 If the parameter C<nrlines> is zero, this returns an empty list.");
2566 ("tail", (RStringList "lines", [String "path"]), 123, [ProtocolLimitWarning],
2567 [InitBasicFS, Always, TestOutputList (
2568 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2569 ["tail"; "/10klines"]], ["9990abcdefghijklmnopqrstuvwxyz";"9991abcdefghijklmnopqrstuvwxyz";"9992abcdefghijklmnopqrstuvwxyz";"9993abcdefghijklmnopqrstuvwxyz";"9994abcdefghijklmnopqrstuvwxyz";"9995abcdefghijklmnopqrstuvwxyz";"9996abcdefghijklmnopqrstuvwxyz";"9997abcdefghijklmnopqrstuvwxyz";"9998abcdefghijklmnopqrstuvwxyz";"9999abcdefghijklmnopqrstuvwxyz"])],
2570 "return last 10 lines of a file",
2572 This command returns up to the last 10 lines of a file as
2573 a list of strings.");
2575 ("tail_n", (RStringList "lines", [Int "nrlines"; String "path"]), 124, [ProtocolLimitWarning],
2576 [InitBasicFS, Always, TestOutputList (
2577 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2578 ["tail_n"; "3"; "/10klines"]], ["9997abcdefghijklmnopqrstuvwxyz";"9998abcdefghijklmnopqrstuvwxyz";"9999abcdefghijklmnopqrstuvwxyz"]);
2579 InitBasicFS, Always, TestOutputList (
2580 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2581 ["tail_n"; "-9998"; "/10klines"]], ["9997abcdefghijklmnopqrstuvwxyz";"9998abcdefghijklmnopqrstuvwxyz";"9999abcdefghijklmnopqrstuvwxyz"]);
2582 InitBasicFS, Always, TestOutputList (
2583 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2584 ["tail_n"; "0"; "/10klines"]], [])],
2585 "return last N lines of a file",
2587 If the parameter C<nrlines> is a positive number, this returns the last
2588 C<nrlines> lines of the file C<path>.
2590 If the parameter C<nrlines> is a negative number, this returns lines
2591 from the file C<path>, starting with the C<-nrlines>th line.
2593 If the parameter C<nrlines> is zero, this returns an empty list.");
2595 ("df", (RString "output", []), 125, [],
2596 [], (* XXX Tricky to test because it depends on the exact format
2597 * of the 'df' command and other imponderables.
2599 "report file system disk space usage",
2601 This command runs the C<df> command to report disk space used.
2603 This command is mostly useful for interactive sessions. It
2604 is I<not> intended that you try to parse the output string.
2605 Use C<statvfs> from programs.");
2607 ("df_h", (RString "output", []), 126, [],
2608 [], (* XXX Tricky to test because it depends on the exact format
2609 * of the 'df' command and other imponderables.
2611 "report file system disk space usage (human readable)",
2613 This command runs the C<df -h> command to report disk space used
2614 in human-readable format.
2616 This command is mostly useful for interactive sessions. It
2617 is I<not> intended that you try to parse the output string.
2618 Use C<statvfs> from programs.");
2620 ("du", (RInt64 "sizekb", [String "path"]), 127, [],
2621 [InitBasicFS, Always, TestOutputInt (
2623 ["du"; "/p"]], 1 (* ie. 1 block, so depends on ext3 blocksize *))],
2624 "estimate file space usage",
2626 This command runs the C<du -s> command to estimate file space
2629 C<path> can be a file or a directory. If C<path> is a directory
2630 then the estimate includes the contents of the directory and all
2631 subdirectories (recursively).
2633 The result is the estimated size in I<kilobytes>
2634 (ie. units of 1024 bytes).");
2636 ("initrd_list", (RStringList "filenames", [String "path"]), 128, [],
2637 [InitBasicFS, Always, TestOutputList (
2638 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2639 ["initrd_list"; "/initrd"]], ["empty";"known-1";"known-2";"known-3"])],
2640 "list files in an initrd",
2642 This command lists out files contained in an initrd.
2644 The files are listed without any initial C</> character. The
2645 files are listed in the order they appear (not necessarily
2646 alphabetical). Directory names are listed as separate items.
2648 Old Linux kernels (2.4 and earlier) used a compressed ext2
2649 filesystem as initrd. We I<only> support the newer initramfs
2650 format (compressed cpio files).");
2652 ("mount_loop", (RErr, [String "file"; String "mountpoint"]), 129, [],
2654 "mount a file using the loop device",
2656 This command lets you mount C<file> (a filesystem image
2657 in a file) on a mount point. It is entirely equivalent to
2658 the command C<mount -o loop file mountpoint>.");
2660 ("mkswap", (RErr, [String "device"]), 130, [],
2661 [InitEmpty, Always, TestRun (
2662 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
2663 ["mkswap"; "/dev/sda1"]])],
2664 "create a swap partition",
2666 Create a swap partition on C<device>.");
2668 ("mkswap_L", (RErr, [String "label"; String "device"]), 131, [],
2669 [InitEmpty, Always, TestRun (
2670 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
2671 ["mkswap_L"; "hello"; "/dev/sda1"]])],
2672 "create a swap partition with a label",
2674 Create a swap partition on C<device> with label C<label>.");
2676 ("mkswap_U", (RErr, [String "uuid"; String "device"]), 132, [],
2677 [InitEmpty, Always, TestRun (
2678 [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
2679 ["mkswap_U"; "a3a61220-882b-4f61-89f4-cf24dcc7297d"; "/dev/sda1"]])],
2680 "create a swap partition with an explicit UUID",
2682 Create a swap partition on C<device> with UUID C<uuid>.");
2684 ("mknod", (RErr, [Int "mode"; Int "devmajor"; Int "devminor"; String "path"]), 133, [],
2685 [InitBasicFS, Always, TestOutputStruct (
2686 [["mknod"; "0o10777"; "0"; "0"; "/node"];
2687 (* NB: default umask 022 means 0777 -> 0755 in these tests *)
2688 ["stat"; "/node"]], [CompareWithInt ("mode", 0o10755)]);
2689 InitBasicFS, Always, TestOutputStruct (
2690 [["mknod"; "0o60777"; "66"; "99"; "/node"];
2691 ["stat"; "/node"]], [CompareWithInt ("mode", 0o60755)])],
2692 "make block, character or FIFO devices",
2694 This call creates block or character special devices, or
2695 named pipes (FIFOs).
2697 The C<mode> parameter should be the mode, using the standard
2698 constants. C<devmajor> and C<devminor> are the
2699 device major and minor numbers, only used when creating block
2700 and character special devices.");
2702 ("mkfifo", (RErr, [Int "mode"; String "path"]), 134, [],
2703 [InitBasicFS, Always, TestOutputStruct (
2704 [["mkfifo"; "0o777"; "/node"];
2705 ["stat"; "/node"]], [CompareWithInt ("mode", 0o10755)])],
2706 "make FIFO (named pipe)",
2708 This call creates a FIFO (named pipe) called C<path> with
2709 mode C<mode>. It is just a convenient wrapper around
2710 C<guestfs_mknod>.");
2712 ("mknod_b", (RErr, [Int "mode"; Int "devmajor"; Int "devminor"; String "path"]), 135, [],
2713 [InitBasicFS, Always, TestOutputStruct (
2714 [["mknod_b"; "0o777"; "99"; "66"; "/node"];
2715 ["stat"; "/node"]], [CompareWithInt ("mode", 0o60755)])],
2716 "make block device node",
2718 This call creates a block device node called C<path> with
2719 mode C<mode> and device major/minor C<devmajor> and C<devminor>.
2720 It is just a convenient wrapper around C<guestfs_mknod>.");
2722 ("mknod_c", (RErr, [Int "mode"; Int "devmajor"; Int "devminor"; String "path"]), 136, [],
2723 [InitBasicFS, Always, TestOutputStruct (
2724 [["mknod_c"; "0o777"; "99"; "66"; "/node"];
2725 ["stat"; "/node"]], [CompareWithInt ("mode", 0o20755)])],
2726 "make char device node",
2728 This call creates a char device node called C<path> with
2729 mode C<mode> and device major/minor C<devmajor> and C<devminor>.
2730 It is just a convenient wrapper around C<guestfs_mknod>.");
2732 ("umask", (RInt "oldmask", [Int "mask"]), 137, [],
2733 [], (* XXX umask is one of those stateful things that we should
2734 * reset between each test.
2736 "set file mode creation mask (umask)",
2738 This function sets the mask used for creating new files and
2739 device nodes to C<mask & 0777>.
2741 Typical umask values would be C<022> which creates new files
2742 with permissions like \"-rw-r--r--\" or \"-rwxr-xr-x\", and
2743 C<002> which creates new files with permissions like
2744 \"-rw-rw-r--\" or \"-rwxrwxr-x\".
2746 The default umask is C<022>. This is important because it
2747 means that directories and device nodes will be created with
2748 C<0644> or C<0755> mode even if you specify C<0777>.
2750 See also L<umask(2)>, C<guestfs_mknod>, C<guestfs_mkdir>.
2752 This call returns the previous umask.");
2756 let all_functions = non_daemon_functions @ daemon_functions
2758 (* In some places we want the functions to be displayed sorted
2759 * alphabetically, so this is useful:
2761 let all_functions_sorted =
2762 List.sort (fun (n1,_,_,_,_,_,_) (n2,_,_,_,_,_,_) ->
2763 compare n1 n2) all_functions
2765 (* Column names and types from LVM PVs/VGs/LVs. *)
2774 "pv_attr", `String (* XXX *);
2775 "pv_pe_count", `Int;
2776 "pv_pe_alloc_count", `Int;
2779 "pv_mda_count", `Int;
2780 "pv_mda_free", `Bytes;
2781 (* Not in Fedora 10:
2782 "pv_mda_size", `Bytes;
2789 "vg_attr", `String (* XXX *);
2792 "vg_sysid", `String;
2793 "vg_extent_size", `Bytes;
2794 "vg_extent_count", `Int;
2795 "vg_free_count", `Int;
2803 "vg_mda_count", `Int;
2804 "vg_mda_free", `Bytes;
2805 (* Not in Fedora 10:
2806 "vg_mda_size", `Bytes;
2812 "lv_attr", `String (* XXX *);
2815 "lv_kernel_major", `Int;
2816 "lv_kernel_minor", `Int;
2820 "snap_percent", `OptPercent;
2821 "copy_percent", `OptPercent;
2824 "mirror_log", `String;
2828 (* Column names and types from stat structures.
2829 * NB. Can't use things like 'st_atime' because glibc header files
2830 * define some of these as macros. Ugh.
2847 let statvfs_cols = [
2861 (* Used for testing language bindings. *)
2863 | CallString of string
2864 | CallOptString of string option
2865 | CallStringList of string list
2869 (* Useful functions.
2870 * Note we don't want to use any external OCaml libraries which
2871 * makes this a bit harder than it should be.
2873 let failwithf fs = ksprintf failwith fs
2875 let replace_char s c1 c2 =
2876 let s2 = String.copy s in
2877 let r = ref false in
2878 for i = 0 to String.length s2 - 1 do
2879 if String.unsafe_get s2 i = c1 then (
2880 String.unsafe_set s2 i c2;
2884 if not !r then s else s2
2888 (* || c = '\f' *) || c = '\n' || c = '\r' || c = '\t' (* || c = '\v' *)
2890 let triml ?(test = isspace) str =
2892 let n = ref (String.length str) in
2893 while !n > 0 && test str.[!i]; do
2898 else String.sub str !i !n
2900 let trimr ?(test = isspace) str =
2901 let n = ref (String.length str) in
2902 while !n > 0 && test str.[!n-1]; do
2905 if !n = String.length str then str
2906 else String.sub str 0 !n
2908 let trim ?(test = isspace) str =
2909 trimr ~test (triml ~test str)
2911 let rec find s sub =
2912 let len = String.length s in
2913 let sublen = String.length sub in
2915 if i <= len-sublen then (
2917 if j < sublen then (
2918 if s.[i+j] = sub.[j] then loop2 (j+1)
2924 if r = -1 then loop (i+1) else r
2930 let rec replace_str s s1 s2 =
2931 let len = String.length s in
2932 let sublen = String.length s1 in
2933 let i = find s s1 in
2936 let s' = String.sub s 0 i in
2937 let s'' = String.sub s (i+sublen) (len-i-sublen) in
2938 s' ^ s2 ^ replace_str s'' s1 s2
2941 let rec string_split sep str =
2942 let len = String.length str in
2943 let seplen = String.length sep in
2944 let i = find str sep in
2945 if i = -1 then [str]
2947 let s' = String.sub str 0 i in
2948 let s'' = String.sub str (i+seplen) (len-i-seplen) in
2949 s' :: string_split sep s''
2952 let files_equal n1 n2 =
2953 let cmd = sprintf "cmp -s %s %s" (Filename.quote n1) (Filename.quote n2) in
2954 match Sys.command cmd with
2957 | i -> failwithf "%s: failed with error code %d" cmd i
2959 let rec find_map f = function
2960 | [] -> raise Not_found
2964 | None -> find_map f xs
2967 let rec loop i = function
2969 | x :: xs -> f i x; loop (i+1) xs
2974 let rec loop i = function
2976 | x :: xs -> let r = f i x in r :: loop (i+1) xs
2980 let name_of_argt = function
2981 | String n | OptString n | StringList n | Bool n | Int n
2982 | FileIn n | FileOut n -> n
2984 let seq_of_test = function
2985 | TestRun s | TestOutput (s, _) | TestOutputList (s, _)
2986 | TestOutputListOfDevices (s, _)
2987 | TestOutputInt (s, _) | TestOutputTrue s | TestOutputFalse s
2988 | TestOutputLength (s, _) | TestOutputStruct (s, _)
2989 | TestLastFail s -> s
2991 (* Check function names etc. for consistency. *)
2992 let check_functions () =
2993 let contains_uppercase str =
2994 let len = String.length str in
2996 if i >= len then false
2999 if c >= 'A' && c <= 'Z' then true
3006 (* Check function names. *)
3008 fun (name, _, _, _, _, _, _) ->
3009 if String.length name >= 7 && String.sub name 0 7 = "guestfs" then
3010 failwithf "function name %s does not need 'guestfs' prefix" name;
3012 failwithf "function name is empty";
3013 if name.[0] < 'a' || name.[0] > 'z' then
3014 failwithf "function name %s must start with lowercase a-z" name;
3015 if String.contains name '-' then
3016 failwithf "function name %s should not contain '-', use '_' instead."
3020 (* Check function parameter/return names. *)
3022 fun (name, style, _, _, _, _, _) ->
3023 let check_arg_ret_name n =
3024 if contains_uppercase n then
3025 failwithf "%s param/ret %s should not contain uppercase chars"
3027 if String.contains n '-' || String.contains n '_' then
3028 failwithf "%s param/ret %s should not contain '-' or '_'"
3031 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;
3032 if n = "int" || n = "char" || n = "short" || n = "long" then
3033 failwithf "%s has a param/ret which conflicts with a C type (eg. 'int', 'char' etc.)" name;
3034 if n = "i" || n = "n" then
3035 failwithf "%s has a param/ret called 'i' or 'n', which will cause some conflicts in the generated code" name;
3036 if n = "argv" || n = "args" then
3037 failwithf "%s has a param/ret called 'argv' or 'args', which will cause some conflicts in the generated code" name
3040 (match fst style with
3042 | RInt n | RInt64 n | RBool n | RConstString n | RString n
3043 | RStringList n | RPVList n | RVGList n | RLVList n
3044 | RStat n | RStatVFS n
3046 check_arg_ret_name n
3048 check_arg_ret_name n;
3049 check_arg_ret_name m
3051 List.iter (fun arg -> check_arg_ret_name (name_of_argt arg)) (snd style)
3054 (* Check short descriptions. *)
3056 fun (name, _, _, _, _, shortdesc, _) ->
3057 if shortdesc.[0] <> Char.lowercase shortdesc.[0] then
3058 failwithf "short description of %s should begin with lowercase." name;
3059 let c = shortdesc.[String.length shortdesc-1] in
3060 if c = '\n' || c = '.' then
3061 failwithf "short description of %s should not end with . or \\n." name
3064 (* Check long dscriptions. *)
3066 fun (name, _, _, _, _, _, longdesc) ->
3067 if longdesc.[String.length longdesc-1] = '\n' then
3068 failwithf "long description of %s should not end with \\n." name
3071 (* Check proc_nrs. *)
3073 fun (name, _, proc_nr, _, _, _, _) ->
3074 if proc_nr <= 0 then
3075 failwithf "daemon function %s should have proc_nr > 0" name
3079 fun (name, _, proc_nr, _, _, _, _) ->
3080 if proc_nr <> -1 then
3081 failwithf "non-daemon function %s should have proc_nr -1" name
3082 ) non_daemon_functions;
3085 List.map (fun (name, _, proc_nr, _, _, _, _) -> name, proc_nr)
3088 List.sort (fun (_,nr1) (_,nr2) -> compare nr1 nr2) proc_nrs in
3089 let rec loop = function
3092 | (name1,nr1) :: ((name2,nr2) :: _ as rest) when nr1 < nr2 ->
3094 | (name1,nr1) :: (name2,nr2) :: _ ->
3095 failwithf "%s and %s have conflicting procedure numbers (%d, %d)"
3103 (* Ignore functions that have no tests. We generate a
3104 * warning when the user does 'make check' instead.
3106 | name, _, _, _, [], _, _ -> ()
3107 | name, _, _, _, tests, _, _ ->
3111 match seq_of_test test with
3113 failwithf "%s has a test containing an empty sequence" name
3114 | cmds -> List.map List.hd cmds
3116 let funcs = List.flatten funcs in
3118 let tested = List.mem name funcs in
3121 failwithf "function %s has tests but does not test itself" name
3124 (* 'pr' prints to the current output file. *)
3125 let chan = ref stdout
3126 let pr fs = ksprintf (output_string !chan) fs
3128 (* Generate a header block in a number of standard styles. *)
3129 type comment_style = CStyle | HashStyle | OCamlStyle | HaskellStyle
3130 type license = GPLv2 | LGPLv2
3132 let generate_header comment license =
3133 let c = match comment with
3134 | CStyle -> pr "/* "; " *"
3135 | HashStyle -> pr "# "; "#"
3136 | OCamlStyle -> pr "(* "; " *"
3137 | HaskellStyle -> pr "{- "; " " in
3138 pr "libguestfs generated file\n";
3139 pr "%s WARNING: THIS FILE IS GENERATED BY 'src/generator.ml'.\n" c;
3140 pr "%s ANY CHANGES YOU MAKE TO THIS FILE WILL BE LOST.\n" c;
3142 pr "%s Copyright (C) 2009 Red Hat Inc.\n" c;
3146 pr "%s This program is free software; you can redistribute it and/or modify\n" c;
3147 pr "%s it under the terms of the GNU General Public License as published by\n" c;
3148 pr "%s the Free Software Foundation; either version 2 of the License, or\n" c;
3149 pr "%s (at your option) any later version.\n" c;
3151 pr "%s This program is distributed in the hope that it will be useful,\n" c;
3152 pr "%s but WITHOUT ANY WARRANTY; without even the implied warranty of\n" c;
3153 pr "%s MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" c;
3154 pr "%s GNU General Public License for more details.\n" c;
3156 pr "%s You should have received a copy of the GNU General Public License along\n" c;
3157 pr "%s with this program; if not, write to the Free Software Foundation, Inc.,\n" c;
3158 pr "%s 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\n" c;
3161 pr "%s This library is free software; you can redistribute it and/or\n" c;
3162 pr "%s modify it under the terms of the GNU Lesser General Public\n" c;
3163 pr "%s License as published by the Free Software Foundation; either\n" c;
3164 pr "%s version 2 of the License, or (at your option) any later version.\n" c;
3166 pr "%s This library is distributed in the hope that it will be useful,\n" c;
3167 pr "%s but WITHOUT ANY WARRANTY; without even the implied warranty of\n" c;
3168 pr "%s MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n" c;
3169 pr "%s Lesser General Public License for more details.\n" c;
3171 pr "%s You should have received a copy of the GNU Lesser General Public\n" c;
3172 pr "%s License along with this library; if not, write to the Free Software\n" c;
3173 pr "%s Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n" c;
3176 | CStyle -> pr " */\n"
3178 | OCamlStyle -> pr " *)\n"
3179 | HaskellStyle -> pr "-}\n"
3183 (* Start of main code generation functions below this line. *)
3185 (* Generate the pod documentation for the C API. *)
3186 let rec generate_actions_pod () =
3188 fun (shortname, style, _, flags, _, _, longdesc) ->
3189 if not (List.mem NotInDocs flags) then (
3190 let name = "guestfs_" ^ shortname in
3191 pr "=head2 %s\n\n" name;
3193 generate_prototype ~extern:false ~handle:"handle" name style;
3195 pr "%s\n\n" longdesc;
3196 (match fst style with
3198 pr "This function returns 0 on success or -1 on error.\n\n"
3200 pr "On error this function returns -1.\n\n"
3202 pr "On error this function returns -1.\n\n"
3204 pr "This function returns a C truth value on success or -1 on error.\n\n"
3206 pr "This function returns a string, or NULL on error.
3207 The string is owned by the guest handle and must I<not> be freed.\n\n"
3209 pr "This function returns a string, or NULL on error.
3210 I<The caller must free the returned string after use>.\n\n"
3212 pr "This function returns a NULL-terminated array of strings
3213 (like L<environ(3)>), or NULL if there was an error.
3214 I<The caller must free the strings and the array after use>.\n\n"
3216 pr "This function returns a C<struct guestfs_int_bool *>,
3217 or NULL if there was an error.
3218 I<The caller must call C<guestfs_free_int_bool> after use>.\n\n"
3220 pr "This function returns a C<struct guestfs_lvm_pv_list *>
3221 (see E<lt>guestfs-structs.hE<gt>),
3222 or NULL if there was an error.
3223 I<The caller must call C<guestfs_free_lvm_pv_list> after use>.\n\n"
3225 pr "This function returns a C<struct guestfs_lvm_vg_list *>
3226 (see E<lt>guestfs-structs.hE<gt>),
3227 or NULL if there was an error.
3228 I<The caller must call C<guestfs_free_lvm_vg_list> after use>.\n\n"
3230 pr "This function returns a C<struct guestfs_lvm_lv_list *>
3231 (see E<lt>guestfs-structs.hE<gt>),
3232 or NULL if there was an error.
3233 I<The caller must call C<guestfs_free_lvm_lv_list> after use>.\n\n"
3235 pr "This function returns a C<struct guestfs_stat *>
3236 (see L<stat(2)> and E<lt>guestfs-structs.hE<gt>),
3237 or NULL if there was an error.
3238 I<The caller must call C<free> after use>.\n\n"
3240 pr "This function returns a C<struct guestfs_statvfs *>
3241 (see L<statvfs(2)> and E<lt>guestfs-structs.hE<gt>),
3242 or NULL if there was an error.
3243 I<The caller must call C<free> after use>.\n\n"
3245 pr "This function returns a NULL-terminated array of
3246 strings, or NULL if there was an error.
3247 The array of strings will always have length C<2n+1>, where
3248 C<n> keys and values alternate, followed by the trailing NULL entry.
3249 I<The caller must free the strings and the array after use>.\n\n"
3251 if List.mem ProtocolLimitWarning flags then
3252 pr "%s\n\n" protocol_limit_warning;
3253 if List.mem DangerWillRobinson flags then
3254 pr "%s\n\n" danger_will_robinson
3256 ) all_functions_sorted
3258 and generate_structs_pod () =
3259 (* LVM structs documentation. *)
3262 pr "=head2 guestfs_lvm_%s\n" typ;
3264 pr " struct guestfs_lvm_%s {\n" typ;
3267 | name, `String -> pr " char *%s;\n" name
3269 pr " /* The next field is NOT nul-terminated, be careful when printing it: */\n";
3270 pr " char %s[32];\n" name
3271 | name, `Bytes -> pr " uint64_t %s;\n" name
3272 | name, `Int -> pr " int64_t %s;\n" name
3273 | name, `OptPercent ->
3274 pr " /* The next field is [0..100] or -1 meaning 'not present': */\n";
3275 pr " float %s;\n" name
3278 pr " struct guestfs_lvm_%s_list {\n" typ;
3279 pr " uint32_t len; /* Number of elements in list. */\n";
3280 pr " struct guestfs_lvm_%s *val; /* Elements. */\n" typ;
3283 pr " void guestfs_free_lvm_%s_list (struct guestfs_free_lvm_%s_list *);\n"
3286 ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols]
3288 (* Generate the protocol (XDR) file, 'guestfs_protocol.x' and
3289 * indirectly 'guestfs_protocol.h' and 'guestfs_protocol.c'.
3291 * We have to use an underscore instead of a dash because otherwise
3292 * rpcgen generates incorrect code.
3294 * This header is NOT exported to clients, but see also generate_structs_h.
3296 and generate_xdr () =
3297 generate_header CStyle LGPLv2;
3299 (* This has to be defined to get around a limitation in Sun's rpcgen. *)
3300 pr "typedef string str<>;\n";
3303 (* LVM internal structures. *)
3307 pr "struct guestfs_lvm_int_%s {\n" typ;
3309 | name, `String -> pr " string %s<>;\n" name
3310 | name, `UUID -> pr " opaque %s[32];\n" name
3311 | name, `Bytes -> pr " hyper %s;\n" name
3312 | name, `Int -> pr " hyper %s;\n" name
3313 | name, `OptPercent -> pr " float %s;\n" name
3317 pr "typedef struct guestfs_lvm_int_%s guestfs_lvm_int_%s_list<>;\n" typ typ;
3319 ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols];
3321 (* Stat internal structures. *)
3325 pr "struct guestfs_int_%s {\n" typ;
3327 | name, `Int -> pr " hyper %s;\n" name
3331 ) ["stat", stat_cols; "statvfs", statvfs_cols];
3334 fun (shortname, style, _, _, _, _, _) ->
3335 let name = "guestfs_" ^ shortname in
3337 (match snd style with
3340 pr "struct %s_args {\n" name;
3343 | String n -> pr " string %s<>;\n" n
3344 | OptString n -> pr " str *%s;\n" n
3345 | StringList n -> pr " str %s<>;\n" n
3346 | Bool n -> pr " bool %s;\n" n
3347 | Int n -> pr " int %s;\n" n
3348 | FileIn _ | FileOut _ -> ()
3352 (match fst style with
3355 pr "struct %s_ret {\n" name;
3359 pr "struct %s_ret {\n" name;
3360 pr " hyper %s;\n" n;
3363 pr "struct %s_ret {\n" name;
3367 failwithf "RConstString cannot be returned from a daemon function"
3369 pr "struct %s_ret {\n" name;
3370 pr " string %s<>;\n" n;
3373 pr "struct %s_ret {\n" name;
3374 pr " str %s<>;\n" n;
3377 pr "struct %s_ret {\n" name;
3382 pr "struct %s_ret {\n" name;
3383 pr " guestfs_lvm_int_pv_list %s;\n" n;
3386 pr "struct %s_ret {\n" name;
3387 pr " guestfs_lvm_int_vg_list %s;\n" n;
3390 pr "struct %s_ret {\n" name;
3391 pr " guestfs_lvm_int_lv_list %s;\n" n;
3394 pr "struct %s_ret {\n" name;
3395 pr " guestfs_int_stat %s;\n" n;
3398 pr "struct %s_ret {\n" name;
3399 pr " guestfs_int_statvfs %s;\n" n;
3402 pr "struct %s_ret {\n" name;
3403 pr " str %s<>;\n" n;
3408 (* Table of procedure numbers. *)
3409 pr "enum guestfs_procedure {\n";
3411 fun (shortname, _, proc_nr, _, _, _, _) ->
3412 pr " GUESTFS_PROC_%s = %d,\n" (String.uppercase shortname) proc_nr
3414 pr " GUESTFS_PROC_NR_PROCS\n";
3418 (* Having to choose a maximum message size is annoying for several
3419 * reasons (it limits what we can do in the API), but it (a) makes
3420 * the protocol a lot simpler, and (b) provides a bound on the size
3421 * of the daemon which operates in limited memory space. For large
3422 * file transfers you should use FTP.
3424 pr "const GUESTFS_MESSAGE_MAX = %d;\n" (4 * 1024 * 1024);
3427 (* Message header, etc. *)
3429 /* The communication protocol is now documented in the guestfs(3)
3433 const GUESTFS_PROGRAM = 0x2000F5F5;
3434 const GUESTFS_PROTOCOL_VERSION = 1;
3436 /* These constants must be larger than any possible message length. */
3437 const GUESTFS_LAUNCH_FLAG = 0xf5f55ff5;
3438 const GUESTFS_CANCEL_FLAG = 0xffffeeee;
3440 enum guestfs_message_direction {
3441 GUESTFS_DIRECTION_CALL = 0, /* client -> daemon */
3442 GUESTFS_DIRECTION_REPLY = 1 /* daemon -> client */
3445 enum guestfs_message_status {
3446 GUESTFS_STATUS_OK = 0,
3447 GUESTFS_STATUS_ERROR = 1
3450 const GUESTFS_ERROR_LEN = 256;
3452 struct guestfs_message_error {
3453 string error_message<GUESTFS_ERROR_LEN>;
3456 struct guestfs_message_header {
3457 unsigned prog; /* GUESTFS_PROGRAM */
3458 unsigned vers; /* GUESTFS_PROTOCOL_VERSION */
3459 guestfs_procedure proc; /* GUESTFS_PROC_x */
3460 guestfs_message_direction direction;
3461 unsigned serial; /* message serial number */
3462 guestfs_message_status status;
3465 const GUESTFS_MAX_CHUNK_SIZE = 8192;
3467 struct guestfs_chunk {
3468 int cancel; /* if non-zero, transfer is cancelled */
3469 /* data size is 0 bytes if the transfer has finished successfully */
3470 opaque data<GUESTFS_MAX_CHUNK_SIZE>;
3474 (* Generate the guestfs-structs.h file. *)
3475 and generate_structs_h () =
3476 generate_header CStyle LGPLv2;
3478 (* This is a public exported header file containing various
3479 * structures. The structures are carefully written to have
3480 * exactly the same in-memory format as the XDR structures that
3481 * we use on the wire to the daemon. The reason for creating
3482 * copies of these structures here is just so we don't have to
3483 * export the whole of guestfs_protocol.h (which includes much
3484 * unrelated and XDR-dependent stuff that we don't want to be
3485 * public, or required by clients).
3487 * To reiterate, we will pass these structures to and from the
3488 * client with a simple assignment or memcpy, so the format
3489 * must be identical to what rpcgen / the RFC defines.
3492 (* guestfs_int_bool structure. *)
3493 pr "struct guestfs_int_bool {\n";
3499 (* LVM public structures. *)
3503 pr "struct guestfs_lvm_%s {\n" typ;
3506 | name, `String -> pr " char *%s;\n" name
3507 | name, `UUID -> pr " char %s[32]; /* this is NOT nul-terminated, be careful when printing */\n" name
3508 | name, `Bytes -> pr " uint64_t %s;\n" name
3509 | name, `Int -> pr " int64_t %s;\n" name
3510 | name, `OptPercent -> pr " float %s; /* [0..100] or -1 */\n" name
3514 pr "struct guestfs_lvm_%s_list {\n" typ;
3515 pr " uint32_t len;\n";
3516 pr " struct guestfs_lvm_%s *val;\n" typ;
3519 ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols];
3521 (* Stat structures. *)
3525 pr "struct guestfs_%s {\n" typ;
3528 | name, `Int -> pr " int64_t %s;\n" name
3532 ) ["stat", stat_cols; "statvfs", statvfs_cols]
3534 (* Generate the guestfs-actions.h file. *)
3535 and generate_actions_h () =
3536 generate_header CStyle LGPLv2;
3538 fun (shortname, style, _, _, _, _, _) ->
3539 let name = "guestfs_" ^ shortname in
3540 generate_prototype ~single_line:true ~newline:true ~handle:"handle"
3544 (* Generate the client-side dispatch stubs. *)
3545 and generate_client_actions () =
3546 generate_header CStyle LGPLv2;
3552 #include \"guestfs.h\"
3553 #include \"guestfs_protocol.h\"
3555 #define error guestfs_error
3556 #define perrorf guestfs_perrorf
3557 #define safe_malloc guestfs_safe_malloc
3558 #define safe_realloc guestfs_safe_realloc
3559 #define safe_strdup guestfs_safe_strdup
3560 #define safe_memdup guestfs_safe_memdup
3562 /* Check the return message from a call for validity. */
3564 check_reply_header (guestfs_h *g,
3565 const struct guestfs_message_header *hdr,
3566 int proc_nr, int serial)
3568 if (hdr->prog != GUESTFS_PROGRAM) {
3569 error (g, \"wrong program (%%d/%%d)\", hdr->prog, GUESTFS_PROGRAM);
3572 if (hdr->vers != GUESTFS_PROTOCOL_VERSION) {
3573 error (g, \"wrong protocol version (%%d/%%d)\",
3574 hdr->vers, GUESTFS_PROTOCOL_VERSION);
3577 if (hdr->direction != GUESTFS_DIRECTION_REPLY) {
3578 error (g, \"unexpected message direction (%%d/%%d)\",
3579 hdr->direction, GUESTFS_DIRECTION_REPLY);
3582 if (hdr->proc != proc_nr) {
3583 error (g, \"unexpected procedure number (%%d/%%d)\", hdr->proc, proc_nr);
3586 if (hdr->serial != serial) {
3587 error (g, \"unexpected serial (%%d/%%d)\", hdr->serial, serial);
3594 /* Check we are in the right state to run a high-level action. */
3596 check_state (guestfs_h *g, const char *caller)
3598 if (!guestfs_is_ready (g)) {
3599 if (guestfs_is_config (g))
3600 error (g, \"%%s: call launch() before using this function\",
3602 else if (guestfs_is_launching (g))
3603 error (g, \"%%s: call wait_ready() before using this function\",
3606 error (g, \"%%s called from the wrong state, %%d != READY\",
3607 caller, guestfs_get_state (g));
3615 (* Client-side stubs for each function. *)
3617 fun (shortname, style, _, _, _, _, _) ->
3618 let name = "guestfs_" ^ shortname in
3620 (* Generate the context struct which stores the high-level
3621 * state between callback functions.
3623 pr "struct %s_ctx {\n" shortname;
3624 pr " /* This flag is set by the callbacks, so we know we've done\n";
3625 pr " * the callbacks as expected, and in the right sequence.\n";
3626 pr " * 0 = not called, 1 = reply_cb called.\n";
3628 pr " int cb_sequence;\n";
3629 pr " struct guestfs_message_header hdr;\n";
3630 pr " struct guestfs_message_error err;\n";
3631 (match fst style with
3634 failwithf "RConstString cannot be returned from a daemon function"
3636 | RBool _ | RString _ | RStringList _
3638 | RPVList _ | RVGList _ | RLVList _
3639 | RStat _ | RStatVFS _
3641 pr " struct %s_ret ret;\n" name
3646 (* Generate the reply callback function. *)
3647 pr "static void %s_reply_cb (guestfs_h *g, void *data, XDR *xdr)\n" shortname;
3649 pr " guestfs_main_loop *ml = guestfs_get_main_loop (g);\n";
3650 pr " struct %s_ctx *ctx = (struct %s_ctx *) data;\n" shortname shortname;
3652 pr " /* This should definitely not happen. */\n";
3653 pr " if (ctx->cb_sequence != 0) {\n";
3654 pr " ctx->cb_sequence = 9999;\n";
3655 pr " error (g, \"%%s: internal error: reply callback called twice\", \"%s\");\n" name;
3659 pr " ml->main_loop_quit (ml, g);\n";
3661 pr " if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {\n";
3662 pr " error (g, \"%%s: failed to parse reply header\", \"%s\");\n" name;
3665 pr " if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {\n";
3666 pr " if (!xdr_guestfs_message_error (xdr, &ctx->err)) {\n";
3667 pr " error (g, \"%%s: failed to parse reply error\", \"%s\");\n"
3674 (match fst style with
3677 failwithf "RConstString cannot be returned from a daemon function"
3679 | RBool _ | RString _ | RStringList _
3681 | RPVList _ | RVGList _ | RLVList _
3682 | RStat _ | RStatVFS _
3684 pr " if (!xdr_%s_ret (xdr, &ctx->ret)) {\n" name;
3685 pr " error (g, \"%%s: failed to parse reply\", \"%s\");\n" name;
3691 pr " ctx->cb_sequence = 1;\n";
3694 (* Generate the action stub. *)
3695 generate_prototype ~extern:false ~semicolon:false ~newline:true
3696 ~handle:"g" name style;
3699 match fst style with
3700 | RErr | RInt _ | RInt64 _ | RBool _ -> "-1"
3702 failwithf "RConstString cannot be returned from a daemon function"
3703 | RString _ | RStringList _ | RIntBool _
3704 | RPVList _ | RVGList _ | RLVList _
3705 | RStat _ | RStatVFS _
3711 (match snd style with
3713 | _ -> pr " struct %s_args args;\n" name
3716 pr " struct %s_ctx ctx;\n" shortname;
3717 pr " guestfs_main_loop *ml = guestfs_get_main_loop (g);\n";
3718 pr " int serial;\n";
3720 pr " if (check_state (g, \"%s\") == -1) return %s;\n" name error_code;
3721 pr " guestfs_set_busy (g);\n";
3723 pr " memset (&ctx, 0, sizeof ctx);\n";
3726 (* Send the main header and arguments. *)
3727 (match snd style with
3729 pr " serial = guestfs__send_sync (g, GUESTFS_PROC_%s, NULL, NULL);\n"
3730 (String.uppercase shortname)
3735 pr " args.%s = (char *) %s;\n" n n
3737 pr " args.%s = %s ? (char **) &%s : NULL;\n" n n n
3739 pr " args.%s.%s_val = (char **) %s;\n" n n n;
3740 pr " for (args.%s.%s_len = 0; %s[args.%s.%s_len]; args.%s.%s_len++) ;\n" n n n n n n n;
3742 pr " args.%s = %s;\n" n n
3744 pr " args.%s = %s;\n" n n
3745 | FileIn _ | FileOut _ -> ()
3747 pr " serial = guestfs__send_sync (g, GUESTFS_PROC_%s,\n"
3748 (String.uppercase shortname);
3749 pr " (xdrproc_t) xdr_%s_args, (char *) &args);\n"
3752 pr " if (serial == -1) {\n";
3753 pr " guestfs_end_busy (g);\n";
3754 pr " return %s;\n" error_code;
3758 (* Send any additional files (FileIn) requested. *)
3759 let need_read_reply_label = ref false in
3766 pr " r = guestfs__send_file_sync (g, %s);\n" n;
3767 pr " if (r == -1) {\n";
3768 pr " guestfs_end_busy (g);\n";
3769 pr " return %s;\n" error_code;
3771 pr " if (r == -2) /* daemon cancelled */\n";
3772 pr " goto read_reply;\n";
3773 need_read_reply_label := true;
3779 (* Wait for the reply from the remote end. *)
3780 if !need_read_reply_label then pr " read_reply:\n";
3781 pr " guestfs__switch_to_receiving (g);\n";
3782 pr " ctx.cb_sequence = 0;\n";
3783 pr " guestfs_set_reply_callback (g, %s_reply_cb, &ctx);\n" shortname;
3784 pr " (void) ml->main_loop_run (ml, g);\n";
3785 pr " guestfs_set_reply_callback (g, NULL, NULL);\n";
3786 pr " if (ctx.cb_sequence != 1) {\n";
3787 pr " error (g, \"%%s reply failed, see earlier error messages\", \"%s\");\n" name;
3788 pr " guestfs_end_busy (g);\n";
3789 pr " return %s;\n" error_code;
3793 pr " if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_%s, serial) == -1) {\n"
3794 (String.uppercase shortname);
3795 pr " guestfs_end_busy (g);\n";
3796 pr " return %s;\n" error_code;
3800 pr " if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {\n";
3801 pr " error (g, \"%%s\", ctx.err.error_message);\n";
3802 pr " free (ctx.err.error_message);\n";
3803 pr " guestfs_end_busy (g);\n";
3804 pr " return %s;\n" error_code;
3808 (* Expecting to receive further files (FileOut)? *)
3812 pr " if (guestfs__receive_file_sync (g, %s) == -1) {\n" n;
3813 pr " guestfs_end_busy (g);\n";
3814 pr " return %s;\n" error_code;
3820 pr " guestfs_end_busy (g);\n";
3822 (match fst style with
3823 | RErr -> pr " return 0;\n"
3824 | RInt n | RInt64 n | RBool n ->
3825 pr " return ctx.ret.%s;\n" n
3827 failwithf "RConstString cannot be returned from a daemon function"
3829 pr " return ctx.ret.%s; /* caller will free */\n" n
3830 | RStringList n | RHashtable n ->
3831 pr " /* caller will free this, but we need to add a NULL entry */\n";
3832 pr " ctx.ret.%s.%s_val =\n" n n;
3833 pr " safe_realloc (g, ctx.ret.%s.%s_val,\n" n n;
3834 pr " sizeof (char *) * (ctx.ret.%s.%s_len + 1));\n"
3836 pr " ctx.ret.%s.%s_val[ctx.ret.%s.%s_len] = NULL;\n" n n n n;
3837 pr " return ctx.ret.%s.%s_val;\n" n n
3839 pr " /* caller with free this */\n";
3840 pr " return safe_memdup (g, &ctx.ret, sizeof (ctx.ret));\n"
3841 | RPVList n | RVGList n | RLVList n
3842 | RStat n | RStatVFS n ->
3843 pr " /* caller will free this */\n";
3844 pr " return safe_memdup (g, &ctx.ret.%s, sizeof (ctx.ret.%s));\n" n n
3850 (* Generate daemon/actions.h. *)
3851 and generate_daemon_actions_h () =
3852 generate_header CStyle GPLv2;
3854 pr "#include \"../src/guestfs_protocol.h\"\n";
3858 fun (name, style, _, _, _, _, _) ->
3860 ~single_line:true ~newline:true ~in_daemon:true ~prefix:"do_"
3864 (* Generate the server-side stubs. *)
3865 and generate_daemon_actions () =
3866 generate_header CStyle GPLv2;
3868 pr "#include <config.h>\n";
3870 pr "#include <stdio.h>\n";
3871 pr "#include <stdlib.h>\n";
3872 pr "#include <string.h>\n";
3873 pr "#include <inttypes.h>\n";
3874 pr "#include <ctype.h>\n";
3875 pr "#include <rpc/types.h>\n";
3876 pr "#include <rpc/xdr.h>\n";
3878 pr "#include \"daemon.h\"\n";
3879 pr "#include \"../src/guestfs_protocol.h\"\n";
3880 pr "#include \"actions.h\"\n";
3884 fun (name, style, _, _, _, _, _) ->
3885 (* Generate server-side stubs. *)
3886 pr "static void %s_stub (XDR *xdr_in)\n" name;
3889 match fst style with
3890 | RErr | RInt _ -> pr " int r;\n"; "-1"
3891 | RInt64 _ -> pr " int64_t r;\n"; "-1"
3892 | RBool _ -> pr " int r;\n"; "-1"
3894 failwithf "RConstString cannot be returned from a daemon function"
3895 | RString _ -> pr " char *r;\n"; "NULL"
3896 | RStringList _ | RHashtable _ -> pr " char **r;\n"; "NULL"
3897 | RIntBool _ -> pr " guestfs_%s_ret *r;\n" name; "NULL"
3898 | RPVList _ -> pr " guestfs_lvm_int_pv_list *r;\n"; "NULL"
3899 | RVGList _ -> pr " guestfs_lvm_int_vg_list *r;\n"; "NULL"
3900 | RLVList _ -> pr " guestfs_lvm_int_lv_list *r;\n"; "NULL"
3901 | RStat _ -> pr " guestfs_int_stat *r;\n"; "NULL"
3902 | RStatVFS _ -> pr " guestfs_int_statvfs *r;\n"; "NULL" in
3904 (match snd style with
3907 pr " struct guestfs_%s_args args;\n" name;
3910 (* Note we allow the string to be writable, in order to
3911 * allow device name translation. This is safe because
3912 * we can modify the string (passed from RPC).
3915 | OptString n -> pr " char *%s;\n" n
3916 | StringList n -> pr " char **%s;\n" n
3917 | Bool n -> pr " int %s;\n" n
3918 | Int n -> pr " int %s;\n" n
3919 | FileIn _ | FileOut _ -> ()
3924 (match snd style with
3927 pr " memset (&args, 0, sizeof args);\n";
3929 pr " if (!xdr_guestfs_%s_args (xdr_in, &args)) {\n" name;
3930 pr " reply_with_error (\"%%s: daemon failed to decode procedure arguments\", \"%s\");\n" name;
3935 | String n -> pr " %s = args.%s;\n" n n
3936 | OptString n -> pr " %s = args.%s ? *args.%s : NULL;\n" n n n
3938 pr " %s = realloc (args.%s.%s_val,\n" n n n;
3939 pr " sizeof (char *) * (args.%s.%s_len+1));\n" n n;
3940 pr " if (%s == NULL) {\n" n;
3941 pr " reply_with_perror (\"realloc\");\n";
3944 pr " %s[args.%s.%s_len] = NULL;\n" n n n;
3945 pr " args.%s.%s_val = %s;\n" n n n;
3946 | Bool n -> pr " %s = args.%s;\n" n n
3947 | Int n -> pr " %s = args.%s;\n" n n
3948 | FileIn _ | FileOut _ -> ()
3953 (* Don't want to call the impl with any FileIn or FileOut
3954 * parameters, since these go "outside" the RPC protocol.
3957 List.filter (function FileIn _ | FileOut _ -> false | _ -> true)
3959 pr " r = do_%s " name;
3960 generate_call_args argsnofile;
3963 pr " if (r == %s)\n" error_code;
3964 pr " /* do_%s has already called reply_with_error */\n" name;
3968 (* If there are any FileOut parameters, then the impl must
3969 * send its own reply.
3972 List.exists (function FileOut _ -> true | _ -> false) (snd style) in
3974 pr " /* do_%s has already sent a reply */\n" name
3976 match fst style with
3977 | RErr -> pr " reply (NULL, NULL);\n"
3978 | RInt n | RInt64 n | RBool n ->
3979 pr " struct guestfs_%s_ret ret;\n" name;
3980 pr " ret.%s = r;\n" n;
3981 pr " reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
3984 failwithf "RConstString cannot be returned from a daemon function"
3986 pr " struct guestfs_%s_ret ret;\n" name;
3987 pr " ret.%s = r;\n" n;
3988 pr " reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
3991 | RStringList n | RHashtable n ->
3992 pr " struct guestfs_%s_ret ret;\n" name;
3993 pr " ret.%s.%s_len = count_strings (r);\n" n n;
3994 pr " ret.%s.%s_val = r;\n" n n;
3995 pr " reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
3997 pr " free_strings (r);\n"
3999 pr " reply ((xdrproc_t) xdr_guestfs_%s_ret, (char *) r);\n"
4001 pr " xdr_free ((xdrproc_t) xdr_guestfs_%s_ret, (char *) r);\n" name
4002 | RPVList n | RVGList n | RLVList n
4003 | RStat n | RStatVFS n ->
4004 pr " struct guestfs_%s_ret ret;\n" name;
4005 pr " ret.%s = *r;\n" n;
4006 pr " reply ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
4008 pr " xdr_free ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
4012 (* Free the args. *)
4013 (match snd style with
4018 pr " xdr_free ((xdrproc_t) xdr_guestfs_%s_args, (char *) &args);\n"
4025 (* Dispatch function. *)
4026 pr "void dispatch_incoming_message (XDR *xdr_in)\n";
4028 pr " switch (proc_nr) {\n";
4031 fun (name, style, _, _, _, _, _) ->
4032 pr " case GUESTFS_PROC_%s:\n" (String.uppercase name);
4033 pr " %s_stub (xdr_in);\n" name;
4038 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";
4043 (* LVM columns and tokenization functions. *)
4044 (* XXX This generates crap code. We should rethink how we
4050 pr "static const char *lvm_%s_cols = \"%s\";\n"
4051 typ (String.concat "," (List.map fst cols));
4054 pr "static int lvm_tokenize_%s (char *str, struct guestfs_lvm_int_%s *r)\n" typ typ;
4056 pr " char *tok, *p, *next;\n";
4060 pr " fprintf (stderr, \"%%s: <<%%s>>\\n\", __func__, str);\n";
4063 pr " if (!str) {\n";
4064 pr " fprintf (stderr, \"%%s: failed: passed a NULL string\\n\", __func__);\n";
4067 pr " if (!*str || isspace (*str)) {\n";
4068 pr " fprintf (stderr, \"%%s: failed: passed a empty string or one beginning with whitespace\\n\", __func__);\n";
4073 fun (name, coltype) ->
4074 pr " if (!tok) {\n";
4075 pr " fprintf (stderr, \"%%s: failed: string finished early, around token %%s\\n\", __func__, \"%s\");\n" name;
4078 pr " p = strchrnul (tok, ',');\n";
4079 pr " if (*p) next = p+1; else next = NULL;\n";
4080 pr " *p = '\\0';\n";
4083 pr " r->%s = strdup (tok);\n" name;
4084 pr " if (r->%s == NULL) {\n" name;
4085 pr " perror (\"strdup\");\n";
4089 pr " for (i = j = 0; i < 32; ++j) {\n";
4090 pr " if (tok[j] == '\\0') {\n";
4091 pr " fprintf (stderr, \"%%s: failed to parse UUID from '%%s'\\n\", __func__, tok);\n";
4093 pr " } else if (tok[j] != '-')\n";
4094 pr " r->%s[i++] = tok[j];\n" name;
4097 pr " if (sscanf (tok, \"%%\"SCNu64, &r->%s) != 1) {\n" name;
4098 pr " fprintf (stderr, \"%%s: failed to parse size '%%s' from token %%s\\n\", __func__, tok, \"%s\");\n" name;
4102 pr " if (sscanf (tok, \"%%\"SCNi64, &r->%s) != 1) {\n" name;
4103 pr " fprintf (stderr, \"%%s: failed to parse int '%%s' from token %%s\\n\", __func__, tok, \"%s\");\n" name;
4107 pr " if (tok[0] == '\\0')\n";
4108 pr " r->%s = -1;\n" name;
4109 pr " else if (sscanf (tok, \"%%f\", &r->%s) != 1) {\n" name;
4110 pr " fprintf (stderr, \"%%s: failed to parse float '%%s' from token %%s\\n\", __func__, tok, \"%s\");\n" name;
4114 pr " tok = next;\n";
4117 pr " if (tok != NULL) {\n";
4118 pr " fprintf (stderr, \"%%s: failed: extra tokens at end of string\\n\", __func__);\n";
4125 pr "guestfs_lvm_int_%s_list *\n" typ;
4126 pr "parse_command_line_%ss (void)\n" typ;
4128 pr " char *out, *err;\n";
4129 pr " char *p, *pend;\n";
4131 pr " guestfs_lvm_int_%s_list *ret;\n" typ;
4132 pr " void *newp;\n";
4134 pr " ret = malloc (sizeof *ret);\n";
4135 pr " if (!ret) {\n";
4136 pr " reply_with_perror (\"malloc\");\n";
4137 pr " return NULL;\n";
4140 pr " ret->guestfs_lvm_int_%s_list_len = 0;\n" typ;
4141 pr " ret->guestfs_lvm_int_%s_list_val = NULL;\n" typ;
4143 pr " r = command (&out, &err,\n";
4144 pr " \"/sbin/lvm\", \"%ss\",\n" typ;
4145 pr " \"-o\", lvm_%s_cols, \"--unbuffered\", \"--noheadings\",\n" typ;
4146 pr " \"--nosuffix\", \"--separator\", \",\", \"--units\", \"b\", NULL);\n";
4147 pr " if (r == -1) {\n";
4148 pr " reply_with_error (\"%%s\", err);\n";
4149 pr " free (out);\n";
4150 pr " free (err);\n";
4151 pr " free (ret);\n";
4152 pr " return NULL;\n";
4155 pr " free (err);\n";
4157 pr " /* Tokenize each line of the output. */\n";
4160 pr " while (p) {\n";
4161 pr " pend = strchr (p, '\\n'); /* Get the next line of output. */\n";
4162 pr " if (pend) {\n";
4163 pr " *pend = '\\0';\n";
4167 pr " while (*p && isspace (*p)) /* Skip any leading whitespace. */\n";
4170 pr " if (!*p) { /* Empty line? Skip it. */\n";
4175 pr " /* Allocate some space to store this next entry. */\n";
4176 pr " newp = realloc (ret->guestfs_lvm_int_%s_list_val,\n" typ;
4177 pr " sizeof (guestfs_lvm_int_%s) * (i+1));\n" typ;
4178 pr " if (newp == NULL) {\n";
4179 pr " reply_with_perror (\"realloc\");\n";
4180 pr " free (ret->guestfs_lvm_int_%s_list_val);\n" typ;
4181 pr " free (ret);\n";
4182 pr " free (out);\n";
4183 pr " return NULL;\n";
4185 pr " ret->guestfs_lvm_int_%s_list_val = newp;\n" typ;
4187 pr " /* Tokenize the next entry. */\n";
4188 pr " r = lvm_tokenize_%s (p, &ret->guestfs_lvm_int_%s_list_val[i]);\n" typ typ;
4189 pr " if (r == -1) {\n";
4190 pr " reply_with_error (\"failed to parse output of '%ss' command\");\n" typ;
4191 pr " free (ret->guestfs_lvm_int_%s_list_val);\n" typ;
4192 pr " free (ret);\n";
4193 pr " free (out);\n";
4194 pr " return NULL;\n";
4201 pr " ret->guestfs_lvm_int_%s_list_len = i;\n" typ;
4203 pr " free (out);\n";
4204 pr " return ret;\n";
4207 ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols]
4209 (* Generate the tests. *)
4210 and generate_tests () =
4211 generate_header CStyle GPLv2;
4218 #include <sys/types.h>
4221 #include \"guestfs.h\"
4223 static guestfs_h *g;
4224 static int suppress_error = 0;
4226 static void print_error (guestfs_h *g, void *data, const char *msg)
4228 if (!suppress_error)
4229 fprintf (stderr, \"%%s\\n\", msg);
4232 static void print_strings (char * const * const argv)
4236 for (argc = 0; argv[argc] != NULL; ++argc)
4237 printf (\"\\t%%s\\n\", argv[argc]);
4241 static void print_table (char * const * const argv)
4245 for (i = 0; argv[i] != NULL; i += 2)
4246 printf (\"%%s: %%s\\n\", argv[i], argv[i+1]);
4250 static void no_test_warnings (void)
4256 | name, _, _, _, [], _, _ ->
4257 pr " fprintf (stderr, \"warning: \\\"guestfs_%s\\\" has no tests\\n\");\n" name
4258 | name, _, _, _, tests, _, _ -> ()
4264 (* Generate the actual tests. Note that we generate the tests
4265 * in reverse order, deliberately, so that (in general) the
4266 * newest tests run first. This makes it quicker and easier to
4271 fun (name, _, _, _, tests, _, _) ->
4272 mapi (generate_one_test name) tests
4273 ) (List.rev all_functions) in
4274 let test_names = List.concat test_names in
4275 let nr_tests = List.length test_names in
4278 int main (int argc, char *argv[])
4282 const char *filename;
4284 int nr_tests, test_num = 0;
4286 setbuf (stdout, NULL);
4288 no_test_warnings ();
4290 g = guestfs_create ();
4292 printf (\"guestfs_create FAILED\\n\");
4296 guestfs_set_error_handler (g, print_error, NULL);
4298 guestfs_set_path (g, \"../appliance\");
4300 filename = \"test1.img\";
4301 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
4306 if (lseek (fd, %d, SEEK_SET) == -1) {
4312 if (write (fd, &c, 1) == -1) {
4318 if (close (fd) == -1) {
4323 if (guestfs_add_drive (g, filename) == -1) {
4324 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
4328 filename = \"test2.img\";
4329 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
4334 if (lseek (fd, %d, SEEK_SET) == -1) {
4340 if (write (fd, &c, 1) == -1) {
4346 if (close (fd) == -1) {
4351 if (guestfs_add_drive (g, filename) == -1) {
4352 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
4356 filename = \"test3.img\";
4357 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
4362 if (lseek (fd, %d, SEEK_SET) == -1) {
4368 if (write (fd, &c, 1) == -1) {
4374 if (close (fd) == -1) {
4379 if (guestfs_add_drive (g, filename) == -1) {
4380 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
4384 if (guestfs_add_drive_ro (g, \"../images/test.sqsh\") == -1) {
4385 printf (\"guestfs_add_drive_ro ../images/test.sqsh FAILED\\n\");
4389 if (guestfs_launch (g) == -1) {
4390 printf (\"guestfs_launch FAILED\\n\");
4394 /* Set a timeout in case qemu hangs during launch (RHBZ#505329). */
4397 if (guestfs_wait_ready (g) == -1) {
4398 printf (\"guestfs_wait_ready FAILED\\n\");
4402 /* Cancel previous alarm. */
4407 " (500 * 1024 * 1024) (50 * 1024 * 1024) (10 * 1024 * 1024) nr_tests;
4411 pr " test_num++;\n";
4412 pr " printf (\"%%3d/%%3d %s\\n\", test_num, nr_tests);\n" test_name;
4413 pr " if (%s () == -1) {\n" test_name;
4414 pr " printf (\"%s FAILED\\n\");\n" test_name;
4420 pr " guestfs_close (g);\n";
4421 pr " unlink (\"test1.img\");\n";
4422 pr " unlink (\"test2.img\");\n";
4423 pr " unlink (\"test3.img\");\n";
4426 pr " if (failed > 0) {\n";
4427 pr " printf (\"***** %%d / %%d tests FAILED *****\\n\", failed, nr_tests);\n";
4435 and generate_one_test name i (init, prereq, test) =
4436 let test_name = sprintf "test_%s_%d" name i in
4439 static int %s_skip (void)
4443 str = getenv (\"TEST_ONLY\");
4445 return strstr (str, \"%s\") == NULL;
4446 str = getenv (\"SKIP_%s\");
4447 if (str && strcmp (str, \"1\") == 0) return 1;
4448 str = getenv (\"SKIP_TEST_%s\");
4449 if (str && strcmp (str, \"1\") == 0) return 1;
4453 " test_name name (String.uppercase test_name) (String.uppercase name);
4456 | Disabled | Always -> ()
4457 | If code | Unless code ->
4458 pr "static int %s_prereq (void)\n" test_name;
4466 static int %s (void)
4469 printf (\" %%s skipped (reason: environment variable set)\\n\", \"%s\");
4473 " test_name test_name test_name;
4477 pr " printf (\" %%s skipped (reason: test disabled in generator)\\n\", \"%s\");\n" test_name
4479 pr " if (! %s_prereq ()) {\n" test_name;
4480 pr " printf (\" %%s skipped (reason: test prerequisite)\\n\", \"%s\");\n" test_name;
4484 generate_one_test_body name i test_name init test;
4486 pr " if (%s_prereq ()) {\n" test_name;
4487 pr " printf (\" %%s skipped (reason: test prerequisite)\\n\", \"%s\");\n" test_name;
4491 generate_one_test_body name i test_name init test;
4493 generate_one_test_body name i test_name init test
4501 and generate_one_test_body name i test_name init test =
4505 pr " /* InitNone|InitEmpty for %s */\n" test_name;
4506 List.iter (generate_test_command_call test_name)
4507 [["blockdev_setrw"; "/dev/sda"];
4511 pr " /* InitBasicFS for %s: create ext2 on /dev/sda1 */\n" test_name;
4512 List.iter (generate_test_command_call test_name)
4513 [["blockdev_setrw"; "/dev/sda"];
4516 ["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
4517 ["mkfs"; "ext2"; "/dev/sda1"];
4518 ["mount"; "/dev/sda1"; "/"]]
4519 | InitBasicFSonLVM ->
4520 pr " /* InitBasicFSonLVM for %s: create ext2 on /dev/VG/LV */\n"
4522 List.iter (generate_test_command_call test_name)
4523 [["blockdev_setrw"; "/dev/sda"];
4526 ["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ","];
4527 ["pvcreate"; "/dev/sda1"];
4528 ["vgcreate"; "VG"; "/dev/sda1"];
4529 ["lvcreate"; "LV"; "VG"; "8"];
4530 ["mkfs"; "ext2"; "/dev/VG/LV"];
4531 ["mount"; "/dev/VG/LV"; "/"]]
4534 let get_seq_last = function
4536 failwithf "%s: you cannot use [] (empty list) when expecting a command"
4539 let seq = List.rev seq in
4540 List.rev (List.tl seq), List.hd seq
4545 pr " /* TestRun for %s (%d) */\n" name i;
4546 List.iter (generate_test_command_call test_name) seq
4547 | TestOutput (seq, expected) ->
4548 pr " /* TestOutput for %s (%d) */\n" name i;
4549 pr " char expected[] = \"%s\";\n" (c_quote expected);
4550 let seq, last = get_seq_last seq in
4552 pr " if (strcmp (r, expected) != 0) {\n";
4553 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name;
4557 List.iter (generate_test_command_call test_name) seq;
4558 generate_test_command_call ~test test_name last
4559 | TestOutputList (seq, expected) ->
4560 pr " /* TestOutputList for %s (%d) */\n" name i;
4561 let seq, last = get_seq_last seq in
4565 pr " if (!r[%d]) {\n" i;
4566 pr " fprintf (stderr, \"%s: short list returned from command\\n\");\n" test_name;
4567 pr " print_strings (r);\n";
4571 pr " char expected[] = \"%s\";\n" (c_quote str);
4572 pr " if (strcmp (r[%d], expected) != 0) {\n" i;
4573 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r[%d]);\n" test_name i;
4578 pr " if (r[%d] != NULL) {\n" (List.length expected);
4579 pr " fprintf (stderr, \"%s: extra elements returned from command\\n\");\n"
4581 pr " print_strings (r);\n";
4585 List.iter (generate_test_command_call test_name) seq;
4586 generate_test_command_call ~test test_name last
4587 | TestOutputListOfDevices (seq, expected) ->
4588 pr " /* TestOutputListOfDevices for %s (%d) */\n" name i;
4589 let seq, last = get_seq_last seq in
4593 pr " if (!r[%d]) {\n" i;
4594 pr " fprintf (stderr, \"%s: short list returned from command\\n\");\n" test_name;
4595 pr " print_strings (r);\n";
4599 pr " char expected[] = \"%s\";\n" (c_quote str);
4600 pr " r[%d][5] = 's';\n" i;
4601 pr " if (strcmp (r[%d], expected) != 0) {\n" i;
4602 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r[%d]);\n" test_name i;
4607 pr " if (r[%d] != NULL) {\n" (List.length expected);
4608 pr " fprintf (stderr, \"%s: extra elements returned from command\\n\");\n"
4610 pr " print_strings (r);\n";
4614 List.iter (generate_test_command_call test_name) seq;
4615 generate_test_command_call ~test test_name last
4616 | TestOutputInt (seq, expected) ->
4617 pr " /* TestOutputInt for %s (%d) */\n" name i;
4618 let seq, last = get_seq_last seq in
4620 pr " if (r != %d) {\n" expected;
4621 pr " fprintf (stderr, \"%s: expected %d but got %%d\\n\","
4627 List.iter (generate_test_command_call test_name) seq;
4628 generate_test_command_call ~test test_name last
4629 | TestOutputTrue seq ->
4630 pr " /* TestOutputTrue for %s (%d) */\n" name i;
4631 let seq, last = get_seq_last seq in
4634 pr " fprintf (stderr, \"%s: expected true, got false\\n\");\n"
4639 List.iter (generate_test_command_call test_name) seq;
4640 generate_test_command_call ~test test_name last
4641 | TestOutputFalse seq ->
4642 pr " /* TestOutputFalse for %s (%d) */\n" name i;
4643 let seq, last = get_seq_last seq in
4646 pr " fprintf (stderr, \"%s: expected false, got true\\n\");\n"
4651 List.iter (generate_test_command_call test_name) seq;
4652 generate_test_command_call ~test test_name last
4653 | TestOutputLength (seq, expected) ->
4654 pr " /* TestOutputLength for %s (%d) */\n" name i;
4655 let seq, last = get_seq_last seq in
4658 pr " for (j = 0; j < %d; ++j)\n" expected;
4659 pr " if (r[j] == NULL) {\n";
4660 pr " fprintf (stderr, \"%s: short list returned\\n\");\n"
4662 pr " print_strings (r);\n";
4665 pr " if (r[j] != NULL) {\n";
4666 pr " fprintf (stderr, \"%s: long list returned\\n\");\n"
4668 pr " print_strings (r);\n";
4672 List.iter (generate_test_command_call test_name) seq;
4673 generate_test_command_call ~test test_name last
4674 | TestOutputStruct (seq, checks) ->
4675 pr " /* TestOutputStruct for %s (%d) */\n" name i;
4676 let seq, last = get_seq_last seq in
4680 | CompareWithInt (field, expected) ->
4681 pr " if (r->%s != %d) {\n" field expected;
4682 pr " fprintf (stderr, \"%s: %s was %%d, expected %d\\n\",\n"
4683 test_name field expected;
4684 pr " (int) r->%s);\n" field;
4687 | CompareWithString (field, expected) ->
4688 pr " if (strcmp (r->%s, \"%s\") != 0) {\n" field expected;
4689 pr " fprintf (stderr, \"%s: %s was \"%%s\", expected \"%s\"\\n\",\n"
4690 test_name field expected;
4691 pr " r->%s);\n" field;
4694 | CompareFieldsIntEq (field1, field2) ->
4695 pr " if (r->%s != r->%s) {\n" field1 field2;
4696 pr " fprintf (stderr, \"%s: %s (%%d) <> %s (%%d)\\n\",\n"
4697 test_name field1 field2;
4698 pr " (int) r->%s, (int) r->%s);\n" field1 field2;
4701 | CompareFieldsStrEq (field1, field2) ->
4702 pr " if (strcmp (r->%s, r->%s) != 0) {\n" field1 field2;
4703 pr " fprintf (stderr, \"%s: %s (\"%%s\") <> %s (\"%%s\")\\n\",\n"
4704 test_name field1 field2;
4705 pr " r->%s, r->%s);\n" field1 field2;
4710 List.iter (generate_test_command_call test_name) seq;
4711 generate_test_command_call ~test test_name last
4712 | TestLastFail seq ->
4713 pr " /* TestLastFail for %s (%d) */\n" name i;
4714 let seq, last = get_seq_last seq in
4715 List.iter (generate_test_command_call test_name) seq;
4716 generate_test_command_call test_name ~expect_error:true last
4718 (* Generate the code to run a command, leaving the result in 'r'.
4719 * If you expect to get an error then you should set expect_error:true.
4721 and generate_test_command_call ?(expect_error = false) ?test test_name cmd =
4723 | [] -> assert false
4725 (* Look up the command to find out what args/ret it has. *)
4728 let _, style, _, _, _, _, _ =
4729 List.find (fun (n, _, _, _, _, _, _) -> n = name) all_functions in
4732 failwithf "%s: in test, command %s was not found" test_name name in
4734 if List.length (snd style) <> List.length args then
4735 failwithf "%s: in test, wrong number of args given to %s"
4742 | OptString n, "NULL" -> ()
4744 | OptString n, arg ->
4745 pr " char %s[] = \"%s\";\n" n (c_quote arg);
4748 | FileIn _, _ | FileOut _, _ -> ()
4749 | StringList n, arg ->
4750 let strs = string_split " " arg in
4753 pr " char %s_%d[] = \"%s\";\n" n i (c_quote str);
4755 pr " char *%s[] = {\n" n;
4757 fun i _ -> pr " %s_%d,\n" n i
4761 ) (List.combine (snd style) args);
4764 match fst style with
4765 | RErr | RInt _ | RBool _ -> pr " int r;\n"; "-1"
4766 | RInt64 _ -> pr " int64_t r;\n"; "-1"
4767 | RConstString _ -> pr " const char *r;\n"; "NULL"
4768 | RString _ -> pr " char *r;\n"; "NULL"
4769 | RStringList _ | RHashtable _ ->
4774 pr " struct guestfs_int_bool *r;\n"; "NULL"
4776 pr " struct guestfs_lvm_pv_list *r;\n"; "NULL"
4778 pr " struct guestfs_lvm_vg_list *r;\n"; "NULL"
4780 pr " struct guestfs_lvm_lv_list *r;\n"; "NULL"
4782 pr " struct guestfs_stat *r;\n"; "NULL"
4784 pr " struct guestfs_statvfs *r;\n"; "NULL" in
4786 pr " suppress_error = %d;\n" (if expect_error then 1 else 0);
4787 pr " r = guestfs_%s (g" name;
4789 (* Generate the parameters. *)
4792 | OptString _, "NULL" -> pr ", NULL"
4796 | FileIn _, arg | FileOut _, arg ->
4797 pr ", \"%s\"" (c_quote arg)
4798 | StringList n, _ ->
4802 try int_of_string arg
4803 with Failure "int_of_string" ->
4804 failwithf "%s: expecting an int, but got '%s'" test_name arg in
4807 let b = bool_of_string arg in pr ", %d" (if b then 1 else 0)
4808 ) (List.combine (snd style) args);
4811 if not expect_error then
4812 pr " if (r == %s)\n" error_code
4814 pr " if (r != %s)\n" error_code;
4817 (* Insert the test code. *)
4823 (match fst style with
4824 | RErr | RInt _ | RInt64 _ | RBool _ | RConstString _ -> ()
4825 | RString _ -> pr " free (r);\n"
4826 | RStringList _ | RHashtable _ ->
4827 pr " for (i = 0; r[i] != NULL; ++i)\n";
4828 pr " free (r[i]);\n";
4831 pr " guestfs_free_int_bool (r);\n"
4833 pr " guestfs_free_lvm_pv_list (r);\n"
4835 pr " guestfs_free_lvm_vg_list (r);\n"
4837 pr " guestfs_free_lvm_lv_list (r);\n"
4838 | RStat _ | RStatVFS _ ->
4845 let str = replace_str str "\r" "\\r" in
4846 let str = replace_str str "\n" "\\n" in
4847 let str = replace_str str "\t" "\\t" in
4848 let str = replace_str str "\000" "\\0" in
4851 (* Generate a lot of different functions for guestfish. *)
4852 and generate_fish_cmds () =
4853 generate_header CStyle GPLv2;
4857 fun (_, _, _, flags, _, _, _) -> not (List.mem NotInFish flags)
4859 let all_functions_sorted =
4861 fun (_, _, _, flags, _, _, _) -> not (List.mem NotInFish flags)
4862 ) all_functions_sorted in
4864 pr "#include <stdio.h>\n";
4865 pr "#include <stdlib.h>\n";
4866 pr "#include <string.h>\n";
4867 pr "#include <inttypes.h>\n";
4869 pr "#include <guestfs.h>\n";
4870 pr "#include \"fish.h\"\n";
4873 (* list_commands function, which implements guestfish -h *)
4874 pr "void list_commands (void)\n";
4876 pr " printf (\" %%-16s %%s\\n\", \"Command\", \"Description\");\n";
4877 pr " list_builtin_commands ();\n";
4879 fun (name, _, _, flags, _, shortdesc, _) ->
4880 let name = replace_char name '_' '-' in
4881 pr " printf (\"%%-20s %%s\\n\", \"%s\", \"%s\");\n"
4883 ) all_functions_sorted;
4884 pr " printf (\" Use -h <cmd> / help <cmd> to show detailed help for a command.\\n\");\n";
4888 (* display_command function, which implements guestfish -h cmd *)
4889 pr "void display_command (const char *cmd)\n";
4892 fun (name, style, _, flags, _, shortdesc, longdesc) ->
4893 let name2 = replace_char name '_' '-' in
4895 try find_map (function FishAlias n -> Some n | _ -> None) flags
4896 with Not_found -> name in
4897 let longdesc = replace_str longdesc "C<guestfs_" "C<" in
4899 match snd style with
4903 name2 (String.concat "> <" (List.map name_of_argt args)) in
4906 if List.mem ProtocolLimitWarning flags then
4907 ("\n\n" ^ protocol_limit_warning)
4910 (* For DangerWillRobinson commands, we should probably have
4911 * guestfish prompt before allowing you to use them (especially
4912 * in interactive mode). XXX
4916 if List.mem DangerWillRobinson flags then
4917 ("\n\n" ^ danger_will_robinson)
4920 let describe_alias =
4921 if name <> alias then
4922 sprintf "\n\nYou can use '%s' as an alias for this command." alias
4926 pr "strcasecmp (cmd, \"%s\") == 0" name;
4927 if name <> name2 then
4928 pr " || strcasecmp (cmd, \"%s\") == 0" name2;
4929 if name <> alias then
4930 pr " || strcasecmp (cmd, \"%s\") == 0" alias;
4932 pr " pod2text (\"%s - %s\", %S);\n"
4934 (" " ^ synopsis ^ "\n\n" ^ longdesc ^ warnings ^ describe_alias);
4937 pr " display_builtin_command (cmd);\n";
4941 (* print_{pv,vg,lv}_list functions *)
4945 pr "static void print_%s (struct guestfs_lvm_%s *%s)\n" typ typ typ;
4952 pr " printf (\"%s: %%s\\n\", %s->%s);\n" name typ name
4954 pr " printf (\"%s: \");\n" name;
4955 pr " for (i = 0; i < 32; ++i)\n";
4956 pr " printf (\"%%c\", %s->%s[i]);\n" typ name;
4957 pr " printf (\"\\n\");\n"
4959 pr " printf (\"%s: %%\" PRIu64 \"\\n\", %s->%s);\n" name typ name
4961 pr " printf (\"%s: %%\" PRIi64 \"\\n\", %s->%s);\n" name typ name
4962 | name, `OptPercent ->
4963 pr " if (%s->%s >= 0) printf (\"%s: %%g %%%%\\n\", %s->%s);\n"
4964 typ name name typ name;
4965 pr " else printf (\"%s: \\n\");\n" name
4969 pr "static void print_%s_list (struct guestfs_lvm_%s_list *%ss)\n"
4974 pr " for (i = 0; i < %ss->len; ++i)\n" typ;
4975 pr " print_%s (&%ss->val[i]);\n" typ typ;
4978 ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols];
4980 (* print_{stat,statvfs} functions *)
4984 pr "static void print_%s (struct guestfs_%s *%s)\n" typ typ typ;
4989 pr " printf (\"%s: %%\" PRIi64 \"\\n\", %s->%s);\n" name typ name
4993 ) ["stat", stat_cols; "statvfs", statvfs_cols];
4995 (* run_<action> actions *)
4997 fun (name, style, _, flags, _, _, _) ->
4998 pr "static int run_%s (const char *cmd, int argc, char *argv[])\n" name;
5000 (match fst style with
5003 | RBool _ -> pr " int r;\n"
5004 | RInt64 _ -> pr " int64_t r;\n"
5005 | RConstString _ -> pr " const char *r;\n"
5006 | RString _ -> pr " char *r;\n"
5007 | RStringList _ | RHashtable _ -> pr " char **r;\n"
5008 | RIntBool _ -> pr " struct guestfs_int_bool *r;\n"
5009 | RPVList _ -> pr " struct guestfs_lvm_pv_list *r;\n"
5010 | RVGList _ -> pr " struct guestfs_lvm_vg_list *r;\n"
5011 | RLVList _ -> pr " struct guestfs_lvm_lv_list *r;\n"
5012 | RStat _ -> pr " struct guestfs_stat *r;\n"
5013 | RStatVFS _ -> pr " struct guestfs_statvfs *r;\n"
5020 | FileOut n -> pr " const char *%s;\n" n
5021 | StringList n -> pr " char **%s;\n" n
5022 | Bool n -> pr " int %s;\n" n
5023 | Int n -> pr " int %s;\n" n
5026 (* Check and convert parameters. *)
5027 let argc_expected = List.length (snd style) in
5028 pr " if (argc != %d) {\n" argc_expected;
5029 pr " fprintf (stderr, \"%%s should have %d parameter(s)\\n\", cmd);\n"
5031 pr " fprintf (stderr, \"type 'help %%s' for help on %%s\\n\", cmd, cmd);\n";
5037 | String name -> pr " %s = argv[%d];\n" name i
5039 pr " %s = strcmp (argv[%d], \"\") != 0 ? argv[%d] : NULL;\n"
5042 pr " %s = strcmp (argv[%d], \"-\") != 0 ? argv[%d] : \"/dev/stdin\";\n"
5045 pr " %s = strcmp (argv[%d], \"-\") != 0 ? argv[%d] : \"/dev/stdout\";\n"
5047 | StringList name ->
5048 pr " %s = parse_string_list (argv[%d]);\n" name i
5050 pr " %s = is_true (argv[%d]) ? 1 : 0;\n" name i
5052 pr " %s = atoi (argv[%d]);\n" name i
5055 (* Call C API function. *)
5057 try find_map (function FishAction n -> Some n | _ -> None) flags
5058 with Not_found -> sprintf "guestfs_%s" name in
5060 generate_call_args ~handle:"g" (snd style);
5063 (* Check return value for errors and display command results. *)
5064 (match fst style with
5065 | RErr -> pr " return r;\n"
5067 pr " if (r == -1) return -1;\n";
5068 pr " printf (\"%%d\\n\", r);\n";
5071 pr " if (r == -1) return -1;\n";
5072 pr " printf (\"%%\" PRIi64 \"\\n\", r);\n";
5075 pr " if (r == -1) return -1;\n";
5076 pr " if (r) printf (\"true\\n\"); else printf (\"false\\n\");\n";
5079 pr " if (r == NULL) return -1;\n";
5080 pr " printf (\"%%s\\n\", r);\n";
5083 pr " if (r == NULL) return -1;\n";
5084 pr " printf (\"%%s\\n\", r);\n";
5088 pr " if (r == NULL) return -1;\n";
5089 pr " print_strings (r);\n";
5090 pr " free_strings (r);\n";
5093 pr " if (r == NULL) return -1;\n";
5094 pr " printf (\"%%d, %%s\\n\", r->i,\n";
5095 pr " r->b ? \"true\" : \"false\");\n";
5096 pr " guestfs_free_int_bool (r);\n";
5099 pr " if (r == NULL) return -1;\n";
5100 pr " print_pv_list (r);\n";
5101 pr " guestfs_free_lvm_pv_list (r);\n";
5104 pr " if (r == NULL) return -1;\n";
5105 pr " print_vg_list (r);\n";
5106 pr " guestfs_free_lvm_vg_list (r);\n";
5109 pr " if (r == NULL) return -1;\n";
5110 pr " print_lv_list (r);\n";
5111 pr " guestfs_free_lvm_lv_list (r);\n";
5114 pr " if (r == NULL) return -1;\n";
5115 pr " print_stat (r);\n";
5119 pr " if (r == NULL) return -1;\n";
5120 pr " print_statvfs (r);\n";
5124 pr " if (r == NULL) return -1;\n";
5125 pr " print_table (r);\n";
5126 pr " free_strings (r);\n";
5133 (* run_action function *)
5134 pr "int run_action (const char *cmd, int argc, char *argv[])\n";
5137 fun (name, _, _, flags, _, _, _) ->
5138 let name2 = replace_char name '_' '-' in
5140 try find_map (function FishAlias n -> Some n | _ -> None) flags
5141 with Not_found -> name in
5143 pr "strcasecmp (cmd, \"%s\") == 0" name;
5144 if name <> name2 then
5145 pr " || strcasecmp (cmd, \"%s\") == 0" name2;
5146 if name <> alias then
5147 pr " || strcasecmp (cmd, \"%s\") == 0" alias;
5149 pr " return run_%s (cmd, argc, argv);\n" name;
5153 pr " fprintf (stderr, \"%%s: unknown command\\n\", cmd);\n";
5160 (* Readline completion for guestfish. *)
5161 and generate_fish_completion () =
5162 generate_header CStyle GPLv2;
5166 fun (_, _, _, flags, _, _, _) -> not (List.mem NotInFish flags)
5176 #ifdef HAVE_LIBREADLINE
5177 #include <readline/readline.h>
5182 #ifdef HAVE_LIBREADLINE
5184 static const char *const commands[] = {
5185 BUILTIN_COMMANDS_FOR_COMPLETION,
5188 (* Get the commands, including the aliases. They don't need to be
5189 * sorted - the generator() function just does a dumb linear search.
5193 fun (name, _, _, flags, _, _, _) ->
5194 let name2 = replace_char name '_' '-' in
5196 try find_map (function FishAlias n -> Some n | _ -> None) flags
5197 with Not_found -> name in
5199 if name <> alias then [name2; alias] else [name2]
5201 let commands = List.flatten commands in
5203 List.iter (pr " \"%s\",\n") commands;
5209 generator (const char *text, int state)
5211 static int index, len;
5216 len = strlen (text);
5219 rl_attempted_completion_over = 1;
5221 while ((name = commands[index]) != NULL) {
5223 if (strncasecmp (name, text, len) == 0)
5224 return strdup (name);
5230 #endif /* HAVE_LIBREADLINE */
5232 char **do_completion (const char *text, int start, int end)
5234 char **matches = NULL;
5236 #ifdef HAVE_LIBREADLINE
5237 rl_completion_append_character = ' ';
5240 matches = rl_completion_matches (text, generator);
5241 else if (complete_dest_paths)
5242 matches = rl_completion_matches (text, complete_dest_paths_generator);
5249 (* Generate the POD documentation for guestfish. *)
5250 and generate_fish_actions_pod () =
5251 let all_functions_sorted =
5253 fun (_, _, _, flags, _, _, _) ->
5254 not (List.mem NotInFish flags || List.mem NotInDocs flags)
5255 ) all_functions_sorted in
5257 let rex = Str.regexp "C<guestfs_\\([^>]+\\)>" in
5260 fun (name, style, _, flags, _, _, longdesc) ->
5262 Str.global_substitute rex (
5265 try Str.matched_group 1 s
5267 failwithf "error substituting C<guestfs_...> in longdesc of function %s" name in
5268 "C<" ^ replace_char sub '_' '-' ^ ">"
5270 let name = replace_char name '_' '-' in
5272 try find_map (function FishAlias n -> Some n | _ -> None) flags
5273 with Not_found -> name in
5275 pr "=head2 %s" name;
5276 if name <> alias then
5283 | String n -> pr " %s" n
5284 | OptString n -> pr " %s" n
5285 | StringList n -> pr " '%s ...'" n
5286 | Bool _ -> pr " true|false"
5287 | Int n -> pr " %s" n
5288 | FileIn n | FileOut n -> pr " (%s|-)" n
5292 pr "%s\n\n" longdesc;
5294 if List.exists (function FileIn _ | FileOut _ -> true
5295 | _ -> false) (snd style) then
5296 pr "Use C<-> instead of a filename to read/write from stdin/stdout.\n\n";
5298 if List.mem ProtocolLimitWarning flags then
5299 pr "%s\n\n" protocol_limit_warning;
5301 if List.mem DangerWillRobinson flags then
5302 pr "%s\n\n" danger_will_robinson
5303 ) all_functions_sorted
5305 (* Generate a C function prototype. *)
5306 and generate_prototype ?(extern = true) ?(static = false) ?(semicolon = true)
5307 ?(single_line = false) ?(newline = false) ?(in_daemon = false)
5309 ?handle name style =
5310 if extern then pr "extern ";
5311 if static then pr "static ";
5312 (match fst style with
5314 | RInt _ -> pr "int "
5315 | RInt64 _ -> pr "int64_t "
5316 | RBool _ -> pr "int "
5317 | RConstString _ -> pr "const char *"
5318 | RString _ -> pr "char *"
5319 | RStringList _ | RHashtable _ -> pr "char **"
5321 if not in_daemon then pr "struct guestfs_int_bool *"
5322 else pr "guestfs_%s_ret *" name
5324 if not in_daemon then pr "struct guestfs_lvm_pv_list *"
5325 else pr "guestfs_lvm_int_pv_list *"
5327 if not in_daemon then pr "struct guestfs_lvm_vg_list *"
5328 else pr "guestfs_lvm_int_vg_list *"
5330 if not in_daemon then pr "struct guestfs_lvm_lv_list *"
5331 else pr "guestfs_lvm_int_lv_list *"
5333 if not in_daemon then pr "struct guestfs_stat *"
5334 else pr "guestfs_int_stat *"
5336 if not in_daemon then pr "struct guestfs_statvfs *"
5337 else pr "guestfs_int_statvfs *"
5339 pr "%s%s (" prefix name;
5340 if handle = None && List.length (snd style) = 0 then
5343 let comma = ref false in
5346 | Some handle -> pr "guestfs_h *%s" handle; comma := true
5350 if single_line then pr ", " else pr ",\n\t\t"
5359 if not in_daemon then pr "const char *%s" n
5360 else pr "char *%s" n
5363 if not in_daemon then pr "char * const* const %s" n
5364 else pr "char **%s" n
5365 | Bool n -> next (); pr "int %s" n
5366 | Int n -> next (); pr "int %s" n
5369 if not in_daemon then (next (); pr "const char *%s" n)
5373 if semicolon then pr ";";
5374 if newline then pr "\n"
5376 (* Generate C call arguments, eg "(handle, foo, bar)" *)
5377 and generate_call_args ?handle args =
5379 let comma = ref false in
5382 | Some handle -> pr "%s" handle; comma := true
5386 if !comma then pr ", ";
5388 pr "%s" (name_of_argt arg)
5392 (* Generate the OCaml bindings interface. *)
5393 and generate_ocaml_mli () =
5394 generate_header OCamlStyle LGPLv2;
5397 (** For API documentation you should refer to the C API
5398 in the guestfs(3) manual page. The OCaml API uses almost
5399 exactly the same calls. *)
5402 (** A [guestfs_h] handle. *)
5404 exception Error of string
5405 (** This exception is raised when there is an error. *)
5407 val create : unit -> t
5409 val close : t -> unit
5410 (** Handles are closed by the garbage collector when they become
5411 unreferenced, but callers can also call this in order to
5412 provide predictable cleanup. *)
5415 generate_ocaml_lvm_structure_decls ();
5417 generate_ocaml_stat_structure_decls ();
5421 fun (name, style, _, _, _, shortdesc, _) ->
5422 generate_ocaml_prototype name style;
5423 pr "(** %s *)\n" shortdesc;
5427 (* Generate the OCaml bindings implementation. *)
5428 and generate_ocaml_ml () =
5429 generate_header OCamlStyle LGPLv2;
5433 exception Error of string
5434 external create : unit -> t = \"ocaml_guestfs_create\"
5435 external close : t -> unit = \"ocaml_guestfs_close\"
5438 Callback.register_exception \"ocaml_guestfs_error\" (Error \"\")
5442 generate_ocaml_lvm_structure_decls ();
5444 generate_ocaml_stat_structure_decls ();
5448 fun (name, style, _, _, _, shortdesc, _) ->
5449 generate_ocaml_prototype ~is_external:true name style;
5452 (* Generate the OCaml bindings C implementation. *)
5453 and generate_ocaml_c () =
5454 generate_header CStyle LGPLv2;
5461 #include <caml/config.h>
5462 #include <caml/alloc.h>
5463 #include <caml/callback.h>
5464 #include <caml/fail.h>
5465 #include <caml/memory.h>
5466 #include <caml/mlvalues.h>
5467 #include <caml/signals.h>
5469 #include <guestfs.h>
5471 #include \"guestfs_c.h\"
5473 /* Copy a hashtable of string pairs into an assoc-list. We return
5474 * the list in reverse order, but hashtables aren't supposed to be
5477 static CAMLprim value
5478 copy_table (char * const * argv)
5481 CAMLlocal5 (rv, pairv, kv, vv, cons);
5485 for (i = 0; argv[i] != NULL; i += 2) {
5486 kv = caml_copy_string (argv[i]);
5487 vv = caml_copy_string (argv[i+1]);
5488 pairv = caml_alloc (2, 0);
5489 Store_field (pairv, 0, kv);
5490 Store_field (pairv, 1, vv);
5491 cons = caml_alloc (2, 0);
5492 Store_field (cons, 1, rv);
5494 Store_field (cons, 0, pairv);
5502 (* LVM struct copy functions. *)
5505 let has_optpercent_col =
5506 List.exists (function (_, `OptPercent) -> true | _ -> false) cols in
5508 pr "static CAMLprim value\n";
5509 pr "copy_lvm_%s (const struct guestfs_lvm_%s *%s)\n" typ typ typ;
5511 pr " CAMLparam0 ();\n";
5512 if has_optpercent_col then
5513 pr " CAMLlocal3 (rv, v, v2);\n"
5515 pr " CAMLlocal2 (rv, v);\n";
5517 pr " rv = caml_alloc (%d, 0);\n" (List.length cols);
5522 pr " v = caml_copy_string (%s->%s);\n" typ name
5524 pr " v = caml_alloc_string (32);\n";
5525 pr " memcpy (String_val (v), %s->%s, 32);\n" typ name
5528 pr " v = caml_copy_int64 (%s->%s);\n" typ name
5529 | name, `OptPercent ->
5530 pr " if (%s->%s >= 0) { /* Some %s */\n" typ name name;
5531 pr " v2 = caml_copy_double (%s->%s);\n" typ name;
5532 pr " v = caml_alloc (1, 0);\n";
5533 pr " Store_field (v, 0, v2);\n";
5534 pr " } else /* None */\n";
5535 pr " v = Val_int (0);\n";
5537 pr " Store_field (rv, %d, v);\n" i
5539 pr " CAMLreturn (rv);\n";
5543 pr "static CAMLprim value\n";
5544 pr "copy_lvm_%s_list (const struct guestfs_lvm_%s_list *%ss)\n"
5547 pr " CAMLparam0 ();\n";
5548 pr " CAMLlocal2 (rv, v);\n";
5551 pr " if (%ss->len == 0)\n" typ;
5552 pr " CAMLreturn (Atom (0));\n";
5554 pr " rv = caml_alloc (%ss->len, 0);\n" typ;
5555 pr " for (i = 0; i < %ss->len; ++i) {\n" typ;
5556 pr " v = copy_lvm_%s (&%ss->val[i]);\n" typ typ;
5557 pr " caml_modify (&Field (rv, i), v);\n";
5559 pr " CAMLreturn (rv);\n";
5563 ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols];
5565 (* Stat copy functions. *)
5568 pr "static CAMLprim value\n";
5569 pr "copy_%s (const struct guestfs_%s *%s)\n" typ typ typ;
5571 pr " CAMLparam0 ();\n";
5572 pr " CAMLlocal2 (rv, v);\n";
5574 pr " rv = caml_alloc (%d, 0);\n" (List.length cols);
5579 pr " v = caml_copy_int64 (%s->%s);\n" typ name
5581 pr " Store_field (rv, %d, v);\n" i
5583 pr " CAMLreturn (rv);\n";
5586 ) ["stat", stat_cols; "statvfs", statvfs_cols];
5590 fun (name, style, _, _, _, _, _) ->
5592 "gv" :: List.map (fun arg -> name_of_argt arg ^ "v") (snd style) in
5594 pr "CAMLprim value\n";
5595 pr "ocaml_guestfs_%s (value %s" name (List.hd params);
5596 List.iter (pr ", value %s") (List.tl params);
5601 | [p1; p2; p3; p4; p5] ->
5602 pr " CAMLparam5 (%s);\n" (String.concat ", " params)
5603 | p1 :: p2 :: p3 :: p4 :: p5 :: rest ->
5604 pr " CAMLparam5 (%s);\n" (String.concat ", " [p1; p2; p3; p4; p5]);
5605 pr " CAMLxparam%d (%s);\n"
5606 (List.length rest) (String.concat ", " rest)
5608 pr " CAMLparam%d (%s);\n" (List.length ps) (String.concat ", " ps)
5610 pr " CAMLlocal1 (rv);\n";
5613 pr " guestfs_h *g = Guestfs_val (gv);\n";
5614 pr " if (g == NULL)\n";
5615 pr " caml_failwith (\"%s: used handle after closing it\");\n" name;
5623 pr " const char *%s = String_val (%sv);\n" n n
5625 pr " const char *%s =\n" n;
5626 pr " %sv != Val_int (0) ? String_val (Field (%sv, 0)) : NULL;\n"
5629 pr " char **%s = ocaml_guestfs_strings_val (g, %sv);\n" n n
5631 pr " int %s = Bool_val (%sv);\n" n n
5633 pr " int %s = Int_val (%sv);\n" n n
5636 match fst style with
5637 | RErr -> pr " int r;\n"; "-1"
5638 | RInt _ -> pr " int r;\n"; "-1"
5639 | RInt64 _ -> pr " int64_t r;\n"; "-1"
5640 | RBool _ -> pr " int r;\n"; "-1"
5641 | RConstString _ -> pr " const char *r;\n"; "NULL"
5642 | RString _ -> pr " char *r;\n"; "NULL"
5648 pr " struct guestfs_int_bool *r;\n"; "NULL"
5650 pr " struct guestfs_lvm_pv_list *r;\n"; "NULL"
5652 pr " struct guestfs_lvm_vg_list *r;\n"; "NULL"
5654 pr " struct guestfs_lvm_lv_list *r;\n"; "NULL"
5656 pr " struct guestfs_stat *r;\n"; "NULL"
5658 pr " struct guestfs_statvfs *r;\n"; "NULL"
5665 pr " caml_enter_blocking_section ();\n";
5666 pr " r = guestfs_%s " name;
5667 generate_call_args ~handle:"g" (snd style);
5669 pr " caml_leave_blocking_section ();\n";
5674 pr " ocaml_guestfs_free_strings (%s);\n" n;
5675 | String _ | OptString _ | Bool _ | Int _ | FileIn _ | FileOut _ -> ()
5678 pr " if (r == %s)\n" error_code;
5679 pr " ocaml_guestfs_raise_error (g, \"%s\");\n" name;
5682 (match fst style with
5683 | RErr -> pr " rv = Val_unit;\n"
5684 | RInt _ -> pr " rv = Val_int (r);\n"
5686 pr " rv = caml_copy_int64 (r);\n"
5687 | RBool _ -> pr " rv = Val_bool (r);\n"
5688 | RConstString _ -> pr " rv = caml_copy_string (r);\n"
5690 pr " rv = caml_copy_string (r);\n";
5693 pr " rv = caml_copy_string_array ((const char **) r);\n";
5694 pr " for (i = 0; r[i] != NULL; ++i) free (r[i]);\n";
5697 pr " rv = caml_alloc (2, 0);\n";
5698 pr " Store_field (rv, 0, Val_int (r->i));\n";
5699 pr " Store_field (rv, 1, Val_bool (r->b));\n";
5700 pr " guestfs_free_int_bool (r);\n";
5702 pr " rv = copy_lvm_pv_list (r);\n";
5703 pr " guestfs_free_lvm_pv_list (r);\n";
5705 pr " rv = copy_lvm_vg_list (r);\n";
5706 pr " guestfs_free_lvm_vg_list (r);\n";
5708 pr " rv = copy_lvm_lv_list (r);\n";
5709 pr " guestfs_free_lvm_lv_list (r);\n";
5711 pr " rv = copy_stat (r);\n";
5714 pr " rv = copy_statvfs (r);\n";
5717 pr " rv = copy_table (r);\n";
5718 pr " for (i = 0; r[i] != NULL; ++i) free (r[i]);\n";
5722 pr " CAMLreturn (rv);\n";
5726 if List.length params > 5 then (
5727 pr "CAMLprim value\n";
5728 pr "ocaml_guestfs_%s_byte (value *argv, int argn)\n" name;
5730 pr " return ocaml_guestfs_%s (argv[0]" name;
5731 iteri (fun i _ -> pr ", argv[%d]" i) (List.tl params);
5738 and generate_ocaml_lvm_structure_decls () =
5741 pr "type lvm_%s = {\n" typ;
5744 | name, `String -> pr " %s : string;\n" name
5745 | name, `UUID -> pr " %s : string;\n" name
5746 | name, `Bytes -> pr " %s : int64;\n" name
5747 | name, `Int -> pr " %s : int64;\n" name
5748 | name, `OptPercent -> pr " %s : float option;\n" name
5752 ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols]
5754 and generate_ocaml_stat_structure_decls () =
5757 pr "type %s = {\n" typ;
5760 | name, `Int -> pr " %s : int64;\n" name
5764 ) ["stat", stat_cols; "statvfs", statvfs_cols]
5766 and generate_ocaml_prototype ?(is_external = false) name style =
5767 if is_external then pr "external " else pr "val ";
5768 pr "%s : t -> " name;
5771 | String _ | FileIn _ | FileOut _ -> pr "string -> "
5772 | OptString _ -> pr "string option -> "
5773 | StringList _ -> pr "string array -> "
5774 | Bool _ -> pr "bool -> "
5775 | Int _ -> pr "int -> "
5777 (match fst style with
5778 | RErr -> pr "unit" (* all errors are turned into exceptions *)
5779 | RInt _ -> pr "int"
5780 | RInt64 _ -> pr "int64"
5781 | RBool _ -> pr "bool"
5782 | RConstString _ -> pr "string"
5783 | RString _ -> pr "string"
5784 | RStringList _ -> pr "string array"
5785 | RIntBool _ -> pr "int * bool"
5786 | RPVList _ -> pr "lvm_pv array"
5787 | RVGList _ -> pr "lvm_vg array"
5788 | RLVList _ -> pr "lvm_lv array"
5789 | RStat _ -> pr "stat"
5790 | RStatVFS _ -> pr "statvfs"
5791 | RHashtable _ -> pr "(string * string) list"
5793 if is_external then (
5795 if List.length (snd style) + 1 > 5 then
5796 pr "\"ocaml_guestfs_%s_byte\" " name;
5797 pr "\"ocaml_guestfs_%s\"" name
5801 (* Generate Perl xs code, a sort of crazy variation of C with macros. *)
5802 and generate_perl_xs () =
5803 generate_header CStyle LGPLv2;
5806 #include \"EXTERN.h\"
5810 #include <guestfs.h>
5813 #define PRId64 \"lld\"
5817 my_newSVll(long long val) {
5818 #ifdef USE_64_BIT_ALL
5819 return newSViv(val);
5823 len = snprintf(buf, 100, \"%%\" PRId64, val);
5824 return newSVpv(buf, len);
5829 #define PRIu64 \"llu\"
5833 my_newSVull(unsigned long long val) {
5834 #ifdef USE_64_BIT_ALL
5835 return newSVuv(val);
5839 len = snprintf(buf, 100, \"%%\" PRIu64, val);
5840 return newSVpv(buf, len);
5844 /* http://www.perlmonks.org/?node_id=680842 */
5846 XS_unpack_charPtrPtr (SV *arg) {
5851 if (!arg || !SvOK (arg) || !SvROK (arg) || SvTYPE (SvRV (arg)) != SVt_PVAV)
5852 croak (\"array reference expected\");
5854 av = (AV *)SvRV (arg);
5855 ret = malloc ((av_len (av) + 1 + 1) * sizeof (char *));
5857 croak (\"malloc failed\");
5859 for (i = 0; i <= av_len (av); i++) {
5860 SV **elem = av_fetch (av, i, 0);
5862 if (!elem || !*elem)
5863 croak (\"missing element in list\");
5865 ret[i] = SvPV_nolen (*elem);
5873 MODULE = Sys::Guestfs PACKAGE = Sys::Guestfs
5880 RETVAL = guestfs_create ();
5882 croak (\"could not create guestfs handle\");
5883 guestfs_set_error_handler (RETVAL, NULL, NULL);
5896 fun (name, style, _, _, _, _, _) ->
5897 (match fst style with
5898 | RErr -> pr "void\n"
5899 | RInt _ -> pr "SV *\n"
5900 | RInt64 _ -> pr "SV *\n"
5901 | RBool _ -> pr "SV *\n"
5902 | RConstString _ -> pr "SV *\n"
5903 | RString _ -> pr "SV *\n"
5906 | RPVList _ | RVGList _ | RLVList _
5907 | RStat _ | RStatVFS _
5909 pr "void\n" (* all lists returned implictly on the stack *)
5911 (* Call and arguments. *)
5913 generate_call_args ~handle:"g" (snd style);
5915 pr " guestfs_h *g;\n";
5919 | String n | FileIn n | FileOut n -> pr " char *%s;\n" n
5921 (* http://www.perlmonks.org/?node_id=554277
5922 * Note that the implicit handle argument means we have
5923 * to add 1 to the ST(x) operator.
5925 pr " char *%s = SvOK(ST(%d)) ? SvPV_nolen(ST(%d)) : NULL;\n" n (i+1) (i+1)
5926 | StringList n -> pr " char **%s;\n" n
5927 | Bool n -> pr " int %s;\n" n
5928 | Int n -> pr " int %s;\n" n
5931 let do_cleanups () =
5934 | String _ | OptString _ | Bool _ | Int _
5935 | FileIn _ | FileOut _ -> ()
5936 | StringList n -> pr " free (%s);\n" n
5941 (match fst style with
5946 pr " r = guestfs_%s " name;
5947 generate_call_args ~handle:"g" (snd style);
5950 pr " if (r == -1)\n";
5951 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
5957 pr " %s = guestfs_%s " n name;
5958 generate_call_args ~handle:"g" (snd style);
5961 pr " if (%s == -1)\n" n;
5962 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
5963 pr " RETVAL = newSViv (%s);\n" n;
5968 pr " int64_t %s;\n" n;
5970 pr " %s = guestfs_%s " n name;
5971 generate_call_args ~handle:"g" (snd style);
5974 pr " if (%s == -1)\n" n;
5975 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
5976 pr " RETVAL = my_newSVll (%s);\n" n;
5981 pr " const char *%s;\n" n;
5983 pr " %s = guestfs_%s " n name;
5984 generate_call_args ~handle:"g" (snd style);
5987 pr " if (%s == NULL)\n" n;
5988 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
5989 pr " RETVAL = newSVpv (%s, 0);\n" n;
5994 pr " char *%s;\n" n;
5996 pr " %s = guestfs_%s " n name;
5997 generate_call_args ~handle:"g" (snd style);
6000 pr " if (%s == NULL)\n" n;
6001 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
6002 pr " RETVAL = newSVpv (%s, 0);\n" n;
6003 pr " free (%s);\n" n;
6006 | RStringList n | RHashtable n ->
6008 pr " char **%s;\n" n;
6011 pr " %s = guestfs_%s " n name;
6012 generate_call_args ~handle:"g" (snd style);
6015 pr " if (%s == NULL)\n" n;
6016 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
6017 pr " for (n = 0; %s[n] != NULL; ++n) /**/;\n" n;
6018 pr " EXTEND (SP, n);\n";
6019 pr " for (i = 0; i < n; ++i) {\n";
6020 pr " PUSHs (sv_2mortal (newSVpv (%s[i], 0)));\n" n;
6021 pr " free (%s[i]);\n" n;
6023 pr " free (%s);\n" n;
6026 pr " struct guestfs_int_bool *r;\n";
6028 pr " r = guestfs_%s " name;
6029 generate_call_args ~handle:"g" (snd style);
6032 pr " if (r == NULL)\n";
6033 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
6034 pr " EXTEND (SP, 2);\n";
6035 pr " PUSHs (sv_2mortal (newSViv (r->i)));\n";
6036 pr " PUSHs (sv_2mortal (newSViv (r->b)));\n";
6037 pr " guestfs_free_int_bool (r);\n";
6039 generate_perl_lvm_code "pv" pv_cols name style n do_cleanups
6041 generate_perl_lvm_code "vg" vg_cols name style n do_cleanups
6043 generate_perl_lvm_code "lv" lv_cols name style n do_cleanups
6045 generate_perl_stat_code "stat" stat_cols name style n do_cleanups
6047 generate_perl_stat_code
6048 "statvfs" statvfs_cols name style n do_cleanups
6054 and generate_perl_lvm_code typ cols name style n do_cleanups =
6056 pr " struct guestfs_lvm_%s_list *%s;\n" typ n;
6060 pr " %s = guestfs_%s " n name;
6061 generate_call_args ~handle:"g" (snd style);
6064 pr " if (%s == NULL)\n" n;
6065 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
6066 pr " EXTEND (SP, %s->len);\n" n;
6067 pr " for (i = 0; i < %s->len; ++i) {\n" n;
6068 pr " hv = newHV ();\n";
6072 pr " (void) hv_store (hv, \"%s\", %d, newSVpv (%s->val[i].%s, 0), 0);\n"
6073 name (String.length name) n name
6075 pr " (void) hv_store (hv, \"%s\", %d, newSVpv (%s->val[i].%s, 32), 0);\n"
6076 name (String.length name) n name
6078 pr " (void) hv_store (hv, \"%s\", %d, my_newSVull (%s->val[i].%s), 0);\n"
6079 name (String.length name) n name
6081 pr " (void) hv_store (hv, \"%s\", %d, my_newSVll (%s->val[i].%s), 0);\n"
6082 name (String.length name) n name
6083 | name, `OptPercent ->
6084 pr " (void) hv_store (hv, \"%s\", %d, newSVnv (%s->val[i].%s), 0);\n"
6085 name (String.length name) n name
6087 pr " PUSHs (sv_2mortal ((SV *) hv));\n";
6089 pr " guestfs_free_lvm_%s_list (%s);\n" typ n
6091 and generate_perl_stat_code typ cols name style n do_cleanups =
6093 pr " struct guestfs_%s *%s;\n" typ n;
6095 pr " %s = guestfs_%s " n name;
6096 generate_call_args ~handle:"g" (snd style);
6099 pr " if (%s == NULL)\n" n;
6100 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
6101 pr " EXTEND (SP, %d);\n" (List.length cols);
6105 pr " PUSHs (sv_2mortal (my_newSVll (%s->%s)));\n" n name
6107 pr " free (%s);\n" n
6109 (* Generate Sys/Guestfs.pm. *)
6110 and generate_perl_pm () =
6111 generate_header HashStyle LGPLv2;
6118 Sys::Guestfs - Perl bindings for libguestfs
6124 my $h = Sys::Guestfs->new ();
6125 $h->add_drive ('guest.img');
6128 $h->mount ('/dev/sda1', '/');
6129 $h->touch ('/hello');
6134 The C<Sys::Guestfs> module provides a Perl XS binding to the
6135 libguestfs API for examining and modifying virtual machine
6138 Amongst the things this is good for: making batch configuration
6139 changes to guests, getting disk used/free statistics (see also:
6140 virt-df), migrating between virtualization systems (see also:
6141 virt-p2v), performing partial backups, performing partial guest
6142 clones, cloning guests and changing registry/UUID/hostname info, and
6145 Libguestfs uses Linux kernel and qemu code, and can access any type of
6146 guest filesystem that Linux and qemu can, including but not limited
6147 to: ext2/3/4, btrfs, FAT and NTFS, LVM, many different disk partition
6148 schemes, qcow, qcow2, vmdk.
6150 Libguestfs provides ways to enumerate guest storage (eg. partitions,
6151 LVs, what filesystem is in each LV, etc.). It can also run commands
6152 in the context of the guest. Also you can access filesystems over FTP.
6156 All errors turn into calls to C<croak> (see L<Carp(3)>).
6164 package Sys::Guestfs;
6170 XSLoader::load ('Sys::Guestfs');
6172 =item $h = Sys::Guestfs->new ();
6174 Create a new guestfs handle.
6180 my $class = ref ($proto) || $proto;
6182 my $self = Sys::Guestfs::_create ();
6183 bless $self, $class;
6189 (* Actions. We only need to print documentation for these as
6190 * they are pulled in from the XS code automatically.
6193 fun (name, style, _, flags, _, _, longdesc) ->
6194 if not (List.mem NotInDocs flags) then (
6195 let longdesc = replace_str longdesc "C<guestfs_" "C<$h-E<gt>" in
6197 generate_perl_prototype name style;
6199 pr "%s\n\n" longdesc;
6200 if List.mem ProtocolLimitWarning flags then
6201 pr "%s\n\n" protocol_limit_warning;
6202 if List.mem DangerWillRobinson flags then
6203 pr "%s\n\n" danger_will_robinson
6205 ) all_functions_sorted;
6217 Copyright (C) 2009 Red Hat Inc.
6221 Please see the file COPYING.LIB for the full license.
6225 L<guestfs(3)>, L<guestfish(1)>.
6230 and generate_perl_prototype name style =
6231 (match fst style with
6237 | RString n -> pr "$%s = " n
6238 | RIntBool (n, m) -> pr "($%s, $%s) = " n m
6242 | RLVList n -> pr "@%s = " n
6245 | RHashtable n -> pr "%%%s = " n
6248 let comma = ref false in
6251 if !comma then pr ", ";
6254 | String n | OptString n | Bool n | Int n | FileIn n | FileOut n ->
6261 (* Generate Python C module. *)
6262 and generate_python_c () =
6263 generate_header CStyle LGPLv2;
6272 #include \"guestfs.h\"
6280 get_handle (PyObject *obj)
6283 assert (obj != Py_None);
6284 return ((Pyguestfs_Object *) obj)->g;
6288 put_handle (guestfs_h *g)
6292 PyCObject_FromVoidPtrAndDesc ((void *) g, (char *) \"guestfs_h\", NULL);
6295 /* This list should be freed (but not the strings) after use. */
6296 static const char **
6297 get_string_list (PyObject *obj)
6304 if (!PyList_Check (obj)) {
6305 PyErr_SetString (PyExc_RuntimeError, \"expecting a list parameter\");
6309 len = PyList_Size (obj);
6310 r = malloc (sizeof (char *) * (len+1));
6312 PyErr_SetString (PyExc_RuntimeError, \"get_string_list: out of memory\");
6316 for (i = 0; i < len; ++i)
6317 r[i] = PyString_AsString (PyList_GetItem (obj, i));
6324 put_string_list (char * const * const argv)
6329 for (argc = 0; argv[argc] != NULL; ++argc)
6332 list = PyList_New (argc);
6333 for (i = 0; i < argc; ++i)
6334 PyList_SetItem (list, i, PyString_FromString (argv[i]));
6340 put_table (char * const * const argv)
6342 PyObject *list, *item;
6345 for (argc = 0; argv[argc] != NULL; ++argc)
6348 list = PyList_New (argc >> 1);
6349 for (i = 0; i < argc; i += 2) {
6350 item = PyTuple_New (2);
6351 PyTuple_SetItem (item, 0, PyString_FromString (argv[i]));
6352 PyTuple_SetItem (item, 1, PyString_FromString (argv[i+1]));
6353 PyList_SetItem (list, i >> 1, item);
6360 free_strings (char **argv)
6364 for (argc = 0; argv[argc] != NULL; ++argc)
6370 py_guestfs_create (PyObject *self, PyObject *args)
6374 g = guestfs_create ();
6376 PyErr_SetString (PyExc_RuntimeError,
6377 \"guestfs.create: failed to allocate handle\");
6380 guestfs_set_error_handler (g, NULL, NULL);
6381 return put_handle (g);
6385 py_guestfs_close (PyObject *self, PyObject *args)
6390 if (!PyArg_ParseTuple (args, (char *) \"O:guestfs_close\", &py_g))
6392 g = get_handle (py_g);
6396 Py_INCREF (Py_None);
6402 (* LVM structures, turned into Python dictionaries. *)
6405 pr "static PyObject *\n";
6406 pr "put_lvm_%s (struct guestfs_lvm_%s *%s)\n" typ typ typ;
6408 pr " PyObject *dict;\n";
6410 pr " dict = PyDict_New ();\n";
6414 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
6415 pr " PyString_FromString (%s->%s));\n"
6418 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
6419 pr " PyString_FromStringAndSize (%s->%s, 32));\n"
6422 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
6423 pr " PyLong_FromUnsignedLongLong (%s->%s));\n"
6426 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
6427 pr " PyLong_FromLongLong (%s->%s));\n"
6429 | name, `OptPercent ->
6430 pr " if (%s->%s >= 0)\n" typ name;
6431 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
6432 pr " PyFloat_FromDouble ((double) %s->%s));\n"
6435 pr " Py_INCREF (Py_None);\n";
6436 pr " PyDict_SetItemString (dict, \"%s\", Py_None);" name;
6439 pr " return dict;\n";
6443 pr "static PyObject *\n";
6444 pr "put_lvm_%s_list (struct guestfs_lvm_%s_list *%ss)\n" typ typ typ;
6446 pr " PyObject *list;\n";
6449 pr " list = PyList_New (%ss->len);\n" typ;
6450 pr " for (i = 0; i < %ss->len; ++i)\n" typ;
6451 pr " PyList_SetItem (list, i, put_lvm_%s (&%ss->val[i]));\n" typ typ;
6452 pr " return list;\n";
6455 ) ["pv", pv_cols; "vg", vg_cols; "lv", lv_cols];
6457 (* Stat structures, turned into Python dictionaries. *)
6460 pr "static PyObject *\n";
6461 pr "put_%s (struct guestfs_%s *%s)\n" typ typ typ;
6463 pr " PyObject *dict;\n";
6465 pr " dict = PyDict_New ();\n";
6469 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
6470 pr " PyLong_FromLongLong (%s->%s));\n"
6473 pr " return dict;\n";
6476 ) ["stat", stat_cols; "statvfs", statvfs_cols];
6478 (* Python wrapper functions. *)
6480 fun (name, style, _, _, _, _, _) ->
6481 pr "static PyObject *\n";
6482 pr "py_guestfs_%s (PyObject *self, PyObject *args)\n" name;
6485 pr " PyObject *py_g;\n";
6486 pr " guestfs_h *g;\n";
6487 pr " PyObject *py_r;\n";
6490 match fst style with
6491 | RErr | RInt _ | RBool _ -> pr " int r;\n"; "-1"
6492 | RInt64 _ -> pr " int64_t r;\n"; "-1"
6493 | RConstString _ -> pr " const char *r;\n"; "NULL"
6494 | RString _ -> pr " char *r;\n"; "NULL"
6495 | RStringList _ | RHashtable _ -> pr " char **r;\n"; "NULL"
6496 | RIntBool _ -> pr " struct guestfs_int_bool *r;\n"; "NULL"
6497 | RPVList n -> pr " struct guestfs_lvm_pv_list *r;\n"; "NULL"
6498 | RVGList n -> pr " struct guestfs_lvm_vg_list *r;\n"; "NULL"
6499 | RLVList n -> pr " struct guestfs_lvm_lv_list *r;\n"; "NULL"
6500 | RStat n -> pr " struct guestfs_stat *r;\n"; "NULL"
6501 | RStatVFS n -> pr " struct guestfs_statvfs *r;\n"; "NULL" in
6505 | String n | FileIn n | FileOut n -> pr " const char *%s;\n" n
6506 | OptString n -> pr " const char *%s;\n" n
6508 pr " PyObject *py_%s;\n" n;
6509 pr " const char **%s;\n" n
6510 | Bool n -> pr " int %s;\n" n
6511 | Int n -> pr " int %s;\n" n
6516 (* Convert the parameters. *)
6517 pr " if (!PyArg_ParseTuple (args, (char *) \"O";
6520 | String _ | FileIn _ | FileOut _ -> pr "s"
6521 | OptString _ -> pr "z"
6522 | StringList _ -> pr "O"
6523 | Bool _ -> pr "i" (* XXX Python has booleans? *)
6526 pr ":guestfs_%s\",\n" name;
6530 | String n | FileIn n | FileOut n -> pr ", &%s" n
6531 | OptString n -> pr ", &%s" n
6532 | StringList n -> pr ", &py_%s" n
6533 | Bool n -> pr ", &%s" n
6534 | Int n -> pr ", &%s" n
6538 pr " return NULL;\n";
6540 pr " g = get_handle (py_g);\n";
6543 | String _ | FileIn _ | FileOut _ | OptString _ | Bool _ | Int _ -> ()
6545 pr " %s = get_string_list (py_%s);\n" n n;
6546 pr " if (!%s) return NULL;\n" n
6551 pr " r = guestfs_%s " name;
6552 generate_call_args ~handle:"g" (snd style);
6557 | String _ | FileIn _ | FileOut _ | OptString _ | Bool _ | Int _ -> ()
6559 pr " free (%s);\n" n
6562 pr " if (r == %s) {\n" error_code;
6563 pr " PyErr_SetString (PyExc_RuntimeError, guestfs_last_error (g));\n";
6564 pr " return NULL;\n";
6568 (match fst style with
6570 pr " Py_INCREF (Py_None);\n";
6571 pr " py_r = Py_None;\n"
6573 | RBool _ -> pr " py_r = PyInt_FromLong ((long) r);\n"
6574 | RInt64 _ -> pr " py_r = PyLong_FromLongLong (r);\n"
6575 | RConstString _ -> pr " py_r = PyString_FromString (r);\n"
6577 pr " py_r = PyString_FromString (r);\n";
6580 pr " py_r = put_string_list (r);\n";
6581 pr " free_strings (r);\n"
6583 pr " py_r = PyTuple_New (2);\n";
6584 pr " PyTuple_SetItem (py_r, 0, PyInt_FromLong ((long) r->i));\n";
6585 pr " PyTuple_SetItem (py_r, 1, PyInt_FromLong ((long) r->b));\n";
6586 pr " guestfs_free_int_bool (r);\n"
6588 pr " py_r = put_lvm_pv_list (r);\n";
6589 pr " guestfs_free_lvm_pv_list (r);\n"
6591 pr " py_r = put_lvm_vg_list (r);\n";
6592 pr " guestfs_free_lvm_vg_list (r);\n"
6594 pr " py_r = put_lvm_lv_list (r);\n";
6595 pr " guestfs_free_lvm_lv_list (r);\n"
6597 pr " py_r = put_stat (r);\n";
6600 pr " py_r = put_statvfs (r);\n";
6603 pr " py_r = put_table (r);\n";
6604 pr " free_strings (r);\n"
6607 pr " return py_r;\n";
6612 (* Table of functions. *)
6613 pr "static PyMethodDef methods[] = {\n";
6614 pr " { (char *) \"create\", py_guestfs_create, METH_VARARGS, NULL },\n";
6615 pr " { (char *) \"close\", py_guestfs_close, METH_VARARGS, NULL },\n";
6617 fun (name, _, _, _, _, _, _) ->
6618 pr " { (char *) \"%s\", py_guestfs_%s, METH_VARARGS, NULL },\n"
6621 pr " { NULL, NULL, 0, NULL }\n";
6625 (* Init function. *)
6628 initlibguestfsmod (void)
6630 static int initialized = 0;
6632 if (initialized) return;
6633 Py_InitModule ((char *) \"libguestfsmod\", methods);
6638 (* Generate Python module. *)
6639 and generate_python_py () =
6640 generate_header HashStyle LGPLv2;
6643 u\"\"\"Python bindings for libguestfs
6646 g = guestfs.GuestFS ()
6647 g.add_drive (\"guest.img\")
6650 parts = g.list_partitions ()
6652 The guestfs module provides a Python binding to the libguestfs API
6653 for examining and modifying virtual machine disk images.
6655 Amongst the things this is good for: making batch configuration
6656 changes to guests, getting disk used/free statistics (see also:
6657 virt-df), migrating between virtualization systems (see also:
6658 virt-p2v), performing partial backups, performing partial guest
6659 clones, cloning guests and changing registry/UUID/hostname info, and
6662 Libguestfs uses Linux kernel and qemu code, and can access any type of
6663 guest filesystem that Linux and qemu can, including but not limited
6664 to: ext2/3/4, btrfs, FAT and NTFS, LVM, many different disk partition
6665 schemes, qcow, qcow2, vmdk.
6667 Libguestfs provides ways to enumerate guest storage (eg. partitions,
6668 LVs, what filesystem is in each LV, etc.). It can also run commands
6669 in the context of the guest. Also you can access filesystems over FTP.
6671 Errors which happen while using the API are turned into Python
6672 RuntimeError exceptions.
6674 To create a guestfs handle you usually have to perform the following
6677 # Create the handle, call add_drive at least once, and possibly
6678 # several times if the guest has multiple block devices:
6679 g = guestfs.GuestFS ()
6680 g.add_drive (\"guest.img\")
6682 # Launch the qemu subprocess and wait for it to become ready:
6686 # Now you can issue commands, for example:
6691 import libguestfsmod
6694 \"\"\"Instances of this class are libguestfs API handles.\"\"\"
6696 def __init__ (self):
6697 \"\"\"Create a new libguestfs handle.\"\"\"
6698 self._o = libguestfsmod.create ()
6701 libguestfsmod.close (self._o)
6706 fun (name, style, _, flags, _, _, longdesc) ->
6708 generate_call_args ~handle:"self" (snd style);
6711 if not (List.mem NotInDocs flags) then (
6712 let doc = replace_str longdesc "C<guestfs_" "C<g." in
6714 match fst style with
6715 | RErr | RInt _ | RInt64 _ | RBool _ | RConstString _
6718 doc ^ "\n\nThis function returns a list of strings."
6720 doc ^ "\n\nThis function returns a tuple (int, bool).\n"
6722 doc ^ "\n\nThis function returns a list of PVs. Each PV is represented as a dictionary."
6724 doc ^ "\n\nThis function returns a list of VGs. Each VG is represented as a dictionary."
6726 doc ^ "\n\nThis function returns a list of LVs. Each LV is represented as a dictionary."
6728 doc ^ "\n\nThis function returns a dictionary, with keys matching the various fields in the stat structure."
6730 doc ^ "\n\nThis function returns a dictionary, with keys matching the various fields in the statvfs structure."
6732 doc ^ "\n\nThis function returns a dictionary." in
6734 if List.mem ProtocolLimitWarning flags then
6735 doc ^ "\n\n" ^ protocol_limit_warning
6738 if List.mem DangerWillRobinson flags then
6739 doc ^ "\n\n" ^ danger_will_robinson
6741 let doc = pod2text ~width:60 name doc in
6742 let doc = List.map (fun line -> replace_str line "\\" "\\\\") doc in
6743 let doc = String.concat "\n " doc in
6744 pr " u\"\"\"%s\"\"\"\n" doc;
6746 pr " return libguestfsmod.%s " name;
6747 generate_call_args ~handle:"self._o" (snd style);
6752 (* Useful if you need the longdesc POD text as plain text. Returns a
6755 * This is the slowest thing about autogeneration.
6757 and pod2text ~width name longdesc =
6758 let filename, chan = Filename.open_temp_file "gen" ".tmp" in
6759 fprintf chan "=head1 %s\n\n%s\n" name longdesc;
6761 let cmd = sprintf "pod2text -w %d %s" width (Filename.quote filename) in
6762 let chan = Unix.open_process_in cmd in
6763 let lines = ref [] in
6765 let line = input_line chan in
6766 if i = 1 then (* discard the first line of output *)
6769 let line = triml line in
6770 lines := line :: !lines;
6773 let lines = try loop 1 with End_of_file -> List.rev !lines in
6774 Unix.unlink filename;
6775 match Unix.close_process_in chan with
6776 | Unix.WEXITED 0 -> lines
6778 failwithf "pod2text: process exited with non-zero status (%d)" i
6779 | Unix.WSIGNALED i | Unix.WSTOPPED i ->
6780 failwithf "pod2text: process signalled or stopped by signal %d" i
6782 (* Generate ruby bindings. *)
6783 and generate_ruby_c () =
6784 generate_header CStyle LGPLv2;
6792 #include \"guestfs.h\"
6794 #include \"extconf.h\"
6796 /* For Ruby < 1.9 */
6798 #define RARRAY_LEN(r) (RARRAY((r))->len)
6801 static VALUE m_guestfs; /* guestfs module */
6802 static VALUE c_guestfs; /* guestfs_h handle */
6803 static VALUE e_Error; /* used for all errors */
6805 static void ruby_guestfs_free (void *p)
6808 guestfs_close ((guestfs_h *) p);
6811 static VALUE ruby_guestfs_create (VALUE m)
6815 g = guestfs_create ();
6817 rb_raise (e_Error, \"failed to create guestfs handle\");
6819 /* Don't print error messages to stderr by default. */
6820 guestfs_set_error_handler (g, NULL, NULL);
6822 /* Wrap it, and make sure the close function is called when the
6825 return Data_Wrap_Struct (c_guestfs, NULL, ruby_guestfs_free, g);
6828 static VALUE ruby_guestfs_close (VALUE gv)
6831 Data_Get_Struct (gv, guestfs_h, g);
6833 ruby_guestfs_free (g);
6834 DATA_PTR (gv) = NULL;
6842 fun (name, style, _, _, _, _, _) ->
6843 pr "static VALUE ruby_guestfs_%s (VALUE gv" name;
6844 List.iter (fun arg -> pr ", VALUE %sv" (name_of_argt arg)) (snd style);
6847 pr " guestfs_h *g;\n";
6848 pr " Data_Get_Struct (gv, guestfs_h, g);\n";
6850 pr " rb_raise (rb_eArgError, \"%%s: used handle after closing it\", \"%s\");\n"
6856 | String n | FileIn n | FileOut n ->
6857 pr " Check_Type (%sv, T_STRING);\n" n;
6858 pr " const char *%s = StringValueCStr (%sv);\n" n n;
6860 pr " rb_raise (rb_eTypeError, \"expected string for parameter %%s of %%s\",\n";
6861 pr " \"%s\", \"%s\");\n" n name
6863 pr " const char *%s = !NIL_P (%sv) ? StringValueCStr (%sv) : NULL;\n" n n n
6865 pr " char **%s;\n" n;
6866 pr " Check_Type (%sv, T_ARRAY);\n" n;
6868 pr " int i, len;\n";
6869 pr " len = RARRAY_LEN (%sv);\n" n;
6870 pr " %s = guestfs_safe_malloc (g, sizeof (char *) * (len+1));\n"
6872 pr " for (i = 0; i < len; ++i) {\n";
6873 pr " VALUE v = rb_ary_entry (%sv, i);\n" n;
6874 pr " %s[i] = StringValueCStr (v);\n" n;
6876 pr " %s[len] = NULL;\n" n;
6879 pr " int %s = RTEST (%sv);\n" n n
6881 pr " int %s = NUM2INT (%sv);\n" n n
6886 match fst style with
6887 | RErr | RInt _ | RBool _ -> pr " int r;\n"; "-1"
6888 | RInt64 _ -> pr " int64_t r;\n"; "-1"
6889 | RConstString _ -> pr " const char *r;\n"; "NULL"
6890 | RString _ -> pr " char *r;\n"; "NULL"
6891 | RStringList _ | RHashtable _ -> pr " char **r;\n"; "NULL"
6892 | RIntBool _ -> pr " struct guestfs_int_bool *r;\n"; "NULL"
6893 | RPVList n -> pr " struct guestfs_lvm_pv_list *r;\n"; "NULL"
6894 | RVGList n -> pr " struct guestfs_lvm_vg_list *r;\n"; "NULL"
6895 | RLVList n -> pr " struct guestfs_lvm_lv_list *r;\n"; "NULL"
6896 | RStat n -> pr " struct guestfs_stat *r;\n"; "NULL"
6897 | RStatVFS n -> pr " struct guestfs_statvfs *r;\n"; "NULL" in
6900 pr " r = guestfs_%s " name;
6901 generate_call_args ~handle:"g" (snd style);
6906 | String _ | FileIn _ | FileOut _ | OptString _ | Bool _ | Int _ -> ()
6908 pr " free (%s);\n" n
6911 pr " if (r == %s)\n" error_code;
6912 pr " rb_raise (e_Error, \"%%s\", guestfs_last_error (g));\n";
6915 (match fst style with
6917 pr " return Qnil;\n"
6918 | RInt _ | RBool _ ->
6919 pr " return INT2NUM (r);\n"
6921 pr " return ULL2NUM (r);\n"
6923 pr " return rb_str_new2 (r);\n";
6925 pr " VALUE rv = rb_str_new2 (r);\n";
6929 pr " int i, len = 0;\n";
6930 pr " for (i = 0; r[i] != NULL; ++i) len++;\n";
6931 pr " VALUE rv = rb_ary_new2 (len);\n";
6932 pr " for (i = 0; r[i] != NULL; ++i) {\n";
6933 pr " rb_ary_push (rv, rb_str_new2 (r[i]));\n";
6934 pr " free (r[i]);\n";
6939 pr " VALUE rv = rb_ary_new2 (2);\n";
6940 pr " rb_ary_push (rv, INT2NUM (r->i));\n";
6941 pr " rb_ary_push (rv, INT2NUM (r->b));\n";
6942 pr " guestfs_free_int_bool (r);\n";
6945 generate_ruby_lvm_code "pv" pv_cols
6947 generate_ruby_lvm_code "vg" vg_cols
6949 generate_ruby_lvm_code "lv" lv_cols
6951 pr " VALUE rv = rb_hash_new ();\n";
6955 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), ULL2NUM (r->%s));\n" name name
6960 pr " VALUE rv = rb_hash_new ();\n";
6964 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), ULL2NUM (r->%s));\n" name name
6969 pr " VALUE rv = rb_hash_new ();\n";
6971 pr " for (i = 0; r[i] != NULL; i+=2) {\n";
6972 pr " rb_hash_aset (rv, rb_str_new2 (r[i]), rb_str_new2 (r[i+1]));\n";
6973 pr " free (r[i]);\n";
6974 pr " free (r[i+1]);\n";
6985 /* Initialize the module. */
6986 void Init__guestfs ()
6988 m_guestfs = rb_define_module (\"Guestfs\");
6989 c_guestfs = rb_define_class_under (m_guestfs, \"Guestfs\", rb_cObject);
6990 e_Error = rb_define_class_under (m_guestfs, \"Error\", rb_eStandardError);
6992 rb_define_module_function (m_guestfs, \"create\", ruby_guestfs_create, 0);
6993 rb_define_method (c_guestfs, \"close\", ruby_guestfs_close, 0);
6996 (* Define the rest of the methods. *)
6998 fun (name, style, _, _, _, _, _) ->
6999 pr " rb_define_method (c_guestfs, \"%s\",\n" name;
7000 pr " ruby_guestfs_%s, %d);\n" name (List.length (snd style))
7005 (* Ruby code to return an LVM struct list. *)
7006 and generate_ruby_lvm_code typ cols =
7007 pr " VALUE rv = rb_ary_new2 (r->len);\n";
7009 pr " for (i = 0; i < r->len; ++i) {\n";
7010 pr " VALUE hv = rb_hash_new ();\n";
7014 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_str_new2 (r->val[i].%s));\n" name name
7016 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_str_new (r->val[i].%s, 32));\n" name name
7019 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), ULL2NUM (r->val[i].%s));\n" name name
7020 | name, `OptPercent ->
7021 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_dbl2big (r->val[i].%s));\n" name name
7023 pr " rb_ary_push (rv, hv);\n";
7025 pr " guestfs_free_lvm_%s_list (r);\n" typ;
7028 (* Generate Java bindings GuestFS.java file. *)
7029 and generate_java_java () =
7030 generate_header CStyle LGPLv2;
7033 package com.redhat.et.libguestfs;
7035 import java.util.HashMap;
7036 import com.redhat.et.libguestfs.LibGuestFSException;
7037 import com.redhat.et.libguestfs.PV;
7038 import com.redhat.et.libguestfs.VG;
7039 import com.redhat.et.libguestfs.LV;
7040 import com.redhat.et.libguestfs.Stat;
7041 import com.redhat.et.libguestfs.StatVFS;
7042 import com.redhat.et.libguestfs.IntBool;
7045 * The GuestFS object is a libguestfs handle.
7049 public class GuestFS {
7050 // Load the native code.
7052 System.loadLibrary (\"guestfs_jni\");
7056 * The native guestfs_h pointer.
7061 * Create a libguestfs handle.
7063 * @throws LibGuestFSException
7065 public GuestFS () throws LibGuestFSException
7069 private native long _create () throws LibGuestFSException;
7072 * Close a libguestfs handle.
7074 * You can also leave handles to be collected by the garbage
7075 * collector, but this method ensures that the resources used
7076 * by the handle are freed up immediately. If you call any
7077 * other methods after closing the handle, you will get an
7080 * @throws LibGuestFSException
7082 public void close () throws LibGuestFSException
7088 private native void _close (long g) throws LibGuestFSException;
7090 public void finalize () throws LibGuestFSException
7098 fun (name, style, _, flags, _, shortdesc, longdesc) ->
7099 if not (List.mem NotInDocs flags); then (
7100 let doc = replace_str longdesc "C<guestfs_" "C<g." in
7102 if List.mem ProtocolLimitWarning flags then
7103 doc ^ "\n\n" ^ protocol_limit_warning
7106 if List.mem DangerWillRobinson flags then
7107 doc ^ "\n\n" ^ danger_will_robinson
7109 let doc = pod2text ~width:60 name doc in
7110 let doc = List.map ( (* RHBZ#501883 *)
7113 | nonempty -> nonempty
7115 let doc = String.concat "\n * " doc in
7118 pr " * %s\n" shortdesc;
7121 pr " * @throws LibGuestFSException\n";
7125 generate_java_prototype ~public:true ~semicolon:false name style;
7128 pr " if (g == 0)\n";
7129 pr " throw new LibGuestFSException (\"%s: handle is closed\");\n"
7132 if fst style <> RErr then pr "return ";
7134 generate_call_args ~handle:"g" (snd style);
7138 generate_java_prototype ~privat:true ~native:true name style;
7145 and generate_java_prototype ?(public=false) ?(privat=false) ?(native=false)
7146 ?(semicolon=true) name style =
7147 if privat then pr "private ";
7148 if public then pr "public ";
7149 if native then pr "native ";
7152 (match fst style with
7153 | RErr -> pr "void ";
7154 | RInt _ -> pr "int ";
7155 | RInt64 _ -> pr "long ";
7156 | RBool _ -> pr "boolean ";
7157 | RConstString _ | RString _ -> pr "String ";
7158 | RStringList _ -> pr "String[] ";
7159 | RIntBool _ -> pr "IntBool ";
7160 | RPVList _ -> pr "PV[] ";
7161 | RVGList _ -> pr "VG[] ";
7162 | RLVList _ -> pr "LV[] ";
7163 | RStat _ -> pr "Stat ";
7164 | RStatVFS _ -> pr "StatVFS ";
7165 | RHashtable _ -> pr "HashMap<String,String> ";
7168 if native then pr "_%s " name else pr "%s " name;
7170 let needs_comma = ref false in
7179 if !needs_comma then pr ", ";
7180 needs_comma := true;
7197 pr " throws LibGuestFSException";
7198 if semicolon then pr ";"
7200 and generate_java_struct typ cols =
7201 generate_header CStyle LGPLv2;
7204 package com.redhat.et.libguestfs;
7207 * Libguestfs %s structure.
7218 | name, `UUID -> pr " public String %s;\n" name
7220 | name, `Int -> pr " public long %s;\n" name
7221 | name, `OptPercent ->
7222 pr " /* The next field is [0..100] or -1 meaning 'not present': */\n";
7223 pr " public float %s;\n" name
7228 and generate_java_c () =
7229 generate_header CStyle LGPLv2;
7236 #include \"com_redhat_et_libguestfs_GuestFS.h\"
7237 #include \"guestfs.h\"
7239 /* Note that this function returns. The exception is not thrown
7240 * until after the wrapper function returns.
7243 throw_exception (JNIEnv *env, const char *msg)
7246 cl = (*env)->FindClass (env,
7247 \"com/redhat/et/libguestfs/LibGuestFSException\");
7248 (*env)->ThrowNew (env, cl, msg);
7251 JNIEXPORT jlong JNICALL
7252 Java_com_redhat_et_libguestfs_GuestFS__1create
7253 (JNIEnv *env, jobject obj)
7257 g = guestfs_create ();
7259 throw_exception (env, \"GuestFS.create: failed to allocate handle\");
7262 guestfs_set_error_handler (g, NULL, NULL);
7263 return (jlong) (long) g;
7266 JNIEXPORT void JNICALL
7267 Java_com_redhat_et_libguestfs_GuestFS__1close
7268 (JNIEnv *env, jobject obj, jlong jg)
7270 guestfs_h *g = (guestfs_h *) (long) jg;
7277 fun (name, style, _, _, _, _, _) ->
7279 (match fst style with
7280 | RErr -> pr "void ";
7281 | RInt _ -> pr "jint ";
7282 | RInt64 _ -> pr "jlong ";
7283 | RBool _ -> pr "jboolean ";
7284 | RConstString _ | RString _ -> pr "jstring ";
7285 | RIntBool _ | RStat _ | RStatVFS _ | RHashtable _ ->
7287 | RStringList _ | RPVList _ | RVGList _ | RLVList _ ->
7291 pr "Java_com_redhat_et_libguestfs_GuestFS_";
7292 pr "%s" (replace_str ("_" ^ name) "_" "_1");
7294 pr " (JNIEnv *env, jobject obj, jlong jg";
7301 pr ", jstring j%s" n
7303 pr ", jobjectArray j%s" n
7305 pr ", jboolean j%s" n
7311 pr " guestfs_h *g = (guestfs_h *) (long) jg;\n";
7312 let error_code, no_ret =
7313 match fst style with
7314 | RErr -> pr " int r;\n"; "-1", ""
7316 | RInt _ -> pr " int r;\n"; "-1", "0"
7317 | RInt64 _ -> pr " int64_t r;\n"; "-1", "0"
7318 | RConstString _ -> pr " const char *r;\n"; "NULL", "NULL"
7320 pr " jstring jr;\n";
7321 pr " char *r;\n"; "NULL", "NULL"
7323 pr " jobjectArray jr;\n";
7326 pr " jstring jstr;\n";
7327 pr " char **r;\n"; "NULL", "NULL"
7329 pr " jobject jr;\n";
7331 pr " jfieldID fl;\n";
7332 pr " struct guestfs_int_bool *r;\n"; "NULL", "NULL"
7334 pr " jobject jr;\n";
7336 pr " jfieldID fl;\n";
7337 pr " struct guestfs_stat *r;\n"; "NULL", "NULL"
7339 pr " jobject jr;\n";
7341 pr " jfieldID fl;\n";
7342 pr " struct guestfs_statvfs *r;\n"; "NULL", "NULL"
7344 pr " jobjectArray jr;\n";
7346 pr " jfieldID fl;\n";
7347 pr " jobject jfl;\n";
7348 pr " struct guestfs_lvm_pv_list *r;\n"; "NULL", "NULL"
7350 pr " jobjectArray jr;\n";
7352 pr " jfieldID fl;\n";
7353 pr " jobject jfl;\n";
7354 pr " struct guestfs_lvm_vg_list *r;\n"; "NULL", "NULL"
7356 pr " jobjectArray jr;\n";
7358 pr " jfieldID fl;\n";
7359 pr " jobject jfl;\n";
7360 pr " struct guestfs_lvm_lv_list *r;\n"; "NULL", "NULL"
7361 | RHashtable _ -> pr " char **r;\n"; "NULL", "NULL" in
7368 pr " const char *%s;\n" n
7370 pr " int %s_len;\n" n;
7371 pr " const char **%s;\n" n
7378 (match fst style with
7379 | RStringList _ | RPVList _ | RVGList _ | RLVList _ -> true
7380 | RErr | RBool _ | RInt _ | RInt64 _ | RConstString _
7381 | RString _ | RIntBool _ | RStat _ | RStatVFS _
7382 | RHashtable _ -> false) ||
7383 List.exists (function StringList _ -> true | _ -> false) (snd style) in
7389 (* Get the parameters. *)
7395 pr " %s = (*env)->GetStringUTFChars (env, j%s, NULL);\n" n n
7397 (* This is completely undocumented, but Java null becomes
7400 pr " %s = j%s ? (*env)->GetStringUTFChars (env, j%s, NULL) : NULL;\n" n n n
7402 pr " %s_len = (*env)->GetArrayLength (env, j%s);\n" n n;
7403 pr " %s = guestfs_safe_malloc (g, sizeof (char *) * (%s_len+1));\n" n n;
7404 pr " for (i = 0; i < %s_len; ++i) {\n" n;
7405 pr " jobject o = (*env)->GetObjectArrayElement (env, j%s, i);\n"
7407 pr " %s[i] = (*env)->GetStringUTFChars (env, o, NULL);\n" n;
7409 pr " %s[%s_len] = NULL;\n" n n;
7412 pr " %s = j%s;\n" n n
7415 (* Make the call. *)
7416 pr " r = guestfs_%s " name;
7417 generate_call_args ~handle:"g" (snd style);
7420 (* Release the parameters. *)
7426 pr " (*env)->ReleaseStringUTFChars (env, j%s, %s);\n" n n
7429 pr " (*env)->ReleaseStringUTFChars (env, j%s, %s);\n" n n
7431 pr " for (i = 0; i < %s_len; ++i) {\n" n;
7432 pr " jobject o = (*env)->GetObjectArrayElement (env, j%s, i);\n"
7434 pr " (*env)->ReleaseStringUTFChars (env, o, %s[i]);\n" n;
7436 pr " free (%s);\n" n
7441 (* Check for errors. *)
7442 pr " if (r == %s) {\n" error_code;
7443 pr " throw_exception (env, guestfs_last_error (g));\n";
7444 pr " return %s;\n" no_ret;
7448 (match fst style with
7450 | RInt _ -> pr " return (jint) r;\n"
7451 | RBool _ -> pr " return (jboolean) r;\n"
7452 | RInt64 _ -> pr " return (jlong) r;\n"
7453 | RConstString _ -> pr " return (*env)->NewStringUTF (env, r);\n"
7455 pr " jr = (*env)->NewStringUTF (env, r);\n";
7459 pr " for (r_len = 0; r[r_len] != NULL; ++r_len) ;\n";
7460 pr " cl = (*env)->FindClass (env, \"java/lang/String\");\n";
7461 pr " jstr = (*env)->NewStringUTF (env, \"\");\n";
7462 pr " jr = (*env)->NewObjectArray (env, r_len, cl, jstr);\n";
7463 pr " for (i = 0; i < r_len; ++i) {\n";
7464 pr " jstr = (*env)->NewStringUTF (env, r[i]);\n";
7465 pr " (*env)->SetObjectArrayElement (env, jr, i, jstr);\n";
7466 pr " free (r[i]);\n";
7471 pr " cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/IntBool\");\n";
7472 pr " jr = (*env)->AllocObject (env, cl);\n";
7473 pr " fl = (*env)->GetFieldID (env, cl, \"i\", \"I\");\n";
7474 pr " (*env)->SetIntField (env, jr, fl, r->i);\n";
7475 pr " fl = (*env)->GetFieldID (env, cl, \"i\", \"Z\");\n";
7476 pr " (*env)->SetBooleanField (env, jr, fl, r->b);\n";
7477 pr " guestfs_free_int_bool (r);\n";
7480 pr " cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/Stat\");\n";
7481 pr " jr = (*env)->AllocObject (env, cl);\n";
7485 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"J\");\n"
7487 pr " (*env)->SetLongField (env, jr, fl, r->%s);\n" name;
7492 pr " cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/StatVFS\");\n";
7493 pr " jr = (*env)->AllocObject (env, cl);\n";
7497 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"J\");\n"
7499 pr " (*env)->SetLongField (env, jr, fl, r->%s);\n" name;
7504 generate_java_lvm_return "pv" "PV" pv_cols
7506 generate_java_lvm_return "vg" "VG" vg_cols
7508 generate_java_lvm_return "lv" "LV" lv_cols
7511 pr " throw_exception (env, \"%s: internal error: please let us know how to make a Java HashMap from JNI bindings!\");\n" name;
7512 pr " return NULL;\n"
7519 and generate_java_lvm_return typ jtyp cols =
7520 pr " cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/%s\");\n" jtyp;
7521 pr " jr = (*env)->NewObjectArray (env, r->len, cl, NULL);\n";
7522 pr " for (i = 0; i < r->len; ++i) {\n";
7523 pr " jfl = (*env)->AllocObject (env, cl);\n";
7527 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
7528 pr " (*env)->SetObjectField (env, jfl, fl, (*env)->NewStringUTF (env, r->val[i].%s));\n" name;
7531 pr " char s[33];\n";
7532 pr " memcpy (s, r->val[i].%s, 32);\n" name;
7534 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
7535 pr " (*env)->SetObjectField (env, jfl, fl, (*env)->NewStringUTF (env, s));\n";
7537 | name, (`Bytes|`Int) ->
7538 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"J\");\n" name;
7539 pr " (*env)->SetLongField (env, jfl, fl, r->val[i].%s);\n" name;
7540 | name, `OptPercent ->
7541 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"F\");\n" name;
7542 pr " (*env)->SetFloatField (env, jfl, fl, r->val[i].%s);\n" name;
7544 pr " (*env)->SetObjectArrayElement (env, jfl, i, jfl);\n";
7546 pr " guestfs_free_lvm_%s_list (r);\n" typ;
7549 and generate_haskell_hs () =
7550 generate_header HaskellStyle LGPLv2;
7552 (* XXX We only know how to generate partial FFI for Haskell
7553 * at the moment. Please help out!
7555 let can_generate style =
7559 | RInt64 _, _ -> true
7570 | RHashtable _, _ -> false in
7573 {-# INCLUDE <guestfs.h> #-}
7574 {-# LANGUAGE ForeignFunctionInterface #-}
7579 (* List out the names of the actions we want to export. *)
7581 fun (name, style, _, _, _, _, _) ->
7582 if can_generate style then pr ",\n %s" name
7589 import Foreign.C.Types
7591 import Control.Exception
7592 import Data.Typeable
7594 data GuestfsS = GuestfsS -- represents the opaque C struct
7595 type GuestfsP = Ptr GuestfsS -- guestfs_h *
7596 type GuestfsH = ForeignPtr GuestfsS -- guestfs_h * with attached finalizer
7598 -- XXX define properly later XXX
7602 data IntBool = IntBool
7604 data StatVFS = StatVFS
7605 data Hashtable = Hashtable
7607 foreign import ccall unsafe \"guestfs_create\" c_create
7609 foreign import ccall unsafe \"&guestfs_close\" c_close
7610 :: FunPtr (GuestfsP -> IO ())
7611 foreign import ccall unsafe \"guestfs_set_error_handler\" c_set_error_handler
7612 :: GuestfsP -> Ptr CInt -> Ptr CInt -> IO ()
7614 create :: IO GuestfsH
7617 c_set_error_handler p nullPtr nullPtr
7618 h <- newForeignPtr c_close p
7621 foreign import ccall unsafe \"guestfs_last_error\" c_last_error
7622 :: GuestfsP -> IO CString
7624 -- last_error :: GuestfsH -> IO (Maybe String)
7625 -- last_error h = do
7626 -- str <- withForeignPtr h (\\p -> c_last_error p)
7627 -- maybePeek peekCString str
7629 last_error :: GuestfsH -> IO (String)
7631 str <- withForeignPtr h (\\p -> c_last_error p)
7633 then return \"no error\"
7634 else peekCString str
7638 (* Generate wrappers for each foreign function. *)
7640 fun (name, style, _, _, _, _, _) ->
7641 if can_generate style then (
7642 pr "foreign import ccall unsafe \"guestfs_%s\" c_%s\n" name name;
7644 generate_haskell_prototype ~handle:"GuestfsP" style;
7648 generate_haskell_prototype ~handle:"GuestfsH" ~hs:true style;
7650 pr "%s %s = do\n" name
7651 (String.concat " " ("h" :: List.map name_of_argt (snd style)));
7653 (* Convert pointer arguments using with* functions. *)
7658 | String n -> pr "withCString %s $ \\%s -> " n n
7659 | OptString n -> pr "maybeWith withCString %s $ \\%s -> " n n
7660 | StringList n -> pr "withMany withCString %s $ \\%s -> withArray0 nullPtr %s $ \\%s -> " n n n n
7661 | Bool _ | Int _ -> ()
7663 (* Convert integer arguments. *)
7667 | Bool n -> sprintf "(fromBool %s)" n
7668 | Int n -> sprintf "(fromIntegral %s)" n
7669 | FileIn n | FileOut n | String n | OptString n | StringList n -> n
7671 pr "withForeignPtr h (\\p -> c_%s %s)\n" name
7672 (String.concat " " ("p" :: args));
7673 (match fst style with
7674 | RErr | RInt _ | RInt64 _ | RBool _ ->
7675 pr " if (r == -1)\n";
7677 pr " err <- last_error h\n";
7679 | RConstString _ | RString _ | RStringList _ | RIntBool _
7680 | RPVList _ | RVGList _ | RLVList _ | RStat _ | RStatVFS _
7682 pr " if (r == nullPtr)\n";
7684 pr " err <- last_error h\n";
7687 (match fst style with
7689 pr " else return ()\n"
7691 pr " else return (fromIntegral r)\n"
7693 pr " else return (fromIntegral r)\n"
7695 pr " else return (toBool r)\n"
7706 pr " else return ()\n" (* XXXXXXXXXXXXXXXXXXXX *)
7712 and generate_haskell_prototype ~handle ?(hs = false) style =
7714 let string = if hs then "String" else "CString" in
7715 let int = if hs then "Int" else "CInt" in
7716 let bool = if hs then "Bool" else "CInt" in
7717 let int64 = if hs then "Integer" else "Int64" in
7721 | String _ -> pr "%s" string
7722 | OptString _ -> if hs then pr "Maybe String" else pr "CString"
7723 | StringList _ -> if hs then pr "[String]" else pr "Ptr CString"
7724 | Bool _ -> pr "%s" bool
7725 | Int _ -> pr "%s" int
7726 | FileIn _ -> pr "%s" string
7727 | FileOut _ -> pr "%s" string
7732 (match fst style with
7733 | RErr -> if not hs then pr "CInt"
7734 | RInt _ -> pr "%s" int
7735 | RInt64 _ -> pr "%s" int64
7736 | RBool _ -> pr "%s" bool
7737 | RConstString _ -> pr "%s" string
7738 | RString _ -> pr "%s" string
7739 | RStringList _ -> pr "[%s]" string
7740 | RIntBool _ -> pr "IntBool"
7741 | RPVList _ -> pr "[PV]"
7742 | RVGList _ -> pr "[VG]"
7743 | RLVList _ -> pr "[LV]"
7744 | RStat _ -> pr "Stat"
7745 | RStatVFS _ -> pr "StatVFS"
7746 | RHashtable _ -> pr "Hashtable"
7750 and generate_bindtests () =
7751 generate_header CStyle LGPLv2;
7756 #include <inttypes.h>
7759 #include \"guestfs.h\"
7760 #include \"guestfs_protocol.h\"
7762 #define error guestfs_error
7765 print_strings (char * const* const argv)
7770 for (argc = 0; argv[argc] != NULL; ++argc) {
7771 if (argc > 0) printf (\", \");
7772 printf (\"\\\"%%s\\\"\", argv[argc]);
7777 /* The test0 function prints its parameters to stdout. */
7781 match test_functions with
7782 | [] -> assert false
7783 | test0 :: tests -> test0, tests in
7786 let (name, style, _, _, _, _, _) = test0 in
7787 generate_prototype ~extern:false ~semicolon:false ~newline:true
7788 ~handle:"g" ~prefix:"guestfs_" name style;
7794 | FileOut n -> pr " printf (\"%%s\\n\", %s);\n" n
7795 | OptString n -> pr " printf (\"%%s\\n\", %s ? %s : \"null\");\n" n n
7796 | StringList n -> pr " print_strings (%s);\n" n
7797 | Bool n -> pr " printf (\"%%s\\n\", %s ? \"true\" : \"false\");\n" n
7798 | Int n -> pr " printf (\"%%d\\n\", %s);\n" n
7800 pr " /* Java changes stdout line buffering so we need this: */\n";
7801 pr " fflush (stdout);\n";
7807 fun (name, style, _, _, _, _, _) ->
7808 if String.sub name (String.length name - 3) 3 <> "err" then (
7809 pr "/* Test normal return. */\n";
7810 generate_prototype ~extern:false ~semicolon:false ~newline:true
7811 ~handle:"g" ~prefix:"guestfs_" name style;
7813 (match fst style with
7818 pr " sscanf (val, \"%%d\", &r);\n";
7822 pr " sscanf (val, \"%%\" SCNi64, &r);\n";
7825 pr " return strcmp (val, \"true\") == 0;\n"
7827 (* Can't return the input string here. Return a static
7828 * string so we ensure we get a segfault if the caller
7831 pr " return \"static string\";\n"
7833 pr " return strdup (val);\n"
7835 pr " char **strs;\n";
7837 pr " sscanf (val, \"%%d\", &n);\n";
7838 pr " strs = malloc ((n+1) * sizeof (char *));\n";
7839 pr " for (i = 0; i < n; ++i) {\n";
7840 pr " strs[i] = malloc (16);\n";
7841 pr " snprintf (strs[i], 16, \"%%d\", i);\n";
7843 pr " strs[n] = NULL;\n";
7844 pr " return strs;\n"
7846 pr " struct guestfs_int_bool *r;\n";
7847 pr " r = malloc (sizeof (struct guestfs_int_bool));\n";
7848 pr " sscanf (val, \"%%\" SCNi32, &r->i);\n";
7852 pr " struct guestfs_lvm_pv_list *r;\n";
7854 pr " r = malloc (sizeof (struct guestfs_lvm_pv_list));\n";
7855 pr " sscanf (val, \"%%d\", &r->len);\n";
7856 pr " r->val = calloc (r->len, sizeof (struct guestfs_lvm_pv));\n";
7857 pr " for (i = 0; i < r->len; ++i) {\n";
7858 pr " r->val[i].pv_name = malloc (16);\n";
7859 pr " snprintf (r->val[i].pv_name, 16, \"%%d\", i);\n";
7863 pr " struct guestfs_lvm_vg_list *r;\n";
7865 pr " r = malloc (sizeof (struct guestfs_lvm_vg_list));\n";
7866 pr " sscanf (val, \"%%d\", &r->len);\n";
7867 pr " r->val = calloc (r->len, sizeof (struct guestfs_lvm_vg));\n";
7868 pr " for (i = 0; i < r->len; ++i) {\n";
7869 pr " r->val[i].vg_name = malloc (16);\n";
7870 pr " snprintf (r->val[i].vg_name, 16, \"%%d\", i);\n";
7874 pr " struct guestfs_lvm_lv_list *r;\n";
7876 pr " r = malloc (sizeof (struct guestfs_lvm_lv_list));\n";
7877 pr " sscanf (val, \"%%d\", &r->len);\n";
7878 pr " r->val = calloc (r->len, sizeof (struct guestfs_lvm_lv));\n";
7879 pr " for (i = 0; i < r->len; ++i) {\n";
7880 pr " r->val[i].lv_name = malloc (16);\n";
7881 pr " snprintf (r->val[i].lv_name, 16, \"%%d\", i);\n";
7885 pr " struct guestfs_stat *r;\n";
7886 pr " r = calloc (1, sizeof (*r));\n";
7887 pr " sscanf (val, \"%%\" SCNi64, &r->dev);\n";
7890 pr " struct guestfs_statvfs *r;\n";
7891 pr " r = calloc (1, sizeof (*r));\n";
7892 pr " sscanf (val, \"%%\" SCNi64, &r->bsize);\n";
7895 pr " char **strs;\n";
7897 pr " sscanf (val, \"%%d\", &n);\n";
7898 pr " strs = malloc ((n*2+1) * sizeof (char *));\n";
7899 pr " for (i = 0; i < n; ++i) {\n";
7900 pr " strs[i*2] = malloc (16);\n";
7901 pr " strs[i*2+1] = malloc (16);\n";
7902 pr " snprintf (strs[i*2], 16, \"%%d\", i);\n";
7903 pr " snprintf (strs[i*2+1], 16, \"%%d\", i);\n";
7905 pr " strs[n*2] = NULL;\n";
7906 pr " return strs;\n"
7911 pr "/* Test error return. */\n";
7912 generate_prototype ~extern:false ~semicolon:false ~newline:true
7913 ~handle:"g" ~prefix:"guestfs_" name style;
7915 pr " error (g, \"error\");\n";
7916 (match fst style with
7917 | RErr | RInt _ | RInt64 _ | RBool _ ->
7920 | RString _ | RStringList _ | RIntBool _
7921 | RPVList _ | RVGList _ | RLVList _ | RStat _ | RStatVFS _
7923 pr " return NULL;\n"
7930 and generate_ocaml_bindtests () =
7931 generate_header OCamlStyle GPLv2;
7935 let g = Guestfs.create () in
7942 | CallString s -> "\"" ^ s ^ "\""
7943 | CallOptString None -> "None"
7944 | CallOptString (Some s) -> sprintf "(Some \"%s\")" s
7945 | CallStringList xs ->
7946 "[|" ^ String.concat ";" (List.map (sprintf "\"%s\"") xs) ^ "|]"
7947 | CallInt i when i >= 0 -> string_of_int i
7948 | CallInt i (* when i < 0 *) -> "(" ^ string_of_int i ^ ")"
7949 | CallBool b -> string_of_bool b
7954 generate_lang_bindtests (
7955 fun f args -> pr " Guestfs.%s g %s;\n" f (mkargs args)
7958 pr "print_endline \"EOF\"\n"
7960 and generate_perl_bindtests () =
7961 pr "#!/usr/bin/perl -w\n";
7962 generate_header HashStyle GPLv2;
7969 my $g = Sys::Guestfs->new ();
7973 String.concat ", " (
7976 | CallString s -> "\"" ^ s ^ "\""
7977 | CallOptString None -> "undef"
7978 | CallOptString (Some s) -> sprintf "\"%s\"" s
7979 | CallStringList xs ->
7980 "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
7981 | CallInt i -> string_of_int i
7982 | CallBool b -> if b then "1" else "0"
7987 generate_lang_bindtests (
7988 fun f args -> pr "$g->%s (%s);\n" f (mkargs args)
7991 pr "print \"EOF\\n\"\n"
7993 and generate_python_bindtests () =
7994 generate_header HashStyle GPLv2;
7999 g = guestfs.GuestFS ()
8003 String.concat ", " (
8006 | CallString s -> "\"" ^ s ^ "\""
8007 | CallOptString None -> "None"
8008 | CallOptString (Some s) -> sprintf "\"%s\"" s
8009 | CallStringList xs ->
8010 "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
8011 | CallInt i -> string_of_int i
8012 | CallBool b -> if b then "1" else "0"
8017 generate_lang_bindtests (
8018 fun f args -> pr "g.%s (%s)\n" f (mkargs args)
8021 pr "print \"EOF\"\n"
8023 and generate_ruby_bindtests () =
8024 generate_header HashStyle GPLv2;
8029 g = Guestfs::create()
8033 String.concat ", " (
8036 | CallString s -> "\"" ^ s ^ "\""
8037 | CallOptString None -> "nil"
8038 | CallOptString (Some s) -> sprintf "\"%s\"" s
8039 | CallStringList xs ->
8040 "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
8041 | CallInt i -> string_of_int i
8042 | CallBool b -> string_of_bool b
8047 generate_lang_bindtests (
8048 fun f args -> pr "g.%s(%s)\n" f (mkargs args)
8051 pr "print \"EOF\\n\"\n"
8053 and generate_java_bindtests () =
8054 generate_header CStyle GPLv2;
8057 import com.redhat.et.libguestfs.*;
8059 public class Bindtests {
8060 public static void main (String[] argv)
8063 GuestFS g = new GuestFS ();
8067 String.concat ", " (
8070 | CallString s -> "\"" ^ s ^ "\""
8071 | CallOptString None -> "null"
8072 | CallOptString (Some s) -> sprintf "\"%s\"" s
8073 | CallStringList xs ->
8075 String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "}"
8076 | CallInt i -> string_of_int i
8077 | CallBool b -> string_of_bool b
8082 generate_lang_bindtests (
8083 fun f args -> pr " g.%s (%s);\n" f (mkargs args)
8087 System.out.println (\"EOF\");
8089 catch (Exception exn) {
8090 System.err.println (exn);
8097 and generate_haskell_bindtests () =
8098 generate_header HaskellStyle GPLv2;
8101 module Bindtests where
8102 import qualified Guestfs
8112 | CallString s -> "\"" ^ s ^ "\""
8113 | CallOptString None -> "Nothing"
8114 | CallOptString (Some s) -> sprintf "(Just \"%s\")" s
8115 | CallStringList xs ->
8116 "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
8117 | CallInt i when i < 0 -> "(" ^ string_of_int i ^ ")"
8118 | CallInt i -> string_of_int i
8119 | CallBool true -> "True"
8120 | CallBool false -> "False"
8125 generate_lang_bindtests (
8126 fun f args -> pr " Guestfs.%s g %s\n" f (mkargs args)
8129 pr " putStrLn \"EOF\"\n"
8131 (* Language-independent bindings tests - we do it this way to
8132 * ensure there is parity in testing bindings across all languages.
8134 and generate_lang_bindtests call =
8135 call "test0" [CallString "abc"; CallOptString (Some "def");
8136 CallStringList []; CallBool false;
8137 CallInt 0; CallString "123"; CallString "456"];
8138 call "test0" [CallString "abc"; CallOptString None;
8139 CallStringList []; CallBool false;
8140 CallInt 0; CallString "123"; CallString "456"];
8141 call "test0" [CallString ""; CallOptString (Some "def");
8142 CallStringList []; CallBool false;
8143 CallInt 0; CallString "123"; CallString "456"];
8144 call "test0" [CallString ""; CallOptString (Some "");
8145 CallStringList []; CallBool false;
8146 CallInt 0; CallString "123"; CallString "456"];
8147 call "test0" [CallString "abc"; CallOptString (Some "def");
8148 CallStringList ["1"]; CallBool false;
8149 CallInt 0; CallString "123"; CallString "456"];
8150 call "test0" [CallString "abc"; CallOptString (Some "def");
8151 CallStringList ["1"; "2"]; CallBool false;
8152 CallInt 0; CallString "123"; CallString "456"];
8153 call "test0" [CallString "abc"; CallOptString (Some "def");
8154 CallStringList ["1"]; CallBool true;
8155 CallInt 0; CallString "123"; CallString "456"];
8156 call "test0" [CallString "abc"; CallOptString (Some "def");
8157 CallStringList ["1"]; CallBool false;
8158 CallInt (-1); CallString "123"; CallString "456"];
8159 call "test0" [CallString "abc"; CallOptString (Some "def");
8160 CallStringList ["1"]; CallBool false;
8161 CallInt (-2); CallString "123"; CallString "456"];
8162 call "test0" [CallString "abc"; CallOptString (Some "def");
8163 CallStringList ["1"]; CallBool false;
8164 CallInt 1; CallString "123"; CallString "456"];
8165 call "test0" [CallString "abc"; CallOptString (Some "def");
8166 CallStringList ["1"]; CallBool false;
8167 CallInt 2; CallString "123"; CallString "456"];
8168 call "test0" [CallString "abc"; CallOptString (Some "def");
8169 CallStringList ["1"]; CallBool false;
8170 CallInt 4095; CallString "123"; CallString "456"];
8171 call "test0" [CallString "abc"; CallOptString (Some "def");
8172 CallStringList ["1"]; CallBool false;
8173 CallInt 0; CallString ""; CallString ""]
8175 (* XXX Add here tests of the return and error functions. *)
8177 (* This is used to generate the src/MAX_PROC_NR file which
8178 * contains the maximum procedure number, a surrogate for the
8179 * ABI version number. See src/Makefile.am for the details.
8181 and generate_max_proc_nr () =
8182 let proc_nrs = List.map (
8183 fun (_, _, proc_nr, _, _, _, _) -> proc_nr
8184 ) daemon_functions in
8186 let max_proc_nr = List.fold_left max 0 proc_nrs in
8188 pr "%d\n" max_proc_nr
8190 let output_to filename =
8191 let filename_new = filename ^ ".new" in
8192 chan := open_out filename_new;
8197 (* Is the new file different from the current file? *)
8198 if Sys.file_exists filename && files_equal filename filename_new then
8199 Unix.unlink filename_new (* same, so skip it *)
8201 (* different, overwrite old one *)
8202 (try Unix.chmod filename 0o644 with Unix.Unix_error _ -> ());
8203 Unix.rename filename_new filename;
8204 Unix.chmod filename 0o444;
8205 printf "written %s\n%!" filename;
8214 if not (Sys.file_exists "configure.ac") then (
8216 You are probably running this from the wrong directory.
8217 Run it from the top source directory using the command
8223 let close = output_to "src/guestfs_protocol.x" in
8227 let close = output_to "src/guestfs-structs.h" in
8228 generate_structs_h ();
8231 let close = output_to "src/guestfs-actions.h" in
8232 generate_actions_h ();
8235 let close = output_to "src/guestfs-actions.c" in
8236 generate_client_actions ();
8239 let close = output_to "daemon/actions.h" in
8240 generate_daemon_actions_h ();
8243 let close = output_to "daemon/stubs.c" in
8244 generate_daemon_actions ();
8247 let close = output_to "capitests/tests.c" in
8251 let close = output_to "src/guestfs-bindtests.c" in
8252 generate_bindtests ();
8255 let close = output_to "fish/cmds.c" in
8256 generate_fish_cmds ();
8259 let close = output_to "fish/completion.c" in
8260 generate_fish_completion ();
8263 let close = output_to "guestfs-structs.pod" in
8264 generate_structs_pod ();
8267 let close = output_to "guestfs-actions.pod" in
8268 generate_actions_pod ();
8271 let close = output_to "guestfish-actions.pod" in
8272 generate_fish_actions_pod ();
8275 let close = output_to "ocaml/guestfs.mli" in
8276 generate_ocaml_mli ();
8279 let close = output_to "ocaml/guestfs.ml" in
8280 generate_ocaml_ml ();
8283 let close = output_to "ocaml/guestfs_c_actions.c" in
8284 generate_ocaml_c ();
8287 let close = output_to "ocaml/bindtests.ml" in
8288 generate_ocaml_bindtests ();
8291 let close = output_to "perl/Guestfs.xs" in
8292 generate_perl_xs ();
8295 let close = output_to "perl/lib/Sys/Guestfs.pm" in
8296 generate_perl_pm ();
8299 let close = output_to "perl/bindtests.pl" in
8300 generate_perl_bindtests ();
8303 let close = output_to "python/guestfs-py.c" in
8304 generate_python_c ();
8307 let close = output_to "python/guestfs.py" in
8308 generate_python_py ();
8311 let close = output_to "python/bindtests.py" in
8312 generate_python_bindtests ();
8315 let close = output_to "ruby/ext/guestfs/_guestfs.c" in
8319 let close = output_to "ruby/bindtests.rb" in
8320 generate_ruby_bindtests ();
8323 let close = output_to "java/com/redhat/et/libguestfs/GuestFS.java" in
8324 generate_java_java ();
8327 let close = output_to "java/com/redhat/et/libguestfs/PV.java" in
8328 generate_java_struct "PV" pv_cols;
8331 let close = output_to "java/com/redhat/et/libguestfs/VG.java" in
8332 generate_java_struct "VG" vg_cols;
8335 let close = output_to "java/com/redhat/et/libguestfs/LV.java" in
8336 generate_java_struct "LV" lv_cols;
8339 let close = output_to "java/com/redhat/et/libguestfs/Stat.java" in
8340 generate_java_struct "Stat" stat_cols;
8343 let close = output_to "java/com/redhat/et/libguestfs/StatVFS.java" in
8344 generate_java_struct "StatVFS" statvfs_cols;
8347 let close = output_to "java/com_redhat_et_libguestfs_GuestFS.c" in
8351 let close = output_to "java/Bindtests.java" in
8352 generate_java_bindtests ();
8355 let close = output_to "haskell/Guestfs.hs" in
8356 generate_haskell_hs ();
8359 let close = output_to "haskell/Bindtests.hs" in
8360 generate_haskell_bindtests ();
8363 let close = output_to "src/MAX_PROC_NR" in
8364 generate_max_proc_nr ();