Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix reading /etc/machine-id in kernel-install #25388

Merged
merged 2 commits into from Nov 15, 2022

Conversation

schaefi
Copy link
Contributor

@schaefi schaefi commented Nov 15, 2022

The kernel-install script has code to read the contents of /etc/machine-id into the MACHINE_ID variable. Depending on the variable content kernel-install either logs the value or creates a new machine id via systemd-id128 new. In that logic there is one issue. If the file /etc/machine-id exists but is empty, the script tries to call read on an empty file which return with an exit code != 0. As the script code also uses set -e, kernel-install will exit at this point which is unexpected.

The condition of an empty /etc/machine-id file exists for example when building OS images, which should initialize the system id on first boot but not staticly inside of the image. afaik an empty /etc/machine-id is also a common approach to make systemd indicate that it should create a new system id. Because of this, the commit makes sure the reading of /etc/machine-id does not fail in any case such that the handling of the MACHINE_ID variable takes place.

The kernel-install script has code to read the contents of
/etc/machine-id into the MACHINE_ID variable. Depending
on the variable content kernel-install either logs the
value or creates a new machine id via 'systemd-id128 new'.
In that logic there is one issue. If the file /etc/machine-id
exists but is empty, the script tries to call read on an
empty file which return with an exit code != 0. As the
script code also uses 'set -e', kernel-install will exit at
this point which is unexpected.

The condition of an empty /etc/machine-id file exists for
example when building OS images, which should initialize the
system id on first boot but not staticly inside of the image.
afaik an empty /etc/machine-id is also a common approach
to make systemd indicate that it should create a new system
id. Because of this, the commit makes sure the reading of
/etc/machine-id does not fail in any case such that the
handling of the MACHINE_ID variable takes place.
Copy link
Member

@yuwata yuwata left a comment

Choose a reason for hiding this comment

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

LGTM.

@yuwata yuwata added good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed kernel-install labels Nov 15, 2022
Per review comment use test -s to make sure the subsequent
reading of /etc/machine-id is only done if the file exists
and has size > 0
@bluca bluca merged commit 883e7cb into systemd:main Nov 15, 2022
@poettering
Copy link
Member

btw, to be fully correct we should add code that handles the case where /etc/machine-id contains the string "uninitialized", too. As per man page machine-id(5).

@schaefi
Copy link
Contributor Author

schaefi commented Nov 16, 2022

btw, to be fully correct we should add code that handles the case where /etc/machine-id contains the string "uninitialized", too. As per man page machine-id(5).

yes good point. I opened #25407 with a first suggestion how to handle it. Thanks

@keszybz keszybz added needs-stable-backport and removed good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed labels Nov 16, 2022
eworm-de pushed a commit to eworm-de/systemd that referenced this pull request Nov 28, 2022
* Fix reading /etc/machine-id in kernel-install

The kernel-install script has code to read the contents of
/etc/machine-id into the MACHINE_ID variable. Depending
on the variable content kernel-install either logs the
value or creates a new machine id via 'systemd-id128 new'.
In that logic there is one issue. If the file /etc/machine-id
exists but is empty, the script tries to call read on an
empty file which return with an exit code != 0. As the
script code also uses 'set -e', kernel-install will exit at
this point which is unexpected.

The condition of an empty /etc/machine-id file exists for
example when building OS images, which should initialize the
system id on first boot but not staticly inside of the image.
afaik an empty /etc/machine-id is also a common approach
to make systemd indicate that it should create a new system
id. Because of this, the commit makes sure the reading of
/etc/machine-id does not fail in any case such that the
handling of the MACHINE_ID variable takes place.

(cherry picked from commit 883e7cb)
d-hatayama pushed a commit to d-hatayama/systemd that referenced this pull request Feb 15, 2023
* Fix reading /etc/machine-id in kernel-install

The kernel-install script has code to read the contents of
/etc/machine-id into the MACHINE_ID variable. Depending
on the variable content kernel-install either logs the
value or creates a new machine id via 'systemd-id128 new'.
In that logic there is one issue. If the file /etc/machine-id
exists but is empty, the script tries to call read on an
empty file which return with an exit code != 0. As the
script code also uses 'set -e', kernel-install will exit at
this point which is unexpected.

The condition of an empty /etc/machine-id file exists for
example when building OS images, which should initialize the
system id on first boot but not staticly inside of the image.
afaik an empty /etc/machine-id is also a common approach
to make systemd indicate that it should create a new system
id. Because of this, the commit makes sure the reading of
/etc/machine-id does not fail in any case such that the
handling of the MACHINE_ID variable takes place.
valentindavid pushed a commit to valentindavid/systemd that referenced this pull request Aug 8, 2023
The kernel-install script has code to read the contents of
/etc/machine-id into the MACHINE_ID variable. Depending
on the variable content kernel-install either logs the
value or creates a new machine id via 'systemd-id128 new'.
In that logic there is one issue. If the file /etc/machine-id
exists but is empty, the script tries to call read on an
empty file which return with an exit code != 0. As the
script code also uses 'set -e', kernel-install will exit at
this point which is unexpected.

The condition of an empty /etc/machine-id file exists for
example when building OS images, which should initialize the
system id on first boot but not staticly inside of the image.
afaik an empty /etc/machine-id is also a common approach
to make systemd indicate that it should create a new system
id. Because of this, the commit makes sure the reading of
/etc/machine-id does not fail in any case such that the
handling of the MACHINE_ID variable takes place.

(cherry picked from commit 883e7cb)
(cherry picked from commit d34ea41)

Backported for https://bugzilla.redhat.com/show_bug.cgi?id=2148464.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

6 participants