forked from toltec-dev/toltec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap
executable file
·348 lines (289 loc) · 10.1 KB
/
bootstrap
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env bash
# Copyright (c) 2021 The Toltec Contributors
# SPDX-License-Identifier: MIT
#
# bootstrap
#
# Install and configure Opkg, Entware and Toltec
#
# Arguments: Additional packages to install after setting up Toltec
# Example: ./bootstrap harmony draft
#
# Based on Evan Widloski’s remarkable_entware (2019-03-21)
# Based on the Entware installer from
# <http://bin.entware.net/armv7sf-k3.2/installer/generic.sh>
#
set -eE
# Path for the temporary local wget used in this installer
[[ -z $wget_path ]] && wget_path=/home/root/.local/bin/wget
# Path for the systemd unit that mounts Entware to /opt
[[ -z $old_systemd_mount_path ]] && old_systemd_mount_path=/etc/systemd/system/opt.mount
[[ -z $systemd_mount_path ]] && systemd_mount_path=/lib/systemd/system/opt.mount
# Path where the actual Entware distribution resides (will be mounted to /opt)
[[ -z $entware_path ]] && entware_path=/home/root/.entware
# Find out current branch or get default one
get-branch() {
local opkg_conf="$entware_path/etc/opkg.conf"
local branch
if [[ -f "$opkg_conf" ]]; then
# Get the first (...) value (aka \1) of the pattern if matching
branch="$(sed -E 's|^src/gz\s+toltec\s+https://toltec-dev.org/([[:alnum:]]*).*$|\1|;t;d' "$opkg_conf" | head -n1)"
fi
echo "${branch:-stable}"
}
# Toltec branch to use for this install - you almost always want the default
[[ -z $toltec_branch ]] && toltec_branch="$(get-branch)"
# Remove all temporary files
cleanup() {
if [[ -e $wget_path ]]; then
rm "$wget_path"
fi
}
# Remove unfinished installation after unexpected error
error-cleanup() {
read -ra trace < <(caller 0)
log ERROR "Unexpected error on line ${trace[2]}:${trace[0]} in function ${trace[1]}"
# Get out of /opt so it can be unmounted
cd /home/root
if [[ -d /opt ]]; then
umount /opt
rm /opt -rf
fi
if [[ -d $entware_path ]]; then
rm -rf "$entware_path"
fi
if [[ -f $systemd_mount_path ]]; then
systemctl disable --now opt.mount 2> /dev/null
rm "$systemd_mount_path"
systemctl daemon-reload
fi
log ERROR "This script failed to install. If you can't solve the above" \
"issue yourself, please report it at:" \
"" \
" https://github.com/toltec-dev/toltec/issues" \
"" \
"(Please also include these error logs to help solving the" \
"problem faster. Thank you!)"
}
# Install a local wget binary which supports TLS (the original one
# installed on the reMarkable does not) in the PATH
wget-bootstrap() {
local wget_remote=https://toltec-dev.org/thirdparty/bin/wget-v1.21.1
local wget_checksum=8798fcdabbe560722a02f95b30385926e4452e2c98c15c2c217583eaa0db30fc
if [[ ! -x $wget_path ]]; then
if [[ -e $wget_path ]]; then
log ERROR "'$wget_path' exists, but is not executable"
error-cleanup
exit 1
fi
# Download and compare to hash
mkdir -p "$(dirname "$wget_path")"
wget "$wget_remote" --output-document "$wget_path" 2> /dev/null
if ! sha256sum -c <(echo "$wget_checksum $wget_path") > /dev/null 2>&1; then
log ERROR "Invalid checksum for the local wget binary"
error-cleanup
exit 1
fi
chmod +x "$wget_path"
fi
# Ensure the local binary is used
if [[ $(command -v wget) != "$wget_path" ]]; then
export PATH
PATH="$(dirname "$wget_path"):$PATH"
fi
}
# Mount Entware to /opt
entware-mount() {
mkdir -p /opt
mkdir -p "$entware_path"
if [[ -f $old_systemd_mount_path ]] && [[ ! -f $systemd_mount_path ]]; then
# The user was probably using https://github.com/Evidlo/remarkable_entware
# before switching to toltec. Removing old .mount file to prevent duplicate files
log "Updating opt.mount location"
if systemctl -q is-enabled opt.mount; then
# Remove path to old file if still symlinked
systemctl disable opt.mount
fi
rm $old_systemd_mount_path
fi
# Create systemd mount unit to mount over /opt on reboot
cat > "$systemd_mount_path" << UNIT
[Unit]
Description=Bind mount Entware over /opt
DefaultDependencies=no
Conflicts=umount.target
Before=local-fs.target umount.target
[Mount]
What=/home/root/.entware
Where=/opt
Type=none
Options=bind
[Install]
WantedBy=local-fs.target
UNIT
systemctl daemon-reload
systemctl enable --now opt.mount 2> /dev/null
}
# Install Entware to /opt
entware-install() {
log "Installing Entware to /opt"
# Create basic folders, entware-opt package creates the rest
for folder in bin etc lib lib/opkg tmp var var/lock; do
if [ -d "/opt/$folder" ]; then
log WARN "Folder /opt/$folder exists!" \
"If something goes wrong please clean the /opt folder and try again."
else
mkdir "/opt/$folder"
fi
done
local dynamic_loader="ld-linux.so.3"
local entware_remote=https://bin.entware.net/armv7sf-k3.2/installer
wget --no-verbose "$entware_remote/opkg" -O /opt/bin/opkg
chmod 755 /opt/bin/opkg
wget --no-verbose "$entware_remote/opkg.conf" -O /opt/etc/opkg.conf
sed -i 's|http://|https://|g' /opt/etc/opkg.conf
wget --no-verbose "$entware_remote/ld-2.27.so" -O /opt/lib/ld-2.27.so
wget --no-verbose "$entware_remote/libc-2.27.so" -O /opt/lib/libc-2.27.so
wget --no-verbose "$entware_remote/libgcc_s.so.1" -O /opt/lib/libgcc_s.so.1
wget --no-verbose "$entware_remote/libpthread-2.27.so" -O /opt/lib/libpthread-2.27.so
cd /opt/lib
chmod 755 ld-2.27.so
ln -s ld-2.27.so "$dynamic_loader"
ln -s libc-2.27.so libc.so.6
ln -s libpthread-2.27.so libpthread.so.0
/opt/bin/opkg update
/opt/bin/opkg install entware-opt
# Fix for multiuser environment
chmod 777 /opt/tmp
# Create basic administrative files
if [[ -f /etc/passwd ]]; then
ln -sf /etc/passwd /opt/etc/passwd
else
cp /opt/etc/passwd.1 /opt/etc/passwd
fi
if [[ -f /etc/group ]]; then
ln -sf /etc/group /opt/etc/group
else
cp /opt/etc/group.1 /opt/etc/group
fi
if [[ -f /etc/shells ]]; then
ln -sf /etc/shells /opt/etc/shells
else
cp /opt/etc/shells.1 /opt/etc/shells
fi
if [[ -f /etc/shadow ]]; then
ln -sf /etc/shadow /opt/etc/shadow
fi
if [[ -f /etc/gshadow ]]; then
ln -sf /etc/gshadow /opt/etc/gshadow
fi
if [[ -f /etc/localtime ]]; then
ln -sf /etc/localtime /opt/etc/localtime
fi
}
# Add Toltec configuration to an existing Entware install
#
# Arguments: List of additional packages to install, one package per argument
toltec-install() {
# Make sure that Opkg is configured to fetch the chosen Toltec branch
if grep -q "^src/gz\b.*\bhttps://toltec-dev\.org/$toltec_branch$" /opt/etc/opkg.conf; then
log "Already on the Toltec $toltec_branch branch"
else
log "Adding the Toltec $toltec_branch repository"
sed -i '/^src\/gz\b.*\bhttps:\/\/toltec\.delab\.re\//d' /opt/etc/opkg.conf
sed -i '/^src\/gz\b.*\bhttps:\/\/toltec-dev\.org\//d' /opt/etc/opkg.conf
echo "src/gz toltec https://toltec-dev.org/$toltec_branch" >> /opt/etc/opkg.conf
fi
/opt/bin/opkg update
# Make sure that the packages needed for wget TLS support are installed
local additional_packages=()
[[ ! -d /opt/etc/ssl/certs ]] && additional_packages+=(ca-certificates)
[[ ! -f /opt/bin/wget ]] && additional_packages+=(wget)
if [[ ${#additional_packages[@]} -gt 0 ]]; then
/opt/bin/opkg install "${additional_packages[@]}"
fi
/opt/bin/opkg update
/opt/bin/opkg install toltec-bootstrap
# Install additional packages
if [[ $# -gt 0 ]]; then
/opt/bin/opkg install "$@"
fi
# Remove existing PATH definitions where the binaries in /opt/bin and
# /opt/sbin have lesser priority than the others
# shellcheck disable=SC2016
if [[ -e /home/root/.bashrc ]]; then
sed -i '/^\(export \)\?PATH="\?\$PATH:\/opt\/bin:\/opt\/sbin"\?$/d' /home/root/.bashrc
fi
# Add binaries in /opt/bin and /opt/sbin to the PATH before any others
# shellcheck disable=SC2016
if [[ ! -e /home/root/.bashrc ]] \
|| ! grep -q '^\(export \)\?PATH="\?/opt/bin:/opt/sbin:\$PATH"\?$' /home/root/.bashrc; then
log "Adding /opt/bin and /opt/sbin to your PATH"
log "Please run '. \$HOME/.bashrc' to use Toltec"
cat >> /home/root/.bashrc << 'SHELL'
# Path added by Toltec bootstrap
PATH="/opt/bin:/opt/sbin:$PATH"
SHELL
fi
}
# Print a log message
#
# Arguments:
#
# [$1] - Log level: INFO, WARN or ERROR (default: INFO)
# $2... - Messages to print, each argument goes to a separate line
log() {
# Output stream where the messages will be sent
local log_type="INFO"
local fd=1
local colored_prefix
if [[ $# -ge 2 ]]; then
case "$1" in
INFO | WARN | ERROR)
log_type="$1"
shift
;;
esac
fi
case "$log_type" in
INFO) colored_prefix='\e[32mINFO:\e[0m ' ;;
WARN)
colored_prefix='\e[33mWARN:\e[0m '
fd=2
;;
ERROR)
colored_prefix='\e[31mERROR:\e[0m '
fd=2
;;
esac
echo -e "${colored_prefix}$1" >&$fd
# Extra lines to print indented
shift
local line
for line in "$@"; do
echo -e " $line" >&$fd
done
}
main() {
trap cleanup EXIT
trap error-cleanup ERR
if [[ "$(command -v wget)" == /usr/bin/wget ]]; then
log "Fetching secure wget"
wget-bootstrap
fi
if [[ -d $entware_path ]]; then
if [[ -d /opt ]] && files="$(ls -A -- /opt)" && [[ -n $files ]]; then
log "Entware is already installed and active"
else
log "Re-enabling existing Entware install"
entware-mount
fi
else
log "Creating $entware_path and mounting to /opt"
entware-mount
entware-install
fi
toltec-install "$@"
log "Use '/home/root/entware-reenable' to re-enable Toltec after a system update"
}
main "$@"