[PATCH] customize: firstboot: make sure to run Linux scripts only once
If a script does not finish, hangs, etc, it would be executed again at the next boot, since the injected firstboot.sh helper removes it only after it finished. Since firstboot.sh only runs executable files, then remove the executable attributes before running it, so it will not run again. Also, remove any file found in the scripts subdirectory, be it just run or run in a previous boot. This fixes RHBZ#1159651. --- customize/firstboot.ml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/customize/firstboot.ml b/customize/firstboot.ml index 67b9479..5f68a2e 100644 --- a/customize/firstboot.ml +++ b/customize/firstboot.ml @@ -57,10 +57,13 @@ then for f in $d/* ; do if test -x \"$f\" then + # remove +x from the script being executed, so it is not + # executed again at the next boot + chmod -x $f echo '=== Running' $f '===' 2>&1 | tee $logfile - $f 2>&1 | tee $logfile - rm -f $f + sh $f 2>&1 | tee $logfile fi + rm -f $f done fi " firstboot_dir -- 1.9.3
On Mon, Nov 03, 2014 at 08:49:08PM +0100, Pino Toscano wrote:
If a script does not finish, hangs, etc, it would be executed again at the next boot, since the injected firstboot.sh helper removes it only after it finished.
Since firstboot.sh only runs executable files, then remove the executable attributes before running it, so it will not run again. Also, remove any file found in the scripts subdirectory, be it just run or run in a previous boot.
This fixes RHBZ#1159651. --- customize/firstboot.ml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/customize/firstboot.ml b/customize/firstboot.ml index 67b9479..5f68a2e 100644 --- a/customize/firstboot.ml +++ b/customize/firstboot.ml @@ -57,10 +57,13 @@ then for f in $d/* ; do if test -x \"$f\" then + # remove +x from the script being executed, so it is not + # executed again at the next boot + chmod -x $f echo '=== Running' $f '===' 2>&1 | tee $logfile - $f 2>&1 | tee $logfile - rm -f $f + sh $f 2>&1 | tee $logfile
I like the idea .. unfortunately I believe this will make it run the wrong interpreter. eg. If the firstboot script started with '#!/usr/bin/perl' then this would run it with 'sh' instead. I'm not sure how to work around that. 'env' maybe? Is it better to document the problem instead? Arguably this bug is user error. Perhaps they'd be better off doing something like: ( sleep 10 ; reboot ) & at the end of their script. Rich.
fi + rm -f $f done fi " firstboot_dir -- 1.9.3
_______________________________________________ Libguestfs mailing list Libguestfs@redhat.com https://www.redhat.com/mailman/listinfo/libguestfs
-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://libguestfs.org
If a script does not finish, hangs, etc, it would be executed again at the next boot, since the injected firstboot.sh helper removes it only after it finished. Before executing a script, move it to another internal subdirectory (scripts-done) and execute it from there, so it is not run again by firstboot.sh. The downside is that now scripts are executed only once at all, so if a script fails it will not be attempted at the next boot. Also, remove all the files found in scripts-done, as they have been run (or at least attempted) in a previous boot. This fixes RHBZ#1159651. --- customize/firstboot.ml | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/customize/firstboot.ml b/customize/firstboot.ml index 67b9479..89821f3 100644 --- a/customize/firstboot.ml +++ b/customize/firstboot.ml @@ -47,6 +47,7 @@ module Linux = struct ### END INIT INFO d=%s/scripts +d_done=%s/scripts-done logfile=~root/virt-sysprep-firstboot.log echo \"$0\" \"$@\" 2>&1 | tee $logfile @@ -54,16 +55,20 @@ echo \"Scripts dir: $d\" 2>&1 | tee $logfile if test \"$1\" = \"start\" then + mkdir -p $d_done for f in $d/* ; do if test -x \"$f\" then + # move the script to the 'scripts-done' directory, so it is not + # executed again at the next boot + mv $f $d_done echo '=== Running' $f '===' 2>&1 | tee $logfile - $f 2>&1 | tee $logfile - rm -f $f + $d_done/$(basename $f) 2>&1 | tee $logfile fi done + rm -f $d_done/* fi -" firstboot_dir +" firstboot_dir firstboot_dir let firstboot_service = sprintf "\ [Unit] -- 1.9.3
On Mon, Nov 03, 2014 at 09:19:44PM +0100, Pino Toscano wrote:
If a script does not finish, hangs, etc, it would be executed again at the next boot, since the injected firstboot.sh helper removes it only after it finished.
Before executing a script, move it to another internal subdirectory (scripts-done) and execute it from there, so it is not run again by firstboot.sh. The downside is that now scripts are executed only once at all, so if a script fails it will not be attempted at the next boot.
Also, remove all the files found in scripts-done, as they have been run (or at least attempted) in a previous boot.
This fixes RHBZ#1159651. --- customize/firstboot.ml | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/customize/firstboot.ml b/customize/firstboot.ml index 67b9479..89821f3 100644 --- a/customize/firstboot.ml +++ b/customize/firstboot.ml @@ -47,6 +47,7 @@ module Linux = struct ### END INIT INFO
d=%s/scripts +d_done=%s/scripts-done logfile=~root/virt-sysprep-firstboot.log
echo \"$0\" \"$@\" 2>&1 | tee $logfile @@ -54,16 +55,20 @@ echo \"Scripts dir: $d\" 2>&1 | tee $logfile
if test \"$1\" = \"start\" then + mkdir -p $d_done for f in $d/* ; do if test -x \"$f\" then + # move the script to the 'scripts-done' directory, so it is not + # executed again at the next boot + mv $f $d_done echo '=== Running' $f '===' 2>&1 | tee $logfile - $f 2>&1 | tee $logfile - rm -f $f + $d_done/$(basename $f) 2>&1 | tee $logfile fi done + rm -f $d_done/* fi -" firstboot_dir +" firstboot_dir firstboot_dir
let firstboot_service = sprintf "\ [Unit] -- 1.9.3
This looks like a better approach to me, so ACK. Thanks, Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v
participants (2)
-
Pino Toscano -
Richard W.M. Jones