Skip to content

Commit

Permalink
Rework Hybrid MBR #168 (#508)
Browse files Browse the repository at this point in the history
Add hybrid MBR functionality to the gpt type
  • Loading branch information
danjujan committed Jan 26, 2024
1 parent c127198 commit f742462
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 3 deletions.
4 changes: 2 additions & 2 deletions disk-deactivate/disk-deactivate.jq
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def remove:
;

def deactivate:
if .type == "disk" then
if .type == "disk" or .type == "loop" then
[
# 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}')",
Expand Down Expand Up @@ -54,7 +54,7 @@ def deactivate:
"mdadm --stop \(.name)"
]
else
[]
["echo Warning: unknown type '\(.type)'. Consider handling this in https://github.com/nix-community/disko/blob/master/disk-deactivate/disk-deactivate.jq"]
end
;

Expand Down
47 changes: 47 additions & 0 deletions example/hybrid-mbr.nix
Original file line number Diff line number Diff line change
@@ -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 = "/";
};
};
};
};
};
};
};
}
4 changes: 3 additions & 1 deletion lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ let
"${dev}${toString index}" # /dev/md/raid1 style
else if match "/dev/mapper/.+" dev != null then
"${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
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
Expand Down
49 changes: 49 additions & 0 deletions lib/types/gpt.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{ config, options, lib, diskoLib, parent, device, ... }:
let
sortedPartitions = lib.sort (x: y: x.priority < y.priority) (lib.attrValues config.partitions);
sortedHybridPartitions = lib.filter (p: p.hybrid != null) sortedPartitions;
in
{
options = {
Expand Down Expand Up @@ -80,6 +81,35 @@ in
'';
};
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 {
internal = true;
default = diskoLib.indexOf (x: x.name == partition.config.name) sortedPartitions 0;
Expand All @@ -89,6 +119,11 @@ in
default = { };
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 {
internal = true;
default = parent;
Expand Down Expand Up @@ -122,6 +157,20 @@ in
${lib.optionalString (partition.content != null) partition.content._create}
'') 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 {
Expand Down
11 changes: 11 additions & 0 deletions tests/hybrid-mbr.nix
Original file line number Diff line number Diff line change
@@ -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 /");
'';
}

0 comments on commit f742462

Please sign in to comment.