summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@cryptonomic.net>2020-11-17 12:45:21 -0500
committerAndrew Cady <d@cryptonomic.net>2020-11-17 12:47:32 -0500
commit56b8ba1c8198bce8eef9a2eb23b10cf115d91133 (patch)
treedfedc740c28d59152d59828941daf6c9d66a0aa0
parentfa2c13629e15fe80ad18b214583d9e514f855559 (diff)
new partition tool
-rw-r--r--partitions/.gitignore1
-rw-r--r--partitions/Makefile5
-rw-r--r--partitions/part1.conf4
-rw-r--r--partitions/part2.conf4
-rw-r--r--partitions/part3.conf4
-rw-r--r--partitions/part4.conf3
-rw-r--r--partitions/part5.conf3
-rwxr-xr-xsrc/partvi127
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
4all:
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 @@
1name=samizdat-efi
2type=efi-system-partition
3allocation=64M
4rebuild=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 @@
1name=samizdat-grub
2type=bios-grub
3allocation=64M
4rebuild=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 @@
1name=samizdat-keys
2type=samizdat-keys
3allocation=256MB
4rebuild=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 @@
1name=samizdat-root-seed
2type=dm-verity-data
3data_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 @@
1name=samizdat-root-seed-verity
2type=dm-verity-hashes
3data_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
2shopt -s nullglob
3PATH=/sbin:$PATH
4
5msg() { printf '%s: %s: %s\n' "$0" "$1" "$2" >&2; }
6die() { msg Error "${*:-exiting on fatal error.}"; exit 1; }
7warn() { msg Warning "${*:-Something is wrong.}"; }
8notice() { msg Notice "$*"; }
9
10validate_name()
11{
12 case "$1" in
13 *[^a-zA-Z0-9_]*) false ;;
14 *) true ;;
15 esac
16}
17
18read_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
31inquire_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
45require_var() { _require_var "$img" "$1"; }
46_require_var()
47{
48 _inquire_var "$@" || die "Missing required field '$2' for image file '$1'"
49}
50
51get_root_hash()
52{
53 sed -ne 's/^Root hash:[ \t]*//p' "$1"
54}
55
56builddir=_build
57mkdir -p "$builddir"
58
59for f in part*.conf
60do
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
127done