From 2eed1de920645e2a5a22c0c668fcf37efdf7e365 Mon Sep 17 00:00:00 2001 From: Matthias Berndt Date: Thu, 4 May 2023 00:43:21 +0200 Subject: [PATCH 1/9] stratisd: split out initrd support --- pkgs/tools/filesystems/stratisd/default.nix | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pkgs/tools/filesystems/stratisd/default.nix b/pkgs/tools/filesystems/stratisd/default.nix index 73cb3798ef2e..b9095df43e44 100644 --- a/pkgs/tools/filesystems/stratisd/default.nix +++ b/pkgs/tools/filesystems/stratisd/default.nix @@ -72,6 +72,8 @@ stdenv.mkDerivation rec { lvm2 ]; + outputs = ["out" "initrd"]; + EXECUTABLES_PATHS = lib.makeBinPath ([ xfsprogs thin-provisioning-tools @@ -93,6 +95,14 @@ stdenv.mkDerivation rec { # remove files for supporting dracut postInstall = '' + mkdir -p "$initrd/bin" + cp "dracut/90stratis/stratis-rootfs-setup" "$initrd/bin" + mkdir -p "$initrd/lib/systemd/system" + substitute "dracut/90stratis/stratisd-min.service" "$initrd/lib/systemd/system/stratisd-min.service" \ + --replace /usr "$out" \ + --replace mkdir "${coreutils}/bin/mkdir" + mkdir -p "$initrd/lib/udev/rules.d" + cp udev/61-stratisd.rules "$initrd/lib/udev/rules.d" rm -r "$out/lib/dracut" rm -r "$out/lib/systemd/system-generators" ''; From 1632e73b19cd25ac3bc0c27f07e932728c3e893c Mon Sep 17 00:00:00 2001 From: Matthias Berndt Date: Thu, 4 May 2023 00:44:36 +0200 Subject: [PATCH 2/9] nixos/stratis: enable booting from stratis volume --- .../manual/release-notes/rl-2305.section.md | 2 + nixos/modules/system/boot/stratisroot.nix | 68 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 nixos/modules/system/boot/stratisroot.nix diff --git a/nixos/doc/manual/release-notes/rl-2305.section.md b/nixos/doc/manual/release-notes/rl-2305.section.md index ec213e5f2f74..44d82a950294 100644 --- a/nixos/doc/manual/release-notes/rl-2305.section.md +++ b/nixos/doc/manual/release-notes/rl-2305.section.md @@ -476,6 +476,8 @@ In addition to numerous new and upgraded packages, this release has the followin - `boot.initrd.luks.device.` has a new `tryEmptyPassphrase` option, this is useful for OEM's who need to install an encrypted disk with a future settable passphrase +- there is a new `boot/stratisroot.nix` module that enables booting from a volume managed by the Stratis storage management daemon. Use `boot.stratis.rootPoolUuid` to configure the pool containing the root volume + - Lisp gained a [manual section](https://nixos.org/manual/nixpkgs/stable/#lisp), documenting a new and backwards incompatible interface. The previous interface will be removed in a future release. - The `bind` module now allows the per-zone `allow-query` setting to be configured (previously it was hard-coded to `any`; it still defaults to `any` to retain compatibility). diff --git a/nixos/modules/system/boot/stratisroot.nix b/nixos/modules/system/boot/stratisroot.nix new file mode 100644 index 000000000000..b4e2dbde6d4c --- /dev/null +++ b/nixos/modules/system/boot/stratisroot.nix @@ -0,0 +1,68 @@ +{ config, lib, pkgs, ... }: +let + types = lib.types; +in +{ + options.boot.stratis = { + rootPoolUuid = lib.mkOption { + type = types.uniq types.str; + description = lib.mdoc '' + UUID of the stratis pool that the root fs is located in + ''; + example = "04c68063-90a5-4235-b9dd-6180098a20d9"; + }; + }; + config = { + assertions = [ + { + assertion = config.boot.initrd.systemd.enable; + message = "stratis root fs requires systemd initrd"; + } + ]; + boot.initrd = { + systemd = { + storePaths = [ + "${pkgs.stratisd}/lib/udev/stratis-base32-decode" + "${pkgs.stratisd}/lib/udev/stratis-str-cmp" + "${pkgs.lvm2.bin}/bin/dmsetup" + "${pkgs.stratisd}/libexec/stratisd-min" + "${pkgs.stratisd.initrd}/bin/stratis-rootfs-setup" + ]; + packages = [pkgs.stratisd.initrd]; + extraBin = { + thin_check = "${pkgs."thin-provisioning-tools"}/bin/thin_check"; + thin_repair = "${pkgs."thin-provisioning-tools"}/bin/thin_repair"; + thin_metadata_size = "${pkgs."thin-provisioning-tools"}/bin/thin_metadata_size"; + stratis-min = "${pkgs.stratisd}/bin/stratis-min"; + }; + services = { + stratis-setup = { + description = "setup for Stratis root filesystem"; + unitConfig.DefaultDependencies = "no"; + conflicts = [ "shutdown.target" ]; + onFailure = [ "emergency.target" ]; + unitConfig.OnFailureJobMode = "isolate"; + wants = [ "stratisd-min.service" "plymouth-start.service" "stratis-clevis-setup.service" ]; + wantedBy = [ "initrd.target" ]; + after = [ "paths.target" "plymouth-start.service" "stratisd-min.service" ]; + before = [ "initrd.target" ]; + environment.STRATIS_ROOTFS_UUID = config.boot.stratis.rootPoolUuid; + serviceConfig = { + Type = "oneshot"; + ExecStart = "${pkgs.stratisd.initrd}/bin/stratis-rootfs-setup"; + RemainAfterExit = "yes"; + }; + }; + }; + }; + availableKernelModules = [ "dm-thin-pool" "dm-crypt" ] ++ [ "aes" "aes_generic" "blowfish" "twofish" + "serpent" "cbc" "xts" "lrw" "sha1" "sha256" "sha512" + "af_alg" "algif_skcipher" + ]; + services.udev.packages = [ + pkgs.stratisd.initrd + pkgs.lvm2 + ]; + }; + }; +} From 9f1bc0fa02647fb826968f5d217c204feb38babc Mon Sep 17 00:00:00 2001 From: Matthias Berndt Date: Fri, 5 May 2023 00:02:27 +0200 Subject: [PATCH 3/9] address review comments --- nixos/modules/system/boot/stratisroot.nix | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nixos/modules/system/boot/stratisroot.nix b/nixos/modules/system/boot/stratisroot.nix index b4e2dbde6d4c..a53d2c496306 100644 --- a/nixos/modules/system/boot/stratisroot.nix +++ b/nixos/modules/system/boot/stratisroot.nix @@ -39,13 +39,13 @@ in stratis-setup = { description = "setup for Stratis root filesystem"; unitConfig.DefaultDependencies = "no"; - conflicts = [ "shutdown.target" ]; + conflicts = [ "shutdown.target" "initrd-switch-root.target" ]; onFailure = [ "emergency.target" ]; unitConfig.OnFailureJobMode = "isolate"; - wants = [ "stratisd-min.service" "plymouth-start.service" "stratis-clevis-setup.service" ]; + wants = [ "stratisd-min.service" "plymouth-start.service" ]; wantedBy = [ "initrd.target" ]; after = [ "paths.target" "plymouth-start.service" "stratisd-min.service" ]; - before = [ "initrd.target" ]; + before = [ "initrd.target" "shutdown.target" "initrd-switch-root.target" ]; environment.STRATIS_ROOTFS_UUID = config.boot.stratis.rootPoolUuid; serviceConfig = { Type = "oneshot"; From d1411444b62e4d4c0d83dd8d7d44a4a45de1c51d Mon Sep 17 00:00:00 2001 From: Matthias Berndt Date: Sun, 7 May 2023 18:47:44 +0200 Subject: [PATCH 4/9] add installer test for stratis root fs --- nixos/tests/installer-systemd-stage-1.nix | 1 + nixos/tests/installer.nix | 60 +++++++++++++++++++++-- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/nixos/tests/installer-systemd-stage-1.nix b/nixos/tests/installer-systemd-stage-1.nix index 03f0ec8d746b..05fb2b2ae89c 100644 --- a/nixos/tests/installer-systemd-stage-1.nix +++ b/nixos/tests/installer-systemd-stage-1.nix @@ -27,6 +27,7 @@ simpleUefiGrub simpleUefiGrubSpecialisation simpleUefiSystemdBoot + stratisRoot # swraid zfsroot ; diff --git a/nixos/tests/installer.nix b/nixos/tests/installer.nix index 51d0d232ebbf..5a58744d6209 100644 --- a/nixos/tests/installer.nix +++ b/nixos/tests/installer.nix @@ -19,6 +19,7 @@ let { imports = [ ./hardware-configuration.nix + ./amendments.nix ]; documentation.enable = false; @@ -72,7 +73,7 @@ let # partitions and filesystems. testScriptFun = { bootLoader, createPartitions, grubVersion, grubDevice, grubUseEfi , grubIdentifier, preBootCommands, postBootCommands, extraConfig - , testSpecialisationConfig + , testSpecialisationConfig, amendConfig }: let iface = if grubVersion == 1 then "ide" else "virtio"; isEfi = bootLoader == "systemd-boot" || (bootLoader == "grub" && grubUseEfi); @@ -129,6 +130,9 @@ let "/mnt/etc/nixos/configuration.nix", ) machine.copy_from_host("${pkgs.writeText "secret" "secret"}", "/mnt/etc/nixos/secret") + amendments = '{}' + ${amendConfig} + machine.succeed(f"printf '{amendments}' > /mnt/etc/nixos/amendments.nix") with subtest("Perform the installation"): machine.succeed("nixos-install < /dev/null >&2") @@ -281,7 +285,7 @@ let makeInstallerTest = name: - { createPartitions, preBootCommands ? "", postBootCommands ? "", extraConfig ? "" + { createPartitions, preBootCommands ? "", postBootCommands ? "", extraConfig ? "", amendConfig ? "" , extraInstallerConfig ? {} , bootLoader ? "grub" # either "grub" or "systemd-boot" , grubVersion ? 2, grubDevice ? "/dev/vda", grubIdentifier ? "uuid", grubUseEfi ? false @@ -392,7 +396,7 @@ let testScript = testScriptFun { inherit bootLoader createPartitions preBootCommands postBootCommands - grubVersion grubDevice grubIdentifier grubUseEfi extraConfig + grubVersion grubDevice grubIdentifier grubUseEfi extraConfig amendConfig testSpecialisationConfig; }; }; @@ -1017,4 +1021,54 @@ in { ) ''; }; +} // optionalAttrs systemdStage1 { + stratisRoot = makeInstallerTest "stratisRoot" { + createPartitions = '' + machine.succeed( + "sgdisk --zap-all /dev/vda", + "sgdisk --new=1:0:+100M --typecode=0:ef00 /dev/vda", # /boot + "sgdisk --new=2:0:+1G --typecode=0:8200 /dev/vda", # swap + "sgdisk --new=3:0:+5G --typecode=0:8300 /dev/vda", # / + "udevadm settle", + + "mkfs.vfat /dev/vda1", + "mkswap /dev/vda2 -L swap", + "swapon -L swap", + "stratis pool create my-pool /dev/vda3", + "stratis filesystem create my-pool nixos", + "udevadm settle", + + "mount /dev/stratis/my-pool/nixos /mnt", + "mkdir -p /mnt/boot", + "mount /dev/vda1 /mnt/boot" + ) + ''; + bootLoader = "systemd-boot"; + extraInstallerConfig = { modulesPath, ...}: { + imports = [ (modulesPath + "/tasks/stratis.nix") ]; + config = { + services.stratis.enable = true; + environment.systemPackages = [ + pkgs.stratis-cli + pkgs.thin-provisioning-tools + pkgs.lvm2.bin + pkgs.stratisd.initrd + ]; + }; + }; + amendConfig = '' + # This comment is here for Python indentation purposes + (header, pool_line) = machine.succeed("stratis pool list").splitlines() + index = header.find("UUID") + uuid = pool_line[index - 32: index + 4] + amendments = f"""{{ modulesPath, ... }}: {{ + imports = [ + (modulesPath + "/system/boot/stratisroot.nix") + ]; + config = {{ + boot.stratis.rootPoolUuid = "{uuid}"; + }}; + }}""" + ''; + }; } From 8aa320b4c2d0d7699170f6da154ebb7598e4d154 Mon Sep 17 00:00:00 2001 From: Matthias Berndt Date: Mon, 8 May 2023 23:18:55 +0200 Subject: [PATCH 5/9] address code review issues --- nixos/modules/module-list.nix | 1 + nixos/modules/system/boot/stratisroot.nix | 9 ++-- nixos/tests/installer.nix | 59 +++++++++-------------- 3 files changed, 30 insertions(+), 39 deletions(-) diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix index 0eb9631e6d42..4821b4033fdd 100644 --- a/nixos/modules/module-list.nix +++ b/nixos/modules/module-list.nix @@ -1331,6 +1331,7 @@ ./system/boot/loader/raspberrypi/raspberrypi.nix ./system/boot/loader/systemd-boot/systemd-boot.nix ./system/boot/luksroot.nix + ./system/boot/stratisroot.nix ./system/boot/modprobe.nix ./system/boot/networkd.nix ./system/boot/plymouth.nix diff --git a/nixos/modules/system/boot/stratisroot.nix b/nixos/modules/system/boot/stratisroot.nix index a53d2c496306..53621008c338 100644 --- a/nixos/modules/system/boot/stratisroot.nix +++ b/nixos/modules/system/boot/stratisroot.nix @@ -5,18 +5,19 @@ in { options.boot.stratis = { rootPoolUuid = lib.mkOption { - type = types.uniq types.str; - description = lib.mdoc '' + type = types.uniq (types.nullOr types.str); + description = lib.mdDoc '' UUID of the stratis pool that the root fs is located in ''; example = "04c68063-90a5-4235-b9dd-6180098a20d9"; + default = null; }; }; - config = { + config = lib.mkIf (config.boot.stratis.rootPoolUuid != null) { assertions = [ { assertion = config.boot.initrd.systemd.enable; - message = "stratis root fs requires systemd initrd"; + message = "stratis root fs requires systemd stage 1"; } ]; boot.initrd = { diff --git a/nixos/tests/installer.nix b/nixos/tests/installer.nix index 5a58744d6209..1346eb36c36e 100644 --- a/nixos/tests/installer.nix +++ b/nixos/tests/installer.nix @@ -19,7 +19,6 @@ let { imports = [ ./hardware-configuration.nix - ./amendments.nix ]; documentation.enable = false; @@ -73,7 +72,7 @@ let # partitions and filesystems. testScriptFun = { bootLoader, createPartitions, grubVersion, grubDevice, grubUseEfi , grubIdentifier, preBootCommands, postBootCommands, extraConfig - , testSpecialisationConfig, amendConfig + , testSpecialisationConfig }: let iface = if grubVersion == 1 then "ide" else "virtio"; isEfi = bootLoader == "systemd-boot" || (bootLoader == "grub" && grubUseEfi); @@ -130,9 +129,6 @@ let "/mnt/etc/nixos/configuration.nix", ) machine.copy_from_host("${pkgs.writeText "secret" "secret"}", "/mnt/etc/nixos/secret") - amendments = '{}' - ${amendConfig} - machine.succeed(f"printf '{amendments}' > /mnt/etc/nixos/amendments.nix") with subtest("Perform the installation"): machine.succeed("nixos-install < /dev/null >&2") @@ -285,7 +281,7 @@ let makeInstallerTest = name: - { createPartitions, preBootCommands ? "", postBootCommands ? "", extraConfig ? "", amendConfig ? "" + { createPartitions, preBootCommands ? "", postBootCommands ? "", extraConfig ? "" , extraInstallerConfig ? {} , bootLoader ? "grub" # either "grub" or "systemd-boot" , grubVersion ? 2, grubDevice ? "/dev/vda", grubIdentifier ? "uuid", grubUseEfi ? false @@ -396,7 +392,7 @@ let testScript = testScriptFun { inherit bootLoader createPartitions preBootCommands postBootCommands - grubVersion grubDevice grubIdentifier grubUseEfi extraConfig amendConfig + grubVersion grubDevice grubIdentifier grubUseEfi extraConfig testSpecialisationConfig; }; }; @@ -1025,23 +1021,29 @@ in { stratisRoot = makeInstallerTest "stratisRoot" { createPartitions = '' machine.succeed( - "sgdisk --zap-all /dev/vda", - "sgdisk --new=1:0:+100M --typecode=0:ef00 /dev/vda", # /boot - "sgdisk --new=2:0:+1G --typecode=0:8200 /dev/vda", # swap - "sgdisk --new=3:0:+5G --typecode=0:8300 /dev/vda", # / - "udevadm settle", + "sgdisk --zap-all /dev/vda", + "sgdisk --new=1:0:+100M --typecode=0:ef00 /dev/vda", # /boot + "sgdisk --new=2:0:+1G --typecode=0:8200 /dev/vda", # swap + "sgdisk --new=3:0:+5G --typecode=0:8300 /dev/vda", # / + "udevadm settle", - "mkfs.vfat /dev/vda1", - "mkswap /dev/vda2 -L swap", - "swapon -L swap", - "stratis pool create my-pool /dev/vda3", - "stratis filesystem create my-pool nixos", - "udevadm settle", + "mkfs.vfat /dev/vda1", + "mkswap /dev/vda2 -L swap", + "swapon -L swap", + "stratis pool create my-pool /dev/vda3", + "stratis filesystem create my-pool nixos", + "udevadm settle", - "mount /dev/stratis/my-pool/nixos /mnt", - "mkdir -p /mnt/boot", - "mount /dev/vda1 /mnt/boot" + "mount /dev/stratis/my-pool/nixos /mnt", + "mkdir -p /mnt/boot", + "mount /dev/vda1 /mnt/boot" ) + + (header, pool_line) = machine.succeed("stratis pool list").splitlines() + index = header.find("UUID") + uuid = pool_line[index - 32: index + 4] + machine.succeed("mkdir -p /mnt/etc/nixos") + machine.succeed(f"printf %s {uuid} > /mnt/etc/nixos/rootPoolUuid.txt") ''; bootLoader = "systemd-boot"; extraInstallerConfig = { modulesPath, ...}: { @@ -1056,19 +1058,6 @@ in { ]; }; }; - amendConfig = '' - # This comment is here for Python indentation purposes - (header, pool_line) = machine.succeed("stratis pool list").splitlines() - index = header.find("UUID") - uuid = pool_line[index - 32: index + 4] - amendments = f"""{{ modulesPath, ... }}: {{ - imports = [ - (modulesPath + "/system/boot/stratisroot.nix") - ]; - config = {{ - boot.stratis.rootPoolUuid = "{uuid}"; - }}; - }}""" - ''; + extraConfig = "boot.stratis.rootPoolUuid = builtins.readFile ./rootPoolUuid.txt;"; }; } From 3aa262b644feff5153888ed85661450cb151088b Mon Sep 17 00:00:00 2001 From: Matthias Berndt Date: Mon, 15 May 2023 20:41:10 -0400 Subject: [PATCH 6/9] make nixos-generate-config generate stratis pool UUIDs --- .../installer/tools/nixos-generate-config.pl | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/nixos/modules/installer/tools/nixos-generate-config.pl b/nixos/modules/installer/tools/nixos-generate-config.pl index c65898b261cb..c822ea61200d 100644 --- a/nixos/modules/installer/tools/nixos-generate-config.pl +++ b/nixos/modules/installer/tools/nixos-generate-config.pl @@ -335,7 +335,7 @@ sub findStableDevPath { my $st = stat($dev) or return $dev; - foreach my $dev2 (glob("/dev/disk/by-uuid/*"), glob("/dev/mapper/*"), glob("/dev/disk/by-label/*")) { + foreach my $dev2 (glob("/dev/stratis/*/*"), glob("/dev/disk/by-uuid/*"), glob("/dev/mapper/*"), glob("/dev/disk/by-label/*")) { my $st2 = stat($dev2) or next; return $dev2 if $st->rdev == $st2->rdev; } @@ -467,6 +467,17 @@ EOF } } + # is this a stratis fs? + my $stableDevPath = findStableDevPath $device; + my $stratisPool; + if ($stableDevPath =~ qr#/dev/stratis/(.*)/.*#) { + my $poolName = $1; + my ($header, @lines) = split "\n", qx/stratis pool list/; + my $uuidIndex = index $header, 'UUID'; + my ($line) = grep /^$poolName /, @lines; + $stratisPool = substr $line, $uuidIndex - 32, 36; + } + # Don't emit tmpfs entry for /tmp, because it most likely comes from the # boot.tmp.useTmpfs option in configuration.nix (managed declaratively). next if ($mountPoint eq "/tmp" && $fsType eq "tmpfs"); @@ -474,7 +485,7 @@ EOF # Emit the filesystem. $fileSystems .= < Date: Tue, 16 May 2023 22:48:36 -0400 Subject: [PATCH 7/9] improve stratis initrd support it is now possible to supply a stratis pool uuid for every filesystem, and if that filesystem is required for boot, the relevant pool will be started in the initramfs. --- .../manual/release-notes/rl-2305.section.md | 2 +- nixos/modules/system/boot/stratisroot.nix | 58 +++++++++---------- nixos/modules/tasks/filesystems.nix | 9 +++ nixos/tests/installer.nix | 7 --- 4 files changed, 37 insertions(+), 39 deletions(-) diff --git a/nixos/doc/manual/release-notes/rl-2305.section.md b/nixos/doc/manual/release-notes/rl-2305.section.md index 44d82a950294..07b6a5f2445d 100644 --- a/nixos/doc/manual/release-notes/rl-2305.section.md +++ b/nixos/doc/manual/release-notes/rl-2305.section.md @@ -476,7 +476,7 @@ In addition to numerous new and upgraded packages, this release has the followin - `boot.initrd.luks.device.` has a new `tryEmptyPassphrase` option, this is useful for OEM's who need to install an encrypted disk with a future settable passphrase -- there is a new `boot/stratisroot.nix` module that enables booting from a volume managed by the Stratis storage management daemon. Use `boot.stratis.rootPoolUuid` to configure the pool containing the root volume +- there is a new `boot/stratisroot.nix` module that enables booting from a volume managed by the Stratis storage management daemon. Use `fileSystems..stratis.poolUuid` to configure the pool containing the fs. - Lisp gained a [manual section](https://nixos.org/manual/nixpkgs/stable/#lisp), documenting a new and backwards incompatible interface. The previous interface will be removed in a future release. diff --git a/nixos/modules/system/boot/stratisroot.nix b/nixos/modules/system/boot/stratisroot.nix index 53621008c338..f0d1b5475554 100644 --- a/nixos/modules/system/boot/stratisroot.nix +++ b/nixos/modules/system/boot/stratisroot.nix @@ -1,19 +1,11 @@ -{ config, lib, pkgs, ... }: +{ config, lib, pkgs, utils, ... }: let types = lib.types; + requiredStratisFilesystems = lib.attrsets.filterAttrs (_: x: utils.fsNeededForBoot x && x.stratis.poolUuid != null) config.fileSystems; in { - options.boot.stratis = { - rootPoolUuid = lib.mkOption { - type = types.uniq (types.nullOr types.str); - description = lib.mdDoc '' - UUID of the stratis pool that the root fs is located in - ''; - example = "04c68063-90a5-4235-b9dd-6180098a20d9"; - default = null; - }; - }; - config = lib.mkIf (config.boot.stratis.rootPoolUuid != null) { + options = {}; + config = lib.mkIf (builtins.length (lib.attrsets.attrValues requiredStratisFilesystems) != 0) { assertions = [ { assertion = config.boot.initrd.systemd.enable; @@ -36,25 +28,29 @@ in thin_metadata_size = "${pkgs."thin-provisioning-tools"}/bin/thin_metadata_size"; stratis-min = "${pkgs.stratisd}/bin/stratis-min"; }; - services = { - stratis-setup = { - description = "setup for Stratis root filesystem"; - unitConfig.DefaultDependencies = "no"; - conflicts = [ "shutdown.target" "initrd-switch-root.target" ]; - onFailure = [ "emergency.target" ]; - unitConfig.OnFailureJobMode = "isolate"; - wants = [ "stratisd-min.service" "plymouth-start.service" ]; - wantedBy = [ "initrd.target" ]; - after = [ "paths.target" "plymouth-start.service" "stratisd-min.service" ]; - before = [ "initrd.target" "shutdown.target" "initrd-switch-root.target" ]; - environment.STRATIS_ROOTFS_UUID = config.boot.stratis.rootPoolUuid; - serviceConfig = { - Type = "oneshot"; - ExecStart = "${pkgs.stratisd.initrd}/bin/stratis-rootfs-setup"; - RemainAfterExit = "yes"; - }; - }; - }; + services = + lib.attrsets.mapAttrs' ( + mountPoint: fileSystem: { + name = "stratis-setup-${fileSystem.stratis.poolUuid}"; + value = { + description = "setup for Stratis root filesystem"; + unitConfig.DefaultDependencies = "no"; + conflicts = [ "shutdown.target" "initrd-switch-root.target" ]; + onFailure = [ "emergency.target" ]; + unitConfig.OnFailureJobMode = "isolate"; + wants = [ "stratisd-min.service" "plymouth-start.service" ]; + wantedBy = [ "initrd.target" ]; + after = [ "paths.target" "plymouth-start.service" "stratisd-min.service" ]; + before = [ "initrd.target" "shutdown.target" "initrd-switch-root.target" ]; + environment.STRATIS_ROOTFS_UUID = fileSystem.stratis.poolUuid; + serviceConfig = { + Type = "oneshot"; + ExecStart = "${pkgs.stratisd.initrd}/bin/stratis-rootfs-setup"; + RemainAfterExit = "yes"; + }; + }; + } + ) requiredStratisFilesystems; }; availableKernelModules = [ "dm-thin-pool" "dm-crypt" ] ++ [ "aes" "aes_generic" "blowfish" "twofish" "serpent" "cbc" "xts" "lrw" "sha1" "sha256" "sha512" diff --git a/nixos/modules/tasks/filesystems.nix b/nixos/modules/tasks/filesystems.nix index 326862f836a5..2f032c3faf5c 100644 --- a/nixos/modules/tasks/filesystems.nix +++ b/nixos/modules/tasks/filesystems.nix @@ -36,6 +36,15 @@ let description = lib.mdDoc "Location of the mounted file system."; }; + stratis.poolUuid = lib.mkOption { + type = types.uniq (types.nullOr types.str); + description = lib.mdDoc '' + UUID of the stratis pool that the fs is located in + ''; + example = "04c68063-90a5-4235-b9dd-6180098a20d9"; + default = null; + }; + device = mkOption { default = null; example = "/dev/sda"; diff --git a/nixos/tests/installer.nix b/nixos/tests/installer.nix index 1346eb36c36e..d398924aa4a6 100644 --- a/nixos/tests/installer.nix +++ b/nixos/tests/installer.nix @@ -1038,12 +1038,6 @@ in { "mkdir -p /mnt/boot", "mount /dev/vda1 /mnt/boot" ) - - (header, pool_line) = machine.succeed("stratis pool list").splitlines() - index = header.find("UUID") - uuid = pool_line[index - 32: index + 4] - machine.succeed("mkdir -p /mnt/etc/nixos") - machine.succeed(f"printf %s {uuid} > /mnt/etc/nixos/rootPoolUuid.txt") ''; bootLoader = "systemd-boot"; extraInstallerConfig = { modulesPath, ...}: { @@ -1058,6 +1052,5 @@ in { ]; }; }; - extraConfig = "boot.stratis.rootPoolUuid = builtins.readFile ./rootPoolUuid.txt;"; }; } From 765349d3450b1e2b559d0d91bd58978c8effd938 Mon Sep 17 00:00:00 2001 From: Matthias Berndt Date: Fri, 19 May 2023 10:22:45 -0400 Subject: [PATCH 8/9] minor refactoring --- nixos/modules/system/boot/stratisroot.nix | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nixos/modules/system/boot/stratisroot.nix b/nixos/modules/system/boot/stratisroot.nix index f0d1b5475554..241d044db2fe 100644 --- a/nixos/modules/system/boot/stratisroot.nix +++ b/nixos/modules/system/boot/stratisroot.nix @@ -1,11 +1,10 @@ { config, lib, pkgs, utils, ... }: let - types = lib.types; requiredStratisFilesystems = lib.attrsets.filterAttrs (_: x: utils.fsNeededForBoot x && x.stratis.poolUuid != null) config.fileSystems; in { options = {}; - config = lib.mkIf (builtins.length (lib.attrsets.attrValues requiredStratisFilesystems) != 0) { + config = lib.mkIf (requiredStratisFilesystems != {}) { assertions = [ { assertion = config.boot.initrd.systemd.enable; From feb5a3cdcd523e7884a5ff4635bac63d632197b1 Mon Sep 17 00:00:00 2001 From: Matthias Berndt Date: Tue, 23 May 2023 14:56:49 +0200 Subject: [PATCH 9/9] remove unneeded import --- nixos/tests/installer.nix | 1 - 1 file changed, 1 deletion(-) diff --git a/nixos/tests/installer.nix b/nixos/tests/installer.nix index f51c631c96b3..1ac164f4b816 100644 --- a/nixos/tests/installer.nix +++ b/nixos/tests/installer.nix @@ -1013,7 +1013,6 @@ in { ''; bootLoader = "systemd-boot"; extraInstallerConfig = { modulesPath, ...}: { - imports = [ (modulesPath + "/tasks/stratis.nix") ]; config = { services.stratis.enable = true; environment.systemPackages = [