summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-core/dhcp-check/dhcp-check/dhcp-check.sh
blob: e2ee80d89e2c19af3316f320c92ba5ab750e4623 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/sh

# if eth0 is configured DHCP and the interface is up (has carrier) and there is
# no IP address after 2 mins, restart the interface to try again

LOG_TAG=dhcp-check
CHK_DELAY=120
CFG_DELAY=60
IFC=eth0
NETWORK_CONFIG=/etc/systemd/network/00-bmc-${IFC}.network

carrier=$(cat /sys/class/net/${IFC}/carrier 2>/dev/null)
if [[ "$carrier" == "1" ]]; then
    dhcp=$(egrep -c "DHCP=(true|ipv4)" $NETWORK_CONFIG)
    if [[ "$dhcp" != "0" ]]; then
        # Give time for DHCP address to be configured
        sleep $CHK_DELAY

        hasip=$(ip addr show dev ${IFC} | grep -c dynamic)
        if [[ "$hasip" == "0" ]]; then
            logger -t $LOG_TAG \
                "$IFC: No DHCP address after ${CHK_DELAY}s; reconfigure"
            networkctl reconfigure $IFC

            # give time for network to come up and acquire DHCP address
            sleep $CFG_DELAY
        fi

        IP=$(ip addr show dev ${IFC} | sed -n '/dynamic/s/^.*inet \([0-9\.]*\).*$/\1/p')
        logger -t $LOG_TAG "$IFC: DHCP address $IP"
    fi
fi

exit 0