X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=src%2Fgenerator.ml;h=7d210654dbea4d764b113b1220567df8bc6d209d;hp=a01eeb7983a177efd38c558c3bd2be05c4710740;hb=d9ea3e8d979c3ade1b21f27083788fd33fa3b1fa;hpb=6dff51fc7d8a33925f164bd66c2c26cd4b94b3df diff --git a/src/generator.ml b/src/generator.ml index a01eeb7..7d21065 100755 --- a/src/generator.ml +++ b/src/generator.ml @@ -339,8 +339,12 @@ return the default path."); "set autosync mode", "\ If C is true, this enables autosync. Libguestfs will make a -best effort attempt to run C when the handle is closed -(also if the program exits without closing handles)."); +best effort attempt to run C followed by +C when the handle is closed +(also if the program exits without closing handles). + +This is disabled by default (except in guestfish where it is +enabled by default)."); ("get_autosync", (RBool "autosync", []), -1, [], [], @@ -1074,6 +1078,20 @@ Some internal mounts are not shown."); ("umount_all", (RErr, []), 47, [FishAlias "unmount-all"], [InitBasicFS, TestOutputList ( [["umount_all"]; + ["mounts"]], []); + (* check that umount_all can unmount nested mounts correctly: *) + InitEmpty, TestOutputList ( + [["sfdisk"; "/dev/sda"; "0"; "0"; "0"; ",10 ,20 ,"]; + ["mkfs"; "ext2"; "/dev/sda1"]; + ["mkfs"; "ext2"; "/dev/sda2"]; + ["mkfs"; "ext2"; "/dev/sda3"]; + ["mount"; "/dev/sda1"; "/"]; + ["mkdir"; "/mp1"]; + ["mount"; "/dev/sda2"; "/mp1"]; + ["mkdir"; "/mp1/mp2"]; + ["mount"; "/dev/sda3"; "/mp1/mp2"]; + ["mkdir"; "/mp1/mp2/mp3"]; + ["umount_all"]; ["mounts"]], [])], "unmount all filesystems", "\ @@ -1614,6 +1632,168 @@ to return the existing UUID of a filesystem."); This returns the ext2/3/4 filesystem UUID of the filesystem on C."); + ("fsck", (RInt "status", [String "fstype"; String "device"]), 84, [], + [InitBasicFS, TestOutputInt ( + [["umount"; "/dev/sda1"]; + ["fsck"; "ext2"; "/dev/sda1"]], 0); + InitBasicFS, TestOutputInt ( + [["umount"; "/dev/sda1"]; + ["zero"; "/dev/sda1"]; + ["fsck"; "ext2"; "/dev/sda1"]], 8)], + "run the filesystem checker", + "\ +This runs the filesystem checker (fsck) on C which +should have filesystem type C. + +The returned integer is the status. See L for the +list of status codes from C. + +Notes: + +=over 4 + +=item * + +Multiple status codes can be summed together. + +=item * + +A non-zero return code can mean \"success\", for example if +errors have been corrected on the filesystem. + +=item * + +Checking or repairing NTFS volumes is not supported +(by linux-ntfs). + +=back + +This command is entirely equivalent to running C."); + + ("zero", (RErr, [String "device"]), 85, [], + [InitBasicFS, TestOutput ( + [["umount"; "/dev/sda1"]; + ["zero"; "/dev/sda1"]; + ["file"; "/dev/sda1"]], "data")], + "write zeroes to the device", + "\ +This command writes zeroes over the first few blocks of C. + +How many blocks are zeroed isn't specified (but it's I enough +to securely wipe the device). It should be sufficient to remove +any partition tables, filesystem superblocks and so on."); + + ("grub_install", (RErr, [String "root"; String "device"]), 86, [], + [InitBasicFS, TestOutputTrue ( + [["grub_install"; "/"; "/dev/sda1"]; + ["is_dir"; "/boot"]])], + "install GRUB", + "\ +This command installs GRUB (the Grand Unified Bootloader) on +C, with the root directory being C."); + + ("cp", (RErr, [String "src"; String "dest"]), 87, [], + [InitBasicFS, TestOutput ( + [["write_file"; "/old"; "file content"; "0"]; + ["cp"; "/old"; "/new"]; + ["cat"; "/new"]], "file content"); + InitBasicFS, TestOutputTrue ( + [["write_file"; "/old"; "file content"; "0"]; + ["cp"; "/old"; "/new"]; + ["is_file"; "/old"]]); + InitBasicFS, TestOutput ( + [["write_file"; "/old"; "file content"; "0"]; + ["mkdir"; "/dir"]; + ["cp"; "/old"; "/dir/new"]; + ["cat"; "/dir/new"]], "file content")], + "copy a file", + "\ +This copies a file from C to C where C is +either a destination filename or destination directory."); + + ("cp_a", (RErr, [String "src"; String "dest"]), 88, [], + [InitBasicFS, TestOutput ( + [["mkdir"; "/olddir"]; + ["mkdir"; "/newdir"]; + ["write_file"; "/olddir/file"; "file content"; "0"]; + ["cp_a"; "/olddir"; "/newdir"]; + ["cat"; "/newdir/olddir/file"]], "file content")], + "copy a file or directory recursively", + "\ +This copies a file or directory from C to C +recursively using the C command."); + + ("mv", (RErr, [String "src"; String "dest"]), 89, [], + [InitBasicFS, TestOutput ( + [["write_file"; "/old"; "file content"; "0"]; + ["mv"; "/old"; "/new"]; + ["cat"; "/new"]], "file content"); + InitBasicFS, TestOutputFalse ( + [["write_file"; "/old"; "file content"; "0"]; + ["mv"; "/old"; "/new"]; + ["is_file"; "/old"]])], + "move a file", + "\ +This moves a file from C to C where C is +either a destination filename or destination directory."); + + ("drop_caches", (RErr, [Int "whattodrop"]), 90, [], + [InitEmpty, TestRun ( + [["drop_caches"; "3"]])], + "drop kernel page cache, dentries and inodes", + "\ +This instructs the guest kernel to drop its page cache, +and/or dentries and inode caches. The parameter C +tells the kernel what precisely to drop, see +L + +Setting C to 3 should drop everything. + +This automatically calls L before the operation, +so that the maximum guest memory is freed."); + + ("dmesg", (RString "kmsgs", []), 91, [], + [InitEmpty, TestRun ( + [["dmesg"]])], + "return kernel messages", + "\ +This returns the kernel messages (C output) from +the guest kernel. This is sometimes useful for extended +debugging of problems. + +Another way to get the same information is to enable +verbose messages with C or by setting +the environment variable C before +running the program."); + + ("ping_daemon", (RErr, []), 92, [], + [InitEmpty, TestRun ( + [["ping_daemon"]])], + "ping the guest daemon", + "\ +This is a test probe into the guestfs daemon running inside +the qemu subprocess. Calling this function checks that the +daemon responds to the ping message, without affecting the daemon +or attached block device(s) in any other way."); + + ("equal", (RBool "equality", [String "file1"; String "file2"]), 93, [], + [InitBasicFS, TestOutputTrue ( + [["write_file"; "/file1"; "contents of a file"; "0"]; + ["cp"; "/file1"; "/file2"]; + ["equal"; "/file1"; "/file2"]]); + InitBasicFS, TestOutputFalse ( + [["write_file"; "/file1"; "contents of a file"; "0"]; + ["write_file"; "/file2"; "contents of another file"; "0"]; + ["equal"; "/file1"; "/file2"]]); + InitBasicFS, TestLastFail ( + [["equal"; "/file1"; "/file2"]])], + "test if two files have equal contents", + "\ +This compares the two files C and C and returns +true if their content is exactly equal, or false otherwise. + +The external L program is used for the comparison."); + ] let all_functions = non_daemon_functions @ daemon_functions @@ -1804,6 +1984,13 @@ let rec string_split sep str = s' :: string_split sep s'' ) +let files_equal n1 n2 = + let cmd = sprintf "cmp -s %s %s" (Filename.quote n1) (Filename.quote n2) in + match Sys.command cmd with + | 0 -> true + | 1 -> false + | i -> failwithf "%s: failed with error code %d" cmd i + let rec find_map f = function | [] -> raise Not_found | x :: xs -> @@ -3904,7 +4091,7 @@ and generate_fish_completion () = #ifdef HAVE_LIBREADLINE -static const char *commands[] = { +static const char *const commands[] = { "; (* Get the commands and sort them, including the aliases. *) @@ -4334,7 +4521,7 @@ copy_table (char * const * argv) pr " %sv != Val_int (0) ? String_val (Field (%sv, 0)) : NULL;\n" n n | StringList n -> - pr " char **%s = ocaml_guestfs_strings_val (%sv);\n" n n + pr " char **%s = ocaml_guestfs_strings_val (g, %sv);\n" n n | Bool n -> pr " int %s = Bool_val (%sv);\n" n n | Int n -> @@ -4556,12 +4743,13 @@ XS_unpack_charPtrPtr (SV *arg) { AV *av; I32 i; - if (!arg || !SvOK (arg) || !SvROK (arg) || SvTYPE (SvRV (arg)) != SVt_PVAV) { + if (!arg || !SvOK (arg) || !SvROK (arg) || SvTYPE (SvRV (arg)) != SVt_PVAV) croak (\"array reference expected\"); - } av = (AV *)SvRV (arg); - ret = (char **)malloc (av_len (av) + 1 + 1); + ret = malloc (av_len (av) + 1 + 1); + if (!ret) + croak (\"malloc failed\"); for (i = 0; i <= av_len (av); i++) { SV **elem = av_fetch (av, i, 0); @@ -5555,7 +5743,8 @@ static VALUE ruby_guestfs_close (VALUE gv) pr " {\n"; pr " int i, len;\n"; pr " len = RARRAY_LEN (%sv);\n" n; - pr " %s = malloc (sizeof (char *) * (len+1));\n" n; + pr " %s = guestfs_safe_malloc (g, sizeof (char *) * (len+1));\n" + n; pr " for (i = 0; i < len; ++i) {\n"; pr " VALUE v = rb_ary_entry (%sv, i);\n" n; pr " %s[i] = StringValueCStr (v);\n" n; @@ -6075,7 +6264,7 @@ Java_com_redhat_et_libguestfs_GuestFS__1close pr " %s = (*env)->GetStringUTFChars (env, j%s, NULL);\n" n n | StringList n -> pr " %s_len = (*env)->GetArrayLength (env, j%s);\n" n n; - pr " %s = malloc (sizeof (char *) * (%s_len+1));\n" n n; + pr " %s = guestfs_safe_malloc (g, sizeof (char *) * (%s_len+1));\n" n n; pr " for (i = 0; i < %s_len; ++i) {\n" n; pr " jobject o = (*env)->GetObjectArrayElement (env, j%s, i);\n" n; @@ -6225,8 +6414,17 @@ let output_to filename = let close () = close_out !chan; chan := stdout; - Unix.rename filename_new filename; - printf "written %s\n%!" filename; + + (* Is the new file different from the current file? *) + if Sys.file_exists filename && files_equal filename filename_new then + Unix.unlink filename_new (* same, so skip it *) + else ( + (* different, overwrite old one *) + (try Unix.chmod filename 0o644 with Unix.Unix_error _ -> ()); + Unix.rename filename_new filename; + Unix.chmod filename 0o444; + printf "written %s\n%!" filename; + ) in close