patches and low-level development discussion
 help / color / mirror / code / Atom feed
blob 91c6038f67d4d4906fec4a3412f2ff5fca2671d5 1950 bytes (raw)
name: scripts/make-gpt.sh 	 # note: path name is non-authoritative(*)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
 
#!/usr/bin/env -S bash --
# SPDX-FileCopyrightText: 2021-2023 Alyssa Ross <hi@alyssa.is>
# SPDX-FileCopyrightText: 2022 Unikie
# SPDX-License-Identifier: EUPL-1.2+
#
# usage: bash make-gpt.sh GPT_PATH PATH:PARTTYPE[:PARTUUID[:PARTLABEL]]...

set -xeuo pipefail
ONE_MiB=1048576

# Prints the number of 1MiB blocks required to store the file named
# $1.  We use 1MiB blocks because that's what sfdisk uses for
# alignment.  It would be possible to get a slightly smaller image
# using actual normal-sized 512-byte blocks, but it's probably not
# worth it to configure sfdisk to do that.
sizeMiB() {
	wc -c "$1" | awk -v ONE_MiB=$ONE_MiB \
		'{printf "%d\n", ($1 + ONE_MiB - 1) / ONE_MiB}'
}

# Copies from path $3 into partition number $2 in partition table $1.
fillPartition() {
	start="$(sfdisk -J "$1" | jq -r --argjson index "$2" \
		'.partitiontable.partitions[$index].start * 512')"

	# GNU cat will use copy_file_range(2) if possible, whereas dd
	# will always do a userspace copy, which is significantly slower.
	lseek -S 1 "$start" cat "$3" 1<>"$1"
}

# Prints the partition path from a PATH:PARTTYPE[:PARTUUID[:PARTLABEL]] string.
partitionPath() {
	awk -F: '{print $1}' <<EOF
$1
EOF
}

scriptsDir="$(dirname "$0")"

out="$1"
shift

table="label: gpt"

# Keep 1MiB free at the start, and 1MiB free at the end.
gptBytes=$((ONE_MiB * 2))
for partition; do
	if [[ "$partition" =~ :([1-9][0-9]*)MiB$ ]]; then
		sizeMiB=${BASH_REMATCH[1]}
		partition=${partition%:*}
	else
		partitionPath=$(partitionPath "$partition")
		sizeMiB=$(sizeMiB "$partitionPath")
	fi
	table+=$'\n'"size=${sizeMiB}MiB,$(awk -f "$scriptsDir/sfdisk-field.awk" -v partition="$partition")"
	gptBytes=$((gptBytes + sizeMiB * ONE_MiB))
done

rm -f "$out"
truncate -s "$gptBytes" "$out"
sfdisk --no-reread --no-tell-kernel "$out" <<EOF
$table
EOF

n=0
for partition; do
	partitionPath=$(partitionPath "$partition")
	fillPartition "$out" "$n" "$partitionPath"
	n=$((n + 1))
done

debug log:

solving 91c6038f67d4d4906fec4a3412f2ff5fca2671d5 ...
found 91c6038f67d4d4906fec4a3412f2ff5fca2671d5 in https://inbox.spectrum-os.org/spectrum-devel/20251121-updates-v4-5-d4561c42776e@gmail.com/ ||
	https://inbox.spectrum-os.org/spectrum-devel/20251119-updates-v3-5-b88a99915509@gmail.com/
found 0ae3d0005e5ae7d6214270fedb2fafb0a7064cf5 in https://inbox.spectrum-os.org/spectrum-devel/20251121-updates-v4-4-d4561c42776e@gmail.com/ ||
	https://inbox.spectrum-os.org/spectrum-devel/20251126-updates-v4-2-40c438d2dcaf@gmail.com/ ||
	https://inbox.spectrum-os.org/spectrum-devel/20251126-updates-v5-2-fd746748febd@gmail.com/ ||
	https://inbox.spectrum-os.org/spectrum-devel/20251119-updates-v3-4-b88a99915509@gmail.com/
found 96f0d2c8494c093558c0e32e7e920b569bb078ef in https://spectrum-os.org/git/spectrum
preparing index
index prepared:
100644 96f0d2c8494c093558c0e32e7e920b569bb078ef	scripts/make-gpt.sh

applying [1/2] https://inbox.spectrum-os.org/spectrum-devel/20251121-updates-v4-4-d4561c42776e@gmail.com/
diff --git a/scripts/make-gpt.sh b/scripts/make-gpt.sh
old mode 100755
new mode 100644
index 96f0d2c8494c093558c0e32e7e920b569bb078ef..0ae3d0005e5ae7d6214270fedb2fafb0a7064cf5

Checking patch scripts/make-gpt.sh...
warning: scripts/make-gpt.sh has type 100644, expected 100755
Applied patch scripts/make-gpt.sh cleanly.

skipping https://inbox.spectrum-os.org/spectrum-devel/20251126-updates-v4-2-40c438d2dcaf@gmail.com/ for 0ae3d0005e5ae7d6214270fedb2fafb0a7064cf5
skipping https://inbox.spectrum-os.org/spectrum-devel/20251126-updates-v5-2-fd746748febd@gmail.com/ for 0ae3d0005e5ae7d6214270fedb2fafb0a7064cf5
skipping https://inbox.spectrum-os.org/spectrum-devel/20251119-updates-v3-4-b88a99915509@gmail.com/ for 0ae3d0005e5ae7d6214270fedb2fafb0a7064cf5
index at:
100644 0ae3d0005e5ae7d6214270fedb2fafb0a7064cf5	scripts/make-gpt.sh

applying [2/2] https://inbox.spectrum-os.org/spectrum-devel/20251121-updates-v4-5-d4561c42776e@gmail.com/
diff --git a/scripts/make-gpt.sh b/scripts/make-gpt.sh
index 0ae3d0005e5ae7d6214270fedb2fafb0a7064cf5..91c6038f67d4d4906fec4a3412f2ff5fca2671d5 100644

Checking patch scripts/make-gpt.sh...
Applied patch scripts/make-gpt.sh cleanly.

skipping https://inbox.spectrum-os.org/spectrum-devel/20251119-updates-v3-5-b88a99915509@gmail.com/ for 91c6038f67d4d4906fec4a3412f2ff5fca2671d5
index at:
100644 91c6038f67d4d4906fec4a3412f2ff5fca2671d5	scripts/make-gpt.sh

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://spectrum-os.org/git/doc
	https://spectrum-os.org/git/mktuntap
	https://spectrum-os.org/git/spectrum
	https://spectrum-os.org/git/ucspi-vsock

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).