mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone

Blame SOURCES/0070-sysprep-add-a-update_system_ca_store-side-effect.patch

da373f
From f4613f408e4584741d08aa39c950de304f5e18db Mon Sep 17 00:00:00 2001
3efd08
From: Pino Toscano <ptoscano@redhat.com>
3efd08
Date: Mon, 4 May 2020 12:05:18 +0200
3efd08
Subject: [PATCH] sysprep: add a update_system_ca_store side effect
3efd08
3efd08
Add a simple side effect to make operation flag that a regeneration of
3efd08
the system CA store is needed. In case it is flagged, regenerate the
3efd08
system CA store directly, or using a firstboot script in case of
3efd08
incompatible architectures.
3efd08
3efd08
This change is almost a no-op, since no operation requires the
3efd08
regeneration of the system CA store yet.
3efd08
3efd08
(cherry picked from commit bb7fc6d0a1ed670d16a92d2afd9ff2f876edc595)
3efd08
---
3efd08
 sysprep/main.ml               |  5 +++++
3efd08
 sysprep/sysprep_operation.ml  |  3 +++
3efd08
 sysprep/sysprep_operation.mli |  2 ++
3efd08
 sysprep/utils.ml              | 32 ++++++++++++++++++++++++++++++++
3efd08
 sysprep/utils.mli             |  4 ++++
3efd08
 5 files changed, 46 insertions(+)
3efd08
3efd08
diff --git a/sysprep/main.ml b/sysprep/main.ml
3efd08
index 04fd7b23c..4b03d5b48 100644
3efd08
--- a/sysprep/main.ml
3efd08
+++ b/sysprep/main.ml
3efd08
@@ -25,6 +25,7 @@ open Common_gettext.Gettext
3efd08
 open Getopt.OptionName
3efd08
 
3efd08
 open Sysprep_operation
3efd08
+open Utils
3efd08
 
3efd08
 module G = Guestfs
3efd08
 
3efd08
@@ -236,6 +237,10 @@ read the man page virt-sysprep(1).
3efd08
         Sysprep_operation.perform_operations_on_filesystems
3efd08
           ?operations g root side_effects;
3efd08
 
3efd08
+        (* Do we need to update the system CA store? *)
3efd08
+        if side_effects#get_update_system_ca_store then
3efd08
+          update_system_ca_store g root;
3efd08
+
3efd08
         (* Unmount everything in this guest. *)
3efd08
         g#umount_all ();
3efd08
 
3efd08
diff --git a/sysprep/sysprep_operation.ml b/sysprep/sysprep_operation.ml
3efd08
index 0013ff504..53f042402 100644
3efd08
--- a/sysprep/sysprep_operation.ml
3efd08
+++ b/sysprep/sysprep_operation.ml
3efd08
@@ -27,10 +27,13 @@ class filesystem_side_effects =
3efd08
 object
3efd08
   val mutable m_created_file = false
3efd08
   val mutable m_changed_file = false
3efd08
+  val mutable m_update_system_ca_store = false
3efd08
   method created_file () = m_created_file <- true
3efd08
   method get_created_file = m_created_file
3efd08
   method changed_file () = m_changed_file <- true
3efd08
   method get_changed_file = m_changed_file
3efd08
+  method update_system_ca_store () = m_update_system_ca_store <- true
3efd08
+  method get_update_system_ca_store = m_update_system_ca_store
3efd08
 end
3efd08
 
3efd08
 class device_side_effects = object end
3efd08
diff --git a/sysprep/sysprep_operation.mli b/sysprep/sysprep_operation.mli
3efd08
index 3d9f12550..2a02d5e79 100644
3efd08
--- a/sysprep/sysprep_operation.mli
3efd08
+++ b/sysprep/sysprep_operation.mli
3efd08
@@ -23,6 +23,8 @@ class filesystem_side_effects : object
3efd08
   method get_created_file : bool
3efd08
   method changed_file : unit -> unit
3efd08
   method get_changed_file : bool
3efd08
+  method update_system_ca_store : unit -> unit
3efd08
+  method get_update_system_ca_store : bool
3efd08
 end
3efd08
 (** The callback should indicate if it has side effects by calling
3efd08
     methods in this class. *)
3efd08
diff --git a/sysprep/utils.ml b/sysprep/utils.ml
3efd08
index 4c45d42de..29460b3c0 100644
3efd08
--- a/sysprep/utils.ml
3efd08
+++ b/sysprep/utils.ml
3efd08
@@ -20,6 +20,9 @@
3efd08
 
3efd08
 open Printf
3efd08
 
3efd08
+open Tools_utils
3efd08
+open Common_gettext.Gettext
3efd08
+
3efd08
 let rec pod_of_list ?(style = `Dot) xs =
3efd08
   match style with
3efd08
   | `Verbatim -> String.concat "\n" (List.map ((^) " ") xs)
3efd08
@@ -31,3 +34,32 @@ and _pod_of_list delim xs =
3efd08
   "=over 4\n\n" ^
3efd08
   String.concat "" (List.map (sprintf "=item %s\n\n%s\n\n" delim) xs) ^
3efd08
   "=back"
3efd08
+
3efd08
+let rec update_system_ca_store g root =
3efd08
+  let cmd = update_system_ca_store_command g root in
3efd08
+  match cmd with
3efd08
+  | None -> ()
3efd08
+  | Some cmd ->
3efd08
+    (* Try to run the command directly if possible, adding it as
3efd08
+     * firstboot script in case of incompatible architectures.
3efd08
+     *)
3efd08
+    let cmd = String.concat " " cmd in
3efd08
+    let incompatible_fn () =
3efd08
+      Firstboot.add_firstboot_script g root cmd cmd
3efd08
+    in
3efd08
+
3efd08
+    run_in_guest_command g root ~incompatible_fn cmd
3efd08
+
3efd08
+and update_system_ca_store_command g root =
3efd08
+  let typ = g#inspect_get_type root in
3efd08
+  let distro = g#inspect_get_distro root in
3efd08
+  match typ, distro with
3efd08
+  | "linux", ("fedora"|"rhel"|"centos"|"scientificlinux"|"oraclelinux"|"redhat-based") ->
3efd08
+    Some [ "update-ca-trust"; "extract" ]
3efd08
+
3efd08
+  | "linux", ("debian"|"ubuntu"|"kalilinux") ->
3efd08
+    Some [ "update-ca-certificates" ]
3efd08
+
3efd08
+  | _, _ ->
3efd08
+    warning (f_"updating the system CA store on this guest %s/%s is not supported") typ distro;
3efd08
+    None
3efd08
diff --git a/sysprep/utils.mli b/sysprep/utils.mli
3efd08
index a57a0d876..82779620e 100644
3efd08
--- a/sysprep/utils.mli
3efd08
+++ b/sysprep/utils.mli
3efd08
@@ -26,3 +26,7 @@ val pod_of_list : ?style:[`Verbatim|`Star|`Dash|`Dot] -> string list -> string
3efd08
     use a space-indented (verbatim) block.  [`Star], [`Dash] or [`Dot]
3efd08
     meaning use a real list with [*], [-] or [·].  The default
3efd08
     style is [·] ([`Dot]). *)
3efd08
+
3efd08
+val update_system_ca_store : Guestfs.guestfs -> string -> unit
3efd08
+(** Update the system CA store on the guest for the specified root
3efd08
+    (which is fully mounted). *)
3efd08
-- 
da373f
2.18.4
3efd08