Skip to content

nixos/systemd/initrd: follow init param symlinks#209964

Merged
ElvishJerricco merged 1 commit intoNixOS:masterfrom
Majiir:fix-initrd-systemd-closure
Mar 18, 2024
Merged

nixos/systemd/initrd: follow init param symlinks#209964
ElvishJerricco merged 1 commit intoNixOS:masterfrom
Majiir:fix-initrd-systemd-closure

Conversation

@Majiir
Copy link
Contributor

@Majiir Majiir commented Jan 9, 2023

Description of changes

Fixes an incompatibility between boot.initrd.systemd and boot.loader.generationsDir. Fixes #219767.

The initrd-nixos-activation service determines which system closure to activate by inspecting the init kernel parameter. It assumes that the init parameter is a full path to the system closure, such that taking dirname and appending /prepare-root will point to the right script.

However, some boot loaders pass an init path which includes symlinks. For example, on a system using boot.loader.generationsDir, init=/boot/default/init where /boot/default is a symlink to a folder like /boot/system-38, which in turn contains init, a symlink to the init script in the system closure in the Nix store. This setup breaks with boot.initrd.systemd because the initrd-nixos-activation script fails to find and activate the system.

This change resolves symlinks in the init parameter. It does this by repeatedly calling readlink -m because the filesystem is mounted at /sysroot, so absolute symlinks will terminate symlink resolution. It now uses chroot.

Testing

Tested booting with these configurations:

Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • armv7l-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/)
  • 23.05 Release Notes (or backporting 22.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.

@github-actions github-actions bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 6.topic: systemd Software suite that provides an array of system components for Linux operating systems. 8.has: module (update) This PR changes an existing module in `nixos/` labels Jan 9, 2023
@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 Jan 9, 2023
@Majiir Majiir mentioned this pull request Jan 13, 2023
13 tasks
@Majiir Majiir force-pushed the fix-initrd-systemd-closure branch from 088b65b to eebf78c Compare March 5, 2023 20:05
@Majiir Majiir force-pushed the fix-initrd-systemd-closure branch from eebf78c to 39f2146 Compare July 16, 2023 22:30
@Majiir Majiir marked this pull request as ready for review July 16, 2023 23:22
@Majiir Majiir requested a review from a team as a code owner July 16, 2023 23:22
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/prs-ready-for-review/3032/2467

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/prs-ready-for-review/3032/2550

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/prs-ready-for-review/3032/2683

@ElvishJerricco
Copy link
Contributor

Wouldn't it be better to just chroot into /sysroot and run realpath, rather than writing a custom bash loop?

@Majiir
Copy link
Contributor Author

Majiir commented Feb 9, 2024

Wouldn't it be better to just chroot into /sysroot and run realpath, rather than writing a custom bash loop?

Yes, I agree. I think I got stuck on what path to use for realpath inside chroot. We don't know the system closure yet, so we can't go to sw/bin. Do we mount the initrd store temporarily to get its realpath? Bake in a store path from the actual store into initrd?

@ElvishJerricco
Copy link
Contributor

Well, the coreutils in use by initrd should exist in /sysroot, right? So just use the one that's already on PATH, expecting it to exist in /sysroot post-chroot?

I guess it's technically possible that the coreutils in initrd has been overriden or something like such that it isn't part of the system generation's runtime dependencies, but that seems like it isn't worth working around.

@Majiir Majiir force-pushed the fix-initrd-systemd-closure branch from 39f2146 to 58a3738 Compare February 20, 2024 04:43
@Majiir
Copy link
Contributor Author

Majiir commented Feb 20, 2024

Updated to use chroot and re-tested.

philiptaron added a commit to philiptaron/flock.nix that referenced this pull request Feb 21, 2024
Copy link
Contributor

@philiptaron philiptaron left a comment

Choose a reason for hiding this comment

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

$ journalctl -b -u initrd-nixos-activation
Feb 21 08:16:34 localhost systemd[1]: initrd-nixos-activation.service: starting held back, waiting for: initrd-fs.target
Feb 21 08:16:37 localhost systemd[1]: initrd-nixos-activation.service: Reinstalled deserialized job initrd-nixos-activation.service/start as 33
Feb 21 08:16:37 localhost systemd[1]: initrd-nixos-activation.service: starting held back, waiting for: initrd-fs.target
Feb 21 08:16:37 localhost systemd[1]: initrd-nixos-activation.service: AssertPathExists=/etc/initrd-release succeeded.
Feb 21 08:16:37 localhost systemd[1]: initrd-nixos-activation.service: Will spawn child (service_enter_start): /nix/store/ck5cf7fg48r7jh1w8fxgri5kqs5gy1hn-unit-script-initrd-nixos-activation-start/bin/initrd-nixos-activation-start
Feb 21 08:16:37 localhost systemd[1]: initrd-nixos-activation.service: Passing 0 fds to service
Feb 21 08:16:37 localhost systemd[1]: initrd-nixos-activation.service: About to execute: /nix/store/ck5cf7fg48r7jh1w8fxgri5kqs5gy1hn-unit-script-initrd-nixos-activation-start/bin/initrd-nixos-activation-start
Feb 21 08:16:37 localhost systemd[1]: initrd-nixos-activation.service: Forked /nix/store/ck5cf7fg48r7jh1w8fxgri5kqs5gy1hn-unit-script-initrd-nixos-activation-start/bin/initrd-nixos-activation-start as 486
Feb 21 08:16:37 localhost systemd[1]: initrd-nixos-activation.service: Changed dead -> start
Feb 21 08:16:37 localhost systemd[1]: Starting NixOS Activation...
Feb 21 08:16:37 localhost (on-start)[486]: Found cgroup2 on /sys/fs/cgroup/, full unified hierarchy
Feb 21 08:16:37 localhost (on-start)[486]: Found cgroup2 on /sys/fs/cgroup/, full unified hierarchy
Feb 21 08:16:37 localhost initrd-nixos-activation-start[486]: booting system configuration /nix/store/vh11rbcx3w3innm2q2x2rsnydlynlg1p-nixos-system-zebul-24.05.20240218.58a3738
Feb 21 08:16:37 localhost initrd-nixos-activation-start[486]: running activation script...
Feb 21 08:16:38 localhost initrd-nixos-activation-start[517]: setting up /etc...
Feb 21 08:16:38 localhost systemd[1]: initrd-nixos-activation.service: Child 486 belongs to initrd-nixos-activation.service.
Feb 21 08:16:38 localhost systemd[1]: initrd-nixos-activation.service: Main process exited, code=exited, status=0/SUCCESS (success)
Feb 21 08:16:38 localhost systemd[1]: initrd-nixos-activation.service: Deactivated successfully.
Feb 21 08:16:38 localhost systemd[1]: initrd-nixos-activation.service: Service will not restart (restart setting)
Feb 21 08:16:38 localhost systemd[1]: initrd-nixos-activation.service: Changed start -> dead
Feb 21 08:16:38 localhost systemd[1]: initrd-nixos-activation.service: Job 33 initrd-nixos-activation.service/start finished, result=done
Feb 21 08:16:38 localhost systemd[1]: Finished NixOS Activation.
Feb 21 08:16:38 localhost systemd[1]: initrd-nixos-activation.service: Consumed 136ms CPU time.
Feb 21 08:16:38 localhost systemd[1]: initrd-nixos-activation.service: Releasing resources...
Feb 21 08:16:38 zebul systemd[1]: initrd-nixos-activation.service: Collecting.

@Majiir Majiir marked this pull request as draft February 24, 2024 03:41
@Majiir
Copy link
Contributor Author

Majiir commented Feb 24, 2024

I ran into errors related to the chroot change while setting up a new Raspberry Pi. Marking this as draft until I can figure out the cause.

@Majiir Majiir marked this pull request as ready for review February 24, 2024 06:50
@Majiir
Copy link
Contributor Author

Majiir commented Feb 24, 2024

Nevermind, the issue was something else. And now I've tested this change with extlinux.

@wegank wegank added the 12.approvals: 2 This PR was reviewed and approved by two persons. label Mar 9, 2024
@philiptaron
Copy link
Contributor

Is there anything that prevents this PR from being merged?

@ElvishJerricco
Copy link
Contributor

@ofborg test systemd-initrd-simple

@philiptaron
Copy link
Contributor

Unrelated error, but otherwise successful.

machine # [ 6.995250] dbus-daemon[610]: dbus[610]: Unknown username "systemd-timesync" in message bus configuration file

@ElvishJerricco ElvishJerricco merged commit 99587a3 into NixOS:master Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 6.topic: systemd Software suite that provides an array of system components for Linux operating systems. 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

Development

Successfully merging this pull request may close these issues.

systemd-initrd does not follow symlinks in init

5 participants