From ca0c35d813c2edd317cb6f1236cd6430aa6410dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charlotte=20=F0=9F=A6=9D=20Dele=C5=84kec?= Date: Tue, 24 Jun 2025 08:16:33 +0100 Subject: [PATCH] bcachefs-unlock: respect x-systemd mount options This will allow unlocking to take place *after* all of the devices have been probed, as indicated by the x-systemd.wants and x-systemd.requires options. This allows for multi-device bcachefs volumes to be reliably unlocked. --- doc/release-notes/rl-2511.section.md | 2 ++ nixos/modules/tasks/filesystems/bcachefs.nix | 12 ++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/doc/release-notes/rl-2511.section.md b/doc/release-notes/rl-2511.section.md index 3e906b17836b..a0c44875f200 100644 --- a/doc/release-notes/rl-2511.section.md +++ b/doc/release-notes/rl-2511.section.md @@ -37,6 +37,8 @@ - Added `rewriteURL` attribute to the nixpkgs `config`, to allow for rewriting the URLs downloaded by `fetchurl`. +- The systemd initrd will now respect `x-systemd.wants` and `x-systemd.requires` for reliably unlocking multi-disk bcachefs volumes. + - New hardening flags, `strictflexarrays1` and `strictflexarrays3` were made available, corresponding to the gcc/clang options `-fstrict-flex-arrays=1` and `-fstrict-flex-arrays=3` respectively. - `gramps` has been updated to 6.0.0 diff --git a/nixos/modules/tasks/filesystems/bcachefs.nix b/nixos/modules/tasks/filesystems/bcachefs.nix index 75a3fb21686c..1390b87f1711 100644 --- a/nixos/modules/tasks/filesystems/bcachefs.nix +++ b/nixos/modules/tasks/filesystems/bcachefs.nix @@ -93,19 +93,27 @@ let let mountUnit = "${utils.escapeSystemdPath (prefix + (lib.removeSuffix "/" fs.mountPoint))}.mount"; device = firstDevice fs; - deviceUnit = "${utils.escapeSystemdPath device}.device"; + mkDeviceUnit = device: "${utils.escapeSystemdPath device}.device"; + deviceUnit = mkDeviceUnit device; + extractProperty = + prop: options: (map (lib.removePrefix "${prop}=") (builtins.filter (lib.hasPrefix prop) options)); + normalizeUnits = unit: if lib.hasPrefix "/" unit then mkDeviceUnit unit else unit; + requiredUnits = map normalizeUnits (extractProperty "x-systemd.requires" fs.options); + wantedUnits = map normalizeUnits (extractProperty "x-systemd.wants" fs.options); in { name = "unlock-bcachefs-${utils.escapeSystemdPath fs.mountPoint}"; value = { description = "Unlock bcachefs for ${fs.mountPoint}"; requiredBy = [ mountUnit ]; - after = [ deviceUnit ]; + after = [ deviceUnit ] ++ requiredUnits ++ wantedUnits; before = [ mountUnit "shutdown.target" ]; bindsTo = [ deviceUnit ]; + requires = requiredUnits; + wants = wantedUnits; conflicts = [ "shutdown.target" ]; unitConfig.DefaultDependencies = false; serviceConfig = {