Demi Marie Obenour <demiobenour@gmail.com> writes:
On 11/28/25 16:14, Alyssa Ross wrote:
Demi Marie Obenour <demiobenour@gmail.com> writes:
On 11/28/25 08:22, Alyssa Ross wrote:
We want non-minimally-sized partitions to leave space for updates. All such partitions will also be labelled, so we can just add another optional field at the end.
Since we don't parse partition specifications in sh, we can't keep a running total any more, so instead we just go through the table at the end and add up all the sizes, taking advantage of our knowledge that the size will always be the last thing in each line in our tables.
Signed-off-by: Alyssa Ross <hi@alyssa.is> Message-ID: <20251127174054.2056835-2-hi@alyssa.is> --- v2: fix conflicts after applying Demi's optimization, and avoid awk to sum partition sizes, taking inspiration from the same. v1: https://spectrum-os.org/lists/archives/spectrum-devel/20251127174054.2056835...
scripts/make-gpt.sh | 30 ++++++++++++++++++------------ scripts/sfdisk-field.awk | 22 +++++++++++++++++----- 2 files changed, 35 insertions(+), 17 deletions(-)
diff --git a/scripts/make-gpt.sh b/scripts/make-gpt.sh index 3cae441..cb19868 100755 --- a/scripts/make-gpt.sh +++ b/scripts/make-gpt.sh @@ -1,10 +1,10 @@ #!/bin/sh -eu # -# SPDX-FileCopyrightText: 2021-2023 Alyssa Ross <hi@alyssa.is> +# SPDX-FileCopyrightText: 2021-2023, 2025 Alyssa Ross <hi@alyssa.is> # SPDX-FileCopyrightText: 2022 Unikie # SPDX-License-Identifier: EUPL-1.2+ # -# usage: make-gpt.sh GPT_PATH PATH:PARTTYPE[:PARTUUID[:PARTLABEL]]... +# usage: make-gpt.sh GPT_PATH PATH:PARTTYPE[:PARTUUID[:PARTLABEL[:PARTMiB]]]...
ONE_MiB=1048576
@@ -33,21 +33,27 @@ scriptsDir="$(dirname "$0")" out="$1" shift
-nl=' -' -table="label: gpt" +table=$(for partition; do + awk -f "$scriptsDir/sfdisk-field.awk" \ + -v partition="$partition" \ + -v size="$(sizeMiB "${partition%%:*}")" +done)
$(sizeMiB) should be moved into a separate command. Shellcheck can catch this if you enable SC2312.
Good catch, thanks. Feel free to suggest other optional shellcheck things we should enable. (Although I've just noticed release/checks/shellcheck.nix doesn't seem to be reading the .shellcheckrc file properly so don't use that for testing.)
I'll look later.
# Keep 1MiB free at the start, and 1MiB free at the end. -gptBytes=$((ONE_MiB * 2)) -for partition; do - sizeMiB="$(sizeMiB "${partition%%:*}")" - table="$table${nl}size=${sizeMiB}MiB,$(awk -f "$scriptsDir/sfdisk-field.awk" -v partition="$partition")" - gptBytes="$((gptBytes + sizeMiB * ONE_MiB))" -done +gptMiB=2 +while read -r partition; do + # Here we rely on sfdisk-field.awk always putting size last. + : $((gptMiB += ${partition##*=})) +done <<EOF +$table +EOF
Nit: should ${partition##*=} be in double quotes?
I tried changing it, but it is apparently an "arithmetic syntax error" to use double quotes here.
I'd use a separate variable then.
Doesn't seem to make a difference. As an experiment: partition="size=2 + 3" size=${partition##*=} printf "%s\n" $(($size)) This prints 5 even though a separate variable is used. It doesn't seem to be possible to get $(( … )) *not* to interpret arithmetic in variables. In bash, but not busybox ash as used in Nix builds, it is possible to use double quotes like you suggested, but $(("$size")) is /still/ 5.