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 (* "RStruct" is a function which returns a single named structure
70 * or an error indication (in C, a struct, and in other languages
71 * with varying representations, but usually very efficient). See
72 * after the function list below for the structures.
74 | RStruct of string * string (* name of retval, name of struct *)
75 (* "RStructList" is a function which returns either a list/array
76 * of structures (could be zero-length), or an error indication.
78 | RStructList of string * string (* name of retval, name of struct *)
79 (* Key-value pairs of untyped strings. Turns into a hashtable or
80 * dictionary in languages which support it. DON'T use this as a
81 * general "bucket" for results. Prefer a stronger typed return
82 * value if one is available, or write a custom struct. Don't use
83 * this if the list could potentially be very long, since it is
84 * inefficient. Keys should be unique. NULLs are not permitted.
86 | RHashtable of string
88 and args = argt list (* Function parameters, guestfs handle is implicit. *)
90 (* Note in future we should allow a "variable args" parameter as
91 * the final parameter, to allow commands like
92 * chmod mode file [file(s)...]
93 * This is not implemented yet, but many commands (such as chmod)
94 * are currently defined with the argument order keeping this future
95 * possibility in mind.
98 | String of string (* const char *name, cannot be NULL *)
99 | OptString of string (* const char *name, may be NULL *)
100 | StringList of string(* list of strings (each string cannot be NULL) *)
101 | Bool of string (* boolean *)
102 | Int of string (* int (smallish ints, signed, <= 31 bits) *)
103 (* These are treated as filenames (simple string parameters) in
104 * the C API and bindings. But in the RPC protocol, we transfer
105 * the actual file content up to or down from the daemon.
106 * FileIn: local machine -> daemon (in request)
107 * FileOut: daemon -> local machine (in reply)
108 * In guestfish (only), the special name "-" means read from
109 * stdin or write to stdout.
115 | ProtocolLimitWarning (* display warning about protocol size limits *)
116 | DangerWillRobinson (* flags particularly dangerous commands *)
117 | FishAlias of string (* provide an alias for this cmd in guestfish *)
118 | FishAction of string (* call this function in guestfish *)
119 | NotInFish (* do not export via guestfish *)
120 | NotInDocs (* do not add this function to documentation *)
122 let protocol_limit_warning =
123 "Because of the message protocol, there is a transfer limit
124 of somewhere between 2MB and 4MB. To transfer large files you should use
127 let danger_will_robinson =
128 "B<This command is dangerous. Without careful use you
129 can easily destroy all your data>."
131 (* You can supply zero or as many tests as you want per API call.
133 * Note that the test environment has 3 block devices, of size 500MB,
134 * 50MB and 10MB (respectively /dev/sda, /dev/sdb, /dev/sdc), and
135 * a fourth squashfs block device with some known files on it (/dev/sdd).
137 * Note for partitioning purposes, the 500MB device has 1015 cylinders.
138 * Number of cylinders was 63 for IDE emulated disks with precisely
139 * the same size. How exactly this is calculated is a mystery.
141 * The squashfs block device (/dev/sdd) comes from images/test.sqsh.
143 * To be able to run the tests in a reasonable amount of time,
144 * the virtual machine and block devices are reused between tests.
145 * So don't try testing kill_subprocess :-x
147 * Between each test we blockdev-setrw, umount-all, lvm-remove-all.
149 * Don't assume anything about the previous contents of the block
150 * devices. Use 'Init*' to create some initial scenarios.
152 * You can add a prerequisite clause to any individual test. This
153 * is a run-time check, which, if it fails, causes the test to be
154 * skipped. Useful if testing a command which might not work on
155 * all variations of libguestfs builds. A test that has prerequisite
156 * of 'Always' is run unconditionally.
158 * In addition, packagers can skip individual tests by setting the
159 * environment variables: eg:
160 * SKIP_TEST_<CMD>_<NUM>=1 SKIP_TEST_COMMAND_3=1 (skips test #3 of command)
161 * SKIP_TEST_<CMD>=1 SKIP_TEST_ZEROFREE=1 (skips all zerofree tests)
163 type tests = (test_init * test_prereq * test) list
165 (* Run the command sequence and just expect nothing to fail. *)
167 (* Run the command sequence and expect the output of the final
168 * command to be the string.
170 | TestOutput of seq * string
171 (* Run the command sequence and expect the output of the final
172 * command to be the list of strings.
174 | TestOutputList of seq * string list
175 (* Run the command sequence and expect the output of the final
176 * command to be the list of block devices (could be either
177 * "/dev/sd.." or "/dev/hd.." form - we don't check the 5th
178 * character of each string).
180 | TestOutputListOfDevices of seq * string list
181 (* Run the command sequence and expect the output of the final
182 * command to be the integer.
184 | TestOutputInt of seq * int
185 (* Run the command sequence and expect the output of the final
186 * command to be a true value (!= 0 or != NULL).
188 | TestOutputTrue of seq
189 (* Run the command sequence and expect the output of the final
190 * command to be a false value (== 0 or == NULL, but not an error).
192 | TestOutputFalse of seq
193 (* Run the command sequence and expect the output of the final
194 * command to be a list of the given length (but don't care about
197 | TestOutputLength of seq * int
198 (* Run the command sequence and expect the output of the final
199 * command to be a structure.
201 | TestOutputStruct of seq * test_field_compare list
202 (* Run the command sequence and expect the final command (only)
205 | TestLastFail of seq
207 and test_field_compare =
208 | CompareWithInt of string * int
209 | CompareWithString of string * string
210 | CompareFieldsIntEq of string * string
211 | CompareFieldsStrEq of string * string
213 (* Test prerequisites. *)
215 (* Test always runs. *)
217 (* Test is currently disabled - eg. it fails, or it tests some
218 * unimplemented feature.
221 (* 'string' is some C code (a function body) that should return
222 * true or false. The test will run if the code returns true.
225 (* As for 'If' but the test runs _unless_ the code returns true. *)
228 (* Some initial scenarios for testing. *)
230 (* Do nothing, block devices could contain random stuff including
231 * LVM PVs, and some filesystems might be mounted. This is usually
235 (* Block devices are empty and no filesystems are mounted. *)
237 (* /dev/sda contains a single partition /dev/sda1, which is formatted
238 * as ext2, empty [except for lost+found] and mounted on /.
239 * /dev/sdb and /dev/sdc may have random content.
244 * /dev/sda1 (is a PV):
245 * /dev/VG/LV (size 8MB):
246 * formatted as ext2, empty [except for lost+found], mounted on /
247 * /dev/sdb and /dev/sdc may have random content.
251 (* Sequence of commands for testing. *)
253 and cmd = string list
255 (* Note about long descriptions: When referring to another
256 * action, use the format C<guestfs_other> (ie. the full name of
257 * the C function). This will be replaced as appropriate in other
260 * Apart from that, long descriptions are just perldoc paragraphs.
263 (* These test functions are used in the language binding tests. *)
265 let test_all_args = [
268 StringList "strlist";
275 let test_all_rets = [
276 (* except for RErr, which is tested thoroughly elsewhere *)
277 "test0rint", RInt "valout";
278 "test0rint64", RInt64 "valout";
279 "test0rbool", RBool "valout";
280 "test0rconststring", RConstString "valout";
281 "test0rstring", RString "valout";
282 "test0rstringlist", RStringList "valout";
283 "test0rstruct", RStruct ("valout", "lvm_pv");
284 "test0rstructlist", RStructList ("valout", "lvm_pv");
285 "test0rhashtable", RHashtable "valout";
288 let test_functions = [
289 ("test0", (RErr, test_all_args), -1, [NotInFish; NotInDocs],
291 "internal test function - do not use",
293 This is an internal test function which is used to test whether
294 the automatically generated bindings can handle every possible
295 parameter type correctly.
297 It echos the contents of each parameter to stdout.
299 You probably don't want to call this function.");
303 [(name, (ret, [String "val"]), -1, [NotInFish; NotInDocs],
305 "internal test function - do not use",
307 This is an internal test function which is used to test whether
308 the automatically generated bindings can handle every possible
309 return type correctly.
311 It converts string C<val> to the return type.
313 You probably don't want to call this function.");
314 (name ^ "err", (ret, []), -1, [NotInFish; NotInDocs],
316 "internal test function - do not use",
318 This is an internal test function which is used to test whether
319 the automatically generated bindings can handle every possible
320 return type correctly.
322 This function always returns an error.
324 You probably don't want to call this function.")]
328 (* non_daemon_functions are any functions which don't get processed
329 * in the daemon, eg. functions for setting and getting local
330 * configuration values.
333 let non_daemon_functions = test_functions @ [
334 ("launch", (RErr, []), -1, [FishAlias "run"; FishAction "launch"],
336 "launch the qemu subprocess",
338 Internally libguestfs is implemented by running a virtual machine
341 You should call this after configuring the handle
342 (eg. adding drives) but before performing any actions.");
344 ("wait_ready", (RErr, []), -1, [NotInFish],
346 "wait until the qemu subprocess launches",
348 Internally libguestfs is implemented by running a virtual machine
351 You should call this after C<guestfs_launch> to wait for the launch
354 ("kill_subprocess", (RErr, []), -1, [],
356 "kill the qemu subprocess",
358 This kills the qemu subprocess. You should never need to call this.");
360 ("add_drive", (RErr, [String "filename"]), -1, [FishAlias "add"],
362 "add an image to examine or modify",
364 This function adds a virtual machine disk image C<filename> to the
365 guest. The first time you call this function, the disk appears as IDE
366 disk 0 (C</dev/sda>) in the guest, the second time as C</dev/sdb>, and
369 You don't necessarily need to be root when using libguestfs. However
370 you obviously do need sufficient permissions to access the filename
371 for whatever operations you want to perform (ie. read access if you
372 just want to read the image or write access if you want to modify the
375 This is equivalent to the qemu parameter
376 C<-drive file=filename,cache=off,if=...>.
378 Note that this call checks for the existence of C<filename>. This
379 stops you from specifying other types of drive which are supported
380 by qemu such as C<nbd:> and C<http:> URLs. To specify those, use
381 the general C<guestfs_config> call instead.");
383 ("add_cdrom", (RErr, [String "filename"]), -1, [FishAlias "cdrom"],
385 "add a CD-ROM disk image to examine",
387 This function adds a virtual CD-ROM disk image to the guest.
389 This is equivalent to the qemu parameter C<-cdrom filename>.
391 Note that this call checks for the existence of C<filename>. This
392 stops you from specifying other types of drive which are supported
393 by qemu such as C<nbd:> and C<http:> URLs. To specify those, use
394 the general C<guestfs_config> call instead.");
396 ("add_drive_ro", (RErr, [String "filename"]), -1, [FishAlias "add-ro"],
398 "add a drive in snapshot mode (read-only)",
400 This adds a drive in snapshot mode, making it effectively
403 Note that writes to the device are allowed, and will be seen for
404 the duration of the guestfs handle, but they are written
405 to a temporary file which is discarded as soon as the guestfs
406 handle is closed. We don't currently have any method to enable
407 changes to be committed, although qemu can support this.
409 This is equivalent to the qemu parameter
410 C<-drive file=filename,snapshot=on,if=...>.
412 Note that this call checks for the existence of C<filename>. This
413 stops you from specifying other types of drive which are supported
414 by qemu such as C<nbd:> and C<http:> URLs. To specify those, use
415 the general C<guestfs_config> call instead.");
417 ("config", (RErr, [String "qemuparam"; OptString "qemuvalue"]), -1, [],
419 "add qemu parameters",
421 This can be used to add arbitrary qemu command line parameters
422 of the form C<-param value>. Actually it's not quite arbitrary - we
423 prevent you from setting some parameters which would interfere with
424 parameters that we use.
426 The first character of C<param> string must be a C<-> (dash).
428 C<value> can be NULL.");
430 ("set_qemu", (RErr, [String "qemu"]), -1, [FishAlias "qemu"],
432 "set the qemu binary",
434 Set the qemu binary that we will use.
436 The default is chosen when the library was compiled by the
439 You can also override this by setting the C<LIBGUESTFS_QEMU>
440 environment variable.
442 Setting C<qemu> to C<NULL> restores the default qemu binary.");
444 ("get_qemu", (RConstString "qemu", []), -1, [],
446 "get the qemu binary",
448 Return the current qemu binary.
450 This is always non-NULL. If it wasn't set already, then this will
451 return the default qemu binary name.");
453 ("set_path", (RErr, [String "path"]), -1, [FishAlias "path"],
455 "set the search path",
457 Set the path that libguestfs searches for kernel and initrd.img.
459 The default is C<$libdir/guestfs> unless overridden by setting
460 C<LIBGUESTFS_PATH> environment variable.
462 Setting C<path> to C<NULL> restores the default path.");
464 ("get_path", (RConstString "path", []), -1, [],
466 "get the search path",
468 Return the current search path.
470 This is always non-NULL. If it wasn't set already, then this will
471 return the default path.");
473 ("set_append", (RErr, [String "append"]), -1, [FishAlias "append"],
475 "add options to kernel command line",
477 This function is used to add additional options to the
478 guest kernel command line.
480 The default is C<NULL> unless overridden by setting
481 C<LIBGUESTFS_APPEND> environment variable.
483 Setting C<append> to C<NULL> means I<no> additional options
484 are passed (libguestfs always adds a few of its own).");
486 ("get_append", (RConstString "append", []), -1, [],
488 "get the additional kernel options",
490 Return the additional kernel options which are added to the
491 guest kernel command line.
493 If C<NULL> then no options are added.");
495 ("set_autosync", (RErr, [Bool "autosync"]), -1, [FishAlias "autosync"],
499 If C<autosync> is true, this enables autosync. Libguestfs will make a
500 best effort attempt to run C<guestfs_umount_all> followed by
501 C<guestfs_sync> when the handle is closed
502 (also if the program exits without closing handles).
504 This is disabled by default (except in guestfish where it is
505 enabled by default).");
507 ("get_autosync", (RBool "autosync", []), -1, [],
511 Get the autosync flag.");
513 ("set_verbose", (RErr, [Bool "verbose"]), -1, [FishAlias "verbose"],
517 If C<verbose> is true, this turns on verbose messages (to C<stderr>).
519 Verbose messages are disabled unless the environment variable
520 C<LIBGUESTFS_DEBUG> is defined and set to C<1>.");
522 ("get_verbose", (RBool "verbose", []), -1, [],
526 This returns the verbose messages flag.");
528 ("is_ready", (RBool "ready", []), -1, [],
530 "is ready to accept commands",
532 This returns true iff this handle is ready to accept commands
533 (in the C<READY> state).
535 For more information on states, see L<guestfs(3)>.");
537 ("is_config", (RBool "config", []), -1, [],
539 "is in configuration state",
541 This returns true iff this handle is being configured
542 (in the C<CONFIG> state).
544 For more information on states, see L<guestfs(3)>.");
546 ("is_launching", (RBool "launching", []), -1, [],
548 "is launching subprocess",
550 This returns true iff this handle is launching the subprocess
551 (in the C<LAUNCHING> state).
553 For more information on states, see L<guestfs(3)>.");
555 ("is_busy", (RBool "busy", []), -1, [],
557 "is busy processing a command",
559 This returns true iff this handle is busy processing a command
560 (in the C<BUSY> state).
562 For more information on states, see L<guestfs(3)>.");
564 ("get_state", (RInt "state", []), -1, [],
566 "get the current state",
568 This returns the current state as an opaque integer. This is
569 only useful for printing debug and internal error messages.
571 For more information on states, see L<guestfs(3)>.");
573 ("set_busy", (RErr, []), -1, [NotInFish],
577 This sets the state to C<BUSY>. This is only used when implementing
578 actions using the low-level API.
580 For more information on states, see L<guestfs(3)>.");
582 ("set_ready", (RErr, []), -1, [NotInFish],
584 "set state to ready",
586 This sets the state to C<READY>. This is only used when implementing
587 actions using the low-level API.
589 For more information on states, see L<guestfs(3)>.");
591 ("end_busy", (RErr, []), -1, [NotInFish],
593 "leave the busy state",
595 This sets the state to C<READY>, or if in C<CONFIG> then it leaves the
596 state as is. This is only used when implementing
597 actions using the low-level API.
599 For more information on states, see L<guestfs(3)>.");
601 ("set_memsize", (RErr, [Int "memsize"]), -1, [FishAlias "memsize"],
603 "set memory allocated to the qemu subprocess",
605 This sets the memory size in megabytes allocated to the
606 qemu subprocess. This only has any effect if called before
609 You can also change this by setting the environment
610 variable C<LIBGUESTFS_MEMSIZE> before the handle is
613 For more information on the architecture of libguestfs,
614 see L<guestfs(3)>.");
616 ("get_memsize", (RInt "memsize", []), -1, [],
618 "get memory allocated to the qemu subprocess",
620 This gets the memory size in megabytes allocated to the
623 If C<guestfs_set_memsize> was not called
624 on this handle, and if C<LIBGUESTFS_MEMSIZE> was not set,
625 then this returns the compiled-in default value for memsize.
627 For more information on the architecture of libguestfs,
628 see L<guestfs(3)>.");
630 ("get_pid", (RInt "pid", []), -1, [FishAlias "pid"],
632 "get PID of qemu subprocess",
634 Return the process ID of the qemu subprocess. If there is no
635 qemu subprocess, then this will return an error.
637 This is an internal call used for debugging and testing.");
639 ("version", (RStruct ("version", "version"), []), -1, [],
640 [InitBasicFS, Always, TestOutputStruct (
641 [["version"]], [CompareWithInt ("major", 1)])],
642 "get the library version number",
644 Return the libguestfs version number that the program is linked
647 Note that because of dynamic linking this is not necessarily
648 the version of libguestfs that you compiled against. You can
649 compile the program, and then at runtime dynamically link
650 against a completely different C<libguestfs.so> library.
652 This call was added in version C<1.0.58>. In previous
653 versions of libguestfs there was no way to get the version
654 number. From C code you can use ELF weak linking tricks to find out if
655 this symbol exists (if it doesn't, then it's an earlier version).
657 The call returns a structure with four elements. The first
658 three (C<major>, C<minor> and C<release>) are numbers and
659 correspond to the usual version triplet. The fourth element
660 (C<extra>) is a string and is normally empty, but may be
661 used for distro-specific information.
663 To construct the original version string:
664 C<$major.$minor.$release$extra>
666 I<Note:> Don't use this call to test for availability
667 of features. Distro backports makes this unreliable.");
671 (* daemon_functions are any functions which cause some action
672 * to take place in the daemon.
675 let daemon_functions = [
676 ("mount", (RErr, [String "device"; String "mountpoint"]), 1, [],
677 [InitEmpty, Always, TestOutput (
678 [["sfdiskM"; "/dev/sda"; ","];
679 ["mkfs"; "ext2"; "/dev/sda1"];
680 ["mount"; "/dev/sda1"; "/"];
681 ["write_file"; "/new"; "new file contents"; "0"];
682 ["cat"; "/new"]], "new file contents")],
683 "mount a guest disk at a position in the filesystem",
685 Mount a guest disk at a position in the filesystem. Block devices
686 are named C</dev/sda>, C</dev/sdb> and so on, as they were added to
687 the guest. If those block devices contain partitions, they will have
688 the usual names (eg. C</dev/sda1>). Also LVM C</dev/VG/LV>-style
691 The rules are the same as for L<mount(2)>: A filesystem must
692 first be mounted on C</> before others can be mounted. Other
693 filesystems can only be mounted on directories which already
696 The mounted filesystem is writable, if we have sufficient permissions
697 on the underlying device.
699 The filesystem options C<sync> and C<noatime> are set with this
700 call, in order to improve reliability.");
702 ("sync", (RErr, []), 2, [],
703 [ InitEmpty, Always, TestRun [["sync"]]],
704 "sync disks, writes are flushed through to the disk image",
706 This syncs the disk, so that any writes are flushed through to the
707 underlying disk image.
709 You should always call this if you have modified a disk image, before
710 closing the handle.");
712 ("touch", (RErr, [String "path"]), 3, [],
713 [InitBasicFS, Always, TestOutputTrue (
715 ["exists"; "/new"]])],
716 "update file timestamps or create a new file",
718 Touch acts like the L<touch(1)> command. It can be used to
719 update the timestamps on a file, or, if the file does not exist,
720 to create a new zero-length file.");
722 ("cat", (RString "content", [String "path"]), 4, [ProtocolLimitWarning],
723 [InitBasicFS, Always, TestOutput (
724 [["write_file"; "/new"; "new file contents"; "0"];
725 ["cat"; "/new"]], "new file contents")],
726 "list the contents of a file",
728 Return the contents of the file named C<path>.
730 Note that this function cannot correctly handle binary files
731 (specifically, files containing C<\\0> character which is treated
732 as end of string). For those you need to use the C<guestfs_download>
733 function which has a more complex interface.");
735 ("ll", (RString "listing", [String "directory"]), 5, [],
736 [], (* XXX Tricky to test because it depends on the exact format
737 * of the 'ls -l' command, which changes between F10 and F11.
739 "list the files in a directory (long format)",
741 List the files in C<directory> (relative to the root directory,
742 there is no cwd) in the format of 'ls -la'.
744 This command is mostly useful for interactive sessions. It
745 is I<not> intended that you try to parse the output string.");
747 ("ls", (RStringList "listing", [String "directory"]), 6, [],
748 [InitBasicFS, Always, TestOutputList (
751 ["touch"; "/newest"];
752 ["ls"; "/"]], ["lost+found"; "new"; "newer"; "newest"])],
753 "list the files in a directory",
755 List the files in C<directory> (relative to the root directory,
756 there is no cwd). The '.' and '..' entries are not returned, but
757 hidden files are shown.
759 This command is mostly useful for interactive sessions. Programs
760 should probably use C<guestfs_readdir> instead.");
762 ("list_devices", (RStringList "devices", []), 7, [],
763 [InitEmpty, Always, TestOutputListOfDevices (
764 [["list_devices"]], ["/dev/sda"; "/dev/sdb"; "/dev/sdc"; "/dev/sdd"])],
765 "list the block devices",
767 List all the block devices.
769 The full block device names are returned, eg. C</dev/sda>");
771 ("list_partitions", (RStringList "partitions", []), 8, [],
772 [InitBasicFS, Always, TestOutputListOfDevices (
773 [["list_partitions"]], ["/dev/sda1"]);
774 InitEmpty, Always, TestOutputListOfDevices (
775 [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
776 ["list_partitions"]], ["/dev/sda1"; "/dev/sda2"; "/dev/sda3"])],
777 "list the partitions",
779 List all the partitions detected on all block devices.
781 The full partition device names are returned, eg. C</dev/sda1>
783 This does not return logical volumes. For that you will need to
784 call C<guestfs_lvs>.");
786 ("pvs", (RStringList "physvols", []), 9, [],
787 [InitBasicFSonLVM, Always, TestOutputListOfDevices (
788 [["pvs"]], ["/dev/sda1"]);
789 InitEmpty, Always, TestOutputListOfDevices (
790 [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
791 ["pvcreate"; "/dev/sda1"];
792 ["pvcreate"; "/dev/sda2"];
793 ["pvcreate"; "/dev/sda3"];
794 ["pvs"]], ["/dev/sda1"; "/dev/sda2"; "/dev/sda3"])],
795 "list the LVM physical volumes (PVs)",
797 List all the physical volumes detected. This is the equivalent
798 of the L<pvs(8)> command.
800 This returns a list of just the device names that contain
801 PVs (eg. C</dev/sda2>).
803 See also C<guestfs_pvs_full>.");
805 ("vgs", (RStringList "volgroups", []), 10, [],
806 [InitBasicFSonLVM, Always, TestOutputList (
808 InitEmpty, Always, TestOutputList (
809 [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
810 ["pvcreate"; "/dev/sda1"];
811 ["pvcreate"; "/dev/sda2"];
812 ["pvcreate"; "/dev/sda3"];
813 ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"];
814 ["vgcreate"; "VG2"; "/dev/sda3"];
815 ["vgs"]], ["VG1"; "VG2"])],
816 "list the LVM volume groups (VGs)",
818 List all the volumes groups detected. This is the equivalent
819 of the L<vgs(8)> command.
821 This returns a list of just the volume group names that were
822 detected (eg. C<VolGroup00>).
824 See also C<guestfs_vgs_full>.");
826 ("lvs", (RStringList "logvols", []), 11, [],
827 [InitBasicFSonLVM, Always, TestOutputList (
828 [["lvs"]], ["/dev/VG/LV"]);
829 InitEmpty, Always, TestOutputList (
830 [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
831 ["pvcreate"; "/dev/sda1"];
832 ["pvcreate"; "/dev/sda2"];
833 ["pvcreate"; "/dev/sda3"];
834 ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"];
835 ["vgcreate"; "VG2"; "/dev/sda3"];
836 ["lvcreate"; "LV1"; "VG1"; "50"];
837 ["lvcreate"; "LV2"; "VG1"; "50"];
838 ["lvcreate"; "LV3"; "VG2"; "50"];
839 ["lvs"]], ["/dev/VG1/LV1"; "/dev/VG1/LV2"; "/dev/VG2/LV3"])],
840 "list the LVM logical volumes (LVs)",
842 List all the logical volumes detected. This is the equivalent
843 of the L<lvs(8)> command.
845 This returns a list of the logical volume device names
846 (eg. C</dev/VolGroup00/LogVol00>).
848 See also C<guestfs_lvs_full>.");
850 ("pvs_full", (RStructList ("physvols", "lvm_pv"), []), 12, [],
851 [], (* XXX how to test? *)
852 "list the LVM physical volumes (PVs)",
854 List all the physical volumes detected. This is the equivalent
855 of the L<pvs(8)> command. The \"full\" version includes all fields.");
857 ("vgs_full", (RStructList ("volgroups", "lvm_vg"), []), 13, [],
858 [], (* XXX how to test? *)
859 "list the LVM volume groups (VGs)",
861 List all the volumes groups detected. This is the equivalent
862 of the L<vgs(8)> command. The \"full\" version includes all fields.");
864 ("lvs_full", (RStructList ("logvols", "lvm_lv"), []), 14, [],
865 [], (* XXX how to test? *)
866 "list the LVM logical volumes (LVs)",
868 List all the logical volumes detected. This is the equivalent
869 of the L<lvs(8)> command. The \"full\" version includes all fields.");
871 ("read_lines", (RStringList "lines", [String "path"]), 15, [],
872 [InitBasicFS, Always, TestOutputList (
873 [["write_file"; "/new"; "line1\r\nline2\nline3"; "0"];
874 ["read_lines"; "/new"]], ["line1"; "line2"; "line3"]);
875 InitBasicFS, Always, TestOutputList (
876 [["write_file"; "/new"; ""; "0"];
877 ["read_lines"; "/new"]], [])],
878 "read file as lines",
880 Return the contents of the file named C<path>.
882 The file contents are returned as a list of lines. Trailing
883 C<LF> and C<CRLF> character sequences are I<not> returned.
885 Note that this function cannot correctly handle binary files
886 (specifically, files containing C<\\0> character which is treated
887 as end of line). For those you need to use the C<guestfs_read_file>
888 function which has a more complex interface.");
890 ("aug_init", (RErr, [String "root"; Int "flags"]), 16, [],
891 [], (* XXX Augeas code needs tests. *)
892 "create a new Augeas handle",
894 Create a new Augeas handle for editing configuration files.
895 If there was any previous Augeas handle associated with this
896 guestfs session, then it is closed.
898 You must call this before using any other C<guestfs_aug_*>
901 C<root> is the filesystem root. C<root> must not be NULL,
904 The flags are the same as the flags defined in
905 E<lt>augeas.hE<gt>, the logical I<or> of the following
910 =item C<AUG_SAVE_BACKUP> = 1
912 Keep the original file with a C<.augsave> extension.
914 =item C<AUG_SAVE_NEWFILE> = 2
916 Save changes into a file with extension C<.augnew>, and
917 do not overwrite original. Overrides C<AUG_SAVE_BACKUP>.
919 =item C<AUG_TYPE_CHECK> = 4
921 Typecheck lenses (can be expensive).
923 =item C<AUG_NO_STDINC> = 8
925 Do not use standard load path for modules.
927 =item C<AUG_SAVE_NOOP> = 16
929 Make save a no-op, just record what would have been changed.
931 =item C<AUG_NO_LOAD> = 32
933 Do not load the tree in C<guestfs_aug_init>.
937 To close the handle, you can call C<guestfs_aug_close>.
939 To find out more about Augeas, see L<http://augeas.net/>.");
941 ("aug_close", (RErr, []), 26, [],
942 [], (* XXX Augeas code needs tests. *)
943 "close the current Augeas handle",
945 Close the current Augeas handle and free up any resources
946 used by it. After calling this, you have to call
947 C<guestfs_aug_init> again before you can use any other
950 ("aug_defvar", (RInt "nrnodes", [String "name"; OptString "expr"]), 17, [],
951 [], (* XXX Augeas code needs tests. *)
952 "define an Augeas variable",
954 Defines an Augeas variable C<name> whose value is the result
955 of evaluating C<expr>. If C<expr> is NULL, then C<name> is
958 On success this returns the number of nodes in C<expr>, or
959 C<0> if C<expr> evaluates to something which is not a nodeset.");
961 ("aug_defnode", (RStruct ("nrnodescreated", "int_bool"), [String "name"; String "expr"; String "val"]), 18, [],
962 [], (* XXX Augeas code needs tests. *)
963 "define an Augeas node",
965 Defines a variable C<name> whose value is the result of
968 If C<expr> evaluates to an empty nodeset, a node is created,
969 equivalent to calling C<guestfs_aug_set> C<expr>, C<value>.
970 C<name> will be the nodeset containing that single node.
972 On success this returns a pair containing the
973 number of nodes in the nodeset, and a boolean flag
974 if a node was created.");
976 ("aug_get", (RString "val", [String "path"]), 19, [],
977 [], (* XXX Augeas code needs tests. *)
978 "look up the value of an Augeas path",
980 Look up the value associated with C<path>. If C<path>
981 matches exactly one node, the C<value> is returned.");
983 ("aug_set", (RErr, [String "path"; String "val"]), 20, [],
984 [], (* XXX Augeas code needs tests. *)
985 "set Augeas path to value",
987 Set the value associated with C<path> to C<value>.");
989 ("aug_insert", (RErr, [String "path"; String "label"; Bool "before"]), 21, [],
990 [], (* XXX Augeas code needs tests. *)
991 "insert a sibling Augeas node",
993 Create a new sibling C<label> for C<path>, inserting it into
994 the tree before or after C<path> (depending on the boolean
997 C<path> must match exactly one existing node in the tree, and
998 C<label> must be a label, ie. not contain C</>, C<*> or end
999 with a bracketed index C<[N]>.");
1001 ("aug_rm", (RInt "nrnodes", [String "path"]), 22, [],
1002 [], (* XXX Augeas code needs tests. *)
1003 "remove an Augeas path",
1005 Remove C<path> and all of its children.
1007 On success this returns the number of entries which were removed.");
1009 ("aug_mv", (RErr, [String "src"; String "dest"]), 23, [],
1010 [], (* XXX Augeas code needs tests. *)
1013 Move the node C<src> to C<dest>. C<src> must match exactly
1014 one node. C<dest> is overwritten if it exists.");
1016 ("aug_match", (RStringList "matches", [String "path"]), 24, [],
1017 [], (* XXX Augeas code needs tests. *)
1018 "return Augeas nodes which match path",
1020 Returns a list of paths which match the path expression C<path>.
1021 The returned paths are sufficiently qualified so that they match
1022 exactly one node in the current tree.");
1024 ("aug_save", (RErr, []), 25, [],
1025 [], (* XXX Augeas code needs tests. *)
1026 "write all pending Augeas changes to disk",
1028 This writes all pending changes to disk.
1030 The flags which were passed to C<guestfs_aug_init> affect exactly
1031 how files are saved.");
1033 ("aug_load", (RErr, []), 27, [],
1034 [], (* XXX Augeas code needs tests. *)
1035 "load files into the tree",
1037 Load files into the tree.
1039 See C<aug_load> in the Augeas documentation for the full gory
1042 ("aug_ls", (RStringList "matches", [String "path"]), 28, [],
1043 [], (* XXX Augeas code needs tests. *)
1044 "list Augeas nodes under a path",
1046 This is just a shortcut for listing C<guestfs_aug_match>
1047 C<path/*> and sorting the resulting nodes into alphabetical order.");
1049 ("rm", (RErr, [String "path"]), 29, [],
1050 [InitBasicFS, Always, TestRun
1053 InitBasicFS, Always, TestLastFail
1055 InitBasicFS, Always, TestLastFail
1060 Remove the single file C<path>.");
1062 ("rmdir", (RErr, [String "path"]), 30, [],
1063 [InitBasicFS, Always, TestRun
1066 InitBasicFS, Always, TestLastFail
1067 [["rmdir"; "/new"]];
1068 InitBasicFS, Always, TestLastFail
1070 ["rmdir"; "/new"]]],
1071 "remove a directory",
1073 Remove the single directory C<path>.");
1075 ("rm_rf", (RErr, [String "path"]), 31, [],
1076 [InitBasicFS, Always, TestOutputFalse
1078 ["mkdir"; "/new/foo"];
1079 ["touch"; "/new/foo/bar"];
1081 ["exists"; "/new"]]],
1082 "remove a file or directory recursively",
1084 Remove the file or directory C<path>, recursively removing the
1085 contents if its a directory. This is like the C<rm -rf> shell
1088 ("mkdir", (RErr, [String "path"]), 32, [],
1089 [InitBasicFS, Always, TestOutputTrue
1091 ["is_dir"; "/new"]];
1092 InitBasicFS, Always, TestLastFail
1093 [["mkdir"; "/new/foo/bar"]]],
1094 "create a directory",
1096 Create a directory named C<path>.");
1098 ("mkdir_p", (RErr, [String "path"]), 33, [],
1099 [InitBasicFS, Always, TestOutputTrue
1100 [["mkdir_p"; "/new/foo/bar"];
1101 ["is_dir"; "/new/foo/bar"]];
1102 InitBasicFS, Always, TestOutputTrue
1103 [["mkdir_p"; "/new/foo/bar"];
1104 ["is_dir"; "/new/foo"]];
1105 InitBasicFS, Always, TestOutputTrue
1106 [["mkdir_p"; "/new/foo/bar"];
1107 ["is_dir"; "/new"]];
1108 (* Regression tests for RHBZ#503133: *)
1109 InitBasicFS, Always, TestRun
1111 ["mkdir_p"; "/new"]];
1112 InitBasicFS, Always, TestLastFail
1114 ["mkdir_p"; "/new"]]],
1115 "create a directory and parents",
1117 Create a directory named C<path>, creating any parent directories
1118 as necessary. This is like the C<mkdir -p> shell command.");
1120 ("chmod", (RErr, [Int "mode"; String "path"]), 34, [],
1121 [], (* XXX Need stat command to test *)
1124 Change the mode (permissions) of C<path> to C<mode>. Only
1125 numeric modes are supported.");
1127 ("chown", (RErr, [Int "owner"; Int "group"; String "path"]), 35, [],
1128 [], (* XXX Need stat command to test *)
1129 "change file owner and group",
1131 Change the file owner to C<owner> and group to C<group>.
1133 Only numeric uid and gid are supported. If you want to use
1134 names, you will need to locate and parse the password file
1135 yourself (Augeas support makes this relatively easy).");
1137 ("exists", (RBool "existsflag", [String "path"]), 36, [],
1138 [InitBasicFS, Always, TestOutputTrue (
1140 ["exists"; "/new"]]);
1141 InitBasicFS, Always, TestOutputTrue (
1143 ["exists"; "/new"]])],
1144 "test if file or directory exists",
1146 This returns C<true> if and only if there is a file, directory
1147 (or anything) with the given C<path> name.
1149 See also C<guestfs_is_file>, C<guestfs_is_dir>, C<guestfs_stat>.");
1151 ("is_file", (RBool "fileflag", [String "path"]), 37, [],
1152 [InitBasicFS, Always, TestOutputTrue (
1154 ["is_file"; "/new"]]);
1155 InitBasicFS, Always, TestOutputFalse (
1157 ["is_file"; "/new"]])],
1158 "test if file exists",
1160 This returns C<true> if and only if there is a file
1161 with the given C<path> name. Note that it returns false for
1162 other objects like directories.
1164 See also C<guestfs_stat>.");
1166 ("is_dir", (RBool "dirflag", [String "path"]), 38, [],
1167 [InitBasicFS, Always, TestOutputFalse (
1169 ["is_dir"; "/new"]]);
1170 InitBasicFS, Always, TestOutputTrue (
1172 ["is_dir"; "/new"]])],
1173 "test if file exists",
1175 This returns C<true> if and only if there is a directory
1176 with the given C<path> name. Note that it returns false for
1177 other objects like files.
1179 See also C<guestfs_stat>.");
1181 ("pvcreate", (RErr, [String "device"]), 39, [],
1182 [InitEmpty, Always, TestOutputListOfDevices (
1183 [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
1184 ["pvcreate"; "/dev/sda1"];
1185 ["pvcreate"; "/dev/sda2"];
1186 ["pvcreate"; "/dev/sda3"];
1187 ["pvs"]], ["/dev/sda1"; "/dev/sda2"; "/dev/sda3"])],
1188 "create an LVM physical volume",
1190 This creates an LVM physical volume on the named C<device>,
1191 where C<device> should usually be a partition name such
1194 ("vgcreate", (RErr, [String "volgroup"; StringList "physvols"]), 40, [],
1195 [InitEmpty, Always, TestOutputList (
1196 [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
1197 ["pvcreate"; "/dev/sda1"];
1198 ["pvcreate"; "/dev/sda2"];
1199 ["pvcreate"; "/dev/sda3"];
1200 ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"];
1201 ["vgcreate"; "VG2"; "/dev/sda3"];
1202 ["vgs"]], ["VG1"; "VG2"])],
1203 "create an LVM volume group",
1205 This creates an LVM volume group called C<volgroup>
1206 from the non-empty list of physical volumes C<physvols>.");
1208 ("lvcreate", (RErr, [String "logvol"; String "volgroup"; Int "mbytes"]), 41, [],
1209 [InitEmpty, Always, TestOutputList (
1210 [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
1211 ["pvcreate"; "/dev/sda1"];
1212 ["pvcreate"; "/dev/sda2"];
1213 ["pvcreate"; "/dev/sda3"];
1214 ["vgcreate"; "VG1"; "/dev/sda1 /dev/sda2"];
1215 ["vgcreate"; "VG2"; "/dev/sda3"];
1216 ["lvcreate"; "LV1"; "VG1"; "50"];
1217 ["lvcreate"; "LV2"; "VG1"; "50"];
1218 ["lvcreate"; "LV3"; "VG2"; "50"];
1219 ["lvcreate"; "LV4"; "VG2"; "50"];
1220 ["lvcreate"; "LV5"; "VG2"; "50"];
1222 ["/dev/VG1/LV1"; "/dev/VG1/LV2";
1223 "/dev/VG2/LV3"; "/dev/VG2/LV4"; "/dev/VG2/LV5"])],
1224 "create an LVM volume group",
1226 This creates an LVM volume group called C<logvol>
1227 on the volume group C<volgroup>, with C<size> megabytes.");
1229 ("mkfs", (RErr, [String "fstype"; String "device"]), 42, [],
1230 [InitEmpty, Always, TestOutput (
1231 [["sfdiskM"; "/dev/sda"; ","];
1232 ["mkfs"; "ext2"; "/dev/sda1"];
1233 ["mount"; "/dev/sda1"; "/"];
1234 ["write_file"; "/new"; "new file contents"; "0"];
1235 ["cat"; "/new"]], "new file contents")],
1236 "make a filesystem",
1238 This creates a filesystem on C<device> (usually a partition
1239 or LVM logical volume). The filesystem type is C<fstype>, for
1242 ("sfdisk", (RErr, [String "device";
1243 Int "cyls"; Int "heads"; Int "sectors";
1244 StringList "lines"]), 43, [DangerWillRobinson],
1246 "create partitions on a block device",
1248 This is a direct interface to the L<sfdisk(8)> program for creating
1249 partitions on block devices.
1251 C<device> should be a block device, for example C</dev/sda>.
1253 C<cyls>, C<heads> and C<sectors> are the number of cylinders, heads
1254 and sectors on the device, which are passed directly to sfdisk as
1255 the I<-C>, I<-H> and I<-S> parameters. If you pass C<0> for any
1256 of these, then the corresponding parameter is omitted. Usually for
1257 'large' disks, you can just pass C<0> for these, but for small
1258 (floppy-sized) disks, sfdisk (or rather, the kernel) cannot work
1259 out the right geometry and you will need to tell it.
1261 C<lines> is a list of lines that we feed to C<sfdisk>. For more
1262 information refer to the L<sfdisk(8)> manpage.
1264 To create a single partition occupying the whole disk, you would
1265 pass C<lines> as a single element list, when the single element being
1266 the string C<,> (comma).
1268 See also: C<guestfs_sfdisk_l>, C<guestfs_sfdisk_N>");
1270 ("write_file", (RErr, [String "path"; String "content"; Int "size"]), 44, [ProtocolLimitWarning],
1271 [InitBasicFS, Always, TestOutput (
1272 [["write_file"; "/new"; "new file contents"; "0"];
1273 ["cat"; "/new"]], "new file contents");
1274 InitBasicFS, Always, TestOutput (
1275 [["write_file"; "/new"; "\nnew file contents\n"; "0"];
1276 ["cat"; "/new"]], "\nnew file contents\n");
1277 InitBasicFS, Always, TestOutput (
1278 [["write_file"; "/new"; "\n\n"; "0"];
1279 ["cat"; "/new"]], "\n\n");
1280 InitBasicFS, Always, TestOutput (
1281 [["write_file"; "/new"; ""; "0"];
1282 ["cat"; "/new"]], "");
1283 InitBasicFS, Always, TestOutput (
1284 [["write_file"; "/new"; "\n\n\n"; "0"];
1285 ["cat"; "/new"]], "\n\n\n");
1286 InitBasicFS, Always, TestOutput (
1287 [["write_file"; "/new"; "\n"; "0"];
1288 ["cat"; "/new"]], "\n")],
1291 This call creates a file called C<path>. The contents of the
1292 file is the string C<content> (which can contain any 8 bit data),
1293 with length C<size>.
1295 As a special case, if C<size> is C<0>
1296 then the length is calculated using C<strlen> (so in this case
1297 the content cannot contain embedded ASCII NULs).
1299 I<NB.> Owing to a bug, writing content containing ASCII NUL
1300 characters does I<not> work, even if the length is specified.
1301 We hope to resolve this bug in a future version. In the meantime
1302 use C<guestfs_upload>.");
1304 ("umount", (RErr, [String "pathordevice"]), 45, [FishAlias "unmount"],
1305 [InitEmpty, Always, TestOutputListOfDevices (
1306 [["sfdiskM"; "/dev/sda"; ","];
1307 ["mkfs"; "ext2"; "/dev/sda1"];
1308 ["mount"; "/dev/sda1"; "/"];
1309 ["mounts"]], ["/dev/sda1"]);
1310 InitEmpty, Always, TestOutputList (
1311 [["sfdiskM"; "/dev/sda"; ","];
1312 ["mkfs"; "ext2"; "/dev/sda1"];
1313 ["mount"; "/dev/sda1"; "/"];
1316 "unmount a filesystem",
1318 This unmounts the given filesystem. The filesystem may be
1319 specified either by its mountpoint (path) or the device which
1320 contains the filesystem.");
1322 ("mounts", (RStringList "devices", []), 46, [],
1323 [InitBasicFS, Always, TestOutputListOfDevices (
1324 [["mounts"]], ["/dev/sda1"])],
1325 "show mounted filesystems",
1327 This returns the list of currently mounted filesystems. It returns
1328 the list of devices (eg. C</dev/sda1>, C</dev/VG/LV>).
1330 Some internal mounts are not shown.");
1332 ("umount_all", (RErr, []), 47, [FishAlias "unmount-all"],
1333 [InitBasicFS, Always, TestOutputList (
1336 (* check that umount_all can unmount nested mounts correctly: *)
1337 InitEmpty, Always, TestOutputList (
1338 [["sfdiskM"; "/dev/sda"; ",100 ,200 ,"];
1339 ["mkfs"; "ext2"; "/dev/sda1"];
1340 ["mkfs"; "ext2"; "/dev/sda2"];
1341 ["mkfs"; "ext2"; "/dev/sda3"];
1342 ["mount"; "/dev/sda1"; "/"];
1344 ["mount"; "/dev/sda2"; "/mp1"];
1345 ["mkdir"; "/mp1/mp2"];
1346 ["mount"; "/dev/sda3"; "/mp1/mp2"];
1347 ["mkdir"; "/mp1/mp2/mp3"];
1350 "unmount all filesystems",
1352 This unmounts all mounted filesystems.
1354 Some internal mounts are not unmounted by this call.");
1356 ("lvm_remove_all", (RErr, []), 48, [DangerWillRobinson],
1358 "remove all LVM LVs, VGs and PVs",
1360 This command removes all LVM logical volumes, volume groups
1361 and physical volumes.");
1363 ("file", (RString "description", [String "path"]), 49, [],
1364 [InitBasicFS, Always, TestOutput (
1366 ["file"; "/new"]], "empty");
1367 InitBasicFS, Always, TestOutput (
1368 [["write_file"; "/new"; "some content\n"; "0"];
1369 ["file"; "/new"]], "ASCII text");
1370 InitBasicFS, Always, TestLastFail (
1371 [["file"; "/nofile"]])],
1372 "determine file type",
1374 This call uses the standard L<file(1)> command to determine
1375 the type or contents of the file. This also works on devices,
1376 for example to find out whether a partition contains a filesystem.
1378 The exact command which runs is C<file -bsL path>. Note in
1379 particular that the filename is not prepended to the output
1380 (the C<-b> option).");
1382 ("command", (RString "output", [StringList "arguments"]), 50, [ProtocolLimitWarning],
1383 [InitBasicFS, Always, TestOutput (
1384 [["upload"; "test-command"; "/test-command"];
1385 ["chmod"; "0o755"; "/test-command"];
1386 ["command"; "/test-command 1"]], "Result1");
1387 InitBasicFS, Always, TestOutput (
1388 [["upload"; "test-command"; "/test-command"];
1389 ["chmod"; "0o755"; "/test-command"];
1390 ["command"; "/test-command 2"]], "Result2\n");
1391 InitBasicFS, Always, TestOutput (
1392 [["upload"; "test-command"; "/test-command"];
1393 ["chmod"; "0o755"; "/test-command"];
1394 ["command"; "/test-command 3"]], "\nResult3");
1395 InitBasicFS, Always, TestOutput (
1396 [["upload"; "test-command"; "/test-command"];
1397 ["chmod"; "0o755"; "/test-command"];
1398 ["command"; "/test-command 4"]], "\nResult4\n");
1399 InitBasicFS, Always, TestOutput (
1400 [["upload"; "test-command"; "/test-command"];
1401 ["chmod"; "0o755"; "/test-command"];
1402 ["command"; "/test-command 5"]], "\nResult5\n\n");
1403 InitBasicFS, Always, TestOutput (
1404 [["upload"; "test-command"; "/test-command"];
1405 ["chmod"; "0o755"; "/test-command"];
1406 ["command"; "/test-command 6"]], "\n\nResult6\n\n");
1407 InitBasicFS, Always, TestOutput (
1408 [["upload"; "test-command"; "/test-command"];
1409 ["chmod"; "0o755"; "/test-command"];
1410 ["command"; "/test-command 7"]], "");
1411 InitBasicFS, Always, TestOutput (
1412 [["upload"; "test-command"; "/test-command"];
1413 ["chmod"; "0o755"; "/test-command"];
1414 ["command"; "/test-command 8"]], "\n");
1415 InitBasicFS, Always, TestOutput (
1416 [["upload"; "test-command"; "/test-command"];
1417 ["chmod"; "0o755"; "/test-command"];
1418 ["command"; "/test-command 9"]], "\n\n");
1419 InitBasicFS, Always, TestOutput (
1420 [["upload"; "test-command"; "/test-command"];
1421 ["chmod"; "0o755"; "/test-command"];
1422 ["command"; "/test-command 10"]], "Result10-1\nResult10-2\n");
1423 InitBasicFS, Always, TestOutput (
1424 [["upload"; "test-command"; "/test-command"];
1425 ["chmod"; "0o755"; "/test-command"];
1426 ["command"; "/test-command 11"]], "Result11-1\nResult11-2");
1427 InitBasicFS, Always, TestLastFail (
1428 [["upload"; "test-command"; "/test-command"];
1429 ["chmod"; "0o755"; "/test-command"];
1430 ["command"; "/test-command"]])],
1431 "run a command from the guest filesystem",
1433 This call runs a command from the guest filesystem. The
1434 filesystem must be mounted, and must contain a compatible
1435 operating system (ie. something Linux, with the same
1436 or compatible processor architecture).
1438 The single parameter is an argv-style list of arguments.
1439 The first element is the name of the program to run.
1440 Subsequent elements are parameters. The list must be
1441 non-empty (ie. must contain a program name). Note that
1442 the command runs directly, and is I<not> invoked via
1443 the shell (see C<guestfs_sh>).
1445 The return value is anything printed to I<stdout> by
1448 If the command returns a non-zero exit status, then
1449 this function returns an error message. The error message
1450 string is the content of I<stderr> from the command.
1452 The C<$PATH> environment variable will contain at least
1453 C</usr/bin> and C</bin>. If you require a program from
1454 another location, you should provide the full path in the
1457 Shared libraries and data files required by the program
1458 must be available on filesystems which are mounted in the
1459 correct places. It is the caller's responsibility to ensure
1460 all filesystems that are needed are mounted at the right
1463 ("command_lines", (RStringList "lines", [StringList "arguments"]), 51, [ProtocolLimitWarning],
1464 [InitBasicFS, Always, TestOutputList (
1465 [["upload"; "test-command"; "/test-command"];
1466 ["chmod"; "0o755"; "/test-command"];
1467 ["command_lines"; "/test-command 1"]], ["Result1"]);
1468 InitBasicFS, Always, TestOutputList (
1469 [["upload"; "test-command"; "/test-command"];
1470 ["chmod"; "0o755"; "/test-command"];
1471 ["command_lines"; "/test-command 2"]], ["Result2"]);
1472 InitBasicFS, Always, TestOutputList (
1473 [["upload"; "test-command"; "/test-command"];
1474 ["chmod"; "0o755"; "/test-command"];
1475 ["command_lines"; "/test-command 3"]], ["";"Result3"]);
1476 InitBasicFS, Always, TestOutputList (
1477 [["upload"; "test-command"; "/test-command"];
1478 ["chmod"; "0o755"; "/test-command"];
1479 ["command_lines"; "/test-command 4"]], ["";"Result4"]);
1480 InitBasicFS, Always, TestOutputList (
1481 [["upload"; "test-command"; "/test-command"];
1482 ["chmod"; "0o755"; "/test-command"];
1483 ["command_lines"; "/test-command 5"]], ["";"Result5";""]);
1484 InitBasicFS, Always, TestOutputList (
1485 [["upload"; "test-command"; "/test-command"];
1486 ["chmod"; "0o755"; "/test-command"];
1487 ["command_lines"; "/test-command 6"]], ["";"";"Result6";""]);
1488 InitBasicFS, Always, TestOutputList (
1489 [["upload"; "test-command"; "/test-command"];
1490 ["chmod"; "0o755"; "/test-command"];
1491 ["command_lines"; "/test-command 7"]], []);
1492 InitBasicFS, Always, TestOutputList (
1493 [["upload"; "test-command"; "/test-command"];
1494 ["chmod"; "0o755"; "/test-command"];
1495 ["command_lines"; "/test-command 8"]], [""]);
1496 InitBasicFS, Always, TestOutputList (
1497 [["upload"; "test-command"; "/test-command"];
1498 ["chmod"; "0o755"; "/test-command"];
1499 ["command_lines"; "/test-command 9"]], ["";""]);
1500 InitBasicFS, Always, TestOutputList (
1501 [["upload"; "test-command"; "/test-command"];
1502 ["chmod"; "0o755"; "/test-command"];
1503 ["command_lines"; "/test-command 10"]], ["Result10-1";"Result10-2"]);
1504 InitBasicFS, Always, TestOutputList (
1505 [["upload"; "test-command"; "/test-command"];
1506 ["chmod"; "0o755"; "/test-command"];
1507 ["command_lines"; "/test-command 11"]], ["Result11-1";"Result11-2"])],
1508 "run a command, returning lines",
1510 This is the same as C<guestfs_command>, but splits the
1511 result into a list of lines.
1513 See also: C<guestfs_sh_lines>");
1515 ("stat", (RStruct ("statbuf", "stat"), [String "path"]), 52, [],
1516 [InitBasicFS, Always, TestOutputStruct (
1518 ["stat"; "/new"]], [CompareWithInt ("size", 0)])],
1519 "get file information",
1521 Returns file information for the given C<path>.
1523 This is the same as the C<stat(2)> system call.");
1525 ("lstat", (RStruct ("statbuf", "stat"), [String "path"]), 53, [],
1526 [InitBasicFS, Always, TestOutputStruct (
1528 ["lstat"; "/new"]], [CompareWithInt ("size", 0)])],
1529 "get file information for a symbolic link",
1531 Returns file information for the given C<path>.
1533 This is the same as C<guestfs_stat> except that if C<path>
1534 is a symbolic link, then the link is stat-ed, not the file it
1537 This is the same as the C<lstat(2)> system call.");
1539 ("statvfs", (RStruct ("statbuf", "statvfs"), [String "path"]), 54, [],
1540 [InitBasicFS, Always, TestOutputStruct (
1541 [["statvfs"; "/"]], [CompareWithInt ("namemax", 255);
1542 CompareWithInt ("bsize", 1024)])],
1543 "get file system statistics",
1545 Returns file system statistics for any mounted file system.
1546 C<path> should be a file or directory in the mounted file system
1547 (typically it is the mount point itself, but it doesn't need to be).
1549 This is the same as the C<statvfs(2)> system call.");
1551 ("tune2fs_l", (RHashtable "superblock", [String "device"]), 55, [],
1553 "get ext2/ext3/ext4 superblock details",
1555 This returns the contents of the ext2, ext3 or ext4 filesystem
1556 superblock on C<device>.
1558 It is the same as running C<tune2fs -l device>. See L<tune2fs(8)>
1559 manpage for more details. The list of fields returned isn't
1560 clearly defined, and depends on both the version of C<tune2fs>
1561 that libguestfs was built against, and the filesystem itself.");
1563 ("blockdev_setro", (RErr, [String "device"]), 56, [],
1564 [InitEmpty, Always, TestOutputTrue (
1565 [["blockdev_setro"; "/dev/sda"];
1566 ["blockdev_getro"; "/dev/sda"]])],
1567 "set block device to read-only",
1569 Sets the block device named C<device> to read-only.
1571 This uses the L<blockdev(8)> command.");
1573 ("blockdev_setrw", (RErr, [String "device"]), 57, [],
1574 [InitEmpty, Always, TestOutputFalse (
1575 [["blockdev_setrw"; "/dev/sda"];
1576 ["blockdev_getro"; "/dev/sda"]])],
1577 "set block device to read-write",
1579 Sets the block device named C<device> to read-write.
1581 This uses the L<blockdev(8)> command.");
1583 ("blockdev_getro", (RBool "ro", [String "device"]), 58, [],
1584 [InitEmpty, Always, TestOutputTrue (
1585 [["blockdev_setro"; "/dev/sda"];
1586 ["blockdev_getro"; "/dev/sda"]])],
1587 "is block device set to read-only",
1589 Returns a boolean indicating if the block device is read-only
1590 (true if read-only, false if not).
1592 This uses the L<blockdev(8)> command.");
1594 ("blockdev_getss", (RInt "sectorsize", [String "device"]), 59, [],
1595 [InitEmpty, Always, TestOutputInt (
1596 [["blockdev_getss"; "/dev/sda"]], 512)],
1597 "get sectorsize of block device",
1599 This returns the size of sectors on a block device.
1600 Usually 512, but can be larger for modern devices.
1602 (Note, this is not the size in sectors, use C<guestfs_blockdev_getsz>
1605 This uses the L<blockdev(8)> command.");
1607 ("blockdev_getbsz", (RInt "blocksize", [String "device"]), 60, [],
1608 [InitEmpty, Always, TestOutputInt (
1609 [["blockdev_getbsz"; "/dev/sda"]], 4096)],
1610 "get blocksize of block device",
1612 This returns the block size of a device.
1614 (Note this is different from both I<size in blocks> and
1615 I<filesystem block size>).
1617 This uses the L<blockdev(8)> command.");
1619 ("blockdev_setbsz", (RErr, [String "device"; Int "blocksize"]), 61, [],
1621 "set blocksize of block device",
1623 This sets the block size of a device.
1625 (Note this is different from both I<size in blocks> and
1626 I<filesystem block size>).
1628 This uses the L<blockdev(8)> command.");
1630 ("blockdev_getsz", (RInt64 "sizeinsectors", [String "device"]), 62, [],
1631 [InitEmpty, Always, TestOutputInt (
1632 [["blockdev_getsz"; "/dev/sda"]], 1024000)],
1633 "get total size of device in 512-byte sectors",
1635 This returns the size of the device in units of 512-byte sectors
1636 (even if the sectorsize isn't 512 bytes ... weird).
1638 See also C<guestfs_blockdev_getss> for the real sector size of
1639 the device, and C<guestfs_blockdev_getsize64> for the more
1640 useful I<size in bytes>.
1642 This uses the L<blockdev(8)> command.");
1644 ("blockdev_getsize64", (RInt64 "sizeinbytes", [String "device"]), 63, [],
1645 [InitEmpty, Always, TestOutputInt (
1646 [["blockdev_getsize64"; "/dev/sda"]], 524288000)],
1647 "get total size of device in bytes",
1649 This returns the size of the device in bytes.
1651 See also C<guestfs_blockdev_getsz>.
1653 This uses the L<blockdev(8)> command.");
1655 ("blockdev_flushbufs", (RErr, [String "device"]), 64, [],
1656 [InitEmpty, Always, TestRun
1657 [["blockdev_flushbufs"; "/dev/sda"]]],
1658 "flush device buffers",
1660 This tells the kernel to flush internal buffers associated
1663 This uses the L<blockdev(8)> command.");
1665 ("blockdev_rereadpt", (RErr, [String "device"]), 65, [],
1666 [InitEmpty, Always, TestRun
1667 [["blockdev_rereadpt"; "/dev/sda"]]],
1668 "reread partition table",
1670 Reread the partition table on C<device>.
1672 This uses the L<blockdev(8)> command.");
1674 ("upload", (RErr, [FileIn "filename"; String "remotefilename"]), 66, [],
1675 [InitBasicFS, Always, TestOutput (
1676 (* Pick a file from cwd which isn't likely to change. *)
1677 [["upload"; "../COPYING.LIB"; "/COPYING.LIB"];
1678 ["checksum"; "md5"; "/COPYING.LIB"]], "e3eda01d9815f8d24aae2dbd89b68b06")],
1679 "upload a file from the local machine",
1681 Upload local file C<filename> to C<remotefilename> on the
1684 C<filename> can also be a named pipe.
1686 See also C<guestfs_download>.");
1688 ("download", (RErr, [String "remotefilename"; FileOut "filename"]), 67, [],
1689 [InitBasicFS, Always, TestOutput (
1690 (* Pick a file from cwd which isn't likely to change. *)
1691 [["upload"; "../COPYING.LIB"; "/COPYING.LIB"];
1692 ["download"; "/COPYING.LIB"; "testdownload.tmp"];
1693 ["upload"; "testdownload.tmp"; "/upload"];
1694 ["checksum"; "md5"; "/upload"]], "e3eda01d9815f8d24aae2dbd89b68b06")],
1695 "download a file to the local machine",
1697 Download file C<remotefilename> and save it as C<filename>
1698 on the local machine.
1700 C<filename> can also be a named pipe.
1702 See also C<guestfs_upload>, C<guestfs_cat>.");
1704 ("checksum", (RString "checksum", [String "csumtype"; String "path"]), 68, [],
1705 [InitBasicFS, Always, TestOutput (
1706 [["write_file"; "/new"; "test\n"; "0"];
1707 ["checksum"; "crc"; "/new"]], "935282863");
1708 InitBasicFS, Always, TestLastFail (
1709 [["checksum"; "crc"; "/new"]]);
1710 InitBasicFS, Always, TestOutput (
1711 [["write_file"; "/new"; "test\n"; "0"];
1712 ["checksum"; "md5"; "/new"]], "d8e8fca2dc0f896fd7cb4cb0031ba249");
1713 InitBasicFS, Always, TestOutput (
1714 [["write_file"; "/new"; "test\n"; "0"];
1715 ["checksum"; "sha1"; "/new"]], "4e1243bd22c66e76c2ba9eddc1f91394e57f9f83");
1716 InitBasicFS, Always, TestOutput (
1717 [["write_file"; "/new"; "test\n"; "0"];
1718 ["checksum"; "sha224"; "/new"]], "52f1bf093f4b7588726035c176c0cdb4376cfea53819f1395ac9e6ec");
1719 InitBasicFS, Always, TestOutput (
1720 [["write_file"; "/new"; "test\n"; "0"];
1721 ["checksum"; "sha256"; "/new"]], "f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2");
1722 InitBasicFS, Always, TestOutput (
1723 [["write_file"; "/new"; "test\n"; "0"];
1724 ["checksum"; "sha384"; "/new"]], "109bb6b5b6d5547c1ce03c7a8bd7d8f80c1cb0957f50c4f7fda04692079917e4f9cad52b878f3d8234e1a170b154b72d");
1725 InitBasicFS, Always, TestOutput (
1726 [["write_file"; "/new"; "test\n"; "0"];
1727 ["checksum"; "sha512"; "/new"]], "0e3e75234abc68f4378a86b3f4b32a198ba301845b0cd6e50106e874345700cc6663a86c1ea125dc5e92be17c98f9a0f85ca9d5f595db2012f7cc3571945c123");
1728 InitBasicFS, Always, TestOutput (
1729 (* RHEL 5 thinks this is an HFS+ filesystem unless we give
1730 * the type explicitly.
1732 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
1733 ["checksum"; "md5"; "/known-3"]], "46d6ca27ee07cdc6fa99c2e138cc522c")],
1734 "compute MD5, SHAx or CRC checksum of file",
1736 This call computes the MD5, SHAx or CRC checksum of the
1739 The type of checksum to compute is given by the C<csumtype>
1740 parameter which must have one of the following values:
1746 Compute the cyclic redundancy check (CRC) specified by POSIX
1747 for the C<cksum> command.
1751 Compute the MD5 hash (using the C<md5sum> program).
1755 Compute the SHA1 hash (using the C<sha1sum> program).
1759 Compute the SHA224 hash (using the C<sha224sum> program).
1763 Compute the SHA256 hash (using the C<sha256sum> program).
1767 Compute the SHA384 hash (using the C<sha384sum> program).
1771 Compute the SHA512 hash (using the C<sha512sum> program).
1775 The checksum is returned as a printable string.");
1777 ("tar_in", (RErr, [FileIn "tarfile"; String "directory"]), 69, [],
1778 [InitBasicFS, Always, TestOutput (
1779 [["tar_in"; "../images/helloworld.tar"; "/"];
1780 ["cat"; "/hello"]], "hello\n")],
1781 "unpack tarfile to directory",
1783 This command uploads and unpacks local file C<tarfile> (an
1784 I<uncompressed> tar file) into C<directory>.
1786 To upload a compressed tarball, use C<guestfs_tgz_in>.");
1788 ("tar_out", (RErr, [String "directory"; FileOut "tarfile"]), 70, [],
1790 "pack directory into tarfile",
1792 This command packs the contents of C<directory> and downloads
1793 it to local file C<tarfile>.
1795 To download a compressed tarball, use C<guestfs_tgz_out>.");
1797 ("tgz_in", (RErr, [FileIn "tarball"; String "directory"]), 71, [],
1798 [InitBasicFS, Always, TestOutput (
1799 [["tgz_in"; "../images/helloworld.tar.gz"; "/"];
1800 ["cat"; "/hello"]], "hello\n")],
1801 "unpack compressed tarball to directory",
1803 This command uploads and unpacks local file C<tarball> (a
1804 I<gzip compressed> tar file) into C<directory>.
1806 To upload an uncompressed tarball, use C<guestfs_tar_in>.");
1808 ("tgz_out", (RErr, [String "directory"; FileOut "tarball"]), 72, [],
1810 "pack directory into compressed tarball",
1812 This command packs the contents of C<directory> and downloads
1813 it to local file C<tarball>.
1815 To download an uncompressed tarball, use C<guestfs_tar_out>.");
1817 ("mount_ro", (RErr, [String "device"; String "mountpoint"]), 73, [],
1818 [InitBasicFS, Always, TestLastFail (
1820 ["mount_ro"; "/dev/sda1"; "/"];
1821 ["touch"; "/new"]]);
1822 InitBasicFS, Always, TestOutput (
1823 [["write_file"; "/new"; "data"; "0"];
1825 ["mount_ro"; "/dev/sda1"; "/"];
1826 ["cat"; "/new"]], "data")],
1827 "mount a guest disk, read-only",
1829 This is the same as the C<guestfs_mount> command, but it
1830 mounts the filesystem with the read-only (I<-o ro>) flag.");
1832 ("mount_options", (RErr, [String "options"; String "device"; String "mountpoint"]), 74, [],
1834 "mount a guest disk with mount options",
1836 This is the same as the C<guestfs_mount> command, but it
1837 allows you to set the mount options as for the
1838 L<mount(8)> I<-o> flag.");
1840 ("mount_vfs", (RErr, [String "options"; String "vfstype"; String "device"; String "mountpoint"]), 75, [],
1842 "mount a guest disk with mount options and vfstype",
1844 This is the same as the C<guestfs_mount> command, but it
1845 allows you to set both the mount options and the vfstype
1846 as for the L<mount(8)> I<-o> and I<-t> flags.");
1848 ("debug", (RString "result", [String "subcmd"; StringList "extraargs"]), 76, [],
1850 "debugging and internals",
1852 The C<guestfs_debug> command exposes some internals of
1853 C<guestfsd> (the guestfs daemon) that runs inside the
1856 There is no comprehensive help for this command. You have
1857 to look at the file C<daemon/debug.c> in the libguestfs source
1858 to find out what you can do.");
1860 ("lvremove", (RErr, [String "device"]), 77, [],
1861 [InitEmpty, Always, TestOutputList (
1862 [["sfdiskM"; "/dev/sda"; ","];
1863 ["pvcreate"; "/dev/sda1"];
1864 ["vgcreate"; "VG"; "/dev/sda1"];
1865 ["lvcreate"; "LV1"; "VG"; "50"];
1866 ["lvcreate"; "LV2"; "VG"; "50"];
1867 ["lvremove"; "/dev/VG/LV1"];
1868 ["lvs"]], ["/dev/VG/LV2"]);
1869 InitEmpty, Always, TestOutputList (
1870 [["sfdiskM"; "/dev/sda"; ","];
1871 ["pvcreate"; "/dev/sda1"];
1872 ["vgcreate"; "VG"; "/dev/sda1"];
1873 ["lvcreate"; "LV1"; "VG"; "50"];
1874 ["lvcreate"; "LV2"; "VG"; "50"];
1875 ["lvremove"; "/dev/VG"];
1877 InitEmpty, Always, TestOutputList (
1878 [["sfdiskM"; "/dev/sda"; ","];
1879 ["pvcreate"; "/dev/sda1"];
1880 ["vgcreate"; "VG"; "/dev/sda1"];
1881 ["lvcreate"; "LV1"; "VG"; "50"];
1882 ["lvcreate"; "LV2"; "VG"; "50"];
1883 ["lvremove"; "/dev/VG"];
1885 "remove an LVM logical volume",
1887 Remove an LVM logical volume C<device>, where C<device> is
1888 the path to the LV, such as C</dev/VG/LV>.
1890 You can also remove all LVs in a volume group by specifying
1891 the VG name, C</dev/VG>.");
1893 ("vgremove", (RErr, [String "vgname"]), 78, [],
1894 [InitEmpty, Always, TestOutputList (
1895 [["sfdiskM"; "/dev/sda"; ","];
1896 ["pvcreate"; "/dev/sda1"];
1897 ["vgcreate"; "VG"; "/dev/sda1"];
1898 ["lvcreate"; "LV1"; "VG"; "50"];
1899 ["lvcreate"; "LV2"; "VG"; "50"];
1902 InitEmpty, Always, TestOutputList (
1903 [["sfdiskM"; "/dev/sda"; ","];
1904 ["pvcreate"; "/dev/sda1"];
1905 ["vgcreate"; "VG"; "/dev/sda1"];
1906 ["lvcreate"; "LV1"; "VG"; "50"];
1907 ["lvcreate"; "LV2"; "VG"; "50"];
1910 "remove an LVM volume group",
1912 Remove an LVM volume group C<vgname>, (for example C<VG>).
1914 This also forcibly removes all logical volumes in the volume
1917 ("pvremove", (RErr, [String "device"]), 79, [],
1918 [InitEmpty, Always, TestOutputListOfDevices (
1919 [["sfdiskM"; "/dev/sda"; ","];
1920 ["pvcreate"; "/dev/sda1"];
1921 ["vgcreate"; "VG"; "/dev/sda1"];
1922 ["lvcreate"; "LV1"; "VG"; "50"];
1923 ["lvcreate"; "LV2"; "VG"; "50"];
1925 ["pvremove"; "/dev/sda1"];
1927 InitEmpty, Always, TestOutputListOfDevices (
1928 [["sfdiskM"; "/dev/sda"; ","];
1929 ["pvcreate"; "/dev/sda1"];
1930 ["vgcreate"; "VG"; "/dev/sda1"];
1931 ["lvcreate"; "LV1"; "VG"; "50"];
1932 ["lvcreate"; "LV2"; "VG"; "50"];
1934 ["pvremove"; "/dev/sda1"];
1936 InitEmpty, Always, TestOutputListOfDevices (
1937 [["sfdiskM"; "/dev/sda"; ","];
1938 ["pvcreate"; "/dev/sda1"];
1939 ["vgcreate"; "VG"; "/dev/sda1"];
1940 ["lvcreate"; "LV1"; "VG"; "50"];
1941 ["lvcreate"; "LV2"; "VG"; "50"];
1943 ["pvremove"; "/dev/sda1"];
1945 "remove an LVM physical volume",
1947 This wipes a physical volume C<device> so that LVM will no longer
1950 The implementation uses the C<pvremove> command which refuses to
1951 wipe physical volumes that contain any volume groups, so you have
1952 to remove those first.");
1954 ("set_e2label", (RErr, [String "device"; String "label"]), 80, [],
1955 [InitBasicFS, Always, TestOutput (
1956 [["set_e2label"; "/dev/sda1"; "testlabel"];
1957 ["get_e2label"; "/dev/sda1"]], "testlabel")],
1958 "set the ext2/3/4 filesystem label",
1960 This sets the ext2/3/4 filesystem label of the filesystem on
1961 C<device> to C<label>. Filesystem labels are limited to
1964 You can use either C<guestfs_tune2fs_l> or C<guestfs_get_e2label>
1965 to return the existing label on a filesystem.");
1967 ("get_e2label", (RString "label", [String "device"]), 81, [],
1969 "get the ext2/3/4 filesystem label",
1971 This returns the ext2/3/4 filesystem label of the filesystem on
1974 ("set_e2uuid", (RErr, [String "device"; String "uuid"]), 82, [],
1975 [InitBasicFS, Always, TestOutput (
1976 [["set_e2uuid"; "/dev/sda1"; "a3a61220-882b-4f61-89f4-cf24dcc7297d"];
1977 ["get_e2uuid"; "/dev/sda1"]], "a3a61220-882b-4f61-89f4-cf24dcc7297d");
1978 InitBasicFS, Always, TestOutput (
1979 [["set_e2uuid"; "/dev/sda1"; "clear"];
1980 ["get_e2uuid"; "/dev/sda1"]], "");
1981 (* We can't predict what UUIDs will be, so just check the commands run. *)
1982 InitBasicFS, Always, TestRun (
1983 [["set_e2uuid"; "/dev/sda1"; "random"]]);
1984 InitBasicFS, Always, TestRun (
1985 [["set_e2uuid"; "/dev/sda1"; "time"]])],
1986 "set the ext2/3/4 filesystem UUID",
1988 This sets the ext2/3/4 filesystem UUID of the filesystem on
1989 C<device> to C<uuid>. The format of the UUID and alternatives
1990 such as C<clear>, C<random> and C<time> are described in the
1991 L<tune2fs(8)> manpage.
1993 You can use either C<guestfs_tune2fs_l> or C<guestfs_get_e2uuid>
1994 to return the existing UUID of a filesystem.");
1996 ("get_e2uuid", (RString "uuid", [String "device"]), 83, [],
1998 "get the ext2/3/4 filesystem UUID",
2000 This returns the ext2/3/4 filesystem UUID of the filesystem on
2003 ("fsck", (RInt "status", [String "fstype"; String "device"]), 84, [],
2004 [InitBasicFS, Always, TestOutputInt (
2005 [["umount"; "/dev/sda1"];
2006 ["fsck"; "ext2"; "/dev/sda1"]], 0);
2007 InitBasicFS, Always, TestOutputInt (
2008 [["umount"; "/dev/sda1"];
2009 ["zero"; "/dev/sda1"];
2010 ["fsck"; "ext2"; "/dev/sda1"]], 8)],
2011 "run the filesystem checker",
2013 This runs the filesystem checker (fsck) on C<device> which
2014 should have filesystem type C<fstype>.
2016 The returned integer is the status. See L<fsck(8)> for the
2017 list of status codes from C<fsck>.
2025 Multiple status codes can be summed together.
2029 A non-zero return code can mean \"success\", for example if
2030 errors have been corrected on the filesystem.
2034 Checking or repairing NTFS volumes is not supported
2039 This command is entirely equivalent to running C<fsck -a -t fstype device>.");
2041 ("zero", (RErr, [String "device"]), 85, [],
2042 [InitBasicFS, Always, TestOutput (
2043 [["umount"; "/dev/sda1"];
2044 ["zero"; "/dev/sda1"];
2045 ["file"; "/dev/sda1"]], "data")],
2046 "write zeroes to the device",
2048 This command writes zeroes over the first few blocks of C<device>.
2050 How many blocks are zeroed isn't specified (but it's I<not> enough
2051 to securely wipe the device). It should be sufficient to remove
2052 any partition tables, filesystem superblocks and so on.
2054 See also: C<guestfs_scrub_device>.");
2056 ("grub_install", (RErr, [String "root"; String "device"]), 86, [],
2057 (* Test disabled because grub-install incompatible with virtio-blk driver.
2058 * See also: https://bugzilla.redhat.com/show_bug.cgi?id=479760
2060 [InitBasicFS, Disabled, TestOutputTrue (
2061 [["grub_install"; "/"; "/dev/sda1"];
2062 ["is_dir"; "/boot"]])],
2065 This command installs GRUB (the Grand Unified Bootloader) on
2066 C<device>, with the root directory being C<root>.");
2068 ("cp", (RErr, [String "src"; String "dest"]), 87, [],
2069 [InitBasicFS, Always, TestOutput (
2070 [["write_file"; "/old"; "file content"; "0"];
2071 ["cp"; "/old"; "/new"];
2072 ["cat"; "/new"]], "file content");
2073 InitBasicFS, Always, TestOutputTrue (
2074 [["write_file"; "/old"; "file content"; "0"];
2075 ["cp"; "/old"; "/new"];
2076 ["is_file"; "/old"]]);
2077 InitBasicFS, Always, TestOutput (
2078 [["write_file"; "/old"; "file content"; "0"];
2080 ["cp"; "/old"; "/dir/new"];
2081 ["cat"; "/dir/new"]], "file content")],
2084 This copies a file from C<src> to C<dest> where C<dest> is
2085 either a destination filename or destination directory.");
2087 ("cp_a", (RErr, [String "src"; String "dest"]), 88, [],
2088 [InitBasicFS, Always, TestOutput (
2089 [["mkdir"; "/olddir"];
2090 ["mkdir"; "/newdir"];
2091 ["write_file"; "/olddir/file"; "file content"; "0"];
2092 ["cp_a"; "/olddir"; "/newdir"];
2093 ["cat"; "/newdir/olddir/file"]], "file content")],
2094 "copy a file or directory recursively",
2096 This copies a file or directory from C<src> to C<dest>
2097 recursively using the C<cp -a> command.");
2099 ("mv", (RErr, [String "src"; String "dest"]), 89, [],
2100 [InitBasicFS, Always, TestOutput (
2101 [["write_file"; "/old"; "file content"; "0"];
2102 ["mv"; "/old"; "/new"];
2103 ["cat"; "/new"]], "file content");
2104 InitBasicFS, Always, TestOutputFalse (
2105 [["write_file"; "/old"; "file content"; "0"];
2106 ["mv"; "/old"; "/new"];
2107 ["is_file"; "/old"]])],
2110 This moves a file from C<src> to C<dest> where C<dest> is
2111 either a destination filename or destination directory.");
2113 ("drop_caches", (RErr, [Int "whattodrop"]), 90, [],
2114 [InitEmpty, Always, TestRun (
2115 [["drop_caches"; "3"]])],
2116 "drop kernel page cache, dentries and inodes",
2118 This instructs the guest kernel to drop its page cache,
2119 and/or dentries and inode caches. The parameter C<whattodrop>
2120 tells the kernel what precisely to drop, see
2121 L<http://linux-mm.org/Drop_Caches>
2123 Setting C<whattodrop> to 3 should drop everything.
2125 This automatically calls L<sync(2)> before the operation,
2126 so that the maximum guest memory is freed.");
2128 ("dmesg", (RString "kmsgs", []), 91, [],
2129 [InitEmpty, Always, TestRun (
2131 "return kernel messages",
2133 This returns the kernel messages (C<dmesg> output) from
2134 the guest kernel. This is sometimes useful for extended
2135 debugging of problems.
2137 Another way to get the same information is to enable
2138 verbose messages with C<guestfs_set_verbose> or by setting
2139 the environment variable C<LIBGUESTFS_DEBUG=1> before
2140 running the program.");
2142 ("ping_daemon", (RErr, []), 92, [],
2143 [InitEmpty, Always, TestRun (
2144 [["ping_daemon"]])],
2145 "ping the guest daemon",
2147 This is a test probe into the guestfs daemon running inside
2148 the qemu subprocess. Calling this function checks that the
2149 daemon responds to the ping message, without affecting the daemon
2150 or attached block device(s) in any other way.");
2152 ("equal", (RBool "equality", [String "file1"; String "file2"]), 93, [],
2153 [InitBasicFS, Always, TestOutputTrue (
2154 [["write_file"; "/file1"; "contents of a file"; "0"];
2155 ["cp"; "/file1"; "/file2"];
2156 ["equal"; "/file1"; "/file2"]]);
2157 InitBasicFS, Always, TestOutputFalse (
2158 [["write_file"; "/file1"; "contents of a file"; "0"];
2159 ["write_file"; "/file2"; "contents of another file"; "0"];
2160 ["equal"; "/file1"; "/file2"]]);
2161 InitBasicFS, Always, TestLastFail (
2162 [["equal"; "/file1"; "/file2"]])],
2163 "test if two files have equal contents",
2165 This compares the two files C<file1> and C<file2> and returns
2166 true if their content is exactly equal, or false otherwise.
2168 The external L<cmp(1)> program is used for the comparison.");
2170 ("strings", (RStringList "stringsout", [String "path"]), 94, [ProtocolLimitWarning],
2171 [InitBasicFS, Always, TestOutputList (
2172 [["write_file"; "/new"; "hello\nworld\n"; "0"];
2173 ["strings"; "/new"]], ["hello"; "world"]);
2174 InitBasicFS, Always, TestOutputList (
2176 ["strings"; "/new"]], [])],
2177 "print the printable strings in a file",
2179 This runs the L<strings(1)> command on a file and returns
2180 the list of printable strings found.");
2182 ("strings_e", (RStringList "stringsout", [String "encoding"; String "path"]), 95, [ProtocolLimitWarning],
2183 [InitBasicFS, Always, TestOutputList (
2184 [["write_file"; "/new"; "hello\nworld\n"; "0"];
2185 ["strings_e"; "b"; "/new"]], []);
2186 InitBasicFS, Disabled, TestOutputList (
2187 [["write_file"; "/new"; "\000h\000e\000l\000l\000o\000\n\000w\000o\000r\000l\000d\000\n"; "24"];
2188 ["strings_e"; "b"; "/new"]], ["hello"; "world"])],
2189 "print the printable strings in a file",
2191 This is like the C<guestfs_strings> command, but allows you to
2192 specify the encoding.
2194 See the L<strings(1)> manpage for the full list of encodings.
2196 Commonly useful encodings are C<l> (lower case L) which will
2197 show strings inside Windows/x86 files.
2199 The returned strings are transcoded to UTF-8.");
2201 ("hexdump", (RString "dump", [String "path"]), 96, [ProtocolLimitWarning],
2202 [InitBasicFS, Always, TestOutput (
2203 [["write_file"; "/new"; "hello\nworld\n"; "12"];
2204 ["hexdump"; "/new"]], "00000000 68 65 6c 6c 6f 0a 77 6f 72 6c 64 0a |hello.world.|\n0000000c\n");
2205 (* Test for RHBZ#501888c2 regression which caused large hexdump
2206 * commands to segfault.
2208 InitBasicFS, Always, TestRun (
2209 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2210 ["hexdump"; "/100krandom"]])],
2211 "dump a file in hexadecimal",
2213 This runs C<hexdump -C> on the given C<path>. The result is
2214 the human-readable, canonical hex dump of the file.");
2216 ("zerofree", (RErr, [String "device"]), 97, [],
2217 [InitNone, Always, TestOutput (
2218 [["sfdiskM"; "/dev/sda"; ","];
2219 ["mkfs"; "ext3"; "/dev/sda1"];
2220 ["mount"; "/dev/sda1"; "/"];
2221 ["write_file"; "/new"; "test file"; "0"];
2222 ["umount"; "/dev/sda1"];
2223 ["zerofree"; "/dev/sda1"];
2224 ["mount"; "/dev/sda1"; "/"];
2225 ["cat"; "/new"]], "test file")],
2226 "zero unused inodes and disk blocks on ext2/3 filesystem",
2228 This runs the I<zerofree> program on C<device>. This program
2229 claims to zero unused inodes and disk blocks on an ext2/3
2230 filesystem, thus making it possible to compress the filesystem
2233 You should B<not> run this program if the filesystem is
2236 It is possible that using this program can damage the filesystem
2237 or data on the filesystem.");
2239 ("pvresize", (RErr, [String "device"]), 98, [],
2241 "resize an LVM physical volume",
2243 This resizes (expands or shrinks) an existing LVM physical
2244 volume to match the new size of the underlying device.");
2246 ("sfdisk_N", (RErr, [String "device"; Int "partnum";
2247 Int "cyls"; Int "heads"; Int "sectors";
2248 String "line"]), 99, [DangerWillRobinson],
2250 "modify a single partition on a block device",
2252 This runs L<sfdisk(8)> option to modify just the single
2253 partition C<n> (note: C<n> counts from 1).
2255 For other parameters, see C<guestfs_sfdisk>. You should usually
2256 pass C<0> for the cyls/heads/sectors parameters.");
2258 ("sfdisk_l", (RString "partitions", [String "device"]), 100, [],
2260 "display the partition table",
2262 This displays the partition table on C<device>, in the
2263 human-readable output of the L<sfdisk(8)> command. It is
2264 not intended to be parsed.");
2266 ("sfdisk_kernel_geometry", (RString "partitions", [String "device"]), 101, [],
2268 "display the kernel geometry",
2270 This displays the kernel's idea of the geometry of C<device>.
2272 The result is in human-readable format, and not designed to
2275 ("sfdisk_disk_geometry", (RString "partitions", [String "device"]), 102, [],
2277 "display the disk geometry from the partition table",
2279 This displays the disk geometry of C<device> read from the
2280 partition table. Especially in the case where the underlying
2281 block device has been resized, this can be different from the
2282 kernel's idea of the geometry (see C<guestfs_sfdisk_kernel_geometry>).
2284 The result is in human-readable format, and not designed to
2287 ("vg_activate_all", (RErr, [Bool "activate"]), 103, [],
2289 "activate or deactivate all volume groups",
2291 This command activates or (if C<activate> is false) deactivates
2292 all logical volumes in all volume groups.
2293 If activated, then they are made known to the
2294 kernel, ie. they appear as C</dev/mapper> devices. If deactivated,
2295 then those devices disappear.
2297 This command is the same as running C<vgchange -a y|n>");
2299 ("vg_activate", (RErr, [Bool "activate"; StringList "volgroups"]), 104, [],
2301 "activate or deactivate some volume groups",
2303 This command activates or (if C<activate> is false) deactivates
2304 all logical volumes in the listed volume groups C<volgroups>.
2305 If activated, then they are made known to the
2306 kernel, ie. they appear as C</dev/mapper> devices. If deactivated,
2307 then those devices disappear.
2309 This command is the same as running C<vgchange -a y|n volgroups...>
2311 Note that if C<volgroups> is an empty list then B<all> volume groups
2312 are activated or deactivated.");
2314 ("lvresize", (RErr, [String "device"; Int "mbytes"]), 105, [],
2315 [InitNone, Always, TestOutput (
2316 [["sfdiskM"; "/dev/sda"; ","];
2317 ["pvcreate"; "/dev/sda1"];
2318 ["vgcreate"; "VG"; "/dev/sda1"];
2319 ["lvcreate"; "LV"; "VG"; "10"];
2320 ["mkfs"; "ext2"; "/dev/VG/LV"];
2321 ["mount"; "/dev/VG/LV"; "/"];
2322 ["write_file"; "/new"; "test content"; "0"];
2324 ["lvresize"; "/dev/VG/LV"; "20"];
2325 ["e2fsck_f"; "/dev/VG/LV"];
2326 ["resize2fs"; "/dev/VG/LV"];
2327 ["mount"; "/dev/VG/LV"; "/"];
2328 ["cat"; "/new"]], "test content")],
2329 "resize an LVM logical volume",
2331 This resizes (expands or shrinks) an existing LVM logical
2332 volume to C<mbytes>. When reducing, data in the reduced part
2335 ("resize2fs", (RErr, [String "device"]), 106, [],
2336 [], (* lvresize tests this *)
2337 "resize an ext2/ext3 filesystem",
2339 This resizes an ext2 or ext3 filesystem to match the size of
2340 the underlying device.
2342 I<Note:> It is sometimes required that you run C<guestfs_e2fsck_f>
2343 on the C<device> before calling this command. For unknown reasons
2344 C<resize2fs> sometimes gives an error about this and sometimes not.
2345 In any case, it is always safe to call C<guestfs_e2fsck_f> before
2346 calling this function.");
2348 ("find", (RStringList "names", [String "directory"]), 107, [],
2349 [InitBasicFS, Always, TestOutputList (
2350 [["find"; "/"]], ["lost+found"]);
2351 InitBasicFS, Always, TestOutputList (
2355 ["find"; "/"]], ["a"; "b"; "b/c"; "lost+found"]);
2356 InitBasicFS, Always, TestOutputList (
2357 [["mkdir_p"; "/a/b/c"];
2358 ["touch"; "/a/b/c/d"];
2359 ["find"; "/a/b/"]], ["c"; "c/d"])],
2360 "find all files and directories",
2362 This command lists out all files and directories, recursively,
2363 starting at C<directory>. It is essentially equivalent to
2364 running the shell command C<find directory -print> but some
2365 post-processing happens on the output, described below.
2367 This returns a list of strings I<without any prefix>. Thus
2368 if the directory structure was:
2374 then the returned list from C<guestfs_find> C</tmp> would be
2382 If C<directory> is not a directory, then this command returns
2385 The returned list is sorted.");
2387 ("e2fsck_f", (RErr, [String "device"]), 108, [],
2388 [], (* lvresize tests this *)
2389 "check an ext2/ext3 filesystem",
2391 This runs C<e2fsck -p -f device>, ie. runs the ext2/ext3
2392 filesystem checker on C<device>, noninteractively (C<-p>),
2393 even if the filesystem appears to be clean (C<-f>).
2395 This command is only needed because of C<guestfs_resize2fs>
2396 (q.v.). Normally you should use C<guestfs_fsck>.");
2398 ("sleep", (RErr, [Int "secs"]), 109, [],
2399 [InitNone, Always, TestRun (
2401 "sleep for some seconds",
2403 Sleep for C<secs> seconds.");
2405 ("ntfs_3g_probe", (RInt "status", [Bool "rw"; String "device"]), 110, [],
2406 [InitNone, Always, TestOutputInt (
2407 [["sfdiskM"; "/dev/sda"; ","];
2408 ["mkfs"; "ntfs"; "/dev/sda1"];
2409 ["ntfs_3g_probe"; "true"; "/dev/sda1"]], 0);
2410 InitNone, Always, TestOutputInt (
2411 [["sfdiskM"; "/dev/sda"; ","];
2412 ["mkfs"; "ext2"; "/dev/sda1"];
2413 ["ntfs_3g_probe"; "true"; "/dev/sda1"]], 12)],
2414 "probe NTFS volume",
2416 This command runs the L<ntfs-3g.probe(8)> command which probes
2417 an NTFS C<device> for mountability. (Not all NTFS volumes can
2418 be mounted read-write, and some cannot be mounted at all).
2420 C<rw> is a boolean flag. Set it to true if you want to test
2421 if the volume can be mounted read-write. Set it to false if
2422 you want to test if the volume can be mounted read-only.
2424 The return value is an integer which C<0> if the operation
2425 would succeed, or some non-zero value documented in the
2426 L<ntfs-3g.probe(8)> manual page.");
2428 ("sh", (RString "output", [String "command"]), 111, [],
2429 [], (* XXX needs tests *)
2430 "run a command via the shell",
2432 This call runs a command from the guest filesystem via the
2435 This is like C<guestfs_command>, but passes the command to:
2437 /bin/sh -c \"command\"
2439 Depending on the guest's shell, this usually results in
2440 wildcards being expanded, shell expressions being interpolated
2443 All the provisos about C<guestfs_command> apply to this call.");
2445 ("sh_lines", (RStringList "lines", [String "command"]), 112, [],
2446 [], (* XXX needs tests *)
2447 "run a command via the shell returning lines",
2449 This is the same as C<guestfs_sh>, but splits the result
2450 into a list of lines.
2452 See also: C<guestfs_command_lines>");
2454 ("glob_expand", (RStringList "paths", [String "pattern"]), 113, [],
2455 [InitBasicFS, Always, TestOutputList (
2456 [["mkdir_p"; "/a/b/c"];
2457 ["touch"; "/a/b/c/d"];
2458 ["touch"; "/a/b/c/e"];
2459 ["glob_expand"; "/a/b/c/*"]], ["/a/b/c/d"; "/a/b/c/e"]);
2460 InitBasicFS, Always, TestOutputList (
2461 [["mkdir_p"; "/a/b/c"];
2462 ["touch"; "/a/b/c/d"];
2463 ["touch"; "/a/b/c/e"];
2464 ["glob_expand"; "/a/*/c/*"]], ["/a/b/c/d"; "/a/b/c/e"]);
2465 InitBasicFS, Always, TestOutputList (
2466 [["mkdir_p"; "/a/b/c"];
2467 ["touch"; "/a/b/c/d"];
2468 ["touch"; "/a/b/c/e"];
2469 ["glob_expand"; "/a/*/x/*"]], [])],
2470 "expand a wildcard path",
2472 This command searches for all the pathnames matching
2473 C<pattern> according to the wildcard expansion rules
2476 If no paths match, then this returns an empty list
2477 (note: not an error).
2479 It is just a wrapper around the C L<glob(3)> function
2480 with flags C<GLOB_MARK|GLOB_BRACE>.
2481 See that manual page for more details.");
2483 ("scrub_device", (RErr, [String "device"]), 114, [DangerWillRobinson],
2484 [InitNone, Always, TestRun ( (* use /dev/sdc because it's smaller *)
2485 [["scrub_device"; "/dev/sdc"]])],
2486 "scrub (securely wipe) a device",
2488 This command writes patterns over C<device> to make data retrieval
2491 It is an interface to the L<scrub(1)> program. See that
2492 manual page for more details.");
2494 ("scrub_file", (RErr, [String "file"]), 115, [],
2495 [InitBasicFS, Always, TestRun (
2496 [["write_file"; "/file"; "content"; "0"];
2497 ["scrub_file"; "/file"]])],
2498 "scrub (securely wipe) a file",
2500 This command writes patterns over a file to make data retrieval
2503 The file is I<removed> after scrubbing.
2505 It is an interface to the L<scrub(1)> program. See that
2506 manual page for more details.");
2508 ("scrub_freespace", (RErr, [String "dir"]), 116, [],
2509 [], (* XXX needs testing *)
2510 "scrub (securely wipe) free space",
2512 This command creates the directory C<dir> and then fills it
2513 with files until the filesystem is full, and scrubs the files
2514 as for C<guestfs_scrub_file>, and deletes them.
2515 The intention is to scrub any free space on the partition
2518 It is an interface to the L<scrub(1)> program. See that
2519 manual page for more details.");
2521 ("mkdtemp", (RString "dir", [String "template"]), 117, [],
2522 [InitBasicFS, Always, TestRun (
2524 ["mkdtemp"; "/tmp/tmpXXXXXX"]])],
2525 "create a temporary directory",
2527 This command creates a temporary directory. The
2528 C<template> parameter should be a full pathname for the
2529 temporary directory name with the final six characters being
2532 For example: \"/tmp/myprogXXXXXX\" or \"/Temp/myprogXXXXXX\",
2533 the second one being suitable for Windows filesystems.
2535 The name of the temporary directory that was created
2538 The temporary directory is created with mode 0700
2539 and is owned by root.
2541 The caller is responsible for deleting the temporary
2542 directory and its contents after use.
2544 See also: L<mkdtemp(3)>");
2546 ("wc_l", (RInt "lines", [String "path"]), 118, [],
2547 [InitBasicFS, Always, TestOutputInt (
2548 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2549 ["wc_l"; "/10klines"]], 10000)],
2550 "count lines in a file",
2552 This command counts the lines in a file, using the
2553 C<wc -l> external command.");
2555 ("wc_w", (RInt "words", [String "path"]), 119, [],
2556 [InitBasicFS, Always, TestOutputInt (
2557 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2558 ["wc_w"; "/10klines"]], 10000)],
2559 "count words in a file",
2561 This command counts the words in a file, using the
2562 C<wc -w> external command.");
2564 ("wc_c", (RInt "chars", [String "path"]), 120, [],
2565 [InitBasicFS, Always, TestOutputInt (
2566 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2567 ["wc_c"; "/100kallspaces"]], 102400)],
2568 "count characters in a file",
2570 This command counts the characters in a file, using the
2571 C<wc -c> external command.");
2573 ("head", (RStringList "lines", [String "path"]), 121, [ProtocolLimitWarning],
2574 [InitBasicFS, Always, TestOutputList (
2575 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2576 ["head"; "/10klines"]], ["0abcdefghijklmnopqrstuvwxyz";"1abcdefghijklmnopqrstuvwxyz";"2abcdefghijklmnopqrstuvwxyz";"3abcdefghijklmnopqrstuvwxyz";"4abcdefghijklmnopqrstuvwxyz";"5abcdefghijklmnopqrstuvwxyz";"6abcdefghijklmnopqrstuvwxyz";"7abcdefghijklmnopqrstuvwxyz";"8abcdefghijklmnopqrstuvwxyz";"9abcdefghijklmnopqrstuvwxyz"])],
2577 "return first 10 lines of a file",
2579 This command returns up to the first 10 lines of a file as
2580 a list of strings.");
2582 ("head_n", (RStringList "lines", [Int "nrlines"; String "path"]), 122, [ProtocolLimitWarning],
2583 [InitBasicFS, Always, TestOutputList (
2584 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2585 ["head_n"; "3"; "/10klines"]], ["0abcdefghijklmnopqrstuvwxyz";"1abcdefghijklmnopqrstuvwxyz";"2abcdefghijklmnopqrstuvwxyz"]);
2586 InitBasicFS, Always, TestOutputList (
2587 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2588 ["head_n"; "-9997"; "/10klines"]], ["0abcdefghijklmnopqrstuvwxyz";"1abcdefghijklmnopqrstuvwxyz";"2abcdefghijklmnopqrstuvwxyz"]);
2589 InitBasicFS, Always, TestOutputList (
2590 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2591 ["head_n"; "0"; "/10klines"]], [])],
2592 "return first N lines of a file",
2594 If the parameter C<nrlines> is a positive number, this returns the first
2595 C<nrlines> lines of the file C<path>.
2597 If the parameter C<nrlines> is a negative number, this returns lines
2598 from the file C<path>, excluding the last C<nrlines> lines.
2600 If the parameter C<nrlines> is zero, this returns an empty list.");
2602 ("tail", (RStringList "lines", [String "path"]), 123, [ProtocolLimitWarning],
2603 [InitBasicFS, Always, TestOutputList (
2604 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2605 ["tail"; "/10klines"]], ["9990abcdefghijklmnopqrstuvwxyz";"9991abcdefghijklmnopqrstuvwxyz";"9992abcdefghijklmnopqrstuvwxyz";"9993abcdefghijklmnopqrstuvwxyz";"9994abcdefghijklmnopqrstuvwxyz";"9995abcdefghijklmnopqrstuvwxyz";"9996abcdefghijklmnopqrstuvwxyz";"9997abcdefghijklmnopqrstuvwxyz";"9998abcdefghijklmnopqrstuvwxyz";"9999abcdefghijklmnopqrstuvwxyz"])],
2606 "return last 10 lines of a file",
2608 This command returns up to the last 10 lines of a file as
2609 a list of strings.");
2611 ("tail_n", (RStringList "lines", [Int "nrlines"; String "path"]), 124, [ProtocolLimitWarning],
2612 [InitBasicFS, Always, TestOutputList (
2613 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2614 ["tail_n"; "3"; "/10klines"]], ["9997abcdefghijklmnopqrstuvwxyz";"9998abcdefghijklmnopqrstuvwxyz";"9999abcdefghijklmnopqrstuvwxyz"]);
2615 InitBasicFS, Always, TestOutputList (
2616 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2617 ["tail_n"; "-9998"; "/10klines"]], ["9997abcdefghijklmnopqrstuvwxyz";"9998abcdefghijklmnopqrstuvwxyz";"9999abcdefghijklmnopqrstuvwxyz"]);
2618 InitBasicFS, Always, TestOutputList (
2619 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2620 ["tail_n"; "0"; "/10klines"]], [])],
2621 "return last N lines of a file",
2623 If the parameter C<nrlines> is a positive number, this returns the last
2624 C<nrlines> lines of the file C<path>.
2626 If the parameter C<nrlines> is a negative number, this returns lines
2627 from the file C<path>, starting with the C<-nrlines>th line.
2629 If the parameter C<nrlines> is zero, this returns an empty list.");
2631 ("df", (RString "output", []), 125, [],
2632 [], (* XXX Tricky to test because it depends on the exact format
2633 * of the 'df' command and other imponderables.
2635 "report file system disk space usage",
2637 This command runs the C<df> command to report disk space used.
2639 This command is mostly useful for interactive sessions. It
2640 is I<not> intended that you try to parse the output string.
2641 Use C<statvfs> from programs.");
2643 ("df_h", (RString "output", []), 126, [],
2644 [], (* XXX Tricky to test because it depends on the exact format
2645 * of the 'df' command and other imponderables.
2647 "report file system disk space usage (human readable)",
2649 This command runs the C<df -h> command to report disk space used
2650 in human-readable format.
2652 This command is mostly useful for interactive sessions. It
2653 is I<not> intended that you try to parse the output string.
2654 Use C<statvfs> from programs.");
2656 ("du", (RInt64 "sizekb", [String "path"]), 127, [],
2657 [InitBasicFS, Always, TestOutputInt (
2659 ["du"; "/p"]], 1 (* ie. 1 block, so depends on ext3 blocksize *))],
2660 "estimate file space usage",
2662 This command runs the C<du -s> command to estimate file space
2665 C<path> can be a file or a directory. If C<path> is a directory
2666 then the estimate includes the contents of the directory and all
2667 subdirectories (recursively).
2669 The result is the estimated size in I<kilobytes>
2670 (ie. units of 1024 bytes).");
2672 ("initrd_list", (RStringList "filenames", [String "path"]), 128, [],
2673 [InitBasicFS, Always, TestOutputList (
2674 [["mount_vfs"; "ro"; "squashfs"; "/dev/sdd"; "/"];
2675 ["initrd_list"; "/initrd"]], ["empty";"known-1";"known-2";"known-3"])],
2676 "list files in an initrd",
2678 This command lists out files contained in an initrd.
2680 The files are listed without any initial C</> character. The
2681 files are listed in the order they appear (not necessarily
2682 alphabetical). Directory names are listed as separate items.
2684 Old Linux kernels (2.4 and earlier) used a compressed ext2
2685 filesystem as initrd. We I<only> support the newer initramfs
2686 format (compressed cpio files).");
2688 ("mount_loop", (RErr, [String "file"; String "mountpoint"]), 129, [],
2690 "mount a file using the loop device",
2692 This command lets you mount C<file> (a filesystem image
2693 in a file) on a mount point. It is entirely equivalent to
2694 the command C<mount -o loop file mountpoint>.");
2696 ("mkswap", (RErr, [String "device"]), 130, [],
2697 [InitEmpty, Always, TestRun (
2698 [["sfdiskM"; "/dev/sda"; ","];
2699 ["mkswap"; "/dev/sda1"]])],
2700 "create a swap partition",
2702 Create a swap partition on C<device>.");
2704 ("mkswap_L", (RErr, [String "label"; String "device"]), 131, [],
2705 [InitEmpty, Always, TestRun (
2706 [["sfdiskM"; "/dev/sda"; ","];
2707 ["mkswap_L"; "hello"; "/dev/sda1"]])],
2708 "create a swap partition with a label",
2710 Create a swap partition on C<device> with label C<label>.");
2712 ("mkswap_U", (RErr, [String "uuid"; String "device"]), 132, [],
2713 [InitEmpty, Always, TestRun (
2714 [["sfdiskM"; "/dev/sda"; ","];
2715 ["mkswap_U"; "a3a61220-882b-4f61-89f4-cf24dcc7297d"; "/dev/sda1"]])],
2716 "create a swap partition with an explicit UUID",
2718 Create a swap partition on C<device> with UUID C<uuid>.");
2720 ("mknod", (RErr, [Int "mode"; Int "devmajor"; Int "devminor"; String "path"]), 133, [],
2721 [InitBasicFS, Always, TestOutputStruct (
2722 [["mknod"; "0o10777"; "0"; "0"; "/node"];
2723 (* NB: default umask 022 means 0777 -> 0755 in these tests *)
2724 ["stat"; "/node"]], [CompareWithInt ("mode", 0o10755)]);
2725 InitBasicFS, Always, TestOutputStruct (
2726 [["mknod"; "0o60777"; "66"; "99"; "/node"];
2727 ["stat"; "/node"]], [CompareWithInt ("mode", 0o60755)])],
2728 "make block, character or FIFO devices",
2730 This call creates block or character special devices, or
2731 named pipes (FIFOs).
2733 The C<mode> parameter should be the mode, using the standard
2734 constants. C<devmajor> and C<devminor> are the
2735 device major and minor numbers, only used when creating block
2736 and character special devices.");
2738 ("mkfifo", (RErr, [Int "mode"; String "path"]), 134, [],
2739 [InitBasicFS, Always, TestOutputStruct (
2740 [["mkfifo"; "0o777"; "/node"];
2741 ["stat"; "/node"]], [CompareWithInt ("mode", 0o10755)])],
2742 "make FIFO (named pipe)",
2744 This call creates a FIFO (named pipe) called C<path> with
2745 mode C<mode>. It is just a convenient wrapper around
2746 C<guestfs_mknod>.");
2748 ("mknod_b", (RErr, [Int "mode"; Int "devmajor"; Int "devminor"; String "path"]), 135, [],
2749 [InitBasicFS, Always, TestOutputStruct (
2750 [["mknod_b"; "0o777"; "99"; "66"; "/node"];
2751 ["stat"; "/node"]], [CompareWithInt ("mode", 0o60755)])],
2752 "make block device node",
2754 This call creates a block device node called C<path> with
2755 mode C<mode> and device major/minor C<devmajor> and C<devminor>.
2756 It is just a convenient wrapper around C<guestfs_mknod>.");
2758 ("mknod_c", (RErr, [Int "mode"; Int "devmajor"; Int "devminor"; String "path"]), 136, [],
2759 [InitBasicFS, Always, TestOutputStruct (
2760 [["mknod_c"; "0o777"; "99"; "66"; "/node"];
2761 ["stat"; "/node"]], [CompareWithInt ("mode", 0o20755)])],
2762 "make char device node",
2764 This call creates a char device node called C<path> with
2765 mode C<mode> and device major/minor C<devmajor> and C<devminor>.
2766 It is just a convenient wrapper around C<guestfs_mknod>.");
2768 ("umask", (RInt "oldmask", [Int "mask"]), 137, [],
2769 [], (* XXX umask is one of those stateful things that we should
2770 * reset between each test.
2772 "set file mode creation mask (umask)",
2774 This function sets the mask used for creating new files and
2775 device nodes to C<mask & 0777>.
2777 Typical umask values would be C<022> which creates new files
2778 with permissions like \"-rw-r--r--\" or \"-rwxr-xr-x\", and
2779 C<002> which creates new files with permissions like
2780 \"-rw-rw-r--\" or \"-rwxrwxr-x\".
2782 The default umask is C<022>. This is important because it
2783 means that directories and device nodes will be created with
2784 C<0644> or C<0755> mode even if you specify C<0777>.
2786 See also L<umask(2)>, C<guestfs_mknod>, C<guestfs_mkdir>.
2788 This call returns the previous umask.");
2790 ("readdir", (RStructList ("entries", "dirent"), [String "dir"]), 138, [],
2792 "read directories entries",
2794 This returns the list of directory entries in directory C<dir>.
2796 All entries in the directory are returned, including C<.> and
2797 C<..>. The entries are I<not> sorted, but returned in the same
2798 order as the underlying filesystem.
2800 This function is primarily intended for use by programs. To
2801 get a simple list of names, use C<guestfs_ls>. To get a printable
2802 directory for human consumption, use C<guestfs_ll>.");
2804 ("sfdiskM", (RErr, [String "device"; StringList "lines"]), 139, [DangerWillRobinson],
2806 "create partitions on a block device",
2808 This is a simplified interface to the C<guestfs_sfdisk>
2809 command, where partition sizes are specified in megabytes
2810 only (rounded to the nearest cylinder) and you don't need
2811 to specify the cyls, heads and sectors parameters which
2812 were rarely if ever used anyway.
2814 See also C<guestfs_sfdisk> and the L<sfdisk(8)> manpage.");
2818 let all_functions = non_daemon_functions @ daemon_functions
2820 (* In some places we want the functions to be displayed sorted
2821 * alphabetically, so this is useful:
2823 let all_functions_sorted =
2824 List.sort (fun (n1,_,_,_,_,_,_) (n2,_,_,_,_,_,_) ->
2825 compare n1 n2) all_functions
2827 (* Field types for structures. *)
2829 | FChar (* C 'char' (really, a 7 bit byte). *)
2830 | FString (* nul-terminated ASCII string. *)
2835 | FBytes (* Any int measure that counts bytes. *)
2836 | FUUID (* 32 bytes long, NOT nul-terminated. *)
2837 | FOptPercent (* [0..100], or -1 meaning "not present". *)
2839 (* Because we generate extra parsing code for LVM command line tools,
2840 * we have to pull out the LVM columns separately here.
2850 "pv_attr", FString (* XXX *);
2851 "pv_pe_count", FInt64;
2852 "pv_pe_alloc_count", FInt64;
2855 "pv_mda_count", FInt64;
2856 "pv_mda_free", FBytes;
2857 (* Not in Fedora 10:
2858 "pv_mda_size", FBytes;
2865 "vg_attr", FString (* XXX *);
2868 "vg_sysid", FString;
2869 "vg_extent_size", FBytes;
2870 "vg_extent_count", FInt64;
2871 "vg_free_count", FInt64;
2876 "snap_count", FInt64;
2879 "vg_mda_count", FInt64;
2880 "vg_mda_free", FBytes;
2881 (* Not in Fedora 10:
2882 "vg_mda_size", FBytes;
2888 "lv_attr", FString (* XXX *);
2891 "lv_kernel_major", FInt64;
2892 "lv_kernel_minor", FInt64;
2894 "seg_count", FInt64;
2896 "snap_percent", FOptPercent;
2897 "copy_percent", FOptPercent;
2900 "mirror_log", FString;
2904 (* Names and fields in all structures (in RStruct and RStructList)
2908 (* The old RIntBool return type, only ever used for aug_defnode. Do
2909 * not use this struct in any new code.
2912 "i", FInt32; (* for historical compatibility *)
2913 "b", FInt32; (* for historical compatibility *)
2916 (* LVM PVs, VGs, LVs. *)
2917 "lvm_pv", lvm_pv_cols;
2918 "lvm_vg", lvm_vg_cols;
2919 "lvm_lv", lvm_lv_cols;
2921 (* Column names and types from stat structures.
2922 * NB. Can't use things like 'st_atime' because glibc header files
2923 * define some of these as macros. Ugh.
2954 (* Column names in dirent structure. *)
2957 (* 'b' 'c' 'd' 'f' (FIFO) 'l' 'r' (regular file) 's' 'u' '?' *)
2962 (* Version numbers. *)
2969 ] (* end of structs *)
2971 (* Ugh, Java has to be different ..
2972 * These names are also used by the Haskell bindings.
2974 let java_structs = [
2975 "int_bool", "IntBool";
2980 "statvfs", "StatVFS";
2982 "version", "Version";
2985 (* Used for testing language bindings. *)
2987 | CallString of string
2988 | CallOptString of string option
2989 | CallStringList of string list
2993 (* Used to memoize the result of pod2text. *)
2994 let pod2text_memo_filename = "src/.pod2text.data"
2995 let pod2text_memo : ((int * string * string), string list) Hashtbl.t =
2997 let chan = open_in pod2text_memo_filename in
2998 let v = input_value chan in
3002 _ -> Hashtbl.create 13
3004 (* Useful functions.
3005 * Note we don't want to use any external OCaml libraries which
3006 * makes this a bit harder than it should be.
3008 let failwithf fs = ksprintf failwith fs
3010 let replace_char s c1 c2 =
3011 let s2 = String.copy s in
3012 let r = ref false in
3013 for i = 0 to String.length s2 - 1 do
3014 if String.unsafe_get s2 i = c1 then (
3015 String.unsafe_set s2 i c2;
3019 if not !r then s else s2
3023 (* || c = '\f' *) || c = '\n' || c = '\r' || c = '\t' (* || c = '\v' *)
3025 let triml ?(test = isspace) str =
3027 let n = ref (String.length str) in
3028 while !n > 0 && test str.[!i]; do
3033 else String.sub str !i !n
3035 let trimr ?(test = isspace) str =
3036 let n = ref (String.length str) in
3037 while !n > 0 && test str.[!n-1]; do
3040 if !n = String.length str then str
3041 else String.sub str 0 !n
3043 let trim ?(test = isspace) str =
3044 trimr ~test (triml ~test str)
3046 let rec find s sub =
3047 let len = String.length s in
3048 let sublen = String.length sub in
3050 if i <= len-sublen then (
3052 if j < sublen then (
3053 if s.[i+j] = sub.[j] then loop2 (j+1)
3059 if r = -1 then loop (i+1) else r
3065 let rec replace_str s s1 s2 =
3066 let len = String.length s in
3067 let sublen = String.length s1 in
3068 let i = find s s1 in
3071 let s' = String.sub s 0 i in
3072 let s'' = String.sub s (i+sublen) (len-i-sublen) in
3073 s' ^ s2 ^ replace_str s'' s1 s2
3076 let rec string_split sep str =
3077 let len = String.length str in
3078 let seplen = String.length sep in
3079 let i = find str sep in
3080 if i = -1 then [str]
3082 let s' = String.sub str 0 i in
3083 let s'' = String.sub str (i+seplen) (len-i-seplen) in
3084 s' :: string_split sep s''
3087 let files_equal n1 n2 =
3088 let cmd = sprintf "cmp -s %s %s" (Filename.quote n1) (Filename.quote n2) in
3089 match Sys.command cmd with
3092 | i -> failwithf "%s: failed with error code %d" cmd i
3094 let rec find_map f = function
3095 | [] -> raise Not_found
3099 | None -> find_map f xs
3102 let rec loop i = function
3104 | x :: xs -> f i x; loop (i+1) xs
3109 let rec loop i = function
3111 | x :: xs -> let r = f i x in r :: loop (i+1) xs
3115 let name_of_argt = function
3116 | String n | OptString n | StringList n | Bool n | Int n
3117 | FileIn n | FileOut n -> n
3119 let java_name_of_struct typ =
3120 try List.assoc typ java_structs
3123 "java_name_of_struct: no java_structs entry corresponding to %s" typ
3125 let cols_of_struct typ =
3126 try List.assoc typ structs
3128 failwithf "cols_of_struct: unknown struct %s" typ
3130 let seq_of_test = function
3131 | TestRun s | TestOutput (s, _) | TestOutputList (s, _)
3132 | TestOutputListOfDevices (s, _)
3133 | TestOutputInt (s, _) | TestOutputTrue s | TestOutputFalse s
3134 | TestOutputLength (s, _) | TestOutputStruct (s, _)
3135 | TestLastFail s -> s
3137 (* Check function names etc. for consistency. *)
3138 let check_functions () =
3139 let contains_uppercase str =
3140 let len = String.length str in
3142 if i >= len then false
3145 if c >= 'A' && c <= 'Z' then true
3152 (* Check function names. *)
3154 fun (name, _, _, _, _, _, _) ->
3155 if String.length name >= 7 && String.sub name 0 7 = "guestfs" then
3156 failwithf "function name %s does not need 'guestfs' prefix" name;
3158 failwithf "function name is empty";
3159 if name.[0] < 'a' || name.[0] > 'z' then
3160 failwithf "function name %s must start with lowercase a-z" name;
3161 if String.contains name '-' then
3162 failwithf "function name %s should not contain '-', use '_' instead."
3166 (* Check function parameter/return names. *)
3168 fun (name, style, _, _, _, _, _) ->
3169 let check_arg_ret_name n =
3170 if contains_uppercase n then
3171 failwithf "%s param/ret %s should not contain uppercase chars"
3173 if String.contains n '-' || String.contains n '_' then
3174 failwithf "%s param/ret %s should not contain '-' or '_'"
3177 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;
3178 if n = "int" || n = "char" || n = "short" || n = "long" then
3179 failwithf "%s has a param/ret which conflicts with a C type (eg. 'int', 'char' etc.)" name;
3180 if n = "i" || n = "n" then
3181 failwithf "%s has a param/ret called 'i' or 'n', which will cause some conflicts in the generated code" name;
3182 if n = "argv" || n = "args" then
3183 failwithf "%s has a param/ret called 'argv' or 'args', which will cause some conflicts in the generated code" name
3186 (match fst style with
3188 | RInt n | RInt64 n | RBool n | RConstString n | RString n
3189 | RStringList n | RStruct (n, _) | RStructList (n, _)
3191 check_arg_ret_name n
3193 List.iter (fun arg -> check_arg_ret_name (name_of_argt arg)) (snd style)
3196 (* Check short descriptions. *)
3198 fun (name, _, _, _, _, shortdesc, _) ->
3199 if shortdesc.[0] <> Char.lowercase shortdesc.[0] then
3200 failwithf "short description of %s should begin with lowercase." name;
3201 let c = shortdesc.[String.length shortdesc-1] in
3202 if c = '\n' || c = '.' then
3203 failwithf "short description of %s should not end with . or \\n." name
3206 (* Check long dscriptions. *)
3208 fun (name, _, _, _, _, _, longdesc) ->
3209 if longdesc.[String.length longdesc-1] = '\n' then
3210 failwithf "long description of %s should not end with \\n." name
3213 (* Check proc_nrs. *)
3215 fun (name, _, proc_nr, _, _, _, _) ->
3216 if proc_nr <= 0 then
3217 failwithf "daemon function %s should have proc_nr > 0" name
3221 fun (name, _, proc_nr, _, _, _, _) ->
3222 if proc_nr <> -1 then
3223 failwithf "non-daemon function %s should have proc_nr -1" name
3224 ) non_daemon_functions;
3227 List.map (fun (name, _, proc_nr, _, _, _, _) -> name, proc_nr)
3230 List.sort (fun (_,nr1) (_,nr2) -> compare nr1 nr2) proc_nrs in
3231 let rec loop = function
3234 | (name1,nr1) :: ((name2,nr2) :: _ as rest) when nr1 < nr2 ->
3236 | (name1,nr1) :: (name2,nr2) :: _ ->
3237 failwithf "%s and %s have conflicting procedure numbers (%d, %d)"
3245 (* Ignore functions that have no tests. We generate a
3246 * warning when the user does 'make check' instead.
3248 | name, _, _, _, [], _, _ -> ()
3249 | name, _, _, _, tests, _, _ ->
3253 match seq_of_test test with
3255 failwithf "%s has a test containing an empty sequence" name
3256 | cmds -> List.map List.hd cmds
3258 let funcs = List.flatten funcs in
3260 let tested = List.mem name funcs in
3263 failwithf "function %s has tests but does not test itself" name
3266 (* 'pr' prints to the current output file. *)
3267 let chan = ref stdout
3268 let pr fs = ksprintf (output_string !chan) fs
3270 (* Generate a header block in a number of standard styles. *)
3271 type comment_style = CStyle | HashStyle | OCamlStyle | HaskellStyle
3272 type license = GPLv2 | LGPLv2
3274 let generate_header comment license =
3275 let c = match comment with
3276 | CStyle -> pr "/* "; " *"
3277 | HashStyle -> pr "# "; "#"
3278 | OCamlStyle -> pr "(* "; " *"
3279 | HaskellStyle -> pr "{- "; " " in
3280 pr "libguestfs generated file\n";
3281 pr "%s WARNING: THIS FILE IS GENERATED BY 'src/generator.ml'.\n" c;
3282 pr "%s ANY CHANGES YOU MAKE TO THIS FILE WILL BE LOST.\n" c;
3284 pr "%s Copyright (C) 2009 Red Hat Inc.\n" c;
3288 pr "%s This program is free software; you can redistribute it and/or modify\n" c;
3289 pr "%s it under the terms of the GNU General Public License as published by\n" c;
3290 pr "%s the Free Software Foundation; either version 2 of the License, or\n" c;
3291 pr "%s (at your option) any later version.\n" c;
3293 pr "%s This program is distributed in the hope that it will be useful,\n" c;
3294 pr "%s but WITHOUT ANY WARRANTY; without even the implied warranty of\n" c;
3295 pr "%s MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" c;
3296 pr "%s GNU General Public License for more details.\n" c;
3298 pr "%s You should have received a copy of the GNU General Public License along\n" c;
3299 pr "%s with this program; if not, write to the Free Software Foundation, Inc.,\n" c;
3300 pr "%s 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\n" c;
3303 pr "%s This library is free software; you can redistribute it and/or\n" c;
3304 pr "%s modify it under the terms of the GNU Lesser General Public\n" c;
3305 pr "%s License as published by the Free Software Foundation; either\n" c;
3306 pr "%s version 2 of the License, or (at your option) any later version.\n" c;
3308 pr "%s This library is distributed in the hope that it will be useful,\n" c;
3309 pr "%s but WITHOUT ANY WARRANTY; without even the implied warranty of\n" c;
3310 pr "%s MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n" c;
3311 pr "%s Lesser General Public License for more details.\n" c;
3313 pr "%s You should have received a copy of the GNU Lesser General Public\n" c;
3314 pr "%s License along with this library; if not, write to the Free Software\n" c;
3315 pr "%s Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n" c;
3318 | CStyle -> pr " */\n"
3320 | OCamlStyle -> pr " *)\n"
3321 | HaskellStyle -> pr "-}\n"
3325 (* Start of main code generation functions below this line. *)
3327 (* Generate the pod documentation for the C API. *)
3328 let rec generate_actions_pod () =
3330 fun (shortname, style, _, flags, _, _, longdesc) ->
3331 if not (List.mem NotInDocs flags) then (
3332 let name = "guestfs_" ^ shortname in
3333 pr "=head2 %s\n\n" name;
3335 generate_prototype ~extern:false ~handle:"handle" name style;
3337 pr "%s\n\n" longdesc;
3338 (match fst style with
3340 pr "This function returns 0 on success or -1 on error.\n\n"
3342 pr "On error this function returns -1.\n\n"
3344 pr "On error this function returns -1.\n\n"
3346 pr "This function returns a C truth value on success or -1 on error.\n\n"
3348 pr "This function returns a string, or NULL on error.
3349 The string is owned by the guest handle and must I<not> be freed.\n\n"
3351 pr "This function returns a string, or NULL on error.
3352 I<The caller must free the returned string after use>.\n\n"
3354 pr "This function returns a NULL-terminated array of strings
3355 (like L<environ(3)>), or NULL if there was an error.
3356 I<The caller must free the strings and the array after use>.\n\n"
3357 | RStruct (_, typ) ->
3358 pr "This function returns a C<struct guestfs_%s *>,
3359 or NULL if there was an error.
3360 I<The caller must call C<guestfs_free_%s> after use>.\n\n" typ typ
3361 | RStructList (_, typ) ->
3362 pr "This function returns a C<struct guestfs_%s_list *>
3363 (see E<lt>guestfs-structs.hE<gt>),
3364 or NULL if there was an error.
3365 I<The caller must call C<guestfs_free_%s_list> after use>.\n\n" typ typ
3367 pr "This function returns a NULL-terminated array of
3368 strings, or NULL if there was an error.
3369 The array of strings will always have length C<2n+1>, where
3370 C<n> keys and values alternate, followed by the trailing NULL entry.
3371 I<The caller must free the strings and the array after use>.\n\n"
3373 if List.mem ProtocolLimitWarning flags then
3374 pr "%s\n\n" protocol_limit_warning;
3375 if List.mem DangerWillRobinson flags then
3376 pr "%s\n\n" danger_will_robinson
3378 ) all_functions_sorted
3380 and generate_structs_pod () =
3381 (* Structs documentation. *)
3384 pr "=head2 guestfs_%s\n" typ;
3386 pr " struct guestfs_%s {\n" typ;
3389 | name, FChar -> pr " char %s;\n" name
3390 | name, FUInt32 -> pr " uint32_t %s;\n" name
3391 | name, FInt32 -> pr " int32_t %s;\n" name
3392 | name, (FUInt64|FBytes) -> pr " uint64_t %s;\n" name
3393 | name, FInt64 -> pr " int64_t %s;\n" name
3394 | name, FString -> pr " char *%s;\n" name
3396 pr " /* The next field is NOT nul-terminated, be careful when printing it: */\n";
3397 pr " char %s[32];\n" name
3398 | name, FOptPercent ->
3399 pr " /* The next field is [0..100] or -1 meaning 'not present': */\n";
3400 pr " float %s;\n" name
3404 pr " struct guestfs_%s_list {\n" typ;
3405 pr " uint32_t len; /* Number of elements in list. */\n";
3406 pr " struct guestfs_%s *val; /* Elements. */\n" typ;
3409 pr " void guestfs_free_%s (struct guestfs_free_%s *);\n" typ typ;
3410 pr " void guestfs_free_%s_list (struct guestfs_free_%s_list *);\n"
3415 (* Generate the protocol (XDR) file, 'guestfs_protocol.x' and
3416 * indirectly 'guestfs_protocol.h' and 'guestfs_protocol.c'.
3418 * We have to use an underscore instead of a dash because otherwise
3419 * rpcgen generates incorrect code.
3421 * This header is NOT exported to clients, but see also generate_structs_h.
3423 and generate_xdr () =
3424 generate_header CStyle LGPLv2;
3426 (* This has to be defined to get around a limitation in Sun's rpcgen. *)
3427 pr "typedef string str<>;\n";
3430 (* Internal structures. *)
3434 pr "struct guestfs_int_%s {\n" typ;
3436 | name, FChar -> pr " char %s;\n" name
3437 | name, FString -> pr " string %s<>;\n" name
3438 | name, FUUID -> pr " opaque %s[32];\n" name
3439 | name, (FInt32|FUInt32) -> pr " int %s;\n" name
3440 | name, (FInt64|FUInt64|FBytes) -> pr " hyper %s;\n" name
3441 | name, FOptPercent -> pr " float %s;\n" name
3445 pr "typedef struct guestfs_int_%s guestfs_int_%s_list<>;\n" typ typ;
3450 fun (shortname, style, _, _, _, _, _) ->
3451 let name = "guestfs_" ^ shortname in
3453 (match snd style with
3456 pr "struct %s_args {\n" name;
3459 | String n -> pr " string %s<>;\n" n
3460 | OptString n -> pr " str *%s;\n" n
3461 | StringList n -> pr " str %s<>;\n" n
3462 | Bool n -> pr " bool %s;\n" n
3463 | Int n -> pr " int %s;\n" n
3464 | FileIn _ | FileOut _ -> ()
3468 (match fst style with
3471 pr "struct %s_ret {\n" name;
3475 pr "struct %s_ret {\n" name;
3476 pr " hyper %s;\n" n;
3479 pr "struct %s_ret {\n" name;
3483 failwithf "RConstString cannot be returned from a daemon function"
3485 pr "struct %s_ret {\n" name;
3486 pr " string %s<>;\n" n;
3489 pr "struct %s_ret {\n" name;
3490 pr " str %s<>;\n" n;
3492 | RStruct (n, typ) ->
3493 pr "struct %s_ret {\n" name;
3494 pr " guestfs_int_%s %s;\n" typ n;
3496 | RStructList (n, typ) ->
3497 pr "struct %s_ret {\n" name;
3498 pr " guestfs_int_%s_list %s;\n" typ n;
3501 pr "struct %s_ret {\n" name;
3502 pr " str %s<>;\n" n;
3507 (* Table of procedure numbers. *)
3508 pr "enum guestfs_procedure {\n";
3510 fun (shortname, _, proc_nr, _, _, _, _) ->
3511 pr " GUESTFS_PROC_%s = %d,\n" (String.uppercase shortname) proc_nr
3513 pr " GUESTFS_PROC_NR_PROCS\n";
3517 (* Having to choose a maximum message size is annoying for several
3518 * reasons (it limits what we can do in the API), but it (a) makes
3519 * the protocol a lot simpler, and (b) provides a bound on the size
3520 * of the daemon which operates in limited memory space. For large
3521 * file transfers you should use FTP.
3523 pr "const GUESTFS_MESSAGE_MAX = %d;\n" (4 * 1024 * 1024);
3526 (* Message header, etc. *)
3528 /* The communication protocol is now documented in the guestfs(3)
3532 const GUESTFS_PROGRAM = 0x2000F5F5;
3533 const GUESTFS_PROTOCOL_VERSION = 1;
3535 /* These constants must be larger than any possible message length. */
3536 const GUESTFS_LAUNCH_FLAG = 0xf5f55ff5;
3537 const GUESTFS_CANCEL_FLAG = 0xffffeeee;
3539 enum guestfs_message_direction {
3540 GUESTFS_DIRECTION_CALL = 0, /* client -> daemon */
3541 GUESTFS_DIRECTION_REPLY = 1 /* daemon -> client */
3544 enum guestfs_message_status {
3545 GUESTFS_STATUS_OK = 0,
3546 GUESTFS_STATUS_ERROR = 1
3549 const GUESTFS_ERROR_LEN = 256;
3551 struct guestfs_message_error {
3552 string error_message<GUESTFS_ERROR_LEN>;
3555 struct guestfs_message_header {
3556 unsigned prog; /* GUESTFS_PROGRAM */
3557 unsigned vers; /* GUESTFS_PROTOCOL_VERSION */
3558 guestfs_procedure proc; /* GUESTFS_PROC_x */
3559 guestfs_message_direction direction;
3560 unsigned serial; /* message serial number */
3561 guestfs_message_status status;
3564 const GUESTFS_MAX_CHUNK_SIZE = 8192;
3566 struct guestfs_chunk {
3567 int cancel; /* if non-zero, transfer is cancelled */
3568 /* data size is 0 bytes if the transfer has finished successfully */
3569 opaque data<GUESTFS_MAX_CHUNK_SIZE>;
3573 (* Generate the guestfs-structs.h file. *)
3574 and generate_structs_h () =
3575 generate_header CStyle LGPLv2;
3577 (* This is a public exported header file containing various
3578 * structures. The structures are carefully written to have
3579 * exactly the same in-memory format as the XDR structures that
3580 * we use on the wire to the daemon. The reason for creating
3581 * copies of these structures here is just so we don't have to
3582 * export the whole of guestfs_protocol.h (which includes much
3583 * unrelated and XDR-dependent stuff that we don't want to be
3584 * public, or required by clients).
3586 * To reiterate, we will pass these structures to and from the
3587 * client with a simple assignment or memcpy, so the format
3588 * must be identical to what rpcgen / the RFC defines.
3591 (* Public structures. *)
3594 pr "struct guestfs_%s {\n" typ;
3597 | name, FChar -> pr " char %s;\n" name
3598 | name, FString -> pr " char *%s;\n" name
3599 | name, FUUID -> pr " char %s[32]; /* this is NOT nul-terminated, be careful when printing */\n" name
3600 | name, FUInt32 -> pr " uint32_t %s;\n" name
3601 | name, FInt32 -> pr " int32_t %s;\n" name
3602 | name, (FUInt64|FBytes) -> pr " uint64_t %s;\n" name
3603 | name, FInt64 -> pr " int64_t %s;\n" name
3604 | name, FOptPercent -> pr " float %s; /* [0..100] or -1 */\n" name
3608 pr "struct guestfs_%s_list {\n" typ;
3609 pr " uint32_t len;\n";
3610 pr " struct guestfs_%s *val;\n" typ;
3613 pr "extern void guestfs_free_%s (struct guestfs_%s *);\n" typ typ;
3614 pr "extern void guestfs_free_%s_list (struct guestfs_%s_list *);\n" typ typ;
3618 (* Generate the guestfs-actions.h file. *)
3619 and generate_actions_h () =
3620 generate_header CStyle LGPLv2;
3622 fun (shortname, style, _, _, _, _, _) ->
3623 let name = "guestfs_" ^ shortname in
3624 generate_prototype ~single_line:true ~newline:true ~handle:"handle"
3628 (* Generate the client-side dispatch stubs. *)
3629 and generate_client_actions () =
3630 generate_header CStyle LGPLv2;
3636 #include \"guestfs.h\"
3637 #include \"guestfs_protocol.h\"
3639 #define error guestfs_error
3640 #define perrorf guestfs_perrorf
3641 #define safe_malloc guestfs_safe_malloc
3642 #define safe_realloc guestfs_safe_realloc
3643 #define safe_strdup guestfs_safe_strdup
3644 #define safe_memdup guestfs_safe_memdup
3646 /* Check the return message from a call for validity. */
3648 check_reply_header (guestfs_h *g,
3649 const struct guestfs_message_header *hdr,
3650 int proc_nr, int serial)
3652 if (hdr->prog != GUESTFS_PROGRAM) {
3653 error (g, \"wrong program (%%d/%%d)\", hdr->prog, GUESTFS_PROGRAM);
3656 if (hdr->vers != GUESTFS_PROTOCOL_VERSION) {
3657 error (g, \"wrong protocol version (%%d/%%d)\",
3658 hdr->vers, GUESTFS_PROTOCOL_VERSION);
3661 if (hdr->direction != GUESTFS_DIRECTION_REPLY) {
3662 error (g, \"unexpected message direction (%%d/%%d)\",
3663 hdr->direction, GUESTFS_DIRECTION_REPLY);
3666 if (hdr->proc != proc_nr) {
3667 error (g, \"unexpected procedure number (%%d/%%d)\", hdr->proc, proc_nr);
3670 if (hdr->serial != serial) {
3671 error (g, \"unexpected serial (%%d/%%d)\", hdr->serial, serial);
3678 /* Check we are in the right state to run a high-level action. */
3680 check_state (guestfs_h *g, const char *caller)
3682 if (!guestfs_is_ready (g)) {
3683 if (guestfs_is_config (g))
3684 error (g, \"%%s: call launch() before using this function\",
3686 else if (guestfs_is_launching (g))
3687 error (g, \"%%s: call wait_ready() before using this function\",
3690 error (g, \"%%s called from the wrong state, %%d != READY\",
3691 caller, guestfs_get_state (g));
3699 (* Client-side stubs for each function. *)
3701 fun (shortname, style, _, _, _, _, _) ->
3702 let name = "guestfs_" ^ shortname in
3704 (* Generate the context struct which stores the high-level
3705 * state between callback functions.
3707 pr "struct %s_ctx {\n" shortname;
3708 pr " /* This flag is set by the callbacks, so we know we've done\n";
3709 pr " * the callbacks as expected, and in the right sequence.\n";
3710 pr " * 0 = not called, 1 = reply_cb called.\n";
3712 pr " int cb_sequence;\n";
3713 pr " struct guestfs_message_header hdr;\n";
3714 pr " struct guestfs_message_error err;\n";
3715 (match fst style with
3718 failwithf "RConstString cannot be returned from a daemon function"
3720 | RBool _ | RString _ | RStringList _
3721 | RStruct _ | RStructList _
3723 pr " struct %s_ret ret;\n" name
3728 (* Generate the reply callback function. *)
3729 pr "static void %s_reply_cb (guestfs_h *g, void *data, XDR *xdr)\n" shortname;
3731 pr " guestfs_main_loop *ml = guestfs_get_main_loop (g);\n";
3732 pr " struct %s_ctx *ctx = (struct %s_ctx *) data;\n" shortname shortname;
3734 pr " /* This should definitely not happen. */\n";
3735 pr " if (ctx->cb_sequence != 0) {\n";
3736 pr " ctx->cb_sequence = 9999;\n";
3737 pr " error (g, \"%%s: internal error: reply callback called twice\", \"%s\");\n" name;
3741 pr " ml->main_loop_quit (ml, g);\n";
3743 pr " if (!xdr_guestfs_message_header (xdr, &ctx->hdr)) {\n";
3744 pr " error (g, \"%%s: failed to parse reply header\", \"%s\");\n" name;
3747 pr " if (ctx->hdr.status == GUESTFS_STATUS_ERROR) {\n";
3748 pr " if (!xdr_guestfs_message_error (xdr, &ctx->err)) {\n";
3749 pr " error (g, \"%%s: failed to parse reply error\", \"%s\");\n"
3756 (match fst style with
3759 failwithf "RConstString cannot be returned from a daemon function"
3761 | RBool _ | RString _ | RStringList _
3762 | RStruct _ | RStructList _
3764 pr " if (!xdr_%s_ret (xdr, &ctx->ret)) {\n" name;
3765 pr " error (g, \"%%s: failed to parse reply\", \"%s\");\n" name;
3771 pr " ctx->cb_sequence = 1;\n";
3774 (* Generate the action stub. *)
3775 generate_prototype ~extern:false ~semicolon:false ~newline:true
3776 ~handle:"g" name style;
3779 match fst style with
3780 | RErr | RInt _ | RInt64 _ | RBool _ -> "-1"
3782 failwithf "RConstString cannot be returned from a daemon function"
3783 | RString _ | RStringList _
3784 | RStruct _ | RStructList _
3790 (match snd style with
3792 | _ -> pr " struct %s_args args;\n" name
3795 pr " struct %s_ctx ctx;\n" shortname;
3796 pr " guestfs_main_loop *ml = guestfs_get_main_loop (g);\n";
3797 pr " int serial;\n";
3799 pr " if (check_state (g, \"%s\") == -1) return %s;\n" name error_code;
3800 pr " guestfs_set_busy (g);\n";
3802 pr " memset (&ctx, 0, sizeof ctx);\n";
3805 (* Send the main header and arguments. *)
3806 (match snd style with
3808 pr " serial = guestfs__send_sync (g, GUESTFS_PROC_%s, NULL, NULL);\n"
3809 (String.uppercase shortname)
3814 pr " args.%s = (char *) %s;\n" n n
3816 pr " args.%s = %s ? (char **) &%s : NULL;\n" n n n
3818 pr " args.%s.%s_val = (char **) %s;\n" n n n;
3819 pr " for (args.%s.%s_len = 0; %s[args.%s.%s_len]; args.%s.%s_len++) ;\n" n n n n n n n;
3821 pr " args.%s = %s;\n" n n
3823 pr " args.%s = %s;\n" n n
3824 | FileIn _ | FileOut _ -> ()
3826 pr " serial = guestfs__send_sync (g, GUESTFS_PROC_%s,\n"
3827 (String.uppercase shortname);
3828 pr " (xdrproc_t) xdr_%s_args, (char *) &args);\n"
3831 pr " if (serial == -1) {\n";
3832 pr " guestfs_end_busy (g);\n";
3833 pr " return %s;\n" error_code;
3837 (* Send any additional files (FileIn) requested. *)
3838 let need_read_reply_label = ref false in
3845 pr " r = guestfs__send_file_sync (g, %s);\n" n;
3846 pr " if (r == -1) {\n";
3847 pr " guestfs_end_busy (g);\n";
3848 pr " return %s;\n" error_code;
3850 pr " if (r == -2) /* daemon cancelled */\n";
3851 pr " goto read_reply;\n";
3852 need_read_reply_label := true;
3858 (* Wait for the reply from the remote end. *)
3859 if !need_read_reply_label then pr " read_reply:\n";
3860 pr " guestfs__switch_to_receiving (g);\n";
3861 pr " ctx.cb_sequence = 0;\n";
3862 pr " guestfs_set_reply_callback (g, %s_reply_cb, &ctx);\n" shortname;
3863 pr " (void) ml->main_loop_run (ml, g);\n";
3864 pr " guestfs_set_reply_callback (g, NULL, NULL);\n";
3865 pr " if (ctx.cb_sequence != 1) {\n";
3866 pr " error (g, \"%%s reply failed, see earlier error messages\", \"%s\");\n" name;
3867 pr " guestfs_end_busy (g);\n";
3868 pr " return %s;\n" error_code;
3872 pr " if (check_reply_header (g, &ctx.hdr, GUESTFS_PROC_%s, serial) == -1) {\n"
3873 (String.uppercase shortname);
3874 pr " guestfs_end_busy (g);\n";
3875 pr " return %s;\n" error_code;
3879 pr " if (ctx.hdr.status == GUESTFS_STATUS_ERROR) {\n";
3880 pr " error (g, \"%%s\", ctx.err.error_message);\n";
3881 pr " free (ctx.err.error_message);\n";
3882 pr " guestfs_end_busy (g);\n";
3883 pr " return %s;\n" error_code;
3887 (* Expecting to receive further files (FileOut)? *)
3891 pr " if (guestfs__receive_file_sync (g, %s) == -1) {\n" n;
3892 pr " guestfs_end_busy (g);\n";
3893 pr " return %s;\n" error_code;
3899 pr " guestfs_end_busy (g);\n";
3901 (match fst style with
3902 | RErr -> pr " return 0;\n"
3903 | RInt n | RInt64 n | RBool n ->
3904 pr " return ctx.ret.%s;\n" n
3906 failwithf "RConstString cannot be returned from a daemon function"
3908 pr " return ctx.ret.%s; /* caller will free */\n" n
3909 | RStringList n | RHashtable n ->
3910 pr " /* caller will free this, but we need to add a NULL entry */\n";
3911 pr " ctx.ret.%s.%s_val =\n" n n;
3912 pr " safe_realloc (g, ctx.ret.%s.%s_val,\n" n n;
3913 pr " sizeof (char *) * (ctx.ret.%s.%s_len + 1));\n"
3915 pr " ctx.ret.%s.%s_val[ctx.ret.%s.%s_len] = NULL;\n" n n n n;
3916 pr " return ctx.ret.%s.%s_val;\n" n n
3918 pr " /* caller will free this */\n";
3919 pr " return safe_memdup (g, &ctx.ret.%s, sizeof (ctx.ret.%s));\n" n n
3920 | RStructList (n, _) ->
3921 pr " /* caller will free this */\n";
3922 pr " return safe_memdup (g, &ctx.ret.%s, sizeof (ctx.ret.%s));\n" n n
3928 (* Functions to free structures. *)
3929 pr "/* Structure-freeing functions. These rely on the fact that the\n";
3930 pr " * structure format is identical to the XDR format. See note in\n";
3931 pr " * generator.ml.\n";
3938 pr "guestfs_free_%s (struct guestfs_%s *x)\n" typ typ;
3940 pr " xdr_free ((xdrproc_t) xdr_guestfs_int_%s, (char *) x);\n" typ;
3946 pr "guestfs_free_%s_list (struct guestfs_%s_list *x)\n" typ typ;
3948 pr " xdr_free ((xdrproc_t) xdr_guestfs_int_%s_list, (char *) x);\n" typ;
3955 (* Generate daemon/actions.h. *)
3956 and generate_daemon_actions_h () =
3957 generate_header CStyle GPLv2;
3959 pr "#include \"../src/guestfs_protocol.h\"\n";
3963 fun (name, style, _, _, _, _, _) ->
3965 ~single_line:true ~newline:true ~in_daemon:true ~prefix:"do_"
3969 (* Generate the server-side stubs. *)
3970 and generate_daemon_actions () =
3971 generate_header CStyle GPLv2;
3973 pr "#include <config.h>\n";
3975 pr "#include <stdio.h>\n";
3976 pr "#include <stdlib.h>\n";
3977 pr "#include <string.h>\n";
3978 pr "#include <inttypes.h>\n";
3979 pr "#include <ctype.h>\n";
3980 pr "#include <rpc/types.h>\n";
3981 pr "#include <rpc/xdr.h>\n";
3983 pr "#include \"daemon.h\"\n";
3984 pr "#include \"../src/guestfs_protocol.h\"\n";
3985 pr "#include \"actions.h\"\n";
3989 fun (name, style, _, _, _, _, _) ->
3990 (* Generate server-side stubs. *)
3991 pr "static void %s_stub (XDR *xdr_in)\n" name;
3994 match fst style with
3995 | RErr | RInt _ -> pr " int r;\n"; "-1"
3996 | RInt64 _ -> pr " int64_t r;\n"; "-1"
3997 | RBool _ -> pr " int r;\n"; "-1"
3999 failwithf "RConstString cannot be returned from a daemon function"
4000 | RString _ -> pr " char *r;\n"; "NULL"
4001 | RStringList _ | RHashtable _ -> pr " char **r;\n"; "NULL"
4002 | RStruct (_, typ) -> pr " guestfs_int_%s *r;\n" typ; "NULL"
4003 | RStructList (_, typ) -> pr " guestfs_int_%s_list *r;\n" typ; "NULL" in
4005 (match snd style with
4008 pr " struct guestfs_%s_args args;\n" name;
4011 (* Note we allow the string to be writable, in order to
4012 * allow device name translation. This is safe because
4013 * we can modify the string (passed from RPC).
4016 | OptString n -> pr " char *%s;\n" n
4017 | StringList n -> pr " char **%s;\n" n
4018 | Bool n -> pr " int %s;\n" n
4019 | Int n -> pr " int %s;\n" n
4020 | FileIn _ | FileOut _ -> ()
4025 (match snd style with
4028 pr " memset (&args, 0, sizeof args);\n";
4030 pr " if (!xdr_guestfs_%s_args (xdr_in, &args)) {\n" name;
4031 pr " reply_with_error (\"%%s: daemon failed to decode procedure arguments\", \"%s\");\n" name;
4036 | String n -> pr " %s = args.%s;\n" n n
4037 | OptString n -> pr " %s = args.%s ? *args.%s : NULL;\n" n n n
4039 pr " %s = realloc (args.%s.%s_val,\n" n n n;
4040 pr " sizeof (char *) * (args.%s.%s_len+1));\n" n n;
4041 pr " if (%s == NULL) {\n" n;
4042 pr " reply_with_perror (\"realloc\");\n";
4045 pr " %s[args.%s.%s_len] = NULL;\n" n n n;
4046 pr " args.%s.%s_val = %s;\n" n n n;
4047 | Bool n -> pr " %s = args.%s;\n" n n
4048 | Int n -> pr " %s = args.%s;\n" n n
4049 | FileIn _ | FileOut _ -> ()
4054 (* Don't want to call the impl with any FileIn or FileOut
4055 * parameters, since these go "outside" the RPC protocol.
4058 List.filter (function FileIn _ | FileOut _ -> false | _ -> true)
4060 pr " r = do_%s " name;
4061 generate_call_args argsnofile;
4064 pr " if (r == %s)\n" error_code;
4065 pr " /* do_%s has already called reply_with_error */\n" name;
4069 (* If there are any FileOut parameters, then the impl must
4070 * send its own reply.
4073 List.exists (function FileOut _ -> true | _ -> false) (snd style) in
4075 pr " /* do_%s has already sent a reply */\n" name
4077 match fst style with
4078 | RErr -> pr " reply (NULL, NULL);\n"
4079 | RInt n | RInt64 n | RBool n ->
4080 pr " struct guestfs_%s_ret ret;\n" name;
4081 pr " ret.%s = r;\n" n;
4082 pr " reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
4085 failwithf "RConstString cannot be returned from a daemon function"
4087 pr " struct guestfs_%s_ret ret;\n" name;
4088 pr " ret.%s = r;\n" n;
4089 pr " reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
4092 | RStringList n | RHashtable n ->
4093 pr " struct guestfs_%s_ret ret;\n" name;
4094 pr " ret.%s.%s_len = count_strings (r);\n" n n;
4095 pr " ret.%s.%s_val = r;\n" n n;
4096 pr " reply ((xdrproc_t) &xdr_guestfs_%s_ret, (char *) &ret);\n"
4098 pr " free_strings (r);\n"
4100 pr " struct guestfs_%s_ret ret;\n" name;
4101 pr " ret.%s = *r;\n" n;
4102 pr " reply ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
4104 pr " xdr_free ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
4106 | RStructList (n, _) ->
4107 pr " struct guestfs_%s_ret ret;\n" name;
4108 pr " ret.%s = *r;\n" n;
4109 pr " reply ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
4111 pr " xdr_free ((xdrproc_t) xdr_guestfs_%s_ret, (char *) &ret);\n"
4115 (* Free the args. *)
4116 (match snd style with
4121 pr " xdr_free ((xdrproc_t) xdr_guestfs_%s_args, (char *) &args);\n"
4128 (* Dispatch function. *)
4129 pr "void dispatch_incoming_message (XDR *xdr_in)\n";
4131 pr " switch (proc_nr) {\n";
4134 fun (name, style, _, _, _, _, _) ->
4135 pr " case GUESTFS_PROC_%s:\n" (String.uppercase name);
4136 pr " %s_stub (xdr_in);\n" name;
4141 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";
4146 (* LVM columns and tokenization functions. *)
4147 (* XXX This generates crap code. We should rethink how we
4153 pr "static const char *lvm_%s_cols = \"%s\";\n"
4154 typ (String.concat "," (List.map fst cols));
4157 pr "static int lvm_tokenize_%s (char *str, guestfs_int_lvm_%s *r)\n" typ typ;
4159 pr " char *tok, *p, *next;\n";
4163 pr " fprintf (stderr, \"%%s: <<%%s>>\\n\", __func__, str);\n";
4166 pr " if (!str) {\n";
4167 pr " fprintf (stderr, \"%%s: failed: passed a NULL string\\n\", __func__);\n";
4170 pr " if (!*str || isspace (*str)) {\n";
4171 pr " fprintf (stderr, \"%%s: failed: passed a empty string or one beginning with whitespace\\n\", __func__);\n";
4176 fun (name, coltype) ->
4177 pr " if (!tok) {\n";
4178 pr " fprintf (stderr, \"%%s: failed: string finished early, around token %%s\\n\", __func__, \"%s\");\n" name;
4181 pr " p = strchrnul (tok, ',');\n";
4182 pr " if (*p) next = p+1; else next = NULL;\n";
4183 pr " *p = '\\0';\n";
4186 pr " r->%s = strdup (tok);\n" name;
4187 pr " if (r->%s == NULL) {\n" name;
4188 pr " perror (\"strdup\");\n";
4192 pr " for (i = j = 0; i < 32; ++j) {\n";
4193 pr " if (tok[j] == '\\0') {\n";
4194 pr " fprintf (stderr, \"%%s: failed to parse UUID from '%%s'\\n\", __func__, tok);\n";
4196 pr " } else if (tok[j] != '-')\n";
4197 pr " r->%s[i++] = tok[j];\n" name;
4200 pr " if (sscanf (tok, \"%%\"SCNu64, &r->%s) != 1) {\n" name;
4201 pr " fprintf (stderr, \"%%s: failed to parse size '%%s' from token %%s\\n\", __func__, tok, \"%s\");\n" name;
4205 pr " if (sscanf (tok, \"%%\"SCNi64, &r->%s) != 1) {\n" name;
4206 pr " fprintf (stderr, \"%%s: failed to parse int '%%s' from token %%s\\n\", __func__, tok, \"%s\");\n" name;
4210 pr " if (tok[0] == '\\0')\n";
4211 pr " r->%s = -1;\n" name;
4212 pr " else if (sscanf (tok, \"%%f\", &r->%s) != 1) {\n" name;
4213 pr " fprintf (stderr, \"%%s: failed to parse float '%%s' from token %%s\\n\", __func__, tok, \"%s\");\n" name;
4216 | FInt32 | FUInt32 | FUInt64 | FChar ->
4217 assert false (* can never be an LVM column *)
4219 pr " tok = next;\n";
4222 pr " if (tok != NULL) {\n";
4223 pr " fprintf (stderr, \"%%s: failed: extra tokens at end of string\\n\", __func__);\n";
4230 pr "guestfs_int_lvm_%s_list *\n" typ;
4231 pr "parse_command_line_%ss (void)\n" typ;
4233 pr " char *out, *err;\n";
4234 pr " char *p, *pend;\n";
4236 pr " guestfs_int_lvm_%s_list *ret;\n" typ;
4237 pr " void *newp;\n";
4239 pr " ret = malloc (sizeof *ret);\n";
4240 pr " if (!ret) {\n";
4241 pr " reply_with_perror (\"malloc\");\n";
4242 pr " return NULL;\n";
4245 pr " ret->guestfs_int_lvm_%s_list_len = 0;\n" typ;
4246 pr " ret->guestfs_int_lvm_%s_list_val = NULL;\n" typ;
4248 pr " r = command (&out, &err,\n";
4249 pr " \"/sbin/lvm\", \"%ss\",\n" typ;
4250 pr " \"-o\", lvm_%s_cols, \"--unbuffered\", \"--noheadings\",\n" typ;
4251 pr " \"--nosuffix\", \"--separator\", \",\", \"--units\", \"b\", NULL);\n";
4252 pr " if (r == -1) {\n";
4253 pr " reply_with_error (\"%%s\", err);\n";
4254 pr " free (out);\n";
4255 pr " free (err);\n";
4256 pr " free (ret);\n";
4257 pr " return NULL;\n";
4260 pr " free (err);\n";
4262 pr " /* Tokenize each line of the output. */\n";
4265 pr " while (p) {\n";
4266 pr " pend = strchr (p, '\\n'); /* Get the next line of output. */\n";
4267 pr " if (pend) {\n";
4268 pr " *pend = '\\0';\n";
4272 pr " while (*p && isspace (*p)) /* Skip any leading whitespace. */\n";
4275 pr " if (!*p) { /* Empty line? Skip it. */\n";
4280 pr " /* Allocate some space to store this next entry. */\n";
4281 pr " newp = realloc (ret->guestfs_int_lvm_%s_list_val,\n" typ;
4282 pr " sizeof (guestfs_int_lvm_%s) * (i+1));\n" typ;
4283 pr " if (newp == NULL) {\n";
4284 pr " reply_with_perror (\"realloc\");\n";
4285 pr " free (ret->guestfs_int_lvm_%s_list_val);\n" typ;
4286 pr " free (ret);\n";
4287 pr " free (out);\n";
4288 pr " return NULL;\n";
4290 pr " ret->guestfs_int_lvm_%s_list_val = newp;\n" typ;
4292 pr " /* Tokenize the next entry. */\n";
4293 pr " r = lvm_tokenize_%s (p, &ret->guestfs_int_lvm_%s_list_val[i]);\n" typ typ;
4294 pr " if (r == -1) {\n";
4295 pr " reply_with_error (\"failed to parse output of '%ss' command\");\n" typ;
4296 pr " free (ret->guestfs_int_lvm_%s_list_val);\n" typ;
4297 pr " free (ret);\n";
4298 pr " free (out);\n";
4299 pr " return NULL;\n";
4306 pr " ret->guestfs_int_lvm_%s_list_len = i;\n" typ;
4308 pr " free (out);\n";
4309 pr " return ret;\n";
4312 ) ["pv", lvm_pv_cols; "vg", lvm_vg_cols; "lv", lvm_lv_cols]
4314 (* Generate a list of function names, for debugging in the daemon.. *)
4315 and generate_daemon_names () =
4316 generate_header CStyle GPLv2;
4318 pr "#include <config.h>\n";
4320 pr "#include \"daemon.h\"\n";
4323 pr "/* This array is indexed by proc_nr. See guestfs_protocol.x. */\n";
4324 pr "const char *function_names[] = {\n";
4326 fun (name, _, proc_nr, _, _, _, _) -> pr " [%d] = \"%s\",\n" proc_nr name
4330 (* Generate the tests. *)
4331 and generate_tests () =
4332 generate_header CStyle GPLv2;
4339 #include <sys/types.h>
4342 #include \"guestfs.h\"
4344 static guestfs_h *g;
4345 static int suppress_error = 0;
4347 static void print_error (guestfs_h *g, void *data, const char *msg)
4349 if (!suppress_error)
4350 fprintf (stderr, \"%%s\\n\", msg);
4353 static void print_strings (char * const * const argv)
4357 for (argc = 0; argv[argc] != NULL; ++argc)
4358 printf (\"\\t%%s\\n\", argv[argc]);
4362 static void print_table (char * const * const argv)
4366 for (i = 0; argv[i] != NULL; i += 2)
4367 printf (\"%%s: %%s\\n\", argv[i], argv[i+1]);
4371 static void no_test_warnings (void)
4377 | name, _, _, _, [], _, _ ->
4378 pr " fprintf (stderr, \"warning: \\\"guestfs_%s\\\" has no tests\\n\");\n" name
4379 | name, _, _, _, tests, _, _ -> ()
4385 (* Generate the actual tests. Note that we generate the tests
4386 * in reverse order, deliberately, so that (in general) the
4387 * newest tests run first. This makes it quicker and easier to
4392 fun (name, _, _, _, tests, _, _) ->
4393 mapi (generate_one_test name) tests
4394 ) (List.rev all_functions) in
4395 let test_names = List.concat test_names in
4396 let nr_tests = List.length test_names in
4399 int main (int argc, char *argv[])
4403 const char *filename;
4405 int nr_tests, test_num = 0;
4407 setbuf (stdout, NULL);
4409 no_test_warnings ();
4411 g = guestfs_create ();
4413 printf (\"guestfs_create FAILED\\n\");
4417 guestfs_set_error_handler (g, print_error, NULL);
4419 guestfs_set_path (g, \"../appliance\");
4421 filename = \"test1.img\";
4422 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
4427 if (lseek (fd, %d, SEEK_SET) == -1) {
4433 if (write (fd, &c, 1) == -1) {
4439 if (close (fd) == -1) {
4444 if (guestfs_add_drive (g, filename) == -1) {
4445 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
4449 filename = \"test2.img\";
4450 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
4455 if (lseek (fd, %d, SEEK_SET) == -1) {
4461 if (write (fd, &c, 1) == -1) {
4467 if (close (fd) == -1) {
4472 if (guestfs_add_drive (g, filename) == -1) {
4473 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
4477 filename = \"test3.img\";
4478 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666);
4483 if (lseek (fd, %d, SEEK_SET) == -1) {
4489 if (write (fd, &c, 1) == -1) {
4495 if (close (fd) == -1) {
4500 if (guestfs_add_drive (g, filename) == -1) {
4501 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
4505 if (guestfs_add_drive_ro (g, \"../images/test.sqsh\") == -1) {
4506 printf (\"guestfs_add_drive_ro ../images/test.sqsh FAILED\\n\");
4510 if (guestfs_launch (g) == -1) {
4511 printf (\"guestfs_launch FAILED\\n\");
4515 /* Set a timeout in case qemu hangs during launch (RHBZ#505329). */
4518 if (guestfs_wait_ready (g) == -1) {
4519 printf (\"guestfs_wait_ready FAILED\\n\");
4523 /* Cancel previous alarm. */
4528 " (500 * 1024 * 1024) (50 * 1024 * 1024) (10 * 1024 * 1024) nr_tests;
4532 pr " test_num++;\n";
4533 pr " printf (\"%%3d/%%3d %s\\n\", test_num, nr_tests);\n" test_name;
4534 pr " if (%s () == -1) {\n" test_name;
4535 pr " printf (\"%s FAILED\\n\");\n" test_name;
4541 pr " guestfs_close (g);\n";
4542 pr " unlink (\"test1.img\");\n";
4543 pr " unlink (\"test2.img\");\n";
4544 pr " unlink (\"test3.img\");\n";
4547 pr " if (failed > 0) {\n";
4548 pr " printf (\"***** %%d / %%d tests FAILED *****\\n\", failed, nr_tests);\n";
4556 and generate_one_test name i (init, prereq, test) =
4557 let test_name = sprintf "test_%s_%d" name i in
4560 static int %s_skip (void)
4564 str = getenv (\"TEST_ONLY\");
4566 return strstr (str, \"%s\") == NULL;
4567 str = getenv (\"SKIP_%s\");
4568 if (str && strcmp (str, \"1\") == 0) return 1;
4569 str = getenv (\"SKIP_TEST_%s\");
4570 if (str && strcmp (str, \"1\") == 0) return 1;
4574 " test_name name (String.uppercase test_name) (String.uppercase name);
4577 | Disabled | Always -> ()
4578 | If code | Unless code ->
4579 pr "static int %s_prereq (void)\n" test_name;
4587 static int %s (void)
4590 printf (\" %%s skipped (reason: environment variable set)\\n\", \"%s\");
4594 " test_name test_name test_name;
4598 pr " printf (\" %%s skipped (reason: test disabled in generator)\\n\", \"%s\");\n" test_name
4600 pr " if (! %s_prereq ()) {\n" test_name;
4601 pr " printf (\" %%s skipped (reason: test prerequisite)\\n\", \"%s\");\n" test_name;
4605 generate_one_test_body name i test_name init test;
4607 pr " if (%s_prereq ()) {\n" test_name;
4608 pr " printf (\" %%s skipped (reason: test prerequisite)\\n\", \"%s\");\n" test_name;
4612 generate_one_test_body name i test_name init test;
4614 generate_one_test_body name i test_name init test
4622 and generate_one_test_body name i test_name init test =
4626 pr " /* InitNone|InitEmpty for %s */\n" test_name;
4627 List.iter (generate_test_command_call test_name)
4628 [["blockdev_setrw"; "/dev/sda"];
4632 pr " /* InitBasicFS for %s: create ext2 on /dev/sda1 */\n" test_name;
4633 List.iter (generate_test_command_call test_name)
4634 [["blockdev_setrw"; "/dev/sda"];
4637 ["sfdiskM"; "/dev/sda"; ","];
4638 ["mkfs"; "ext2"; "/dev/sda1"];
4639 ["mount"; "/dev/sda1"; "/"]]
4640 | InitBasicFSonLVM ->
4641 pr " /* InitBasicFSonLVM for %s: create ext2 on /dev/VG/LV */\n"
4643 List.iter (generate_test_command_call test_name)
4644 [["blockdev_setrw"; "/dev/sda"];
4647 ["sfdiskM"; "/dev/sda"; ","];
4648 ["pvcreate"; "/dev/sda1"];
4649 ["vgcreate"; "VG"; "/dev/sda1"];
4650 ["lvcreate"; "LV"; "VG"; "8"];
4651 ["mkfs"; "ext2"; "/dev/VG/LV"];
4652 ["mount"; "/dev/VG/LV"; "/"]]
4655 let get_seq_last = function
4657 failwithf "%s: you cannot use [] (empty list) when expecting a command"
4660 let seq = List.rev seq in
4661 List.rev (List.tl seq), List.hd seq
4666 pr " /* TestRun for %s (%d) */\n" name i;
4667 List.iter (generate_test_command_call test_name) seq
4668 | TestOutput (seq, expected) ->
4669 pr " /* TestOutput for %s (%d) */\n" name i;
4670 pr " const char *expected = \"%s\";\n" (c_quote expected);
4671 let seq, last = get_seq_last seq in
4673 pr " if (strcmp (r, expected) != 0) {\n";
4674 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name;
4678 List.iter (generate_test_command_call test_name) seq;
4679 generate_test_command_call ~test test_name last
4680 | TestOutputList (seq, expected) ->
4681 pr " /* TestOutputList for %s (%d) */\n" name i;
4682 let seq, last = get_seq_last seq in
4686 pr " if (!r[%d]) {\n" i;
4687 pr " fprintf (stderr, \"%s: short list returned from command\\n\");\n" test_name;
4688 pr " print_strings (r);\n";
4692 pr " const char *expected = \"%s\";\n" (c_quote str);
4693 pr " if (strcmp (r[%d], expected) != 0) {\n" i;
4694 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r[%d]);\n" test_name i;
4699 pr " if (r[%d] != NULL) {\n" (List.length expected);
4700 pr " fprintf (stderr, \"%s: extra elements returned from command\\n\");\n"
4702 pr " print_strings (r);\n";
4706 List.iter (generate_test_command_call test_name) seq;
4707 generate_test_command_call ~test test_name last
4708 | TestOutputListOfDevices (seq, expected) ->
4709 pr " /* TestOutputListOfDevices for %s (%d) */\n" name i;
4710 let seq, last = get_seq_last seq in
4714 pr " if (!r[%d]) {\n" i;
4715 pr " fprintf (stderr, \"%s: short list returned from command\\n\");\n" test_name;
4716 pr " print_strings (r);\n";
4720 pr " const char *expected = \"%s\";\n" (c_quote str);
4721 pr " r[%d][5] = 's';\n" i;
4722 pr " if (strcmp (r[%d], expected) != 0) {\n" i;
4723 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r[%d]);\n" test_name i;
4728 pr " if (r[%d] != NULL) {\n" (List.length expected);
4729 pr " fprintf (stderr, \"%s: extra elements returned from command\\n\");\n"
4731 pr " print_strings (r);\n";
4735 List.iter (generate_test_command_call test_name) seq;
4736 generate_test_command_call ~test test_name last
4737 | TestOutputInt (seq, expected) ->
4738 pr " /* TestOutputInt for %s (%d) */\n" name i;
4739 let seq, last = get_seq_last seq in
4741 pr " if (r != %d) {\n" expected;
4742 pr " fprintf (stderr, \"%s: expected %d but got %%d\\n\","
4748 List.iter (generate_test_command_call test_name) seq;
4749 generate_test_command_call ~test test_name last
4750 | TestOutputTrue seq ->
4751 pr " /* TestOutputTrue for %s (%d) */\n" name i;
4752 let seq, last = get_seq_last seq in
4755 pr " fprintf (stderr, \"%s: expected true, got false\\n\");\n"
4760 List.iter (generate_test_command_call test_name) seq;
4761 generate_test_command_call ~test test_name last
4762 | TestOutputFalse seq ->
4763 pr " /* TestOutputFalse for %s (%d) */\n" name i;
4764 let seq, last = get_seq_last seq in
4767 pr " fprintf (stderr, \"%s: expected false, got true\\n\");\n"
4772 List.iter (generate_test_command_call test_name) seq;
4773 generate_test_command_call ~test test_name last
4774 | TestOutputLength (seq, expected) ->
4775 pr " /* TestOutputLength for %s (%d) */\n" name i;
4776 let seq, last = get_seq_last seq in
4779 pr " for (j = 0; j < %d; ++j)\n" expected;
4780 pr " if (r[j] == NULL) {\n";
4781 pr " fprintf (stderr, \"%s: short list returned\\n\");\n"
4783 pr " print_strings (r);\n";
4786 pr " if (r[j] != NULL) {\n";
4787 pr " fprintf (stderr, \"%s: long list returned\\n\");\n"
4789 pr " print_strings (r);\n";
4793 List.iter (generate_test_command_call test_name) seq;
4794 generate_test_command_call ~test test_name last
4795 | TestOutputStruct (seq, checks) ->
4796 pr " /* TestOutputStruct for %s (%d) */\n" name i;
4797 let seq, last = get_seq_last seq in
4801 | CompareWithInt (field, expected) ->
4802 pr " if (r->%s != %d) {\n" field expected;
4803 pr " fprintf (stderr, \"%s: %s was %%d, expected %d\\n\",\n"
4804 test_name field expected;
4805 pr " (int) r->%s);\n" field;
4808 | CompareWithString (field, expected) ->
4809 pr " if (strcmp (r->%s, \"%s\") != 0) {\n" field expected;
4810 pr " fprintf (stderr, \"%s: %s was \"%%s\", expected \"%s\"\\n\",\n"
4811 test_name field expected;
4812 pr " r->%s);\n" field;
4815 | CompareFieldsIntEq (field1, field2) ->
4816 pr " if (r->%s != r->%s) {\n" field1 field2;
4817 pr " fprintf (stderr, \"%s: %s (%%d) <> %s (%%d)\\n\",\n"
4818 test_name field1 field2;
4819 pr " (int) r->%s, (int) r->%s);\n" field1 field2;
4822 | CompareFieldsStrEq (field1, field2) ->
4823 pr " if (strcmp (r->%s, r->%s) != 0) {\n" field1 field2;
4824 pr " fprintf (stderr, \"%s: %s (\"%%s\") <> %s (\"%%s\")\\n\",\n"
4825 test_name field1 field2;
4826 pr " r->%s, r->%s);\n" field1 field2;
4831 List.iter (generate_test_command_call test_name) seq;
4832 generate_test_command_call ~test test_name last
4833 | TestLastFail seq ->
4834 pr " /* TestLastFail for %s (%d) */\n" name i;
4835 let seq, last = get_seq_last seq in
4836 List.iter (generate_test_command_call test_name) seq;
4837 generate_test_command_call test_name ~expect_error:true last
4839 (* Generate the code to run a command, leaving the result in 'r'.
4840 * If you expect to get an error then you should set expect_error:true.
4842 and generate_test_command_call ?(expect_error = false) ?test test_name cmd =
4844 | [] -> assert false
4846 (* Look up the command to find out what args/ret it has. *)
4849 let _, style, _, _, _, _, _ =
4850 List.find (fun (n, _, _, _, _, _, _) -> n = name) all_functions in
4853 failwithf "%s: in test, command %s was not found" test_name name in
4855 if List.length (snd style) <> List.length args then
4856 failwithf "%s: in test, wrong number of args given to %s"
4863 | OptString n, "NULL" -> ()
4865 | OptString n, arg ->
4866 pr " const char *%s = \"%s\";\n" n (c_quote arg);
4869 | FileIn _, _ | FileOut _, _ -> ()
4870 | StringList n, arg ->
4871 let strs = string_split " " arg in
4874 pr " const char *%s_%d = \"%s\";\n" n i (c_quote str);
4876 pr " const char *%s[] = {\n" n;
4878 fun i _ -> pr " %s_%d,\n" n i
4882 ) (List.combine (snd style) args);
4885 match fst style with
4886 | RErr | RInt _ | RBool _ -> pr " int r;\n"; "-1"
4887 | RInt64 _ -> pr " int64_t r;\n"; "-1"
4888 | RConstString _ -> pr " const char *r;\n"; "NULL"
4889 | RString _ -> pr " char *r;\n"; "NULL"
4890 | RStringList _ | RHashtable _ ->
4894 | RStruct (_, typ) ->
4895 pr " struct guestfs_%s *r;\n" typ; "NULL"
4896 | RStructList (_, typ) ->
4897 pr " struct guestfs_%s_list *r;\n" typ; "NULL" in
4899 pr " suppress_error = %d;\n" (if expect_error then 1 else 0);
4900 pr " r = guestfs_%s (g" name;
4902 (* Generate the parameters. *)
4905 | OptString _, "NULL" -> pr ", NULL"
4909 | FileIn _, arg | FileOut _, arg ->
4910 pr ", \"%s\"" (c_quote arg)
4911 | StringList n, _ ->
4915 try int_of_string arg
4916 with Failure "int_of_string" ->
4917 failwithf "%s: expecting an int, but got '%s'" test_name arg in
4920 let b = bool_of_string arg in pr ", %d" (if b then 1 else 0)
4921 ) (List.combine (snd style) args);
4924 if not expect_error then
4925 pr " if (r == %s)\n" error_code
4927 pr " if (r != %s)\n" error_code;
4930 (* Insert the test code. *)
4936 (match fst style with
4937 | RErr | RInt _ | RInt64 _ | RBool _ | RConstString _ -> ()
4938 | RString _ -> pr " free (r);\n"
4939 | RStringList _ | RHashtable _ ->
4940 pr " for (i = 0; r[i] != NULL; ++i)\n";
4941 pr " free (r[i]);\n";
4943 | RStruct (_, typ) ->
4944 pr " guestfs_free_%s (r);\n" typ
4945 | RStructList (_, typ) ->
4946 pr " guestfs_free_%s_list (r);\n" typ
4952 let str = replace_str str "\r" "\\r" in
4953 let str = replace_str str "\n" "\\n" in
4954 let str = replace_str str "\t" "\\t" in
4955 let str = replace_str str "\000" "\\0" in
4958 (* Generate a lot of different functions for guestfish. *)
4959 and generate_fish_cmds () =
4960 generate_header CStyle GPLv2;
4964 fun (_, _, _, flags, _, _, _) -> not (List.mem NotInFish flags)
4966 let all_functions_sorted =
4968 fun (_, _, _, flags, _, _, _) -> not (List.mem NotInFish flags)
4969 ) all_functions_sorted in
4971 pr "#include <stdio.h>\n";
4972 pr "#include <stdlib.h>\n";
4973 pr "#include <string.h>\n";
4974 pr "#include <inttypes.h>\n";
4976 pr "#include <guestfs.h>\n";
4977 pr "#include \"fish.h\"\n";
4980 (* list_commands function, which implements guestfish -h *)
4981 pr "void list_commands (void)\n";
4983 pr " printf (\" %%-16s %%s\\n\", \"Command\", \"Description\");\n";
4984 pr " list_builtin_commands ();\n";
4986 fun (name, _, _, flags, _, shortdesc, _) ->
4987 let name = replace_char name '_' '-' in
4988 pr " printf (\"%%-20s %%s\\n\", \"%s\", \"%s\");\n"
4990 ) all_functions_sorted;
4991 pr " printf (\" Use -h <cmd> / help <cmd> to show detailed help for a command.\\n\");\n";
4995 (* display_command function, which implements guestfish -h cmd *)
4996 pr "void display_command (const char *cmd)\n";
4999 fun (name, style, _, flags, _, shortdesc, longdesc) ->
5000 let name2 = replace_char name '_' '-' in
5002 try find_map (function FishAlias n -> Some n | _ -> None) flags
5003 with Not_found -> name in
5004 let longdesc = replace_str longdesc "C<guestfs_" "C<" in
5006 match snd style with
5010 name2 (String.concat "> <" (List.map name_of_argt args)) in
5013 if List.mem ProtocolLimitWarning flags then
5014 ("\n\n" ^ protocol_limit_warning)
5017 (* For DangerWillRobinson commands, we should probably have
5018 * guestfish prompt before allowing you to use them (especially
5019 * in interactive mode). XXX
5023 if List.mem DangerWillRobinson flags then
5024 ("\n\n" ^ danger_will_robinson)
5027 let describe_alias =
5028 if name <> alias then
5029 sprintf "\n\nYou can use '%s' as an alias for this command." alias
5033 pr "strcasecmp (cmd, \"%s\") == 0" name;
5034 if name <> name2 then
5035 pr " || strcasecmp (cmd, \"%s\") == 0" name2;
5036 if name <> alias then
5037 pr " || strcasecmp (cmd, \"%s\") == 0" alias;
5039 pr " pod2text (\"%s - %s\", %S);\n"
5041 (" " ^ synopsis ^ "\n\n" ^ longdesc ^ warnings ^ describe_alias);
5044 pr " display_builtin_command (cmd);\n";
5048 (* print_* functions *)
5052 List.exists (function (_, FUUID) -> true | _ -> false) cols in
5054 pr "static void print_%s (struct guestfs_%s *%s)\n" typ typ typ;
5063 pr " printf (\"%s: %%s\\n\", %s->%s);\n" name typ name
5065 pr " printf (\"%s: \");\n" name;
5066 pr " for (i = 0; i < 32; ++i)\n";
5067 pr " printf (\"%%c\", %s->%s[i]);\n" typ name;
5068 pr " printf (\"\\n\");\n"
5069 | name, (FUInt64|FBytes) ->
5070 pr " printf (\"%s: %%\" PRIu64 \"\\n\", %s->%s);\n" name typ name
5072 pr " printf (\"%s: %%\" PRIi64 \"\\n\", %s->%s);\n" name typ name
5074 pr " printf (\"%s: %%\" PRIu32 \"\\n\", %s->%s);\n" name typ name
5076 pr " printf (\"%s: %%\" PRIi32 \"\\n\", %s->%s);\n" name typ name
5078 pr " printf (\"%s: %%c\\n\", %s->%s);\n" name typ name
5079 | name, FOptPercent ->
5080 pr " if (%s->%s >= 0) printf (\"%s: %%g %%%%\\n\", %s->%s);\n"
5081 typ name name typ name;
5082 pr " else printf (\"%s: \\n\");\n" name
5086 pr "static void print_%s_list (struct guestfs_%s_list *%ss)\n"
5091 pr " for (i = 0; i < %ss->len; ++i)\n" typ;
5092 pr " print_%s (&%ss->val[i]);\n" typ typ;
5097 (* run_<action> actions *)
5099 fun (name, style, _, flags, _, _, _) ->
5100 pr "static int run_%s (const char *cmd, int argc, char *argv[])\n" name;
5102 (match fst style with
5105 | RBool _ -> pr " int r;\n"
5106 | RInt64 _ -> pr " int64_t r;\n"
5107 | RConstString _ -> pr " const char *r;\n"
5108 | RString _ -> pr " char *r;\n"
5109 | RStringList _ | RHashtable _ -> pr " char **r;\n"
5110 | RStruct (_, typ) -> pr " struct guestfs_%s *r;\n" typ
5111 | RStructList (_, typ) -> pr " struct guestfs_%s_list *r;\n" typ
5118 | FileOut n -> pr " const char *%s;\n" n
5119 | StringList n -> pr " char **%s;\n" n
5120 | Bool n -> pr " int %s;\n" n
5121 | Int n -> pr " int %s;\n" n
5124 (* Check and convert parameters. *)
5125 let argc_expected = List.length (snd style) in
5126 pr " if (argc != %d) {\n" argc_expected;
5127 pr " fprintf (stderr, \"%%s should have %d parameter(s)\\n\", cmd);\n"
5129 pr " fprintf (stderr, \"type 'help %%s' for help on %%s\\n\", cmd, cmd);\n";
5135 | String name -> pr " %s = argv[%d];\n" name i
5137 pr " %s = strcmp (argv[%d], \"\") != 0 ? argv[%d] : NULL;\n"
5140 pr " %s = strcmp (argv[%d], \"-\") != 0 ? argv[%d] : \"/dev/stdin\";\n"
5143 pr " %s = strcmp (argv[%d], \"-\") != 0 ? argv[%d] : \"/dev/stdout\";\n"
5145 | StringList name ->
5146 pr " %s = parse_string_list (argv[%d]);\n" name i
5148 pr " %s = is_true (argv[%d]) ? 1 : 0;\n" name i
5150 pr " %s = atoi (argv[%d]);\n" name i
5153 (* Call C API function. *)
5155 try find_map (function FishAction n -> Some n | _ -> None) flags
5156 with Not_found -> sprintf "guestfs_%s" name in
5158 generate_call_args ~handle:"g" (snd style);
5161 (* Check return value for errors and display command results. *)
5162 (match fst style with
5163 | RErr -> pr " return r;\n"
5165 pr " if (r == -1) return -1;\n";
5166 pr " printf (\"%%d\\n\", r);\n";
5169 pr " if (r == -1) return -1;\n";
5170 pr " printf (\"%%\" PRIi64 \"\\n\", r);\n";
5173 pr " if (r == -1) return -1;\n";
5174 pr " if (r) printf (\"true\\n\"); else printf (\"false\\n\");\n";
5177 pr " if (r == NULL) return -1;\n";
5178 pr " printf (\"%%s\\n\", r);\n";
5181 pr " if (r == NULL) return -1;\n";
5182 pr " printf (\"%%s\\n\", r);\n";
5186 pr " if (r == NULL) return -1;\n";
5187 pr " print_strings (r);\n";
5188 pr " free_strings (r);\n";
5190 | RStruct (_, typ) ->
5191 pr " if (r == NULL) return -1;\n";
5192 pr " print_%s (r);\n" typ;
5193 pr " guestfs_free_%s (r);\n" typ;
5195 | RStructList (_, typ) ->
5196 pr " if (r == NULL) return -1;\n";
5197 pr " print_%s_list (r);\n" typ;
5198 pr " guestfs_free_%s_list (r);\n" typ;
5201 pr " if (r == NULL) return -1;\n";
5202 pr " print_table (r);\n";
5203 pr " free_strings (r);\n";
5210 (* run_action function *)
5211 pr "int run_action (const char *cmd, int argc, char *argv[])\n";
5214 fun (name, _, _, flags, _, _, _) ->
5215 let name2 = replace_char name '_' '-' in
5217 try find_map (function FishAlias n -> Some n | _ -> None) flags
5218 with Not_found -> name in
5220 pr "strcasecmp (cmd, \"%s\") == 0" name;
5221 if name <> name2 then
5222 pr " || strcasecmp (cmd, \"%s\") == 0" name2;
5223 if name <> alias then
5224 pr " || strcasecmp (cmd, \"%s\") == 0" alias;
5226 pr " return run_%s (cmd, argc, argv);\n" name;
5230 pr " fprintf (stderr, \"%%s: unknown command\\n\", cmd);\n";
5237 (* Readline completion for guestfish. *)
5238 and generate_fish_completion () =
5239 generate_header CStyle GPLv2;
5243 fun (_, _, _, flags, _, _, _) -> not (List.mem NotInFish flags)
5253 #ifdef HAVE_LIBREADLINE
5254 #include <readline/readline.h>
5259 #ifdef HAVE_LIBREADLINE
5261 static const char *const commands[] = {
5262 BUILTIN_COMMANDS_FOR_COMPLETION,
5265 (* Get the commands, including the aliases. They don't need to be
5266 * sorted - the generator() function just does a dumb linear search.
5270 fun (name, _, _, flags, _, _, _) ->
5271 let name2 = replace_char name '_' '-' in
5273 try find_map (function FishAlias n -> Some n | _ -> None) flags
5274 with Not_found -> name in
5276 if name <> alias then [name2; alias] else [name2]
5278 let commands = List.flatten commands in
5280 List.iter (pr " \"%s\",\n") commands;
5286 generator (const char *text, int state)
5288 static int index, len;
5293 len = strlen (text);
5296 rl_attempted_completion_over = 1;
5298 while ((name = commands[index]) != NULL) {
5300 if (strncasecmp (name, text, len) == 0)
5301 return strdup (name);
5307 #endif /* HAVE_LIBREADLINE */
5309 char **do_completion (const char *text, int start, int end)
5311 char **matches = NULL;
5313 #ifdef HAVE_LIBREADLINE
5314 rl_completion_append_character = ' ';
5317 matches = rl_completion_matches (text, generator);
5318 else if (complete_dest_paths)
5319 matches = rl_completion_matches (text, complete_dest_paths_generator);
5326 (* Generate the POD documentation for guestfish. *)
5327 and generate_fish_actions_pod () =
5328 let all_functions_sorted =
5330 fun (_, _, _, flags, _, _, _) ->
5331 not (List.mem NotInFish flags || List.mem NotInDocs flags)
5332 ) all_functions_sorted in
5334 let rex = Str.regexp "C<guestfs_\\([^>]+\\)>" in
5337 fun (name, style, _, flags, _, _, longdesc) ->
5339 Str.global_substitute rex (
5342 try Str.matched_group 1 s
5344 failwithf "error substituting C<guestfs_...> in longdesc of function %s" name in
5345 "C<" ^ replace_char sub '_' '-' ^ ">"
5347 let name = replace_char name '_' '-' in
5349 try find_map (function FishAlias n -> Some n | _ -> None) flags
5350 with Not_found -> name in
5352 pr "=head2 %s" name;
5353 if name <> alias then
5360 | String n -> pr " %s" n
5361 | OptString n -> pr " %s" n
5362 | StringList n -> pr " '%s ...'" n
5363 | Bool _ -> pr " true|false"
5364 | Int n -> pr " %s" n
5365 | FileIn n | FileOut n -> pr " (%s|-)" n
5369 pr "%s\n\n" longdesc;
5371 if List.exists (function FileIn _ | FileOut _ -> true
5372 | _ -> false) (snd style) then
5373 pr "Use C<-> instead of a filename to read/write from stdin/stdout.\n\n";
5375 if List.mem ProtocolLimitWarning flags then
5376 pr "%s\n\n" protocol_limit_warning;
5378 if List.mem DangerWillRobinson flags then
5379 pr "%s\n\n" danger_will_robinson
5380 ) all_functions_sorted
5382 (* Generate a C function prototype. *)
5383 and generate_prototype ?(extern = true) ?(static = false) ?(semicolon = true)
5384 ?(single_line = false) ?(newline = false) ?(in_daemon = false)
5386 ?handle name style =
5387 if extern then pr "extern ";
5388 if static then pr "static ";
5389 (match fst style with
5391 | RInt _ -> pr "int "
5392 | RInt64 _ -> pr "int64_t "
5393 | RBool _ -> pr "int "
5394 | RConstString _ -> pr "const char *"
5395 | RString _ -> pr "char *"
5396 | RStringList _ | RHashtable _ -> pr "char **"
5397 | RStruct (_, typ) ->
5398 if not in_daemon then pr "struct guestfs_%s *" typ
5399 else pr "guestfs_int_%s *" typ
5400 | RStructList (_, typ) ->
5401 if not in_daemon then pr "struct guestfs_%s_list *" typ
5402 else pr "guestfs_int_%s_list *" typ
5404 pr "%s%s (" prefix name;
5405 if handle = None && List.length (snd style) = 0 then
5408 let comma = ref false in
5411 | Some handle -> pr "guestfs_h *%s" handle; comma := true
5415 if single_line then pr ", " else pr ",\n\t\t"
5424 if not in_daemon then pr "const char *%s" n
5425 else pr "char *%s" n
5428 if not in_daemon then pr "char * const* const %s" n
5429 else pr "char **%s" n
5430 | Bool n -> next (); pr "int %s" n
5431 | Int n -> next (); pr "int %s" n
5434 if not in_daemon then (next (); pr "const char *%s" n)
5438 if semicolon then pr ";";
5439 if newline then pr "\n"
5441 (* Generate C call arguments, eg "(handle, foo, bar)" *)
5442 and generate_call_args ?handle args =
5444 let comma = ref false in
5447 | Some handle -> pr "%s" handle; comma := true
5451 if !comma then pr ", ";
5453 pr "%s" (name_of_argt arg)
5457 (* Generate the OCaml bindings interface. *)
5458 and generate_ocaml_mli () =
5459 generate_header OCamlStyle LGPLv2;
5462 (** For API documentation you should refer to the C API
5463 in the guestfs(3) manual page. The OCaml API uses almost
5464 exactly the same calls. *)
5467 (** A [guestfs_h] handle. *)
5469 exception Error of string
5470 (** This exception is raised when there is an error. *)
5472 val create : unit -> t
5474 val close : t -> unit
5475 (** Handles are closed by the garbage collector when they become
5476 unreferenced, but callers can also call this in order to
5477 provide predictable cleanup. *)
5480 generate_ocaml_structure_decls ();
5484 fun (name, style, _, _, _, shortdesc, _) ->
5485 generate_ocaml_prototype name style;
5486 pr "(** %s *)\n" shortdesc;
5490 (* Generate the OCaml bindings implementation. *)
5491 and generate_ocaml_ml () =
5492 generate_header OCamlStyle LGPLv2;
5496 exception Error of string
5497 external create : unit -> t = \"ocaml_guestfs_create\"
5498 external close : t -> unit = \"ocaml_guestfs_close\"
5501 Callback.register_exception \"ocaml_guestfs_error\" (Error \"\")
5505 generate_ocaml_structure_decls ();
5509 fun (name, style, _, _, _, shortdesc, _) ->
5510 generate_ocaml_prototype ~is_external:true name style;
5513 (* Generate the OCaml bindings C implementation. *)
5514 and generate_ocaml_c () =
5515 generate_header CStyle LGPLv2;
5522 #include <caml/config.h>
5523 #include <caml/alloc.h>
5524 #include <caml/callback.h>
5525 #include <caml/fail.h>
5526 #include <caml/memory.h>
5527 #include <caml/mlvalues.h>
5528 #include <caml/signals.h>
5530 #include <guestfs.h>
5532 #include \"guestfs_c.h\"
5534 /* Copy a hashtable of string pairs into an assoc-list. We return
5535 * the list in reverse order, but hashtables aren't supposed to be
5538 static CAMLprim value
5539 copy_table (char * const * argv)
5542 CAMLlocal5 (rv, pairv, kv, vv, cons);
5546 for (i = 0; argv[i] != NULL; i += 2) {
5547 kv = caml_copy_string (argv[i]);
5548 vv = caml_copy_string (argv[i+1]);
5549 pairv = caml_alloc (2, 0);
5550 Store_field (pairv, 0, kv);
5551 Store_field (pairv, 1, vv);
5552 cons = caml_alloc (2, 0);
5553 Store_field (cons, 1, rv);
5555 Store_field (cons, 0, pairv);
5563 (* Struct copy functions. *)
5566 let has_optpercent_col =
5567 List.exists (function (_, FOptPercent) -> true | _ -> false) cols in
5569 pr "static CAMLprim value\n";
5570 pr "copy_%s (const struct guestfs_%s *%s)\n" typ typ typ;
5572 pr " CAMLparam0 ();\n";
5573 if has_optpercent_col then
5574 pr " CAMLlocal3 (rv, v, v2);\n"
5576 pr " CAMLlocal2 (rv, v);\n";
5578 pr " rv = caml_alloc (%d, 0);\n" (List.length cols);
5583 pr " v = caml_copy_string (%s->%s);\n" typ name
5585 pr " v = caml_alloc_string (32);\n";
5586 pr " memcpy (String_val (v), %s->%s, 32);\n" typ name
5587 | name, (FBytes|FInt64|FUInt64) ->
5588 pr " v = caml_copy_int64 (%s->%s);\n" typ name
5589 | name, (FInt32|FUInt32) ->
5590 pr " v = caml_copy_int32 (%s->%s);\n" typ name
5591 | name, FOptPercent ->
5592 pr " if (%s->%s >= 0) { /* Some %s */\n" typ name name;
5593 pr " v2 = caml_copy_double (%s->%s);\n" typ name;
5594 pr " v = caml_alloc (1, 0);\n";
5595 pr " Store_field (v, 0, v2);\n";
5596 pr " } else /* None */\n";
5597 pr " v = Val_int (0);\n";
5599 pr " v = Val_int (%s->%s);\n" typ name
5601 pr " Store_field (rv, %d, v);\n" i
5603 pr " CAMLreturn (rv);\n";
5607 pr "static CAMLprim value\n";
5608 pr "copy_%s_list (const struct guestfs_%s_list *%ss)\n"
5611 pr " CAMLparam0 ();\n";
5612 pr " CAMLlocal2 (rv, v);\n";
5615 pr " if (%ss->len == 0)\n" typ;
5616 pr " CAMLreturn (Atom (0));\n";
5618 pr " rv = caml_alloc (%ss->len, 0);\n" typ;
5619 pr " for (i = 0; i < %ss->len; ++i) {\n" typ;
5620 pr " v = copy_%s (&%ss->val[i]);\n" typ typ;
5621 pr " caml_modify (&Field (rv, i), v);\n";
5623 pr " CAMLreturn (rv);\n";
5631 fun (name, style, _, _, _, _, _) ->
5633 "gv" :: List.map (fun arg -> name_of_argt arg ^ "v") (snd style) in
5635 pr "CAMLprim value\n";
5636 pr "ocaml_guestfs_%s (value %s" name (List.hd params);
5637 List.iter (pr ", value %s") (List.tl params);
5642 | [p1; p2; p3; p4; p5] ->
5643 pr " CAMLparam5 (%s);\n" (String.concat ", " params)
5644 | p1 :: p2 :: p3 :: p4 :: p5 :: rest ->
5645 pr " CAMLparam5 (%s);\n" (String.concat ", " [p1; p2; p3; p4; p5]);
5646 pr " CAMLxparam%d (%s);\n"
5647 (List.length rest) (String.concat ", " rest)
5649 pr " CAMLparam%d (%s);\n" (List.length ps) (String.concat ", " ps)
5651 pr " CAMLlocal1 (rv);\n";
5654 pr " guestfs_h *g = Guestfs_val (gv);\n";
5655 pr " if (g == NULL)\n";
5656 pr " caml_failwith (\"%s: used handle after closing it\");\n" name;
5664 pr " const char *%s = String_val (%sv);\n" n n
5666 pr " const char *%s =\n" n;
5667 pr " %sv != Val_int (0) ? String_val (Field (%sv, 0)) : NULL;\n"
5670 pr " char **%s = ocaml_guestfs_strings_val (g, %sv);\n" n n
5672 pr " int %s = Bool_val (%sv);\n" n n
5674 pr " int %s = Int_val (%sv);\n" n n
5677 match fst style with
5678 | RErr -> pr " int r;\n"; "-1"
5679 | RInt _ -> pr " int r;\n"; "-1"
5680 | RInt64 _ -> pr " int64_t r;\n"; "-1"
5681 | RBool _ -> pr " int r;\n"; "-1"
5682 | RConstString _ -> pr " const char *r;\n"; "NULL"
5683 | RString _ -> pr " char *r;\n"; "NULL"
5688 | RStruct (_, typ) ->
5689 pr " struct guestfs_%s *r;\n" typ; "NULL"
5690 | RStructList (_, typ) ->
5691 pr " struct guestfs_%s_list *r;\n" typ; "NULL"
5698 pr " caml_enter_blocking_section ();\n";
5699 pr " r = guestfs_%s " name;
5700 generate_call_args ~handle:"g" (snd style);
5702 pr " caml_leave_blocking_section ();\n";
5707 pr " ocaml_guestfs_free_strings (%s);\n" n;
5708 | String _ | OptString _ | Bool _ | Int _ | FileIn _ | FileOut _ -> ()
5711 pr " if (r == %s)\n" error_code;
5712 pr " ocaml_guestfs_raise_error (g, \"%s\");\n" name;
5715 (match fst style with
5716 | RErr -> pr " rv = Val_unit;\n"
5717 | RInt _ -> pr " rv = Val_int (r);\n"
5719 pr " rv = caml_copy_int64 (r);\n"
5720 | RBool _ -> pr " rv = Val_bool (r);\n"
5721 | RConstString _ -> pr " rv = caml_copy_string (r);\n"
5723 pr " rv = caml_copy_string (r);\n";
5726 pr " rv = caml_copy_string_array ((const char **) r);\n";
5727 pr " for (i = 0; r[i] != NULL; ++i) free (r[i]);\n";
5729 | RStruct (_, typ) ->
5730 pr " rv = copy_%s (r);\n" typ;
5731 pr " guestfs_free_%s (r);\n" typ;
5732 | RStructList (_, typ) ->
5733 pr " rv = copy_%s_list (r);\n" typ;
5734 pr " guestfs_free_%s_list (r);\n" typ;
5736 pr " rv = copy_table (r);\n";
5737 pr " for (i = 0; r[i] != NULL; ++i) free (r[i]);\n";
5741 pr " CAMLreturn (rv);\n";
5745 if List.length params > 5 then (
5746 pr "CAMLprim value\n";
5747 pr "ocaml_guestfs_%s_byte (value *argv, int argn)\n" name;
5749 pr " return ocaml_guestfs_%s (argv[0]" name;
5750 iteri (fun i _ -> pr ", argv[%d]" i) (List.tl params);
5757 and generate_ocaml_structure_decls () =
5760 pr "type %s = {\n" typ;
5763 | name, FString -> pr " %s : string;\n" name
5764 | name, FUUID -> pr " %s : string;\n" name
5765 | name, (FBytes|FInt64|FUInt64) -> pr " %s : int64;\n" name
5766 | name, (FInt32|FUInt32) -> pr " %s : int32;\n" name
5767 | name, FChar -> pr " %s : char;\n" name
5768 | name, FOptPercent -> pr " %s : float option;\n" name
5774 and generate_ocaml_prototype ?(is_external = false) name style =
5775 if is_external then pr "external " else pr "val ";
5776 pr "%s : t -> " name;
5779 | String _ | FileIn _ | FileOut _ -> pr "string -> "
5780 | OptString _ -> pr "string option -> "
5781 | StringList _ -> pr "string array -> "
5782 | Bool _ -> pr "bool -> "
5783 | Int _ -> pr "int -> "
5785 (match fst style with
5786 | RErr -> pr "unit" (* all errors are turned into exceptions *)
5787 | RInt _ -> pr "int"
5788 | RInt64 _ -> pr "int64"
5789 | RBool _ -> pr "bool"
5790 | RConstString _ -> pr "string"
5791 | RString _ -> pr "string"
5792 | RStringList _ -> pr "string array"
5793 | RStruct (_, typ) -> pr "%s" typ
5794 | RStructList (_, typ) -> pr "%s array" typ
5795 | RHashtable _ -> pr "(string * string) list"
5797 if is_external then (
5799 if List.length (snd style) + 1 > 5 then
5800 pr "\"ocaml_guestfs_%s_byte\" " name;
5801 pr "\"ocaml_guestfs_%s\"" name
5805 (* Generate Perl xs code, a sort of crazy variation of C with macros. *)
5806 and generate_perl_xs () =
5807 generate_header CStyle LGPLv2;
5810 #include \"EXTERN.h\"
5814 #include <guestfs.h>
5817 #define PRId64 \"lld\"
5821 my_newSVll(long long val) {
5822 #ifdef USE_64_BIT_ALL
5823 return newSViv(val);
5827 len = snprintf(buf, 100, \"%%\" PRId64, val);
5828 return newSVpv(buf, len);
5833 #define PRIu64 \"llu\"
5837 my_newSVull(unsigned long long val) {
5838 #ifdef USE_64_BIT_ALL
5839 return newSVuv(val);
5843 len = snprintf(buf, 100, \"%%\" PRIu64, val);
5844 return newSVpv(buf, len);
5848 /* http://www.perlmonks.org/?node_id=680842 */
5850 XS_unpack_charPtrPtr (SV *arg) {
5855 if (!arg || !SvOK (arg) || !SvROK (arg) || SvTYPE (SvRV (arg)) != SVt_PVAV)
5856 croak (\"array reference expected\");
5858 av = (AV *)SvRV (arg);
5859 ret = malloc ((av_len (av) + 1 + 1) * sizeof (char *));
5861 croak (\"malloc failed\");
5863 for (i = 0; i <= av_len (av); i++) {
5864 SV **elem = av_fetch (av, i, 0);
5866 if (!elem || !*elem)
5867 croak (\"missing element in list\");
5869 ret[i] = SvPV_nolen (*elem);
5877 MODULE = Sys::Guestfs PACKAGE = Sys::Guestfs
5884 RETVAL = guestfs_create ();
5886 croak (\"could not create guestfs handle\");
5887 guestfs_set_error_handler (RETVAL, NULL, NULL);
5900 fun (name, style, _, _, _, _, _) ->
5901 (match fst style with
5902 | RErr -> pr "void\n"
5903 | RInt _ -> pr "SV *\n"
5904 | RInt64 _ -> pr "SV *\n"
5905 | RBool _ -> pr "SV *\n"
5906 | RConstString _ -> pr "SV *\n"
5907 | RString _ -> pr "SV *\n"
5909 | RStruct _ | RStructList _
5911 pr "void\n" (* all lists returned implictly on the stack *)
5913 (* Call and arguments. *)
5915 generate_call_args ~handle:"g" (snd style);
5917 pr " guestfs_h *g;\n";
5921 | String n | FileIn n | FileOut n -> pr " char *%s;\n" n
5923 (* http://www.perlmonks.org/?node_id=554277
5924 * Note that the implicit handle argument means we have
5925 * to add 1 to the ST(x) operator.
5927 pr " char *%s = SvOK(ST(%d)) ? SvPV_nolen(ST(%d)) : NULL;\n" n (i+1) (i+1)
5928 | StringList n -> pr " char **%s;\n" n
5929 | Bool n -> pr " int %s;\n" n
5930 | Int n -> pr " int %s;\n" n
5933 let do_cleanups () =
5936 | String _ | OptString _ | Bool _ | Int _
5937 | FileIn _ | FileOut _ -> ()
5938 | StringList n -> pr " free (%s);\n" n
5943 (match fst style with
5948 pr " r = guestfs_%s " name;
5949 generate_call_args ~handle:"g" (snd style);
5952 pr " if (r == -1)\n";
5953 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
5959 pr " %s = guestfs_%s " n name;
5960 generate_call_args ~handle:"g" (snd style);
5963 pr " if (%s == -1)\n" n;
5964 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
5965 pr " RETVAL = newSViv (%s);\n" n;
5970 pr " int64_t %s;\n" n;
5972 pr " %s = guestfs_%s " n name;
5973 generate_call_args ~handle:"g" (snd style);
5976 pr " if (%s == -1)\n" n;
5977 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
5978 pr " RETVAL = my_newSVll (%s);\n" n;
5983 pr " const char *%s;\n" n;
5985 pr " %s = guestfs_%s " n name;
5986 generate_call_args ~handle:"g" (snd style);
5989 pr " if (%s == NULL)\n" n;
5990 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
5991 pr " RETVAL = newSVpv (%s, 0);\n" n;
5996 pr " char *%s;\n" n;
5998 pr " %s = guestfs_%s " n name;
5999 generate_call_args ~handle:"g" (snd style);
6002 pr " if (%s == NULL)\n" n;
6003 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
6004 pr " RETVAL = newSVpv (%s, 0);\n" n;
6005 pr " free (%s);\n" n;
6008 | RStringList n | RHashtable n ->
6010 pr " char **%s;\n" n;
6013 pr " %s = guestfs_%s " n name;
6014 generate_call_args ~handle:"g" (snd style);
6017 pr " if (%s == NULL)\n" n;
6018 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
6019 pr " for (n = 0; %s[n] != NULL; ++n) /**/;\n" n;
6020 pr " EXTEND (SP, n);\n";
6021 pr " for (i = 0; i < n; ++i) {\n";
6022 pr " PUSHs (sv_2mortal (newSVpv (%s[i], 0)));\n" n;
6023 pr " free (%s[i]);\n" n;
6025 pr " free (%s);\n" n;
6026 | RStruct (n, typ) ->
6027 let cols = cols_of_struct typ in
6028 generate_perl_struct_code typ cols name style n do_cleanups
6029 | RStructList (n, typ) ->
6030 let cols = cols_of_struct typ in
6031 generate_perl_struct_list_code typ cols name style n do_cleanups
6037 and generate_perl_struct_list_code typ cols name style n do_cleanups =
6039 pr " struct guestfs_%s_list *%s;\n" typ n;
6043 pr " %s = guestfs_%s " n name;
6044 generate_call_args ~handle:"g" (snd style);
6047 pr " if (%s == NULL)\n" n;
6048 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
6049 pr " EXTEND (SP, %s->len);\n" n;
6050 pr " for (i = 0; i < %s->len; ++i) {\n" n;
6051 pr " hv = newHV ();\n";
6055 pr " (void) hv_store (hv, \"%s\", %d, newSVpv (%s->val[i].%s, 0), 0);\n"
6056 name (String.length name) n name
6058 pr " (void) hv_store (hv, \"%s\", %d, newSVpv (%s->val[i].%s, 32), 0);\n"
6059 name (String.length name) n name
6060 | name, (FBytes|FUInt64) ->
6061 pr " (void) hv_store (hv, \"%s\", %d, my_newSVull (%s->val[i].%s), 0);\n"
6062 name (String.length name) n name
6064 pr " (void) hv_store (hv, \"%s\", %d, my_newSVll (%s->val[i].%s), 0);\n"
6065 name (String.length name) n name
6066 | name, (FInt32|FUInt32) ->
6067 pr " (void) hv_store (hv, \"%s\", %d, newSVnv (%s->val[i].%s), 0);\n"
6068 name (String.length name) n name
6070 pr " (void) hv_store (hv, \"%s\", %d, newSVpv (&%s->val[i].%s, 1), 0);\n"
6071 name (String.length name) n name
6072 | name, FOptPercent ->
6073 pr " (void) hv_store (hv, \"%s\", %d, newSVnv (%s->val[i].%s), 0);\n"
6074 name (String.length name) n name
6076 pr " PUSHs (sv_2mortal (newRV ((SV *) hv)));\n";
6078 pr " guestfs_free_%s_list (%s);\n" typ n
6080 and generate_perl_struct_code typ cols name style n do_cleanups =
6082 pr " struct guestfs_%s *%s;\n" typ n;
6084 pr " %s = guestfs_%s " n name;
6085 generate_call_args ~handle:"g" (snd style);
6088 pr " if (%s == NULL)\n" n;
6089 pr " croak (\"%s: %%s\", guestfs_last_error (g));\n" name;
6090 pr " EXTEND (SP, 2 * %d);\n" (List.length cols);
6092 fun ((name, _) as col) ->
6093 pr " PUSHs (sv_2mortal (newSVpv (\"%s\", 0)));\n" name;
6097 pr " PUSHs (sv_2mortal (newSVpv (%s->%s, 0)));\n"
6100 pr " PUSHs (sv_2mortal (newSVpv (%s->%s, 32)));\n"
6102 | name, (FBytes|FUInt64) ->
6103 pr " PUSHs (sv_2mortal (my_newSVull (%s->%s)));\n"
6106 pr " PUSHs (sv_2mortal (my_newSVll (%s->%s)));\n"
6108 | name, (FInt32|FUInt32) ->
6109 pr " PUSHs (sv_2mortal (newSVnv (%s->%s)));\n"
6112 pr " PUSHs (sv_2mortal (newSVpv (&%s->%s, 1)));\n"
6114 | name, FOptPercent ->
6115 pr " PUSHs (sv_2mortal (newSVnv (%s->%s)));\n"
6118 pr " free (%s);\n" n
6120 (* Generate Sys/Guestfs.pm. *)
6121 and generate_perl_pm () =
6122 generate_header HashStyle LGPLv2;
6129 Sys::Guestfs - Perl bindings for libguestfs
6135 my $h = Sys::Guestfs->new ();
6136 $h->add_drive ('guest.img');
6139 $h->mount ('/dev/sda1', '/');
6140 $h->touch ('/hello');
6145 The C<Sys::Guestfs> module provides a Perl XS binding to the
6146 libguestfs API for examining and modifying virtual machine
6149 Amongst the things this is good for: making batch configuration
6150 changes to guests, getting disk used/free statistics (see also:
6151 virt-df), migrating between virtualization systems (see also:
6152 virt-p2v), performing partial backups, performing partial guest
6153 clones, cloning guests and changing registry/UUID/hostname info, and
6156 Libguestfs uses Linux kernel and qemu code, and can access any type of
6157 guest filesystem that Linux and qemu can, including but not limited
6158 to: ext2/3/4, btrfs, FAT and NTFS, LVM, many different disk partition
6159 schemes, qcow, qcow2, vmdk.
6161 Libguestfs provides ways to enumerate guest storage (eg. partitions,
6162 LVs, what filesystem is in each LV, etc.). It can also run commands
6163 in the context of the guest. Also you can access filesystems over FTP.
6165 See also L<Sys::Guestfs::Lib(3)> for a set of useful library
6166 functions for using libguestfs from Perl, including integration
6171 All errors turn into calls to C<croak> (see L<Carp(3)>).
6179 package Sys::Guestfs;
6185 XSLoader::load ('Sys::Guestfs');
6187 =item $h = Sys::Guestfs->new ();
6189 Create a new guestfs handle.
6195 my $class = ref ($proto) || $proto;
6197 my $self = Sys::Guestfs::_create ();
6198 bless $self, $class;
6204 (* Actions. We only need to print documentation for these as
6205 * they are pulled in from the XS code automatically.
6208 fun (name, style, _, flags, _, _, longdesc) ->
6209 if not (List.mem NotInDocs flags) then (
6210 let longdesc = replace_str longdesc "C<guestfs_" "C<$h-E<gt>" in
6212 generate_perl_prototype name style;
6214 pr "%s\n\n" longdesc;
6215 if List.mem ProtocolLimitWarning flags then
6216 pr "%s\n\n" protocol_limit_warning;
6217 if List.mem DangerWillRobinson flags then
6218 pr "%s\n\n" danger_will_robinson
6220 ) all_functions_sorted;
6232 Copyright (C) 2009 Red Hat Inc.
6236 Please see the file COPYING.LIB for the full license.
6242 L<http://libguestfs.org>,
6243 L<Sys::Guestfs::Lib(3)>.
6248 and generate_perl_prototype name style =
6249 (match fst style with
6255 | RString n -> pr "$%s = " n
6257 | RHashtable n -> pr "%%%s = " n
6259 | RStructList (n,_) -> pr "@%s = " n
6262 let comma = ref false in
6265 if !comma then pr ", ";
6268 | String n | OptString n | Bool n | Int n | FileIn n | FileOut n ->
6275 (* Generate Python C module. *)
6276 and generate_python_c () =
6277 generate_header CStyle LGPLv2;
6286 #include \"guestfs.h\"
6294 get_handle (PyObject *obj)
6297 assert (obj != Py_None);
6298 return ((Pyguestfs_Object *) obj)->g;
6302 put_handle (guestfs_h *g)
6306 PyCObject_FromVoidPtrAndDesc ((void *) g, (char *) \"guestfs_h\", NULL);
6309 /* This list should be freed (but not the strings) after use. */
6310 static const char **
6311 get_string_list (PyObject *obj)
6318 if (!PyList_Check (obj)) {
6319 PyErr_SetString (PyExc_RuntimeError, \"expecting a list parameter\");
6323 len = PyList_Size (obj);
6324 r = malloc (sizeof (char *) * (len+1));
6326 PyErr_SetString (PyExc_RuntimeError, \"get_string_list: out of memory\");
6330 for (i = 0; i < len; ++i)
6331 r[i] = PyString_AsString (PyList_GetItem (obj, i));
6338 put_string_list (char * const * const argv)
6343 for (argc = 0; argv[argc] != NULL; ++argc)
6346 list = PyList_New (argc);
6347 for (i = 0; i < argc; ++i)
6348 PyList_SetItem (list, i, PyString_FromString (argv[i]));
6354 put_table (char * const * const argv)
6356 PyObject *list, *item;
6359 for (argc = 0; argv[argc] != NULL; ++argc)
6362 list = PyList_New (argc >> 1);
6363 for (i = 0; i < argc; i += 2) {
6364 item = PyTuple_New (2);
6365 PyTuple_SetItem (item, 0, PyString_FromString (argv[i]));
6366 PyTuple_SetItem (item, 1, PyString_FromString (argv[i+1]));
6367 PyList_SetItem (list, i >> 1, item);
6374 free_strings (char **argv)
6378 for (argc = 0; argv[argc] != NULL; ++argc)
6384 py_guestfs_create (PyObject *self, PyObject *args)
6388 g = guestfs_create ();
6390 PyErr_SetString (PyExc_RuntimeError,
6391 \"guestfs.create: failed to allocate handle\");
6394 guestfs_set_error_handler (g, NULL, NULL);
6395 return put_handle (g);
6399 py_guestfs_close (PyObject *self, PyObject *args)
6404 if (!PyArg_ParseTuple (args, (char *) \"O:guestfs_close\", &py_g))
6406 g = get_handle (py_g);
6410 Py_INCREF (Py_None);
6416 (* Structures, turned into Python dictionaries. *)
6419 pr "static PyObject *\n";
6420 pr "put_%s (struct guestfs_%s *%s)\n" typ typ typ;
6422 pr " PyObject *dict;\n";
6424 pr " dict = PyDict_New ();\n";
6428 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
6429 pr " PyString_FromString (%s->%s));\n"
6432 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
6433 pr " PyString_FromStringAndSize (%s->%s, 32));\n"
6435 | name, (FBytes|FUInt64) ->
6436 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
6437 pr " PyLong_FromUnsignedLongLong (%s->%s));\n"
6440 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
6441 pr " PyLong_FromLongLong (%s->%s));\n"
6444 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
6445 pr " PyLong_FromUnsignedLong (%s->%s));\n"
6448 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
6449 pr " PyLong_FromLong (%s->%s));\n"
6451 | name, FOptPercent ->
6452 pr " if (%s->%s >= 0)\n" typ name;
6453 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
6454 pr " PyFloat_FromDouble ((double) %s->%s));\n"
6457 pr " Py_INCREF (Py_None);\n";
6458 pr " PyDict_SetItemString (dict, \"%s\", Py_None);" name;
6461 pr " PyDict_SetItemString (dict, \"%s\",\n" name;
6462 pr " PyString_FromStringAndSize (&dirent->%s, 1));\n" name
6464 pr " return dict;\n";
6468 pr "static PyObject *\n";
6469 pr "put_%s_list (struct guestfs_%s_list *%ss)\n" typ typ typ;
6471 pr " PyObject *list;\n";
6474 pr " list = PyList_New (%ss->len);\n" typ;
6475 pr " for (i = 0; i < %ss->len; ++i)\n" typ;
6476 pr " PyList_SetItem (list, i, put_%s (&%ss->val[i]));\n" typ typ;
6477 pr " return list;\n";
6482 (* Python wrapper functions. *)
6484 fun (name, style, _, _, _, _, _) ->
6485 pr "static PyObject *\n";
6486 pr "py_guestfs_%s (PyObject *self, PyObject *args)\n" name;
6489 pr " PyObject *py_g;\n";
6490 pr " guestfs_h *g;\n";
6491 pr " PyObject *py_r;\n";
6494 match fst style with
6495 | RErr | RInt _ | RBool _ -> pr " int r;\n"; "-1"
6496 | RInt64 _ -> pr " int64_t r;\n"; "-1"
6497 | RConstString _ -> pr " const char *r;\n"; "NULL"
6498 | RString _ -> pr " char *r;\n"; "NULL"
6499 | RStringList _ | RHashtable _ -> pr " char **r;\n"; "NULL"
6500 | RStruct (_, typ) -> pr " struct guestfs_%s *r;\n" typ; "NULL"
6501 | RStructList (_, typ) ->
6502 pr " struct guestfs_%s_list *r;\n" typ; "NULL" in
6506 | String n | FileIn n | FileOut n -> pr " const char *%s;\n" n
6507 | OptString n -> pr " const char *%s;\n" n
6509 pr " PyObject *py_%s;\n" n;
6510 pr " const char **%s;\n" n
6511 | Bool n -> pr " int %s;\n" n
6512 | Int n -> pr " int %s;\n" n
6517 (* Convert the parameters. *)
6518 pr " if (!PyArg_ParseTuple (args, (char *) \"O";
6521 | String _ | FileIn _ | FileOut _ -> pr "s"
6522 | OptString _ -> pr "z"
6523 | StringList _ -> pr "O"
6524 | Bool _ -> pr "i" (* XXX Python has booleans? *)
6527 pr ":guestfs_%s\",\n" name;
6531 | String n | FileIn n | FileOut n -> pr ", &%s" n
6532 | OptString n -> pr ", &%s" n
6533 | StringList n -> pr ", &py_%s" n
6534 | Bool n -> pr ", &%s" n
6535 | Int n -> pr ", &%s" n
6539 pr " return NULL;\n";
6541 pr " g = get_handle (py_g);\n";
6544 | String _ | FileIn _ | FileOut _ | OptString _ | Bool _ | Int _ -> ()
6546 pr " %s = get_string_list (py_%s);\n" n n;
6547 pr " if (!%s) return NULL;\n" n
6552 pr " r = guestfs_%s " name;
6553 generate_call_args ~handle:"g" (snd style);
6558 | String _ | FileIn _ | FileOut _ | OptString _ | Bool _ | Int _ -> ()
6560 pr " free (%s);\n" n
6563 pr " if (r == %s) {\n" error_code;
6564 pr " PyErr_SetString (PyExc_RuntimeError, guestfs_last_error (g));\n";
6565 pr " return NULL;\n";
6569 (match fst style with
6571 pr " Py_INCREF (Py_None);\n";
6572 pr " py_r = Py_None;\n"
6574 | RBool _ -> pr " py_r = PyInt_FromLong ((long) r);\n"
6575 | RInt64 _ -> pr " py_r = PyLong_FromLongLong (r);\n"
6576 | RConstString _ -> pr " py_r = PyString_FromString (r);\n"
6578 pr " py_r = PyString_FromString (r);\n";
6581 pr " py_r = put_string_list (r);\n";
6582 pr " free_strings (r);\n"
6583 | RStruct (_, typ) ->
6584 pr " py_r = put_%s (r);\n" typ;
6585 pr " guestfs_free_%s (r);\n" typ
6586 | RStructList (_, typ) ->
6587 pr " py_r = put_%s_list (r);\n" typ;
6588 pr " guestfs_free_%s_list (r);\n" typ
6590 pr " py_r = put_table (r);\n";
6591 pr " free_strings (r);\n"
6594 pr " return py_r;\n";
6599 (* Table of functions. *)
6600 pr "static PyMethodDef methods[] = {\n";
6601 pr " { (char *) \"create\", py_guestfs_create, METH_VARARGS, NULL },\n";
6602 pr " { (char *) \"close\", py_guestfs_close, METH_VARARGS, NULL },\n";
6604 fun (name, _, _, _, _, _, _) ->
6605 pr " { (char *) \"%s\", py_guestfs_%s, METH_VARARGS, NULL },\n"
6608 pr " { NULL, NULL, 0, NULL }\n";
6612 (* Init function. *)
6615 initlibguestfsmod (void)
6617 static int initialized = 0;
6619 if (initialized) return;
6620 Py_InitModule ((char *) \"libguestfsmod\", methods);
6625 (* Generate Python module. *)
6626 and generate_python_py () =
6627 generate_header HashStyle LGPLv2;
6630 u\"\"\"Python bindings for libguestfs
6633 g = guestfs.GuestFS ()
6634 g.add_drive (\"guest.img\")
6637 parts = g.list_partitions ()
6639 The guestfs module provides a Python binding to the libguestfs API
6640 for examining and modifying virtual machine disk images.
6642 Amongst the things this is good for: making batch configuration
6643 changes to guests, getting disk used/free statistics (see also:
6644 virt-df), migrating between virtualization systems (see also:
6645 virt-p2v), performing partial backups, performing partial guest
6646 clones, cloning guests and changing registry/UUID/hostname info, and
6649 Libguestfs uses Linux kernel and qemu code, and can access any type of
6650 guest filesystem that Linux and qemu can, including but not limited
6651 to: ext2/3/4, btrfs, FAT and NTFS, LVM, many different disk partition
6652 schemes, qcow, qcow2, vmdk.
6654 Libguestfs provides ways to enumerate guest storage (eg. partitions,
6655 LVs, what filesystem is in each LV, etc.). It can also run commands
6656 in the context of the guest. Also you can access filesystems over FTP.
6658 Errors which happen while using the API are turned into Python
6659 RuntimeError exceptions.
6661 To create a guestfs handle you usually have to perform the following
6664 # Create the handle, call add_drive at least once, and possibly
6665 # several times if the guest has multiple block devices:
6666 g = guestfs.GuestFS ()
6667 g.add_drive (\"guest.img\")
6669 # Launch the qemu subprocess and wait for it to become ready:
6673 # Now you can issue commands, for example:
6678 import libguestfsmod
6681 \"\"\"Instances of this class are libguestfs API handles.\"\"\"
6683 def __init__ (self):
6684 \"\"\"Create a new libguestfs handle.\"\"\"
6685 self._o = libguestfsmod.create ()
6688 libguestfsmod.close (self._o)
6693 fun (name, style, _, flags, _, _, longdesc) ->
6695 generate_call_args ~handle:"self" (snd style);
6698 if not (List.mem NotInDocs flags) then (
6699 let doc = replace_str longdesc "C<guestfs_" "C<g." in
6701 match fst style with
6702 | RErr | RInt _ | RInt64 _ | RBool _ | RConstString _
6705 doc ^ "\n\nThis function returns a list of strings."
6706 | RStruct (_, typ) ->
6707 doc ^ sprintf "\n\nThis function returns a dictionary, with keys matching the various fields in the guestfs_%s structure." typ
6708 | RStructList (_, typ) ->
6709 doc ^ sprintf "\n\nThis function returns a list of %ss. Each %s is represented as a dictionary." typ typ
6711 doc ^ "\n\nThis function returns a dictionary." in
6713 if List.mem ProtocolLimitWarning flags then
6714 doc ^ "\n\n" ^ protocol_limit_warning
6717 if List.mem DangerWillRobinson flags then
6718 doc ^ "\n\n" ^ danger_will_robinson
6720 let doc = pod2text ~width:60 name doc in
6721 let doc = List.map (fun line -> replace_str line "\\" "\\\\") doc in
6722 let doc = String.concat "\n " doc in
6723 pr " u\"\"\"%s\"\"\"\n" doc;
6725 pr " return libguestfsmod.%s " name;
6726 generate_call_args ~handle:"self._o" (snd style);
6731 (* Useful if you need the longdesc POD text as plain text. Returns a
6734 * Because this is very slow (the slowest part of autogeneration),
6735 * we memoize the results.
6737 and pod2text ~width name longdesc =
6738 let key = width, name, longdesc in
6739 try Hashtbl.find pod2text_memo key
6741 let filename, chan = Filename.open_temp_file "gen" ".tmp" in
6742 fprintf chan "=head1 %s\n\n%s\n" name longdesc;
6744 let cmd = sprintf "pod2text -w %d %s" width (Filename.quote filename) in
6745 let chan = Unix.open_process_in cmd in
6746 let lines = ref [] in
6748 let line = input_line chan in
6749 if i = 1 then (* discard the first line of output *)
6752 let line = triml line in
6753 lines := line :: !lines;
6756 let lines = try loop 1 with End_of_file -> List.rev !lines in
6757 Unix.unlink filename;
6758 (match Unix.close_process_in chan with
6759 | Unix.WEXITED 0 -> ()
6761 failwithf "pod2text: process exited with non-zero status (%d)" i
6762 | Unix.WSIGNALED i | Unix.WSTOPPED i ->
6763 failwithf "pod2text: process signalled or stopped by signal %d" i
6765 Hashtbl.add pod2text_memo key lines;
6766 let chan = open_out pod2text_memo_filename in
6767 output_value chan pod2text_memo;
6771 (* Generate ruby bindings. *)
6772 and generate_ruby_c () =
6773 generate_header CStyle LGPLv2;
6781 #include \"guestfs.h\"
6783 #include \"extconf.h\"
6785 /* For Ruby < 1.9 */
6787 #define RARRAY_LEN(r) (RARRAY((r))->len)
6790 static VALUE m_guestfs; /* guestfs module */
6791 static VALUE c_guestfs; /* guestfs_h handle */
6792 static VALUE e_Error; /* used for all errors */
6794 static void ruby_guestfs_free (void *p)
6797 guestfs_close ((guestfs_h *) p);
6800 static VALUE ruby_guestfs_create (VALUE m)
6804 g = guestfs_create ();
6806 rb_raise (e_Error, \"failed to create guestfs handle\");
6808 /* Don't print error messages to stderr by default. */
6809 guestfs_set_error_handler (g, NULL, NULL);
6811 /* Wrap it, and make sure the close function is called when the
6814 return Data_Wrap_Struct (c_guestfs, NULL, ruby_guestfs_free, g);
6817 static VALUE ruby_guestfs_close (VALUE gv)
6820 Data_Get_Struct (gv, guestfs_h, g);
6822 ruby_guestfs_free (g);
6823 DATA_PTR (gv) = NULL;
6831 fun (name, style, _, _, _, _, _) ->
6832 pr "static VALUE ruby_guestfs_%s (VALUE gv" name;
6833 List.iter (fun arg -> pr ", VALUE %sv" (name_of_argt arg)) (snd style);
6836 pr " guestfs_h *g;\n";
6837 pr " Data_Get_Struct (gv, guestfs_h, g);\n";
6839 pr " rb_raise (rb_eArgError, \"%%s: used handle after closing it\", \"%s\");\n"
6845 | String n | FileIn n | FileOut n ->
6846 pr " Check_Type (%sv, T_STRING);\n" n;
6847 pr " const char *%s = StringValueCStr (%sv);\n" n n;
6849 pr " rb_raise (rb_eTypeError, \"expected string for parameter %%s of %%s\",\n";
6850 pr " \"%s\", \"%s\");\n" n name
6852 pr " const char *%s = !NIL_P (%sv) ? StringValueCStr (%sv) : NULL;\n" n n n
6854 pr " char **%s;\n" n;
6855 pr " Check_Type (%sv, T_ARRAY);\n" n;
6857 pr " int i, len;\n";
6858 pr " len = RARRAY_LEN (%sv);\n" n;
6859 pr " %s = guestfs_safe_malloc (g, sizeof (char *) * (len+1));\n"
6861 pr " for (i = 0; i < len; ++i) {\n";
6862 pr " VALUE v = rb_ary_entry (%sv, i);\n" n;
6863 pr " %s[i] = StringValueCStr (v);\n" n;
6865 pr " %s[len] = NULL;\n" n;
6868 pr " int %s = RTEST (%sv);\n" n n
6870 pr " int %s = NUM2INT (%sv);\n" n n
6875 match fst style with
6876 | RErr | RInt _ | RBool _ -> pr " int r;\n"; "-1"
6877 | RInt64 _ -> pr " int64_t r;\n"; "-1"
6878 | RConstString _ -> pr " const char *r;\n"; "NULL"
6879 | RString _ -> pr " char *r;\n"; "NULL"
6880 | RStringList _ | RHashtable _ -> pr " char **r;\n"; "NULL"
6881 | RStruct (_, typ) -> pr " struct guestfs_%s *r;\n" typ; "NULL"
6882 | RStructList (_, typ) ->
6883 pr " struct guestfs_%s_list *r;\n" typ; "NULL" in
6886 pr " r = guestfs_%s " name;
6887 generate_call_args ~handle:"g" (snd style);
6892 | String _ | FileIn _ | FileOut _ | OptString _ | Bool _ | Int _ -> ()
6894 pr " free (%s);\n" n
6897 pr " if (r == %s)\n" error_code;
6898 pr " rb_raise (e_Error, \"%%s\", guestfs_last_error (g));\n";
6901 (match fst style with
6903 pr " return Qnil;\n"
6904 | RInt _ | RBool _ ->
6905 pr " return INT2NUM (r);\n"
6907 pr " return ULL2NUM (r);\n"
6909 pr " return rb_str_new2 (r);\n";
6911 pr " VALUE rv = rb_str_new2 (r);\n";
6915 pr " int i, len = 0;\n";
6916 pr " for (i = 0; r[i] != NULL; ++i) len++;\n";
6917 pr " VALUE rv = rb_ary_new2 (len);\n";
6918 pr " for (i = 0; r[i] != NULL; ++i) {\n";
6919 pr " rb_ary_push (rv, rb_str_new2 (r[i]));\n";
6920 pr " free (r[i]);\n";
6924 | RStruct (_, typ) ->
6925 let cols = cols_of_struct typ in
6926 generate_ruby_struct_code typ cols
6927 | RStructList (_, typ) ->
6928 let cols = cols_of_struct typ in
6929 generate_ruby_struct_list_code typ cols
6931 pr " VALUE rv = rb_hash_new ();\n";
6933 pr " for (i = 0; r[i] != NULL; i+=2) {\n";
6934 pr " rb_hash_aset (rv, rb_str_new2 (r[i]), rb_str_new2 (r[i+1]));\n";
6935 pr " free (r[i]);\n";
6936 pr " free (r[i+1]);\n";
6947 /* Initialize the module. */
6948 void Init__guestfs ()
6950 m_guestfs = rb_define_module (\"Guestfs\");
6951 c_guestfs = rb_define_class_under (m_guestfs, \"Guestfs\", rb_cObject);
6952 e_Error = rb_define_class_under (m_guestfs, \"Error\", rb_eStandardError);
6954 rb_define_module_function (m_guestfs, \"create\", ruby_guestfs_create, 0);
6955 rb_define_method (c_guestfs, \"close\", ruby_guestfs_close, 0);
6958 (* Define the rest of the methods. *)
6960 fun (name, style, _, _, _, _, _) ->
6961 pr " rb_define_method (c_guestfs, \"%s\",\n" name;
6962 pr " ruby_guestfs_%s, %d);\n" name (List.length (snd style))
6967 (* Ruby code to return a struct. *)
6968 and generate_ruby_struct_code typ cols =
6969 pr " VALUE rv = rb_hash_new ();\n";
6973 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_str_new2 (r->%s));\n" name name
6975 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_str_new (r->%s, 32));\n" name name
6976 | name, (FBytes|FUInt64) ->
6977 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), ULL2NUM (r->%s));\n" name name
6979 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), LL2NUM (r->%s));\n" name name
6981 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), UINT2NUM (r->%s));\n" name name
6983 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), INT2NUM (r->%s));\n" name name
6984 | name, FOptPercent ->
6985 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_dbl2big (r->%s));\n" name name
6986 | name, FChar -> (* XXX wrong? *)
6987 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), ULL2NUM (r->%s));\n" name name
6989 pr " guestfs_free_%s (r);\n" typ;
6992 (* Ruby code to return a struct list. *)
6993 and generate_ruby_struct_list_code typ cols =
6994 pr " VALUE rv = rb_ary_new2 (r->len);\n";
6996 pr " for (i = 0; i < r->len; ++i) {\n";
6997 pr " VALUE hv = rb_hash_new ();\n";
7001 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), rb_str_new2 (r->val[i].%s));\n" name name
7003 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), rb_str_new (r->val[i].%s, 32));\n" name name
7004 | name, (FBytes|FUInt64) ->
7005 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), ULL2NUM (r->val[i].%s));\n" name name
7007 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), LL2NUM (r->val[i].%s));\n" name name
7009 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), UINT2NUM (r->val[i].%s));\n" name name
7011 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), INT2NUM (r->val[i].%s));\n" name name
7012 | name, FOptPercent ->
7013 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), rb_dbl2big (r->val[i].%s));\n" name name
7014 | name, FChar -> (* XXX wrong? *)
7015 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), ULL2NUM (r->val[i].%s));\n" name name
7017 pr " rb_ary_push (rv, hv);\n";
7019 pr " guestfs_free_%s_list (r);\n" typ;
7022 (* Generate Java bindings GuestFS.java file. *)
7023 and generate_java_java () =
7024 generate_header CStyle LGPLv2;
7027 package com.redhat.et.libguestfs;
7029 import java.util.HashMap;
7030 import com.redhat.et.libguestfs.LibGuestFSException;
7031 import com.redhat.et.libguestfs.PV;
7032 import com.redhat.et.libguestfs.VG;
7033 import com.redhat.et.libguestfs.LV;
7034 import com.redhat.et.libguestfs.Stat;
7035 import com.redhat.et.libguestfs.StatVFS;
7036 import com.redhat.et.libguestfs.IntBool;
7037 import com.redhat.et.libguestfs.Dirent;
7040 * The GuestFS object is a libguestfs handle.
7044 public class GuestFS {
7045 // Load the native code.
7047 System.loadLibrary (\"guestfs_jni\");
7051 * The native guestfs_h pointer.
7056 * Create a libguestfs handle.
7058 * @throws LibGuestFSException
7060 public GuestFS () throws LibGuestFSException
7064 private native long _create () throws LibGuestFSException;
7067 * Close a libguestfs handle.
7069 * You can also leave handles to be collected by the garbage
7070 * collector, but this method ensures that the resources used
7071 * by the handle are freed up immediately. If you call any
7072 * other methods after closing the handle, you will get an
7075 * @throws LibGuestFSException
7077 public void close () throws LibGuestFSException
7083 private native void _close (long g) throws LibGuestFSException;
7085 public void finalize () throws LibGuestFSException
7093 fun (name, style, _, flags, _, shortdesc, longdesc) ->
7094 if not (List.mem NotInDocs flags); then (
7095 let doc = replace_str longdesc "C<guestfs_" "C<g." in
7097 if List.mem ProtocolLimitWarning flags then
7098 doc ^ "\n\n" ^ protocol_limit_warning
7101 if List.mem DangerWillRobinson flags then
7102 doc ^ "\n\n" ^ danger_will_robinson
7104 let doc = pod2text ~width:60 name doc in
7105 let doc = List.map ( (* RHBZ#501883 *)
7108 | nonempty -> nonempty
7110 let doc = String.concat "\n * " doc in
7113 pr " * %s\n" shortdesc;
7116 pr " * @throws LibGuestFSException\n";
7120 generate_java_prototype ~public:true ~semicolon:false name style;
7123 pr " if (g == 0)\n";
7124 pr " throw new LibGuestFSException (\"%s: handle is closed\");\n"
7127 if fst style <> RErr then pr "return ";
7129 generate_call_args ~handle:"g" (snd style);
7133 generate_java_prototype ~privat:true ~native:true name style;
7140 and generate_java_prototype ?(public=false) ?(privat=false) ?(native=false)
7141 ?(semicolon=true) name style =
7142 if privat then pr "private ";
7143 if public then pr "public ";
7144 if native then pr "native ";
7147 (match fst style with
7148 | RErr -> pr "void ";
7149 | RInt _ -> pr "int ";
7150 | RInt64 _ -> pr "long ";
7151 | RBool _ -> pr "boolean ";
7152 | RConstString _ | RString _ -> pr "String ";
7153 | RStringList _ -> pr "String[] ";
7154 | RStruct (_, typ) ->
7155 let name = java_name_of_struct typ in
7157 | RStructList (_, typ) ->
7158 let name = java_name_of_struct typ in
7160 | RHashtable _ -> pr "HashMap<String,String> ";
7163 if native then pr "_%s " name else pr "%s " name;
7165 let needs_comma = ref false in
7174 if !needs_comma then pr ", ";
7175 needs_comma := true;
7192 pr " throws LibGuestFSException";
7193 if semicolon then pr ";"
7195 and generate_java_struct jtyp cols =
7196 generate_header CStyle LGPLv2;
7199 package com.redhat.et.libguestfs;
7202 * Libguestfs %s structure.
7213 | name, FUUID -> pr " public String %s;\n" name
7214 | name, (FBytes|FUInt64|FInt64) -> pr " public long %s;\n" name
7215 | name, (FUInt32|FInt32) -> pr " public int %s;\n" name
7216 | name, FChar -> pr " public char %s;\n" name
7217 | name, FOptPercent ->
7218 pr " /* The next field is [0..100] or -1 meaning 'not present': */\n";
7219 pr " public float %s;\n" name
7224 and generate_java_c () =
7225 generate_header CStyle LGPLv2;
7232 #include \"com_redhat_et_libguestfs_GuestFS.h\"
7233 #include \"guestfs.h\"
7235 /* Note that this function returns. The exception is not thrown
7236 * until after the wrapper function returns.
7239 throw_exception (JNIEnv *env, const char *msg)
7242 cl = (*env)->FindClass (env,
7243 \"com/redhat/et/libguestfs/LibGuestFSException\");
7244 (*env)->ThrowNew (env, cl, msg);
7247 JNIEXPORT jlong JNICALL
7248 Java_com_redhat_et_libguestfs_GuestFS__1create
7249 (JNIEnv *env, jobject obj)
7253 g = guestfs_create ();
7255 throw_exception (env, \"GuestFS.create: failed to allocate handle\");
7258 guestfs_set_error_handler (g, NULL, NULL);
7259 return (jlong) (long) g;
7262 JNIEXPORT void JNICALL
7263 Java_com_redhat_et_libguestfs_GuestFS__1close
7264 (JNIEnv *env, jobject obj, jlong jg)
7266 guestfs_h *g = (guestfs_h *) (long) jg;
7273 fun (name, style, _, _, _, _, _) ->
7275 (match fst style with
7276 | RErr -> pr "void ";
7277 | RInt _ -> pr "jint ";
7278 | RInt64 _ -> pr "jlong ";
7279 | RBool _ -> pr "jboolean ";
7280 | RConstString _ | RString _ -> pr "jstring ";
7281 | RStruct _ | RHashtable _ ->
7283 | RStringList _ | RStructList _ ->
7287 pr "Java_com_redhat_et_libguestfs_GuestFS_";
7288 pr "%s" (replace_str ("_" ^ name) "_" "_1");
7290 pr " (JNIEnv *env, jobject obj, jlong jg";
7297 pr ", jstring j%s" n
7299 pr ", jobjectArray j%s" n
7301 pr ", jboolean j%s" n
7307 pr " guestfs_h *g = (guestfs_h *) (long) jg;\n";
7308 let error_code, no_ret =
7309 match fst style with
7310 | RErr -> pr " int r;\n"; "-1", ""
7312 | RInt _ -> pr " int r;\n"; "-1", "0"
7313 | RInt64 _ -> pr " int64_t r;\n"; "-1", "0"
7314 | RConstString _ -> pr " const char *r;\n"; "NULL", "NULL"
7316 pr " jstring jr;\n";
7317 pr " char *r;\n"; "NULL", "NULL"
7319 pr " jobjectArray jr;\n";
7322 pr " jstring jstr;\n";
7323 pr " char **r;\n"; "NULL", "NULL"
7324 | RStruct (_, typ) ->
7325 pr " jobject jr;\n";
7327 pr " jfieldID fl;\n";
7328 pr " struct guestfs_%s *r;\n" typ; "NULL", "NULL"
7329 | RStructList (_, typ) ->
7330 pr " jobjectArray jr;\n";
7332 pr " jfieldID fl;\n";
7333 pr " jobject jfl;\n";
7334 pr " struct guestfs_%s_list *r;\n" typ; "NULL", "NULL"
7335 | RHashtable _ -> pr " char **r;\n"; "NULL", "NULL" in
7342 pr " const char *%s;\n" n
7344 pr " int %s_len;\n" n;
7345 pr " const char **%s;\n" n
7352 (match fst style with
7353 | RStringList _ | RStructList _ -> true
7354 | RErr | RBool _ | RInt _ | RInt64 _ | RConstString _
7355 | RString _ | RStruct _ | RHashtable _ -> false) ||
7356 List.exists (function StringList _ -> true | _ -> false) (snd style) in
7362 (* Get the parameters. *)
7368 pr " %s = (*env)->GetStringUTFChars (env, j%s, NULL);\n" n n
7370 (* This is completely undocumented, but Java null becomes
7373 pr " %s = j%s ? (*env)->GetStringUTFChars (env, j%s, NULL) : NULL;\n" n n n
7375 pr " %s_len = (*env)->GetArrayLength (env, j%s);\n" n n;
7376 pr " %s = guestfs_safe_malloc (g, sizeof (char *) * (%s_len+1));\n" n n;
7377 pr " for (i = 0; i < %s_len; ++i) {\n" n;
7378 pr " jobject o = (*env)->GetObjectArrayElement (env, j%s, i);\n"
7380 pr " %s[i] = (*env)->GetStringUTFChars (env, o, NULL);\n" n;
7382 pr " %s[%s_len] = NULL;\n" n n;
7385 pr " %s = j%s;\n" n n
7388 (* Make the call. *)
7389 pr " r = guestfs_%s " name;
7390 generate_call_args ~handle:"g" (snd style);
7393 (* Release the parameters. *)
7399 pr " (*env)->ReleaseStringUTFChars (env, j%s, %s);\n" n n
7402 pr " (*env)->ReleaseStringUTFChars (env, j%s, %s);\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 " (*env)->ReleaseStringUTFChars (env, o, %s[i]);\n" n;
7409 pr " free (%s);\n" n
7414 (* Check for errors. *)
7415 pr " if (r == %s) {\n" error_code;
7416 pr " throw_exception (env, guestfs_last_error (g));\n";
7417 pr " return %s;\n" no_ret;
7421 (match fst style with
7423 | RInt _ -> pr " return (jint) r;\n"
7424 | RBool _ -> pr " return (jboolean) r;\n"
7425 | RInt64 _ -> pr " return (jlong) r;\n"
7426 | RConstString _ -> pr " return (*env)->NewStringUTF (env, r);\n"
7428 pr " jr = (*env)->NewStringUTF (env, r);\n";
7432 pr " for (r_len = 0; r[r_len] != NULL; ++r_len) ;\n";
7433 pr " cl = (*env)->FindClass (env, \"java/lang/String\");\n";
7434 pr " jstr = (*env)->NewStringUTF (env, \"\");\n";
7435 pr " jr = (*env)->NewObjectArray (env, r_len, cl, jstr);\n";
7436 pr " for (i = 0; i < r_len; ++i) {\n";
7437 pr " jstr = (*env)->NewStringUTF (env, r[i]);\n";
7438 pr " (*env)->SetObjectArrayElement (env, jr, i, jstr);\n";
7439 pr " free (r[i]);\n";
7443 | RStruct (_, typ) ->
7444 let jtyp = java_name_of_struct typ in
7445 let cols = cols_of_struct typ in
7446 generate_java_struct_return typ jtyp cols
7447 | RStructList (_, typ) ->
7448 let jtyp = java_name_of_struct typ in
7449 let cols = cols_of_struct typ in
7450 generate_java_struct_list_return typ jtyp cols
7453 pr " throw_exception (env, \"%s: internal error: please let us know how to make a Java HashMap from JNI bindings!\");\n" name;
7454 pr " return NULL;\n"
7461 and generate_java_struct_return typ jtyp cols =
7462 pr " cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/%s\");\n" jtyp;
7463 pr " jr = (*env)->AllocObject (env, cl);\n";
7467 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
7468 pr " (*env)->SetObjectField (env, jr, fl, (*env)->NewStringUTF (env, r->%s));\n" name;
7471 pr " char s[33];\n";
7472 pr " memcpy (s, r->%s, 32);\n" name;
7474 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
7475 pr " (*env)->SetObjectField (env, jr, fl, (*env)->NewStringUTF (env, s));\n";
7477 | name, (FBytes|FUInt64|FInt64) ->
7478 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"J\");\n" name;
7479 pr " (*env)->SetLongField (env, jr, fl, r->%s);\n" name;
7480 | name, (FUInt32|FInt32) ->
7481 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"I\");\n" name;
7482 pr " (*env)->SetLongField (env, jr, fl, r->%s);\n" name;
7483 | name, FOptPercent ->
7484 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"F\");\n" name;
7485 pr " (*env)->SetFloatField (env, jr, fl, r->%s);\n" name;
7487 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"C\");\n" name;
7488 pr " (*env)->SetLongField (env, jr, fl, r->%s);\n" name;
7493 and generate_java_struct_list_return typ jtyp cols =
7494 pr " cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/%s\");\n" jtyp;
7495 pr " jr = (*env)->NewObjectArray (env, r->len, cl, NULL);\n";
7496 pr " for (i = 0; i < r->len; ++i) {\n";
7497 pr " jfl = (*env)->AllocObject (env, cl);\n";
7501 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
7502 pr " (*env)->SetObjectField (env, jfl, fl, (*env)->NewStringUTF (env, r->val[i].%s));\n" name;
7505 pr " char s[33];\n";
7506 pr " memcpy (s, r->val[i].%s, 32);\n" name;
7508 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
7509 pr " (*env)->SetObjectField (env, jfl, fl, (*env)->NewStringUTF (env, s));\n";
7511 | name, (FBytes|FUInt64|FInt64) ->
7512 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"J\");\n" name;
7513 pr " (*env)->SetLongField (env, jfl, fl, r->val[i].%s);\n" name;
7514 | name, (FUInt32|FInt32) ->
7515 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"I\");\n" name;
7516 pr " (*env)->SetLongField (env, jfl, fl, r->val[i].%s);\n" name;
7517 | name, FOptPercent ->
7518 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"F\");\n" name;
7519 pr " (*env)->SetFloatField (env, jfl, fl, r->val[i].%s);\n" name;
7521 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"C\");\n" name;
7522 pr " (*env)->SetLongField (env, jfl, fl, r->val[i].%s);\n" name;
7524 pr " (*env)->SetObjectArrayElement (env, jfl, i, jfl);\n";
7526 pr " guestfs_free_%s_list (r);\n" typ;
7529 and generate_haskell_hs () =
7530 generate_header HaskellStyle LGPLv2;
7532 (* XXX We only know how to generate partial FFI for Haskell
7533 * at the moment. Please help out!
7535 let can_generate style =
7539 | RInt64 _, _ -> true
7546 | RHashtable _, _ -> false in
7549 {-# INCLUDE <guestfs.h> #-}
7550 {-# LANGUAGE ForeignFunctionInterface #-}
7555 (* List out the names of the actions we want to export. *)
7557 fun (name, style, _, _, _, _, _) ->
7558 if can_generate style then pr ",\n %s" name
7565 import Foreign.C.Types
7567 import Control.Exception
7568 import Data.Typeable
7570 data GuestfsS = GuestfsS -- represents the opaque C struct
7571 type GuestfsP = Ptr GuestfsS -- guestfs_h *
7572 type GuestfsH = ForeignPtr GuestfsS -- guestfs_h * with attached finalizer
7574 -- XXX define properly later XXX
7578 data IntBool = IntBool
7580 data StatVFS = StatVFS
7581 data Hashtable = Hashtable
7583 foreign import ccall unsafe \"guestfs_create\" c_create
7585 foreign import ccall unsafe \"&guestfs_close\" c_close
7586 :: FunPtr (GuestfsP -> IO ())
7587 foreign import ccall unsafe \"guestfs_set_error_handler\" c_set_error_handler
7588 :: GuestfsP -> Ptr CInt -> Ptr CInt -> IO ()
7590 create :: IO GuestfsH
7593 c_set_error_handler p nullPtr nullPtr
7594 h <- newForeignPtr c_close p
7597 foreign import ccall unsafe \"guestfs_last_error\" c_last_error
7598 :: GuestfsP -> IO CString
7600 -- last_error :: GuestfsH -> IO (Maybe String)
7601 -- last_error h = do
7602 -- str <- withForeignPtr h (\\p -> c_last_error p)
7603 -- maybePeek peekCString str
7605 last_error :: GuestfsH -> IO (String)
7607 str <- withForeignPtr h (\\p -> c_last_error p)
7609 then return \"no error\"
7610 else peekCString str
7614 (* Generate wrappers for each foreign function. *)
7616 fun (name, style, _, _, _, _, _) ->
7617 if can_generate style then (
7618 pr "foreign import ccall unsafe \"guestfs_%s\" c_%s\n" name name;
7620 generate_haskell_prototype ~handle:"GuestfsP" style;
7624 generate_haskell_prototype ~handle:"GuestfsH" ~hs:true style;
7626 pr "%s %s = do\n" name
7627 (String.concat " " ("h" :: List.map name_of_argt (snd style)));
7629 (* Convert pointer arguments using with* functions. *)
7634 | String n -> pr "withCString %s $ \\%s -> " n n
7635 | OptString n -> pr "maybeWith withCString %s $ \\%s -> " n n
7636 | StringList n -> pr "withMany withCString %s $ \\%s -> withArray0 nullPtr %s $ \\%s -> " n n n n
7637 | Bool _ | Int _ -> ()
7639 (* Convert integer arguments. *)
7643 | Bool n -> sprintf "(fromBool %s)" n
7644 | Int n -> sprintf "(fromIntegral %s)" n
7645 | FileIn n | FileOut n | String n | OptString n | StringList n -> n
7647 pr "withForeignPtr h (\\p -> c_%s %s)\n" name
7648 (String.concat " " ("p" :: args));
7649 (match fst style with
7650 | RErr | RInt _ | RInt64 _ | RBool _ ->
7651 pr " if (r == -1)\n";
7653 pr " err <- last_error h\n";
7655 | RConstString _ | RString _ | RStringList _ | RStruct _
7656 | RStructList _ | RHashtable _ ->
7657 pr " if (r == nullPtr)\n";
7659 pr " err <- last_error h\n";
7662 (match fst style with
7664 pr " else return ()\n"
7666 pr " else return (fromIntegral r)\n"
7668 pr " else return (fromIntegral r)\n"
7670 pr " else return (toBool r)\n"
7677 pr " else return ()\n" (* XXXXXXXXXXXXXXXXXXXX *)
7683 and generate_haskell_prototype ~handle ?(hs = false) style =
7685 let string = if hs then "String" else "CString" in
7686 let int = if hs then "Int" else "CInt" in
7687 let bool = if hs then "Bool" else "CInt" in
7688 let int64 = if hs then "Integer" else "Int64" in
7692 | String _ -> pr "%s" string
7693 | OptString _ -> if hs then pr "Maybe String" else pr "CString"
7694 | StringList _ -> if hs then pr "[String]" else pr "Ptr CString"
7695 | Bool _ -> pr "%s" bool
7696 | Int _ -> pr "%s" int
7697 | FileIn _ -> pr "%s" string
7698 | FileOut _ -> pr "%s" string
7703 (match fst style with
7704 | RErr -> if not hs then pr "CInt"
7705 | RInt _ -> pr "%s" int
7706 | RInt64 _ -> pr "%s" int64
7707 | RBool _ -> pr "%s" bool
7708 | RConstString _ -> pr "%s" string
7709 | RString _ -> pr "%s" string
7710 | RStringList _ -> pr "[%s]" string
7711 | RStruct (_, typ) ->
7712 let name = java_name_of_struct typ in
7714 | RStructList (_, typ) ->
7715 let name = java_name_of_struct typ in
7717 | RHashtable _ -> pr "Hashtable"
7721 and generate_bindtests () =
7722 generate_header CStyle LGPLv2;
7727 #include <inttypes.h>
7730 #include \"guestfs.h\"
7731 #include \"guestfs_protocol.h\"
7733 #define error guestfs_error
7734 #define safe_calloc guestfs_safe_calloc
7735 #define safe_malloc guestfs_safe_malloc
7738 print_strings (char * const* const argv)
7743 for (argc = 0; argv[argc] != NULL; ++argc) {
7744 if (argc > 0) printf (\", \");
7745 printf (\"\\\"%%s\\\"\", argv[argc]);
7750 /* The test0 function prints its parameters to stdout. */
7754 match test_functions with
7755 | [] -> assert false
7756 | test0 :: tests -> test0, tests in
7759 let (name, style, _, _, _, _, _) = test0 in
7760 generate_prototype ~extern:false ~semicolon:false ~newline:true
7761 ~handle:"g" ~prefix:"guestfs_" name style;
7767 | FileOut n -> pr " printf (\"%%s\\n\", %s);\n" n
7768 | OptString n -> pr " printf (\"%%s\\n\", %s ? %s : \"null\");\n" n n
7769 | StringList n -> pr " print_strings (%s);\n" n
7770 | Bool n -> pr " printf (\"%%s\\n\", %s ? \"true\" : \"false\");\n" n
7771 | Int n -> pr " printf (\"%%d\\n\", %s);\n" n
7773 pr " /* Java changes stdout line buffering so we need this: */\n";
7774 pr " fflush (stdout);\n";
7780 fun (name, style, _, _, _, _, _) ->
7781 if String.sub name (String.length name - 3) 3 <> "err" then (
7782 pr "/* Test normal return. */\n";
7783 generate_prototype ~extern:false ~semicolon:false ~newline:true
7784 ~handle:"g" ~prefix:"guestfs_" name style;
7786 (match fst style with
7791 pr " sscanf (val, \"%%d\", &r);\n";
7795 pr " sscanf (val, \"%%\" SCNi64, &r);\n";
7798 pr " return strcmp (val, \"true\") == 0;\n"
7800 (* Can't return the input string here. Return a static
7801 * string so we ensure we get a segfault if the caller
7804 pr " return \"static string\";\n"
7806 pr " return strdup (val);\n"
7808 pr " char **strs;\n";
7810 pr " sscanf (val, \"%%d\", &n);\n";
7811 pr " strs = safe_malloc (g, (n+1) * sizeof (char *));\n";
7812 pr " for (i = 0; i < n; ++i) {\n";
7813 pr " strs[i] = safe_malloc (g, 16);\n";
7814 pr " snprintf (strs[i], 16, \"%%d\", i);\n";
7816 pr " strs[n] = NULL;\n";
7817 pr " return strs;\n"
7818 | RStruct (_, typ) ->
7819 pr " struct guestfs_%s *r;\n" typ;
7820 pr " r = safe_calloc (g, sizeof *r, 1);\n";
7822 | RStructList (_, typ) ->
7823 pr " struct guestfs_%s_list *r;\n" typ;
7824 pr " r = safe_calloc (g, sizeof *r, 1);\n";
7825 pr " sscanf (val, \"%%d\", &r->len);\n";
7826 pr " r->val = safe_calloc (g, r->len, sizeof *r->val);\n";
7829 pr " char **strs;\n";
7831 pr " sscanf (val, \"%%d\", &n);\n";
7832 pr " strs = safe_malloc (g, (n*2+1) * sizeof (*strs));\n";
7833 pr " for (i = 0; i < n; ++i) {\n";
7834 pr " strs[i*2] = safe_malloc (g, 16);\n";
7835 pr " strs[i*2+1] = safe_malloc (g, 16);\n";
7836 pr " snprintf (strs[i*2], 16, \"%%d\", i);\n";
7837 pr " snprintf (strs[i*2+1], 16, \"%%d\", i);\n";
7839 pr " strs[n*2] = NULL;\n";
7840 pr " return strs;\n"
7845 pr "/* Test error return. */\n";
7846 generate_prototype ~extern:false ~semicolon:false ~newline:true
7847 ~handle:"g" ~prefix:"guestfs_" name style;
7849 pr " error (g, \"error\");\n";
7850 (match fst style with
7851 | RErr | RInt _ | RInt64 _ | RBool _ ->
7854 | RString _ | RStringList _ | RStruct _
7857 pr " return NULL;\n"
7864 and generate_ocaml_bindtests () =
7865 generate_header OCamlStyle GPLv2;
7869 let g = Guestfs.create () in
7876 | CallString s -> "\"" ^ s ^ "\""
7877 | CallOptString None -> "None"
7878 | CallOptString (Some s) -> sprintf "(Some \"%s\")" s
7879 | CallStringList xs ->
7880 "[|" ^ String.concat ";" (List.map (sprintf "\"%s\"") xs) ^ "|]"
7881 | CallInt i when i >= 0 -> string_of_int i
7882 | CallInt i (* when i < 0 *) -> "(" ^ string_of_int i ^ ")"
7883 | CallBool b -> string_of_bool b
7888 generate_lang_bindtests (
7889 fun f args -> pr " Guestfs.%s g %s;\n" f (mkargs args)
7892 pr "print_endline \"EOF\"\n"
7894 and generate_perl_bindtests () =
7895 pr "#!/usr/bin/perl -w\n";
7896 generate_header HashStyle GPLv2;
7903 my $g = Sys::Guestfs->new ();
7907 String.concat ", " (
7910 | CallString s -> "\"" ^ s ^ "\""
7911 | CallOptString None -> "undef"
7912 | CallOptString (Some s) -> sprintf "\"%s\"" s
7913 | CallStringList xs ->
7914 "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
7915 | CallInt i -> string_of_int i
7916 | CallBool b -> if b then "1" else "0"
7921 generate_lang_bindtests (
7922 fun f args -> pr "$g->%s (%s);\n" f (mkargs args)
7925 pr "print \"EOF\\n\"\n"
7927 and generate_python_bindtests () =
7928 generate_header HashStyle GPLv2;
7933 g = guestfs.GuestFS ()
7937 String.concat ", " (
7940 | CallString s -> "\"" ^ s ^ "\""
7941 | CallOptString None -> "None"
7942 | CallOptString (Some s) -> sprintf "\"%s\"" s
7943 | CallStringList xs ->
7944 "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
7945 | CallInt i -> string_of_int i
7946 | CallBool b -> if b then "1" else "0"
7951 generate_lang_bindtests (
7952 fun f args -> pr "g.%s (%s)\n" f (mkargs args)
7955 pr "print \"EOF\"\n"
7957 and generate_ruby_bindtests () =
7958 generate_header HashStyle GPLv2;
7963 g = Guestfs::create()
7967 String.concat ", " (
7970 | CallString s -> "\"" ^ s ^ "\""
7971 | CallOptString None -> "nil"
7972 | CallOptString (Some s) -> sprintf "\"%s\"" s
7973 | CallStringList xs ->
7974 "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
7975 | CallInt i -> string_of_int i
7976 | CallBool b -> string_of_bool b
7981 generate_lang_bindtests (
7982 fun f args -> pr "g.%s(%s)\n" f (mkargs args)
7985 pr "print \"EOF\\n\"\n"
7987 and generate_java_bindtests () =
7988 generate_header CStyle GPLv2;
7991 import com.redhat.et.libguestfs.*;
7993 public class Bindtests {
7994 public static void main (String[] argv)
7997 GuestFS g = new GuestFS ();
8001 String.concat ", " (
8004 | CallString s -> "\"" ^ s ^ "\""
8005 | CallOptString None -> "null"
8006 | CallOptString (Some s) -> sprintf "\"%s\"" s
8007 | CallStringList xs ->
8009 String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "}"
8010 | CallInt i -> string_of_int i
8011 | CallBool b -> string_of_bool b
8016 generate_lang_bindtests (
8017 fun f args -> pr " g.%s (%s);\n" f (mkargs args)
8021 System.out.println (\"EOF\");
8023 catch (Exception exn) {
8024 System.err.println (exn);
8031 and generate_haskell_bindtests () =
8032 generate_header HaskellStyle GPLv2;
8035 module Bindtests where
8036 import qualified Guestfs
8046 | CallString s -> "\"" ^ s ^ "\""
8047 | CallOptString None -> "Nothing"
8048 | CallOptString (Some s) -> sprintf "(Just \"%s\")" s
8049 | CallStringList xs ->
8050 "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
8051 | CallInt i when i < 0 -> "(" ^ string_of_int i ^ ")"
8052 | CallInt i -> string_of_int i
8053 | CallBool true -> "True"
8054 | CallBool false -> "False"
8059 generate_lang_bindtests (
8060 fun f args -> pr " Guestfs.%s g %s\n" f (mkargs args)
8063 pr " putStrLn \"EOF\"\n"
8065 (* Language-independent bindings tests - we do it this way to
8066 * ensure there is parity in testing bindings across all languages.
8068 and generate_lang_bindtests call =
8069 call "test0" [CallString "abc"; CallOptString (Some "def");
8070 CallStringList []; CallBool false;
8071 CallInt 0; CallString "123"; CallString "456"];
8072 call "test0" [CallString "abc"; CallOptString None;
8073 CallStringList []; CallBool false;
8074 CallInt 0; CallString "123"; CallString "456"];
8075 call "test0" [CallString ""; CallOptString (Some "def");
8076 CallStringList []; CallBool false;
8077 CallInt 0; CallString "123"; CallString "456"];
8078 call "test0" [CallString ""; CallOptString (Some "");
8079 CallStringList []; CallBool false;
8080 CallInt 0; CallString "123"; CallString "456"];
8081 call "test0" [CallString "abc"; CallOptString (Some "def");
8082 CallStringList ["1"]; CallBool false;
8083 CallInt 0; CallString "123"; CallString "456"];
8084 call "test0" [CallString "abc"; CallOptString (Some "def");
8085 CallStringList ["1"; "2"]; CallBool false;
8086 CallInt 0; CallString "123"; CallString "456"];
8087 call "test0" [CallString "abc"; CallOptString (Some "def");
8088 CallStringList ["1"]; CallBool true;
8089 CallInt 0; CallString "123"; CallString "456"];
8090 call "test0" [CallString "abc"; CallOptString (Some "def");
8091 CallStringList ["1"]; CallBool false;
8092 CallInt (-1); CallString "123"; CallString "456"];
8093 call "test0" [CallString "abc"; CallOptString (Some "def");
8094 CallStringList ["1"]; CallBool false;
8095 CallInt (-2); CallString "123"; CallString "456"];
8096 call "test0" [CallString "abc"; CallOptString (Some "def");
8097 CallStringList ["1"]; CallBool false;
8098 CallInt 1; CallString "123"; CallString "456"];
8099 call "test0" [CallString "abc"; CallOptString (Some "def");
8100 CallStringList ["1"]; CallBool false;
8101 CallInt 2; CallString "123"; CallString "456"];
8102 call "test0" [CallString "abc"; CallOptString (Some "def");
8103 CallStringList ["1"]; CallBool false;
8104 CallInt 4095; CallString "123"; CallString "456"];
8105 call "test0" [CallString "abc"; CallOptString (Some "def");
8106 CallStringList ["1"]; CallBool false;
8107 CallInt 0; CallString ""; CallString ""]
8109 (* XXX Add here tests of the return and error functions. *)
8111 (* This is used to generate the src/MAX_PROC_NR file which
8112 * contains the maximum procedure number, a surrogate for the
8113 * ABI version number. See src/Makefile.am for the details.
8115 and generate_max_proc_nr () =
8116 let proc_nrs = List.map (
8117 fun (_, _, proc_nr, _, _, _, _) -> proc_nr
8118 ) daemon_functions in
8120 let max_proc_nr = List.fold_left max 0 proc_nrs in
8122 pr "%d\n" max_proc_nr
8124 let output_to filename =
8125 let filename_new = filename ^ ".new" in
8126 chan := open_out filename_new;
8131 (* Is the new file different from the current file? *)
8132 if Sys.file_exists filename && files_equal filename filename_new then
8133 Unix.unlink filename_new (* same, so skip it *)
8135 (* different, overwrite old one *)
8136 (try Unix.chmod filename 0o644 with Unix.Unix_error _ -> ());
8137 Unix.rename filename_new filename;
8138 Unix.chmod filename 0o444;
8139 printf "written %s\n%!" filename;
8148 if not (Sys.file_exists "HACKING") then (
8150 You are probably running this from the wrong directory.
8151 Run it from the top source directory using the command
8157 let close = output_to "src/guestfs_protocol.x" in
8161 let close = output_to "src/guestfs-structs.h" in
8162 generate_structs_h ();
8165 let close = output_to "src/guestfs-actions.h" in
8166 generate_actions_h ();
8169 let close = output_to "src/guestfs-actions.c" in
8170 generate_client_actions ();
8173 let close = output_to "daemon/actions.h" in
8174 generate_daemon_actions_h ();
8177 let close = output_to "daemon/stubs.c" in
8178 generate_daemon_actions ();
8181 let close = output_to "daemon/names.c" in
8182 generate_daemon_names ();
8185 let close = output_to "capitests/tests.c" in
8189 let close = output_to "src/guestfs-bindtests.c" in
8190 generate_bindtests ();
8193 let close = output_to "fish/cmds.c" in
8194 generate_fish_cmds ();
8197 let close = output_to "fish/completion.c" in
8198 generate_fish_completion ();
8201 let close = output_to "guestfs-structs.pod" in
8202 generate_structs_pod ();
8205 let close = output_to "guestfs-actions.pod" in
8206 generate_actions_pod ();
8209 let close = output_to "guestfish-actions.pod" in
8210 generate_fish_actions_pod ();
8213 let close = output_to "ocaml/guestfs.mli" in
8214 generate_ocaml_mli ();
8217 let close = output_to "ocaml/guestfs.ml" in
8218 generate_ocaml_ml ();
8221 let close = output_to "ocaml/guestfs_c_actions.c" in
8222 generate_ocaml_c ();
8225 let close = output_to "ocaml/bindtests.ml" in
8226 generate_ocaml_bindtests ();
8229 let close = output_to "perl/Guestfs.xs" in
8230 generate_perl_xs ();
8233 let close = output_to "perl/lib/Sys/Guestfs.pm" in
8234 generate_perl_pm ();
8237 let close = output_to "perl/bindtests.pl" in
8238 generate_perl_bindtests ();
8241 let close = output_to "python/guestfs-py.c" in
8242 generate_python_c ();
8245 let close = output_to "python/guestfs.py" in
8246 generate_python_py ();
8249 let close = output_to "python/bindtests.py" in
8250 generate_python_bindtests ();
8253 let close = output_to "ruby/ext/guestfs/_guestfs.c" in
8257 let close = output_to "ruby/bindtests.rb" in
8258 generate_ruby_bindtests ();
8261 let close = output_to "java/com/redhat/et/libguestfs/GuestFS.java" in
8262 generate_java_java ();
8267 let cols = cols_of_struct typ in
8268 let filename = sprintf "java/com/redhat/et/libguestfs/%s.java" jtyp in
8269 let close = output_to filename in
8270 generate_java_struct jtyp cols;
8274 let close = output_to "java/com_redhat_et_libguestfs_GuestFS.c" in
8278 let close = output_to "java/Bindtests.java" in
8279 generate_java_bindtests ();
8282 let close = output_to "haskell/Guestfs.hs" in
8283 generate_haskell_hs ();
8286 let close = output_to "haskell/Bindtests.hs" in
8287 generate_haskell_bindtests ();
8290 let close = output_to "src/MAX_PROC_NR" in
8291 generate_max_proc_nr ();
8294 (* Always generate this file last, and unconditionally. It's used
8295 * by the Makefile to know when we must re-run the generator.
8297 let chan = open_out "src/stamp-generator" in