You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ConditionFirstBoot=true does not fire if /etc/machine-id is present but empty #8268
In case of bug report: Expected behaviour you didn't see
Units with ConditionFirstBoot=true should be started when /etc/machine-id exists but is empty.
systemd currently decides that it is in a state of first boot (which will trigger ConditionFirstBoot) only if /etc/machine-id is not present. Including the case where /etc/machine-id is present but empty would more closely match the options given for the generation of a machine-id on a first boot, i.e. /etc/machine-id not present with /etc writable, or /etc/machine-id present but empty, in which case a machine-id is generated and committed later. systemd-tmpfiles also considers an empty /etc/machine-id to equate an unpopulated /etc since this commit. Finally, it would allow for setups where the bootloader boots into the system with read-only mounts (later remounted read-write by systemd's remount filesystems service) including /etc and a service that starts on first boot only (using ConditionFirstBoot) is needed for setup purposes using systemd's builtin facilities.
In case of bug report: Unexpected behaviour you saw
Units with ConditionFirstBoot=true weren't started
In case of bug report: Steps to reproduce the problem
Boot a systemd distribution with /etc/machine-id present but empty and services with ConditionFirstBoot=true.
Reacted by Louis Bouchard, Felix Dreissig and Valentin Geffroy
This will break systemd-firstboot.service. The current mechanism is (also read machine-id(5) for details):
Empty /etc means no machine-id, ConditionFirstBoot is true and firstboot wizard is fired for setup.
Empty /etc/machine-id, which means if you are rolling images, you can drop an empty /etc/machine-id and it will be populated on the first boot but no wizard would be run, if /etc is read-only, a temp file is bind mounted there.
Making ConditionFirstBoot= also do the latter breaks the current way of doing things. systemd-firstboot.service itself relies on it and will otherwise fire with an empty machine-id even when that is not desired.
You could instead use ConditionFileNotEmpty=!/etc/machine-id for your use case (or extend firstboot if you require that). There is also systemd-machine-id-setup to do this while making images.
It would indeed mean that systemd-firstboot would run when /etc/machine-id exist but is empty, but I don't see how that's a problem ?
From the docs and sources, I understand that "this is the first boot" really means "there is no machine-id here yet". What I'm asking is "/etc/machine-id exists and is empty" to also mean "there is no machine-id", as "/etc/machine-id isn't present" does. To me, those statements are equivalent.
They are different, because ConditionFirstBoot= considers them differently. There is a usecase where you don't want the interactive firstboot program to run and want the machine-id to be populated without it, which is what empty machine-id does (while deploying images on scale). If you want firstboot to run even when the machine-id is empty, just add a drop-in as follows for systemd-firstboot.service
I know they are considered differently, and this is precisely what I am objecting. Why would you not want the case where /etc/machine-id is empty to be considered a first boot ? I am deploying server images and what I need is the first boot of a server from these images to actually be considered a first boot.
I would argue that it makes more sense to opt-in to not having systemd-firstboot run on an actual first boot by disabling systemd-firstboot itself in the image.
Also, ConditionFileNotEmpty=!/etc/machine-id won't work in this case since one of the first thingssystemd does is setting up /etc/machine-id, before resolving conditions, which means there will be something in it.
I would rather have systemd say it's a first boot, and happily use the condition in my units, or check the flag file, etc.
There is a usecase where you don't want the interactive firstboot program to run and want the machine-id to be populated without it,
Yeah, this is a definite concern; empty machine ID but not wanting firstboot (and presets) is exactly the situation that e.g. current Fedora Atomic Host is in, and I think the "classic" Fedora Cloud Base image. Based on what you're saying we're not alone.
Of course if it had worked this way from the start we'd almost certainly have adapted and been ready, but I know for a fact some of our setup is not ready for the system to be preset from scratch (we write units in /etc not /usr at image time) - which we've fixed as part of getting ready to use ConditionFirstBoot= for Ignition. See e.g. this change.
So...in the end we may need some sort of opt-in behavior here.
Also, ConditionFileNotEmpty=!/etc/machine-id won't work in this case since one of the first things systemd does is setting up /etc/machine-id, before resolving conditions, which means there will be something in it.
Ugh, yeah. How about systemd.image_machine_id=1 kernel cmdline argument (bikeshed on naming here) to mean "treat empty /etc/machine-id as ConditionFirstBoot". Or at least for our case (Fedora CoreOS) we could easily change the initramfs to pass a file in /run or whatever too.
So, currently the protocol implemented by systemd is this:
If /etc/machine-id exists and is populated, we use it and all is good. We'll not assume this to ba a first-boot case. Let's call this the POPULATED state.
If /etc/machine-id is missing entirely, we will populate it if we can. [If we can't (because read-only) we fail.] This case we'll consider a first-boot case. Let's call this the UNPOPULATED state.
If /etc/machine-id exists but it is empty, we'll write a new machine ID into it (case 3a). If this fails (because read-only), we'll over-mount it with a temporary one (case 3b). In the latter case, during boot the systemd-machine-id-commit.service service runs after the file system was changed to writable later during boot, and it will try to atomically write the temporary one to disk, to the file underneath the over-mount (case 3c). This is graceful: if it works it works, if it doesn't we won't fail. Let's call this the EPHEMERAL state (which sometimes can be changed into POPULATED). We do not consider this a first-boot case.
The use-cases the above covers are the following:
a. After a factory reset /etc is entirely empty but for the future persistent. We basically start from zero, but only once. This uses case 2. i.e. we are initially UNPOPULATED, and future boots are POPULATED.
b. You build an immutable image coming up from zero on each boot, with an /etc entirely empty, but on a tmpfs and modifiable during runtime. This uses case 2 as well. Each boot will be UNPOPULATED.
c. You build an entirely immutable image, but with an otherwise configured /etc. Because it is immutable, a new machine ID is generated on each boot that is valid for exactly that one boot. It can't be written to /etc though because that is read-only. This uses case 3b. Each boot will be EPHEMERAL.
d. You build a golden image that is fully set up, and configured, and shall be fully writable during runtime. Consider a distro installer for example, that sets up a traditional distro, by dd'ing or untarring some ready-made image onto disk. This either comes up writable from the beginning (case 3a) or comes up read-only initially, but is mounted writable during boot (case 3c). It will be EPHEMERAL first, and then is converted to POPULATED.
Now, you want to change the protocol so that case 3a, 3b, 3c are thrown away. I think that's a big loss, as this makes usecases c and d impossible, and I think they matter a lot.
What I don't get about your usecase: what's the point of shipping the file at all in your case? I mean, the only reason why an empty but existing file is something we care for is so that we can over-mount it in case we are read-only during early boot. But in that case assuming "first boot" state, and thus doing the whole unit file preset logic is not gonna work anyway, as that stuff is about making changes to /etc. So, let me ask you explicitly: why would you create the file in /etc if you create it empty and if you are not interested in read-only operation?
Ugh, yeah. How about systemd.image_machine_id=1 kernel cmdline argument (bikeshed on naming here) to mean "treat empty /etc/machine-id as ConditionFirstBoot". Or at least for our case (Fedora CoreOS) we could easily change the initramfs to pass a file in /run or whatever too.
But why? Why do you want to create /etc/machine-id empty in your case? Why not just not have it at all? I really don't get it...
As far as I know, no Fedora edition/artifact shipped upstream has ever made use of ConditionFirstBoot=.
It doesn't happen with per-user Anaconda because there the system is configured before rebooting, in fact by this bit in the systemd.spec %post.
And while we also use Anaconda to generate our "golden images" for cloud environments (Fedora Atomic Host, Fedora Cloud Base), we have always been doing an empty machineid (overriding that %post): cloud base kickstart and for FAH the status quo is from this commit until recently we support the ENOENT case.
Now here's another fact: All Fedora systems today have the rootfs handled "traditionally" - mounted read-write via systemd-remount-fs.service.
And combining these two facts, we arrive at the problem that the original poster wrote here:
Finally, it would allow for setups where the bootloader boots into the system with read-only mounts (later remounted read-write by systemd's remount filesystems service) including /etc and a service that starts on first boot only (using ConditionFirstBoot) is needed for setup purposes using systemd's builtin facilities.
Now, as we switch to Fedora CoreOS and adopt Ignition, the model requires us to mount the rootfs writable in the initramfs. And hence avoiding this problem.
But, I agree with the OP that we shouldn't require conflating having the rootfs mounted rw at switchroot time with "no firstboot".
Now, you listed a rather head-spinning variety of use cases. I'm honestly not sure I'd agree every one of those makes sense but let's set that aside for now. It seems clear to me the most conservative path is to add a 4th state for /etc/machine-id that is explicitly asking to opt-in to firstboot, even if / isn't yet writable.
State 4) is exactly like 3 except it begins with: "If /etc/machine-id exists but contains the contents firstboot..."
Finally, it would allow for setups where the bootloader boots into the system with read-only mounts (later remounted read-write by systemd's remount filesystems service) including /etc and a service that starts on first boot only (using ConditionFirstBoot) is needed for setup purposes using systemd's builtin facilities.
This can't work. When the first-boot condition is set two things happen:
before putting together the initial transaction (i.e. really really early on) systemd will do more or less what systemctl preset-all does.
services marked with ConditionFirstBoot= are run
Now, of these two, the first one (which from systemd's PoV is kinda the main one) only works if /etc is writable that early already. This however also means that /etc/machine-id doesn't have to exist, as it can be created just fine.
What you are looking for is apparently a first boot mode where 1 doesn't happen but 2 still does?
What you are looking for is apparently a first boot mode where 1 doesn't happen but 2 still does?
Nope, we want 1 too. I think the implementation path here would be to invoke systemd-remount-fs very early on.
Or honestly, maybe we decide here that the simplest thing is just to change to having the initramfs mount / as rw by default for anyone who wants to make use of ConditionFirstBoot=. I'd probably be fine with that.
EDIT: Or maybe what we should do is change systemd in the initramfs to check before pivoting whether /sysroot/etc/machine-id exists, and if it doesn't, remount / as rw?
Activity
bl33pbl0p commentedon Feb 23, 2018
This will break systemd-firstboot.service. The current mechanism is (also read machine-id(5) for details):
Making ConditionFirstBoot= also do the latter breaks the current way of doing things. systemd-firstboot.service itself relies on it and will otherwise fire with an empty machine-id even when that is not desired.
Empty machine-id is populated at boot anyway, see https://www.freedesktop.org/software/systemd/man/machine-id.html
You could instead use ConditionFileNotEmpty=!/etc/machine-id for your use case (or extend firstboot if you require that). There is also systemd-machine-id-setup to do this while making images.
superseed commentedon Feb 26, 2018
It would indeed mean that systemd-firstboot would run when
/etc/machine-idexist but is empty, but I don't see how that's a problem ?From the docs and sources, I understand that "this is the first boot" really means "there is no machine-id here yet". What I'm asking is "
/etc/machine-idexists and is empty" to also mean "there is no machine-id", as "/etc/machine-idisn't present" does. To me, those statements are equivalent.bl33pbl0p commentedon Feb 26, 2018
They are different, because ConditionFirstBoot= considers them differently. There is a usecase where you don't want the interactive firstboot program to run and want the machine-id to be populated without it, which is what empty machine-id does (while deploying images on scale). If you want firstboot to run even when the machine-id is empty, just add a drop-in as follows for systemd-firstboot.service
You can ship your drop-ins in /usr itself too.
superseed commentedon Mar 15, 2018
I know they are considered differently, and this is precisely what I am objecting. Why would you not want the case where
/etc/machine-idis empty to be considered a first boot ? I am deploying server images and what I need is the first boot of a server from these images to actually be considered a first boot.I would argue that it makes more sense to opt-in to not having
systemd-firstbootrun on an actual first boot by disablingsystemd-firstbootitself in the image.Also,
ConditionFileNotEmpty=!/etc/machine-idwon't work in this case since one of the first thingssystemddoes is setting up/etc/machine-id, before resolving conditions, which means there will be something in it.I would rather have
systemdsay it's a first boot, and happily use the condition in my units, or check the flag file, etc.cgwalters commentedon Aug 2, 2018
Yeah, this is a definite concern; empty machine ID but not wanting firstboot (and presets) is exactly the situation that e.g. current Fedora Atomic Host is in, and I think the "classic" Fedora Cloud Base image. Based on what you're saying we're not alone.
Of course if it had worked this way from the start we'd almost certainly have adapted and been ready, but I know for a fact some of our setup is not ready for the system to be preset from scratch (we write units in
/etcnot/usrat image time) - which we've fixed as part of getting ready to useConditionFirstBoot=for Ignition. See e.g. this change.So...in the end we may need some sort of opt-in behavior here.
Ugh, yeah. How about
systemd.image_machine_id=1kernel cmdline argument (bikeshed on naming here) to mean "treat empty /etc/machine-id asConditionFirstBoot". Or at least for our case (Fedora CoreOS) we could easily change the initramfs to pass a file in/runor whatever too.poettering commentedon Aug 3, 2018
So, currently the protocol implemented by systemd is this:
If /etc/machine-id exists and is populated, we use it and all is good. We'll not assume this to ba a first-boot case. Let's call this the
POPULATEDstate.If /etc/machine-id is missing entirely, we will populate it if we can. [If we can't (because read-only) we fail.] This case we'll consider a first-boot case. Let's call this the
UNPOPULATEDstate.If /etc/machine-id exists but it is empty, we'll write a new machine ID into it (case 3a). If this fails (because read-only), we'll over-mount it with a temporary one (case 3b). In the latter case, during boot the
systemd-machine-id-commit.serviceservice runs after the file system was changed to writable later during boot, and it will try to atomically write the temporary one to disk, to the file underneath the over-mount (case 3c). This is graceful: if it works it works, if it doesn't we won't fail. Let's call this theEPHEMERALstate (which sometimes can be changed into POPULATED). We do not consider this a first-boot case.The use-cases the above covers are the following:
a. After a factory reset /etc is entirely empty but for the future persistent. We basically start from zero, but only once. This uses case 2. i.e. we are initially UNPOPULATED, and future boots are POPULATED.
b. You build an immutable image coming up from zero on each boot, with an /etc entirely empty, but on a tmpfs and modifiable during runtime. This uses case 2 as well. Each boot will be UNPOPULATED.
c. You build an entirely immutable image, but with an otherwise configured /etc. Because it is immutable, a new machine ID is generated on each boot that is valid for exactly that one boot. It can't be written to /etc though because that is read-only. This uses case 3b. Each boot will be EPHEMERAL.
d. You build a golden image that is fully set up, and configured, and shall be fully writable during runtime. Consider a distro installer for example, that sets up a traditional distro, by dd'ing or untarring some ready-made image onto disk. This either comes up writable from the beginning (case 3a) or comes up read-only initially, but is mounted writable during boot (case 3c). It will be EPHEMERAL first, and then is converted to POPULATED.
Now, you want to change the protocol so that case 3a, 3b, 3c are thrown away. I think that's a big loss, as this makes usecases c and d impossible, and I think they matter a lot.
What I don't get about your usecase: what's the point of shipping the file at all in your case? I mean, the only reason why an empty but existing file is something we care for is so that we can over-mount it in case we are read-only during early boot. But in that case assuming "first boot" state, and thus doing the whole unit file preset logic is not gonna work anyway, as that stuff is about making changes to /etc. So, let me ask you explicitly: why would you create the file in /etc if you create it empty and if you are not interested in read-only operation?
poettering commentedon Aug 3, 2018
But why? Why do you want to create /etc/machine-id empty in your case? Why not just not have it at all? I really don't get it...
boucman commentedon Aug 3, 2018
bind mounting a file from a R/W partition on top of it, maybe ?
cgwalters commentedon Aug 3, 2018
As far as I know, no Fedora edition/artifact shipped upstream has ever made use of
ConditionFirstBoot=.It doesn't happen with per-user Anaconda because there the system is configured before rebooting, in fact by this bit in the systemd.spec %post.
And while we also use Anaconda to generate our "golden images" for cloud environments (Fedora Atomic Host, Fedora Cloud Base), we have always been doing an empty machineid (overriding that
%post): cloud base kickstart and for FAH the status quo is from this commit until recently we support the ENOENT case.Now here's another fact: All Fedora systems today have the rootfs handled "traditionally" - mounted read-write via
systemd-remount-fs.service.And combining these two facts, we arrive at the problem that the original poster wrote here:
Now, as we switch to Fedora CoreOS and adopt Ignition, the model requires us to mount the rootfs writable in the initramfs. And hence avoiding this problem.
But, I agree with the OP that we shouldn't require conflating having the rootfs mounted
rwat switchroot time with "no firstboot".cgwalters commentedon Aug 3, 2018
Now, you listed a rather head-spinning variety of use cases. I'm honestly not sure I'd agree every one of those makes sense but let's set that aside for now. It seems clear to me the most conservative path is to add a 4th state for
/etc/machine-idthat is explicitly asking to opt-in to firstboot, even if/isn't yet writable.State
4)is exactly like3except it begins with: "If /etc/machine-id exists but contains the contentsfirstboot..."poettering commentedon Aug 3, 2018
This can't work. When the first-boot condition is set two things happen:
systemctl preset-alldoes.Now, of these two, the first one (which from systemd's PoV is kinda the main one) only works if /etc is writable that early already. This however also means that /etc/machine-id doesn't have to exist, as it can be created just fine.
What you are looking for is apparently a first boot mode where 1 doesn't happen but 2 still does?
cgwalters commentedon Aug 3, 2018
Nope, we want
1too. I think the implementation path here would be to invokesystemd-remount-fsvery early on.Or honestly, maybe we decide here that the simplest thing is just to change to having the initramfs mount
/asrwby default for anyone who wants to make use ofConditionFirstBoot=. I'd probably be fine with that.EDIT: Or maybe what we should do is change systemd in the initramfs to check before pivoting whether
/sysroot/etc/machine-idexists, and if it doesn't, remount/asrw?poettering commentedon Aug 3, 2018
that's a chicken and egg issue: you want to run a service before the the first services shall be run ;-)
22 remaining items
feat: Support this role in container builds
feat: Support this role in container builds
feat: Support this role in container builds
feat: Support this role in container builds
feat: Support this role in container builds
feat: Support this role in container builds
feat: Support this role in container builds
feat: Support this role in container builds
feat: Support this role in container builds
feat: Support this role in container builds
feat: Support this role in container builds
feat: Support this role in container builds
feat: Support this role in container builds
feat: Support this role in container builds
feat: Support this role in container builds