[Libguestfs] [PATCH] virt-sysprep:add logging feature

Wanlong Gao gaowanlong at cn.fujitsu.com
Fri Apr 6 05:37:33 UTC 2012


Hi Rich,

I tried to implement the logging feature, but I can't though
compiling with this patch now, could you please give me
some comments?

The error message is below,
---
ocamlfind ocamlopt -g -warn-error CDEFLMPSUVYZX -package unix -I ../src/.libs -I ../ocaml -c sysprep_operation.ml -o sysprep_operation.cmx
File "sysprep_operation.ml", line 1, characters 0-1:
Error: The implementation sysprep_operation.ml
       does not match the interface sysprep_operation.cmi:
       Type declarations do not match:
         type operation = {
           name : string;
           pod_description : string;
           extra_args : ((Arg.key * Arg.spec * Arg.doc) * string) list;
           perform : Guestfs.guestfs -> string -> bool -> flag list;
         }
       is not included in
         type operation = {
           name : string;
           pod_description : string;
           extra_args : ((Arg.key * Arg.spec * Arg.doc) * string) list;
           perform : Guestfs.guestfs -> string -> flag list;
         }
       The types for field perform are not equal.
make[2]: *** [sysprep_operation.cmx] Error 2
make[2]: Leaving directory `/work/git/libguestfs/sysprep'

Thanks a lot,
Wanlong Gao

Signed-off-by: Wanlong Gao <gaowanlong at cn.fujitsu.com>
---
 sysprep/main.ml                                  |    9 ++++++---
 sysprep/sysprep_operation.ml                     |    6 +++---
 sysprep/sysprep_operation_cron_spool.ml          |   10 ++++++++--
 sysprep/sysprep_operation_dhcp_client_state.ml   |    9 +++++++--
 sysprep/sysprep_operation_dhcp_server_state.ml   |    8 ++++++--
 sysprep/sysprep_operation_hostname.ml            |    4 +++-
 sysprep/sysprep_operation_logfiles.ml            |   11 +++++++++--
 sysprep/sysprep_operation_mail_spool.ml          |    9 +++++++--
 sysprep/sysprep_operation_net_hwaddr.ml          |    3 ++-
 sysprep/sysprep_operation_random_seed.ml         |    3 ++-
 sysprep/sysprep_operation_rhn_systemid.ml        |    8 ++++++--
 sysprep/sysprep_operation_script.ml              |    2 +-
 sysprep/sysprep_operation_smolt_uuid.ml          |    8 ++++++--
 sysprep/sysprep_operation_ssh_hostkeys.ml        |    8 ++++++--
 sysprep/sysprep_operation_udev_persistent_net.ml |    7 +++++--
 sysprep/sysprep_operation_utmp.ml                |    7 +++++--
 sysprep/sysprep_operation_yum_uuid.ml            |    8 ++++++--
 sysprep/utils.ml                                 |    4 ++++
 18 files changed, 92 insertions(+), 32 deletions(-)

diff --git a/sysprep/main.ml b/sysprep/main.ml
index d06b0d2..7d3ce5e 100644
--- a/sysprep/main.ml
+++ b/sysprep/main.ml
@@ -29,7 +29,7 @@ let () = Sysprep_operation.bake ()
 (* Command line argument parsing. *)
 let prog = Filename.basename Sys.executable_name
 
-let debug_gc, operations, g, selinux_relabel =
+let debug_gc, operations, g, selinux_relabel, show_log =
   let debug_gc = ref false in
   let domain = ref None in
   let dryrun = ref false in
@@ -40,6 +40,7 @@ let debug_gc, operations, g, selinux_relabel =
   let selinux_relabel = ref `Auto in
   let trace = ref false in
   let verbose = ref false in
+  let show_log = ref false in
 
   let display_version () =
     let g = new G.guestfs () in
@@ -112,6 +113,7 @@ let debug_gc, operations, g, selinux_relabel =
     "-V",        Arg.Unit display_version,  " Display version and exit";
     "--version", Arg.Unit display_version,  " -\"-";
     "-x",        Arg.Set trace,             " Enable tracing of libguestfs calls";
+    "--log",     Arg.Set show_log,          " Enable operation logging";
   ] @ Sysprep_operation.extra_args () in
   let anon_fun _ = raise (Arg.Bad "extra parameter on the command line") in
   let usage_msg =
@@ -162,6 +164,7 @@ read the man page virt-sysprep(1).
   let selinux_relabel = !selinux_relabel in
   let trace = !trace in
   let verbose = !verbose in
+  let show_log = !show_log in
 
   (* Connect to libguestfs. *)
   let g = new G.guestfs () in
@@ -170,7 +173,7 @@ read the man page virt-sysprep(1).
   add g dryrun;
   g#launch ();
 
-  debug_gc, operations, g, selinux_relabel
+  debug_gc, operations, g, selinux_relabel, show_log
 
 let () =
   (* Inspection. *)
@@ -194,7 +197,7 @@ let () =
         ) mps;
 
         (* Perform the operations. *)
-        let flags = Sysprep_operation.perform_operations ?operations g root in
+        let flags = Sysprep_operation.perform_operations ?operations g root show_log in
 
         (* Parse flags. *)
         let relabel = ref false in
diff --git a/sysprep/sysprep_operation.ml b/sysprep/sysprep_operation.ml
index 3fd8afc..65013c5 100644
--- a/sysprep/sysprep_operation.ml
+++ b/sysprep/sysprep_operation.ml
@@ -24,7 +24,7 @@ type operation = {
   name : string;
   pod_description : string;
   extra_args : ((Arg.key * Arg.spec * Arg.doc) * string) list;
-  perform : Guestfs.guestfs -> string -> flag list;
+  perform : Guestfs.guestfs -> string -> bool -> flag list;
 }
 
 let ops = ref []
@@ -169,7 +169,7 @@ let list_operations () =
    *)
   List.iter (fun op -> print_endline op.name ) !ops
 
-let perform_operations ?operations g root =
+let perform_operations ?operations g root show_log =
   assert !baked;
 
   let ops =
@@ -182,7 +182,7 @@ let perform_operations ?operations g root =
     List.map (
       fun op ->
         (* eprintf "calling %S.perform g %S\n" op.name root; *)
-        op.perform g root
+        op.perform g root show_log
     ) ops in
 
   List.flatten flags
diff --git a/sysprep/sysprep_operation_cron_spool.ml b/sysprep/sysprep_operation_cron_spool.ml
index e67688b..6bd6ec4 100644
--- a/sysprep/sysprep_operation_cron_spool.ml
+++ b/sysprep/sysprep_operation_cron_spool.ml
@@ -17,11 +17,17 @@
  *)
 
 open Sysprep_operation
+open Utils
 
 module G = Guestfs
 
-let cron_spool_perform g root =
-  Array.iter g#rm_rf (g#glob_expand "/var/spool/cron/*");
+let cron_spool_perform g root show_log =
+  let files = g#glob_expand "/var/spool/cron/*" in
+  Array.iter (
+    fun glob ->
+      g#rm glob
+      logging show_log "Deleted %s" glob
+  ) files;
   []
 
 let cron_spool_op = {
diff --git a/sysprep/sysprep_operation_dhcp_client_state.ml b/sysprep/sysprep_operation_dhcp_client_state.ml
index e3e87cb..17bb65b 100644
--- a/sysprep/sysprep_operation_dhcp_client_state.ml
+++ b/sysprep/sysprep_operation_dhcp_client_state.ml
@@ -20,11 +20,16 @@ open Sysprep_operation
 
 module G = Guestfs
 
-let dhcp_client_state_perform g root =
+let dhcp_client_state_perform g root show_log =
   let typ = g#inspect_get_type root in
   if typ = "linux" then (
     List.iter (
-      fun glob -> Array.iter g#rm_rf (g#glob_expand glob)
+      fun glob ->
+      Array.iter (
+        fun glob2 ->
+          g#rm_rf glob2
+          logging show_log "Deleted %s" glob2
+      ) (g#glob_expand glob)
     ) [ "/var/lib/dhclient/*"; "/var/lib/dhcp/*" (* RHEL 3 *) ]
   );
   []
diff --git a/sysprep/sysprep_operation_dhcp_server_state.ml b/sysprep/sysprep_operation_dhcp_server_state.ml
index c5251ce..604a601 100644
--- a/sysprep/sysprep_operation_dhcp_server_state.ml
+++ b/sysprep/sysprep_operation_dhcp_server_state.ml
@@ -20,8 +20,12 @@ open Sysprep_operation
 
 module G = Guestfs
 
-let dhcp_server_state_perform g root =
-  Array.iter g#rm_rf (g#glob_expand "/var/lib/dhcpd/*");
+let dhcp_server_state_perform g root show_log =
+  Array.iter (
+    fun glob ->
+    g#rm_rf glob
+    logging show_log "Deleted %s" glob
+  ) (g#glob_expand "/var/lib/dhcpd/*");
   []
 
 let dhcp_server_state_op = {
diff --git a/sysprep/sysprep_operation_hostname.ml b/sysprep/sysprep_operation_hostname.ml
index 1472a1c..caf5358 100644
--- a/sysprep/sysprep_operation_hostname.ml
+++ b/sysprep/sysprep_operation_hostname.ml
@@ -25,7 +25,7 @@ module G = Guestfs
 
 let hostname = ref "localhost.localdomain"
 
-let hostname_perform g root =
+let hostname_perform g root show_log =
   let typ = g#inspect_get_type root in
   let distro = g#inspect_get_distro root in
   match typ, distro with
@@ -42,10 +42,12 @@ let hostname_perform g root =
       String.concat "\n" lines ^
       sprintf "\nHOSTNAME=%s\n" !hostname in
     g#write filename file;
+    logging show_log "Modified HOSTNAME to %s" !hostname
     [ `Created_files ]
 
   | "linux", ("debian"|"ubuntu") ->
     g#write "/etc/hostname" !hostname;
+    logging show_log "Modified HOSTNAME to %s" !hostname
     [ `Created_files ]
 
   | _ -> []
diff --git a/sysprep/sysprep_operation_logfiles.ml b/sysprep/sysprep_operation_logfiles.ml
index b4c6ea9..ba7fe45 100644
--- a/sysprep/sysprep_operation_logfiles.ml
+++ b/sysprep/sysprep_operation_logfiles.ml
@@ -50,10 +50,17 @@ let globs = List.sort compare [
 ]
 let globs_as_pod = String.concat "\n" (List.map ((^) " ") globs)
 
-let logfiles_perform g root =
+let logfiles_perform g root show_log =
   let typ = g#inspect_get_type root in
   if typ = "linux" then (
-    List.iter (fun glob -> Array.iter g#rm_rf (g#glob_expand glob)) globs
+    List.iter (
+      fun glob ->
+        Array.iter (
+          fun glob2 ->
+            g#rm_rf glob2
+            logging show_log "Deleted %s\n" glob2
+        ) (g#glob_expand glob);
+    ) globs;
   );
   []
 
diff --git a/sysprep/sysprep_operation_mail_spool.ml b/sysprep/sysprep_operation_mail_spool.ml
index 74f2d94..2add3cb 100644
--- a/sysprep/sysprep_operation_mail_spool.ml
+++ b/sysprep/sysprep_operation_mail_spool.ml
@@ -20,9 +20,14 @@ open Sysprep_operation
 
 module G = Guestfs
 
-let mail_spool_perform g root =
+let mail_spool_perform g root show_log =
   List.iter (
-    fun glob -> Array.iter g#rm_rf (g#glob_expand glob)
+    fun glob ->
+      Array.iter (
+        fun glob2 ->
+          g#rm_rf glob2
+          logging show_log "Deleted %s" glob2
+      ) (g#glob_expand glob)
   ) [
     "/var/spool/mail/*";
     "/var/mail/*";
diff --git a/sysprep/sysprep_operation_net_hwaddr.ml b/sysprep/sysprep_operation_net_hwaddr.ml
index 8aa102a..0a59197 100644
--- a/sysprep/sysprep_operation_net_hwaddr.ml
+++ b/sysprep/sysprep_operation_net_hwaddr.ml
@@ -21,7 +21,7 @@ open Sysprep_operation
 
 module G = Guestfs
 
-let net_hwaddr_perform g root =
+let net_hwaddr_perform g root show_log =
   let typ = g#inspect_get_type root in
   let distro = g#inspect_get_distro root in
   match typ, distro with
@@ -36,6 +36,7 @@ let net_hwaddr_perform g root =
         ) lines in
         let file = String.concat "\n" lines ^ "\n" in
         g#write filename file
+        logging show_log "Replaced HWADDR in %s\n" filename
     ) filenames;
 
     if filenames <> [||] then [ `Created_files ] else []
diff --git a/sysprep/sysprep_operation_random_seed.ml b/sysprep/sysprep_operation_random_seed.ml
index 989ffe8..6bafa09 100644
--- a/sysprep/sysprep_operation_random_seed.ml
+++ b/sysprep/sysprep_operation_random_seed.ml
@@ -20,7 +20,7 @@ open Sysprep_operation
 
 module G = Guestfs
 
-let random_seed_perform g root =
+let random_seed_perform g root show_log =
   let typ = g#inspect_get_type root in
   if typ = "linux" then (
     let files = [
@@ -37,6 +37,7 @@ let random_seed_perform g root =
           close_in chan;
 
           g#write file buf
+          logging show_log "Modified %s\n" file
         )
     ) files;
     [ `Created_files ]
diff --git a/sysprep/sysprep_operation_rhn_systemid.ml b/sysprep/sysprep_operation_rhn_systemid.ml
index 35849e4..6b55382 100644
--- a/sysprep/sysprep_operation_rhn_systemid.ml
+++ b/sysprep/sysprep_operation_rhn_systemid.ml
@@ -20,13 +20,17 @@ open Sysprep_operation
 
 module G = Guestfs
 
-let rhn_systemid_perform g root =
+let rhn_systemid_perform g root show_log =
   let typ = g#inspect_get_type root in
   let distro = g#inspect_get_distro root in
 
   match typ, distro with
   | "linux", "rhel" ->
-    (try g#rm "/etc/sysconfig/rhn/systemid" with G.Error _ -> ());
+    (try
+       let file = "/etc/sysconfig/rhn/systemid" in
+       g#rm file
+       logging show_log "Deleted %s\n" file
+     with G.Error _ -> ());
     []
   | _ -> []
 
diff --git a/sysprep/sysprep_operation_script.ml b/sysprep/sysprep_operation_script.ml
index 977d41f..65b23c6 100644
--- a/sysprep/sysprep_operation_script.ml
+++ b/sysprep/sysprep_operation_script.ml
@@ -35,7 +35,7 @@ let set_scriptdir dir =
 let scripts = ref []
 let add_script script = scripts := script :: !scripts
 
-let rec script_perform (g : Guestfs.guestfs) root =
+let rec script_perform (g : Guestfs.guestfs) root show_log =
   let scripts = List.rev !scripts in
   if scripts <> [] then (
     (* Create a temporary directory? *)
diff --git a/sysprep/sysprep_operation_smolt_uuid.ml b/sysprep/sysprep_operation_smolt_uuid.ml
index a85aa9b..78bca48 100644
--- a/sysprep/sysprep_operation_smolt_uuid.ml
+++ b/sysprep/sysprep_operation_smolt_uuid.ml
@@ -20,14 +20,18 @@ open Sysprep_operation
 
 module G = Guestfs
 
-let smolt_uuid_perform g root =
+let smolt_uuid_perform g root show_log =
   let typ = g#inspect_get_type root in
   if typ = "linux" then (
     let files = [ "/etc/sysconfig/hw-uuid";
                   "/etc/smolt/uuid";
                   "/etc/smolt/hw-uuid" ] in
     List.iter (
-      fun file -> try g#rm file with G.Error _ -> ()
+      fun file ->
+        try
+          g#rm file
+          logging show_log "Deleted %s\n" file
+        with G.Error _ -> ()
     ) files;
 
     []
diff --git a/sysprep/sysprep_operation_ssh_hostkeys.ml b/sysprep/sysprep_operation_ssh_hostkeys.ml
index 8da405b..cd62342 100644
--- a/sysprep/sysprep_operation_ssh_hostkeys.ml
+++ b/sysprep/sysprep_operation_ssh_hostkeys.ml
@@ -20,11 +20,15 @@ open Sysprep_operation
 
 module G = Guestfs
 
-let ssh_hostkeys_perform g root =
+let ssh_hostkeys_perform g root show_log =
   let typ = g#inspect_get_type root in
   if typ <> "windows" then (
     let files = g#glob_expand "/etc/ssh/*_host_*" in
-    Array.iter g#rm files;
+    Array.iter (
+      fun file ->
+      g#rm file
+      logging show_log "Deleted %s\n" file
+    ) files;
     []
   )
   else []
diff --git a/sysprep/sysprep_operation_udev_persistent_net.ml b/sysprep/sysprep_operation_udev_persistent_net.ml
index e54e140..c4a6696 100644
--- a/sysprep/sysprep_operation_udev_persistent_net.ml
+++ b/sysprep/sysprep_operation_udev_persistent_net.ml
@@ -20,10 +20,13 @@ open Sysprep_operation
 
 module G = Guestfs
 
-let udev_persistent_net_perform g root =
+let udev_persistent_net_perform g root show_log =
   let typ = g#inspect_get_type root in
   if typ = "linux" then (
-    (try g#rm "/etc/udev/rules.d/70-persistent-net.rules"
+    (try
+       let file = "/etc/udev/rules.d/70-persistent-net.rules" in
+       g#rm file
+       logging show_log "Deleted %s\n" file
      with G.Error _ -> ());
     []
   )
diff --git a/sysprep/sysprep_operation_utmp.ml b/sysprep/sysprep_operation_utmp.ml
index 69867e1..2f9394d 100644
--- a/sysprep/sysprep_operation_utmp.ml
+++ b/sysprep/sysprep_operation_utmp.ml
@@ -20,10 +20,13 @@ open Sysprep_operation
 
 module G = Guestfs
 
-let utmp_perform g root =
+let utmp_perform g root show_log =
   let typ = g#inspect_get_type root in
   if typ <> "windows" then (
-    try g#rm "/var/run/utmp"
+    try
+      let file = "/var/run/utmp" in
+      g#rm file
+      logging show_log "Deleted %s\n" file
     with G.Error _ -> ()
   );
   []
diff --git a/sysprep/sysprep_operation_yum_uuid.ml b/sysprep/sysprep_operation_yum_uuid.ml
index 396cac5..f7940de 100644
--- a/sysprep/sysprep_operation_yum_uuid.ml
+++ b/sysprep/sysprep_operation_yum_uuid.ml
@@ -20,10 +20,14 @@ open Sysprep_operation
 
 module G = Guestfs
 
-let yum_uuid_perform g root =
+let yum_uuid_perform g root show_log =
   let packager = g#inspect_get_package_management root in
   if packager = "yum" then (
-    (try g#rm "/var/lib/yum/uuid" with G.Error _ -> ());
+    (try
+       let file = "/var/lib/yum/uuid" in
+       g#rm file
+       logging show_log "Deleted %s\n" file
+     with G.Error _ -> ());
     []
   )
   else []
diff --git a/sysprep/utils.ml b/sysprep/utils.ml
index dfac57d..55c695a 100644
--- a/sysprep/utils.ml
+++ b/sysprep/utils.ml
@@ -69,3 +69,7 @@ let string_random8 =
           String.make 1 c
       ) [1;2;3;4;5;6;7;8]
     )
+
+let logging show_log list =
+  if show_log then
+    eprintf list
-- 
1.7.10.rc3




More information about the Libguestfs mailing list