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

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