[Libguestfs] [PATCH 1/2] mllib: hostname: replace the hostname on Debian also in /etc/hosts (RHBZ#953907).

Pino Toscano ptoscano at redhat.com
Wed Feb 12 14:30:52 UTC 2014


In Debian/Ubuntu systems, read the previous hostname from /etc/hostname
before replacing it, and try to carefully replace it in /etc/hosts with
the new hostname.

Since Perl_edit to edit /etc/hosts, it is added/changed as dependency
for Hostname.
---
 builder/Makefile.am |  2 +-
 mllib/Makefile.am   |  2 +-
 mllib/hostname.ml   | 22 ++++++++++++++++++++++
 sysprep/Makefile.am |  1 +
 4 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/builder/Makefile.am b/builder/Makefile.am
index 9d2dbc5..0880e03 100644
--- a/builder/Makefile.am
+++ b/builder/Makefile.am
@@ -69,10 +69,10 @@ OBJECTS = \
 	$(top_builddir)/mllib/common_utils.cmx \
 	$(top_builddir)/mllib/urandom.cmx \
 	$(top_builddir)/mllib/random_seed.cmx \
+	$(top_builddir)/mllib/perl_edit.cmx \
 	$(top_builddir)/mllib/hostname.cmx \
 	$(top_builddir)/mllib/timezone.cmx \
 	$(top_builddir)/mllib/firstboot.cmx \
-	$(top_builddir)/mllib/perl_edit.cmx \
 	$(top_builddir)/mllib/crypt-c.o \
 	$(top_builddir)/mllib/crypt.cmx \
 	$(top_builddir)/mllib/fsync-c.o \
diff --git a/mllib/Makefile.am b/mllib/Makefile.am
index 67f0a0a..fc328d6 100644
--- a/mllib/Makefile.am
+++ b/mllib/Makefile.am
@@ -80,10 +80,10 @@ OBJECTS = \
 	common_utils.cmx \
 	urandom.cmx \
 	random_seed.cmx \
+	perl_edit.cmx \
 	hostname.cmx \
 	timezone.cmx \
 	firstboot.cmx \
-	perl_edit.cmx \
 	tTY.cmx \
 	fsync.cmx \
 	progress.cmx \
diff --git a/mllib/hostname.ml b/mllib/hostname.ml
index 6702f29..e1c5d4b 100644
--- a/mllib/hostname.ml
+++ b/mllib/hostname.ml
@@ -42,7 +42,12 @@ let rec set_hostname (g : Guestfs.guestfs) root hostname =
     true
 
   | "linux", ("debian"|"ubuntu"), _ ->
+    let old_hostname = read_etc_hostname g in
     update_etc_hostname g hostname;
+    (match old_hostname with
+    | Some old_hostname -> replace_host_in_etc_hosts g old_hostname hostname
+    | None -> ()
+    );
     true
 
   | "linux", ("fedora"|"rhel"|"centos"|"scientificlinux"|"redhat-based"), _ ->
@@ -78,3 +83,20 @@ and update_etc_hostname g hostname =
 
 and update_etc_machine_info g hostname =
   replace_line_in_file g "/etc/machine-info" "PRETTY_HOSTNAME" hostname
+
+and read_etc_hostname g =
+  let filename = "/etc/hostname" in
+  if g#is_file filename then (
+    let lines = Array.to_list (g#read_lines filename) in
+    match lines with
+    | hd :: _ -> Some hd
+    | [] -> None
+  ) else
+    None
+
+and replace_host_in_etc_hosts g oldhost newhost =
+  let filename = "/etc/hosts" in
+  if g#is_file filename then (
+    Perl_edit.edit_file ~debug:false g filename
+      ("s,(\\s)" ^ (Str.quote oldhost) ^ "(\\s|\\$),\\1" ^ (Str.quote newhost) ^ "\\2, if ($_ !~ /^#/)")
+  )
diff --git a/sysprep/Makefile.am b/sysprep/Makefile.am
index 4c03c7f..25cdaa5 100644
--- a/sysprep/Makefile.am
+++ b/sysprep/Makefile.am
@@ -95,6 +95,7 @@ OBJECTS = \
 	$(top_builddir)/mllib/urandom.cmx \
 	$(top_builddir)/mllib/password.cmx \
 	$(top_builddir)/mllib/random_seed.cmx \
+	$(top_builddir)/mllib/perl_edit.cmx \
 	$(top_builddir)/mllib/hostname.cmx \
 	$(top_builddir)/mllib/timezone.cmx \
 	$(top_builddir)/mllib/firstboot.cmx \
-- 
1.8.3.1




More information about the Libguestfs mailing list