Richard W.M. Jones
2013-Aug-16 16:14 UTC
[Libguestfs] [PATCH v2] sysprep: added --mount-options option to mount selected
Nikita, Please take a look at the attached patch. I have rewritten it a little, and only lightly tested it. Rich.
Richard W.M. Jones
2013-Aug-16 16:14 UTC
[Libguestfs] [PATCH v2] sysprep: added --mount-options option to mount selected partitions with options.
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
Nikita A Menkovich
2013-Aug-19 07:05 UTC
[Libguestfs] [PATCH v2] sysprep: added --mount-options option to mount selected
Thanks, Rich. Your patch to my patch make it easier to read. At weekend I make more copmplex testing and everything works fine for me. On 16 August 2013 20:14, Richard W.M. Jones <rjones at redhat.com> wrote:> Nikita, > > Please take a look at the attached patch. I have rewritten it > a little, and only lightly tested it. > > Rich. > >-- Nikita A Menkovich http://libc6.org/ JID: menkovich at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://listman.redhat.com/archives/libguestfs/attachments/20130819/ee8ed54d/attachment.htm>
Richard W.M. Jones
2013-Aug-19 07:32 UTC
Re: [Libguestfs] [PATCH v2] sysprep: added --mount-options option to mount selected
On Mon, Aug 19, 2013 at 11:05:03AM +0400, Nikita A Menkovich wrote:> Thanks, Rich. > > Your patch to my patch make it easier to read. > > At weekend I make more copmplex testing and everything works fine for me.OK thanks for testing it, I have pushed it. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://people.redhat.com/~rjones/virt-df/
Reasonably Related Threads
- Re: [Bug 1046905] New: RFE: add argument to virt-sysprep to disable individual default operations
- [PATCH 0/5] mllib: Hide bad String functions and miscellaneous refactoring.
- [PATCH (incomplete)] Rewrite virt-sysprep in OCaml.
- [PATCH 5/5] mllib: Replace various ad hoc string_* functions with String.*
- [PATCH] sysprep: add --operations