forked from termux/proot-distro
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproot-distro.sh
executable file
·1749 lines (1603 loc) · 55.5 KB
/
proot-distro.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!@TERMUX_PREFIX@/bin/bash
##
## Script for managing proot'ed Linux distribution installations in Termux.
##
## Copyright (C) 2020 Leonid Pliushch <leonid.pliushch@gmail.com>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
##
PROGRAM_VERSION="1.6.1"
#############################################################################
#
# GLOBAL ENVIRONMENT AND INSTALLATION-SPECIFIC CONFIGURATION
#
set -e -u
PROGRAM_NAME="proot-distro"
# Where distribution plug-ins are stored.
DISTRO_PLUGINS_DIR="@TERMUX_PREFIX@/etc/proot-distro"
# Base directory where script keeps runtime data.
RUNTIME_DIR="@TERMUX_PREFIX@/var/lib/proot-distro"
# Where rootfs tarballs are downloaded.
DOWNLOAD_CACHE_DIR="${RUNTIME_DIR}/dlcache"
# Where extracted rootfs are stored.
INSTALLED_ROOTFS_DIR="${RUNTIME_DIR}/installed-rootfs"
# Colors.
if [ -n "$(command -v tput)" ] && [ $(tput colors) -ge 8 ] && [ -z "${PROOT_DISTRO_FORCE_NO_COLORS-}" ]; then
RST="$(tput sgr0)"
RED="${RST}$(tput setaf 1)"
BRED="${RST}$(tput bold)$(tput setaf 1)"
GREEN="${RST}$(tput setaf 2)"
YELLOW="${RST}$(tput setaf 3)"
BYELLOW="${RST}$(tput bold)$(tput setaf 3)"
BLUE="${RST}$(tput setaf 4)"
CYAN="${RST}$(tput setaf 6)"
BCYAN="${RST}$(tput bold)$(tput setaf 6)"
ICYAN="${RST}$(tput sitm)$(tput setaf 6)"
else
RED=""
BRED=""
GREEN=""
YELLOW=""
BYELLOW=""
BLUE=""
CYAN=""
BCYAN=""
ICYAN=""
RST=""
fi
#############################################################################
#
# FUNCTION TO PRINT A MESSAGE TO CONSOLE
#
# Prints a given text string to stderr. Handles escape sequences.
msg() {
echo -e "$@" >&2
}
#############################################################################
#
# ANTI-ROOT FUSE
#
# This script should never be executed as root as can mess up the ownership,
# and SELinux labels in $PREFIX.
#
if [ "$(id -u)" = "0" ]; then
msg
msg "${BRED}Error: utility '${YELLOW}${PROGRAM_NAME}${BRED}' should not be used as root.${RST}"
msg
exit 1
fi
#############################################################################
#
# FUNCTION TO CHECK WHETHER DISTRIBUTION IS INSTALLED
#
# This is done by checking the presence of /bin directory in rootfs.
#
# Accepted arguments: $1 - name of distribution.
#
is_distro_installed() {
if [ -e "${INSTALLED_ROOTFS_DIR}/${1}/bin" ]; then
return 0
else
return 1
fi
}
#############################################################################
#
# FUNCTION TO INSTALL THE SPECIFIED DISTRIBUTION
#
# Installs the Linux distribution by the following algorithm:
#
# 1. Checks whether requested distribution is supported, if yes - continue.
# 2. Checks whether requested distribution is installed, if not - continue.
# 3. Source the distribution configuration plug-in which contains the
# functionality necessary for installation. It must define at least
# get_download_url() function which returns a download URL and SHA-256.
# 4. Download the rootfs archive, if it is not available in cache.
# 5. Verify the rootfs archive if we have a SHA-256 for it. Otherwise print
# a warning stating that integrity cannot be verified.
# 6. Extract the rootfs by using `tar` running under proot with link2symlink
# extension.
# 7. Write environment variables configuration to /etc/profile.d/termux-proot.sh.
# If profile.d directory is not available, append to /etc/profile.
# 8. Write default /etc/resolv.conf.
# 9. Write default /etc/hosts.
# 10. Add missing Android specific UIDs/GIDs to user database.
# 11. Execute optional setup hook (distro_setup) if present.
#
command_install() {
local distro_name
local override_alias
local distro_plugin_script
while (($# >= 1)); do
case "$1" in
--)
shift 1
break
;;
--help)
command_install_help
return 0
;;
--override-alias)
if [ $# -ge 2 ]; then
shift 1
if [ -z "$1" ]; then
msg
msg "${BRED}Error: argument to option '${YELLOW}--override-alias${BRED}' should not be empty.${RST}"
command_install_help
return 1
fi
override_alias="$1"
else
msg
msg "${BRED}Error: option '${YELLOW}$1${BRED}' requires an argument.${RST}"
command_install_help
return 1
fi
;;
-*)
msg
msg "${BRED}Error: unknown option '${YELLOW}${1}${BRED}'.${RST}"
command_install_help
return 1
;;
*)
if [ -z "${distro_name-}" ]; then
distro_name="$1"
else
msg
msg "${BRED}Error: unknown option '${YELLOW}${1}${BRED}'.${RST}"
msg
msg "${BRED}Error: you have already set distribution as '${YELLOW}${distro_name}${BRED}'.${RST}"
command_install_help
return 1
fi
;;
esac
shift 1
done
if [ -z "${distro_name-}" ]; then
msg
msg "${BRED}Error: distribution alias is not specified.${RST}"
command_install_help
return 1
fi
if [ -z "${SUPPORTED_DISTRIBUTIONS["$distro_name"]+x}" ]; then
msg
msg "${BRED}Error: unknown distribution '${YELLOW}${distro_name}${BRED}' was requested to be installed.${RST}"
msg
msg "${CYAN}Run '${GREEN}${PROGRAM_NAME} list${CYAN}' to see the supported distributions.${RST}"
msg
return 1
fi
if [ -n "${override_alias-}" ]; then
if [ ! -e "${DISTRO_PLUGINS_DIR}/${override_alias}.sh" ]; then
msg "${BLUE}[${GREEN}*${BLUE}] ${CYAN}Creating file '${DISTRO_PLUGINS_DIR}/${override_alias}.sh'...${RST}"
distro_plugin_script="${DISTRO_PLUGINS_DIR}/${override_alias}.override.sh"
cp "${DISTRO_PLUGINS_DIR}/${distro_name}.sh" "${distro_plugin_script}"
sed -i "s/^\(DISTRO_NAME=\)\(.*\)\$/\1\"${SUPPORTED_DISTRIBUTIONS["$distro_name"]} (override)\"/g" "${distro_plugin_script}"
SUPPORTED_DISTRIBUTIONS["${override_alias}"]="${SUPPORTED_DISTRIBUTIONS["$distro_name"]}"
distro_name="${override_alias}"
else
msg
msg "${BRED}Error: you cannot use value '${YELLOW}${override_alias}${BRED}' as alias override.${RST}"
msg
return 1
fi
else
distro_plugin_script="${DISTRO_PLUGINS_DIR}/${distro_name}.sh"
# Try an alternate distribution name.
if [ ! -f "${distro_plugin_script}" ]; then
distro_plugin_script="${DISTRO_PLUGINS_DIR}/${distro_name}.override.sh"
fi
fi
if is_distro_installed "$distro_name"; then
msg
msg "${BRED}Error: distribution '${YELLOW}${distro_name}${BRED}' is already installed.${RST}"
msg
msg "${CYAN}Log in: ${GREEN}${PROGRAM_NAME} login ${distro_name}${RST}"
msg "${CYAN}Reinstall: ${GREEN}${PROGRAM_NAME} reset ${distro_name}${RST}"
msg "${CYAN}Uninstall: ${GREEN}${PROGRAM_NAME} remove ${distro_name}${RST}"
msg
return 1
fi
if [ -f "${distro_plugin_script}" ]; then
msg "${BLUE}[${GREEN}*${BLUE}] ${CYAN}Installing ${YELLOW}${SUPPORTED_DISTRIBUTIONS["$distro_name"]}${CYAN}...${RST}"
if [ ! -d "${INSTALLED_ROOTFS_DIR}/${distro_name}" ]; then
msg "${BLUE}[${GREEN}*${BLUE}] ${CYAN}Creating directory '${INSTALLED_ROOTFS_DIR}/${distro_name}'...${RST}"
mkdir -m 755 -p "${INSTALLED_ROOTFS_DIR}/${distro_name}"
fi
if [ -d "${INSTALLED_ROOTFS_DIR}/${distro_name}/.l2s" ]; then
export PROOT_L2S_DIR="${INSTALLED_ROOTFS_DIR}/${distro_name}/.l2s"
fi
# We need this to disable the preloaded libtermux-exec.so library
# which redefines 'execve()' implementation.
unset LD_PRELOAD
# Needed for compatibility with some devices.
#export PROOT_NO_SECCOMP=1
# Some distributions store rootfs in subdirectory - in this case
# this variable should be set to 1.
DISTRO_TARBALL_STRIP_OPT=0
# Distribution plug-in contains steps on how to get download URL
# and further post-installation configuration.
source "${distro_plugin_script}"
local integrity_sha256
local download_url
if declare -f -F get_download_url >/dev/null 2>&1; then
integrity_sha256=$(get_download_url | cut -d'|' -f1)
download_url=$(get_download_url | cut -d'|' -f2-)
# If this is not a HEX string, then we probably got an URL instead of
# SHA-256. In this case treat that integrity checking was disabled.
if ! grep -qP '^[0-9a-fA-F]+$' <<< "${integrity_sha256}"; then
msg "${BLUE}[${RED}!${BLUE}] ${CYAN}Got malformed SHA-256 string, considering it as URL.${RST}"
download_url="$integrity_sha256"
integrity_sha256=""
fi
else
msg
msg "${BRED}Error: get_download_url() is not defined in ${distro_plugin_script}${RST}"
msg
return 1
fi
if [ -z "$download_url" ]; then
msg "${BLUE}[${RED}!${BLUE}] ${CYAN}Sorry, but distribution download URL is not defined for CPU architecture '$DISTRO_ARCH'.${RST}"
return 1
fi
if [ ! -d "$DOWNLOAD_CACHE_DIR" ]; then
msg "${BLUE}[${GREEN}*${BLUE}] ${CYAN}Creating directory '$DOWNLOAD_CACHE_DIR'...${RST}"
mkdir -p "$DOWNLOAD_CACHE_DIR"
fi
local tarball_name
tarball_name=$(basename "$download_url")
if [ ! -f "${DOWNLOAD_CACHE_DIR}/${tarball_name}" ]; then
msg "${BLUE}[${GREEN}*${BLUE}] ${CYAN}Downloading rootfs tarball...${RST}"
# Using temporary file as script can't distinguish the partially
# downloaded file from the complete. Useful in case if curl will
# fail for some reason.
msg
rm -f "${DOWNLOAD_CACHE_DIR}/${tarball_name}.tmp"
if ! curl --fail --retry 5 --retry-connrefused --retry-delay 5 --location \
--output "${DOWNLOAD_CACHE_DIR}/${tarball_name}.tmp" "$download_url"; then
msg "${BLUE}[${RED}!${BLUE}] ${CYAN}Download failure, please check your network connection.${RST}"
rm -f "${DOWNLOAD_CACHE_DIR}/${tarball_name}.tmp"
return 1
fi
msg
# If curl finished successfully, rename file to original.
mv -f "${DOWNLOAD_CACHE_DIR}/${tarball_name}.tmp" "${DOWNLOAD_CACHE_DIR}/${tarball_name}"
else
msg "${BLUE}[${GREEN}*${BLUE}] ${CYAN}Using cached rootfs tarball...${RST}"
fi
if [ -n "${integrity_sha256}" ]; then
msg "${BLUE}[${GREEN}*${BLUE}] ${CYAN}Checking integrity, please wait...${RST}"
local actual_sha256
actual_sha256=$(sha256sum "${DOWNLOAD_CACHE_DIR}/${tarball_name}" | awk '{ print $1}')
if [ "${integrity_sha256}" != "${actual_sha256}" ]; then
msg "${BLUE}[${RED}!${BLUE}] ${CYAN}Integrity checking failed. Try to redo installation again.${RST}"
rm -f "${DOWNLOAD_CACHE_DIR}/${tarball_name}"
return 1
fi
else
msg "${BLUE}[${RED}!${BLUE}] ${CYAN}Integrity checking of downloaded rootfs has been disabled.${RST}"
fi
msg "${BLUE}[${GREEN}*${BLUE}] ${CYAN}Extracting rootfs, please wait...${RST}"
# --exclude='dev'||: - need to exclude /dev directory which may contain device files.
# --delay-directory-restore - set directory permissions only when files were extracted
# to avoid issues with Arch Linux bootstrap archives.
proot --link2symlink \
tar -C "${INSTALLED_ROOTFS_DIR}/${distro_name}" --warning=no-unknown-keyword \
--delay-directory-restore --preserve-permissions --strip="$DISTRO_TARBALL_STRIP_OPT" \
-xf "${DOWNLOAD_CACHE_DIR}/${tarball_name}" --exclude='dev'||:
# Write important environment variables to profile file as /bin/login does not
# preserve them.
local profile_script
if [ -d "${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/profile.d" ]; then
profile_script="${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/profile.d/termux-proot.sh"
else
profile_script="${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/profile"
fi
msg "${BLUE}[${GREEN}*${BLUE}] ${CYAN}Writing '$profile_script'...${RST}"
local LIBGCC_S_PATH
LIBGCC_S_PATH="/$(cd ${INSTALLED_ROOTFS_DIR}/${distro_name}; find usr/lib/ -name libgcc_s.so.1)"
cat <<- EOF >> "$profile_script"
export ANDROID_ART_ROOT=${ANDROID_ART_ROOT-}
export ANDROID_DATA=${ANDROID_DATA-}
export ANDROID_I18N_ROOT=${ANDROID_I18N_ROOT-}
export ANDROID_ROOT=${ANDROID_ROOT-}
export ANDROID_RUNTIME_ROOT=${ANDROID_RUNTIME_ROOT-}
export ANDROID_TZDATA_ROOT=${ANDROID_TZDATA_ROOT-}
export BOOTCLASSPATH=${BOOTCLASSPATH-}
export COLORTERM=${COLORTERM-}
export DEX2OATBOOTCLASSPATH=${DEX2OATBOOTCLASSPATH-}
export EXTERNAL_STORAGE=${EXTERNAL_STORAGE-}
export LANG=C.UTF-8
export PATH=\${PATH}:/data/data/io.neoterm/files/usr/bin:/system/bin:/system/xbin
export PREFIX=${PREFIX-/data/data/io.neoterm/files/usr}
export TERM=${TERM-xterm-256color}
export TMPDIR=/tmp
export PULSE_SERVER=127.0.0.1
export MOZ_FAKE_NO_SANDBOX=1
EOF
if [ "${LIBGCC_S_PATH}" != "/" ]; then
echo "${LIBGCC_S_PATH}" >> "${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/ld.so.preload"
chmod 644 "${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/ld.so.preload"
fi
unset LIBGCC_S_PATH
# /etc/resolv.conf may not be configured, so write in it our configuraton.
msg "${BLUE}[${GREEN}*${BLUE}] ${CYAN}Writing resolv.conf file (NS 1.1.1.1/1.0.0.1)...${RST}"
rm -f "${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/resolv.conf"
cat <<- EOF > "${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/resolv.conf"
nameserver 1.1.1.1
nameserver 1.0.0.1
EOF
# /etc/hosts may be empty by default on some distributions.
msg "${BLUE}[${GREEN}*${BLUE}] ${CYAN}Writing hosts file...${RST}"
cat <<- EOF > "${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/hosts"
# IPv4.
127.0.0.1 localhost.localdomain localhost
# IPv6.
::1 localhost.localdomain localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
EOF
# Add Android-specific UIDs/GIDs to /etc/group and /etc/gshadow.
msg "${BLUE}[${GREEN}*${BLUE}] ${CYAN}Registering Android-specific UIDs and GIDs...${RST}"
echo "aid_$(id -un):x:$(id -u):$(id -g):Android user:/:/usr/sbin/nologin" >> "${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/passwd"
echo "aid_$(id -un):*:18446:0:99999:7:::" >> "${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/shadow"
local g
for g in $(id -G); do
echo "aid_$(id -gn "$g"):x:${g}:root,aid_$(id -un)" >> "${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/group"
if [ -f "${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/gshadow" ]; then
echo "aid_$(id -gn "$g"):*::root,aid_$(id -un)" >> "${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/gshadow"
fi
done
# Ensure that proot will be able to bind fake /proc entries.
setup_fake_proc
# Run optional distro-specific hook.
if declare -f -F distro_setup >/dev/null 2>&1; then
msg "${BLUE}[${GREEN}*${BLUE}] ${CYAN}Running distro-specific configuration steps...${RST}"
(cd "${INSTALLED_ROOTFS_DIR}/${distro_name}"
distro_setup
)
fi
msg "${BLUE}[${GREEN}*${BLUE}] ${CYAN}Installation finished.${RST}"
msg
msg "${CYAN}Now run '${GREEN}$PROGRAM_NAME login $distro_name${CYAN}' to log in.${RST}"
msg
return 0
else
msg "${BLUE}[${RED}!${BLUE}] ${CYAN}Cannot find '${distro_plugin_script}' which contains distro-specific install functions.${RST}"
return 1
fi
}
# Special function for executing a command in rootfs.
# Can be used only inside distro_setup().
run_proot_cmd() {
if [ -z "${distro_name-}" ]; then
msg
msg "${BRED}Error: called run_proot_cmd() but \${distro_name} is not set. Possible cause: using run_proot_cmd() outside of distro_setup()?${RST}"
msg
return 1
fi
if [ -z "${DISTRO_ARCH-}" ]; then
msg
msg "${BRED}Error: called run_proot_cmd() but \${DISTRO_ARCH} is not set.${RST}"
msg
return 1
fi
local qemu_arg=""
if [ "$(uname -m)" != "$DISTRO_ARCH" ]; then
case "$DISTRO_ARCH" in
aarch64)
qemu_arg="-q @TERMUX_PREFIX@/bin/qemu-aarch64"
if [ ! -e "@TERMUX_PREFIX@/bin/qemu-aarch64" ]; then
msg
msg "${BRED}Error: package 'qemu-user-aarch64' is not installed.${RST}"
msg
return 1
fi
;;
armv7l|armv8l)
qemu_arg="-q @TERMUX_PREFIX@/bin/qemu-arm"
if [ ! -e "@TERMUX_PREFIX@/bin/qemu-arm" ]; then
msg
msg "${BRED}Error: package 'qemu-user-arm' is not installed.${RST}"
msg
return 1
fi
;;
i686)
qemu_arg="-q @TERMUX_PREFIX@/bin/qemu-i386"
if [ ! -e "@TERMUX_PREFIX@/bin/qemu-i386" ]; then
msg
msg "${BRED}Error: package 'qemu-user-i386' is not installed.${RST}"
msg
return 1
fi
;;
x86_64)
qemu_arg="-q @TERMUX_PREFIX@/bin/qemu-x86_64"
if [ ! -e "@TERMUX_PREFIX@/bin/qemu-x86_64" ]; then
msg
msg "${BRED}Error: package 'qemu-user-x86_64' is not installed.${RST}"
msg
return 1
fi
;;
*)
msg
msg "${BRED}Error: DISTRO_ARCH has unknown value '$DISTRO_ARCH'. Valid ones are: aarch64, armv7l, armv8l, i686, x86_64.${RST}"
msg
return 1
;;
esac
fi
proot \
$qemu_arg \
--kernel-release=5.4.0-faked \
--link2symlink \
--kill-on-exit \
--rootfs="${INSTALLED_ROOTFS_DIR}/${distro_name}" \
--root-id \
--cwd=/root \
--bind=/dev \
--bind="/dev/urandom:/dev/random" \
--bind=/proc \
--bind="/proc/self/fd:/dev/fd" \
--bind="/proc/self/fd/0:/dev/stdin" \
--bind="/proc/self/fd/1:/dev/stdout" \
--bind="/proc/self/fd/2:/dev/stderr" \
--bind=/sys \
--bind="${INSTALLED_ROOTFS_DIR}/${distro_name}/proc/.loadavg:/proc/loadavg" \
--bind="${INSTALLED_ROOTFS_DIR}/${distro_name}/proc/.stat:/proc/stat" \
--bind="${INSTALLED_ROOTFS_DIR}/${distro_name}/proc/.uptime:/proc/uptime" \
--bind="${INSTALLED_ROOTFS_DIR}/${distro_name}/proc/.version:/proc/version" \
--bind="${INSTALLED_ROOTFS_DIR}/${distro_name}/proc/.vmstat:/proc/vmstat" \
/usr/bin/env -i \
"HOME=/root" \
"LANG=C.UTF-8" \
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" \
"TERM=$TERM" \
"TMPDIR=/tmp" \
"$@"
}
# A function for preparing fake content for certain /proc
# entries which are known to be restricted on Android.
setup_fake_proc() {
mkdir -p "${INSTALLED_ROOTFS_DIR}/${distro_name}/proc"
chmod 700 "${INSTALLED_ROOTFS_DIR}/${distro_name}/proc"
if [ ! -f "${INSTALLED_ROOTFS_DIR}/${distro_name}/proc/.loadavg" ]; then
cat <<- EOF > "${INSTALLED_ROOTFS_DIR}/${distro_name}/proc/.loadavg"
0.54 0.41 0.30 1/931 370386
EOF
fi
if [ ! -f "${INSTALLED_ROOTFS_DIR}/${distro_name}/proc/.stat" ]; then
cat <<- EOF > "${INSTALLED_ROOTFS_DIR}/${distro_name}/proc/.stat"
cpu 1050008 127632 898432 43828767 37203 63 99244 0 0 0
cpu0 212383 20476 204704 8389202 7253 42 12597 0 0 0
cpu1 224452 24947 215570 8372502 8135 4 42768 0 0 0
cpu2 222993 17440 200925 8424262 8069 9 17732 0 0 0
cpu3 186835 8775 195974 8486330 5746 3 8360 0 0 0
cpu4 107075 32886 48854 8688521 3995 4 5758 0 0 0
cpu5 90733 20914 27798 1429573 2984 1 11419 0 0 0
intr 53261351 0 686 1 0 0 1 12 31 1 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7818 0 0 0 0 0 0 0 0 255 33 1912 33 0 0 0 0 0 0 3449534 2315885 2150546 2399277 696281 339300 22642 19371 0 0 0 0 0 0 0 0 0 0 0 2199 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2445 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 162240 14293 2858 0 151709 151592 0 0 0 284534 0 0 0 0 0 0 0 0 0 0 0 0 0 0 185353 0 0 938962 0 0 0 0 736100 0 0 1 1209 27960 0 0 0 0 0 0 0 0 303 115968 452839 2 0 0 0 0 0 0 0 0 0 0 0 0 0 160361 8835 86413 1292 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3592 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6091 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 35667 0 0 156823 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 138 2667417 0 41 4008 952 16633 533480 0 0 0 0 0 0 262506 0 0 0 0 0 0 126 0 0 1558488 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 8 0 0 6 0 0 0 10 3 4 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 12 1 1 83806 0 1 1 0 1 0 1 1 319686 2 8 0 0 0 0 0 0 0 0 0 244534 0 1 10 9 0 10 112 107 40 221 0 0 0 144
ctxt 90182396
btime 1595203295
processes 270853
procs_running 2
procs_blocked 0
softirq 25293348 2883 7658936 40779 539155 497187 2864 1908702 7229194 279723 7133925
EOF
fi
if [ ! -f "${INSTALLED_ROOTFS_DIR}/${distro_name}/proc/.uptime" ]; then
cat <<- EOF > "${INSTALLED_ROOTFS_DIR}/${distro_name}/proc/.uptime"
284684.56 513853.46
EOF
fi
if [ ! -f "${INSTALLED_ROOTFS_DIR}/${distro_name}/proc/.version" ]; then
cat <<- EOF > "${INSTALLED_ROOTFS_DIR}/${distro_name}/proc/.version"
Linux version 5.4.0-faked (termux@androidos) (gcc version 4.9.x (Faked /proc/version by Proot-Distro) ) #1 SMP PREEMPT Fri Jul 10 00:00:00 UTC 2020
EOF
fi
if [ ! -f "${INSTALLED_ROOTFS_DIR}/${distro_name}/proc/.vmstat" ]; then
cat <<- EOF > "${INSTALLED_ROOTFS_DIR}/${distro_name}/proc/.vmstat"
nr_free_pages 146031
nr_zone_inactive_anon 196744
nr_zone_active_anon 301503
nr_zone_inactive_file 2457066
nr_zone_active_file 729742
nr_zone_unevictable 164
nr_zone_write_pending 8
nr_mlock 34
nr_page_table_pages 6925
nr_kernel_stack 13216
nr_bounce 0
nr_zspages 0
nr_free_cma 0
numa_hit 672391199
numa_miss 0
numa_foreign 0
numa_interleave 62816
numa_local 672391199
numa_other 0
nr_inactive_anon 196744
nr_active_anon 301503
nr_inactive_file 2457066
nr_active_file 729742
nr_unevictable 164
nr_slab_reclaimable 132891
nr_slab_unreclaimable 38582
nr_isolated_anon 0
nr_isolated_file 0
workingset_nodes 25623
workingset_refault 46689297
workingset_activate 4043141
workingset_restore 413848
workingset_nodereclaim 35082
nr_anon_pages 599893
nr_mapped 136339
nr_file_pages 3086333
nr_dirty 8
nr_writeback 0
nr_writeback_temp 0
nr_shmem 13743
nr_shmem_hugepages 0
nr_shmem_pmdmapped 0
nr_file_hugepages 0
nr_file_pmdmapped 0
nr_anon_transparent_hugepages 57
nr_unstable 0
nr_vmscan_write 57250
nr_vmscan_immediate_reclaim 2673
nr_dirtied 79585373
nr_written 72662315
nr_kernel_misc_reclaimable 0
nr_dirty_threshold 657954
nr_dirty_background_threshold 328575
pgpgin 372097889
pgpgout 296950969
pswpin 14675
pswpout 59294
pgalloc_dma 4
pgalloc_dma32 101793210
pgalloc_normal 614157703
pgalloc_movable 0
allocstall_dma 0
allocstall_dma32 0
allocstall_normal 184
allocstall_movable 239
pgskip_dma 0
pgskip_dma32 0
pgskip_normal 0
pgskip_movable 0
pgfree 716918803
pgactivate 68768195
pgdeactivate 7278211
pglazyfree 1398441
pgfault 491284262
pgmajfault 86567
pglazyfreed 1000581
pgrefill 7551461
pgsteal_kswapd 130545619
pgsteal_direct 205772
pgscan_kswapd 131219641
pgscan_direct 207173
pgscan_direct_throttle 0
zone_reclaim_failed 0
pginodesteal 8055
slabs_scanned 9977903
kswapd_inodesteal 13337022
kswapd_low_wmark_hit_quickly 33796
kswapd_high_wmark_hit_quickly 3948
pageoutrun 43580
pgrotated 200299
drop_pagecache 0
drop_slab 0
oom_kill 0
numa_pte_updates 0
numa_huge_pte_updates 0
numa_hint_faults 0
numa_hint_faults_local 0
numa_pages_migrated 0
pgmigrate_success 768502
pgmigrate_fail 1670
compact_migrate_scanned 1288646
compact_free_scanned 44388226
compact_isolated 1575815
compact_stall 863
compact_fail 392
compact_success 471
compact_daemon_wake 975
compact_daemon_migrate_scanned 613634
compact_daemon_free_scanned 26884944
htlb_buddy_alloc_success 0
htlb_buddy_alloc_fail 0
unevictable_pgs_culled 258910
unevictable_pgs_scanned 3690
unevictable_pgs_rescued 200643
unevictable_pgs_mlocked 199204
unevictable_pgs_munlocked 199164
unevictable_pgs_cleared 6
unevictable_pgs_stranded 6
thp_fault_alloc 10655
thp_fault_fallback 130
thp_collapse_alloc 655
thp_collapse_alloc_failed 50
thp_file_alloc 0
thp_file_mapped 0
thp_split_page 612
thp_split_page_failed 0
thp_deferred_split_page 11238
thp_split_pmd 632
thp_split_pud 0
thp_zero_page_alloc 2
thp_zero_page_alloc_failed 0
thp_swpout 4
thp_swpout_fallback 0
balloon_inflate 0
balloon_deflate 0
balloon_migrate 0
swap_ra 9661
swap_ra_hit 7872
EOF
fi
}
# Usage info for command_install.
command_install_help() {
msg
msg "${BYELLOW}Usage: ${BCYAN}$PROGRAM_NAME ${GREEN}install ${CYAN}[${GREEN}DISTRIBUTION ALIAS${CYAN}]${RST}"
msg
msg "${CYAN}This command will create a fresh installation of specified Linux${RST}"
msg "${CYAN}distribution.${RST}"
msg
msg "${CYAN}Options:${RST}"
msg
msg " ${GREEN}--help ${CYAN}- Show this help information.${RST}"
msg
msg " ${GREEN}--override-alias [new alias] ${CYAN}- Set a custom alias for installed${RST}"
msg " ${CYAN}distribution.${RST}"
msg
msg "${CYAN}Selected distribution should be referenced by alias which can be${RST}"
msg "${CYAN}obtained by this command: ${GREEN}$PROGRAM_NAME list${RST}"
msg
show_version
msg
}
#############################################################################
#
# FUNCTION TO UNINSTALL SPECIFIED DISTRIBUTION
#
# Just deletes the rootfs of the given distribution.
#
# Accepted agruments: $1 - name of distribution.
#
command_remove() {
local distro_name
if [ $# -ge 1 ]; then
case "$1" in
-h|--help)
command_remove_help
return 0
;;
*) distro_name="$1";;
esac
else
msg
msg "${BRED}Error: distribution alias is not specified.${RST}"
command_remove_help
return 1
fi
if [ -z "${SUPPORTED_DISTRIBUTIONS["$distro_name"]+x}" ]; then
msg
msg "${BRED}Error: unknown distribution '${YELLOW}${distro_name}${BRED}' was requested to be removed.${RST}"
msg
msg "${CYAN}Use '${GREEN}${PROGRAM_NAME} list${CYAN}' to see which distributions are supported.${RST}"
msg
return 1
fi
# Not using is_distro_installed() here as we only need to know
# whether rootfs directory is available.
if [ ! -d "${INSTALLED_ROOTFS_DIR}/${distro_name}" ]; then
msg
msg "${BRED}Error: distribution '${YELLOW}${distro_name}${BRED}' is not installed.${RST}"
msg
return 1
fi
# Delete plugin with overridden alias.
if [ "${CMD_REMOVE_REQUESTED_RESET-false}" = "false" ] && [ -e "${DISTRO_PLUGINS_DIR}/${distro_name}.override.sh" ]; then
msg "${BLUE}[${GREEN}*${BLUE}] ${CYAN}Deleting ${DISTRO_PLUGINS_DIR}/${distro_name}.override.sh...${RST}"
rm -f "${DISTRO_PLUGINS_DIR}/${distro_name}.override.sh"
fi
msg "${BLUE}[${GREEN}*${BLUE}] ${CYAN}Wiping the rootfs of ${YELLOW}${SUPPORTED_DISTRIBUTIONS["$distro_name"]}${CYAN}...${RST}"
# Attempt to restore permissions so directory can be removed without issues.
chmod u+rwx -R "${INSTALLED_ROOTFS_DIR}/${distro_name}" > /dev/null 2>&1 || true
# There is still chance for failure.
if ! rm -rf "${INSTALLED_ROOTFS_DIR:?}/${distro_name:?}"; then
msg "${BLUE}[${RED}!${BLUE}] ${CYAN}Finished with errors. Some files probably were not deleted.${RST}"
return 1
fi
}
# Usage info for command_remove.
command_remove_help() {
msg
msg "${BYELLOW}Usage: ${BCYAN}$PROGRAM_NAME ${GREEN}remove ${CYAN}[${GREEN}DISTRIBUTION ALIAS${CYAN}]${RST}"
msg
msg "${CYAN}This command will uninstall the specified Linux distribution.${RST}"
msg
msg "${CYAN}Be careful when using it because you will not be prompted for${RST}"
msg "${CYAN}confirmation and all data saved within the distribution will${RST}"
msg "${CYAN}instantly gone.${RST}"
msg
msg "${CYAN}Selected distribution should be referenced by alias which can be${RST}"
msg "${CYAN}obtained by this command: ${GREEN}$PROGRAM_NAME list${RST}"
msg
show_version
msg
}
#############################################################################
#
# FUNCTION TO REINSTALL THE GIVEN DISTRIBUTION
#
# Just a shortcut for command_remove && command_install.
#
# Accepted arguments: $1 - distribution name.
#
command_reset() {
local distro_name
if [ $# -ge 1 ]; then
case "$1" in
-h|--help)
command_reset_help
return 0
;;
*) distro_name="$1";;
esac
else
msg
msg "${BRED}Error: distribution alias is not specified.${RST}"
command_reset_help
return 1
fi
if [ -z "${SUPPORTED_DISTRIBUTIONS["$distro_name"]+x}" ]; then
msg
msg "${BRED}Error: unknown distribution '${YELLOW}${distro_name}${BRED}' was requested to be reinstalled.${RST}"
msg
msg "${CYAN}Use '${GREEN}${PROGRAM_NAME} list${CYAN}' to see which distributions are supported.${RST}"
msg
return 1
fi
if [ ! -d "${INSTALLED_ROOTFS_DIR}/${distro_name}" ]; then
msg
msg "${BRED}Error: distribution '${YELLOW}${distro_name}${BRED}' is not installed.${RST}"
msg
return 1
fi
CMD_REMOVE_REQUESTED_RESET="true" command_remove "$distro_name"
command_install "$distro_name"
}
# Usage info for command_reset.
command_reset_help() {
msg
msg "${BYELLOW}Usage: ${BCYAN}$PROGRAM_NAME ${GREEN}reset ${CYAN}[${GREEN}DISTRIBUTION ALIAS${CYAN}]${RST}"
msg
msg "${CYAN}Reinstall the specified Linux distribution.${RST}"
msg
msg "${CYAN}Be careful when using it because you will not be prompted for${RST}"
msg "${CYAN}confirmation and all data saved within the distribution will${RST}"
msg "${CYAN}instantly gone.${RST}"
msg
msg "${CYAN}Selected distribution should be referenced by alias which can be${RST}"
msg "${CYAN}obtained by this command: ${GREEN}$PROGRAM_NAME list${RST}"
msg
show_version
msg
}
#############################################################################
#
# FUNCTION TO START SHELL OR EXECUTE COMMAND
#
# Starts root shell inside the rootfs of specified Linux distribution.
# If '--' with further arguments was specified, execute the root shell
# command and exit.
#
# Accepts arbitrary amount of arguments.
#
command_login() {
local isolated_environment=false
local use_termux_home=false
local no_link2symlink=false
local no_sysvipc=false
local fix_low_ports=false
local make_host_tmp_shared=false
local distro_name=""
local login_user="root"
local -a custom_fs_bindings
local need_qemu=false
while (($# >= 1)); do
case "$1" in
--)
shift 1
break
;;
--help)
command_login_help
return 0
;;
--fix-low-ports)
fix_low_ports=true
;;
--isolated)
isolated_environment=true
;;
--no-fake-proc)
msg "${BRED}Warning: option '${YELLOW}${1}${BRED}' is deprecated.${RST}"
;;
--termux-home)
use_termux_home=true
;;
--shared-tmp)
make_host_tmp_shared=true
;;
--bind)
if [ $# -ge 2 ]; then
shift 1
if [ -z "$1" ]; then
msg
msg "${BRED}Error: argument to option '${YELLOW}--bind${BRED}' should not be empty.${RST}"
command_login_help
return 1
fi
custom_fs_bindings+=("$1")
else
msg
msg "${BRED}Error: option '${YELLOW}$1${BRED}' requires an argument.${RST}"
command_login_help
return 1
fi
;;
--no-link2symlink)
no_link2symlink=true
;;
--no-sysvipc)
no_sysvipc=true
;;
--user)
if [ $# -ge 2 ]; then
shift 1
if [ -z "$1" ]; then
msg
msg "${BRED}Error: argument to option '${YELLOW}--user${BRED}' should not be empty.${RST}"
command_login_help
return 1
fi
login_user="$1"
else
msg
msg "${BRED}Error: option '${YELLOW}$1${BRED}' requires an argument.${RST}"
command_login_help
return 1
fi
;;
-*)
msg
msg "${BRED}Error: unknown option '${YELLOW}${1}${BRED}'.${RST}"
command_login_help
return 1
;;
*)
if [ -z "$1" ]; then
msg
msg "${BRED}Error: you should not pass empty command line arguments.${RST}"
command_login_help
return 1
fi
if [ -z "$distro_name" ]; then
distro_name="$1"
else
msg
msg "${BRED}Error: unknown option '${YELLOW}${1}${BRED}'.${RST}"
msg
msg "${BRED}Error: you have already set distribution as '${YELLOW}${distro_name}${BRED}'.${RST}"
command_login_help
return 1
fi
;;
esac
shift 1
done