Set machine name after A B update

What is the best way to automatically set the machine hostname after a full A B update? We need it to be set to the same as our serial number which is now kept in /data/device_serial. The SSID needs to be set to that as well.

Thank you :pray:

Please forgive me if I’m wrong but, this doesn’t sound like a mender-specific thing, but more of a Linux thing and will vary depending on your distro and system service manager of choice. :slight_smile:

For example, if using systemd you could add your own service unit file that runs on boot to run a shell script that you have written that reads in /data/device_serial, checks and sets the hostname on boot if required. This could involve changing the entry in /etc/hostname and /etc/hosts as part of the script, or you can use ‘hostnamectl’ to set the hostname rather than changing /etc/hostname manually. But again it will depend on your distro of choice.

With respects to the SSID, it sounds like you are running a wireless hotspot on the device, again i don’t know what daemon you are using for that, but if you are using something like hostapd then the ‘ssid’ entry is set in /etc/hostapd.conf

You are absolutely right, not a direct Mender thing but I read about the Mender state scripts and was wondering if people use them for this purpose.

I guess a systemd that runs on every boot will work just fine. I started testing this script to set the hostname

NEWNAME=$(cat /data/device_serial)
sudo sed -ie "s/127\.0\.0\.1.*$/127.0.0.1\tlocalhost\t$NEWNAME/g" /etc/hosts
sudo sed -ie "s/127\.0\.1\.1.*$/127.0.1.1\t$NEWNAME.localdomain\t$NEWNAME/g" /etc/hosts
sudo hostnamectl set-hostname $NEWNAME

and this updates the hostname immediately after running (without reboot), even though the command prompt displays still the old name until reboot. But that’s ok.

Correct, for the wireless hotspot I use hostapd and I am still looking for a way to update and reload the /etc/hostapd.conf configuration without a system reboot. I might have to stop, wait, change and restart that service and add a check if /data/device_serial != ssid so I don’t add unnecessary boot delays for future boots. I feel like am getting close here :slight_smile: . Thanks for your help!

Just following up with my final solution here. I now have systemd starting this script on boot. I could not get hostapd to reload the hostapt.conf reliably, so I decided to reboot. That however threw the mender update process off and it reverted back. I then removed the reboot in my script and the mender update worked. To my surprise the SSID was also set correctly without my reboot, so maybe mender boots the new partition a couple times? Anyways, here is my script to set hostname and SSID in case it helps someone else:

NEWNAME=$(cat /data/device_serial)
if [ "$HOSTNAME" = "$NEWNAME" ]; then
       echo "HOSTNAME is already device_serial"
elif  ["$NEWNAME" = ""]; then
       echo "HOSTNAME unchanged. No serial found in device_serial"
else
       echo "HOSTNAME and SSID will be set to device_serial"
       sudo sed -ie "s/127\.0\.0\.1.*$/127.0.0.1\tlocalhost\t$NEWNAME/g" /etc/hosts
       sudo sed -ie "s/127\.0\.1\.1.*$/127.0.1.1\t$NEWNAME.localdomain\t$NEWNAME/g" /etc/hosts
       sudo hostnamectl set-hostname $NEWNAME
       sudo sed -ie "s/USE_GENERATED_HOSTAPD=.*$/USE_GENERATED_HOSTAPD=no/g" /etc/default/bb-wl18xx
       sudo sed -ie "s/^ssid=.*$/ssid=$NEWNAME/g" /etc/hostapd.conf
       # sudo reboot  - NO REBOOT HERE IT THROWS MENDER UPDATE OFF. REBOOT HAPPENS VIA MENDER
fi