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 /src/partvi | |
parent | fa2c13629e15fe80ad18b214583d9e514f855559 (diff) |
new partition tool
Diffstat (limited to 'src/partvi')
-rwxr-xr-x | src/partvi | 127 |
1 files changed, 127 insertions, 0 deletions
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 | ||