This series is written by a representative of the latter group, which is comprised mostly of what might be called "productivity users" (perhaps "tinkerly productivity users?"). Though my lack of training precludes me from writing code or improving anyone else's, I can, nonetheless, try and figure out creative ways of utilizing open source programs. And again, because of my lack of expertise, though I may be capable of deploying open source programs in creative ways, my modest technical acumen hinders me from utilizing those programs in what may be the most optimal ways. The open-source character, then, of this series, consists in my presentation to the community of open source users and programmers of my own crude and halting attempts at accomplishing computing tasks, in the hope that those who are more knowledgeable than me can offer advice, alternatives, and corrections. The desired end result is the discovery, through a communal process, of optimal and/or alternate ways of accomplishing the sorts of tasks that I and other open source productivity users need to perform.

Thursday, January 31, 2019

dhcpcd hooks: what are they and why should you care?

I know I didn't know what dhcpcd hooks were and why I should care about them. That is, until I set up a file server that runs headless and which I want to make sounds when certain events, such as the network coming up, occur. This, as may be evident, will help me ascertain whether, in absence of a monitor displaying graphical indicators, the machine booted and came online successfully--a pretty important status for a machine like a file server.

dhcpcd (the final "d" stands for "daemon") is, of course, the utility that runs on a lot of Linux computers in order to get them connected to the network. It polls for an IP from whatever dhcp (dynamic host configuration protocol) server is running on the network, and the IP received gets assigned to the computer's designated network interface. So the "hooks" under discussion essentially latch onto that process and cause some other process(es) to be triggered once dhcpcd accomplishes its task. This, then, could allow me to implement the desired behavior on my headless file server

I had part of the solution in place already, namely the beep program, a utility that allows for the playing of various tones through the pc speaker. Yes, I'm aware that most computer users seem to want only to disable the pc speaker: I, on the other hand, have found it quite useful on my systems.

Having done some on-line research on this matter, I was able to succeed at the task of getting the pc speaker to play a tone once the computer had booted and gotten an IP by using the following steps (geared toward the Void Linux distribution installed on that target system).

I first created a file owned by root and in the root group in /usr/share/dhcpcd/hooks/--(call it, say) 10-beeponIP--with the following content:

if ([ $reason = "BOUND" ] || [ $reason = "RENEW" ]) then
# your script commands here (see below for the command I used)
/usr/bin/beep -f 1000 -r 5 -n -r 5 -l 10
fi


I can't go into many specifics of the bash syntax seen in this file since I understand it rather poorly myself (it was simply lifted from the askubuntu link listed below). But some testing of its claimed efficacy revealed that it would, in fact, result in the behavior I was aiming to enable.

I had to set proper permissions on that file, then symlink it. Doing the former was straightforward (permissions needed are, like the rest of the files in that directory, 444). I ran the following command to create the needed symlink: sudo ln -s /usr/share/dhcpcd/hooks/10-beeponIP /usr/libexec/dhcpcd-hooks.

Having done that, on rebooting the computer, the designated tone plays though the pc speaker, letting me know that the system booted normally and is now on-line. Mission accomplished!

Some links that helped me to better understand and accomplish this task are given below:

https://askubuntu.com/questions/1005653/how-do-i-execute-a-script-after-dhcp-assigns-an-ip-address-on-start-up
https://wiki.voidlinux.eu/Network_Configuration#Starting_wpa_supplicant_through_dhcpcd_hooks
https://man.voidlinux.eu/dhcpcd-run-hooks.8

No comments:

Post a Comment