Merge lp:~harmw/cirros/udhcpc-wrapper into lp:cirros

Proposed by Harm Weites
Status: Merged
Merged at revision: 310
Proposed branch: lp:~harmw/cirros/udhcpc-wrapper
Merge into: lp:cirros
Diff against target: 283 lines (+172/-75)
6 files modified
conf/busybox.config (+1/-1)
patches-buildroot/ifupdown-cirros.patch (+21/-0)
patches-buildroot/series (+1/-0)
src/etc/default/udhcpc (+3/-0)
src/sbin/cirros-dhcpc (+146/-0)
src/usr/share/udhcpc/default.script (+0/-74)
To merge this branch: bzr merge lp:~harmw/cirros/udhcpc-wrapper
Reviewer Review Type Date Requested Status
Scott Moser Pending
Review via email: mp+229114@code.launchpad.net

Description of the change

Add a wrapper for handling dhcp.

This way there is no need to rebuild busybox for having it ask for more options if we'd want that in the future (hostname?), plus we gain flexibility in having one central script that handles dhcp aspects.

The cirros-dhcpc scripts is called by ifup, it in turn calls udhcpc which uses cirros-dhcpc yet again (instead of default.script found in /usr/share) to apply the configuration.

Besides asking for additional routes, udhcpc will also ask for an mtu to configure.

To post a comment you must log in.
lp:~harmw/cirros/udhcpc-wrapper updated
320. By Harm Weites

change: Remove DISABLED since this is better done through etc/networks.

321. By Harm Weites

fix: Have busybox call cirros-dhcpc with the interface to use as
argument, since that will be passed on from ifup/ifdown.

322. By Harm Weites

fix: Explicitly create the pid file, as is done in ifupdown.c in bb.

323. By Harm Weites

fix: Send the hostname as a proper option to the dhcp server, just like
in ifupdown.c. The clientid (-c) was being sent as well, though that
isn't a supported option (-x) anymore.

324. By Harm Weites

fix: Oops, udhcpc will not supply $2 to --script, so don't exit if $2
is missing in every case. Only if it's missing from up or down.

325. By Harm Weites

change: Renamed $INTERFACE to $IFACE to make it easier to distinguish
from $interface (which is lowercase, but still).

326. By Harm Weites

change: Since $2 is assigned to $IFACE at this stage, just use that.

327. By Harm Weites

merge: Pull in some changes from smoser. Most notably we now use the
shlib to take care of some common stuff and the actions in cirros-dhcpc
are moved to separate functions.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'conf/busybox.config'
2--- conf/busybox.config 2013-11-25 20:17:08 +0000
3+++ conf/busybox.config 2014-08-01 19:42:44 +0000
4@@ -781,7 +781,7 @@
5 CONFIG_FEATURE_IFUPDOWN_IPV4=y
6 CONFIG_FEATURE_IFUPDOWN_IPV6=y
7 CONFIG_FEATURE_IFUPDOWN_MAPPING=y
8-# CONFIG_FEATURE_IFUPDOWN_EXTERNAL_DHCP is not set
9+CONFIG_FEATURE_IFUPDOWN_EXTERNAL_DHCP=y
10 CONFIG_INETD=y
11 CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_ECHO=y
12 CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD=y
13
14=== added file 'patches-buildroot/ifupdown-cirros.patch'
15--- patches-buildroot/ifupdown-cirros.patch 1970-01-01 00:00:00 +0000
16+++ patches-buildroot/ifupdown-cirros.patch 2014-08-01 19:42:44 +0000
17@@ -0,0 +1,21 @@
18+--- a/package/busybox/1.20.1/busybox-ifupdown-cirros.patch 1970-01-01 01:00:00.000000000 +0100
19++++ b/package/busybox/1.20.1/busybox-ifupdown-cirros.patch 2014-07-30 11:49:02.213528074 +0200
20+@@ -0,0 +1,17 @@
21++Busybox tries several dhcpclients in a specific order, if one is found as
22++executable it gets the honor of asking for an address. By adding cirros as the
23++first option we make sure the /sbin/ifupdown script is used.
24++This depends heavily on CONFIG_FEATURE_IFUPDOWN_EXTERNAL_DHCP=y.
25++
26++--- a/networking/ifupdown.c 2014-07-30 11:38:03.708210386 +0200
27+++++ b/networking/ifupdown.c 2014-07-30 11:38:24.106941433 +0200
28++@@ -521,6 +521,10 @@
29++ };
30++
31++ static const struct dhcp_client_t ext_dhcp_clients[] = {
32+++ { "cirros-dhcpc",
33+++ "cirros-dhcpc up %iface%",
34+++ "cirros-dhcpc down %iface%",
35+++ },
36++ { "dhcpcd",
37++ "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %client%]][[ -l %leasetime%]] %iface%",
38++ "dhcpcd -k %iface%",
39
40=== modified file 'patches-buildroot/series'
41--- patches-buildroot/series 2012-06-08 02:31:13 +0000
42+++ patches-buildroot/series 2014-08-01 19:42:44 +0000
43@@ -1,1 +1,2 @@
44 sudo-install-sudoers-so.patch
45+ifupdown-cirros.patch
46
47=== added file 'src/etc/default/udhcpc'
48--- src/etc/default/udhcpc 1970-01-01 00:00:00 +0000
49+++ src/etc/default/udhcpc 2014-08-01 19:42:44 +0000
50@@ -0,0 +1,3 @@
51+OPTIONS="staticroutes mtu"
52+TIMEOUT="60"
53+MTU="1500"
54
55=== added file 'src/sbin/cirros-dhcpc'
56--- src/sbin/cirros-dhcpc 1970-01-01 00:00:00 +0000
57+++ src/sbin/cirros-dhcpc 2014-08-01 19:42:44 +0000
58@@ -0,0 +1,146 @@
59+#!/bin/sh
60+# A simple wrapper around udhcpc so that we are able to dynamically specify
61+# which options it should ask for.
62+
63+. ${CIRROS_SHLIB:=/lib/cirros/shlib} ||
64+ { echo "failed to read ${CIRROS_SHLIB}" 1>&2; exit 1; }
65+
66+set -f
67+
68+OPTS_FILE="/etc/default/udhcpc"
69+RESOLV_CONF="/etc/resolv.conf"
70+
71+apply_static_routes() {
72+ # routes are pairs of network and gateway
73+ # 169.254.169.254/32 10.65.0.128
74+ local net="" router="" err=0
75+ while [ $# -ne 0 ]; do
76+ net="$1"
77+ router="$2"
78+ [ -n "$net" ] || continue
79+ debug 1 "adding net $net with router $router"
80+ route add -net "$net" gw "$router" || {
81+ error "WARN: failed: route add -net \"$net\" gw \"$router\""
82+ err=$(($err+1));
83+ }
84+ shift 2 || {
85+ error "apply_static_routes: failed shift 2. odd number of args?"
86+ return 1;
87+ }
88+ done
89+ return $err
90+}
91+
92+
93+readconfig() {
94+ [ ! -f "$OPTS_FILE" ] || . "$OPTS_FILE" ||
95+ { error "failed to read $OPTS_FILE"; return 1; }
96+ # these are expected to be set.
97+ OPTIONS=${OPTIONS:-staticroutes mtu}
98+ TIMEOUT=${TIMEOUT:-60}
99+ MTU=${MTU:-1500}
100+}
101+
102+up() {
103+ [ $# -ge 1 ] || { error "$0 up: must provide interface"; return 1; }
104+ local iface="$1" opts="" hostname=""
105+
106+ readconfig || return
107+ hostname=$(hostname 2>/dev/null)
108+
109+ # Gather all options and start udhcpc.
110+ for opt in $OPTIONS; do
111+ opts="-O $opt $opts"
112+ done
113+
114+ debug 1 "Starting udhcpc on $iface, asking for options {$OPTIONS}"
115+ /sbin/udhcpc -p "/var/run/udhcpc.${iface}.pid" -R -n \
116+ ${TIMEOUT:+"-T ${TIMEOUT}"} -i "$iface" -s "$0" \
117+ $opts ${hostname:+-x "hostname:${hostname}"}
118+}
119+
120+down() {
121+ local iface="$1" pidfile="" pid=""
122+ [ $# -ge 1 ] || { error "$0 down: must provide interface"; return 1; }
123+ pidfile="/var/run/udchpc.$iface.pid"
124+ [ -f "$pidfile" ] ||
125+ { error "$iface: no pidfile"; return 1; }
126+ read pid < "$pidfile" ||
127+ { error "failed to read pid from '$pidfile' for '$iface'"; return 1; }
128+ kill $pid >/dev/null
129+ return
130+}
131+
132+renew_bound() {
133+ local flags="" mode="$1" i=""
134+ shift;
135+ [ -n "$interface" ] ||
136+ { error "$0 $mode: 'interface' not provided"; return 1; }
137+
138+ readconfig || return
139+
140+ [ -n "$broadcast" ] && flags="$flags broadcast $broadcast"
141+ [ -n "$subnet" ] && flags="$flags netmask $subnet"
142+ flags="$flags mtu ${mtu:-${MTU}}"
143+
144+ debug 1 "/sbin/ifconfig $interface $flags"
145+ /sbin/ifconfig $interface $flags
146+
147+ if [ -n "$router" ] ; then
148+ local out="" ret=""
149+ debug 1 "deleting routers"
150+ # cirros: this loop is cirros modification to remove a message
151+ # like the following going to the console:
152+ # route: SIOCDELRT: No such process
153+ while :; do
154+ out=$(route del default gw 0.0.0.0 dev $interface 2>&1)
155+ ret=$?
156+ [ $ret -eq 0 ] && break
157+ case "$out" in
158+ *SIOCDELRT*[Nn]o\ such\ process*) break;;
159+ esac
160+ error "deleting routes failed: $out" 1>&2
161+ done
162+ # end cirros modification
163+
164+ for i in $router; do
165+ route add default gw $i dev $interface
166+ done
167+ fi
168+
169+ local msg="configuring $RESOLV_CONF for $interface. domain=$domain"
170+ msg="$msg nameservers: $dns"
171+ debug 1 "$msg"
172+ {
173+ [ -n "$domain" ] && echo "search $domain"
174+ for i in $dns ; do
175+ echo nameserver $i
176+ done
177+ } > "$RESOLV_CONF"
178+
179+ apply_static_routes $staticroutes
180+}
181+
182+case "$1" in
183+ up)
184+ shift
185+ up "$@"
186+ ;;
187+ down)
188+ shift;
189+ down "$@"
190+ ;;
191+ deconfig)
192+ /sbin/ifconfig $interface 0.0.0.0
193+ ;;
194+ renew|bound)
195+ renew_bound "$@";
196+ ;;
197+ *)
198+ error "Usage: $0 <up|down>"
199+ exit 1
200+ ;;
201+esac
202+
203+exit
204+# vi: ts=4 noexpandtab syntax=sh
205
206=== removed file 'src/usr/share/udhcpc/default.script'
207--- src/usr/share/udhcpc/default.script 2013-11-26 01:40:36 +0000
208+++ src/usr/share/udhcpc/default.script 1970-01-01 00:00:00 +0000
209@@ -1,74 +0,0 @@
210-#!/bin/sh
211-
212-# udhcpc script edited by Tim Riker <Tim@Rikers.org>
213-
214-set -f
215-
216-[ -z "$1" ] && echo "Error: should be called from udhcpc" && exit 1
217-
218-RESOLV_CONF="/etc/resolv.conf"
219-[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
220-[ -n "$subnet" ] && NETMASK="netmask $subnet"
221-
222-apply_static_routes() {
223- # routes are pairs of network and gateway
224- # 169.254.169.254/32 10.65.0.128
225- local net="" router="" err=0
226- while [ $# -ne 0 ]; do
227- net="$1"
228- router="$2"
229- [ -n "$net" ] || continue
230- echo "adding net $net with router $router" 1>&2
231- route add -net "$net" gw "$router" || {
232- echo "WARN: failed: route add -net \"$net\" gw \"$router\"" 1>&2;
233- err=$(($err+1));
234- }
235- shift 2 || {
236- echo "apply_static_routes: failed shift 2. odd number of args?" 1>&2;
237- return 1;
238- }
239- done
240- return $err
241-}
242-
243-case "$1" in
244- deconfig)
245- /sbin/ifconfig $interface 0.0.0.0
246- ;;
247-
248- renew|bound)
249- /sbin/ifconfig $interface $ip $BROADCAST $NETMASK
250-
251- if [ -n "$router" ] ; then
252- echo "deleting routers"
253- # cirros: this loop is cirros modification to remove a message
254- # like the following going to the console:
255- # route: SIOCDELRT: No such process
256- while :; do
257- out=$(route del default gw 0.0.0.0 dev $interface 2>&1)
258- ret=$?
259- [ $ret -eq 0 ] && break
260- case "$out" in
261- *SIOCDELRT*[Nn]o\ such\ process*) break;;
262- esac
263- echo "deleting routes failed: $out" 1>&2
264- done
265- # end cirros modification
266-
267- for i in $router ; do
268- route add default gw $i dev $interface
269- done
270- fi
271-
272- echo -n > $RESOLV_CONF
273- [ -n "$domain" ] && echo search $domain >> $RESOLV_CONF
274- for i in $dns ; do
275- echo adding dns $i
276- echo nameserver $i >> $RESOLV_CONF
277- done
278-
279- apply_static_routes $staticroutes
280- ;;
281-esac
282-
283-exit 0

Subscribers

People subscribed via source and target branches