[Libguestfs] [v2v PATCH 04/12] convert_windows: rewrite "configure_qemu_ga" script purely in PowerShell

Laszlo Ersek lersek at redhat.com
Tue Mar 8 14:30:51 UTC 2022


The "configure_qemu_ga" function embeds a 'powershell.exe -command "..."'
invocation in a CMD batch file. This makes the actual PowerShell script
effectively unreadable, due to both line length and quoting depth.
Everything else that the CMD batch file does is easily reimplementable in
PowerShell.

So reimplement the script in PowerShell purely, which allows us to unnest
one level of quoting, and to break the script into short statements that
align well with the OCaml source code. Register the script with our
"Windows.install_firstboot_powershell" function.

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1788823
Signed-off-by: Laszlo Ersek <lersek at redhat.com>
---
 convert/convert_windows.ml | 35 +++++++++++++++-----
 1 file changed, 26 insertions(+), 9 deletions(-)

diff --git a/convert/convert_windows.ml b/convert/convert_windows.ml
index 5fece75c9691..59ad3520c994 100644
--- a/convert/convert_windows.ml
+++ b/convert/convert_windows.ml
@@ -450,15 +450,32 @@ popd
         * https://stackoverflow.com/a/18730884
         * https://bugzilla.redhat.com/show_bug.cgi?id=1895323
         *)
-       let fb_script = sprintf "\
-echo Removing any previously scheduled qemu-ga installation
-schtasks.exe /Delete /TN Firstboot-qemu-ga /F
-echo Scheduling delayed installation of qemu-ga from %s
-powershell.exe -command \"$d = (get-date).AddSeconds(120); $FormatHack = (($([System.Globalization.DateTimeFormatInfo]::CurrentInfo.ShortDatePattern) -replace 'y+', 'yyyy') -replace 'M+', 'MM') -replace 'd+', 'dd'; schtasks.exe /Create /SC ONCE /ST $d.ToString('HH:mm') /SD $d.ToString($FormatHack) /RU SYSTEM /TN Firstboot-qemu-ga /TR \\\"C:\\%s /forcerestart /qn /l+*vx C:\\%s.log\\\"\"
-      "
-      msi_path msi_path msi_path in
-      Firstboot.add_firstboot_script g inspect.i_root
-        ("install " ^ msi_path) fb_script;
+       let psh_script = ref [] in
+       let add = List.push_back psh_script in
+
+       add "# Uncomment this line for lots of debug output.";
+       add "# Set-PSDebug -Trace 2";
+       add "";
+       add "Write-Host Removing any previously scheduled qemu-ga installation";
+       add "schtasks.exe /Delete /TN Firstboot-qemu-ga /F";
+       add "";
+       add (sprintf
+              "Write-Host Scheduling delayed installation of qemu-ga from %s"
+              msi_path);
+       add "$d = (get-date).AddSeconds(120)";
+       add "$dtfinfo = [System.Globalization.DateTimeFormatInfo]::CurrentInfo";
+       add "$sdp = $dtfinfo.ShortDatePattern";
+       add "$sdp = $sdp -replace 'y+', 'yyyy'";
+       add "$sdp = $sdp -replace 'M+', 'MM'";
+       add "$sdp = $sdp -replace 'd+', 'dd'";
+       add "schtasks.exe /Create /SC ONCE `";
+       add "  /ST $d.ToString('HH:mm') /SD $d.ToString($sdp) `";
+       add "  /RU SYSTEM /TN Firstboot-qemu-ga `";
+       add (sprintf "  /TR \"C:\\%s /forcerestart /qn /l+*vx C:\\%s.log\""
+              msi_path msi_path);
+
+      Windows.install_firstboot_powershell g inspect
+        (sprintf "install-%s.ps1" msi_path) !psh_script;
     ) files
 
 
-- 
2.19.1.3.g30247aa5d201




More information about the Libguestfs mailing list