[Libguestfs] [PATCH v2 1/2] firstboot: rename systemd and sysvinit

Pino Toscano ptoscano at redhat.com
Thu Aug 4 11:56:09 UTC 2016


Currently we install a systemd service named firstboot.service and a
SysV service named virt-sysprep-firstboot.  On systems where systemd is
the init system and runs with the SysV compatibility, the different
names make systemd handle them as different services, and thus trying to
run the firstboot script runner twice.

Rename both the systemd service and the SysV one to guestfs-firstboot:
the new name is less generic, and allows the systemd service to be
shadowed by the SysV service (and thus running just once).

Also cleanup the old services: the old SysV service can be removed
directly, since its former name had "virt-sysprep" in it, and so there
could not be much room for confusion and conflict.  Regarding the old
systemd service: to avoid leaving it behind, a simple cleanup strategy
is in place, checking the content of the old firstboot.service to really
ensure we are removing one of our versions of this file.
---
 customize/firstboot.ml | 74 ++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 54 insertions(+), 20 deletions(-)

diff --git a/customize/firstboot.ml b/customize/firstboot.ml
index f1e5807..52dfbbe 100644
--- a/customize/firstboot.ml
+++ b/customize/firstboot.ml
@@ -82,7 +82,7 @@ fi
 
   let firstboot_service = sprintf "\
 [Unit]
-Description=virt-sysprep firstboot service
+Description=libguestfs firstboot service
 After=network.target
 Before=prefdm.service
 
@@ -122,10 +122,27 @@ WantedBy=default.target
      *)
     let unitdir = "/usr/lib/systemd/system" in
     g#mkdir_p unitdir;
-    let unitfile = sprintf "%s/firstboot.service" unitdir in
+    let unitfile = sprintf "%s/guestfs-firstboot.service" unitdir in
     g#write unitfile firstboot_service;
     g#mkdir_p "/etc/systemd/system/default.target.wants";
-    g#ln_sf unitfile "/etc/systemd/system/default.target.wants"
+    g#ln_sf unitfile "/etc/systemd/system/default.target.wants";
+
+    (* Try to remove the old firstboot.service files. *)
+    let oldunitfile = sprintf "%s/firstboot.service" unitdir in
+    if g#is_file oldunitfile then (
+      g#rm_f "/etc/systemd/system/default.target.wants/firstboot.service";
+      (* Remove the old firstboot.service only if it is one of our
+       * versions. *)
+      match g#checksum "md5" oldunitfile with
+      | "6923781f7a1851b40b32b4960eb9a0fc"  (* < 1.23.24 *)
+      | "56fafd8c990fc9d24e5b8497f3582e8d"  (* < 1.23.32 *)
+      | "a83767e01cf398e2fd7c8f59d65d320a"  (* < 1.25.2 *)
+      | "39aeb10df29104797e3a9aca4db37a6e" ->
+        g#rm oldunitfile
+      | csum ->
+        warning (f_"firstboot: unknown version for old firstboot.service file %s (md5=%s), it will not be removed")
+          oldunitfile csum
+    )
 
   and install_sysvinit_service g = function
     | "fedora"|"rhel"|"centos"|"scientificlinux"|"redhat-based" ->
@@ -142,11 +159,16 @@ WantedBy=default.target
     g#mkdir_p "/etc/rc.d/rc3.d";
     g#mkdir_p "/etc/rc.d/rc5.d";
     g#ln_sf (sprintf "%s/firstboot.sh" firstboot_dir)
-      "/etc/rc.d/rc2.d/S99virt-sysprep-firstboot";
+      "/etc/rc.d/rc2.d/S99guestfs-firstboot";
     g#ln_sf (sprintf "%s/firstboot.sh" firstboot_dir)
-      "/etc/rc.d/rc3.d/S99virt-sysprep-firstboot";
+      "/etc/rc.d/rc3.d/S99guestfs-firstboot";
     g#ln_sf (sprintf "%s/firstboot.sh" firstboot_dir)
-      "/etc/rc.d/rc5.d/S99virt-sysprep-firstboot"
+      "/etc/rc.d/rc5.d/S99guestfs-firstboot";
+
+    (* Try to remove the files of the old service. *)
+    g#rm_f "/etc/rc.d/rc2.d/S99virt-sysprep-firstboot";
+    g#rm_f "/etc/rc.d/rc3.d/S99virt-sysprep-firstboot";
+    g#rm_f "/etc/rc.d/rc5.d/S99virt-sysprep-firstboot"
 
   (* Make firstboot.sh look like a runlevel script to avoid insserv warnings. *)
   and install_sysvinit_suse g =
@@ -154,13 +176,19 @@ WantedBy=default.target
     g#mkdir_p "/etc/init.d/rc3.d";
     g#mkdir_p "/etc/init.d/rc5.d";
     g#ln_sf (sprintf "%s/firstboot.sh" firstboot_dir)
-      "/etc/init.d/virt-sysprep-firstboot";
-    g#ln_sf "../virt-sysprep-firstboot"
-      "/etc/init.d/rc2.d/S99virt-sysprep-firstboot";
-    g#ln_sf "../virt-sysprep-firstboot"
-      "/etc/init.d/rc3.d/S99virt-sysprep-firstboot";
-    g#ln_sf "../virt-sysprep-firstboot"
-      "/etc/init.d/rc5.d/S99virt-sysprep-firstboot"
+      "/etc/init.d/guestfs-firstboot";
+    g#ln_sf "../guestfs-firstboot"
+      "/etc/init.d/rc2.d/S99guestfs-firstboot";
+    g#ln_sf "../guestfs-firstboot"
+      "/etc/init.d/rc3.d/S99guestfs-firstboot";
+    g#ln_sf "../guestfs-firstboot"
+      "/etc/init.d/rc5.d/S99guestfs-firstboot";
+
+    (* Try to remove the files of the old service. *)
+    g#rm_f "/etc/init.d/virt-sysprep-firstboot";
+    g#rm_f "/etc/init.d/rc2.d/S99virt-sysprep-firstboot";
+    g#rm_f "/etc/init.d/rc3.d/S99virt-sysprep-firstboot";
+    g#rm_f "/etc/init.d/rc5.d/S99virt-sysprep-firstboot"
 
   and install_sysvinit_debian g =
     g#mkdir_p "/etc/init.d";
@@ -168,13 +196,19 @@ WantedBy=default.target
     g#mkdir_p "/etc/rc3.d";
     g#mkdir_p "/etc/rc5.d";
     g#ln_sf (sprintf "%s/firstboot.sh" firstboot_dir)
-      "/etc/init.d/virt-sysprep-firstboot";
-    g#ln_sf "/etc/init.d/virt-sysprep-firstboot"
-      "/etc/rc2.d/S99virt-sysprep-firstboot";
-    g#ln_sf "/etc/init.d/virt-sysprep-firstboot"
-      "/etc/rc3.d/S99virt-sysprep-firstboot";
-    g#ln_sf "/etc/init.d/virt-sysprep-firstboot"
-      "/etc/rc5.d/S99virt-sysprep-firstboot"
+      "/etc/init.d/guestfs-firstboot";
+    g#ln_sf "/etc/init.d/guestfs-firstboot"
+      "/etc/rc2.d/S99guestfs-firstboot";
+    g#ln_sf "/etc/init.d/guestfs-firstboot"
+      "/etc/rc3.d/S99guestfs-firstboot";
+    g#ln_sf "/etc/init.d/guestfs-firstboot"
+      "/etc/rc5.d/S99guestfs-firstboot";
+
+    (* Try to remove the files of the old service. *)
+    g#rm_f "/etc/init.d/virt-sysprep-firstboot";
+    g#rm_f "/etc/rc2.d/S99virt-sysprep-firstboot";
+    g#rm_f "/etc/rc3.d/S99virt-sysprep-firstboot";
+    g#rm_f "/etc/rc5.d/S99virt-sysprep-firstboot"
 end
 
 module Windows = struct
-- 
2.7.4




More information about the Libguestfs mailing list