-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzbm-update-github.bash
executable file
·136 lines (113 loc) · 3.14 KB
/
zbm-update-github.bash
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
#! /usr/bin/env bash
# Root check:
if [[ ! 0 == ${UID} ]]; then
echo "You need root privileges to run this."
exit 1
fi
# Dependencies check (better safe than sorry):
for D in curl efibootmgr grep jq mount sed umount; do
if [[ ! $(command -v ${D}) ]]; then
echo "Command not found: ${D}"
unset D
exit 1
fi
done
# Utility cleanup function, takes as parameter the exit value:
function cleanup()
{
umount "${EFI}" &> /dev/null
rmdir "${EFI}" &> /dev/null
rm "${TMP_DWN}" &> /dev/null
unset D EFI EBM EFI_DEVICE ZBM_PATH DATA LATEST URL INSTALLED TMP_DWN
exit $1
}
# EFI executable name:
ZBM_EFI="zfsbootmenu.EFI"
# Get zfsbootmenu's entry:
EBM=$(
efibootmgr --unicode |
grep -i "${ZBM_EFI}" |
head -n1 |
sed -e 's,\\,/,g'
)
if [[ "" == ${EBM} ]]; then
echo "No existing ZBM entry found via efibootmgr."
cleanup 2
fi
# Temporary EFI mountpoint:
EFI="/root/efi_$(date "+%Y%m%d%H%M%S")"
mkdir -p "${EFI}"
# Find the EFI device via partuuid:
EFI_DEVICE="/dev/disk/by-partuuid/$(sed -E 's/(.*GPT,|,.*)//g' <<< ${EBM})"
# Get ZBM's path inside the EFI partition:
ZBM_PATH="${EFI}/$(sed -E "s,(/${ZBM_EFI}.*|.*\)//),,g" <<< ${EBM})/${ZBM_EFI}"
# Temporary ZBM download path:
TMP_DWN="/tmp/${ZBM_EFI}"
# Mount EFI partition:
umount "${EFI_DEVICE}" &> /dev/null
mount \
-o rw \
-o relatime \
-o fmask=0177 \
-o dmask=0077 \
"${EFI_DEVICE}" "${EFI}" &> /dev/null
if [[ ! 0 == $? ]]; then
echo "Failed to mount ${EFI_DEVICE}."
cleanup 3
fi
# Ensure this path exists in case EFI partition is empty but efibootmngr
# reports an entry:
mkdir -p "$(dirname ${ZBM_PATH})"
if [[ ! 0 == $? ]]; then
echo "Failed to create ZBM folder in EFI partition."
cleanup 4
fi
# Fetch info about the latest release from git:
DATA=$(
curl --silent \
"https://api.github.com/repos/zbm-dev/zfsbootmenu/releases/latest"
)
if [[ ! 0 == $? ]]; then
echo "Failed to fetch data from git."
cleanup 5
fi
# Parse latest release data:
LATEST=$(jq -r '.tag_name' <<< ${DATA})
# Find the right download url of the EFI executable:
URL=""
IDX=0
while ! [[ "${URL,,}" =~ .*.efi$ ]]; do
IDX=$((IDX + 1))
URL=$(jq -r ".assets[-${IDX}].browser_download_url" <<< ${DATA})
# This means there was none found, so we may as well quit:
if [[ "null" == "${URL}" ]]; then
print "Failed to find a suitable EFI executable to download!"
cleanup 6
fi
done
# Installed version (if info is available):
INSTALLED=$(cat "${ZBM_PATH}.version" 2> /dev/null)
# Print out some info:
echo "Latest version: ${LATEST}"
echo "Installed version: ${INSTALLED}"
# Update ZBM if necessary:
if [[ ! ${INSTALLED} == ${LATEST} ]]; then
echo -e "\nDownloading latest version via curl:"
# Download ZBM in a temporary folder:
rm "${TMP_DWN}" &> /dev/null
curl -L "${URL}" -o "${TMP_DWN}"
if [[ ! 0 == $? ]]; then
echo "Failed to download latest ZBM."
cleanup 7
fi
# Backup the previous version if needed:
if [[ -e "${ZBM_PATH}" ]]; then
mv -f "${ZBM_PATH}" "${ZBM_PATH}.old"
echo "${INSTALLED}" > "${ZBM_PATH}.old.version"
fi
# Move in the new version:
mv "${TMP_DWN}" "${ZBM_PATH}"
echo "${LATEST}" > "${ZBM_PATH}.version"
fi
# Final touches:
cleanup 0