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

stage2: use atomic bind mounts #142412

Merged
merged 1 commit into from
Nov 2, 2021
Merged

Conversation

manveru
Copy link
Contributor

@manveru manveru commented Oct 20, 2021

Motivation for this change

Stumbled over this when trying to do systemd-nspawn containers with only the requisite store paths for that system instead of mounting the full /nix/store. The second call to mount would fail to find the executable. With the atomic mounting everything works nicely again.
Kernel 4.5 was EOL in 2016, so we should be fine now :)

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 via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • 21.11 Release Notes (or backporting 21.05 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
  • 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 8.has: module (update) This PR changes an existing module in `nixos/` labels Oct 20, 2021
@blaggacao
Copy link
Contributor

/cc @Ma27 (eventually useful in the context of NixOS/rfcs#108)

containers with only the requisite store paths for that system instead of mounting the full /nix/store.

Copy link
Contributor

@jonringer jonringer left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@jonringer jonringer left a comment

Choose a reason for hiding this comment

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

I was able to delete store paths:

[14:08:46] jon@jon-laptop /home/jon/projects/nixpkgs (master)
$ nix-build -A hello
this path will be fetched (0.04 MiB download, 0.20 MiB unpacked):
  /nix/store/15c875mwri8xx3s0gqsdkdw7sqqyv55c-hello-2.10
copying path '/nix/store/15c875mwri8xx3s0gqsdkdw7sqqyv55c-hello-2.10' from 'https://cache.nixos.org'...
/nix/store/15c875mwri8xx3s0gqsdkdw7sqqyv55c-hello-2.10
[14:08:51] jon@jon-laptop /home/jon/projects/nixpkgs (master)
$ sudo rm -r /nix/store/15c875mwri8xx3s0gqsdkdw7sqqyv55c-hello-2.10
[sudo] password for jon:
[14:09:01] jon@jon-laptop /home/jon/projects/nixpkgs (master)
$ ls /nix/store/15c875mwri8xx3s0gqsdkdw7sqqyv55c-hello-2.10
"/nix/store/15c875mwri8xx3s0gqsdkdw7sqqyv55c-hello-2.10": No such file or directory (os error 2)

@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 1-10 labels Oct 20, 2021
#mount /nix/store /nix/store -o bind,remount,ro
mount --bind /nix/store /nix/store
mount -o remount,ro,bind /nix/store
mount /nix/store /nix/store -o bind,remount,ro
Copy link
Contributor

Choose a reason for hiding this comment

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

this should be bind,ro, otherwise it only works when /nix/store is already a mountpoint. interestingly linux even today (running 5.14.8 here) ignores the ro in ro,bind at first, but mount itself then follows up with a bind,remount,ro of its own to get a read-only mount. not sure when mount started doing that.

Copy link
Contributor Author

@manveru manveru Oct 21, 2021

Choose a reason for hiding this comment

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

I tried with just bind,ro, but that didn't work. It fails on https://github.com/pennae/nixpkgs/blob/master/nixos/modules/system/boot/stage-2-init.sh#L74 for some reason.
I'll leave this open for other people to discuss that hopefully know more about filesystems than me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think my problem is that /nix/store isn't a bind mount itself, just the paths within it, since i pass every store path with --bind-ro separately.
I wonder if there's a good way to distinguish this pattern.

Copy link
Contributor

Choose a reason for hiding this comment

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

the currently proposed solution (rbind /nix/store in the container case) also seems a bit lacking; if a store path were bound read-write by accident we would entirely on the host store being read-only to make container store paths read-only (since remounting /nix/store read-only would not propagate to its child mounts in the container). rbinding the store is definitely good, but we should probably also remount all submounts in the container case.

@Ma27
Copy link
Member

Ma27 commented Oct 21, 2021

Btw nspawn instances without full access to the host's /nix/store is something I hacked together within #140669

@manveru
Copy link
Contributor Author

manveru commented Oct 21, 2021

Thanks, your change to stage2 is definitely working better than mine.

@manveru manveru force-pushed the use-atomic-bind-mounts branch from 871bc83 to e629023 Compare October 21, 2021 13:04
Copy link
Member

@Ma27 Ma27 left a comment

Choose a reason for hiding this comment

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

I'm using this change on a few servers for several months now and as part of my nixos container work I also wrote some tests against it (see the linked PR above), so if it also solves your problem, then 👍 :)

Copy link
Contributor

@jonringer jonringer left a comment

Choose a reason for hiding this comment

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

Was able to reboot my laptop, and the store was mounted correctly

@Ma27 seems okay with the container code path.

@jonringer jonringer merged commit cb3a0f5 into NixOS:master Nov 2, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Nov 2, 2021

Successfully created backport PR #144186 for release-21.05.

@arianvp
Copy link
Member

arianvp commented Jan 3, 2024

I'm trying to understand why we special case --rbind for containers only? Why don't we do

mount --rbind /nix/store /nix/store

in both container and non-container cases?

It's confusing to me to have these special cases!

Also I think what is meant is recursive bind mounts? not atomic ? What part of this change make things atomic?

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 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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants