-
Notifications
You must be signed in to change notification settings - Fork 87
/
debian_on_termux.sh
executable file
·356 lines (320 loc) · 9.95 KB
/
debian_on_termux.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
#!/data/data/com.termux/files/usr/bin/sh
#
# some configuration. adapt this to your needs
#
DO_FIRST_STAGE=: #false # required (unpack phase/ executes outside guest invironment)
DO_SECOND_STAGE=: #false # required (complete the install/ executes inside guest invironment)
DO_THIRD_STAGE=: #false # optional (enable local policies/ executes inside guest invironment)
VERSION=stable # debian versions: stable, testing, unstable
ROOTFS_TOP=deboot_debian # name of the top install directory
#
# some automatic configuration.
#
set -e
trap '[ $? -eq 0 ] && exit 0 || (echo; echo "termux-info:"; termux-info)' EXIT
ZONEINFO=$(getprop persist.sys.timezone) # set your desired time zone
ARCHITECTURE=$(uname -m)
case $ARCHITECTURE in # supported architectures include: armel, armhf, arm64, i386, amd64
aarch64) ARCHITECTURE=arm64 ;;
x86_64) ARCHITECTURE=amd64 ;;
i686) ARCHITECTURE=i386 ;;
armv7l) ARCHITECTURE=armhf ;;
armv8l) apt-get -qq install getconf; if [ $(getconf LONG_BIT) -eq 64 ]; then ARCHITECTURE=arm64; else ARCHITECTURE=armhf; fi ;;
armel|armhf|arm64|amd64|mips|mips64el|mipsel|ppc64el|s390x) ;;
# Officially supported Debian Stretch architectures
*) echo "Unsupported architecture $ARCHITECTURE"; exit ;;
esac
patchme() {
#
# minimum patch needed for debootstrap to work in this environment
#
patch -l << 'EOF'
--- functions.a 2020-02-27 13:16:24.000000000 +0100
+++ functions 2020-03-03 11:24:15.810995745 +0100
@@ -1154,6 +1154,10 @@
}
setup_proc () {
+
+echo skipping setup_proc
+return 0
+
case "$HOST_OS" in
*freebsd*)
umount_on_exit /dev
@@ -1265,6 +1269,10 @@
setup_devices_simple () {
+
+echo skipping setup_devices_simple
+return 0
+
# The list of devices that can be created in a container comes from
# src/core/cgroup.c in the systemd source tree.
mknod_if_needed "$TARGET/dev/null" c 1 3
@@ -1589,6 +1597,11 @@
# this directory. (Both may be forbidden by mount options, e.g. nodev and
# noexec respectively.)
check_sane_mount () {
+
+# these checks make no sense in our environment and fail anyway -> skip
+echo skipping check_sane_mount
+return 0
+
mkdir -p "$1"
case "$HOST_OS" in
EOF
return $?
}
USER_ID=$(id -u)
USER_NAME=$(id -un)
unset LD_PRELOAD # just in case termux-exec is installed
#
# workaround https://github.com/termux/termux-app/issues/306
# workaround https://github.com/termux/termux-packages/issues/1644
# or expect 'patch' to fail when doin the install via ssh and sh (not bash) is used
#
export TMPDIR=$PREFIX/tmp
cd
#
# ===============================================================
# first stage - do the initial unpack phase of bootstrapping only
#
$DO_FIRST_STAGE && {
echo ======== DO_FIRST_STAGE ========
[ -e "$HOME/$ROOTFS_TOP" ] && {
echo the target install directory already exists, to continue please remove it by
echo rm -rf "$HOME/$ROOTFS_TOP"
exit
}
apt-get -qq update
unset RESOLV
[ -e "$PREFIX/etc/resolv.conf" ] || {
RESOLV=resolv-conf
}
DEBIAN_FRONTEND=noninteractive apt-get -yqq install coreutils perl proot sed wget gnupg $RESOLV
hash -r
# first try to patch the most recent original of debian debootstrap script
rm -rf debootstrap
V=$(wget http://http.debian.net/debian/pool/main/d/debootstrap/ -qO - \
| sed 's/<[^>]*>//g' \
| grep -E '\.[0-9]+\.tar\.gz' \
| tail -n 1 \
| sed 's/^ +//g;s/.tar.gz.*//g')
wget "http://http.debian.net/debian/pool/main/d/debootstrap/$V.tar.gz" -qO - | tar xfz -
V=$(echo "$V" | sed 's/_/-/g')
ln -nfs "$V" debootstrap
cd debootstrap
patchme || {
# if the above fails patch the last known good backup of an
# older version of debian debootstrap script
cd
echo "patching $V failed using fallback"
rm -rf debootstrap
V=debootstrap_1.0.119
wget "https://github.com/sp4rkie/debian-on-termux/blob/master/$V.tgz?raw=true" -qO - \
| tar xfz -
V=$(echo "$V" | sed 's/_/-/g')
ln -nfs "$V" debootstrap
cd debootstrap
patchme
}
#
# fix https://github.com/sp4rkie/debian-on-termux/issues/21
# fix https://github.com/sp4rkie/debian-on-termux/issues/63
#
# add the key for stable (Debian Stable Release Key)
# Fix "gpg: keyserver receive failed: No name and apt-key(8) Deprecated"
gpg --keyserver keyserver.ubuntu.com --recv-keys DCC9EFBF77E11517
# add the keys for testing, unstable (Debian Archive Automatic Signing Key)
gpg --keyserver keyserver.ubuntu.com --recv-keys 648ACFD622F3D138
#
# you can watch the debootstrap progress via
# tail -F $HOME/$ROOTFS_TOP/debootstrap/debootstrap.log
#
DEBOOTSTRAP_DIR="$(pwd)"
export DEBOOTSTRAP_DIR
O="$("$PREFIX/bin/proot" \
-b /system \
-b /vendor \
-b /data \
-b "$PREFIX/bin:/bin" \
-b "$PREFIX/etc:/etc" \
-b "$PREFIX/lib:/lib" \
-b "$PREFIX/share:/share" \
-b "$PREFIX/tmp:/tmp" \
-b "$PREFIX/var:/var" \
-b /dev \
-b /proc \
-r "$PREFIX/.." \
-0 \
--link2symlink \
./debootstrap --keyring="$PREFIX/etc/apt/trusted.gpg" \
--foreign --arch="$ARCHITECTURE" "$VERSION" "$HOME/$ROOTFS_TOP" 2>&1 || true)"
echo "$O" > ~/debian-on-termux_debootstrap.log
# proot returns invalid exit status
if echo "$O" | grep " error: " > /dev/null ; then
echo "$O"
exit 1
fi
} # end DO_FIRST_STAGE
#
# =================================================
# second stage - complete the bootstrapping process
#
$DO_SECOND_STAGE && {
echo ======== DO_SECOND_STAGE ========
# since there are issues with proot and /proc mounts (https://github.com/termux/termux-packages/issues/1679)
# we currently cease from mounting /proc.
# the guest system now is setup to complete the installation - just dive in
# UPDATE as of 2017_11_27:
# issue https://github.com/termux/termux-packages/issues/1679#ref-commit-bcc972c now got fixed.
# /proc now included in mount list
O="$("$PREFIX/bin/proot" \
-b /dev \
-b /proc \
-r "$HOME/$ROOTFS_TOP" \
-w /root \
-0 \
--link2symlink /usr/bin/env \
-i HOME=/root TERM=xterm /debootstrap/debootstrap --second-stage 2>&1 || true)"
echo "$O" > ~/debian-on-termux_debootstrap_stage2.log
# proot returns invalid exit status
if echo "$O" | grep " error: " > /dev/null ; then
echo "$O"
exit 1
fi
#
# Add termux user in the passwd, group and shadow.
#
echo "$USER_NAME:x:$USER_ID:$USER_ID::/home/$USER_NAME:/bin/bash" >> \
"$HOME/$ROOTFS_TOP/etc/passwd"
echo "$USER_NAME:x:$USER_ID:" >> \
"$HOME/$ROOTFS_TOP/etc/group"
echo "$USER_NAME:*:15277:0:99999:7:::" >> \
"$HOME/$ROOTFS_TOP/etc/shadow"
#
# add the termux user homedir to the new debian guest system
#
mkdir -p "$HOME/$ROOTFS_TOP/home/$USER_NAME"
chmod 755 "$HOME/$ROOTFS_TOP/home/$USER_NAME"
} # end DO_SECOND_STAGE
#
# ======================================================================================
# optional third stage - if enabled edit some system defaults - adapt this to your needs
#
$DO_THIRD_STAGE && {
echo ======== DO_THIRD_STAGE ========
#
# take over an existing 'resolv.conf' from the host system (if there is one)
#
[ -e "$HOME/$ROOTFS_TOP/etc/resolv.conf" ] || {
cp "$PREFIX/etc/resolv.conf" "$HOME/$ROOTFS_TOP/etc/resolv.conf"
chmod 644 "$HOME/$ROOTFS_TOP/etc/resolv.conf"
}
#
# to enter the debian guest system execute '$HOME/bin/enter_deb' on the termux host system
#
mkdir -p "$HOME/bin"
cat << EOF > "$HOME/bin/enter_deb"
#!/data/data/com.termux/files/usr/bin/sh
unset LD_PRELOAD
SHELL_=/bin/bash
ROOTFS_TOP_=$ROOTFS_TOP
ROOT_=1
USER_=$USER_NAME
EOF
cat << 'EOF' >> "$HOME/bin/enter_deb"
SCRIPTNAME=enter_deb
show_usage () {
echo "Usage: $SCRIPTNAME [options] [command]"
echo "$SCRIPTNAME: enter the installed debian guest system"
echo ""
echo " -0 - mimic root (default)"
echo " -n - prefer regular termux uid ($USER_)"
exit 0
}
while getopts :h0n option
do
case "$option" in
h) show_usage;;
0) ;;
n) ROOT_=0;;
?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1;
esac
done
shift $(($OPTIND-1))
HOMEDIR_=/home/$USER_
[ $ROOT_ = 1 ] && {
CAPS_=$CAPS_"-0 "
HOMEDIR_=/root
}
CMD_="$SHELL_ -l"
[ -z "$*" ] || {
CMD_='sh -c "$*"'
}
eval $PREFIX/bin/proot \
-b /dev \
-b /proc \
-r $HOME/$ROOTFS_TOP_ \
-w $HOMEDIR_ \
$CAPS_ \
--link2symlink \
/usr/bin/env -i HOME=$HOMEDIR_ TERM=$TERM LANG=$LANG $CMD_
EOF
chmod 755 "$HOME/bin/enter_deb"
cat << 'EOF' > "$HOME/$ROOTFS_TOP/root/.profile"
# ~/.profile: executed by Bourne-compatible login shells.
if [ "$BASH" ]; then
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
fi
EOF
cat << EOF > "$HOME/$ROOTFS_TOP/tmp/dot_tmp.sh"
#!/bin/sh
filter() {
egrep -v '^$|^WARNING: apt does'
}
#
# select 'vi' as default editor for debconf/frontend
#
update-alternatives --config editor << !
2
!
#
# prefer a text editor for debconf (a GUI makes no sense here)
#
cat << ! | debconf-set-selections -v
debconf debconf/frontend select Editor
debconf debconf/priority select low
locales locales/locales_to_be_generated select en_US.UTF-8 UTF-8
locales locales/default_environment_locale select en_US.UTF-8
!
ln -nfs /usr/share/zoneinfo/$ZONEINFO /etc/localtime
dpkg-reconfigure -fnoninteractive tzdata
dpkg-reconfigure -fnoninteractive debconf
DEBIAN_FRONTEND=noninteractive apt -y update 2>&1 | filter
DEBIAN_FRONTEND=noninteractive apt -y upgrade 2>&1 | filter
DEBIAN_FRONTEND=noninteractive apt -y install locales 2>&1 | filter
update-locale LANG=en_US.UTF-8 LC_COLLATE=C
#
# place any additional packages here as you like
#
#DEBIAN_FRONTEND=noninteractive apt -y install rsync less gawk ssh 2>&1 | filter
apt clean 2>&1 | filter
EOF
chmod 755 "$HOME/$ROOTFS_TOP/tmp/dot_tmp.sh"
"$PREFIX/bin/proot" \
-b /dev \
-b /proc \
-r "$HOME/$ROOTFS_TOP" \
-w /root \
-0 \
--link2symlink \
/usr/bin/env -i HOME=/root TERM=xterm PATH=/usr/sbin:/usr/bin:/sbin:/bin /tmp/dot_tmp.sh \
|| : # proot returns invalid exit status
echo
echo installation successfully completed
echo to enter the guest system type:
echo "\$HOME/bin/enter_deb"
echo
} # end DO_THIRD_STAGE