[PATCH 1/2] scripts/make-gpt.sh: set -o pipefail
Wasn't POSIX at the time this script was originally written! Suggested-by: Demi Marie Obenour <demiobenour@gmail.com> Link: https://spectrum-os.org/lists/archives/spectrum-devel/20251127174054.2056835... Signed-off-by: Alyssa Ross <hi@alyssa.is> --- scripts/make-gpt.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/make-gpt.sh b/scripts/make-gpt.sh index 3cae441..e39828b 100755 --- a/scripts/make-gpt.sh +++ b/scripts/make-gpt.sh @@ -1,4 +1,4 @@ -#!/bin/sh -eu +#!/bin/sh -- # # SPDX-FileCopyrightText: 2021-2023 Alyssa Ross <hi@alyssa.is> # SPDX-FileCopyrightText: 2022 Unikie @@ -6,6 +6,8 @@ # # usage: make-gpt.sh GPT_PATH PATH:PARTTYPE[:PARTUUID[:PARTLABEL]]... +set -euo pipefail + ONE_MiB=1048576 # Prints the number of 1MiB blocks required to store the file named base-commit: d76e9b29aea9f31238d07e21db50d3fe6a80da5a -- 2.51.0
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> --- v3: don't discard return value of sizeMiB v2: https://spectrum-os.org/lists/archives/spectrum-devel/d7a8c13d-5bae-47c8-a1a... scripts/make-gpt.sh | 31 +++++++++++++++++++------------ scripts/sfdisk-field.awk | 22 +++++++++++++++++----- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/scripts/make-gpt.sh b/scripts/make-gpt.sh index e39828b..998a54d 100755 --- a/scripts/make-gpt.sh +++ b/scripts/make-gpt.sh @@ -1,10 +1,10 @@ #!/bin/sh -- # -# 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]]]... set -euo pipefail @@ -35,21 +35,28 @@ scriptsDir="$(dirname "$0")" out="$1" shift -nl=' -' -table="label: gpt" +table=$(for partition; do + size="$(sizeMiB "${partition%%:*}")" + awk -f "$scriptsDir/sfdisk-field.awk" \ + -v partition="$partition" \ + -v size="$size" +done) # 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 rm -f "$out" -truncate -s "$gptBytes" "$out" +truncate -s "${gptMiB}M" "$out" + sfdisk --no-reread --no-tell-kernel "$out" <<EOF +label: gpt +sector-size: $ONE_MiB $table EOF diff --git a/scripts/sfdisk-field.awk b/scripts/sfdisk-field.awk index e13c86d..78b438e 100644 --- a/scripts/sfdisk-field.awk +++ b/scripts/sfdisk-field.awk @@ -1,7 +1,7 @@ #!/usr/bin/awk -f # # SPDX-License-Identifier: EUPL-1.2+ -# SPDX-FileCopyrightText: 2022, 2024 Alyssa Ross <hi@alyssa.is> +# SPDX-FileCopyrightText: 2022, 2024-2025 Alyssa Ross <hi@alyssa.is> BEGIN { types["root.aarch64"] = "b921b045-1df0-41c3-af44-4c6f280d3fae" @@ -9,12 +9,11 @@ BEGIN { types["verity.aarch64"] = "df3300ce-d69f-4c92-978c-9bfb0f38d820" types["verity.x86_64"] = "2c7357ed-ebd2-46d9-aec1-23d437ec2bf5" - # Field #1 is the partition path, which make-gpt.sh will turn into - # the size field. Since it's handled elsewhere, we skip that - # first field. + # Field #1 is the partition path, which is read by make-gpt.sh + # but not relevant for running sfdisk, so skip it. skip=1 - split("type uuid name", keys) + split("type uuid name size", keys) split(partition, fields, ":") arch = ENVIRON["ARCH"] @@ -31,8 +30,21 @@ BEGIN { if (keys[n - skip] == "type") { if (uuid = types[fields[n] "." arch]) fields[n] = uuid + } else if (keys[n - skip] == "size") { + if (fields[n] < size) { + printf "%s MiB partition content is too big for %s MiB partition\n", + size, fields[n] > "/dev/stderr" + exit 1 + } + + size = fields[n] + continue # Handled at the end. } printf "%s=%s,", keys[n - skip], fields[n] } + + # Always output a size field, either supplied in input or + # default value of the size variable. + printf "size=%s\n", size } -- 2.51.0
This patch has been committed as d4832d18f7c938782efd88185b31180d7475b575, which can be viewed online at https://spectrum-os.org/git/spectrum/commit/?id=d4832d18f7c938782efd88185b31.... This is an automated message. Send comments/questions/requests to: Alyssa Ross <hi@alyssa.is>
This patch has been committed as ef85c4be13e8717743dff9661352ca38bb77e4f7, which can be viewed online at https://spectrum-os.org/git/spectrum/commit/?id=ef85c4be13e8717743dff9661352.... This is an automated message. Send comments/questions/requests to: Alyssa Ross <hi@alyssa.is>
participants (2)
-
Alyssa Ross -
Alyssa Ross