Blame SOURCES/0015-convert_linux-install-the-QEMU-guest-agent-with-a-fi.patch

696189
From e32a5ee7deb9a381ab285aba92c4de23e3c6ee2e Mon Sep 17 00:00:00 2001
696189
From: Laszlo Ersek <lersek@redhat.com>
696189
Date: Mon, 13 Jun 2022 19:01:35 +0200
696189
Subject: [PATCH] convert_linux: install the QEMU guest agent with a firstboot
696189
 script
696189
696189
Register a firstboot script, for installing the guest agent with the
696189
guest's own package manager -- that is, "Guest_packages.install_command".
696189
696189
For installing the package, network connectivity is required. Check it
696189
first with "nmcli" (also checking whether NetworkManager is running), then
696189
with "systemd-networkd-wait-online" (dependent on systemd-networkd). Note
696189
that NetworkManager and systemd-networkd are never supposed to be enabled
696189
at the same time.
696189
696189
The source domain's SELinux policy may not allow our firstboot service to
696189
execute the package's installation scripts (if any). For that reason,
696189
temporarily disable SELinux around package installation.
696189
696189
After installation, register another script for launching the agent.
696189
696189
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2028764
696189
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
696189
Message-Id: <20220613170135.12557-5-lersek@redhat.com>
696189
Reviewed-by: Richard W.M. Jones <rjones@redhat.com>
696189
(cherry picked from commit e64356896377af1ac75a03d6a4c6a4208910bbf4)
696189
---
696189
 convert/convert_linux.ml | 78 ++++++++++++++++++++++++++++++++++++++--
696189
 1 file changed, 76 insertions(+), 2 deletions(-)
696189
696189
diff --git a/convert/convert_linux.ml b/convert/convert_linux.ml
696189
index 2ddbc07a..59d143bd 100644
696189
--- a/convert/convert_linux.ml
696189
+++ b/convert/convert_linux.ml
696189
@@ -562,8 +562,82 @@ let convert (g : G.guestfs) source inspect keep_serial_console _ =
696189
               name = qga_pkg
696189
           ) inspect.i_apps in
696189
         if not has_qemu_guest_agent then
696189
-          (* FIXME -- install qemu-guest-agent here *)
696189
-          ()
696189
+          try
696189
+            let inst_cmd = Guest_packages.install_command [qga_pkg]
696189
+                             inspect.i_package_management in
696189
+
696189
+            (* Use only the portable filename character set in this. *)
696189
+            let selinux_enforcing = "/root/virt-v2v-fb-selinux-enforcing"
696189
+            and timeout = 30 in
696189
+            let fbs =
696189
+              Firstboot.add_firstboot_script g inspect.i_root
696189
+            in
696189
+            info (f_"The QEMU Guest Agent will be installed for this guest at \
696189
+                     first boot.");
696189
+
696189
+            (* Wait for the network to come online in the guest (best effort).
696189
+             *)
696189
+            fbs "wait online"
696189
+              (sprintf "#!/bin/sh\n\
696189
+                        if conn=$(nmcli networking connectivity); then\n\
696189
+                        \ \ tries=0\n\
696189
+                        \ \ while\n\
696189
+                        \ \ \ \ test $tries -lt %d &&\n\
696189
+                        \ \ \ \ test full != \"$conn\"\n\
696189
+                        \ \ do\n\
696189
+                        \ \ \ \ sleep 1\n\
696189
+                        \ \ \ \ tries=$((tries + 1))\n\
696189
+                        \ \ \ \ conn=$(nmcli networking connectivity)\n\
696189
+                        \ \ done\n\
696189
+                        elif systemctl -q is-active systemd-networkd; then\n\
696189
+                        \ \ /usr/lib/systemd/systemd-networkd-wait-online \\\n\
696189
+                        \ \ \ \ -q --timeout=%d\n\
696189
+                        fi\n" timeout timeout);
696189
+
696189
+            (* Disable SELinux temporarily around package installation. Refer to
696189
+             * <https://bugzilla.redhat.com/show_bug.cgi?id=2028764#c7> and
696189
+             * <https://bugzilla.redhat.com/show_bug.cgi?id=2028764#c8>.
696189
+             *)
696189
+            fbs "setenforce 0"
696189
+              (sprintf "#!/bin/sh\n\
696189
+                        rm -f %s\n\
696189
+                        if command -v getenforce >/dev/null &&\n\
696189
+                        \ \ test Enforcing = \"$(getenforce)\"\n\
696189
+                        then\n\
696189
+                        \ \ touch %s\n\
696189
+                        \ \ setenforce 0\n\
696189
+                        fi\n" selinux_enforcing selinux_enforcing);
696189
+            fbs "install qga" inst_cmd;
696189
+            fbs "setenforce restore"
696189
+              (sprintf "#!/bin/sh\n\
696189
+                        if test -f %s; then\n\
696189
+                        \ \ setenforce 1\n\
696189
+                        \ \ rm -f %s\n\
696189
+                        fi\n" selinux_enforcing selinux_enforcing);
696189
+
696189
+            (* Start the agent now and at subsequent boots. The following
696189
+             * commands should work on both sysvinit distros / distro versions
696189
+             * (regardless of "/etc/rc.d/" vs. "/etc/init.d/" being the scheme
696189
+             * in use) and systemd distros (via redirection to systemctl).
696189
+             *
696189
+             * On distros where the chkconfig command is redirected to
696189
+             * systemctl, the chkconfig command is likely superfluous. That's
696189
+             * because on systemd distros, the QGA package comes with such
696189
+             * runtime dependencies / triggers that the presence of the
696189
+             * virtio-serial port named "org.qemu.guest_agent.0" automatically
696189
+             * starts the agent during (second and later) boots. However, even
696189
+             * on such distros, the chkconfig command should do no harm.
696189
+             *)
696189
+            fbs "start qga"
696189
+              (sprintf "#!/bin/sh\n\
696189
+                        service %s start\n\
696189
+                        chkconfig %s on\n" qga_pkg qga_pkg)
696189
+          with
696189
+          | Guest_packages.Unknown_package_manager msg
696189
+          | Guest_packages.Unimplemented_package_manager msg ->
696189
+            warning (f_"The QEMU Guest Agent will not be installed.  The \
696189
+                        install command for package ā€˜%sā€™ could not be created: \
696189
+                        %s.") qga_pkg msg
696189
 
696189
   and configure_kernel () =
696189
     (* Previously this function would try to install kernels, but we