Blame SOURCES/0059-mltools-add-run_in_guest_command-helper.patch

d60042
From 1519dfe45d35dbc1f273d468fca3ea77d6cfdfad Mon Sep 17 00:00:00 2001
d60042
From: Pino Toscano <ptoscano@redhat.com>
d60042
Date: Mon, 4 May 2020 15:14:46 +0200
d60042
Subject: [PATCH] mltools: add run_in_guest_command helper
d60042
d60042
Add an helper function to run a command in the guest, checking for the
d60042
host/guest compatibility.  This is mostly extracted from the internal
d60042
do_run helper currently in the Customize_run module of virt-customize.
d60042
d60042
(cherry picked from commit e73eca3b73f7d0a54615c5dc55eadd09dc170035
d60042
in libguestfs-common)
d60042
---
d60042
 common/mltools/tools_utils.ml  | 50 ++++++++++++++++++++++++++++++++++
d60042
 common/mltools/tools_utils.mli | 10 +++++++
d60042
 2 files changed, 60 insertions(+)
d60042
d60042
diff --git a/common/mltools/tools_utils.ml b/common/mltools/tools_utils.ml
d60042
index 127180225..d54ec581e 100644
d60042
--- a/common/mltools/tools_utils.ml
d60042
+++ b/common/mltools/tools_utils.ml
d60042
@@ -679,3 +679,53 @@ let with_timeout op timeout ?(sleep = 2) fn =
d60042
        loop ()
d60042
   in
d60042
   loop ()
d60042
+
d60042
+let run_in_guest_command g root ?logfile ?incompatible_fn cmd =
d60042
+  (* Is the host_cpu compatible with the guest arch?  ie. Can we
d60042
+   * run commands in this guest?
d60042
+   *)
d60042
+  let guest_arch = g#inspect_get_arch root in
d60042
+  let guest_arch_compatible = guest_arch_compatible guest_arch in
d60042
+  if not guest_arch_compatible then (
d60042
+    match incompatible_fn with
d60042
+    | None -> ()
d60042
+    | Some fn -> fn ()
d60042
+  )
d60042
+  else (
d60042
+    (* Add a prologue to the scripts:
d60042
+     * - Pass environment variables through from the host.
d60042
+     * - Optionally send stdout and stderr to a log file so we capture
d60042
+     *   all output in error messages.
d60042
+     * - Use setarch when running x86_64 host + i686 guest.
d60042
+     *)
d60042
+    let env_vars =
d60042
+      List.filter_map (
d60042
+        fun name ->
d60042
+          try Some (sprintf "export %s=%s" name (quote (Sys.getenv name)))
d60042
+          with Not_found -> None
d60042
+      ) [ "http_proxy"; "https_proxy"; "ftp_proxy"; "no_proxy" ] in
d60042
+    let env_vars = String.concat "\n" env_vars ^ "\n" in
d60042
+
d60042
+    let cmd =
d60042
+      match Guestfs_config.host_cpu, guest_arch with
d60042
+      | "x86_64", ("i386"|"i486"|"i586"|"i686") ->
d60042
+        sprintf "setarch i686 <<\"__EOCMD\"
d60042
+%s
d60042
+__EOCMD
d60042
+" cmd
d60042
+      | _ -> cmd in
d60042
+
d60042
+    let logfile_redirect =
d60042
+      match logfile with
d60042
+      | None -> ""
d60042
+      | Some logfile -> sprintf "exec >>%s 2>&1" (quote logfile) in
d60042
+
d60042
+    let cmd = sprintf "\
d60042
+%s
d60042
+%s
d60042
+%s
d60042
+" (logfile_redirect) env_vars cmd in
d60042
+
d60042
+    debug "running command:\n%s" cmd;
d60042
+    ignore (g#sh cmd)
d60042
+  )
d60042
diff --git a/common/mltools/tools_utils.mli b/common/mltools/tools_utils.mli
d60042
index ab70f583e..102abff4d 100644
d60042
--- a/common/mltools/tools_utils.mli
d60042
+++ b/common/mltools/tools_utils.mli
d60042
@@ -212,3 +212,13 @@ val with_timeout : string -> int -> ?sleep:int -> (unit -> 'a option) -> 'a
d60042
     calls {!error} and the program exits.  The error message will
d60042
     contain the diagnostic string [op] to identify the operation
d60042
     which timed out. *)
d60042
+
d60042
+val run_in_guest_command : Guestfs.guestfs -> string -> ?logfile:string -> ?incompatible_fn:(unit -> unit) -> string -> unit
d60042
+(** [run_in_guest_command g root ?incompatible_archs_fn cmd]
d60042
+    runs a command in the guest, which is already mounted for the
d60042
+    specified [root].  The command is run directly in case the
d60042
+    architecture of the host and the guest are compatible, optionally
d60042
+    calling [?incompatible_fn] in case they are not.
d60042
+
d60042
+    [?logfile] is an optional file in the guest to where redirect
d60042
+    stdout and stderr of the command. *)
d60042
-- 
b155d0
2.26.2
d60042