forked from lf-edge/eve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzboot
executable file
·153 lines (132 loc) · 3.02 KB
/
zboot
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
#!/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|partstate} <LABEL>"
echo " $(basename "$0") set_partstate <LABEL> <STATE>"
echo " $(basename "$0") {curpart|reset}"
echo
echo "Commands:"
echo " partdev: return device of partion LABEL."
echo " partstate: return state 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
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}
}
#
# PARTDEVALL: get device from partition label, search all drives
#
partdevall() {
if [ ! $# -eq 1 ]; then
usage
fi
cgpt find -l "$1"
}
#
# PARTSTATE <LABEL>: get state from partition label
#
partstate() {
if [ ! $# -eq 1 ]; then
usage
fi
pn=$(partno $1)
attr=$(cgpt show -i ${pn} -A ${rootdsk})
case ${attr} in
0x102) state="active" ;;
0x13) state="updating" ;;
0x3) state="inprogress" ;;
0x0) state="unused" ;;
*) state="INVALID" ;;
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
reboot -n -f
}
if [ $# -eq 0 ]; then
usage
fi
cmd=$1
shift 1
case $cmd in
curpart) curpart ;;
partdev) partdev "$@" ;;
partdevall) partdevall "$@" ;;
partstate) partstate "$@" ;;
set_partstate) set_partstate "$@" ;;
reset) reset ;;
*) usage ;;
esac