Skip to content

systemd-boot-builder.py: fix crash, clean up code#156798

Closed
m-bdf wants to merge 6 commits intoNixOS:masterfrom
m-bdf:sdboot-builder-cleanup
Closed

systemd-boot-builder.py: fix crash, clean up code#156798
m-bdf wants to merge 6 commits intoNixOS:masterfrom
m-bdf:sdboot-builder-cleanup

Conversation

@m-bdf
Copy link
Contributor

@m-bdf m-bdf commented Jan 26, 2022

Motivation for this change

Fix a crash introduced in e2be5dc and clean up the code to improve coding style consistency, match Python 3 best practices and make the script more readable and easy to maintain overall.

Followed PyLint's advice, which now rates the file 10.00/10 with these reports disabled:

invalid-name,missing-docstring,wrong-import-position,unspecified-encoding
line-too-long,too-many-locals,too-many-branches,too-many-statements

(changes are grouped, so per-commit diffs are a lot clearer than the PR's unified one)

Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 22.05 Release Notes (or backporting 21.11 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
    • (Release notes changes) Ran nixos/doc/manual/md-to-db.sh to update generated release notes
  • Fits CONTRIBUTING.md.

@m-bdf m-bdf requested a review from dasJ as a code owner January 26, 2022 02:24
@github-actions github-actions bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` labels Jan 26, 2022
@m-bdf m-bdf mentioned this pull request Jan 26, 2022
13 tasks
Copy link
Contributor

@edrex edrex left a comment

Choose a reason for hiding this comment

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

I spent ~30 minutes reading through these changes, and no issues jumped out. Overall it's a huge improvement in code quality. The test suite over at nixos/tests/systemd-boot.nix still seems to pass (if I ran it correctly).

@jonringer
Copy link
Contributor

cc better python devs than I. @FRidh @Mic92

@edrex
Copy link
Contributor

edrex commented Feb 12, 2022

also CC @danielfullmer who wrote the tests, @Enzime who authored and @pennae who reviewed #150408

@Enzime
Copy link
Member

Enzime commented Feb 12, 2022

EDIT: My mistake I didn’t see the other commits

Can you write a test case for the specific crash this fixes?

To anyone who wants to review this, I would click on the commits tab and look at them individually

@edrex
Copy link
Contributor

edrex commented Feb 12, 2022

+1 that #155054 (comment) should be reverted before this is merged

@Mic92
Copy link
Member

Mic92 commented Feb 13, 2022

I also did not saw any concerns when reading the code. But maybe address @edrex comment from above.

@lucc
Copy link
Contributor

lucc commented Feb 18, 2022

I also looked through these changes and they are looking good to me.

This should fix #159623.

Can I help to speed this up? (I am currently affected by this and am "motivated" :/ )
@Enzime what kind of test should be written where for this script? The only place where I see it referenced is the nix file next to it and that does only do a static code check with mypy during installation to the nix store:

checkedSystemdBootBuilder = pkgs.runCommand "systemd-boot" {
nativeBuildInputs = [ pkgs.mypy ];
} ''
install -m755 ${systemdBootBuilder} $out
mypy \
--no-implicit-optional \
--disallow-untyped-calls \
--disallow-untyped-defs \
$out
'';

There are some tests for the systemd-boot setting in nixos/tests/systemd-boot.nix but the bug that should be fixed in this PR is in the python script: undefined variable profile in line 230:

gens = get_generations()
for profile in get_profiles():
gens += get_generations(profile)
remove_old_entries(gens)
for gen in gens:
try:
write_entry(*gen, machine_id)
if os.readlink(system_dir(*gen)) == args.default_config:
write_loader_conf(*gen)
except OSError as e:
print("ignoring profile '{}' in the list of boot entries because of the following error:\n{}".format(profile, e), file=sys.stderr)

@edrex
Copy link
Contributor

edrex commented Feb 18, 2022

@lucc AFAIK this doesn't have a fix for #159623, which is a regression from ce8c102. Reverting that change should fix the error reporting, revealing the underlying error (in my case it was ENOSPACE, indicating my boot partition was full). Since this branch hasn't been rebased since that was merged, there would be a merge conflict i guess.

@edrex
Copy link
Contributor

edrex commented Feb 18, 2022

Since ce8c102 has been backported to 21.11, I think that should be reverted in a separate commit before this is merged, and then that commit can be backported. @m-bdf do you want to rebase prepending a revert commit since this seems ready to merge otherwise?

@sternenseemann
Copy link
Member

Resolved the merge conflict and force-pushed. Can confirm this PR solves #15962 for me (i. e. garbled error messages).

I think we should either merge this or revert the regression quickly, as this is a pretty non-obvious pitfall and risks people inadventingly booting into old generations.

@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. labels Feb 21, 2022
@m-bdf
Copy link
Contributor Author

m-bdf commented Feb 22, 2022

Thanks everyone for taking the time to review this PR 💕

@edrex for some reasons I won't explain here, I've been working on a NixOS live USB for a few days already, and trying to clone the nixpkgs repo (unsurprisingly) freezes the entire system, so I haven't been able to revert ce8c102 yet.

In the meantime, @sternenseemann rebased this PR on master, making it mergeable as-is by resolving the conflict in the first commit instead of prepending a proper revert commit. Thus, do you think backporting 7c21a5f (or even this entire PR) would be reasonable?

@edrex
Copy link
Contributor

edrex commented Feb 24, 2022

sorry slow response, some time AFK

do you think backporting 7c21a5f (or even this entire PR) would be reasonable?

It seems like just backporting the regression fix would have the smallest impact, but I'm new here so not sure what the guidelines are for backporting. I feel like a fix should definitely be backported since the regression was.

Also, didn't mean to steamroll @Enzime's request:

Can you write a test case for the specific crash this fixes?

At least characterizing the crash seems like good process.

@Mic92
Copy link
Member

Mic92 commented Feb 25, 2022

I also think we should only backport a fix and not the whole refactoring part of this pr.

Copy link
Member

@FRidh FRidh left a comment

Choose a reason for hiding this comment

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

Paths shouldn't be represented anymore as strings but as pathlib.Path objects.

from shutil import copyfile
from ctypes import CDLL

syncfs = CDLL("libc.so.6").syncfs
Copy link
Member

Choose a reason for hiding this comment

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

how is libc.so.6 found? Should we hardcode a path?

import errno
import subprocess

from argparse import ArgumentParser
Copy link
Member

Choose a reason for hiding this comment

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

please avoid from imports so it is clear directly in code where something comes from.



def profile_path(profile: Optional[str]) -> str:
return "/nix/var/nix/profiles/" + (f"system-profiles/{profile}" if profile else "system")
Copy link
Member

Choose a reason for hiding this comment

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

Avoid string concatenation when the strings represent paths. Instead, use pathlib and the / operator.


def system_dir(gen: SystemIdentifier) -> str:
generation_dir = f"{profile_path(gen.profile)}-{gen.generation}-link"
return f"{generation_dir}/specialisation/{gen.specialisation}" if gen.specialisation else generation_dir
Copy link
Member

Choose a reason for hiding this comment

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

For clarity I would

Suggested change
return f"{generation_dir}/specialisation/{gen.specialisation}" if gen.specialisation else generation_dir
specialisation_dir = f"{generation_dir}/specialisation/{gen.specialisation}"
return specialisation_dir if gen.specialisation else generation_dir

Also, pathlib.

@m-bdf
Copy link
Contributor Author

m-bdf commented Feb 28, 2022

Thanks for your review @FRidh!

I'm working on the changes you've suggested, using pathlib.Path everywhere is quite a big change though, so it's going to take a bit of time to implement properly 😄

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Sep 21, 2022
@wegank wegank added the 2.status: merge conflict This PR has merge conflicts with the target branch label Mar 20, 2024
@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Mar 20, 2024
@wegank wegank added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jul 4, 2024
@nixpkgs-ci nixpkgs-ci bot added the 12.approvals: 2 This PR was reviewed and approved by two persons. label Jun 25, 2025
@m-bdf m-bdf closed this Jan 25, 2026
@m-bdf m-bdf deleted the sdboot-builder-cleanup branch January 25, 2026 07:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.status: merge conflict This PR has merge conflicts with the target branch 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 12.approvals: 2 This PR was reviewed and approved by two persons.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants