Add configration option for partition UUIDs

This commit is contained in:
Rua authored and Jörg Thalheim committed 2025-02-15 01:27:47 +00:00
1 parent e51159153b
commit 54be8abff0
1 file changed
+43 -21
+43 -21
View File
@@ -37,13 +37,18 @@ in
device = lib.mkOption { device = lib.mkOption {
type = lib.types.str; type = lib.types.str;
default = default =
if config._parent.type == "mdadm" then if partition.config.uuid != null then
# workaround because mdadm partlabel do not appear in /dev/disk/by-partlabel "/dev/disk/by-partuuid/${diskoLib.hexEscapeUdevSymlink partition.config.uuid}"
else if config._parent.type == "mdadm" then
# workaround because mdadm partlabel do not appear in /dev/disk/by-partlabel
"/dev/disk/by-id/md-name-any:${config._parent.name}-part${toString partition.config._index}" "/dev/disk/by-id/md-name-any:${config._parent.name}-part${toString partition.config._index}"
else else
"/dev/disk/by-partlabel/${diskoLib.hexEscapeUdevSymlink partition.config.label}"; "/dev/disk/by-partlabel/${diskoLib.hexEscapeUdevSymlink partition.config.label}";
defaultText = '' defaultText = ''
if the parent is an mdadm device: if `uuid` is provided:
/dev/disk/by-partuuid/''${diskoLib.hexEscapeUdevSymlink partition.config.uuid}
otherwise, if the parent is an mdadm device:
/dev/disk/by-id/md-name-any:''${config._parent.name}-part''${toString partition.config._index} /dev/disk/by-id/md-name-any:''${config._parent.name}-part''${toString partition.config._index}
otherwise: otherwise:
@@ -74,6 +79,16 @@ in
description = "Name of the partition"; description = "Name of the partition";
default = name; default = name;
}; };
uuid = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
example = "809b3a2b-828a-4730-95e1-75b6343e415a";
description = ''
The UUID (also known as GUID) of the partition. Note that this is distinct from the UUID of the filesystem.
If none is provided, a random UUID will be generated when creating the partition.
'';
};
label = lib.mkOption { label = lib.mkOption {
type = lib.types.str; type = lib.types.str;
default = default =
@@ -195,24 +210,31 @@ in
if ! blkid "${config.device}" >&2; then if ! blkid "${config.device}" >&2; then
sgdisk --clear "${config.device}" sgdisk --clear "${config.device}"
fi fi
${lib.concatStrings (map (partition: '' ${lib.concatStrings (map (partition:
# try to create the partition, if it fails, try to change the type and name let
if ! sgdisk \ args = ''
--align-end ${lib.optionalString (partition.alignment != 0) ''--set-alignment=${builtins.toString partition.alignment}''} \ --partition-guid="${toString partition._index}:${if partition.uuid == null then "R" else partition.uuid}" \
--new=${toString partition._index}:${partition.start}:${partition.end} \ --change-name="${toString partition._index}:${partition.label}" \
--change-name="${toString partition._index}:${partition.label}" \ --typecode=${toString partition._index}:${partition.type} \
--typecode=${toString partition._index}:${partition.type} \ "${config.device}" \
"${config.device}" '';
then sgdisk \ createArgs = ''
--change-name="${toString partition._index}:${partition.label}" \ --align-end ${lib.optionalString (partition.alignment != 0) ''--set-alignment=${builtins.toString partition.alignment}''} \
--typecode=${toString partition._index}:${partition.type} \ --new=${toString partition._index}:${partition.start}:${partition.end} \
"${config.device}" '';
fi in
# ensure /dev/disk/by-path/..-partN exists before continuing ''
partprobe "${config.device}" || : # sometimes partprobe fails, but the partitions are still up2date # try to create the partition, if it fails, try to change the type and name
udevadm trigger --subsystem-match=block if ! sgdisk ${createArgs} ${args}
udevadm settle then
'') sortedPartitions)} sgdisk ${args}
fi
# ensure /dev/disk/by-path/..-partN exists before continuing
partprobe "${config.device}" || : # sometimes partprobe fails, but the partitions are still up2date
udevadm trigger --subsystem-match=block
udevadm settle
''
) sortedPartitions)}
${ ${
lib.optionalString (sortedHybridPartitions != []) lib.optionalString (sortedHybridPartitions != [])