From eccc1e5bf482491187e914a4c37ba45a5de56703 Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Sun, 15 Jan 2023 02:31:15 +0100 Subject: [PATCH] install-grub.pl: improve initrd-secrets error messages The build of initrd-secrets can routinely fail for old boot entries if the secrets have been removed or renamed in a later generation. This always happens for generation 1, because it's built from the NixOS installer and the paths differs by the mount point (i.e. /mnt). The error is very confusing because it fails to mention it's about an older generation and that it's somewhat harmless. This commit turns the error into a warning for all generations but the current, adds the name of the failed entry to the message and a note explaining why it can happen. --- .../system/boot/loader/grub/install-grub.pl | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/nixos/modules/system/boot/loader/grub/install-grub.pl b/nixos/modules/system/boot/loader/grub/install-grub.pl index 20d48cde4ca..205f1513fd9 100644 --- a/nixos/modules/system/boot/loader/grub/install-grub.pl +++ b/nixos/modules/system/boot/loader/grub/install-grub.pl @@ -442,7 +442,7 @@ sub copyToKernelsDir { } sub addEntry { - my ($name, $path, $options) = @_; + my ($name, $path, $options, $current) = @_; return unless -e "$path/kernel" && -e "$path/initrd"; my $kernel = copyToKernelsDir(Cwd::abs_path("$path/kernel")); @@ -458,7 +458,14 @@ sub addEntry { # Make sure initrd is not world readable (won't work if /boot is FAT) umask 0137; my $initrdSecretsPathTemp = File::Temp::mktemp("$initrdSecretsPath.XXXXXXXX"); - system("$path/append-initrd-secrets", $initrdSecretsPathTemp) == 0 or die "failed to create initrd secrets: $!\n"; + if (system("$path/append-initrd-secrets", $initrdSecretsPathTemp) != 0) { + if ($current) { + die "failed to create initrd secrets $!\n"; + } else { + say STDERR "warning: failed to create initrd secrets for \"$name\", an older generation"; + say STDERR "note: this is normal after having removed or renamed a file in `boot.initrd.secrets`"; + } + } # Check whether any secrets were actually added if (-e $initrdSecretsPathTemp && ! -z _) { rename $initrdSecretsPathTemp, $initrdSecretsPath or die "failed to move initrd secrets into place: $!\n"; @@ -491,7 +498,7 @@ sub addEntry { } $conf .= "\n"; } else { - $conf .= "menuentry \"$name\" " . ($options||"") . " {\n"; + $conf .= "menuentry \"$name\" " . $options . " {\n"; if ($saveDefault) { $conf .= " savedefault\n"; } @@ -511,7 +518,7 @@ sub addEntry { # Add default entries. $conf .= "$extraEntries\n" if $extraEntriesBeforeNixOS; -addEntry("@distroName@ - Default", $defaultConfig, $entryOptions); +addEntry("@distroName@ - Default", $defaultConfig, $entryOptions, 1); $conf .= "$extraEntries\n" unless $extraEntriesBeforeNixOS; @@ -536,7 +543,7 @@ foreach my $link (@links) { my $linkname = basename($link); $entryName = "($linkname - $date - $version)"; } - addEntry("@distroName@ - $entryName", $link); + addEntry("@distroName@ - $entryName", $link, "", 1); } my $grubBootPath = $grubBoot->path; @@ -568,7 +575,7 @@ sub addProfile { -e "$link/nixos-version" ? readFile("$link/nixos-version") : basename((glob(dirname(Cwd::abs_path("$link/kernel")) . "/lib/modules/*"))[0]); - addEntry("@distroName@ - Configuration " . nrFromGen($link) . " ($date - $version)", $link, $subEntryOptions); + addEntry("@distroName@ - Configuration " . nrFromGen($link) . " ($date - $version)", $link, $subEntryOptions, 0); } $conf .= "}\n" if $grubVersion == 2;