sysprep: Refactor setting random seed code into a common library.

This is just code motion.
This commit is contained in:
Richard W.M. Jones
2013-10-02 17:57:08 +01:00
parent da1d0da429
commit 6203c50479
6 changed files with 71 additions and 22 deletions

View File

@@ -37,6 +37,8 @@ SOURCES = \
progress-c.c \
progress.mli \
progress.ml \
random_seed.mli \
random_seed.ml \
tty-c.c \
tTY.mli \
tTY.ml \
@@ -59,6 +61,7 @@ OBJECTS = \
crypt-c.o \
common_gettext.cmx \
common_utils.cmx \
random_seed.cmx \
firstboot.cmx \
tTY.cmx \
progress.cmx \

42
mllib/random_seed.ml Normal file
View File

@@ -0,0 +1,42 @@
(* virt-sysprep
* Copyright (C) 2012-2013 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.
*)
let set_random_seed (g : Guestfs.guestfs) root =
let typ = g#inspect_get_type root in
if typ = "linux" then (
let files = [
"/var/lib/random-seed"; (* Fedora *)
"/var/lib/urandom/random-seed"; (* Debian *)
"/var/lib/misc/random-seed"; (* SuSE *)
] in
List.iter (
fun file ->
if g#is_file file then (
(* Get 8 bytes of randomness from the host. *)
let chan = open_in "/dev/urandom" in
let buf = String.create 8 in
really_input chan buf 0 8;
close_in chan;
g#write file buf
)
) files;
true
)
else
false

21
mllib/random_seed.mli Normal file
View File

@@ -0,0 +1,21 @@
(* virt-sysprep
* Copyright (C) 2012-2013 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.
*)
val set_random_seed : Guestfs.guestfs -> string -> bool
(** Set the random seed in the guest. Returns true if it was able to
do set it, false if not. *)

View File

@@ -5,6 +5,7 @@ mllib/crypt.ml
mllib/firstboot.ml
mllib/password.ml
mllib/progress.ml
mllib/random_seed.ml
mllib/tTY.ml
mllib/uRI.ml
resize/resize.ml

View File

@@ -89,6 +89,7 @@ OBJECTS = \
$(top_builddir)/mllib/crypt-c.o \
$(top_builddir)/mllib/crypt.cmx \
$(top_builddir)/mllib/password.cmx \
$(top_builddir)/mllib/random_seed.cmx \
$(top_builddir)/mllib/firstboot.cmx \
sysprep_operation.cmx \
$(patsubst %,sysprep_operation_%.cmx,$(operations)) \

View File

@@ -19,31 +19,12 @@
open Sysprep_operation
open Common_gettext.Gettext
open Random_seed
module G = Guestfs
let random_seed_perform (g : Guestfs.guestfs) root =
let typ = g#inspect_get_type root in
if typ = "linux" then (
let files = [
"/var/lib/random-seed"; (* Fedora *)
"/var/lib/urandom/random-seed"; (* Debian *)
"/var/lib/misc/random-seed"; (* SuSE *)
] in
List.iter (
fun file ->
if g#is_file file then (
(* Get 8 bytes of randomness from the host. *)
let chan = open_in "/dev/urandom" in
let buf = String.create 8 in
really_input chan buf 0 8;
close_in chan;
g#write file buf
)
) files;
[ `Created_files ]
)
else []
if set_random_seed g root then [ `Created_files ] else []
let op = {
defaults with