3

I have an embedded linux machine which has a read-only root filesystem which is loaded from a UBI/UBIFS.

Every time the device boots it creates a new systemd machine-id which I understand from reading the documentation:

The machine ID is usually generated from a random source during system installation and stays constant for all subsequent boots. Optionally, for stateless systems, it is generated during runtime at early boot if it is found to be empty.

From reading the systemd machine-id-setup.c file, I have an understanding on how it works in the sense that, it will try to generate the machine-id at /etc/machine-id if it doesn't exist, if that fails and the partition is read-only, it will generate it in /run/machine-id and mount it to /etc/machine-id (not a symlink).

There question is, am I able to have a fixed machine-id for a stateless device? The two options I have are:

1) Store machine-id to non-volatile storage (external/eMMC):

  • After booting, check if /etc/machine-id exists
  • If doesn't exist, then continue through systemd services passing the service/mount which generates the machine-id (as normal).
  • New service file: once machine-id has been generated then copy this string to a partition which is not in memory/NAND i.e. eMMC (non-volatile).

Therefore now that the device has been initialised at least once and the machine-id is stored in a non-volatile storage medium; boot sequence is now as follows:

  • Device boots and mounts the non-volatile partition.
  • Checks if non-volatile machine-id exists (same as first step above)
  • This file exists therefore we mount it to /etc/machine-id
  • The service file which handles the population of /etc/machine-id now does not execute because this already exists.

2) Modify the systemd machine-id-setup.c file and change the hardcoded directories where the machine-id is generated, stored and saved. However I would like to avoid this solution as it means I would have to modify the systemd system files.

Could anyone share some insight into this?

Thanks

3 Answers 3

4

Your easiest and best option is probably to pass it as the kernel command line variable systemd.machine_id as per the official documentation. If you want to get really fancy you could have your bootloader generate it based on something truly unique about the SoC that you are running on (like serial number, MAC address, etc).

0

You are asking to mix contradictory concepts: Persistence implies state, but you want the system to be stateless.

Besides the options you mentioned, you could also look into using a "Union" or "Overlay" file system. This would instruct the OS to first look a file on a persistent file system, and then if it's not present, check the stateless file system. This is how LiveCD are implemented, giving the allusion of being able modify files on what's clearly a read-only medium.

0

I've solved this issue by simply remounting rootfs read-write (mount -o remount,rw /) and saving random generated machine ID to /etc/machine-id file.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .