forked from FlyGoat/pmon-update
/
pmon-update
executable file
·225 lines (183 loc) · 4.45 KB
/
pmon-update
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
213
214
215
216
217
218
219
220
221
222
223
224
#!/bin/bash
## PMON Bootloader Menu Updater For Debian
## Copyright (C) 2020 Boyue Zhang <boyue.zhang@pzhang.net>
## Copyright (C) 2020 Jiaxun Yang <jiaxun.yang@flygoat.com>
##
## Based on u-boot-update, which is:
## Copyright (C) 2006-2012 Daniel Baumann <daniel.baumann@progress-technologies.net>
## Copyright (C) 2016-2017 Riku Voipio <riku.voipio@linaro.org>
##
## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
## This is free software, and you are welcome to redistribute it
## under certain conditions; see COPYING for details.
set -e
_PMON_DIRECTORY="/boot"
Update ()
{
# Upate target file using source content
_TARGET="${1}"
_SOURCE="${2}"
_TMPFILE="${_TARGET}.tmp"
rm -f "${_TMPFILE}"
echo "${_SOURCE}" > "${_TMPFILE}"
if [ -e "${_TARGET}" ] && cmp -s "${_TARGET}" "${_TMPFILE}"
then
rm -f "${_TMPFILE}"
else
# FIXME: should use fsync here
echo "P: Updating ${_TARGET}..."
mv -f "${_TMPFILE}" "${_TARGET}"
fi
}
detect_linux_images()
{
unset _KERNELS
cd /boot
for kernel in vmlinuz-* vmlinux-*; do
if ! readelf -h $kernel; then
# Not a valid ELF file, skip.
continue
else
export _KERNELS="${_KERNELS} $kernel"
fi
done
}
# FIXME: switch to check boot.cfg file can be written to
# User is unprivileged
if [ "$(id -u)" -ne 0 ]
then
echo "E: need root privileges"
exit 1
fi
# Redirect stdout to stderr due Debconf usage
exec 1>&2
# Reading the default file
if [ -e /etc/default/pmon-update ]
then
. /etc/default/pmon-update
fi
PMON_UPDATE="${PMON_UPDATE:-true}"
if [ "${PMON_UPDATE}" != "true" ]
then
echo "P: pmon-update is disabled in /etc/default/pmon-update."
exit 0
fi
# Setting defaults if /etc/default/pmon-update is missing
PMON_ALTERNATIVES="${PMON_ALTERNATIVES}"
PMON_BOOTDEV="${PMON_BOOTDEV:-(wd0,0)}"
PMON_DEFAULT="${PMON_DEFAULT:-0}"
PMON_ENTRIES="${PMON_ENTRIES:-all}"
PMON_TIMEOUT="${PMON_TIMEOUT:-5}"
PMON_MENU_LABEL="${PMON_MENU_LABEL:-Linux}"
PMON_PARAMETERS="${PMON_PARAMETERS:-ro quiet}"
# Find parameter for root from fstab
if [ -z "${PMON_ROOT}" ]
then
# Use findmnt(8) to determine system root
if command -v findmnt > /dev/null
then
PMON_ROOT="findmnt -n -o SOURCE /"
elif [ -e /etc/fstab ]
then
# Find system root from /etc/fstab
while read _LINE
do
read _FS_SPEC _FS_FILE _FS_VFSTYPE _FS_MNTOPS _FS_FREQ _FS_PASSNO << EOF
${_LINE}
EOF
if [ "${_FS_FILE}" = "/" ]
then
case "${_FS_SPEC}" in
"#"*)
;;
*)
PMON_ROOT="root=${_FS_SPEC}"
break
;;
esac
fi
done < /etc/fstab
else
echo "E: Unable to detect root device! Please install util-linux or record your system root partition in /etc/fstab."
exit 1
fi
fi
# if not in fsrab, try from current kernel arguments
if [ -z "${PMON_ROOT}" ]
then
for param in `cat /proc/cmdline`
do
if [[ $param == root=* ]]
then
PMON_ROOT="$param"
break
fi
done
fi
# Create extlinux.conf
_CONFIG="\
## ${_PMON_DIRECTORY}/boot.cfg
##
## IMPORTANT WARNING
##
## The configuration of this file is generated automatically.
## Do not edit this file manually, use: pmon-update
default ${PMON_DEFAULT}
timeout ${PMON_TIMEOUT}
showmenu 1
"
# Find linux versions
detect_linux_images
# Find boot directory as seen in pmon, and path prefix while in linux
if [ "$(stat --printf %d /)" = "$(stat --printf %d /boot)" ]
then
# / and /boot are on the same filesystem
_BOOT_PATH="${PMON_BOOTDEV}/boot"
else
# / and /boot are not on the same filesystem
_BOOT_PATH="${PMON_BOOTDEV}"
fi
for _KERNEL in ${_KERNELS}
do
# Strip kernel prefix to derive version.
_VERSION=${_KERNEL#*-}
echo "P: Writing config for ${_KERNEL}..."
_ENTRY="${_ENTRY:-1}"
if [ -e /boot/initrd.img-${_VERSION} ]
then
_INITRD="initrd ${_BOOT_PATH}/initrd.img-${_VERSION}"
else
_INITRD=""
fi
if echo ${PMON_ALTERNATIVES} | grep -q default
then
# Writing default entry
_CONFIG="${_CONFIG}
title ${PMON_MENU_LABEL} ${_VERSION}
kernel ${_BOOT_PATH}/${_KERNEL}
${_INITRD}
args ${PMON_ROOT} ${PMON_PARAMETERS}"
fi
if echo ${PMON_ALTERNATIVES} | grep -q recovery
then
# Writing recovery entry
_CONFIG="${_CONFIG}
title ${PMON_MENU_LABEL} ${_VERSION} (Rescue Target)
kernel ${_BOOT_PATH}/${_KERNEL}
${_INITRD}
args ${PMON_ROOT} $(echo ${PMON_PARAMETERS} | sed -e 's| quiet||') single
"
fi
if [ "${PMON_ENTRIES}" = "${_ENTRY}" ]
then
break
fi
done
# Append extra custom config
if [ -e /boot/pmon/custom.cfg ]
then
_CONFIG="${_CONFIG}
$(cat /boot/pmon/custom.cfg)
"
fi
Update "${_PMON_DIRECTORY}/boot.cfg" "${_CONFIG}"