Skip to content

Improve auto hostname#1045

Merged
ricardosalveti merged 3 commits intofoundriesio:mainfrom
olheureu:improve-auto-hostname
Feb 16, 2023
Merged

Improve auto hostname#1045
ricardosalveti merged 3 commits intofoundriesio:mainfrom
olheureu:improve-auto-hostname

Conversation

@olheureu
Copy link
Copy Markdown
Contributor

3 commits that improves the "lmp-auto-hostname" feature [1]:

  1. Support changing the hostname even if "hostnamectl" is not available, with a fall-back to "/usr/bin/hostname".
  2. Avoid to run the script at every boot, to boot faster, because it is not needed.
  3. Add an option to fetch the serial number from the secure environment with "fiovb", instead of using the CPU serial number.

References:

In our adapted lmp, we have removed "hostnamed" from "systemd", to
boot faster. By removing "hostnamed", the "hostnamectl" is not
available any more, which makes the "lmp-update-hostname.sh"
ineffective, because it uses "hostnamectl" to set the hostname.

It us useful to have a fall-back to change the hostname anyway, even
if the "hostnamectl" utility is absent.

Implemented the fallback: write to "/etc/hostname" directly, and set
the hostname with the "/usr/bin/hostname" command, instead of using
"hostnamectl".

References:
- [1]      "systemd-hostnamed.service, systemd-hostnamed — Daemon to
           control system hostname from programs"
           <https://www.freedesktop.org/software/systemd/man/systemd-hostnamed.service.html#>
- [2]      "Auto Hostname"
           <https://docs.foundries.io/latest/user-guide/lmp-auto-hostname/lmp-auto-hostname.html>

Signed-off-by: Olivier L'Heureux <[email protected]>
Copy link
Copy Markdown
Contributor

@MrCry0 MrCry0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, not tested.

@ricardosalveti
Copy link
Copy Markdown
Member

@olheureu thanks for your contribution, just one minor comment at the service file, but the other two patches are good.

Nice improvements.

@olheureu olheureu force-pushed the improve-auto-hostname branch from fd259b2 to 18688b7 Compare February 14, 2023 16:47
For our lmp-based embedded system, we want to use hostnames containing
serial numbers, we use thus the "Auto Hostname" feature [1]. "Auto
Hostname" installs an "lmp-auto-hostname.service" systemd service that
starts the "lmp-update-hostname.sh" script, which changes the
hostname.

As delivered, the service starts the script at every boot. This is not
needed, because the hostname change should be persistent, at least if
the "/etc/" directory is writable. Running the script at every boot
also slows down the boot, while fast boots is important for us.

Disabled the systemd service after the service has updated the
hostname, so that it runs only once.

References:
- [1]      "Auto Hostname"
           <https://docs.foundries.io/latest/user-guide/lmp-auto-hostname/lmp-auto-hostname.html>

Suggested-by: Ricardo Salveti <[email protected]>
Signed-off-by: Olivier L'Heureux <[email protected]>
The "Auto Hostname" feature [1] can set a hostname that contains the
device serial number. This serial number is fetched from the device
tree, from "/proc/device-tree/serial-number", or
"/sys/devices/soc0/serial_number", if it exists.

Our lmp-based embedded device has its serial number in the "fiovb"
secure environment, where it is safely provisioned. The device tree
contains the CPU serial number, but we prefer to use the device serial
number.

We want to extend the current "Auto Hostname" feature to use the
serial number from the "fiovb" secure environment. We want the "Auto
Hostname" implementation to stay generic: the use of a serial number
from the "fiovb" must stay optional, the default should stay from the
device tree, and the "fiovb" variable name should be a per-MACHINE
model parameter.

Implemented in "lmp-update-hostname.sh": if the "fiovb_printenv"
command exists and if the environment variable named "${FIOVB_VAR}" is
not empty, fetch the serial number from the "fiovb" secure environment
and use it in the host name.

Implemented in "lmp-auto-hostname.service.in": sets the "${FIOVB_VAR}"
environment variable to the content of the
"@@LMP_HOSTNAME_FIOVB_VAR@@" template.

Implemented in "lmp-auto-hostname_0.1.bb": when generating
"lmp-auto-hostname.service" from "lmp-auto-hostname.service.in",
replace the "@@LMP_HOSTNAME_FIOVB_VAR@@" template by the content of
the "${LMP_HOSTNAME_FIOVB_VAR}" Yocto variable, which is empty by
default.

The "${LMP_HOSTNAME_FIOVB_VAR}" Yocto variable could easily be
overloaded in a "lmp-auto-hostname_%.bbappend" recipe, to fetch the
serial number from a chosen "fiovb" variable.

References:
- [1]      "Auto Hostname"
           <https://docs.foundries.io/latest/user-guide/lmp-auto-hostname/lmp-auto-hostname.html>

Signed-off-by: Olivier L'Heureux <[email protected]>
@olheureu olheureu force-pushed the improve-auto-hostname branch from 18688b7 to 5fafff1 Compare February 16, 2023 09:34
Copy link
Copy Markdown
Member

@quaresmajose quaresmajose left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ricardosalveti ricardosalveti merged commit 52bff63 into foundriesio:main Feb 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants