Merge pull request #186163 from lilyinstarlight/feature/systemd-stage-1-fs-label

nixos/systemd-stage-1: unify initrd fstab generation logic with system fstab

Changed files
+31 -25
nixos
modules
system
boot
systemd
tasks
-8
nixos/modules/system/boot/systemd/initrd.nix
···
fileSystems = filter utils.fsNeededForBoot config.system.build.fileSystems;
-
fstab = pkgs.writeText "initrd-fstab" (lib.concatMapStringsSep "\n"
-
({ fsType, mountPoint, device, options, autoFormat, autoResize, ... }@fs: let
-
opts = options ++ optional autoFormat "x-systemd.makefs" ++ optional autoResize "x-systemd.growfs";
-
finalDevice = if (lib.elem "bind" options) then "/sysroot${device}" else device;
-
in "${finalDevice} /sysroot${mountPoint} ${fsType} ${lib.concatStringsSep "," opts}") fileSystems);
-
needMakefs = lib.any (fs: fs.autoFormat) fileSystems;
needGrowfs = lib.any (fs: fs.autoResize) fileSystems;
···
[Manager]
DefaultEnvironment=PATH=/bin:/sbin ${optionalString (isBool cfg.emergencyAccess && cfg.emergencyAccess) "SYSTEMD_SULOGIN_FORCE=1"}
'';
-
-
"/etc/fstab".source = fstab;
"/lib/modules".source = "${modulesClosure}/lib/modules";
"/lib/firmware".source = "${modulesClosure}/lib/firmware";
+31 -17
nixos/modules/tasks/filesystems.nix
···
specialMount "${mount.device}" "${mount.mountPoint}" "${concatStringsSep "," mount.options}" "${mount.fsType}"
'') mounts);
+
makeFstabEntries =
+
let
+
fsToSkipCheck = [ "none" "bindfs" "btrfs" "zfs" "tmpfs" "nfs" "vboxsf" "glusterfs" "apfs" "9p" "cifs" "prl_fs" "vmhgfs" ];
+
isBindMount = fs: builtins.elem "bind" fs.options;
+
skipCheck = fs: fs.noCheck || fs.device == "none" || builtins.elem fs.fsType fsToSkipCheck || isBindMount fs;
+
# https://wiki.archlinux.org/index.php/fstab#Filepath_spaces
+
escape = string: builtins.replaceStrings [ " " "\t" ] [ "\\040" "\\011" ] string;
+
in fstabFileSystems: { rootPrefix ? "", excludeChecks ? false, extraOpts ? (fs: []) }: concatMapStrings (fs:
+
(optionalString (isBindMount fs) (escape rootPrefix))
+
+ (if fs.device != null then escape fs.device
+
else if fs.label != null then "/dev/disk/by-label/${escape fs.label}"
+
else throw "No device specified for mount point ‘${fs.mountPoint}’.")
+
+ " " + escape (rootPrefix + fs.mountPoint)
+
+ " " + fs.fsType
+
+ " " + builtins.concatStringsSep "," (fs.options ++ (extraOpts fs))
+
+ " " + (optionalString (!excludeChecks)
+
("0 " + (if skipCheck fs then "0" else if fs.mountPoint == "/" then "1" else "2")))
+
+ "\n"
+
) fstabFileSystems;
+
+
initrdFstab = pkgs.writeText "initrd-fstab" (makeFstabEntries (filter utils.fsNeededForBoot fileSystems) {
+
rootPrefix = "/sysroot";
+
excludeChecks = true;
+
extraOpts = fs:
+
(optional fs.autoResize "x-systemd.growfs")
+
++ (optional fs.autoFormat "x-systemd.makefs");
+
});
+
in
{
···
environment.etc.fstab.text =
let
-
fsToSkipCheck = [ "none" "bindfs" "btrfs" "zfs" "tmpfs" "nfs" "vboxsf" "glusterfs" "apfs" "9p" "cifs" "prl_fs" "vmhgfs" ];
-
isBindMount = fs: builtins.elem "bind" fs.options;
-
skipCheck = fs: fs.noCheck || fs.device == "none" || builtins.elem fs.fsType fsToSkipCheck || isBindMount fs;
-
# https://wiki.archlinux.org/index.php/fstab#Filepath_spaces
-
escape = string: builtins.replaceStrings [ " " "\t" ] [ "\\040" "\\011" ] string;
swapOptions = sw: concatStringsSep "," (
sw.options
++ optional (sw.priority != null) "pri=${toString sw.priority}"
···
# <file system> <mount point> <type> <options> <dump> <pass>
# Filesystems.
-
${concatMapStrings (fs:
-
(if fs.device != null then escape fs.device
-
else if fs.label != null then "/dev/disk/by-label/${escape fs.label}"
-
else throw "No device specified for mount point ‘${fs.mountPoint}’.")
-
+ " " + escape fs.mountPoint
-
+ " " + fs.fsType
-
+ " " + builtins.concatStringsSep "," fs.options
-
+ " 0"
-
+ " " + (if skipCheck fs then "0" else
-
if fs.mountPoint == "/" then "1" else "2")
-
+ "\n"
-
) fileSystems}
+
${makeFstabEntries fileSystems {}}
# Swap devices.
${flip concatMapStrings config.swapDevices (sw:
"${sw.realDevice} none swap ${swapOptions sw}\n"
)}
'';
+
+
boot.initrd.systemd.contents."/etc/fstab".source = initrdFstab;
# Provide a target that pulls in all filesystems.
systemd.targets.fs =