From 83ff7c416fb7af0e13746edac9867cf7016e7319 Mon Sep 17 00:00:00 2001 From: Dmitry Nedospasov Date: Sat, 5 Jun 2010 16:57:37 +0200 Subject: [PATCH] Cleaned up 50-setup-hostname, added --copy-hosts option * Long overdue clean up of 50-setup-hostname, since simply copying the dom0's /etc/hosts file isn't reliable. * Added a --copy-hosts option for those that do want to copy the dom0's /etc/hosts * Documented both options in the .conf file as well as in the scripts --- bin/xen-create-image | 7 ++- etc/xen-tools.conf | 17 +++++++ hooks/debian/50-setup-hostname | 85 +++++++++++++++++++++------------- 3 files changed, 74 insertions(+), 35 deletions(-) diff --git a/bin/xen-create-image b/bin/xen-create-image index b53f478..31b3ed9 100755 --- a/bin/xen-create-image +++ b/bin/xen-create-image @@ -94,6 +94,8 @@ xen-create-image - Easily create new Xen instances with networking and OpenSSH. --no-hosts Don't touch /etc/hosts on the dom0. + --copy-hosts Copy entries from the dom0's /etc/hosts file to the guest + --partitions=file Use a specific partition layout configuration file. See /etc/xen-tools/partitions.d/sample-server for an @@ -1510,8 +1512,9 @@ sub parseCommandLineArguments "disk_device=s", \$CONFIG{ 'disk_device' }, - # Host options - "no-hosts", \$CONFIG{ 'nohosts' }, + # Hosts options + "no-hosts", \$CONFIG{ 'nohosts' }, + "copy-hosts", \$CONFIG{ 'copyhosts' }, # Networking options "dhcp", \$CONFIG{ 'dhcp' }, diff --git a/etc/xen-tools.conf b/etc/xen-tools.conf index 30efbc5..2dfba35 100644 --- a/etc/xen-tools.conf +++ b/etc/xen-tools.conf @@ -290,3 +290,20 @@ reiser_options = defaults # output = /etc/xen # extension = .cfg # + +# +# Here you can conrol weather your dom0's /etc/hosts file should be +# appended with the new guest, and also if your dom0's /etc/hosts file +# should be copied to the new guest. +# +# Change the following options to 1 to set them +# nohosts - don't touch the domm0's /etc/hosts file +# copyhosts - don't copy the dom0's /etc/hosts to the guest +# +# by default new guests ARE added to the dom0's /etc/hosts file +# nohosts = 0 # default +# +# by default the dom0's /etc/hosts IS NOT copied +# copyhosts = 0 # default +# + diff --git a/hooks/debian/50-setup-hostname b/hooks/debian/50-setup-hostname index 1d23bdc..800041b 100755 --- a/hooks/debian/50-setup-hostname +++ b/hooks/debian/50-setup-hostname @@ -27,42 +27,48 @@ fi # logMessage Script $0 starting +# +# Determine the FQDN and Hostname +# +GUEST_FQDN=${hostname} +GUEST_HOSTNAME=`echo $GUEST_FQDN | awk -F'.' '{print $1}'` # # Setup the mailname + hostname files. # -echo ${hostname} | sed 's/^\([^\.]*\)\..*/\1/' > ${prefix}/etc/hostname -echo ${hostname} > ${prefix}/etc/mailname +echo $GUEST_HOSTNAME > ${prefix}/etc/hostname +echo $GUEST_FQDN > ${prefix}/etc/mailname # # Fixup the /etc/hosts file upon the new image for # machines with static IPs # -if [ -z "${dhcp}" ]; then - - # Non-IPv6 stuff. - # TODO: Think of a better way of doing this, this may have a lot of trash - # depending on the dom0 (i.e. 127.0.0.1 dom0.example.com) - grep -v '\(::\|IPv6\)' /etc/hosts > ${prefix}/etc/hosts - - # New entry. - # echo "127.0.0.1 localhost" >> ${prefix}/etc/hosts - echo "127.0.1.1 $(echo ${hostname} | awk -F '.' '{ print $1 }')" >> ${prefix}/etc/hosts - echo "${ip1} ${hostname}" >> ${prefix}/etc/hosts - echo " " >> ${prefix}/etc/hosts - - # IPv6 stuff. - grep '\(::\|IPv6\)' /etc/hosts >> ${prefix}/etc/hosts - -else - +if [ -z "${copyhosts}" ]; then # - # Stub /etc/hosts for DHCP clients. + # Copy localhost # - cat >> ${prefix}/etc/hosts < ${prefix}/etc/hosts <> ${prefix}/etc/hosts <> ${prefix}/etc/hosts <> ${prefix}/etc/hosts < ${prefix}/etc/hosts + # New entry. + if [ -z "${dhcp}" ]; then + cat >> ${prefix}/etc/hosts <> ${prefix}/etc/hosts <> ${prefix}/etc/hosts fi @@ -83,21 +108,15 @@ fi # if [ -z "${dhcp}" ]; then - if ( grep ${hostname} /etc/hosts > /dev/null ) ; then + if ( grep $GUEST_FQDN /etc/hosts > /dev/null ) ; then - logMessage Host already has IP address for the host ${hostname}. + logMessage Host already has IP address for the host $GUEST_FQDN. else - - # - # Short host name. - # - name=`echo ${hostname} | awk -F. '{print $1}'` - if [ -z "${nohosts}" ]; then - logMessage Adding ${hostname} and ${name} to /etc/hosts on the host - echo "${ip1} ${hostname} ${name}" >> /etc/hosts + logMessage Adding $GUEST_FQDN and $GUEST_HOSTNAME to /etc/hosts on the host + echo "${ip1} $GUEST_FQDN $GUEST_HOSTNAME" >> /etc/hosts # # If we've updated the /etc/hosts file on the host machine