Notes:

systemd relies on a machine id file located on the filesystem here: /etc/machine-id

The unique 32 digit hex machine id in this file is used by various features of systemd, including the journal and the dhcp client.

This machine id can also be passed in via the kernel command line.

At start-up, if this machine id file is missing and there is no kernel command for specifying the machine id, then systemd will generate its own temporary machine-id file under /etc/machine-id. This file is lost between reboots if the filesystem is read-only.

A custom systemd first boot service & script can be created to add the machine id to the kernel boot command file (under /boot/cmdline.txt on RaspberryPi).

First Boot Service

The following is our service, which is only run if the /boot/machine-id file does not exist. If this file does not exist, then the first boot script has not been run, otherwise it has. This service calls our script_first_boot.sh script which generates a new machine ID and adds it to the kernel command line (via cmdline.txt file), and create the /boot/machine-id to indicate this wizard has been run.
[shell title=”File: my-first-boot.service”]
[Unit]
Description=My First Boot Wizard
Conflicts=shutdown.target
ConditionPathExists=!/boot/machine-id
Before=network-pre.target
Wants=network-pre.target

[Service]
Type=oneshot
ExecStart=/usr/bin/script_first_boot.sh

[Install]
WantedBy=multi-user.target
[/shell]

Testing of Service

[shell wraplines=”1″ title=”Testing after first boot”]
root@my-platform1:/boot# cat machine-id
d38d21185c785f6514e42207aa58acb7
root@my-platform1:/boot# cat /etc/machine-id
d38d21185c785f6514e42207aa58acb7
root@my-platform1:/boot# cat cmdline.txt
dwc_otg.lpm_enable=0 rootfstype=ext4 rootwait console=ttyAMA0,115200 systemd.machine_id=d38d21185c785f6514e42207aa58acb7 kgdboc=ttyAMA0,115200 logo.nologo root=${mender_kernel_root}
root@my-platform1:/boot#
[/shell]