[Libguestfs] [PATCH] sysprep: remove hostname from ifcfg-*

Wanlong Gao gaowanlong at cn.fujitsu.com
Mon Sep 3 08:05:37 UTC 2012


Remove hostname from network interface configuration.

Signed-off-by: Wanlong Gao <gaowanlong at cn.fujitsu.com>
---
 po/POTFILES-ml                            |  1 +
 sysprep/Makefile.am                       |  1 +
 sysprep/sysprep_operation_net_hostname.ml | 58 +++++++++++++++++++++++++++++++
 3 files changed, 60 insertions(+)
 create mode 100644 sysprep/sysprep_operation_net_hostname.ml

diff --git a/po/POTFILES-ml b/po/POTFILES-ml
index 76043a0..c9422fb 100644
--- a/po/POTFILES-ml
+++ b/po/POTFILES-ml
@@ -27,6 +27,7 @@ sysprep/sysprep_operation_logfiles.ml
 sysprep/sysprep_operation_lvm_uuids.ml
 sysprep/sysprep_operation_machine_id.ml
 sysprep/sysprep_operation_mail_spool.ml
+sysprep/sysprep_operation_net_hostname.ml
 sysprep/sysprep_operation_net_hwaddr.ml
 sysprep/sysprep_operation_pacct_log.ml
 sysprep/sysprep_operation_package_manager_cache.ml
diff --git a/sysprep/Makefile.am b/sysprep/Makefile.am
index 2ee9abf..46b8309 100644
--- a/sysprep/Makefile.am
+++ b/sysprep/Makefile.am
@@ -55,6 +55,7 @@ operations = \
 	logfiles \
 	machine_id \
 	mail_spool \
+	net_hostname \
 	net_hwaddr \
 	pacct_log \
 	package_manager_cache \
diff --git a/sysprep/sysprep_operation_net_hostname.ml b/sysprep/sysprep_operation_net_hostname.ml
new file mode 100644
index 0000000..44c6939
--- /dev/null
+++ b/sysprep/sysprep_operation_net_hostname.ml
@@ -0,0 +1,58 @@
+(* virt-sysprep
+ * Copyright (C) 2012 Fujitsu Limited
+ *
+ * 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.
+ *)
+
+open Utils
+open Sysprep_operation
+open Sysprep_gettext.Gettext
+
+module G = Guestfs
+
+let net_hostname_perform g root =
+  let typ = g#inspect_get_type root in
+  let distro = g#inspect_get_distro root in
+  match typ, distro with
+  | "linux", ("fedora"|"rhel"|"centos"|"scientificlinux"|"redhat-based") ->
+    let filenames = g#glob_expand "/etc/sysconfig/network-scripts/ifcfg-*" in
+    Array.iter (
+      fun filename ->
+        (* Replace HOSTNAME=... entry. *)
+        let lines = Array.to_list (g#read_lines filename) in
+        let lines = List.filter (
+          fun line -> not (string_prefix line "HOSTNAME=")
+        ) lines in
+        let file = String.concat "\n" lines ^ "\n" in
+        g#write filename file
+    ) filenames;
+
+    if filenames <> [||] then [ `Created_files ] else []
+
+  | _ -> []
+
+let net_hostname_op = {
+  name = "net-hostname";
+  enabled_by_default = true;
+  heading = s_"Remove HOSTNAME in network interface configuration";
+  pod_description = Some (s_"\
+For Fedora and Red Hat Enterprise Linux,
+this is removed from C<ifcfg-*> files.");
+  extra_args = [];
+  perform_on_filesystems = Some net_hostname_perform;
+  perform_on_devices = None;
+}
+
+let () = register_operation net_hostname_op
-- 
1.7.12




More information about the Libguestfs mailing list