mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone
Blob Blame History Raw
From d875df20c30868db210396b64f6dcec2cb6d57b4 Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones@redhat.com>
Date: Tue, 4 Dec 2018 16:09:42 +0000
Subject: [PATCH] v2v: windows: Add a helper function for installing Powershell
 firstboot scripts.

(cherry picked from commit e1e9b3845e76a4bb406d16b96283ac38677cd91f)
---
 v2v/Makefile.am |  1 +
 v2v/windows.ml  | 23 +++++++++++++++++++++++
 v2v/windows.mli |  6 ++++++
 3 files changed, 30 insertions(+)

diff --git a/v2v/Makefile.am b/v2v/Makefile.am
index 30f040d3e..6568c9a6b 100644
--- a/v2v/Makefile.am
+++ b/v2v/Makefile.am
@@ -667,6 +667,7 @@ check_PROGRAMS += v2v_unit_tests var_expander_tests
 endif
 
 v2v_unit_tests_BOBJECTS = \
+	$(top_builddir)/customize/firstboot.cmo \
 	types.cmo \
 	uefi.cmo \
 	utils.cmo \
diff --git a/v2v/windows.ml b/v2v/windows.ml
index 23d589b00..d83f77b7f 100644
--- a/v2v/windows.ml
+++ b/v2v/windows.ml
@@ -45,3 +45,26 @@ and check_app { Guestfs.app2_name = name;
   publisher =~ rex_avg_tech
 
 and (=~) str rex = PCRE.matches rex str
+
+(* Unfortunately Powershell scripts cannot be directly executed
+ * (unless some system config changes are made which for other
+ * reasons we don't want to do) and so we have to run this via
+ * a regular batch file.
+ *)
+let install_firstboot_powershell g { Types.i_windows_systemroot; i_root }
+                                 filename code =
+  let tempdir = sprintf "%s/Temp" i_windows_systemroot in
+  g#mkdir_p tempdir;
+  let code = String.concat "\r\n" code ^ "\r\n" in
+  g#write (sprintf "%s/%s" tempdir filename) code;
+
+  (* Powershell interpreter.  Should we check this exists? XXX *)
+  let ps_exe =
+    i_windows_systemroot ^
+    "\\System32\\WindowsPowerShell\\v1.0\\powershell.exe" in
+
+  (* Windows path to the Powershell script. *)
+  let ps_path = i_windows_systemroot ^ "\\Temp\\" ^ filename in
+
+  let fb = sprintf "%s -ExecutionPolicy ByPass -file %s" ps_exe ps_path in
+  Firstboot.add_firstboot_script g i_root filename fb
diff --git a/v2v/windows.mli b/v2v/windows.mli
index 016ef2a78..6db7874b0 100644
--- a/v2v/windows.mli
+++ b/v2v/windows.mli
@@ -21,3 +21,9 @@
 val detect_antivirus : Types.inspect -> bool
 (** Return [true] if anti-virus (AV) software was detected in
     this Windows guest. *)
+
+val install_firstboot_powershell : Guestfs.guestfs -> Types.inspect ->
+                                   string -> string list -> unit
+(** [install_powershell_firstboot g inspect filename code] installs a
+    Powershell script (the lines of code) as a firstboot script in
+    the Windows VM. *)
-- 
2.18.4