git.iko.soy/typeable/nixpkgs
on master help sign in
grub: Add `boot.loader.grub.extraGrubInstallArgs` option.

Useful for when you need to build grub modules into your grub kernel to get a working boot, as shown in the added example. To store this new value, we switch to more structural JSON approach. Using one line per value to store in `/boot/grub/state` gets really messy when the values are arrays, or even worse, can contain newlines (escaping would be needed). Further, removing a value from the file would get extra messy (empty lines we'd have to keep for backwards compatibility). Thus, from now on we use JSON to store all values we'll need in the future.

commit8665b5ab918607f5775061682874c70a54f17e55 authorNiklas Hambüchen <mail@nh2.me> dateThu 23 Apr 2020, 22:44 (7 years ago) parent81c1574
2 files changed+61−6
nixos/modules/system/boot/loader/grub/grub.nix+29−1 nixos/modules/system/boot/loader/grub/install-grub.pl+32−5
nixos/modules/system/boot/loader/grub/grub.nix+29−1
@@ -61,6 +61,7 @@ let
6161 inherit (efi) canTouchEfiVariables;
6262 inherit (cfg)
6363 version extraConfig extraPerEntryConfig extraEntries forceInstall useOSProber
64+ extraGrubInstallArgs
6465 extraEntriesBeforeNixOS extraPrepareConfig configurationLimit copyKernels
6566 default fsIdentifier efiSupport efiInstallAsRemovable gfxmodeEfi gfxmodeBios gfxpayloadEfi gfxpayloadBios;
6667 path = with pkgs; makeBinPath (
@@ -298,6 +299,33 @@ in
298299 '';
299300 };
300301
302+ extraGrubInstallArgs = mkOption {
303+ default = [ ];
304+ example = [ "--modules=nativedisk ahci pata part_gpt part_msdos diskfilter mdraid1x lvm ext2" ];
305+ type = types.listOf types.str;
306+ description = ''
307+ Additional arguments passed to <literal>grub-install</literal>.
308+
309+ A use case for this is to build specific GRUB2 modules
310+ directly into the GRUB2 kernel image, so that they are available
311+ and activated even in the <literal>grub rescue</literal> shell.
312+
313+ They are also necessary when the BIOS/UEFI is bugged and cannot
314+ correctly read large disks (e.g. above 2 TB), so GRUB2's own
315+ <literal>nativedisk</literal> and related modules can be used
316+ to use its own disk drivers. The example shows one such case.
317+ This is also useful for booting from USB.
318+ See the
319+ <link xlink:href="http://git.savannah.gnu.org/cgit/grub.git/tree/grub-core/commands/nativedisk.c?h=grub-2.04#n326">
320+ GRUB source code
321+ </link>
322+ for which disk modules are available.
323+
324+ The list elements are passed directly as <literal>argv</literal>
325+ arguments to the <literal>grub-install</literal> program, in order.
326+ '';
327+ };
328+
301329 extraPerEntryConfig = mkOption {
302330 default = "";
303331 example = "root (hd0)";
@@ -669,7 +697,7 @@ in
669697 in pkgs.writeScript "install-grub.sh" (''
670698 #!${pkgs.runtimeShell}
671699 set -e
672− export PERL5LIB=${with pkgs.perlPackages; makePerlPath [ FileSlurp XMLLibXML XMLSAX XMLSAXBase ListCompare ]}
700+ export PERL5LIB=${with pkgs.perlPackages; makePerlPath [ FileSlurp XMLLibXML XMLSAX XMLSAXBase ListCompare JSON ]}
673701 ${optionalString cfg.enableCryptodisk "export GRUB_ENABLE_CRYPTODISK=y"}
674702 '' + flip concatMapStrings cfg.mirroredBoots (args: ''
675703 ${pkgs.perl}/bin/perl ${install-grub-pl} ${grubConfig args} $@
nixos/modules/system/boot/loader/grub/install-grub.pl+32−5
@@ -8,6 +8,7 @@ use File::stat;
88use File::Copy;
99use File::Slurp;
1010use File::Temp;
11+use JSON;
1112require List::Compare;
1213use POSIX;
1314use Cwd;
@@ -606,9 +607,12 @@ struct(GrubState => {
606607 efi => '$',
607608 devices => '$',
608609 efiMountPoint => '$',
610+ extraGrubInstallArgs => '@',
609611});
612+# If you add something to the state file, only add it to the end
613+# because it is read line-by-line.
610614sub readGrubState {
611− my $defaultGrubState = GrubState->new(name => "", version => "", efi => "", devices => "", efiMountPoint => "" );
615+ my $defaultGrubState = GrubState->new(name => "", version => "", efi => "", devices => "", efiMountPoint => "", extraGrubInstallArgs => () );
612616 open FILE, "<$bootPath/grub/state" or return $defaultGrubState;
613617 local $/ = "\n";
614618 my $name = <FILE>;
@@ -621,16 +625,34 @@ sub readGrubState {
621625 chomp($devices);
622626 my $efiMountPoint = <FILE>;
623627 chomp($efiMountPoint);
628+ # Historically, arguments in the state file were one per each line, but that
629+ # gets really messy when newlines are involved, structured arguments
630+ # like lists are needed (they have to have a separator encoding), or even worse,
631+ # when we need to remove a setting in the future. Thus, the 6th line is a JSON
632+ # object that can store structured data, with named keys, and all new state
633+ # should go in there.
634+ my $jsonStateLine = <FILE>;
635+ # For historical reasons we do not check the values above for un-definedness
636+ # (that is, when the state file has too few lines and EOF is reached),
637+ # because the above come from the first version of this logic and are thus
638+ # guaranteed to be present.
639+ $jsonStateLine = defined $jsonStateLine ? $jsonStateLine : '{}'; # empty JSON object
640+ chomp($jsonStateLine);
641+ my %jsonState = %{decode_json($jsonStateLine)};
642+ my @extraGrubInstallArgs = @{$jsonState{'extraGrubInstallArgs'}};
624643 close FILE;
625− my $grubState = GrubState->new(name => $name, version => $version, efi => $efi, devices => $devices, efiMountPoint => $efiMountPoint );
644+ my $grubState = GrubState->new(name => $name, version => $version, efi => $efi, devices => $devices, efiMountPoint => $efiMountPoint, extraGrubInstallArgs => \@extraGrubInstallArgs );
626645 return $grubState
627646}
628647
629648my @deviceTargets = getList('devices');
630649my $prevGrubState = readGrubState();
631650my @prevDeviceTargets = split/,/, $prevGrubState->devices;
651+my @extraGrubInstallArgs = getList('extraGrubInstallArgs');
652+my @prevExtraGrubInstallArgs = $prevGrubState->extraGrubInstallArgs;
632653
633654my $devicesDiffer = scalar (List::Compare->new( '-u', '-a', \@deviceTargets, \@prevDeviceTargets)->get_symmetric_difference());
655+my $extraGrubInstallArgsDiffer = scalar (List::Compare->new( '-u', '-a', \@extraGrubInstallArgs, \@prevExtraGrubInstallArgs)->get_symmetric_difference());
634656my $nameDiffer = get("fullName") ne $prevGrubState->name;
635657my $versionDiffer = get("fullVersion") ne $prevGrubState->version;
636658my $efiDiffer = $efiTarget ne $prevGrubState->efi;
@@ -639,7 +661,7 @@ if (($ENV{'NIXOS_INSTALL_GRUB'} // "") eq "1") {
639661 warn "NIXOS_INSTALL_GRUB env var deprecated, use NIXOS_INSTALL_BOOTLOADER";
640662 $ENV{'NIXOS_INSTALL_BOOTLOADER'} = "1";
641663}
642−my $requireNewInstall = $devicesDiffer || $nameDiffer || $versionDiffer || $efiDiffer || $efiMountPointDiffer || (($ENV{'NIXOS_INSTALL_BOOTLOADER'} // "") eq "1");
664+my $requireNewInstall = $devicesDiffer || $extraGrubInstallArgsDiffer || $nameDiffer || $versionDiffer || $efiDiffer || $efiMountPointDiffer || (($ENV{'NIXOS_INSTALL_BOOTLOADER'} // "") eq "1");
643665
644666# install a symlink so that grub can detect the boot drive
645667my $tmpDir = File::Temp::tempdir(CLEANUP => 1) or die "Failed to create temporary space";
@@ -650,7 +672,7 @@ if (($requireNewInstall != 0) && ($efiTarget eq "no" || $efiTarget eq "both")) {
650672 foreach my $dev (@deviceTargets) {
651673 next if $dev eq "nodev";
652674 print STDERR "installing the GRUB $grubVersion boot loader on $dev...\n";
653− my @command = ("$grub/sbin/grub-install", "--recheck", "--root-directory=$tmpDir", Cwd::abs_path($dev));
675+ my @command = ("$grub/sbin/grub-install", "--recheck", "--root-directory=$tmpDir", Cwd::abs_path($dev), @extraGrubInstallArgs);
654676 if ($forceInstall eq "true") {
655677 push @command, "--force";
656678 }
@@ -665,7 +687,7 @@ if (($requireNewInstall != 0) && ($efiTarget eq "no" || $efiTarget eq "both")) {
665687# install EFI GRUB
666688if (($requireNewInstall != 0) && ($efiTarget eq "only" || $efiTarget eq "both")) {
667689 print STDERR "installing the GRUB $grubVersion EFI boot loader into $efiSysMountPoint...\n";
668− my @command = ("$grubEfi/sbin/grub-install", "--recheck", "--target=$grubTargetEfi", "--boot-directory=$bootPath", "--efi-directory=$efiSysMountPoint");
690+ my @command = ("$grubEfi/sbin/grub-install", "--recheck", "--target=$grubTargetEfi", "--boot-directory=$bootPath", "--efi-directory=$efiSysMountPoint", @extraGrubInstallArgs);
669691 if ($forceInstall eq "true") {
670692 push @command, "--force";
671693 }
@@ -692,6 +714,11 @@ if ($requireNewInstall != 0) {
692714 print FILE $efiTarget, "\n" or die;
693715 print FILE join( ",", @deviceTargets ), "\n" or die;
694716 print FILE $efiSysMountPoint, "\n" or die;
717+ my %jsonState = (
718+ extraGrubInstallArgs => \@extraGrubInstallArgs
719+ );
720+ my $jsonStateLine = encode_json(\%jsonState);
721+ print FILE $jsonStateLine, "\n" or die;
695722 close FILE or die;
696723
697724 # Atomically switch to the new state file