[Libguestfs] [libguestfs-common PATCH] mlcustomize/firstboot: take an optional priority in "add_firstboot_script"

Laszlo Ersek lersek at redhat.com
Tue Mar 8 14:29:22 UTC 2022


Enable callers of "add_firstboot_script" to pass an (optional) priority
parameter. With this, groups of scripts can be formed with well-defined
(lexicographical) ordering between the groups. Within a group, the usual
order of registration persists.

Suggested-by: Richard W.M. Jones <rjones at redhat.com>
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1788823
Signed-off-by: Laszlo Ersek <lersek at redhat.com>
---
 mlcustomize/firstboot.mli | 16 ++++++++++++----
 mlcustomize/firstboot.ml  |  9 +++++++--
 2 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/mlcustomize/firstboot.mli b/mlcustomize/firstboot.mli
index fb30886184cc..e6a0d1f70102 100644
--- a/mlcustomize/firstboot.mli
+++ b/mlcustomize/firstboot.mli
@@ -16,16 +16,24 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  *)
 
-val add_firstboot_script : Guestfs.guestfs -> string -> string -> string -> unit
-  (** [add_firstboot_script g root name content] adds a firstboot
-      script called [name] containing [content].
+val default_prio : int
+
+val add_firstboot_script : Guestfs.guestfs -> string -> ?prio:int -> string ->
+                           string -> unit
+  (** [add_firstboot_script g root prio name content] adds a firstboot
+      script called [name] containing [content] with priority [prio].
 
       [content] is the contents of the script, {b not} a filename.
 
       The actual name of the script on the guest filesystem is made of [name]
       with all characters but alphanumeric replaced with dashes.
 
-      The scripts are run in the order they are registered.
+      Within a given priority, the scripts are run in the order they are
+      registered. A group of scripts with a numerically lower priority is run
+      before a group with a numerically greater priority. If [prio] is omitted,
+      it is taken as [default_prio], that is, 5000. If [prio] is smaller than 0
+      or greater than 9999, an Assert_failure is raised (the [prio] parameter
+      is not expected to depend on external data).
 
       For Linux guests using SELinux you should make sure the
       filesystem is relabelled after calling this. *)
diff --git a/mlcustomize/firstboot.ml b/mlcustomize/firstboot.ml
index c3ebfd910d0b..18dc04efa2e6 100644
--- a/mlcustomize/firstboot.ml
+++ b/mlcustomize/firstboot.ml
@@ -345,12 +345,17 @@ end
 
 let script_count = ref 0
 
-let add_firstboot_script (g : Guestfs.guestfs) root name content =
+let default_prio = 5000
+
+let add_firstboot_script (g : Guestfs.guestfs) root ?(prio = default_prio) name
+                         content =
+  assert (prio >= 0 && prio <= 9999);
   let typ = g#inspect_get_type root in
   let distro = g#inspect_get_distro root in
   let major = g#inspect_get_major_version root in
   incr script_count;
-  let filename = sprintf "%04d-%s" !script_count (sanitize_name name) in
+  let filename = sprintf "%04d-%04d-%s" prio !script_count
+                   (sanitize_name name) in
   match typ, distro with
   | "linux", _ ->
     Linux.install_service g root distro major;

base-commit: 60efc5407552c12688a5e99b7a54d23d5990aa8f
-- 
2.19.1.3.g30247aa5d201



More information about the Libguestfs mailing list