1

I have a large volume of devices that boot from a TFTP server and mount their filesystem as a read-only NFS share. All these devices boot from the same custom raspbian image, which saves us a lot of space as we don't have to store one image per device.

To help manage these devices we are trying to use ddns so we can find a device by its hostname. The problem is that the hostname is read from /etc/hostname and as all devices share their image they all have the same hostname.

I have tried changing this manually on boot by reading the serial from /proc/cpu_info:

  • Both a service and init.d script that starts before the network is up that edits /etc/hostname manually. This did not work as the file is read-only.
  • Symlinking the /etc/hostname file to a ramdisk so that is its writable. For some reason soft symlinks do not work and hard links can't be made between two different partitions.
  • Editing the binary for systemd-hostnamed so that it points to a different file. This caused crash on boot.

I'm hoping that there is a way to set the hostname on a read-only system like this but if not I am open to hearing an alternative to ddns and hostnames.

1 Answers1

0

I added this to the bottom of my /etc/rc.local file, before exit 0:

var=` cat /proc/cpuinfo | grep Serial | cut -d ' ' -f 2 | cut -c 9-`
hostname $var
dhclient

The first line gets the serial number of the device and store it in a variable. The next line uses that variable and sets the hostname. The third line forces the device to refetch its IP address, which updates the DHCP server with the new hostname.

Not ideal because the device complains about not being able to resolve its hostname, because what is in the /etc/hosts file is different to the hostname set above. But it seems to run fine for now.

Important Update

Revisiting this at a later date to present some findings, for some reason calling dhclient could sometimes break an ongoing NFS mount.

The actual solution was to create a service that started before the network service that called the hostname command.