Create partitions using individual commands

This commit is contained in:
Manuel Thalmann 2024-06-22 18:53:09 +02:00
parent 5b9a15b03e
commit 63aca41d06

View file

@ -61,14 +61,20 @@
(_: if ((!isSwap _) && (_.size == null)) then 1 else 0)
];
fdiskCommand = arguments: "sudo sfdisk ${arguments}";
fdiskScript = script: append: "echo ${script} | ${fdiskCommand "${if append then "--append" else ""} ${diskVar}"}";
wipeScript = script: fdiskScript script false;
appendScript = script: fdiskScript script true;
cleanup = lib.strings.concatLines (builtins.map
(partition: ''sudo sfdisk --delete ${diskVar} ${toString partition.index}'')
(partition: fdiskCommand "--delete ${diskVar} ${toString partition.index}")
(builtins.filter (_: !_.keepExisting) partitions));
fdiskCommands =
let
print = text: "echo ${text}";
in lib.strings.concatLines (
(lib.optional config.wipe (print "label: gpt")) ++
fdiskCommands = lib.strings.concatLines (
lib.optionals config.wipe [
cleanup
(wipeScript "label: gpt")
] ++
(builtins.concatMap (
partition:
let
@ -83,19 +89,20 @@
${sizeScript} | sed -e "s/.*[^[:space:]]/size=\0/"
'';
config = print "${toString index}: \"$(${sizeOption})\" type=${lib.strings.escapeShellArg type}";
create = appendScript "${toString index}: \"$(${sizeOption})\" type=${lib.strings.escapeShellArg type}";
fallback = ''
if ! { ls "${diskVar}"?(p)${toString partition.index} 2>&1; } > /dev/null
then
${config}
${create}
fi
'';
in [
(if keepExisting then fallback else config)
(if keepExisting then fallback else create)
])
partitions)
);
fixup = lib.strings.concatLines (
fixType = lib.strings.concatLines (
builtins.concatMap
(partition: lib.optional
partition.keepExisting
@ -113,12 +120,8 @@
${if (!config.wipe) then cleanup else ""}
function input() {
${fdiskCommands}
}
input | tee /dev/stderr | sudo sfdisk ${if !config.wipe then "--append" else ""} "${diskVar}";
${fixup}
${fixType}
}
partition