[Libguestfs] [PATCH] builder, sysprep: initialise the random generator

Pino Toscano ptoscano at redhat.com
Tue Jan 28 14:10:29 UTC 2014


virt-builder and virt-sysprep may make use of
Common_utils.string_random8 (which uses Random.int) for constructing
temporary paths; not initialising the random generator means that every
invocation will reuse the same name used previously (!).
Thus just call Random.self_init, just like virt-sparsify already does.

Expand the test-virt-sysprep-script.sh test to ensure that virt-sysprep
is not affected again by this issue.
---
 .gitignore                          |  1 +
 builder/builder.ml                  |  2 ++
 sysprep/main.ml                     |  2 ++
 sysprep/script4.sh                  | 21 +++++++++++++++++++++
 sysprep/test-virt-sysprep-script.sh | 22 +++++++++++++++++++++-
 5 files changed, 47 insertions(+), 1 deletion(-)
 create mode 100755 sysprep/script4.sh

diff --git a/.gitignore b/.gitignore
index 74661b0..d9bef99 100644
--- a/.gitignore
+++ b/.gitignore
@@ -428,6 +428,7 @@ Makefile.in
 /sysprep/.depend
 /sysprep/stamp-script1.sh
 /sysprep/stamp-script2.sh
+/sysprep/stamp-script4.sh
 /sysprep/stamp-virt-sysprep.pod
 /sysprep/sysprep-extra-options.pod
 /sysprep/sysprep-operations.pod
diff --git a/builder/builder.ml b/builder/builder.ml
index 3c45fa5..6dc172f 100644
--- a/builder/builder.ml
+++ b/builder/builder.ml
@@ -33,6 +33,8 @@ let quote = Filename.quote
 
 let prog = Filename.basename Sys.executable_name
 
+let () = Random.self_init ()
+
 let main () =
   (* Command line argument parsing - see cmdline.ml. *)
   let mode, arg,
diff --git a/sysprep/main.ml b/sysprep/main.ml
index c1ce3c7..9431e88 100644
--- a/sysprep/main.ml
+++ b/sysprep/main.ml
@@ -31,6 +31,8 @@ let () = Sysprep_operation.bake ()
 (* Command line argument parsing. *)
 let prog = Filename.basename Sys.executable_name
 
+let () = Random.self_init ()
+
 let debug_gc, operations, g, selinux_relabel, quiet, mount_opts =
   let debug_gc = ref false in
   let domain = ref None in
diff --git a/sysprep/script4.sh b/sysprep/script4.sh
new file mode 100755
index 0000000..fe377e0
--- /dev/null
+++ b/sysprep/script4.sh
@@ -0,0 +1,21 @@
+#!/bin/bash -
+# libguestfs virt-sysprep test --script option
+# Copyright (C) 2014 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+# This is used by test-virt-sysprep-script.sh.
+
+pwd >> $abs_builddir/stamp-script4.sh
diff --git a/sysprep/test-virt-sysprep-script.sh b/sysprep/test-virt-sysprep-script.sh
index 98ecb96..a62cfce 100755
--- a/sysprep/test-virt-sysprep-script.sh
+++ b/sysprep/test-virt-sysprep-script.sh
@@ -31,7 +31,7 @@ if [ ! -w /dev/fuse ]; then
 fi
 
 # Check that multiple scripts can run.
-rm -f stamp-script1.sh stamp-script2.sh
+rm -f stamp-script1.sh stamp-script2.sh stamp-script4.sh
 if ! ./virt-sysprep -q -n -a ../tests/guests/fedora.img --enable script \
         --script $abs_srcdir/script1.sh --script $abs_srcdir/script2.sh; then
     echo "$0: virt-sysprep wasn't expected to exit with error."
@@ -48,3 +48,23 @@ if ./virt-sysprep -q -n -a ../tests/guests/fedora.img --enable script \
     echo "$0: virt-sysprep didn't exit with an error."
     exit 1
 fi
+
+# Check that virt-sysprep uses a new temporary directory every time.
+if ! ./virt-sysprep -q -n -a ../tests/guests/fedora.img --enable script \
+        --script $abs_srcdir/script4.sh; then
+    echo "$0: virt-sysprep (script4.sh, try #1) wasn't expected to exit with error."
+    exit 1
+fi
+if ! ./virt-sysprep -q -n -a ../tests/guests/fedora.img --enable script \
+        --script $abs_srcdir/script4.sh; then
+    echo "$0: virt-sysprep (script4.sh, try #2) wasn't expected to exit with error."
+    exit 1
+fi
+if [ x"`wc -l stamp-script4.sh | awk '{print $1}'`" != x2 ]; then
+    echo "$0: stamp-script4.sh does not contain two lines."
+    exit 1
+fi
+if [ x"`head -n1 stamp-script4.sh`" == x"`tail -n1 stamp-script4.sh`" ]; then
+    echo "$0: stamp-script4.sh does not contain different paths."
+    exit 1
+fi
-- 
1.8.3.1




More information about the Libguestfs mailing list