[systemd-devel] ConditionFirstBoot question

Mantas Mikulėnas grawity at gmail.com
Tue Mar 12 14:59:53 UTC 2024


On Tue, Mar 12, 2024, 15:06 <mattwood2000 at gmail.com> wrote:

> Hi,
>
> I have a system that needs to perform some tasks on first boot.  I have
> this working for the most part but I had some general questions and would
> like some guidance on the proper implementation.
>
> The tasks I need to perform on first boot include changing the hostname,
> formatting some flash and mounting the filesystem, updating some config
> files, etc.
>

If this is supposed to prepare the root filesystem, then the initramfs
might be a good place as it always runs before anything in the rootfs does.

(On the other hand, I've seen designs where the first boot goes to a
completely different target and only after it's done it either reboots or
switches to multi-user.target.)


> I found that if I put these tasks sequentially into single script
> /etc/hostname is updated and the hostname command returns the updated
> value, the hostname shown at login is still the old value.
>
> Now if I split tasks into individual services and order them with Before
> and After the hostname is correct at login.
>

If you mean the banner shown *above* the "login:" prompt – that's shown by
agetty, which is getty at tty1.service. It doesn't look at /etc/hostname, it
looks at the kernel parameter that's set from /etc/hostname during early
boot (before any services).

So you need to update the kernel hostname using the `hostname` command, and
you probably want to order your service before getty at tty1 (maybe before
systemd-user-session.service).


> I suspect this is because of systemd's parallelization and the monolithic
> script will not finish before the service or target that pulls in
> /etc/hostname, is that correct?
>

/etc/hostname is read *very* early – if I remember correctly, that's done
by systemd itself before it starts any services at all. The rest of the
system doesn't use that file but looks only at the kernel hostname.

If you're changing it using the `hostname` command, then it mostly doesn't
matter, as you're updating the same kernel parameter as systemd does.


> It is not clear to me what specific target or service my service needs to
> be set Before so that the script can finish.
>
> It also occurs to me that perhaps it makes more sense to split the tasks
> out so that they can run in parallel to optimize the boot, would this be
> the best practice?
>
> Thanks, Matt.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/systemd-devel/attachments/20240312/079adf55/attachment.htm>


More information about the systemd-devel mailing list