52e4900c2f0a75b99b0ac93ca31db007ae60c98b
[platform/upstream/dracut.git] / modules.d / 40network / dhclient-script.sh
1 #!/bin/sh
2 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3 # ex: ts=8 sw=4 sts=4 et filetype=sh
4
5 setup_interface() {
6     ip=$new_ip_address
7     mtu=$new_interface_mtu
8     mask=$new_subnet_mask
9     bcast=$new_broadcast_address
10     gw=${new_routers%%,*}
11     domain=$new_domain_name
12     search=$(printf "$new_domain_search")
13     namesrv=$new_domain_name_servers
14     hostname=$new_host_name
15
16     [ -f /tmp/net.$netif.override ] && . /tmp/net.$netif.override
17
18     # Taken from debian dhclient-script:
19     # The 576 MTU is only used for X.25 and dialup connections
20     # where the admin wants low latency.  Such a low MTU can cause
21     # problems with UDP traffic, among other things.  As such,
22     # disallow MTUs from 576 and below by default, so that broken
23     # MTUs are ignored, but higher stuff is allowed (1492, 1500, etc).
24     if [ -n "$mtu" ] && [ $mtu -gt 576 ] ; then
25         echo "if ! ip link set $netif mtu $mtu ; then"
26         echo "ip link set $netif down"
27         echo "ip link set $netif mtu $mtu"
28         echo "ip link set $netif up"
29         echo wait_for_if_up $netif
30         echo "fi"
31     fi > /tmp/net.$netif.up
32
33     echo ip addr add $ip${mask:+/$mask} ${bcast:+broadcast $bcast} dev $netif >> /tmp/net.$netif.up
34
35     [ -n "$gw" ] && echo ip route add default via $gw dev $netif > /tmp/net.$netif.gw
36
37     [ -n "${search}${domain}" ] && echo "search $search $domain" > /tmp/net.$netif.resolv.conf
38     if  [ -n "$namesrv" ] ; then
39         for s in $namesrv; do
40             echo nameserver $s
41         done
42     fi >> /tmp/net.$netif.resolv.conf
43
44     # Note: hostname can be fqdn OR short hostname, so chop off any
45     # trailing domain name and explicity add any domain if set.
46     [ -n "$hostname" ] && echo "echo ${hostname%.$domain}${domain+.$domain} > /proc/sys/kernel/hostname" > /tmp/net.$netif.hostname
47 }
48
49 PATH=/usr/sbin:/usr/bin:/sbin:/bin
50
51 export PS4="dhclient.$interface.$$ + "
52 exec >>/run/initramfs/loginit.pipe 2>>/run/initramfs/loginit.pipe
53 type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
54
55 # We already need a set netif here
56 netif=$interface
57
58 # Huh? Interface configured?
59 [ -f "/tmp/net.$netif.up" ] && exit 0
60
61 case $reason in
62     PREINIT)
63         echo "dhcp: PREINIT $netif up"
64         ip link set $netif up
65         wait_for_if_up $netif
66         ;;
67     BOUND)
68         echo "dhcp: BOND setting $netif"
69         if ! arping -q -D -c 2 -I $netif $new_ip_address ; then
70             warn "Duplicate address detected for $new_ip_address while doing dhcp. retrying"
71             exit 1
72         fi
73         setup_interface
74         set | while read line; do
75             [ "${line#new_}" = "$line" ] && continue
76             echo "$line"
77         done >/tmp/dhclient.$netif.dhcpopts
78         echo online > /sys/class/net/$netif/uevent
79
80         if [ -e /tmp/net.$netif.manualup ]; then
81             /sbin/netroot $netif -m
82             rm -f /tmp/net.$netif.manualup
83         else
84             initqueue --onetime --name netroot-$netif netroot $netif
85         fi
86         ;;
87     *) echo "dhcp: $reason";;
88 esac
89
90 exit 0