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