2 # A simple wrapper around udhcpc so that we are able to dynamically specify
3 # which options it should ask for.
5 # note: ifupdown does not allow VERBOSITY through. can be set in OPTS_FILE.
6 VERBOSITY="${VERBOSITY:-0}"
8 . ${CIRROS_SHLIB:=/lib/cirros/shlib} ||
9 { echo "failed to read ${CIRROS_SHLIB}" 1>&2; exit 1; }
13 # ensure PATH has /sbin in it.
14 path_has /sbin || PATH="$PATH:/sbin"
16 OPTS_FILE="/etc/default/udhcpc"
17 RESOLV_CONF="/etc/resolv.conf"
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
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\""
33 error "apply_static_routes: failed shift 2. odd number of args?"
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}
51 [ $# -ge 1 ] || { error "$0 up: must provide interface"; return 1; }
52 local iface="$1" opts="" hostname=""
55 hostname=$(hostname 2>/dev/null)
57 # Gather all options and start udhcpc.
58 for opt in $OPTIONS; do
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}"}
69 local iface="$1" pidfile="" pid=""
70 [ $# -ge 1 ] || { error "$0 down: must provide interface"; return 1; }
71 pidfile="/var/run/udhcpc.$iface.pid"
73 { error "$iface: no pidfile"; return 1; }
74 read pid < "$pidfile" ||
75 { error "failed to read pid from '$pidfile' for '$iface'"; return 1; }
81 local flags="" mode="$1" i=""
83 [ -n "$interface" ] ||
84 { error "$0 $mode: 'interface' not provided"; return 1; }
87 { error "$0 $mode: 'ip' not set in environment"; return 1; }
91 [ -n "$broadcast" ] && flags="${flags}broadcast $broadcast "
92 [ -n "$subnet" ] && flags="${flags}netmask $subnet "
93 flags="${flags}mtu ${mtu:-${MTU}} "
96 debug 1 "ifconfig $interface $ip $flags"
97 ifconfig $interface $ip $flags || {
98 error "$0 $mode: failed: 'ifconfig $interface $ip $flags'";
102 if [ -n "$router" ] ; then
104 debug 2 "deleting routers"
106 out=$(route del default gw 0.0.0.0 dev $interface 2>&1)
108 [ $ret -eq 0 ] && break
110 *SIOCDELRT*[Nn]o\ such\ process*) break;;
112 error "deleting routes failed: $out" 1>&2
116 debug 1 "route add default gw $i dev $interface"
117 route add default gw $i dev $interface
121 local msg="configuring $RESOLV_CONF for $interface. domain=$domain"
122 msg="$msg nameservers: $dns"
125 [ -n "$domain" ] && echo "search $domain"
131 apply_static_routes $staticroutes
144 ifconfig $interface 0.0.0.0
150 error "Usage: $0 <up|down>"
156 # vi: ts=4 noexpandtab syntax=sh