idempotent gpt

This commit is contained in:
lassulus 2024-04-07 12:47:17 +02:00
parent 0a17298c0d
commit 89b617b99d

View file

@ -153,23 +153,26 @@ in
_create = diskoLib.mkCreateOption {
inherit config options;
default = ''
if ! blkid "${config.device}" >/dev/null; then
if ! blkid "${config.device}" >&2; then
sgdisk --clear ${config.device}
fi
${lib.concatStrings (map (partition: ''
if sgdisk \
--info=${toString partition._index} \
${config.device} > /dev/null 2>&1
then
sgdisk \
# try to create the partition, if it fails, try to change the type and name
if ! sgdisk \
--align-end \
--new=${toString partition._index}:${partition.start}:${partition.end} \
--change-name=${toString partition._index}:${partition.label} \
--typecode=${toString partition._index}:${partition.type} \
${config.device}
then sgdisk \
--change-name=${toString partition._index}:${partition.label} \
--typecode=${toString partition._index}:${partition.type} \
${config.device}
fi
# ensure /dev/disk/by-path/..-partN exists before continuing
partprobe ${config.device}
partprobe ${config.device} || : # sometimes partprobe fails, but the partitions are still up2date
udevadm trigger --subsystem-match=block
udevadm settle
fi
'') sortedPartitions)}
${
@ -186,7 +189,6 @@ in
)
sortedHybridPartitions
}
fi
${lib.concatStrings (map (partition: ''
${lib.optionalString (partition.content != null) partition.content._create}