1
0
mirror of synced 2026-02-10 01:29:56 +00:00

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
This commit is contained in:
Dmitry Nedospasov
2010-06-05 16:57:37 +02:00
parent a0fba6f9c7
commit 83ff7c416f
3 changed files with 74 additions and 35 deletions

View File

@@ -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' },

View File

@@ -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
#

View File

@@ -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 <<EOF
cat > ${prefix}/etc/hosts <<EOF
127.0.0.1 localhost
127.0.1.1 $(echo ${hostname} | awk -F '.' '{ print $1 }') ${hostname}
EOF
#
# Make sure our hostname and FQDN is resolvable
#
if [ -z "${dhcp}" ]; then
#
# If dhcp is diabled, FQDN should resolve to our IP
#
cat >> ${prefix}/etc/hosts <<EOF
127.0.0.1 $GUEST_HOSTNAME
${ip1} $GUEST_FQDN
EOF
else
cat >> ${prefix}/etc/hosts <<EOF
127.0.1.1 $GUEST_HOSTNAME $GUEST_FQDN
EOF
fi
cat >> ${prefix}/etc/hosts <<EOF
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
@@ -74,7 +80,26 @@ ff02::3 ip6-allhosts
EOF
else
# Non-IPv6 stuff.
grep -v '\(::\|IPv6\)' /etc/hosts > ${prefix}/etc/hosts
# New entry.
if [ -z "${dhcp}" ]; then
cat >> ${prefix}/etc/hosts <<EOF
127.0.0.1 $GUEST_HOSTNAME
${ip1} $GUEST_FQDN
EOF
else
cat >> ${prefix}/etc/hosts <<EOF
127.0.1.1 $GUEST_HOSTNAME $GUEST_FQDN
EOF
fi
# IPv6 stuff.
grep '\(::\|IPv6\)' /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