diff options
author | Andrew Cady <d@cryptonomic.net> | 2020-11-17 12:45:21 -0500 |
---|---|---|
committer | Andrew Cady <d@cryptonomic.net> | 2020-11-17 12:47:32 -0500 |
commit | 56b8ba1c8198bce8eef9a2eb23b10cf115d91133 (patch) | |
tree | dfedc740c28d59152d59828941daf6c9d66a0aa0 | |
parent | fa2c13629e15fe80ad18b214583d9e514f855559 (diff) |
new partition tool
-rw-r--r-- | partitions/.gitignore | 1 | ||||
-rw-r--r-- | partitions/Makefile | 5 | ||||
-rw-r--r-- | partitions/part1.conf | 4 | ||||
-rw-r--r-- | partitions/part2.conf | 4 | ||||
-rw-r--r-- | partitions/part3.conf | 4 | ||||
-rw-r--r-- | partitions/part4.conf | 3 | ||||
-rw-r--r-- | partitions/part5.conf | 3 | ||||
-rwxr-xr-x | src/partvi | 127 |
8 files changed, 151 insertions, 0 deletions
diff --git a/partitions/.gitignore b/partitions/.gitignore new file mode 100644 index 0000000..e35d885 --- /dev/null +++ b/partitions/.gitignore | |||
@@ -0,0 +1 @@ | |||
_build | |||
diff --git a/partitions/Makefile b/partitions/Makefile new file mode 100644 index 0000000..828e135 --- /dev/null +++ b/partitions/Makefile | |||
@@ -0,0 +1,5 @@ | |||
1 | |||
2 | .PHONY: all | ||
3 | |||
4 | all: | ||
5 | ../src/partvi | ||
diff --git a/partitions/part1.conf b/partitions/part1.conf new file mode 100644 index 0000000..c43d025 --- /dev/null +++ b/partitions/part1.conf | |||
@@ -0,0 +1,4 @@ | |||
1 | name=samizdat-efi | ||
2 | type=efi-system-partition | ||
3 | allocation=64M | ||
4 | rebuild=always | ||
diff --git a/partitions/part2.conf b/partitions/part2.conf new file mode 100644 index 0000000..d4a3419 --- /dev/null +++ b/partitions/part2.conf | |||
@@ -0,0 +1,4 @@ | |||
1 | name=samizdat-grub | ||
2 | type=bios-grub | ||
3 | allocation=64M | ||
4 | rebuild=always | ||
diff --git a/partitions/part3.conf b/partitions/part3.conf new file mode 100644 index 0000000..a83ed06 --- /dev/null +++ b/partitions/part3.conf | |||
@@ -0,0 +1,4 @@ | |||
1 | name=samizdat-keys | ||
2 | type=samizdat-keys | ||
3 | allocation=256MB | ||
4 | rebuild=always | ||
diff --git a/partitions/part4.conf b/partitions/part4.conf new file mode 100644 index 0000000..0115b54 --- /dev/null +++ b/partitions/part4.conf | |||
@@ -0,0 +1,3 @@ | |||
1 | name=samizdat-root-seed | ||
2 | type=dm-verity-data | ||
3 | data_path=../rootfs/samizdat-gold.seed.btrfs | ||
diff --git a/partitions/part5.conf b/partitions/part5.conf new file mode 100644 index 0000000..fce4b18 --- /dev/null +++ b/partitions/part5.conf | |||
@@ -0,0 +1,3 @@ | |||
1 | name=samizdat-root-seed-verity | ||
2 | type=dm-verity-hashes | ||
3 | data_path=../rootfs/samizdat-gold.seed.btrfs | ||
diff --git a/src/partvi b/src/partvi new file mode 100755 index 0000000..b50b918 --- /dev/null +++ b/src/partvi | |||
@@ -0,0 +1,127 @@ | |||
1 | #!/bin/bash | ||
2 | shopt -s nullglob | ||
3 | PATH=/sbin:$PATH | ||
4 | |||
5 | msg() { printf '%s: %s: %s\n' "$0" "$1" "$2" >&2; } | ||
6 | die() { msg Error "${*:-exiting on fatal error.}"; exit 1; } | ||
7 | warn() { msg Warning "${*:-Something is wrong.}"; } | ||
8 | notice() { msg Notice "$*"; } | ||
9 | |||
10 | validate_name() | ||
11 | { | ||
12 | case "$1" in | ||
13 | *[^a-zA-Z0-9_]*) false ;; | ||
14 | *) true ;; | ||
15 | esac | ||
16 | } | ||
17 | |||
18 | read_config_file() | ||
19 | { | ||
20 | validate_name "$img" || { warn "invalid name: $img"; return 1; } | ||
21 | while read line | ||
22 | do | ||
23 | line=${line%%#*} # ignore comments | ||
24 | k=${line%%=*} | ||
25 | v=${line#*=} | ||
26 | [ "$k" -a "$k" != "$line" ] || return | ||
27 | eval "conf_${1}_$k=\$v" | ||
28 | done < "$1".conf | ||
29 | } | ||
30 | |||
31 | inquire_var() { _inquire_var "$img" "$1"; } | ||
32 | _inquire_var() | ||
33 | { | ||
34 | local v | ||
35 | v=conf_${1}_${2} | ||
36 | v=${!v} | ||
37 | if [ "$v" ] | ||
38 | then | ||
39 | eval "$2=\$v" | ||
40 | else | ||
41 | false | ||
42 | fi | ||
43 | } | ||
44 | |||
45 | require_var() { _require_var "$img" "$1"; } | ||
46 | _require_var() | ||
47 | { | ||
48 | _inquire_var "$@" || die "Missing required field '$2' for image file '$1'" | ||
49 | } | ||
50 | |||
51 | get_root_hash() | ||
52 | { | ||
53 | sed -ne 's/^Root hash:[ \t]*//p' "$1" | ||
54 | } | ||
55 | |||
56 | builddir=_build | ||
57 | mkdir -p "$builddir" | ||
58 | |||
59 | for f in part*.conf | ||
60 | do | ||
61 | img=${f%.conf} | ||
62 | |||
63 | read_config_file "$img" || warn "Received error return from command: read_config_file $img" | ||
64 | require_var name | ||
65 | |||
66 | require_var type | ||
67 | case "$type" in | ||
68 | efi-system-partition|bios-grub|samizdat-*) ;; | ||
69 | dm-verity-hashes|dm-verity-data) require_var data_path ;; | ||
70 | *) die "invalid type: $type" ;; | ||
71 | esac | ||
72 | |||
73 | imgfile=$builddir/$img | ||
74 | |||
75 | if inquire_var rebuild | ||
76 | then | ||
77 | case "$rebuild" in | ||
78 | always) ;; | ||
79 | never) ;; | ||
80 | *) die "invalid value for field 'rebuild': $rebuild" ;; | ||
81 | esac | ||
82 | fi | ||
83 | |||
84 | if [ "$rebuild" = 'always' ] || [ ! -e "$imgfile" -a "$rebuild" != 'never' ] | ||
85 | then | ||
86 | |||
87 | if [ -e "$imgfile" ] | ||
88 | then | ||
89 | notice "Image file exists: $imgfile" | ||
90 | fi | ||
91 | |||
92 | case "$type" in | ||
93 | dm-verity-hashes|dm-verity-data) | ||
94 | require_var data_path | ||
95 | [ -f "$data_path" ] | ||
96 | [ -f "$data_path".verity ] | ||
97 | [ -f "$data_path".verity.log ] | ||
98 | root_hash=$(get_root_hash "$data_path".verity.log) | ||
99 | [ ${#root_hash} = 64 ] | ||
100 | ;; | ||
101 | *) | ||
102 | require_var allocation | ||
103 | tmp=$imgfile~tmp | ||
104 | fallocate -l "$allocation" "$tmp" | ||
105 | ;; | ||
106 | esac | ||
107 | |||
108 | case "$type" in | ||
109 | efi-system-partition) mkfs.vfat "$tmp" || die "mkfs.vfat failed" ;; | ||
110 | bios-grub) mkfs.vfat "$tmp" || die "mkfs.vfat failed" ;; | ||
111 | samizdat-keys) mkfs.btrfs "$tmp" || die "mkfs.btrfs failed" ;; | ||
112 | dm-verity-data) | ||
113 | partuuid=${root_hash:0:32} | ||
114 | cp -f -T --reflink "$data_path" "$builddir"/"$partuuid" | ||
115 | ln -sfT "$partuuid" "$tmp" | ||
116 | ;; | ||
117 | dm-verity-hashes) | ||
118 | partuuid=${root_hash:32:32} | ||
119 | cp -f -T --reflink "$data_path".verity "$builddir"/"$partuuid" | ||
120 | ln -sfT "$partuuid" "$tmp" | ||
121 | ;; | ||
122 | *) die "Unrecognized type: $type" ;; | ||
123 | esac | ||
124 | mv -T "$tmp" "$imgfile" | ||
125 | notice "Successfully wrote $imgfile" | ||
126 | fi | ||
127 | done | ||