From b816f29adaab29c7c62ddb90d05715aa8f9e175b Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Tue, 15 Oct 2013 17:31:52 +0100 Subject: [PATCH] firstboot: Ensure firstboot scripts run in command line order. Previously they ran in effectively random order. --- builder/builder.ml | 17 ++++++++--------- mllib/firstboot.ml | 4 ++-- mllib/firstboot.mli | 6 +++--- sysprep/sysprep_operation_firstboot.ml | 23 +++++++---------------- 4 files changed, 20 insertions(+), 30 deletions(-) diff --git a/builder/builder.ml b/builder/builder.ml index 1656c722b..afa9c41ae 100644 --- a/builder/builder.ml +++ b/builder/builder.ml @@ -556,24 +556,23 @@ exec >>%s 2>&1 (* Firstboot scripts/commands/install. *) let () = - let id = ref 0 in + let i = ref 0 in List.iter ( fun op -> - incr id; - let id = sprintf "%03d" !id in + incr i; match op with | `Script script -> - msg (f_"Installing firstboot script: [%s] %s") id script; + msg (f_"Installing firstboot script: [%d] %s") !i script; let cmd = read_whole_file script in - Firstboot.add_firstboot_script g root id cmd + Firstboot.add_firstboot_script g root !i cmd | `Command cmd -> - msg (f_"Installing firstboot command: [%s] %s") id cmd; - Firstboot.add_firstboot_script g root id cmd + msg (f_"Installing firstboot command: [%d] %s") !i cmd; + Firstboot.add_firstboot_script g root !i cmd | `Packages pkgs -> - msg (f_"Installing firstboot packages: [%s] %s") id + msg (f_"Installing firstboot packages: [%d] %s") !i (String.concat " " pkgs); let cmd = guest_install_command pkgs in - Firstboot.add_firstboot_script g root id cmd + Firstboot.add_firstboot_script g root !i cmd ) firstboot in (* Run scripts. *) diff --git a/mllib/firstboot.ml b/mllib/firstboot.ml index 34eb096a5..0207cc113 100644 --- a/mllib/firstboot.ml +++ b/mllib/firstboot.ml @@ -153,7 +153,7 @@ and install_sysvinit_debian g = g#ln_sf "/etc/init.d/virt-sysprep-firstboot" "/etc/rc5.d/S99virt-sysprep-firstboot" -let add_firstboot_script (g : Guestfs.guestfs) root id content = +let add_firstboot_script (g : Guestfs.guestfs) root i content = let typ = g#inspect_get_type root in let distro = g#inspect_get_distro root in match typ, distro with @@ -161,7 +161,7 @@ let add_firstboot_script (g : Guestfs.guestfs) root id content = install_service g distro; let t = Int64.of_float (Unix.time ()) in let r = string_random8 () in - let filename = sprintf "%s/scripts/%Ld-%s-%s" firstboot_dir t r id in + let filename = sprintf "%s/scripts/%04d-%Ld-%s" firstboot_dir i t r in g#write filename content; g#chmod 0o755 filename diff --git a/mllib/firstboot.mli b/mllib/firstboot.mli index 910dd7510..4fb881240 100644 --- a/mllib/firstboot.mli +++ b/mllib/firstboot.mli @@ -16,12 +16,12 @@ * 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 id content] adds a firstboot +val add_firstboot_script : Guestfs.guestfs -> string -> int -> string -> unit + (** [add_firstboot_script g root idx content] adds a firstboot script called [shortname] containing [content]. NB. [content] is the contents of the script, {b not} a filename. - [id] should be a short name containing only 7 bit ASCII [-a-z0-9]. + The scripts run in index ([idx]) order. You should make sure the filesystem is relabelled after calling this. *) diff --git a/sysprep/sysprep_operation_firstboot.ml b/sysprep/sysprep_operation_firstboot.ml index 810a557ad..3473c47a1 100644 --- a/sysprep/sysprep_operation_firstboot.ml +++ b/sysprep/sysprep_operation_firstboot.ml @@ -26,27 +26,18 @@ module G = Guestfs let files = ref [] -let make_id_from_filename filename = - let ret = String.copy filename in - for i = 0 to String.length ret - 1 do - let c = String.unsafe_get ret i in - if not ((c >= 'a' && c <= 'z') || - (c >= 'A' && c <= 'Z') || - (c >= '0' && c <= '9')) then - String.unsafe_set ret i '-' - done; - ret - let firstboot_perform g root = (* Read the files and add them using the {!Firstboot} module. *) + let files = List.rev !files in + let i = ref 0 in List.iter ( fun filename -> + incr i; + let i = !i in let content = read_whole_file filename in - let basename = Filename.basename filename in - let id = make_id_from_filename basename in - Firstboot.add_firstboot_script g root id content - ) !files; - if !files <> [] then [ `Created_files ] else [] + Firstboot.add_firstboot_script g root i content + ) files; + if files <> [] then [ `Created_files ] else [] let op = { defaults with