Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Commit

Permalink
Merge pull request #95 from digitalrebar/conditionalize-dhcp-manageme…
Browse files Browse the repository at this point in the history
…nt-in-discovery

Conditionalize how discovery does DHCP management based on whether sy…
  • Loading branch information
galthaus authored Jun 13, 2018
2 parents 94754ac + 0c02532 commit 01d388b
Showing 1 changed file with 43 additions and 45 deletions.
88 changes: 43 additions & 45 deletions content/bootenvs/discovery.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,7 @@ Templates:
Path: "machines/start-up.sh"
Contents: |
#!/bin/bash
export PS4='${BASH_SOURCE}@${LINENO}(${FUNCNAME[0]}): '
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
set -x
set -e
shopt -s extglob
get_param() {
[[ $(cat /proc/cmdline) =~ $1 ]] && echo "${BASH_REMATCH[1]}"
}
dhcp_param() {
[[ $(cat /var/lib/dhclient/dhclient.leases) =~ $1 ]] && echo "${BASH_REMATCH[1]}"
}
get_macs() {
local maclist=""
Expand All @@ -76,9 +63,19 @@ Templates:
printf '[%s]' "${maclist#,}"
}
if [[ ! -f /etc/systemd/network/20-bootif.network ]]; then
get_param() {
[[ $(cat /proc/cmdline) =~ $1 ]] && echo "${BASH_REMATCH[1]}"
}
dhcp_param() {
[[ $(cat /var/lib/dhclient/dhclient.leases) =~ $1 ]] && echo "${BASH_REMATCH[1]}"
}
# Since we are not using normal networking, make sure that
# dhclient will stick around forever even if we exit
cat >"/etc/systemd/system/dhclient-$BOOTDEV.service" << EOF
cat >"/etc/systemd/system/dhclient-$BOOTDEV.service" << EOF
[Unit]
Description=dhclient for $BOOTDEV.service
After=network-online.target
Expand All @@ -92,38 +89,39 @@ Templates:
WantedBy=multi-user.target
EOF
systemctl daemon-reload
dhclient -x
systemctl start "dhclient-$BOOTDEV"
sleep 5
# Stuff from sledgehammer file that makes this command debuggable
# Some useful boot parameter matches
ip_re='([0-9a-f.:]+/[0-9]+)'
host_re='rs\.uuid=([^ ]+)'
hostname_re='option host-name "([^"]+)'
fixed_ip='fixed-address ([0-9a-f.:]+)'
uuid_re='^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$'
netname_re='"network":"([^ ]+)"'
# Assume nothing about the hostname.
unset HOSTNAME
# Check for DHCP set host name. Expand it to a FQDN if needed.
if dhcp_hostname="$(dhcp_param "$hostname_re")"; then
echo "Hostname set by DHCP to $dhcp_hostname"
if [[ ${dhcp_hostname%%.*} == $dhcp_hostname ]]; then
HOSTNAME="${dhcp_hostname}.${DOMAIN}"
systemctl daemon-reload
dhclient -x
systemctl start "dhclient-$BOOTDEV"
sleep 5
# Stuff from sledgehammer file that makes this command debuggable
# Some useful boot parameter matches
ip_re='([0-9a-f.:]+/[0-9]+)'
host_re='rs\.uuid=([^ ]+)'
hostname_re='option host-name "([^"]+)'
fixed_ip='fixed-address ([0-9a-f.:]+)'
uuid_re='^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$'
netname_re='"network":"([^ ]+)"'
# Assume nothing about the hostname.
unset HOSTNAME
# Check for DHCP set host name. Expand it to a FQDN if needed.
if dhcp_hostname="$(dhcp_param "$hostname_re")"; then
echo "Hostname set by DHCP to $dhcp_hostname"
if [[ ${dhcp_hostname%%.*} == $dhcp_hostname ]]; then
HOSTNAME="${dhcp_hostname}.${DOMAIN}"
else
HOSTNAME="$dhcp_hostname"
fi
else
HOSTNAME="$dhcp_hostname"
fi
else
dhcp_ip="$(dhcp_param "$fixed_ip")"
if [[ $dhcp_ip != "" ]] ; then
lookup_1=$(getent hosts $dhcp_ip | awk '{print $2}')
lookup_2=$(getent hosts $dhcp_ip | awk '{print $2}')
if [[ $lookup_1 && $lookup_1 == $lookup_2 && $lookup_1 != localhost* ]]; then
HOSTNAME=$lookup_1
dhcp_ip="$(dhcp_param "$fixed_ip")"
if [[ $dhcp_ip != "" ]] ; then
lookup_1=$(getent hosts $dhcp_ip | awk '{print $2}')
lookup_2=$(getent hosts $dhcp_ip | awk '{print $2}')
if [[ $lookup_1 && $lookup_1 == $lookup_2 && $lookup_1 != localhost* ]]; then
HOSTNAME=$lookup_1
fi
fi
fi
fi
Expand Down

0 comments on commit 01d388b

Please sign in to comment.