Not meaning to dig up an old thread, but since this is something I had solved on my own before I saw this thread figured would share. This could use some cleaning up. Apologize if I missed something in this post. As going back to document something I did a bit ago is always harder than had I documented it in the first place.
Explanation:
On boot the:
duidsave.service service is set to run after /var/lib/dhcpcd5 is mounted in tmpfs but before dhcpcd5 is ran.
Is runs /usr/local/sbin/duid-save.sh restore
If /usr/local/etc/dhcpcd5/duid file exists then it creates a ln from /var/lib/dhcpcd5/duid to /usr/local/etc/dhcpcd5/duid
Hourly:
duid-save runs by cron to see if duid needs to be save. I tried putting a service to run after dhcpcd5 came up and created a new duid on first boot but has race conditions so cron seemed a good compromise.
"duid-save.sh save" checks if different between /var/lib/dhcp5/duid and /usr/local/etc/dhcpcd5/duid
if their is a different then it makes sure that the /usr/local/etc/dhcpcd5 directory is created and dumps the duid into that directory.
Really this needs to be a oneshot deal, as once duid is saved to nonvolatile storage it doesn't need to run again. But right now it runs and checks if duid on disk is different than in tmp and will archive it away. Might be a better way to do a oneshot trigger to save duid after dhcpcd5 service creates it but I didn't put much effort into it as this worked for my purposes.
/etc/cron.hourly/duid-save
Code: Select all
#!/bin/bash
/usr/local/sbin/duid-save.sh save
/usr/local/sbin/duid-save.sh
Code: Select all
#!/bin/bash
function save {
# check if persistent duid is different that tmpfs one
if ! cmp -s /usr/local/etc/dhcpcd5/duid /var/lib/dhcpcd5/duid ; then
# check if we need to remount rw
if fgrep -q "/dev/root / ext4 ro" /proc/mounts ; then
# mounted ro so mount rw
mount -o remount,rw /
remountit=1
fi
if [ ! -d "/usr/local/etc/dhcpcd5" ]; then
# if directory doesn't exist make it
mkdir /usr/local/etc/dhcpcd5
fi
cp /var/lib/dhcpcd5/duid /usr/local/etc/dhcpcd5/
if [[ $remountit == "1" ]]; then
# if we mounted as RW then put back to RO
mount -o remount,ro /
fi
fi
}
if [ "$1" = "restore" ]; then
if [ -f "/usr/local/etc/dhcpcd5/duid" ]; then
rm /var/lib/dhcpcd5/duid
ln -s /usr/local/etc/dhcpcd5/duid /var/lib/dhcpcd5/duid
fi
fi
if [ "$1" = "save" ]; then
save
fi
if [ "$1" = "new" ]; then
# remove duid from tmpfs
rm /var/lib/dhcpcd5/duid
# restart dhcpcd5
service dhcpcd5 restart
# save new duid
save
fi
/etc/systemd/system/duidsave.service
Code: Select all
[Unit]
Description=Populate dhcpcd4 DUID to tmpfs
After=var-lib-dhcpcd5.mount
Before=dhcpcd5.service
[Service]
Type=simple
ExecStart=/bin/bash -c "/usr/local/sbin/duid-save.sh restore"
[Install]
WantedBy=multi-user.target
Once those files are in place then:
Code: Select all
sudo chmod +x /etc/cron.hourly/duid-save.sh
sudo chmod +x /usr/local/sbin/duid-save.sh
sudo ln -s /etc/systemd/system/duidsave.service /etc/systemd/system/multi-user.target.wants/duidsave.service