diff options
author | Simon Rettberg | 2014-01-15 19:34:27 +0100 |
---|---|---|
committer | Simon Rettberg | 2014-01-15 19:34:27 +0100 |
commit | d22acc91f529019e749581f123e8d0e280ff15ce (patch) | |
tree | 8d21c7129a9e2ef4cf5e7b73acaa35e105689acb /remote/modules/dhcpc-busybox | |
parent | Merge branch 'master' of dnbd3:openslx-ng/tm-scripts (diff) | |
download | tm-scripts-d22acc91f529019e749581f123e8d0e280ff15ce.tar.gz tm-scripts-d22acc91f529019e749581f123e8d0e280ff15ce.tar.xz tm-scripts-d22acc91f529019e749581f123e8d0e280ff15ce.zip |
[dhcpc-busybox] Minor refactoring, more sanity checks
Diffstat (limited to 'remote/modules/dhcpc-busybox')
-rwxr-xr-x | remote/modules/dhcpc-busybox/data/opt/openslx/scripts/udhcpc-openslx | 105 |
1 files changed, 64 insertions, 41 deletions
diff --git a/remote/modules/dhcpc-busybox/data/opt/openslx/scripts/udhcpc-openslx b/remote/modules/dhcpc-busybox/data/opt/openslx/scripts/udhcpc-openslx index b6ea93c4..aec7f0d6 100755 --- a/remote/modules/dhcpc-busybox/data/opt/openslx/scripts/udhcpc-openslx +++ b/remote/modules/dhcpc-busybox/data/opt/openslx/scripts/udhcpc-openslx @@ -1,4 +1,5 @@ #!/bin/bash +# Needs bash for printf -v VARNAME # ----------------------------------------------------------------------------- # # Copyright (c) 2011 - OpenSLX GmbH @@ -19,14 +20,12 @@ . /opt/openslx/config RESOLV_CONF="/etc/resolv.conf" -THISFILE="/run/udhcpc/${interface}.resolv" - -echo "$interface [$1] $ip" >> "/tmp/udhcpclog" +THIS_RESOLV="/run/udhcpc/${interface}.resolv" rebuild_resolv_conf () { # Maybe make this smarter some time, if anyone is using client's that are on multiple networks at once etc... # This is a little braindead but should work most of the time - sort -u /run/udhcpc/*.resolv > "$RESOLV_CONF" + sort -u /run/udhcpc/*.resolv > "$RESOLV_CONF" 2> /dev/null } escape_search() { @@ -37,9 +36,32 @@ escape_replace() { echo "$@" | sed -e 's/[\/&]/\\&/g' } +check_env() { + if [ -z "$ip" -o -z "$subnet" -o -z "$interface" ]; then + echo "$1 event with missing data" >&2 + echo "ip = '$ip'" >&2 + echo "subnet = '$subnet'" >&2 + echo "interface = '$interface'" >&2 + exit 1 + fi +} + +if [ ! -d /run ]; then + echo -n "Waiting for /run." >&2 + while [ ! -d /run ]; do + echo -n "." >&2 + usleep 500000 + done + echo "" >&2 +fi + case "$1" in bound|renew) + check_env "$1" + mkdir -p "/run/udhcpc" + # Set address on interface ip addr add "$ip/$(ipcalc -s -p $ip $subnet|sed s/.*=//)" dev "$interface" + # Set default route, if given if [ -n "$router" ]; then ip route add default via "$router" fi @@ -63,8 +85,7 @@ case "$1" in echo -n "$CONF" | resolvconf -a "${interface}.udhcpc" else # Manual handling required :-( - mkdir -p "/run/udhcpc" - echo -n "$CONF" | sed 's# 132\.200\.# 132.230.#g' > "$THISFILE" # HACK for Pharmazie-Pool, as DHCP gives wrong DNS information. Remove when DHCP guys fixed it... + echo -n "$CONF" > "$THIS_RESOLV" rebuild_resolv_conf fi @@ -81,15 +102,19 @@ case "$1" in # fallback to what the dhcp told us dns_host="$hostname" fi - # Avoid empty hostnames - [ -z "$dns_host" ] && dns_host="slx-client" - echo "$dns_host" > "/proc/sys/kernel/hostname" - echo "$dns_host" > "/etc/hostname" - if grep '^SLX_HOSTNAME=' /opt/openslx/config 2>/dev/null; then - sed -i "s/^\(SLX_HOSTNAME=\).*$/\1'$dns_host'/" /opt/openslx/config - else - echo "# Config written by openslx-dhcp-script (1)" >> /opt/openslx/config - echo "SLX_HOSTNAME='$dns_host'" >> /opt/openslx/config + if [ -z "$dns_host" ]; then + # only if there is no /etc/hostname, we fall back (far back, that is) + [ ! -s "/etc/hostname" ] && dns_host="slx-client" + fi + if [ -n "$dns_host" ]; then + echo "$dns_host" > "/proc/sys/kernel/hostname" + echo "$dns_host" > "/etc/hostname" + if grep '^SLX_HOSTNAME=' /opt/openslx/config 2>/dev/null; then + sed -i "s/^\(SLX_HOSTNAME=\).*$/\1'$dns_host'/" /opt/openslx/config + else + echo "# Config written by openslx-dhcp-script (1)" >> /opt/openslx/config + echo "SLX_HOSTNAME='$dns_host'" >> /opt/openslx/config + fi fi # if domain is given from dhcp, save it to openslx config for later use in VMs @@ -109,28 +134,23 @@ case "$1" in # Hostname in /etc/hosts touch /etc/hosts - if [ -n "$dns_host" ]; then - short="${dns_host%%.*}" - [ "x$short" = "x$dns_host" ] && short="" - sed -i -r "s/\s$(escape_search "$dns_host")(\s|$)/ /g" /etc/hosts - [ -n "$short" ] && sed -i -r "s/\s$(escape_search "$short")(\s|$)/ /g" /etc/hosts - if grep -q -E "^$ip\s" /etc/hosts; then - sed -i "s/^$(escape_search "$ip")\s/$(escape_replace "$ip $dns_host $short ")/g" /etc/hosts - else - echo "$ip $dns_host $short" >> /etc/hosts - fi - fi - if [ -n "$hostname" -a "x$hostname" != "x$dns_host" ]; then - short="${hostname%%.*}" - [ "x$short" = "x$hostname" ] && short="" - sed -i -r "s/\s$(escape_search "$hostname")(\s|$)/ /g" /etc/hosts - [ -n "$short" ] && sed -i -r "s/\s$(escape_search "$short")(\s|$)/ /g" /etc/hosts - if grep -q -E "^$ip\s" /etc/hosts; then - sed -i "s/^$(escape_search "$ip")\s/$(escape_replace "$ip $hostname $short ")/g" /etc/hosts - else - echo "$ip $hostname $short" >> /etc/hosts - fi + hostlist="" + [ -n "$dns_host" ] && hostlist="$dns_host" + [ -n "$hostname" -a "x$hostname" != "x$dns_host" ] && hostlist="$hostlist $hostname" + if [ -n "$hostlist" ]; then + for host in $hostlist; do + short="${host%%.*}" + [ "x$short" = "x$host" ] && short="" + sed -i -r "s/\s$(escape_search "$host")(\s|$)/ /g" /etc/hosts + [ -n "$short" ] && sed -i -r "s/\s$(escape_search "$short")(\s|$)/ /g" /etc/hosts + if grep -q -E "^$ip\s" /etc/hosts; then + sed -i "s/^$(escape_search "$ip")\s/$(escape_replace "$ip $host $short ")/g" /etc/hosts + else + echo "$ip $host $short" >> /etc/hosts + fi + done fi + # Get rid of orphaned lines sed -i -r '/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s*$/d' /etc/hosts # We consider the network setup to be complete if an interface is being configured that @@ -152,6 +172,7 @@ case "$1" in ;; deconfig) + check_env "$1" if [ $(grep -c "nfs=" /proc/cmdline) == 0 ]; then echo 1 > "/proc/sys/net/ipv4/conf/$interface/promote_secondaries" clientip=${ip%%:*} @@ -165,26 +186,28 @@ case "$1" in resolvconf -d "${interface}.udhcpc" else # Manual handling required :-( - rm -f "$THISFILE" + rm -f -- "$THIS_RESOLV" rebuild_resolv_conf fi ;; leasefail) - echo "$0: Lease failed: $message" + echo "$0: Lease failed: $message" >&2 ;; nak) - echo "$0: Received a NAK: $message" + echo "$0: Received a NAK: $message" >&2 ;; *) - echo "$0: Unknown udhcpc command: $1"; - exit 1; + echo "$0: Unknown udhcpc command: $1" >&2 + exit 1 ;; esac +exit 0 + |