[Libguestfs] [PATCH 2/4] sysprep: add a update_system_ca_store side effect

Pino Toscano ptoscano at redhat.com
Mon May 4 13:22:12 UTC 2020


Add a simple side effect to make operation flag that a regeneration of
the system CA store is needed. In case it is flagged, regenerate the
system CA store directly, or using a firstboot script in case of
incompatible architectures.

This change is almost a no-op, since no operation requires the
regeneration of the system CA store yet.
---
 sysprep/main.ml               |  5 +++++
 sysprep/sysprep_operation.ml  |  3 +++
 sysprep/sysprep_operation.mli |  2 ++
 sysprep/utils.ml              | 32 ++++++++++++++++++++++++++++++++
 sysprep/utils.mli             |  4 ++++
 5 files changed, 46 insertions(+)

diff --git a/sysprep/main.ml b/sysprep/main.ml
index db2388cfa..087d1a17f 100644
--- a/sysprep/main.ml
+++ b/sysprep/main.ml
@@ -25,6 +25,7 @@ open Common_gettext.Gettext
 open Getopt.OptionName
 
 open Sysprep_operation
+open Utils
 
 module G = Guestfs
 
@@ -236,6 +237,10 @@ read the man page virt-sysprep(1).
         Sysprep_operation.perform_operations_on_filesystems
           ?operations g root side_effects;
 
+        (* Do we need to update the system CA store? *)
+        if side_effects#get_update_system_ca_store then
+          update_system_ca_store g root;
+
         (* Unmount everything in this guest. *)
         g#umount_all ();
 
diff --git a/sysprep/sysprep_operation.ml b/sysprep/sysprep_operation.ml
index 0013ff504..53f042402 100644
--- a/sysprep/sysprep_operation.ml
+++ b/sysprep/sysprep_operation.ml
@@ -27,10 +27,13 @@ class filesystem_side_effects =
 object
   val mutable m_created_file = false
   val mutable m_changed_file = false
+  val mutable m_update_system_ca_store = false
   method created_file () = m_created_file <- true
   method get_created_file = m_created_file
   method changed_file () = m_changed_file <- true
   method get_changed_file = m_changed_file
+  method update_system_ca_store () = m_update_system_ca_store <- true
+  method get_update_system_ca_store = m_update_system_ca_store
 end
 
 class device_side_effects = object end
diff --git a/sysprep/sysprep_operation.mli b/sysprep/sysprep_operation.mli
index 3d9f12550..2a02d5e79 100644
--- a/sysprep/sysprep_operation.mli
+++ b/sysprep/sysprep_operation.mli
@@ -23,6 +23,8 @@ class filesystem_side_effects : object
   method get_created_file : bool
   method changed_file : unit -> unit
   method get_changed_file : bool
+  method update_system_ca_store : unit -> unit
+  method get_update_system_ca_store : bool
 end
 (** The callback should indicate if it has side effects by calling
     methods in this class. *)
diff --git a/sysprep/utils.ml b/sysprep/utils.ml
index 4c45d42de..29460b3c0 100644
--- a/sysprep/utils.ml
+++ b/sysprep/utils.ml
@@ -20,6 +20,9 @@
 
 open Printf
 
+open Tools_utils
+open Common_gettext.Gettext
+
 let rec pod_of_list ?(style = `Dot) xs =
   match style with
   | `Verbatim -> String.concat "\n" (List.map ((^) " ") xs)
@@ -31,3 +34,32 @@ and _pod_of_list delim xs =
   "=over 4\n\n" ^
   String.concat "" (List.map (sprintf "=item %s\n\n%s\n\n" delim) xs) ^
   "=back"
+
+let rec update_system_ca_store g root =
+  let cmd = update_system_ca_store_command g root in
+  match cmd with
+  | None -> ()
+  | Some cmd ->
+    (* Try to run the command directly if possible, adding it as
+     * firstboot script in case of incompatible architectures.
+     *)
+    let cmd = String.concat " " cmd in
+    let incompatible_fn () =
+      Firstboot.add_firstboot_script g root cmd cmd
+    in
+
+    run_in_guest_command g root ~incompatible_fn cmd
+
+and update_system_ca_store_command g root =
+  let typ = g#inspect_get_type root in
+  let distro = g#inspect_get_distro root in
+  match typ, distro with
+  | "linux", ("fedora"|"rhel"|"centos"|"scientificlinux"|"oraclelinux"|"redhat-based") ->
+    Some [ "update-ca-trust"; "extract" ]
+
+  | "linux", ("debian"|"ubuntu"|"kalilinux") ->
+    Some [ "update-ca-certificates" ]
+
+  | _, _ ->
+    warning (f_"updating the system CA store on this guest %s/%s is not supported") typ distro;
+    None
diff --git a/sysprep/utils.mli b/sysprep/utils.mli
index a57a0d876..82779620e 100644
--- a/sysprep/utils.mli
+++ b/sysprep/utils.mli
@@ -26,3 +26,7 @@ val pod_of_list : ?style:[`Verbatim|`Star|`Dash|`Dot] -> string list -> string
     use a space-indented (verbatim) block.  [`Star], [`Dash] or [`Dot]
     meaning use a real list with [*], [-] or [·].  The default
     style is [·] ([`Dot]). *)
+
+val update_system_ca_store : Guestfs.guestfs -> string -> unit
+(** Update the system CA store on the guest for the specified root
+    (which is fully mounted). *)
-- 
2.25.4




More information about the Libguestfs mailing list