git.iko.soy/typeable/nixpkgs
on master help sign in
nixos: apply toposort to fileSystems to support bind and move mounts

And use new `config.system.build.fileSystems` property everywhere.

commit65d26c4dc12f8f0113b6b128573f18492ac5b6f6 authorJan Malakhovski <oxij@oxij.org> dateWed 25 Nov 2015, 19:09 (11 years ago) parent2c8ca0d
6 files changed+43−19
nixos/lib/utils.nix+5 nixos/modules/security/grsecurity.nix+1−1 nixos/modules/system/boot/stage-1.nix+11−12 nixos/modules/tasks/encrypted-devices.nix+1−1 nixos/modules/tasks/filesystems.nix+23−3 nixos/modules/tasks/filesystems/zfs.nix+2−2
nixos/lib/utils.nix+5
@@ -2,6 +2,11 @@ pkgs: with pkgs.lib;
22
33rec {
44
5+ # Check whenever `b` depends on `a` as a fileSystem
6+ # FIXME: it's incorrect to simply use hasPrefix here: "/dev/a" is not a parent of "/dev/ab"
7+ fsBefore = a: b: ((any (x: elem x [ "bind" "move" ]) b.options) && (a.mountPoint == b.device))
8+ || (hasPrefix a.mountPoint b.mountPoint);
9+
510 # Escape a path according to the systemd rules, e.g. /dev/xyzzy
611 # becomes dev-xyzzy. FIXME: slow.
712 escapeSystemdPath = s:
nixos/modules/security/grsecurity.nix+1−1
@@ -12,7 +12,7 @@ let
1212 (fs: (fs.neededForBoot
1313 || elem fs.mountPoint [ "/" "/nix" "/nix/store" "/var" "/var/log" "/var/lib" "/etc" ])
1414 && fs.fsType == "zfs")
15− (attrValues config.fileSystems) != [];
15+ config.system.build.fileSystems != [];
1616
1717 # Ascertain whether NixOS container support is required
1818 containerSupportRequired =
nixos/modules/system/boot/stage-1.nix+11−12
@@ -3,7 +3,7 @@
33# the modules necessary to mount the root file system, then calls the
44# init in the root file system to start the second boot stage.
55
6−{ config, lib, pkgs, ... }:
6+{ config, lib, utils, pkgs, ... }:
77
88with lib;
99
@@ -23,6 +23,14 @@ let
2323 };
2424
2525
26+ # The initrd only has to mount `/` or any FS marked as necessary for
27+ # booting (such as the FS containing `/nix/store`, or an FS needed for
28+ # mounting `/`, like `/` on a loopback).
29+ fileSystems = filter
30+ (fs: fs.neededForBoot || elem fs.mountPoint [ "/" "/nix" "/nix/store" "/var" "/var/log" "/var/lib" "/etc" ])
31+ config.system.build.fileSystems;
32+
33+
2634 # Some additional utilities needed in stage 1, like mount, lvm, fsck
2735 # etc. We don't want to bring in all of those packages, so we just
2836 # copy what we need. Instead of using statically linked binaries,
@@ -71,7 +79,7 @@ let
7179 ln -sf kmod $out/bin/modprobe
7280
7381 # Copy resize2fs if needed.
74− ${optionalString (any (fs: fs.autoResize) (attrValues config.fileSystems)) ''
82+ ${optionalString (any (fs: fs.autoResize) config.system.build.fileSystems) ''
7583 # We need mke2fs in the initrd.
7684 copy_bin_and_libs ${pkgs.e2fsprogs}/sbin/resize2fs
7785 ''}
@@ -128,14 +136,6 @@ let
128136 ''; # */
129137
130138
131− # The initrd only has to mount / or any FS marked as necessary for
132− # booting (such as the FS containing /nix/store, or an FS needed for
133− # mounting /, like / on a loopback).
134− fileSystems = filter
135− (fs: fs.neededForBoot || elem fs.mountPoint [ "/" "/nix" "/nix/store" "/var" "/var/log" "/var/lib" "/etc" ])
136− (attrValues config.fileSystems);
137−
138−
139139 udevRules = pkgs.stdenv.mkDerivation {
140140 name = "udev-rules";
141141 allowedReferences = [ extraUtils ];
@@ -398,9 +398,8 @@ in
398398 };
399399
400400 config = mkIf (!config.boot.isContainer) {
401−
402401 assertions = [
403− { assertion = any (fs: fs.mountPoint == "/") (attrValues config.fileSystems);
402+ { assertion = any (fs: fs.mountPoint == "/") fileSystems;
404403 message = "The ‘fileSystems’ option does not specify your root file system.";
405404 }
406405 { assertion = let inherit (config.boot) resumeDevice; in
nixos/modules/tasks/encrypted-devices.nix+1−1
@@ -3,7 +3,7 @@
33with lib;
44
55let
6− fileSystems = attrValues config.fileSystems ++ config.swapDevices;
6+ fileSystems = config.system.build.fileSystems ++ config.swapDevices;
77 encDevs = filter (dev: dev.encrypted.enable) fileSystems;
88 keyedEncDevs = filter (dev: dev.encrypted.keyFile != null) encDevs;
99 keylessEncDevs = filter (dev: dev.encrypted.keyFile == null) encDevs;
nixos/modules/tasks/filesystems.nix+23−3
@@ -5,7 +5,16 @@ with utils;
55
66let
77
8− fileSystems = attrValues config.fileSystems;
8+ fileSystems' = toposort fsBefore (attrValues config.fileSystems);
9+
10+ fileSystems = if fileSystems' ? "result"
11+ then # use topologically sorted fileSystems everywhere
12+ fileSystems'.result
13+ else # the assertion below will catch this,
14+ # but we fall back to the original order
15+ # anyway so that other modules could check
16+ # their assertions too
17+ (attrValues config.fileSystems);
918
1019 prioOption = prio: optionalString (prio != null) " pri=${toString prio}";
1120
@@ -162,6 +171,17 @@ in
162171
163172 config = {
164173
174+ assertions = let
175+ ls = sep: concatMapStringsSep sep (x: x.mountPoint);
176+ in [
177+ { assertion = ! (fileSystems' ? "cycle");
178+ message = "The ‘fileSystems’ option can't be topologically sorted: mountpoint dependency path ${ls " -> " fileSystems'.cycle} loops to ${ls ", " fileSystems'.loops}";
179+ }
180+ ];
181+
182+ # Export for use in other modules
183+ system.build.fileSystems = fileSystems;
184+
165185 boot.supportedFilesystems = map (fs: fs.fsType) fileSystems;
166186
167187 # Add the mount helpers to the system path so that `mount' can find them.
@@ -177,7 +197,7 @@ in
177197 # This is a generated file. Do not edit!
178198
179199 # Filesystems.
180− ${flip concatMapStrings fileSystems (fs:
200+ ${concatMapStrings (fs:
181201 (if fs.device != null then fs.device
182202 else if fs.label != null then "/dev/disk/by-label/${fs.label}"
183203 else throw "No device specified for mount point ‘${fs.mountPoint}’.")
@@ -188,7 +208,7 @@ in
188208 + " " + (if skipCheck fs then "0" else
189209 if fs.mountPoint == "/" then "1" else "2")
190210 + "\n"
191− )}
211+ ) fileSystems}
192212
193213 # Swap devices.
194214 ${flip concatMapStrings config.swapDevices (sw:
nixos/modules/tasks/filesystems/zfs.nix+2−2
@@ -36,7 +36,7 @@ let
3636
3737 fsToPool = fs: datasetToPool fs.device;
3838
39− zfsFilesystems = filter (x: x.fsType == "zfs") (attrValues config.fileSystems);
39+ zfsFilesystems = filter (x: x.fsType == "zfs") config.system.build.fileSystems;
4040
4141 isRoot = fs: fs.neededForBoot || elem fs.mountPoint [ "/" "/nix" "/nix/store" "/var" "/var/log" "/var/lib" "/etc" ];
4242
@@ -277,7 +277,7 @@ in
277277
278278 systemd.services = let
279279 getPoolFilesystems = pool:
280− filter (x: x.fsType == "zfs" && (fsToPool x) == pool) (attrValues config.fileSystems);
280+ filter (x: x.fsType == "zfs" && (fsToPool x) == pool) config.system.build.fileSystems;
281281
282282 getPoolMounts = pool:
283283 let