busybox: Add a default udhcpc dispatcher script

The udhcpc binary which currently ships as part of the busybox
derivation will by default search for a dispatcher script at the
location /usr/share/udhcpc/default.script.

This commit includes a working default script with udhcpc and updates
the location where udhcpc searches for this script.

The script was taken the script from the udhcpc package in debian
buster. The only changes from that script is to make it use paths from
the nix store and remove the run-time check for /sbin/resolvconf.
This commit is contained in:
Tethys Svensson 2020-07-07 15:01:49 +02:00
parent 64949ae7c7
commit b98ad8de37
2 changed files with 75 additions and 0 deletions

View file

@ -80,6 +80,9 @@ stdenv.mkDerivation rec {
# Bump from 4KB, much faster I/O
CONFIG_FEATURE_COPYBUF_KB 64
# Set the path for the udhcpc script
CONFIG_UDHCPC_DEFAULT_SCRIPT "$out/share/default.script"
${extraConfig}
CONFIG_CROSS_COMPILER_PREFIX "${stdenv.cc.targetPrefix}"
${libcConfig}
@ -94,6 +97,12 @@ stdenv.mkDerivation rec {
makeFlagsArray+=("CC=${stdenv.cc.targetPrefix}cc -isystem ${musl.dev}/include -B${musl}/lib -L${musl}/lib")
'';
postInstall = ''
mkdir $out/share
substituteAll ${./default.script} $out/share/default.script
chmod +x $out/share/default.script
'';
depsBuildBuild = [ buildPackages.stdenv.cc ];
buildInputs = lib.optionals (enableStatic && !useMusl && stdenv.cc.libc ? static) [ stdenv.cc.libc stdenv.cc.libc.static ];

View file

@ -0,0 +1,66 @@
#!@bash@/bin/sh
# Busybox udhcpc dispatcher script.
# Copyright (C) 2009 by Axel Beckert.
# Copyright (C) 2014 by Michael Tokarev.
#
# Based on the busybox example scripts and the old udhcp source
# package default.* scripts.
RESOLV_CONF="/etc/resolv.conf"
log() {
@out@/bin/logger -t "udhcpc[$PPID]" -p daemon.$1 "$interface: $2"
}
case $1 in
bound|renew)
# Configure new IP address.
# Do it unconditionally even if the address hasn't changed,
# to also set subnet, broadcast, mtu, ...
@out@/bin/ifconfig $interface ${mtu:+mtu $mtu} \
$ip netmask $subnet ${broadcast:+broadcast $broadcast}
# get current ("old") routes (after setting new IP)
crouter=$(@out@/bin/ip -4 route show dev $interface |
@out@/bin/awk '$1 == "default" { print $3; }')
router="${router%% *}" # linux kernel supports only one (default) route
if [ ".$router" != ".$crouter" ]; then
# reset just default routes
@out@/bin/ip -4 route flush exact 0.0.0.0/0 dev $interface
fi
if [ -n "$router" ]; then
# special case for /32 subnets: use onlink keyword
[ ".$subnet" = .255.255.255.255 ] \
&& onlink=onlink || onlink=
@out@/bin/ip -4 route add default via $router dev $interface $onlink
fi
# Update resolver configuration file
[ -n "$domain" ] && R="domain $domain" || R=""
for i in $dns; do
R="$R
nameserver $i"
done
echo "$R" > "$RESOLV_CONF"
log info "$1: IP=$ip/$subnet router=$router domain=\"$domain\" dns=\"$dns\" lease=$lease"
;;
deconfig)
@out@/bin/ip link set $interface up
@out@/bin/ip -4 addr flush dev $interface
@out@/bin/ip -4 route flush dev $interface
log notice "deconfigured"
;;
leasefail | nak)
log err "configuration failed: $1: $message"
;;
*)
echo "$0: Unknown udhcpc command: $1" >&2
exit 1
;;
esac