NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
18.09k stars 14.07k forks source link

fsBefore does not recognise one filesystem as depending on another filesystem in the case of bind mounts where the source of the bind mount is not itself a mount point #86955

Closed jakobrs closed 3 years ago

jakobrs commented 4 years ago

Describe the bug stage-1.nix generates a list of file systems that are needed for boot. This list is not topologically sorted, which leads to situations where the system won't boot because the file systems aren't mounted in the right order.

Edit: After looking at it for a while, it seems the real problem is that this function is too strict:

{
  # Check whenever `b` depends on `a` as a fileSystem
  fsBefore = a: b: a.mountPoint == b.device
                || hasPrefix "${a.mountPoint}${optionalString (!(hasSuffix "/" a.mountPoint)) "/"}" b.mountPoint;
}

I think it should be something like this instead:

{
  # Check whenever `b` depends on `a` as a fileSystem
  fsBefore = a: b: hasPrefix "${a.mountPoint}${optionalString (!(hasSuffix "/" a.mountPoint)) "/"}" b.device
                || hasPrefix "${a.mountPoint}${optionalString (!(hasSuffix "/" a.mountPoint)) "/"}" b.mountPoint;
}

To Reproduce

{
  fileSystems."/persist".device = "/dev/disk/by-uuid/whatever";
  fileSystems."/nix" = {
    device = "/persist/nix";
    fsType = "none";
    options = [ "bind" ];
  };
}

Because /nixcomes before /persist alphabetically, it's mounted first (which fails). You can read the initrd-fsinfo file using:

  1. nixos-rebuild build
  2. nix-store -qR ./result | grep initrd-linux
  3. archivemount <result of last command>/initrd mnt
  4. cat mnt/nix/store/*initrd-fsinfo*

Expected behavior The list of file systems to be topologically sorted.

Additional context Originally found this while trying to try out root-on-tmpfs.

Notify maintainers Couldn't find anyone.

Metadata

Maintainer information:

# a list of nixpkgs attributes affected by the problem
attribute:
# a list of nixos modules affected by the problem
module: system/boot
griff commented 4 years ago

This is also a problem when using overlayfs since the dependencies are specified in options not device or mountPoint and for this problem #86967 is not enough to fix the issue. The fileSystem.<name>.depends option sugested by @jakobrs in https://github.com/NixOS/nixpkgs/pull/86967#issuecomment-624207721 would solve it.

Actually the only reason the current ISO and netboot images aren't broken because of this (they use overlayfs) is solely because /nix/.ro-store and /nix/.rw-store comes alphabetically before /nix/store.

stale[bot] commented 3 years ago

I marked this as stale due to inactivity. → More info

nrdxp commented 3 years ago

This is still an issue, and for me a fairly big one, since I can't generate secrets with agenix with my current setup: https://github.com/ryantm/agenix/issues/45#issuecomment-847852593 https://github.com/nix-community/impermanence/issues/22#issuecomment-847879359