-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathzboot
More file actions
executable file
·212 lines (182 loc) · 4.58 KB
/
zboot
File metadata and controls
executable file
·212 lines (182 loc) · 4.58 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/bin/sh
ZX="EVE"
set -e
#
# How do you get the root dev from a container? With the next two
# functions.
#
makewords() {
awk '{print $0}' RS=' ' < $1
}
rdev () {
uuid=$(makewords /proc/cmdline | grep PARTUUID | awk -F = '{print $3}')
dev=$(lsblk -l -o name,PARTUUID | awk -v uuid="$uuid" '$2==uuid {print $1}')
echo $dev
}
rootdev=$(echo "/dev/"$(rdev))
rootdsk=$(echo "/dev/"$(lsblk -no pkname ${rootdev}))
usage() {
echo "${ZX} Boot Control and Administration API tool."
echo
echo "Usage:"
echo " $(basename "$0") {partdev|partdevsize} <LABEL>"
echo " $(basename "$0") partstate <LABEL> [-d]"
echo " $(basename "$0") set_partstate <LABEL> <STATE>"
echo " $(basename "$0") {curpart|reset}"
echo
echo "Commands:"
echo " partdev: return device of partition LABEL."
echo " partstate: return state of partition LABEL. Add -d flag for detailed decode."
echo " partdevsize: return device size of partition LABEL."
echo " set_partstate: Set state of partition LABEL to STATE."
echo " curpart: return label of current root partition."
echo " reset: reboot the machine with a certain hurry."
echo " poweroff: power off the machine."
echo
echo "<IMAGE> can be IMGA, IMG, or any other GPT partition label."
echo "<STATE> can be either 'active', or 'unused', or 'updating'."
echo "A partition can be in 'inprogress' state -- indicating either"
echo "a current attempt at updating into it or a failure to do so"
echo "in the past -- but such state cannot be set by this tool"
echo
exit 1
}
partno() {
cgpt find -l $1 -n ${rootdsk}
}
#
# CURPART: get partition label of current root
#
curpart() {
if [ ! $# -eq 0 ]; then
usage
fi
lsblk -o PARTLABEL -n ${rootdev}
}
#
# PARTDEV: get device from partition label
#
partdev() {
if [ ! $# -eq 1 ]; then
usage
fi
cgpt find -l $1 ${rootdsk}
}
#
# PARTDEVSIZE: get device size from partition label
#
partdevsize() {
if [ ! $# -eq 1 ]; then
usage
fi
dev="$(partdev "$@")"
lsblk -b -n -o SIZE "$dev"
}
#
# PARTDEVALL: get device from partition label, search all drives
#
partdevall() {
if [ ! $# -eq 1 ]; then
usage
fi
cgpt find -l "$1"
}
#
# DECODE_ATTR <attr>: decode and explain an unknown attribute value
#
decode_attr() {
attr=$1
# Extract bit fields using the formulas
priority=$((attr & 0x0F))
tries=$(((attr >> 4) & 0x0F))
successful=$(((attr >> 8) & 0x01))
# Check for unexpected bits (bits 9 and above)
high_bits=$((attr >> 9))
echo "UNKNOWN 0x$(printf %x "$attr") decoded:"
echo " Priority: $priority"
echo " Tries left: $tries"
echo " Successful: $successful"
if [ $high_bits -ne 0 ]; then
echo " Reserved bits set: 0x$(printf %x "$high_bits")"
fi
}
#
# PARTSTATE <LABEL> [-d]: get state from partition label
#
partstate() {
decode_flag=0
if [ $# -lt 1 ] || [ $# -gt 2 ]; then
usage
fi
# Parse optional -d flag (after label for backward compatibility)
if [ $# -eq 2 ] && [ "$2" = "-d" ]; then
decode_flag=1
fi
pn=$(partno "$1")
attr=$(cgpt show -i "${pn}" -A "${rootdsk}")
if [ "$decode_flag" -eq 1 ]; then
decode_attr "${attr}"
fi
case "${attr}" in
0x102) state="active" ;;
0x13) state="updating" ;;
0x3) state="inprogress" ;;
0) state="unused" ;;
*) state="UNKNOWN ${attr}" ;;
esac
echo "${state}"
}
#
# SETPARTSTATE <LABEL> <STATE>: set partition state
#
set_partstate() {
if [ ! $# -eq 2 ]; then
usage
fi
pn=$(partno "$1")
case $2 in
"active") attr=0x102 ;;
"updating") attr=0x13 ;;
"unused") attr=0 ;;
*)
echo "Unknown state '$2'";
exit 2;
;;
esac
cgpt add -i "${pn}" -A "${attr}" "${rootdsk}"
}
#
# RESET: shutdown and restart
#
reset() {
if [ ! $# -eq 0 ]; then
usage
fi
# Warning, this does not really play nice with init
# do not sync, don't go through init
/sbin/reboot -n -f
}
#
# POWEROFF
#
poweroff() {
# Warning, this does not really play nice with init
# do not sync, don't go through init
/sbin/poweroff -f
}
if [ $# -eq 0 ]; then
usage
fi
cmd=$1
shift 1
case $cmd in
curpart) curpart ;;
partdev) partdev "$@" ;;
partdevsize) partdevsize "$@" ;;
partdevall) partdevall "$@" ;;
partstate) partstate "$@" ;;
set_partstate) set_partstate "$@" ;;
reset) reset ;;
poweroff) poweroff ;;
*) usage ;;
esac