From 4c397ea6de1d93dc90e8ff7757eaf3b47428b851 Mon Sep 17 00:00:00 2001 From: nikstur Date: Mon, 8 Apr 2024 16:36:28 +0200 Subject: [PATCH 1/2] systemd-lib: include GPTMaxLabelLength constant --- nixos/lib/systemd-lib.nix | 5 +++++ nixos/modules/system/boot/systemd/repart.nix | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/nixos/lib/systemd-lib.nix b/nixos/lib/systemd-lib.nix index 832160111da4..b67495609ff5 100644 --- a/nixos/lib/systemd-lib.nix +++ b/nixos/lib/systemd-lib.nix @@ -525,4 +525,9 @@ in rec { )} ''; + # The maximum number of characters allowed in a GPT partition label. This + # limit is specified by UEFI and enforced by systemd-repart. + # Corresponds to GPT_LABEL_MAX from systemd's gpt.h. + GPTMaxLabelLength = 36; + } diff --git a/nixos/modules/system/boot/systemd/repart.nix b/nixos/modules/system/boot/systemd/repart.nix index 6cc387cb6f43..7771f5fe0678 100644 --- a/nixos/modules/system/boot/systemd/repart.nix +++ b/nixos/modules/system/boot/systemd/repart.nix @@ -13,14 +13,14 @@ let partitionAssertions = lib.mapAttrsToList (fileName: definition: let - maxLabelLength = 36; # GPT_LABEL_MAX defined in systemd's gpt.h + inherit (utils.systemdUtils.lib) GPTMaxLabelLength; labelLength = builtins.stringLength definition.Label; in { - assertion = definition ? Label -> maxLabelLength >= labelLength; + assertion = definition ? Label -> GPTMaxLabelLength >= labelLength; message = '' The partition label '${definition.Label}' defined for '${fileName}' is ${toString labelLength} - characters long, but the maximum label length supported by systemd is ${toString maxLabelLength}. + characters long, but the maximum label length supported by systemd is ${toString GPTMaxLabelLength}. ''; } ) cfg.partitions; From ad19cee09cca703b4da9e084fffec2fab8b55f02 Mon Sep 17 00:00:00 2001 From: nikstur Date: Mon, 8 Apr 2024 16:12:31 +0200 Subject: [PATCH 2/2] nixos/image/repart: assert maximum label length The maximum label length is specified by UEFI and enforced/asserted by systemd-repart. This lets evaluation fail already and give the user some more information about what's wrong. Also warn when the suggested label length is exceeded. This serves as a safety mechanism for using systemd-sysupdate style A/B updates where the version number is encoded in the label and might not be incrementable when the maximum label size is reached. --- nixos/modules/image/repart.nix | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/nixos/modules/image/repart.nix b/nixos/modules/image/repart.nix index 1a43297f4b43..569d4a4b0021 100644 --- a/nixos/modules/image/repart.nix +++ b/nixos/modules/image/repart.nix @@ -6,6 +6,8 @@ let cfg = config.image.repart; + inherit (utils.systemdUtils.lib) GPTMaxLabelLength; + partitionOptions = { options = { storePaths = lib.mkOption { @@ -224,6 +226,42 @@ in config = { + assertions = lib.mapAttrsToList (fileName: partitionConfig: + let + inherit (partitionConfig) repartConfig; + labelLength = builtins.stringLength repartConfig.Label; + in + { + assertion = repartConfig ? Label -> GPTMaxLabelLength >= labelLength; + message = '' + The partition label '${repartConfig.Label}' + defined for '${fileName}' is ${toString labelLength} characters long, + but the maximum label length supported by UEFI is ${toString + GPTMaxLabelLength}. + ''; + } + ) cfg.partitions; + + warnings = lib.filter (v: v != null) (lib.mapAttrsToList (fileName: partitionConfig: + let + inherit (partitionConfig) repartConfig; + suggestedMaxLabelLength = GPTMaxLabelLength - 2; + labelLength = builtins.stringLength repartConfig.Label; + in + if (repartConfig ? Label && labelLength >= suggestedMaxLabelLength) then '' + The partition label '${repartConfig.Label}' + defined for '${fileName}' is ${toString labelLength} characters long. + The suggested maximum label length is ${toString + suggestedMaxLabelLength}. + + If you use sytemd-sysupdate style A/B updates, this might + not leave enough space to increment the version number included in + the label in a future release. For example, if your label is + ${toString GPTMaxLabelLength} characters long (the maximum enforced by UEFI) and + you're at version 9, you cannot increment this to 10. + '' else null + ) cfg.partitions); + image.repart = let version = config.image.repart.version;