[Libguestfs] [PATCH v2] sysprep: added --mount-options option to mount selected partitions with options.

Richard W.M. Jones rjones at redhat.com
Fri Aug 16 16:14:28 UTC 2013


From: Nikita Menkovich <n.menkovich at sprinthost.ru>

---
 resize/common_utils.ml   | 19 +++++++++++++++++--
 sysprep/main.ml          | 22 ++++++++++++++++++----
 sysprep/virt-sysprep.pod | 16 ++++++++++++++++
 3 files changed, 51 insertions(+), 6 deletions(-)

diff --git a/resize/common_utils.ml b/resize/common_utils.ml
index 37da8df..fd302f1 100644
--- a/resize/common_utils.ml
+++ b/resize/common_utils.ml
@@ -108,7 +108,8 @@ let rec replace_str s s1 s2 =
     s' ^ s2 ^ replace_str s'' s1 s2
   )
 
-let rec string_split sep str =
+(* Split a string into multiple strings at each separator. *)
+let rec string_nsplit sep str =
   let len = String.length str in
   let seplen = String.length sep in
   let i = string_find str sep in
@@ -116,7 +117,21 @@ let rec string_split sep str =
   else (
     let s' = String.sub str 0 i in
     let s'' = String.sub str (i+seplen) (len-i-seplen) in
-    s' :: string_split sep s''
+    s' :: string_nsplit sep s''
+  )
+
+(* Split a string at the first occurrence of the separator, returning
+ * the part before and the part after.  If separator is not found,
+ * return the whole string and an empty string.
+ *)
+let string_split sep str =
+  let len = String.length sep in
+  let seplen = String.length str in
+  let i = string_find str sep in
+
+  if i = -1 then str, ""
+  else (
+    String.sub str 0 i, String.sub str (i + len) (seplen - i - len)
   )
 
 let string_random8 =
diff --git a/sysprep/main.ml b/sysprep/main.ml
index 49b0eb3..e5fe9c4 100644
--- a/sysprep/main.ml
+++ b/sysprep/main.ml
@@ -31,7 +31,7 @@ let () = Sysprep_operation.bake ()
 (* Command line argument parsing. *)
 let prog = Filename.basename Sys.executable_name
 
-let debug_gc, operations, g, selinux_relabel, quiet =
+let debug_gc, operations, g, selinux_relabel, quiet, mount_opts =
   let debug_gc = ref false in
   let domain = ref None in
   let dryrun = ref false in
@@ -43,6 +43,7 @@ let debug_gc, operations, g, selinux_relabel, quiet =
   let selinux_relabel = ref `Auto in
   let trace = ref false in
   let verbose = ref false in
+  let mount_opts = ref "" in
 
   let display_version () =
     let g = new G.guestfs () in
@@ -79,7 +80,7 @@ let debug_gc, operations, g, selinux_relabel, quiet =
       eprintf (f_"%s: you cannot pass an empty argument to --enable\n") prog;
       exit 1
     );
-    let ops = string_split "," ops in
+    let ops = string_nsplit "," ops in
     let opset = List.fold_left (
       fun opset op_name ->
         try Sysprep_operation.add_to_set op_name opset
@@ -115,6 +116,7 @@ let debug_gc, operations, g, selinux_relabel, quiet =
     "--format",  Arg.Set_string format,     s_"format" ^ " " ^ s_"Set format (default: auto)";
     "--list-operations", Arg.Unit list_operations, " " ^ s_"List supported operations";
     "--long-options", Arg.Unit display_long_options, " " ^ s_"List long options";
+    "--mount-options", Arg.Set_string mount_opts, s_"opts" ^ " " ^ s_"Set mount options (/:noatime;/var:rw,noatime)";
     "-q",        Arg.Set quiet,             " " ^ s_"Don't print log messages";
     "--quiet",   Arg.Set quiet,             " " ^ s_"Don't print log messages";
     "--selinux-relabel", Arg.Unit force_selinux_relabel, " " ^ s_"Force SELinux relabel";
@@ -183,6 +185,15 @@ read the man page virt-sysprep(1).
   let trace = !trace in
   let verbose = !verbose in
 
+  (* Parse the mount options string into a function that maps the
+   * mountpoint to the mount options.
+   *)
+  let mount_opts = !mount_opts in
+  let mount_opts =
+    List.map (string_split ":") (string_nsplit ";" mount_opts) in
+  let mount_opts mp =
+    try List.assoc mp mount_opts with Not_found -> "" in
+
   if not quiet then
     printf (f_"Examining the guest ...\n%!");
 
@@ -193,7 +204,7 @@ read the man page virt-sysprep(1).
   add g dryrun;
   g#launch ();
 
-  debug_gc, operations, g, selinux_relabel, quiet
+  debug_gc, operations, g, selinux_relabel, quiet, mount_opts
 
 let do_sysprep () =
   (* Inspection. *)
@@ -212,7 +223,10 @@ let do_sysprep () =
         let mps = List.sort cmp mps in
         List.iter (
           fun (mp, dev) ->
-            try g#mount dev mp
+            (* Get mount options for this mountpoint. *)
+            let opts = mount_opts mp in
+
+            try g#mount_options opts dev mp;
             with Guestfs.Error msg -> eprintf (f_"%s (ignored)\n") msg
         ) mps;
 
diff --git a/sysprep/virt-sysprep.pod b/sysprep/virt-sysprep.pod
index 5b4b058..bbf4cca 100755
--- a/sysprep/virt-sysprep.pod
+++ b/sysprep/virt-sysprep.pod
@@ -153,6 +153,22 @@ fields on the same line are the description of the operation.
 Before libguestfs 1.17.33 only the first (operation name) field was
 shown and all operations were enabled by default.
 
+=item B<--mount-options> mp:opts[;mp:opts;...]
+
+Set the mount options for each mountpoint in the guest.  Use a
+semicolon-separated list of C<mountpoint:options> pairs.  You may need
+to quote this list to protect it from the shell.
+
+For example:
+
+ --mount-options "/:noatime"
+
+will mount the root directory with C<notime>.  This example:
+
+ --mount-options "/:noatime;/var:rw,nodiratime"
+
+will do the same, plus mount C</var> with C<rw,nodiratime>.
+
 =item B<-q>
 
 =item B<--quiet>
-- 
1.8.3.1




More information about the Libguestfs mailing list