summaryrefslogtreecommitdiffstats
path: root/sysprep/sysprep_operation_hostname.ml
diff options
context:
space:
mode:
Diffstat (limited to 'sysprep/sysprep_operation_hostname.ml')
-rw-r--r--sysprep/sysprep_operation_hostname.ml69
1 files changed, 69 insertions, 0 deletions
diff --git a/sysprep/sysprep_operation_hostname.ml b/sysprep/sysprep_operation_hostname.ml
new file mode 100644
index 00000000..1472a1c4
--- /dev/null
+++ b/sysprep/sysprep_operation_hostname.ml
@@ -0,0 +1,69 @@
+(* virt-sysprep
+ * Copyright (C) 2012 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.
+ *)
+
+open Printf
+
+open Utils
+open Sysprep_operation
+
+module G = Guestfs
+
+let hostname = ref "localhost.localdomain"
+
+let 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") ->
+ (* Replace HOSTNAME=... entry. The code assumes it's a small,
+ * plain text file.
+ *)
+ let filename = "/etc/sysconfig/network" in
+ 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 ^
+ sprintf "\nHOSTNAME=%s\n" !hostname in
+ g#write filename file;
+ [ `Created_files ]
+
+ | "linux", ("debian"|"ubuntu") ->
+ g#write "/etc/hostname" !hostname;
+ [ `Created_files ]
+
+ | _ -> []
+
+let hostname_op = {
+ name = "hostname";
+ pod_description = "\
+Changes the hostname of the guest to the value given in the I<--hostname>
+parameter.
+
+If the I<--hostname> parameter is not given, then the hostname is changed
+to C<localhost.localdomain>.";
+ extra_args = [
+ ("--hostname", Arg.Set_string hostname, "hostname New hostname"),
+ "\
+Change the hostname. If not given, defaults to C<localhost.localdomain>."
+ ];
+ perform = hostname_perform;
+}
+
+let () = register_operation hostname_op