9b095a619d9ee82030c5de9fbb02e828905cba8e
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / src / sbin / cirros-dhcpc
1 #!/bin/sh
2 # A simple wrapper around udhcpc so that we are able to dynamically specify
3 # which options it should ask for.
4
5 # note: ifupdown does not allow VERBOSITY through. can be set in OPTS_FILE.
6 VERBOSITY="${VERBOSITY:-0}"
7
8 . ${CIRROS_SHLIB:=/lib/cirros/shlib} ||
9         { echo "failed to read ${CIRROS_SHLIB}" 1>&2; exit 1; }
10
11 set -f
12
13 # ensure PATH has /sbin in it.
14 path_has /sbin || PATH="$PATH:/sbin"
15
16 OPTS_FILE="/etc/default/udhcpc"
17 RESOLV_CONF="/etc/resolv.conf"
18
19 apply_static_routes() {
20         # routes are pairs of network and gateway
21         # 169.254.169.254/32 10.65.0.128
22         local net="" router="" err=0
23         while [ $# -ne 0 ]; do
24                 net="$1"
25                 router="$2"
26                 [ -n "$net" ] || continue
27                 debug 1 "adding net $net with router $router"
28                 route add -net "$net" gw "$router" || {
29                         error "WARN: failed: route add -net \"$net\" gw \"$router\""
30                         err=$(($err+1));
31                 }
32                 shift 2 || {
33                         error "apply_static_routes: failed shift 2. odd number of args?"
34                         return 1;
35                 }
36         done
37         return $err
38 }
39
40
41 readconfig() {
42         [ ! -f "$OPTS_FILE" ] || . "$OPTS_FILE" ||
43                 { error "failed to read $OPTS_FILE"; return 1; }
44         # these are expected to be set.
45         OPTIONS=${OPTIONS:-staticroutes mtu}
46         TIMEOUT=${TIMEOUT:-60}
47         MTU=${MTU:-1500}
48 }
49
50 up() {
51         [ $# -ge 1 ] || { error "$0 up: must provide interface"; return 1; }
52         local iface="$1" opts="" hostname=""
53
54         readconfig || return
55         hostname=$(hostname 2>/dev/null)
56
57         # Gather all options and start udhcpc.
58         for opt in $OPTIONS; do
59                 opts="-O $opt $opts"
60         done
61
62         debug 1 "Starting udhcpc on $iface, asking for options {$OPTIONS}"
63         udhcpc -p "/var/run/udhcpc.${iface}.pid" -R -n \
64                 ${TIMEOUT:+-T "${TIMEOUT}"} -i "$iface" -s "$0" \
65                 $opts ${hostname:+-x "hostname:${hostname}"}
66 }
67
68 down() {
69         local iface="$1" pidfile="" pid=""
70         [ $# -ge 1 ] || { error "$0 down: must provide interface"; return 1; }
71         pidfile="/var/run/udhcpc.$iface.pid"
72         [ -f "$pidfile" ] ||
73                 { error "$iface: no pidfile"; return 1; }
74         read pid < "$pidfile" ||
75                 { error "failed to read pid from '$pidfile' for '$iface'"; return 1; }
76         kill $pid >/dev/null
77         return
78 }
79
80 renew_bound() {
81         local flags="" mode="$1" i=""
82         shift;
83         [ -n "$interface" ] ||
84                 { error "$0 $mode: 'interface' not provided"; return 1; }
85
86         [ -n "$ip" ] ||
87                 { error "$0 $mode: 'ip' not set in environment"; return 1; }
88
89         readconfig || return
90
91         [ -n "$broadcast" ] && flags="${flags}broadcast $broadcast "
92         [ -n "$subnet" ] && flags="${flags}netmask $subnet "
93         flags="${flags}mtu ${mtu:-${MTU}} "
94         flags=${flags% }
95
96         debug 1 "ifconfig $interface $ip $flags"
97         ifconfig $interface $ip $flags || {
98                 error "$0 $mode: failed: 'ifconfig $interface $ip $flags'";
99                 return 1;
100         }
101
102         if [ -n "$router" ] ; then
103                 local out="" ret=""
104                 debug 2 "deleting routers"
105                 while :; do
106                         out=$(route del default gw 0.0.0.0 dev $interface 2>&1)
107                         ret=$?
108                         [ $ret -eq 0 ] && break
109                         case "$out" in
110                                 *SIOCDELRT*[Nn]o\ such\ process*) break;;
111                         esac
112                         error "deleting routes failed: $out" 1>&2
113                 done
114
115                 for i in $router; do
116                         debug 1 "route add default gw $i dev $interface"
117                         route add default gw $i dev $interface
118                 done
119         fi
120
121         local msg="configuring $RESOLV_CONF for $interface. domain=$domain"
122         msg="$msg nameservers: $dns"
123         debug 1 "$msg"
124         {
125                 [ -n "$domain" ] && echo "search $domain"
126                 for i in $dns ; do
127                         echo nameserver $i
128                 done
129         } > "$RESOLV_CONF"
130
131         apply_static_routes $staticroutes
132 }
133
134 case "$1" in
135         up)
136                 shift
137                 up "$@"
138                 ;;
139         down)
140                 shift;
141                 down "$@"
142                 ;;
143         deconfig)
144                 ifconfig $interface 0.0.0.0
145                 ;;
146         renew|bound)
147                 renew_bound "$@";
148                 ;;
149         *)
150                 error "Usage: $0 <up|down>"
151                 exit 1
152                 ;;
153 esac
154
155 exit
156 # vi: ts=4 noexpandtab syntax=sh