Nov 05

Introduction

Please see the article “Build a Linux-based Infrastructure Solution Demonstration Series” (http://blogs.czapski.id.au/2016/10/build-a-linux-based-infrastructure-solution-demonstration-series) for rationale, introduction and links to articles in this series.

As I work with the various demonstration images I find myself using the same tools, I naturally expect the tools to be ready to hand so I find myself adding the same launchers to the top panel.

In this article I build and execute a script which will add the “Update DHCP address in /etc/hosts at boot” script to the interactive boot level startup sequence so that the ip address is updated at boot to reflect the host ip address assigned to the host by DHCP. As in previous blog articles I also add the new scripts to the “initial configuration” script which is built cumulatively in the various articles in this series.

Pre-Requisites

This article assumes that

  1. The work is done in the Virtual Box Machine Image created in accordance with the instructions in the blog article to be found at http://blogs.czapski.id.au/2016/10/configure-virtual-box-virtual-machine-and-install-centos-6-8-base-image.
  2. The user “demo” has sudo access without a password. If this is not the case use the command “su -” and provide the password instead of saying “sudo -i” in the set of commands below

The instructions should work in other RedHat 6-like OS’ and OS versions.

Discussion

In the demonstration image as configured so far, there is no reference to the demo.demodomain.org in the /etc/hosts file.

We could manually update the /etc/hosts and point the demo.demodomain.org to 120.0.0.1, the loopback address. If it matters that the demo.demodomain.org points to an actual IP address of the NAT interface which a DHCP server assigns, and may change at different times or on different networks, we are better off creating a script to set /etc/hosts at boot so that the correct IP address is embedded. Everything that wants to translate the host name to the IP address uses /etc/hosts.

The /etc/hosts in the image, as at now, looks like this:

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

Let’s explore how this hosts file can be “fixed” to configure the “real” IP address of the host at boot time.

Before making changes to the /etc/hosts file lets save it so we can restore it if things go wrong.

sudo cp /etc/hosts /etc/hosts_as_installed

Let’s append the 127.0.0.1 address to /etc/hosts – it will be replaced by the script we are going to create shortly.

cat <<-'EOF' | sudo tee --append /etc/hosts
127.0.0.1   demo.demodomain.org
EOF

Add boot-time script to insert the current DHCP host IP and associate it with the host name

cat <<-'EOF' | sudo tee /etc/init.d/ofixhostip
#!/bin/bash
ipaddr=$(/sbin/ifconfig eth0| grep 'inet addr' | cut -d: -f2 | awk '{print $1}')
hn=$(hostname -s)
hnd=$(hostname)
sed -i '$s/.*/'$ipaddr'  '$hnd'   '$hn'/' /etc/hosts
EOF
sudo chmod ug+x /etc/init.d/ofixhostip

Add to interactive boot level (5) sequence

sudo /bin/sh -c "cd /etc/rc5.d; ln -s ../init.d/ofixhostip S10ofixhostip"

Reboot

sudo reboot

Verify IP address in /etc/hosts

cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.2.15  demo.demodomain.org   demo

Restore after changes to the /etc/hosts file so that we can test the script we will be developing

sudo cp -v /etc/hosts_as_installed /etc/hosts

Implementation

Now that we understand what needs to be done, and know how to do it, let’s create a script that will automate this part of image configuration for the next time we are creating a new image that needs this functionality.

Create the configuration script

mkdir -p /media/sf_distros/scripts
cat <<-'EODECK' > /media/sf_distros/scripts/009_update_dhcp_address_at_boot.sh
#!/bin/bash
# add replacement placeholder
cat <<-'EOF' | sudo tee --append /etc/hosts
127.0.0.1   demo.demodomain.org
EOF

# Add boot-time script to insert the current DHCP host IP and associate it with the host name
cat <<-'EOF' | sudo tee /etc/init.d/ofixhostip
#!/bin/bash
ipaddr=$(/sbin/ifconfig eth0| grep 'inet addr' | cut -d: -f2 | awk '{print $1}')
hn=$(hostname -s)
hnd=$(hostname)
sed -i '$s/.*/'$ipaddr'  '$hnd'   '$hn'/' /etc/hosts
EOF
sudo chmod ug+x /etc/init.d/ofixhostip

# Add to interactive boot level (5) sequence
sudo /bin/sh -c "cd /etc/rc5.d; ln -f -s ../init.d/ofixhostip S10ofixhostip"

EODECK
chmod ug+x /media/sf_distros/scripts/009_update_dhcp_address_at_boot.sh

Execute the script

/bin/bash -v /media/sf_distros/scripts/009_update_dhcp_address_at_boot.sh

Reboot

sudo reboot

Verify IP address in /etc/hosts

cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.2.15  demo.demodomain.org   demo

It is expected that the image being configured a bit at a time in this series of articles will be created more than once for different purposes. With this assumptions the individual scripts are appended to a single script so that the second and subsequent images can be configured by a single script rather than having lots of scripts to execute manually.

Append “Update DHCP address in /etc/hosts at boot” script execution commands to the initial bulk configuration script. This script is intended to collect all automated configuration commands and scripts so that they can be all executed in one go on a brand new image if one gets to do this the second and subsequent times.

Don’t actually execute this script while you are building the first image.

cat <<-'EODECK' >> /media/sf_distros/scripts/000_initial_bulk_configuration.sh
# Update DHCP address in /etc/hosts at boot
/bin/bash -v /media/sf_distros/scripts/009_update_dhcp_address_at_boot.sh

EODECK
chmod ug+x /media/sf_distros/scripts/000_initial_bulk_configuration.sh
preload preload preload