-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalcove.sh
executable file
·357 lines (285 loc) · 7.93 KB
/
alcove.sh
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
349
350
351
352
353
354
355
356
357
#!/system/bin/sh
# MIT License
#
# Copyright (c) 2018-2019 urain39 & Kyono
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Exit safely
trap ":" HUP INT QUIT TERM
# NOTE: Below exit codes also applied to sub-scripts,
# such as ${NEWROOT}/init.sh ...
NO_ERROR=0
ENV_ERROR=1
PERM_ERROR=2
BOOT_ERROR=3
UNKNOWN_ERROR=255
# XXX: '$(id -u)' may not work on android.
if [ x"${UID}" = "x" ]; then
UID="$(id)"
UID="${UID#*=}"
UID="${UID%% *}"
UID="${UID%\(*}"
fi
BOOT_DIR=""
umask 022 # Fix default permissions.
#
# Prechecks
#
type chroot > /dev/null 2>&1 || {
echo "Not found chroot!"
echo "Please install chroot and try again!"
exit "${ENV_ERROR}"
}
[ "${UID}" != "0" ] && {
echo "Please run ${0##*/} as root!"
exit "${PERM_ERROR}"
}
#
# Functions
#
show_help()
{
# NOTE: "${0}" always is same in script,
# it is name of script.
cat <<HELP
alcove - a script to run linux on termux.
Usage: ${0##*/} <init|boot> <bootdir>
See also:
QQGroup: 494453985
https://github.com/95e2/linux-on-termux
HELP
}
alcove_init()
{
cat > "${BOOT_DIR}/init.sh" <<INIT_SCRIPT
#!/bin/sh
# Exit safely
trap ":" HUP INT QUIT TERM
unset PREFIX TMPDIR HOME SHELL
unset LD_LIBRARY_PATH HISTFILE
unset ANDROID_ROOT ANDROID_DATA
unset EXTERNAL_STORAGE PROOT_TMP_DIR
export TERM="xterm"
export HOME="/root"
export PATH="/bin:/sbin:/usr/bin:/usr/sbin"
export PATH="\${PATH}:/usr/local/bin:/opt/bin"
# Preload profile
. /etc/profile
# Show logo, even though it was crashed.
echo " _ _ "
echo " / \\\\ | | ___ _____ _____ "
echo " / _ \\\\ | |/ __/ _ \\\\ \\\\ / / _ \\\\"
echo " / ___ \\\\| | (_| (_) \\\\ V / __/"
echo "/_/ \\\\_\\\\_|\\\\___\\\\___/ \\\\_/ \\\\___| v1.2.5"
echo " "
echo " A chroot scripts to run linux on termux."
if [ -f /tmp/.alcove/alcove.running ]; then
echo "Do not run /init.sh on a same system twice or more!"
# NOTE: Exit code has been formatted.
exit "${UNKNOWN_ERROR}"
fi
echo "IS RUNNING" > /tmp/.alcove/alcove.running
#
# Event-Hooks
#
COLOR_RESET="\033[0m"
COLOR_CLEAR="\r\033[K"
COLOR_BOLD_RED="\033[1;31m"
COLOR_BOLD_GREEN="\033[1;32m"
clear_print() {
printf "\${COLOR_CLEAR}\${@}"
}
print_msg() {
printf "\${@}"
}
print_ok() {
clear_print "[ \${COLOR_BOLD_GREEN}OK\${COLOR_RESET} ] \${@}\n"
}
print_failed() {
clear_print "[\${COLOR_BOLD_RED}FAILED\${COLOR_RESET}] \${@}\n"
}
if [ -d /alcove-hooks ]; then
rm -rf /tmp/.alcove/alcove-hooks # Path-Safe
cp -rp /alcove-hooks /tmp/.alcove/alcove-hooks
print_msg "Starting...\n"
for s in /tmp/.alcove/alcove-hooks/*; do
if [ ! -f "\${s}" ] || [ ! -x "\${s}" ]; then
continue
fi
print_msg "Starting \${s} ..."
"\${s}" "start"
if [ "\${?}" = 0 ]; then
print_ok "\${s}"
else
print_failed "\${s}"
fi
done
su - root; ret="\${?}"
print_msg "Stopping...\n"
ls /tmp/.alcove/alcove-hooks/* | sort -r | while read -r s; do
if [ ! -f "\${s}" ] || [ ! -x "\${s}" ]; then
continue
fi
print_msg "Stopping \${s} ..."
"\${s}" "stop"
if [ "\${?}" = 0 ]; then
print_ok "\${s}"
else
print_failed "\${s}"
fi
done
else
su - root; ret="\${?}"
fi
# Cleanup
rm /tmp/.alcove/alcove.running
exit "\${ret}"
INIT_SCRIPT
chmod 755 "${BOOT_DIR}"
chmod 750 "${BOOT_DIR}/init.sh"
# alcove binds
cat > "${BOOT_DIR}/alcove.binds" <<ALCOVE_BINDS
# Syntax:
# source_dir newroot_dir
# Use '#' at begin of a line to comment it.
# Example:
# sdcard
#/sdcard /mnt/intsd
ALCOVE_BINDS
chmod 644 "${BOOT_DIR}/alcove.binds"
# mount point
mkdir -p "${BOOT_DIR}/mnt/intsd"
mkdir -p "${BOOT_DIR}/mnt/extsd"
chmod 755 "${BOOT_DIR}/mnt/intsd"
chmod 755 "${BOOT_DIR}/mnt/extsd"
# alcove hooks
mkdir -p "${BOOT_DIR}/alcove-hooks"
cat > "${BOOT_DIR}/alcove-hooks/00-alcover" <<00_ALCOVER
#!/bin/sh
# Do nothing, always exit 0
exit 0
00_ALCOVER
chmod 755 "${BOOT_DIR}/alcove-hooks"
chmod 755 "${BOOT_DIR}/alcove-hooks/00-alcover"
}
alcove_mount()
{
if [ -f "${BOOT_DIR}/tmp/.alcove/alcove.mounted" ]; then
return
fi
mount -o bind /dev "${BOOT_DIR}/dev"
mount -o bind /dev/pts "${BOOT_DIR}/dev/pts"
mount -o bind /proc "${BOOT_DIR}/proc"
mount -o bind /sys "${BOOT_DIR}/sys"
mount -t tmpfs tmpfs "${BOOT_DIR}/tmp"
if [ ! -d "${BOOT_DIR}/tmp/.alcove" ]; then
mkdir "${BOOT_DIR}/tmp/.alcove"
fi
if [ ! -d "${BOOT_DIR}/dev/shm" ]; then
mkdir "${BOOT_DIR}/dev/shm" && mount -t tmpfs tmpfs "${BOOT_DIR}/dev/shm" \
&& chmod 1777 "${BOOT_DIR}/dev/shm"
echo "SHM LOCKED" > "${BOOT_DIR}/tmp/.alcove/alcove.shmlock"
fi
if [ -f "${BOOT_DIR}/alcove.binds" ]; then
# Fix error when user edited /alcove.binds .
sed -n '/^#/d;/^[ \t]*$/d;p' "${BOOT_DIR}/alcove.binds" > "${BOOT_DIR}/tmp/.alcove/alcove.binds"
while read -r SRC_PNT DST_PNT; do
mount -o bind "${SRC_PNT}" "${BOOT_DIR}/${DST_PNT}"
done < "${BOOT_DIR}/tmp/.alcove/alcove.binds"
fi
if [ -d /data ] && [ -d /sdcard ]; then
mount -o suid,remount /data
fi
echo "IS MOUNTED" > "${BOOT_DIR}/tmp/.alcove/alcove.mounted"
}
alcove_umount()
{
if [ ! -f "${BOOT_DIR}/tmp/.alcove/alcove.mounted" ]; then
return
fi
if [ -f "${BOOT_DIR}/tmp/.alcove/alcove.shmlock" ]; then
umount "${BOOT_DIR}/dev/shm" && rm -r "${BOOT_DIR}/dev/shm"
rm "${BOOT_DIR}/tmp/.alcove/alcove.shmlock"
fi
umount "${BOOT_DIR}/dev/pts"
umount "${BOOT_DIR}/dev"
umount "${BOOT_DIR}/proc"
umount "${BOOT_DIR}/sys"
if [ -f "${BOOT_DIR}/tmp/.alcove/alcove.binds" ]; then
while read -r SRC_PNT DST_PNT; do
umount "${BOOT_DIR}/${DST_PNT}"
done < "${BOOT_DIR}/tmp/.alcove/alcove.binds"
fi
if [ -d /data ] && [ -d /sdcard ]; then
mount -o nosuid,remount /data
fi
rm "${BOOT_DIR}/tmp/.alcove/alcove.mounted"
# Why and who cares about others?
rm -r "${BOOT_DIR}/tmp/.alcove"
umount "${BOOT_DIR}/tmp"
}
alcove_boot()
{
unset LD_PRELOAD
if [ ! -f "${BOOT_DIR}/etc/.resolv.conf.ok" ]; then
rm -f "${BOOT_DIR}/etc/resolv.conf"
echo "nameserver 8.8.4.4" > "${BOOT_DIR}/etc/resolv.conf"
echo "nameserver 8.8.8.8" >> "${BOOT_DIR}/etc/resolv.conf"
chmod 644 "${BOOT_DIR}/etc/resolv.conf"
echo "DNS IS OK" > "${BOOT_DIR}/etc/.resolv.conf.ok"
fi
[ ! -f "${BOOT_DIR}/init.sh" ] && {
echo "Not found [${BOOT_DIR}/init.sh]"
echo "Have you run ${0##*/} init?"
exit "${BOOT_ERROR}"
}
[ -f "${BOOT_DIR}/tmp/.alcove/alcove.mounted" ] && {
echo "Do not boot a same system twice or more!"
exit "${BOOT_ERROR}"
}
alcove_mount
chroot "${BOOT_DIR}" /init.sh
alcove_umount
}
main()
{
if [ "${#}" -lt 2 ]; then
show_help
exit "${NO_ERROR}"
fi
BOOT_DIR="${2}"
if [ ! -d "${BOOT_DIR}" ]; then
echo "Not found path [${BOOT_DIR}]!"
exit "${ENV_ERROR}"
fi
[ "${BOOT_DIR}" = "/" ] && exit "${ENV_ERROR}"
case "${1}" in
"init")
alcove_init
;;
"boot")
alcove_boot
;;
*)
show_help
exit "${NO_ERROR}"
;;
esac
}
main "${@}"