From 6203c50479c0c693c3fbdca222d28e98f4e094e5 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Wed, 2 Oct 2013 17:57:08 +0100 Subject: [PATCH] sysprep: Refactor setting random seed code into a common library. This is just code motion. --- mllib/Makefile.am | 3 ++ mllib/random_seed.ml | 42 ++++++++++++++++++++++++ mllib/random_seed.mli | 21 ++++++++++++ po/POTFILES-ml | 1 + sysprep/Makefile.am | 1 + sysprep/sysprep_operation_random_seed.ml | 25 ++------------ 6 files changed, 71 insertions(+), 22 deletions(-) create mode 100644 mllib/random_seed.ml create mode 100644 mllib/random_seed.mli diff --git a/mllib/Makefile.am b/mllib/Makefile.am index 10c6b594a..cfaaa3ac1 100644 --- a/mllib/Makefile.am +++ b/mllib/Makefile.am @@ -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 \ diff --git a/mllib/random_seed.ml b/mllib/random_seed.ml new file mode 100644 index 000000000..ed984ad02 --- /dev/null +++ b/mllib/random_seed.ml @@ -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 diff --git a/mllib/random_seed.mli b/mllib/random_seed.mli new file mode 100644 index 000000000..8c8ecf2e7 --- /dev/null +++ b/mllib/random_seed.mli @@ -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. *) diff --git a/po/POTFILES-ml b/po/POTFILES-ml index 3d6971a86..55861bcb7 100644 --- a/po/POTFILES-ml +++ b/po/POTFILES-ml @@ -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 diff --git a/sysprep/Makefile.am b/sysprep/Makefile.am index e2fb5674a..ad04ffa8e 100644 --- a/sysprep/Makefile.am +++ b/sysprep/Makefile.am @@ -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)) \ diff --git a/sysprep/sysprep_operation_random_seed.ml b/sysprep/sysprep_operation_random_seed.ml index fdb7b1190..0e6a2a268 100644 --- a/sysprep/sysprep_operation_random_seed.ml +++ b/sysprep/sysprep_operation_random_seed.ml @@ -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