Add hybrid MBR functionality to the gpt type
This commit is contained in:
danjujan 2024-01-26 21:59:23 +01:00 committed by GitHub
parent c12719812d
commit f7424625dc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 112 additions and 3 deletions

View file

@ -24,7 +24,7 @@ def remove:
; ;
def deactivate: def deactivate:
if .type == "disk" then if .type == "disk" or .type == "loop" then
[ [
# If this disk is a member of raid, stop that raid # If this disk is a member of raid, stop that raid
"md_dev=$(lsblk \(.path) -l -p -o type,name | awk 'match($1,\"raid.*\") {print $2}')", "md_dev=$(lsblk \(.path) -l -p -o type,name | awk 'match($1,\"raid.*\") {print $2}')",
@ -54,7 +54,7 @@ def deactivate:
"mdadm --stop \(.name)" "mdadm --stop \(.name)"
] ]
else else
[] ["echo Warning: unknown type '\(.type)'. Consider handling this in https://github.com/nix-community/disko/blob/master/disk-deactivate/disk-deactivate.jq"]
end end
; ;

47
example/hybrid-mbr.nix Normal file
View file

@ -0,0 +1,47 @@
{disks ? ["/dev/sda"], ...}: {
disko.devices = {
disk = {
main = {
type = "disk";
device = builtins.elemAt disks 0;
content = {
type = "gpt";
efiGptPartitionFirst = false;
partitions = {
TOW-BOOT-FI = {
priority = 1;
type = "EF00";
size = "32M";
content = {
type = "filesystem";
format = "vfat";
mountpoint = null;
};
hybrid = {
mbrPartitionType = "0x0c";
mbrBootableFlag = false;
};
};
ESP = {
type = "EF00";
size = "512M";
content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot";
};
};
root = {
size = "100%";
content = {
type = "filesystem";
format = "ext4";
mountpoint = "/";
};
};
};
};
};
};
};
}

View file

@ -84,9 +84,11 @@ let
"${dev}${toString index}" # /dev/md/raid1 style "${dev}${toString index}" # /dev/md/raid1 style
else if match "/dev/mapper/.+" dev != null then else if match "/dev/mapper/.+" dev != null then
"${dev}${toString index}" # /dev/mapper/vg-lv1 style "${dev}${toString index}" # /dev/mapper/vg-lv1 style
else if match "/dev/loop[[:digit:]]+" dev != null
then "${dev}p${toString index}" # /dev/mapper/vg-lv1 style
else else
abort '' abort ''
${dev} seems not to be a supported disk format. Please add this to disko in https://github.com/nix-community/disko/blob/master/types/default.nix ${dev} seems not to be a supported disk format. Please add this to disko in https://github.com/nix-community/disko/blob/master/lib/default.nix
''; '';
/* get the index an item in a list /* get the index an item in a list

View file

@ -1,6 +1,7 @@
{ config, options, lib, diskoLib, parent, device, ... }: { config, options, lib, diskoLib, parent, device, ... }:
let let
sortedPartitions = lib.sort (x: y: x.priority < y.priority) (lib.attrValues config.partitions); sortedPartitions = lib.sort (x: y: x.priority < y.priority) (lib.attrValues config.partitions);
sortedHybridPartitions = lib.filter (p: p.hybrid != null) sortedPartitions;
in in
{ {
options = { options = {
@ -80,6 +81,35 @@ in
''; '';
}; };
content = diskoLib.partitionType { parent = config; device = partition.config.device; }; content = diskoLib.partitionType { parent = config; device = partition.config.device; };
hybrid = lib.mkOption {
type = lib.types.nullOr (lib.types.submodule ({name, ...} @ hp: {
options = {
mbrPartitionType = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "MBR type code";
};
mbrBootableFlag = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Set the bootable flag (aka the active flag) on any or all of your hybridized partitions";
};
_create = diskoLib.mkCreateOption {
inherit config options;
default = ''
${lib.optionalString (hp.config.mbrPartitionType != null) ''
sfdisk --label-nested dos --part-type ${parent.device} ${(toString partition.config._index)} ${hp.config.mbrPartitionType}
''}
${lib.optionalString hp.config.mbrBootableFlag ''
sfdisk --label-nested dos --activate ${parent.device} ${(toString partition.config._index)}
''}
'';
};
};
}));
default = null;
description = "Entry to add to the Hybrid MBR table";
};
_index = lib.mkOption { _index = lib.mkOption {
internal = true; internal = true;
default = diskoLib.indexOf (x: x.name == partition.config.name) sortedPartitions 0; default = diskoLib.indexOf (x: x.name == partition.config.name) sortedPartitions 0;
@ -89,6 +119,11 @@ in
default = { }; default = { };
description = "Attrs of partitions to add to the partition table"; description = "Attrs of partitions to add to the partition table";
}; };
efiGptPartitionFirst = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Place EFI GPT (0xEE) partition first in MBR (good for GRUB)";
};
_parent = lib.mkOption { _parent = lib.mkOption {
internal = true; internal = true;
default = parent; default = parent;
@ -122,6 +157,20 @@ in
${lib.optionalString (partition.content != null) partition.content._create} ${lib.optionalString (partition.content != null) partition.content._create}
'') sortedPartitions)} '') sortedPartitions)}
${
lib.optionalString (sortedHybridPartitions != [])
("sgdisk -h "
+ (lib.concatStringsSep ":" (map (p: (toString p._index)) sortedHybridPartitions))
+ (
lib.optionalString (!config.efiGptPartitionFirst) ":EE "
)
+ parent.device)
}
${lib.concatMapStrings (p:
p.hybrid._create
)
sortedHybridPartitions}
''; '';
}; };
_mount = diskoLib.mkMountOption { _mount = diskoLib.mkMountOption {

11
tests/hybrid-mbr.nix Normal file
View file

@ -0,0 +1,11 @@
{ pkgs ? import <nixpkgs> { }
, diskoLib ? pkgs.callPackage ../lib { }
}:
diskoLib.testLib.makeDiskoTest {
inherit pkgs;
name = "hybrid-mbr";
disko-config = ../example/hybrid-mbr.nix;
extraTestScript = ''
machine.succeed("mountpoint /");
'';
}