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
=== modified file 'conf/busybox.config'
--- conf/busybox.config 2013-11-25 20:17:08 +0000
+++ conf/busybox.config 2014-08-01 19:42:44 +0000
@@ -781,7 +781,7 @@
781CONFIG_FEATURE_IFUPDOWN_IPV4=y781CONFIG_FEATURE_IFUPDOWN_IPV4=y
782CONFIG_FEATURE_IFUPDOWN_IPV6=y782CONFIG_FEATURE_IFUPDOWN_IPV6=y
783CONFIG_FEATURE_IFUPDOWN_MAPPING=y783CONFIG_FEATURE_IFUPDOWN_MAPPING=y
784# CONFIG_FEATURE_IFUPDOWN_EXTERNAL_DHCP is not set784CONFIG_FEATURE_IFUPDOWN_EXTERNAL_DHCP=y
785CONFIG_INETD=y785CONFIG_INETD=y
786CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_ECHO=y786CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_ECHO=y
787CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD=y787CONFIG_FEATURE_INETD_SUPPORT_BUILTIN_DISCARD=y
788788
=== added file 'patches-buildroot/ifupdown-cirros.patch'
--- patches-buildroot/ifupdown-cirros.patch 1970-01-01 00:00:00 +0000
+++ patches-buildroot/ifupdown-cirros.patch 2014-08-01 19:42:44 +0000
@@ -0,0 +1,21 @@
1--- a/package/busybox/1.20.1/busybox-ifupdown-cirros.patch 1970-01-01 01:00:00.000000000 +0100
2+++ b/package/busybox/1.20.1/busybox-ifupdown-cirros.patch 2014-07-30 11:49:02.213528074 +0200
3@@ -0,0 +1,17 @@
4+Busybox tries several dhcpclients in a specific order, if one is found as
5+executable it gets the honor of asking for an address. By adding cirros as the
6+first option we make sure the /sbin/ifupdown script is used.
7+This depends heavily on CONFIG_FEATURE_IFUPDOWN_EXTERNAL_DHCP=y.
8+
9+--- a/networking/ifupdown.c 2014-07-30 11:38:03.708210386 +0200
10++++ b/networking/ifupdown.c 2014-07-30 11:38:24.106941433 +0200
11+@@ -521,6 +521,10 @@
12+ };
13+
14+ static const struct dhcp_client_t ext_dhcp_clients[] = {
15++ { "cirros-dhcpc",
16++ "cirros-dhcpc up %iface%",
17++ "cirros-dhcpc down %iface%",
18++ },
19+ { "dhcpcd",
20+ "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %client%]][[ -l %leasetime%]] %iface%",
21+ "dhcpcd -k %iface%",
022
=== modified file 'patches-buildroot/series'
--- patches-buildroot/series 2012-06-08 02:31:13 +0000
+++ patches-buildroot/series 2014-08-01 19:42:44 +0000
@@ -1,1 +1,2 @@
1sudo-install-sudoers-so.patch1sudo-install-sudoers-so.patch
2ifupdown-cirros.patch
23
=== added file 'src/etc/default/udhcpc'
--- src/etc/default/udhcpc 1970-01-01 00:00:00 +0000
+++ src/etc/default/udhcpc 2014-08-01 19:42:44 +0000
@@ -0,0 +1,3 @@
1OPTIONS="staticroutes mtu"
2TIMEOUT="60"
3MTU="1500"
04
=== added file 'src/sbin/cirros-dhcpc'
--- src/sbin/cirros-dhcpc 1970-01-01 00:00:00 +0000
+++ src/sbin/cirros-dhcpc 2014-08-01 19:42:44 +0000
@@ -0,0 +1,146 @@
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. ${CIRROS_SHLIB:=/lib/cirros/shlib} ||
6 { echo "failed to read ${CIRROS_SHLIB}" 1>&2; exit 1; }
7
8set -f
9
10OPTS_FILE="/etc/default/udhcpc"
11RESOLV_CONF="/etc/resolv.conf"
12
13apply_static_routes() {
14 # routes are pairs of network and gateway
15 # 169.254.169.254/32 10.65.0.128
16 local net="" router="" err=0
17 while [ $# -ne 0 ]; do
18 net="$1"
19 router="$2"
20 [ -n "$net" ] || continue
21 debug 1 "adding net $net with router $router"
22 route add -net "$net" gw "$router" || {
23 error "WARN: failed: route add -net \"$net\" gw \"$router\""
24 err=$(($err+1));
25 }
26 shift 2 || {
27 error "apply_static_routes: failed shift 2. odd number of args?"
28 return 1;
29 }
30 done
31 return $err
32}
33
34
35readconfig() {
36 [ ! -f "$OPTS_FILE" ] || . "$OPTS_FILE" ||
37 { error "failed to read $OPTS_FILE"; return 1; }
38 # these are expected to be set.
39 OPTIONS=${OPTIONS:-staticroutes mtu}
40 TIMEOUT=${TIMEOUT:-60}
41 MTU=${MTU:-1500}
42}
43
44up() {
45 [ $# -ge 1 ] || { error "$0 up: must provide interface"; return 1; }
46 local iface="$1" opts="" hostname=""
47
48 readconfig || return
49 hostname=$(hostname 2>/dev/null)
50
51 # Gather all options and start udhcpc.
52 for opt in $OPTIONS; do
53 opts="-O $opt $opts"
54 done
55
56 debug 1 "Starting udhcpc on $iface, asking for options {$OPTIONS}"
57 /sbin/udhcpc -p "/var/run/udhcpc.${iface}.pid" -R -n \
58 ${TIMEOUT:+"-T ${TIMEOUT}"} -i "$iface" -s "$0" \
59 $opts ${hostname:+-x "hostname:${hostname}"}
60}
61
62down() {
63 local iface="$1" pidfile="" pid=""
64 [ $# -ge 1 ] || { error "$0 down: must provide interface"; return 1; }
65 pidfile="/var/run/udchpc.$iface.pid"
66 [ -f "$pidfile" ] ||
67 { error "$iface: no pidfile"; return 1; }
68 read pid < "$pidfile" ||
69 { error "failed to read pid from '$pidfile' for '$iface'"; return 1; }
70 kill $pid >/dev/null
71 return
72}
73
74renew_bound() {
75 local flags="" mode="$1" i=""
76 shift;
77 [ -n "$interface" ] ||
78 { error "$0 $mode: 'interface' not provided"; return 1; }
79
80 readconfig || return
81
82 [ -n "$broadcast" ] && flags="$flags broadcast $broadcast"
83 [ -n "$subnet" ] && flags="$flags netmask $subnet"
84 flags="$flags mtu ${mtu:-${MTU}}"
85
86 debug 1 "/sbin/ifconfig $interface $flags"
87 /sbin/ifconfig $interface $flags
88
89 if [ -n "$router" ] ; then
90 local out="" ret=""
91 debug 1 "deleting routers"
92 # cirros: this loop is cirros modification to remove a message
93 # like the following going to the console:
94 # route: SIOCDELRT: No such process
95 while :; do
96 out=$(route del default gw 0.0.0.0 dev $interface 2>&1)
97 ret=$?
98 [ $ret -eq 0 ] && break
99 case "$out" in
100 *SIOCDELRT*[Nn]o\ such\ process*) break;;
101 esac
102 error "deleting routes failed: $out" 1>&2
103 done
104 # end cirros modification
105
106 for i in $router; do
107 route add default gw $i dev $interface
108 done
109 fi
110
111 local msg="configuring $RESOLV_CONF for $interface. domain=$domain"
112 msg="$msg nameservers: $dns"
113 debug 1 "$msg"
114 {
115 [ -n "$domain" ] && echo "search $domain"
116 for i in $dns ; do
117 echo nameserver $i
118 done
119 } > "$RESOLV_CONF"
120
121 apply_static_routes $staticroutes
122}
123
124case "$1" in
125 up)
126 shift
127 up "$@"
128 ;;
129 down)
130 shift;
131 down "$@"
132 ;;
133 deconfig)
134 /sbin/ifconfig $interface 0.0.0.0
135 ;;
136 renew|bound)
137 renew_bound "$@";
138 ;;
139 *)
140 error "Usage: $0 <up|down>"
141 exit 1
142 ;;
143esac
144
145exit
146# vi: ts=4 noexpandtab syntax=sh
0147
=== removed file 'src/usr/share/udhcpc/default.script'
--- src/usr/share/udhcpc/default.script 2013-11-26 01:40:36 +0000
+++ src/usr/share/udhcpc/default.script 1970-01-01 00:00:00 +0000
@@ -1,74 +0,0 @@
1#!/bin/sh
2
3# udhcpc script edited by Tim Riker <Tim@Rikers.org>
4
5set -f
6
7[ -z "$1" ] && echo "Error: should be called from udhcpc" && exit 1
8
9RESOLV_CONF="/etc/resolv.conf"
10[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
11[ -n "$subnet" ] && NETMASK="netmask $subnet"
12
13apply_static_routes() {
14 # routes are pairs of network and gateway
15 # 169.254.169.254/32 10.65.0.128
16 local net="" router="" err=0
17 while [ $# -ne 0 ]; do
18 net="$1"
19 router="$2"
20 [ -n "$net" ] || continue
21 echo "adding net $net with router $router" 1>&2
22 route add -net "$net" gw "$router" || {
23 echo "WARN: failed: route add -net \"$net\" gw \"$router\"" 1>&2;
24 err=$(($err+1));
25 }
26 shift 2 || {
27 echo "apply_static_routes: failed shift 2. odd number of args?" 1>&2;
28 return 1;
29 }
30 done
31 return $err
32}
33
34case "$1" in
35 deconfig)
36 /sbin/ifconfig $interface 0.0.0.0
37 ;;
38
39 renew|bound)
40 /sbin/ifconfig $interface $ip $BROADCAST $NETMASK
41
42 if [ -n "$router" ] ; then
43 echo "deleting routers"
44 # cirros: this loop is cirros modification to remove a message
45 # like the following going to the console:
46 # route: SIOCDELRT: No such process
47 while :; do
48 out=$(route del default gw 0.0.0.0 dev $interface 2>&1)
49 ret=$?
50 [ $ret -eq 0 ] && break
51 case "$out" in
52 *SIOCDELRT*[Nn]o\ such\ process*) break;;
53 esac
54 echo "deleting routes failed: $out" 1>&2
55 done
56 # end cirros modification
57
58 for i in $router ; do
59 route add default gw $i dev $interface
60 done
61 fi
62
63 echo -n > $RESOLV_CONF
64 [ -n "$domain" ] && echo search $domain >> $RESOLV_CONF
65 for i in $dns ; do
66 echo adding dns $i
67 echo nameserver $i >> $RESOLV_CONF
68 done
69
70 apply_static_routes $staticroutes
71 ;;
72esac
73
74exit 0

Subscribers

People subscribed via source and target branches