HOWTO: A lightweight DHCP client for your Plug

Guides written by the community, for the community, and only guides!

HOWTO: A lightweight DHCP client for your Plug

Postby Frex » Mon Apr 04, 2011 2:22 am

This quick HOWTO will show you how to replace the default ArchLinux DHCP clent, dhcpcd, with a lighter alternative: udhcpc. The latter is the DHCP client of choice on several embedded distributions and even non-embedded ones, such as Debian Almighty.

How much can you save? On my setup, almost 70%. After some memory pressure, dhcpcd still had a resident size of ~1150 kiB, vs ~370 for udhcpc.
Is that all? Some people have found udhcpc works better for them. Google a bit if you don't believe me.

Step 0. Prerequites
udhcpc lives in the AUR, the ArchLinux User Repository, you will have to use an AUR helper to install it. If you don't have an AUR helper yet, you'll have to get one. Also you need tools to compile code. Confused? Well ArchLinux has a pretty helpful wiki

Step 1. Get udhcpc
So you're ready to fetch it off the AUR? The package name is udhcp (unfortunately, the server is supplied along with the client, something that may be worth remembering if you're tight on space). Change to your unprivileged user (e.g. su frex), then using your AUR helper of choice, get udhcp. I use packer so:
Code: Select all
EDITOR=nano packer -Sy udhcp


At some point, you will be asked if you want to edit the PKGBUILD file, reply yes. Look for the arch line, and add arm, so that it reads:
Code: Select all
arch=('i686' 'x86_64' 'arm')

Save the file and exit, the AUR helper will go on doing its magic and eventually it will ask your (root) password. After that, you'll have udhcp installed.

Step 2. Setting it up

2a. Editing /etc/rc.d/network
Unfortunately the DHCP client is hard-wired in ArchLinux, so to replace it, you'll have to edit your /etc/rc.d/network file and turn all the references to dhcpcd into proper invocations of udhcpc. If you're in for some fun, you can figure it out yourself: be advised however that failing to properly set up the DHCP client will leave you with no way to access the machine. As a precautionary step, you might want to set up a static IP until you're 100% sure that udhcpc works for you. To do that, just consult the Unofficial ArchLinux Beginners' Guide. Don't remove the exclamation mark from the gateway line, you will use this later as a check.

For all the lazy bums :) I'm attaching an edited version of /etc/rc.d/network which works for me.

network.7z
/etc/rc.d/network
(1.7 KiB) Downloaded 406 times


Keep in mind, if you have made changes to your network file, you may need to do some diff-ing and patch-ing. The changes are very limited: in the header (to include a configuration file), and then the ifup and ifdown functions. If that sits well with you, you can also just open up a couple of editor windows and do it by hand.

2b. Placing the scripts
udhcpc uses a set of scripts to handle a variety of events (first set up, renewal, etc). As far as I can tell, there's a perfectly fine set of scripts packaged with the program (which I suspect were lifted directly off the Debian package), you just have to put them in the right place. So, head to your ssh and:
Code: Select all
mkdir /etc/udhcpc
cp /usr/share/udhcpc/default.* /etc/udhcpc


3. Final Steps
Check again that you've done everything correctly, then check again. If you're like me, it's late and you're a bit sleepy. I sure am now :) Ok, now you can reboot.

After the reboot check the routes: if you have your default gateway set up alright and the Plug can access the Internet, then everything should be ok. You can now revert the changes to /etc/rc.conf so the address is no longer static and relies again entirely on DHCP.

Enjoy your new lightweight client! 8-)

Edits:
01. Fixed some typos and reworded a bit; updated udhcpc RSS figure; removed a useless comment caused by lack of sleep.
Frex
 
Posts: 23
Joined: Thu Mar 31, 2011 7:57 pm

Re: HOWTO: A lightweight DHCP client for your Plug

Postby tramusen » Mon Jul 25, 2011 8:37 pm

Just some additional notes for anyone that hasn't built packages before. (Most all is covered at the above link, but just in case since this is a pretty useful package to have...) :

Make backups of files if you aren't comfortable with what you are doing. Or even if you are.
Code: Select all
 cp /etc/rc.conf /etc/rc.conf.bak

If you blow something up, you can mount your drive on another linux machine and undo the changes.
Code: Select all
rm /etc/rc.conf
cp /etc/rc.conf.bak /etc/rc.conf


For the base installation, you will need to install base-devel, otherwise you won't be able to build AUR packages. Install all packages in this group.
Code: Select all
pacman -S base-devel


I didn't try with packer, I'm more familiar with yaourt, but you can get an AUR helper by doing the following:
Code: Select all
pacman -S yaourt


It was slightly harder previously (on an i686 PC) to get yaourt, so this was a nice surprise.

yaourt should be run as non-root user, but can be used just like pacman.
Code: Select all
yaourt -S udhcp


If you do not have a swap file, you may get a memory allocation error during the package build. (At least I did.) Restarting your device and running the AUR helper immediately seemed to work OK.
tramusen
 
Posts: 29
Joined: Fri Jul 15, 2011 4:23 pm

Re: HOWTO: A lightweight DHCP client for your Plug

Postby morph027 » Sat Feb 23, 2013 9:42 pm

up to date rc-file:

network.udhcpc.tar.gz
modified network rc script
(1.91 KiB) Downloaded 178 times


Code: Select all
--- /etc/rc.d/network   2013-02-23 22:29:12.000000000 +0100
+++ /etc/rc.d/network.org   2013-02-23 21:18:24.000000000 +0100
@@ -3,7 +3,7 @@
 . /etc/rc.conf
 . /etc/rc.d/functions
 
-for s in wireless bonding bridges; do
+for s in wireless bonding bridges dhcpcd; do
    [[ -f /etc/conf.d/$s ]] && . "/etc/conf.d/$s"
 done
 
@@ -27,18 +27,18 @@
       ip addr add $address/${netmask:-24} broadcast ${broadcast:-+} dev $interface || return 1
       [[ $gateway ]] && { ip route add default via $gateway || return 1; }
    else
-      udhcpc -i $interface -h `hostname` -p /var/run/udhcpc.${interface}.pid || return 1
+      dhcpcd $DHCPCD_ARGS $interface || return 1
    fi
 }
 
 network_down() {
    if [[ ! -n $interface ]]; then
-      if [[ -f /var/run/udhcpc-${1}.pid ]]; then
-         kill $(cat /var/run/udhcpc-${1}.pid) || return 1
+      if [[ -f /run/dhcpcd.pid ]]; then
+         dhcpcd -qk || return 1
       fi
    else
-      if [[ ! -n $address && -f /var/run/udhcpc-${1}.pid ]]; then
-         kill $(cat /var/run/udhcpc-${1}.pid) || return 1
+      if [[ ! -n $address && -f /run/dhcpcd-$interface.pid ]]; then
+         dhcpcd -qk $interface || return 1
       else
          ip addr flush dev $interface || return 1
       fi
@@ -67,8 +67,9 @@
 
    if [[ $ifcfg = dhcp ]]; then
       # remove the .pid file if it exists
-      rm -f /var/run/udhcpc-${1}.pid >/dev/null 2>&1
-      udhcpc -i ${1} -h `hostname` -p /var/run/udhcpc.${1}.pid
+      rm -f /run/dhcpcd-${1}.pid >/dev/null 2>&1
+      rm -f /run/dhcpcd-${1}.cache >/dev/null 2>&1
+      dhcpcd $DHCPCD_ARGS ${1}
    else
       ifconfig $ifcfg
    fi
@@ -99,8 +100,8 @@
       return 1
    fi
 
-   if [[ $ifcfg = dhcp && -f /var/run/udhcpc-${1}.pid ]]; then
-      kill $(cat /var/run/udhcpc-${1}.pid)
+   if [[ $ifcfg = dhcp && -f /run/dhcpcd-${1}.pid ]]; then
+      dhcpcd -k ${1} >/dev/null 2>&1
    fi
    # Always bring the interface itself down
    ifconfig ${1} down >/dev/null 2>&1
morph027
 
Posts: 10
Joined: Sun Dec 23, 2012 10:46 pm


Return to Community Guides

Who is online

Users browsing this forum: No registered users and 1 guest