Blame SOURCES/0006-sysprep-Add-new-fs-uuids-operation.patch

022f11
From 883418f9a3e98eefaba4e9958f1b446270c034cf Mon Sep 17 00:00:00 2001
022f11
From: "Richard W.M. Jones" <rjones@redhat.com>
022f11
Date: Mon, 22 Jul 2013 13:28:28 +0100
022f11
Subject: [PATCH] sysprep: Add new fs-uuids operation.
022f11
022f11
This creates new random UUIDs for all filesystems in a guest.
022f11
022f11
Cherry picked from commit 8965368eb89532ac1613fffb0a3a661f005bae81
022f11
and modified for virt-sysprep in libguestfs 1.22.
022f11
---
022f11
 po/POTFILES-ml                        |  1 +
022f11
 resize/common_utils.ml                | 23 ++++++++++++++
022f11
 sysprep/Makefile.am                   |  1 +
022f11
 sysprep/sysprep_operation_fs_uuids.ml | 56 +++++++++++++++++++++++++++++++++++
022f11
 4 files changed, 81 insertions(+)
022f11
 create mode 100644 sysprep/sysprep_operation_fs_uuids.ml
022f11
022f11
diff --git a/po/POTFILES-ml b/po/POTFILES-ml
022f11
index ba64fa3..6ea4ee2 100644
022f11
--- a/po/POTFILES-ml
022f11
+++ b/po/POTFILES-ml
022f11
@@ -20,6 +20,7 @@ sysprep/sysprep_operation_dhcp_server_state.ml
022f11
 sysprep/sysprep_operation_dovecot_data.ml
022f11
 sysprep/sysprep_operation_firstboot.ml
022f11
 sysprep/sysprep_operation_flag_reconfiguration.ml
022f11
+sysprep/sysprep_operation_fs_uuids.ml
022f11
 sysprep/sysprep_operation_hostname.ml
022f11
 sysprep/sysprep_operation_kerberos_data.ml
022f11
 sysprep/sysprep_operation_logfiles.ml
022f11
diff --git a/resize/common_utils.ml b/resize/common_utils.ml
022f11
index 0f71810..37da8df 100644
022f11
--- a/resize/common_utils.ml
022f11
+++ b/resize/common_utils.ml
022f11
@@ -252,3 +252,26 @@ let display_long_options () =
022f11
         printf "%s\n" arg
022f11
   ) !long_options;
022f11
   exit 0
022f11
+
022f11
+let uuidgen () =
022f11
+  let cmd = "uuidgen -r" in
022f11
+  let chan = Unix.open_process_in cmd in
022f11
+  let uuid = input_line chan in
022f11
+  let stat = Unix.close_process_in chan in
022f11
+  (match stat with
022f11
+  | Unix.WEXITED 0 -> ()
022f11
+  | Unix.WEXITED i ->
022f11
+    error (f_"external command '%s' exited with error %d") cmd i
022f11
+  | Unix.WSIGNALED i ->
022f11
+    error (f_"external command '%s' killed by signal %d") cmd i
022f11
+  | Unix.WSTOPPED i ->
022f11
+    error (f_"external command '%s' stopped by signal %d") cmd i
022f11
+  );
022f11
+  let len = String.length uuid in
022f11
+  let uuid, len =
022f11
+    if len > 0 && uuid.[len-1] = '\n' then
022f11
+      String.sub uuid 0 (len-1), len-1
022f11
+    else
022f11
+      uuid, len in
022f11
+  if len < 10 then assert false; (* sanity check on uuidgen *)
022f11
+  uuid
022f11
diff --git a/sysprep/Makefile.am b/sysprep/Makefile.am
022f11
index 6bc5873..3060587 100644
022f11
--- a/sysprep/Makefile.am
022f11
+++ b/sysprep/Makefile.am
022f11
@@ -41,6 +41,7 @@ operations = \
022f11
 	dovecot_data \
022f11
 	flag_reconfiguration \
022f11
 	firstboot \
022f11
+	fs_uuids \
022f11
 	hostname \
022f11
 	kerberos_data \
022f11
 	lvm_uuids \
022f11
diff --git a/sysprep/sysprep_operation_fs_uuids.ml b/sysprep/sysprep_operation_fs_uuids.ml
022f11
new file mode 100644
022f11
index 0000000..3c319f0
022f11
--- /dev/null
022f11
+++ b/sysprep/sysprep_operation_fs_uuids.ml
022f11
@@ -0,0 +1,56 @@
022f11
+(* virt-sysprep
022f11
+ * Copyright (C) 2013 Red Hat Inc.
022f11
+ *
022f11
+ * This program is free software; you can redistribute it and/or modify
022f11
+ * it under the terms of the GNU General Public License as published by
022f11
+ * the Free Software Foundation; either version 2 of the License, or
022f11
+ * (at your option) any later version.
022f11
+ *
022f11
+ * This program is distributed in the hope that it will be useful,
022f11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
022f11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
022f11
+ * GNU General Public License for more details.
022f11
+ *
022f11
+ * You should have received a copy of the GNU General Public License along
022f11
+ * with this program; if not, write to the Free Software Foundation, Inc.,
022f11
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
022f11
+ *)
022f11
+
022f11
+open Printf
022f11
+
022f11
+open Sysprep_operation
022f11
+open Common_gettext.Gettext
022f11
+
022f11
+module G = Guestfs
022f11
+
022f11
+let rec fs_uuids_perform g root =
022f11
+  let fses = g#list_filesystems () in
022f11
+  List.iter (function
022f11
+  | _, "unknown" -> ()
022f11
+  | _, "swap" ->
022f11
+    (* XXX Not implemented *) ()
022f11
+  | dev, typ ->
022f11
+    let new_uuid = Common_utils.uuidgen () in
022f11
+    try
022f11
+      g#set_uuid dev new_uuid
022f11
+    with
022f11
+      G.Error msg ->
022f11
+        eprintf (f_"warning: cannot set random UUID on filesystem %s type %s: %s\n")
022f11
+          dev typ msg
022f11
+  ) fses;
022f11
+  []
022f11
+
022f11
+let fs_uuids_op = {
022f11
+  name = "fs-uuids";
022f11
+  enabled_by_default = true;
022f11
+  heading = s_"Change filesystem UUIDs";
022f11
+  pod_description = Some (s_"\
022f11
+On guests and filesystem types where this is supported,
022f11
+new random UUIDs are generated and assigned to filesystems.");
022f11
+  pod_notes = None;
022f11
+  extra_args = [];
022f11
+  perform_on_filesystems = None;
022f11
+  perform_on_devices = Some fs_uuids_perform;
022f11
+}
022f11
+
022f11
+let () = register_operation fs_uuids_op
022f11
-- 
022f11
1.8.3.1
022f11