[Pkg-libvirt-commits] [libguestfs] 206/233: mllib: hostname: on Debian replace it also in /etc/hosts (RHBZ#953907).

Hilko Bengen bengen at moszumanska.debian.org
Wed Feb 19 21:12:25 UTC 2014


This is an automated email from the git hooks/post-receive script.

bengen pushed a commit to branch experimental
in repository libguestfs.

commit ab2df2e6598d421067d600f89a3d225162424d9b
Author: Pino Toscano <ptoscano at redhat.com>
Date:   Thu Feb 13 14:52:50 2014 +0100

    mllib: hostname: on Debian replace it also in /etc/hosts (RHBZ#953907).
    
    In Debian/Ubuntu systems, read the previous hostname from /etc/hostname
    before replacing it, and replace it in /etc/hosts with the new hostname.
---
 mllib/hostname.ml | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/mllib/hostname.ml b/mllib/hostname.ml
index fce16ff..70ca934 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,28 @@ 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 =
+  if g#is_file "/etc/hosts" then (
+    let expr = "/files/etc/hosts/*[label() != '#comment']/*[label() != 'ipaddr']" in
+    g#aug_init "/" 0;
+    let matches = Array.to_list (g#aug_match expr) in
+    List.iter (
+      fun m ->
+        let value = g#aug_get m in
+        if value = oldhost then (
+          g#aug_set m newhost
+        )
+    ) matches;
+    g#aug_save ()
+  )

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-libvirt/libguestfs.git



More information about the Pkg-libvirt-commits mailing list