[Dnsmasq-discuss] Reg: Info related to leases file
Lonnie Abelbeck
lists at lonnie.abelbeck.com
Thu Oct 3 16:27:46 BST 2013
On Oct 3, 2013, at 8:05 AM, Nehal J Wani wrote:
>> Yes. I just added contrib/mactable/macscript to the git repo, which is your
>> previous script slightly less elegantly modified by me for this
>> circumstance. I also put back the "make new file then atomically rename"
>> behaviour since that means anything using this file doesn't risk a race
>> condition leading to a half-written file.
>>
>
> I gave a try to the --dhcp-script option of dnsmasq. Following are the findings:
>
> Script used: (a little modified version of
> http://thekelleys.org.uk/gitweb/?p=dnsmasq.git;a=blob_plain;f=contrib/mactable/macscript;hb=HEAD):
>
> #!/bin/bash
>
> STATUS_FILE="/var/lib/libvirt/
> dnsmasq/dnsmasq-ip-mac.status"
>
> # Script for dnsmasq lease-change hook.
> # Maintains the above file with a IP address/MAC address pairs,
> # one lease per line. Works with IPv4 and IPv6 leases, file is
> # atomically updated, so no races for users of the data.
>
> action="$1"
> mac="$2" # IPv4
> ip="$3"
>
> expirytime="$DNSMASQ_LEASE_EXPIRES"
> hostname="$DNSMASQ_SUPPLIED_HOSTNAME"
> clientid="$DNSMASQ_CLIENT_ID"
>
> # ensure it always exists.
>
> if [ ! -f "$STATUS_FILE" ]; then
> touch "$STATUS_FILE"
> fi
>
> if [ -n "$DNSMASQ_IAID" ]; then
> mac="$DNSMASQ_MAC" # IPv6
> clientid="$2"
> fi
>
> # worry about an add or old action when the MAC address is not known:
> # leave any old one in place in that case.
>
> if [ "$action" = "add" -o "$action" = "old" -o "$action" = "del" ]; then
> if [ -n "$mac" -o "$action" = "del" ]; then
> sed "/^${ip//./\.} / d" "$STATUS_FILE" > "$STATUS_FILE".new
>
> if [ "$action" = "add" -o "$action" = "old" ]; then
> echo "$expirytime $mac $ip $hostname $clientid" >> "$STATUS_FILE".new
> fi
> mv "$STATUS_FILE".new "$STATUS_FILE" # atomic update.
> fi
> fi
-- snip --
> --
> Nehal J Wani
Unrelated to your questions, you broke the script by not keeping the $ip as the first field in the status file. The "$ip" is used as a unique index of sorts.
The simple fix would be to put $ip as the first output of your "echo" command, otherwise some regex work on the "sed" command is required to match $ip in the middle rather than at the beginning.
Lonnie
More information about the Dnsmasq-discuss
mailing list