Skip to content

fix: Use and configure grub for mirrored efi partitions #1022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions example/btrfs-raid-mirrored-boot.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
disko.devices.disk = {
one = {
type = "disk";
device = "/dev/vda";

content = {
type = "gpt";

partitions = {
boot = {
size = "1M";
type = "EF02";
};

esp = {
size = "1G";
type = "EF00";

content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot0";
mountOptions = [ "umask=0077" ];
};
};

root = {
size = "100%";

content = {
type = "btrfs";
};
};
};
};
};

two = {
type = "disk";
device = "/dev/vdb";

content = {
type = "gpt";

partitions = {
boot = {
size = "1M";
type = "EF02";
};

esp = {
size = "1G";
type = "EF00";

content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot1";
mountOptions = [ "umask=0077" ];
};
};

root = {
size = "100%";

content = {
type = "btrfs";
mountpoint = "/";
extraArgs = [
"-f"
"-d raid1"
"/dev/disk/by-partlabel/disk-one-root"
];
};
};
};
};
};
};
}
38 changes: 31 additions & 7 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1033,13 +1033,37 @@ let
'';
default =
with lib;
let
configKeys = flatten (
map attrNames (flatten (map (dev: dev._config) (flatten (map attrValues (attrValues devices)))))
);
collectedConfigs = flatten (map (dev: dev._config) (flatten (map attrValues (attrValues devices))));
in
genAttrs configKeys (key: mkMerge (catAttrs key collectedConfigs));
(
let
configKeys = flatten (
map attrNames (flatten (map (dev: dev._config) (flatten (map attrValues (attrValues devices)))))
);
collectedConfigs = flatten (map (dev: dev._config) (flatten (map attrValues (attrValues devices))));
in
genAttrs configKeys (key: mkMerge (catAttrs key collectedConfigs))
)
// (
let
efi_partitions = builtins.filter (part: part.type == "EF00") (
flatten (
map (disk: map (part: part // { device = disk.device; }) (attrValues disk.content.partitions)) (
flatten (map attrValues (attrValues devices))
)
)
);
in
(optionalAttrs (builtins.length efi_partitions >= 2) {
# Mirrored boot partitions are not supported on systemd-boot.
boot.loader.grub.enable = mkForce true;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be an assertion for system.boot.loader.id == "grub", but I'm not sure how to access config here.

boot.loader.grub.devices = mkForce [ ];
boot.loader.grub.mirroredBoots = mkForce (
map (part: {
devices = [ part.device ];
path = part.content.mountpoint;
}) efi_partitions
);
})
);
};
};
}
Expand Down
2 changes: 2 additions & 0 deletions lib/make-disk-image.nix
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ let
disko.testMode = true;
disko.devices = lib.mkForce cleanedConfig.disko.devices;
boot.loader.grub.devices = lib.mkForce cleanedConfig.boot.loader.grub.devices;
boot.loader.grub.mirroredBoots = lib.mkForce cleanedConfig.boot.loader.grub.mirroredBoots;
}
];
};
Expand All @@ -73,6 +74,7 @@ let
disko.testMode = true;
disko.devices = lib.mkForce cleanedConfig.disko.devices;
boot.loader.grub.devices = lib.mkForce cleanedConfig.boot.loader.grub.devices;
boot.loader.grub.mirroredBoots = lib.mkForce cleanedConfig.boot.loader.grub.mirroredBoots;
nixpkgs.hostPlatform = lib.mkForce pkgs.stdenv.hostPlatform;
nixpkgs.buildPlatform = lib.mkForce pkgs.stdenv.hostPlatform;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ let
boot.zfs.devNodes = "/dev/disk/by-uuid"; # needed because /dev/disk/by-id is empty in qemu-vms

# grub will install to these devices, we need to force those or we are offset by 1
# we use mkOveride 70, so that users can override this with mkForce in case they are testing grub mirrored boots
# we use mkOverride 70, so that users can override this with mkForce in case they are testing grub mirrored boots
boot.loader.grub.devices = lib.mkOverride 70 testConfigInstall.boot.loader.grub.devices;

assertions = [
Expand Down
16 changes: 16 additions & 0 deletions tests/btrfs-raid-mirrored-boot.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
pkgs ? import <nixpkgs> { },
diskoLib ? pkgs.callPackage ../lib { },
}:
diskoLib.testLib.makeDiskoTest {
inherit pkgs;
name = "btrfs-raid-mirrored-boot";
disko-config = ../example/btrfs-raid-mirrored-boot.nix;
extraTestScript = ''
machine.succeed("mountpoint /");
'';
extraSystemConfig = {
# Mirrored boot partitions are not supported on systemd-boot.
boot.loader.systemd-boot.enable = false;
};
}