forked from termux/proot-distro
-
Notifications
You must be signed in to change notification settings - Fork 1
/
proot-distro.sh
executable file
·2600 lines (2392 loc) · 87.2 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 containers with Linux distributions.
##
## Originally created by Leonid Pliushch <[email protected]> for Termux
## project.
##
## 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="3.9.1"
#############################################################################
##
## >>> CONTRIBUTOR'S NOTICE <<<
##
## By contributing to the PRoot-Distro project you are obligated to follow
## the next mandatory rules:
##
## 1. The PRoot-Distro project is built around PRoot and is intended to be
## developed as a wrapper around PRoot. Supporting classical Linux chroot
## is outside of the project scope.
##
## 2. The PRoot-Distro is designed to work only in Termux. Supporting other
## distributions where PRoot is available is out of scope.
##
## 3. The PRoot-Distro is intended to be used by a non-root user. Using it
## as superuser (root) is possible but has a potential of messing up
## ownership of Termux environment directories and in some cases it also
## may wipe Android device data completely.
##
## The measures such as checking current user id must be used to prevent
## using PRoot-Distro by the root user.
##
## 4. PRoot-Distro is not a generic tool. Its idea is to provide user a
## high level intrument for managing Linux distributions for PRoot and
## hide the all underlying routines and automate everything that is
## possible. It is not all-in-one swiss-knife framework for PRoot.
## Therefore PRoot-Distro does not provide a way to modify its behavior.
##
## ***
##
## 5. Follow the general design.
##
## Proot Distro has a specific, well defined structure which makes it
## unique and separates from analogous projects. This structure expected
## to be followed during whole project lifetime. If you have ideas
## how to restructure the PRoot-Distro, then please keep them in your
## own fork! Source customization is not welcome.
##
## 5.1 PRoot-Distro is written in Bash script language with using features
## which are not compatible with POSIX shell. The script itself is
## monolithic and is not supposed to be split on multiple parts.
##
## 5.2 The tabs must be used for indenting code blocks everywhere.
##
## 5.3 All features are split on functions. There are 3 types of functions:
##
## * Commands: implement specific features of PRoot-Distro such as
## install, backup, login the distribution. These functions have
## following name scheme: 'command_name()'.
## * Command help: supplementary functions implementing informational
## output describing usage of PRoot-Distro commands. These functions
## are named as 'command_name_help()' and are defined AFTER the
## commands.
## * Utility functions: reusable or very long pieces of code.
##
## 5.4 Each command function definition should begin with comment section.
##
## Comment sections logically separate functions on groups and provide
## a brief overview which part of PRoot-Distro is implemented here. The
## comment must provide a description of how the function works.
##
## Utility functions are allowed to have a very brief description and
## command help functions.
##
## Command help functions should not have comments. They are self
## descriptive.
##
## 5.5 Code pieces that do non-obvious actions must be commented.
##
## 5.6 Functions are not allowed to define global variables. Use 'local'
## keyword to define the local variables instead.
##
## 5.7 Global variables must be defined either at beginning of PRoot-Distro
## script or at the entry point.
##
## 5.8 Use of getopt for options handling is considered as non-flexible
## and thus not allowed.
##
## 5.9 The checks for error conditions must be implemented. If error has
## been encountered, a message should be printed on stderr and further
## execution should be terminated.
##
## 5.10 If error was encountered inside function, exiting should be done
## using the command 'return 1' to pass a status code to the caller.
## Use of exit command is not allowed unless used in script entry point.
##
## 5.11 User input in command functions must be validated during the
## command line arguments handling.
##
## 5.12 Use heredocs to write files, especially if their content is long.
##
## 5.13 Certain configuration such as plug-in directory path may be set only
## during installation of the PRoot-Distro. Such configuration is
## treated as persistent and should not be changed by user.
##
## 5.14 PRoot-Distro uses string place holders to define certain common
## values for some variables:
##
## * @TERMUX_APP_PACKAGE@ - sets Termux app package (com.termux).
## * @TERMUX_PREFIX@ - sets the installation prefix path.
## * @TERMUX_HOME@ - sets the Termux home directory path.
##
## It is not allowed to hardcode the mentioned values instead of using
## the place holder.
##
## ***
##
## 6. All informational messages are printed in specific format.
##
## 6.1 Use function 'msg' to print messages.
##
## 6.2 Messages printed by help functions must fit in 76 columns. If the
## message is too long, split in on multiple lines. This is not relevant
## to errors and other informational messages. Pay extra attention to
## keep the message properly indented.
##
## 6.3 All messages printable by PRoot-Distro script must support colored
## text through the escape sequences defined below by variables such as
## RED (red), BRED (bold red text), YELLOW, BYELLOW, etc.
##
## 6.4 PRoot-Distro operation errors must have bold red color (${BRED}).
## The key information such as arguments caused an error should be
## encloded in quotes and highlighted by yellow color (${YELLOW}).
##
## 6.5 Messages should be terminated by ${RST} which resets the text colors
## and other attributes.
##
## 6.7 Informational messages produced by PRoot-Distro command steps must
## have the following format:
##
## ${BLUE}[${GREEN}*${BLUE}] ${CYAN}Message...${RST}"
##
## 6.8 If the step encountered a condition where a warning or error should
## be printed, the following message format should be used instead:
##
## ${BLUE}[${RED}!${BLUE}] ${CYAN}Warning message.${RST}
##
## ***
##
## 7. Handling distributions.
##
## 7.1 PRoot-Distro script is intended to be distribution-agnostic. That
## means it treats all distributions equally and handles them in the
## same way despite the possible differences at level of distribition
## root file system package.
##
## 7.2 Support of specific distribution is enabled by using a plug-in. The
## plug-in is a Bash script that is sourced by PRoot-Distro and has the
## following format:
##
## DISTRO_NAME="Example"
## DISTRO_COMMENT="Example distribution."
##
## TARBALL_STRIP_OPT=1
##
## TARBALL_URL['aarch64']="https://example.com/archive-aarch64.tar.gz"
## TARBALL_URL['arm']="https://example.com/archive-armv7.tar.gz"
## TARBALL_URL['i686']="https://example.com/archive-i386.tar.gz"
## TARBALL_URL['x86_64']="https://example.com/archive-amd64.tar.gz"
##
## TARBALL_SHA256['aarch64']="0000000000000000000000000000000000000000000000000000000000000000"
## TARBALL_SHA256['arm']="0000000000000000000000000000000000000000000000000000000000000000"
## TARBALL_SHA256['i686']="0000000000000000000000000000000000000000000000000000000000000000"
## TARBALL_SHA256['x86_64']="0000000000000000000000000000000000000000000000000000000000000000"
##
## distro_setup() {
## run_proot_cmd touch /etc/hello-world
## run_proot_cmd bash -c "echo '127.0.0.1 hello-world' >> /etc/hosts"
## }
##
## 7.4 The variable DISTRO_NAME specifies a full name of distribution such
## as "Ubuntu" or "Debian (stable)". This variable is mandatory.
##
## 7.3 The variable TARBALL_URL is a Bash associative array which contains
## URLs to distribution rootfs tarball for given CPU architectures.
## This variable is mandatory.
##
## 7.4 The variable TARBALL_SHA256 is a Bash associative array which
## contains SHA-256 checksums of rootfs tarballs for given CPU
## architectures. This variable is mandatory.
##
## 7.5 Post-installation steps that may be required for some distributions
## are defined in 'distro_setup()' function (optional). This function
## has access to all variables defined by PRoot-Distro during the
## execution of 'command_install()'.
##
## 7.6 Commands inside 'distro_setup()' that are intended to be executed in
## distribution environment must be defined as arguments of the
## command 'run_proot_cmd'.
##
## 7.7 Distributions must be adressed by their alias which in fact is a
## file name of plug-in except the extension part.
##
## 7.8 The alias files are located in $PREFIX/etc/proot-distro and have
## extensions .sh or .override.sh.
##
## * dist.sh name format is used for standard plug-ins.
## * dist2.override.sh name is used to indicate that this is a
## renamed distribution created by command 'rename' or by the
## option '--override-alias' of command 'install'.
##
## 7.9 The alias for distribution must be unique and PRoot-Distro should
## take care of that. User should not end with having two plug-ins
## for same alias, for example ubuntu.sh and ubuntu.override.sh.
##
## 7.10 The rootfs for distribution may be extracted only under PRoot
## session with active link2symlink extension.
##
## ***
##
## 8. Project versioning: major.minor.patch
##
## 8.1 Major version should be incremented when breaking changes were
## released. Examples are deprecated features, changed locations of
## files, command line format changes.
##
## 8.2 Minor version is incremented for significant but non-breaking
## changes such as added new features or upgraded distributions. The
## minor version is set to 0 when major version was incremented.
##
## 8.3 Patch version is incremented for small changes such as improvements
## to existing features and bug fixes. It is set to 0 when major or
## minor versions were incremented.
##
## ***
##
## 9. Warranty disclaimer.
##
## 9.1 PRoot-Distro has been created with aim to solve a specific range
## of tasks and its functionality may not align with your needs.
##
## 9.2 PRoot-Distro is a Bash script wrapper for PRoot. If PRoot does not
## work on your device, the PRoot-Distro will not work here as well.
## You shall not beg the authors of PRoot-Distro to fix the PRoot. Such
## requests will be ignored.
##
## 9.3 Certain functionality of PRoot-Distro relies on the Internet and
## services like GitHub. If you do not have the Internet connection or
## the GitHub is blocked by your ISP, then you may not be able to
## install distributions. Please do not beg for setting up a censorship
## resistant distribution tarball hosts.
##
## 9.4 The distributions are provided as-is, possibly with small changes
## to set of preinstalled packages and their configuration. Bugs that
## may exist in a specific distribution will not be fixed by authors
## of the PRoot-Distro.
##
## 9.5 PRoot-Distro authors make the final decision whether to accept
## submitted patches (pull requests) or not.
##
## 9.6 PRoot-Distro authors do not provide the time frames for fullfilling
## a specific request (new feature, etc).
##
## 9.7 PRoot-Distro does not have upgrade schedule. Authors work on the
## project development whenever have opportunity and desire to do so.
## Releases are submitted when ready. There no alpha or beta builds.
##
#############################################################################
#############################################################################
#
# 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"
# Default name servers.
DEFAULT_PRIMARY_NAMESERVER="8.8.8.8"
DEFAULT_SECONDARY_NAMESERVER="8.8.4.4"
# Default fake kernel version.
# Note: faking kernel version is required when using PRoot-Distro on
# old devices that are not compatible with up-to-date versions of GNU libc.
DEFAULT_FAKE_KERNEL_VERSION="6.2.1-PRoot-Distro"
# 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
# Disable termux-exec or other things which may interfere with proot.
# It is expected that all dependencies have fixed hardcoded paths according
# to Termux file system layout.
unset LD_PRELOAD
#############################################################################
#
# FUNCTION TO PRINT A MESSAGE TO CONSOLE
#
# Prints a given text string to stderr. Supports escape sequences.
#
#############################################################################
msg() {
echo -e "$@" >&2
}
#############################################################################
#
# DEPENDENCY CHECK
#
# Make sure all needed utilities are available in PATH before continuing.
#
#############################################################################
for i in awk basename bzip2 cat chmod cp curl cut du find grep gzip \
head id mkdir proot rm sed tar xargs xz; do
if [ -z "$(command -v "$i")" ]; then
msg
msg "${BRED}Utility '${i}' is not installed. Cannot continue.${RST}"
msg
exit 1
fi
done
unset i
#############################################################################
#
# 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: ${PROGRAM_NAME} should not be executed as root user.${RST}"
msg
exit 1
fi
#############################################################################
#
# ANTI NESTED PROOT FUSE
#
# Nested PRoot usage leads to performance degradation and other issues.
#
#############################################################################
TRACER_PID=$(grep TracerPid "/proc/$$/status" | cut -d $'\t' -f 2)
if [ "$TRACER_PID" != 0 ]; then
TRACER_NAME=$(grep Name "/proc/${TRACER_PID}/status" | cut -d $'\t' -f 2)
if [ "$TRACER_NAME" = "proot" ]; then
msg
msg "${BRED}Error: ${PROGRAM_NAME} should not be executed under PRoot.${RST}"
msg
exit 1
fi
unset TRACER_NAME
fi
unset TRACER_PID
#############################################################################
#
# FUNCTION TO INSTALL THE SPECIFIED DISTRIBUTION
#
# Brief algorithm how it works:
#
# 1. Process arguments supplied to 'install' command.
# 2. Ensure that requested distribution is supported and is not installed.
# 3. Source the distribution configuration plug-in.
# 4. Download the tarball of rootfs for requested distribution unless found
# in cache.
# 5. Verify SHA-256 checksum of the rootfs tarball.
# 6. Extract the rootfs under PRoot with link2symlink extension enabled.
# 7. Perform post-installation actions on distribution to make it ready.
#
#############################################################################
command_install() {
local distro_name
local override_alias
local distro_plugin_script
while (($# >= 1)); do
case "$1" in
--)
shift 1
break
;;
-h|--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
if ! grep -qP '^[a-z0-9][a-z0-9_.+\-]*$' <<< "$1"; then
msg
msg "${BRED}Error: argument to option '${YELLOW}--override-alias${BRED}' should start only with an alphanumeric character and consist of alphanumeric characters including symbols '_.+-'."
msg
return 1
fi
if grep -qP '^.*\.sh$' <<< "$1"; then
msg
msg "${BRED}Error: argument to option '${YELLOW}--override-alias${BRED}' should not end with '.sh'.${RST}"
msg
return 1
fi
override_alias="$1"
else
msg
msg "${BRED}Error: option '${YELLOW}--override-alias${BRED}' requires an argument.${RST}"
command_install_help
return 1
fi
;;
-*)
msg
msg "${BRED}Error: got unknown option '${YELLOW}${1}${BRED}'.${RST}"
command_install_help
return 1
;;
*)
if [ -z "${distro_name-}" ]; then
if [ -z "$1" ]; then
msg
msg "${BRED}Error: distribution alias argument should not be empty.${RST}"
command_install_help
return 1
fi
distro_name="$1"
else
msg
msg "${BRED}Error: got excessive positional argument '${YELLOW}${1}${BRED}'. Note that distribution can be specified only once.${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" ] && [ ! -e "${DISTRO_PLUGINS_DIR}/${override_alias}.override.sh" ]; then
msg "${BLUE}[${GREEN}*${BLUE}] ${CYAN}Creating file '${DISTRO_PLUGINS_DIR}/${override_alias}.override.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_alias}\"/g" "${distro_plugin_script}"
SUPPORTED_DISTRIBUTIONS["${override_alias}"]="${SUPPORTED_DISTRIBUTIONS["$distro_name"]}"
distro_name="${override_alias}"
else
msg
msg "${BRED}Error: distribution with alias '${YELLOW}${override_alias}${BRED}' already exists.${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 [ -d "${INSTALLED_ROOTFS_DIR}/${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
# Notify user if tar available in PATH is not GNU tar.
if ! grep -q 'tar (GNU tar)' <(tar --version 2>/dev/null | head -n 1); then
msg
msg "${BRED}Warning: tar binary that is available in PATH appears to be not a GNU tar. You may experience issues during installation, backup and restore operations.${RST}"
msg
fi
msg "${BLUE}[${GREEN}*${BLUE}] ${CYAN}Installing ${YELLOW}${SUPPORTED_DISTRIBUTIONS["$distro_name"]}${CYAN}...${RST}"
# Make sure things are cleared up on failure or user requested exit.
trap "echo -e \"\\r\\e[2K${BLUE}[${RED}!${BLUE}] ${CYAN}Exiting due to failure.${RST}\"; rm -rf \"${INSTALLED_ROOTFS_DIR:?}/${distro_name:?}\"; [ -e \"${DISTRO_PLUGINS_DIR}/${distro_name}.override.sh\" ] && rm -f \"${DISTRO_PLUGINS_DIR}/${distro_name}.override.sh\"; exit 1;" EXIT
trap "trap - EXIT; echo -e \"\\r\\e[2K${BLUE}[${RED}!${BLUE}] ${CYAN}Exiting immediately as requested.${RST}\"; rm -rf \"${INSTALLED_ROOTFS_DIR:?}/${distro_name:?}\"; [ -e \"${DISTRO_PLUGINS_DIR}/${distro_name}.override.sh\" ] && rm -f \"${DISTRO_PLUGINS_DIR}/${distro_name}.override.sh\"; exit 1;" HUP INT TERM
msg "${BLUE}[${GREEN}*${BLUE}] ${CYAN}Creating directory '${INSTALLED_ROOTFS_DIR}/${distro_name}'...${RST}"
mkdir -m 755 -p "${INSTALLED_ROOTFS_DIR}/${distro_name}"
export PROOT_L2S_DIR="${INSTALLED_ROOTFS_DIR}/${distro_name}/.l2s"
if [ ! -d "${INSTALLED_ROOTFS_DIR}/${distro_name}/.l2s" ]; then
echo -e "${BLUE}[${GREEN}*${BLUE}] ${CYAN}Creating directory '${PROOT_L2S_DIR}'...${RST}"
mkdir -p "$PROOT_L2S_DIR"
fi
# This should be overridden in distro plug-in with valid URL for
# each architecture where possible.
TARBALL_URL["aarch64"]=""
TARBALL_URL["arm"]=""
TARBALL_URL["i686"]=""
TARBALL_URL["x86_64"]=""
# This should be overridden in distro plug-in with valid SHA-256
# for corresponding tarballs.
TARBALL_SHA256["aarch64"]=""
TARBALL_SHA256["arm"]=""
TARBALL_SHA256["i686"]=""
TARBALL_SHA256["x86_64"]=""
# If your content inside tarball isn't stored in subdirectory,
# you can override this variable in distro plug-in with 0.
TARBALL_STRIP_OPT=1
# Distribution plug-in contains steps on how to get download URL
# and further post-installation configuration.
source "${distro_plugin_script}"
# Cannot proceed without URL and SHA-256.
if [ -z "${TARBALL_URL["$DISTRO_ARCH"]}" ]; then
msg "${BLUE}[${RED}!${BLUE}] ${CYAN}The distribution download URL is not defined for CPU architecture '${DISTRO_ARCH}'.${RST}"
return 1
fi
if ! grep -qP '^[0-9a-fA-F]{64}$' <<< "${TARBALL_SHA256["$DISTRO_ARCH"]}"; then
msg
msg "${BRED}Error: got malformed SHA-256 from plug-in script '${distro_plugin_script}'.${RST}"
msg
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 "${TARBALL_URL["$DISTRO_ARCH"]}")
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" "${TARBALL_URL["$DISTRO_ARCH"]}"; then
msg
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 "${TARBALL_SHA256["$DISTRO_ARCH"]}" ]; 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 [ "${TARBALL_SHA256["$DISTRO_ARCH"]}" != "${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.
set +e
proot --link2symlink \
tar -C "${INSTALLED_ROOTFS_DIR}/${distro_name}" --warning=no-unknown-keyword \
--delay-directory-restore --preserve-permissions --strip="${TARBALL_STRIP_OPT}" \
-xf "${DOWNLOAD_CACHE_DIR}/${tarball_name}" --exclude='dev' |& grep -v "/linkerconfig/" >&2
set -e
# 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
chmod u+rw "${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/profile" >/dev/null 2>&1 || true
profile_script="${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/profile"
fi
msg "${BLUE}[${GREEN}*${BLUE}] ${CYAN}Creating file '${profile_script}'...${RST}"
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-}
[ -z "\$LANG" ] && export LANG=C.UTF-8
export PATH=\${PATH}:@TERMUX_PREFIX@/bin:/system/bin:/system/xbin
export TERM=${TERM-xterm-256color}
export TMPDIR=/tmp
export PULSE_SERVER=127.0.0.1
export MOZ_FAKE_NO_SANDBOX=1
EOF
# Default /etc/resolv.conf may be empty or unsuitable for use.
msg "${BLUE}[${GREEN}*${BLUE}] ${CYAN}Creating file '${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/resolv.conf'...${RST}"
rm -f "${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/resolv.conf"
cat <<- EOF > "${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/resolv.conf"
nameserver ${DEFAULT_PRIMARY_NAMESERVER}
nameserver ${DEFAULT_SECONDARY_NAMESERVER}
EOF
# Default /etc/hosts may be empty or incomplete.
msg "${BLUE}[${GREEN}*${BLUE}] ${CYAN}Creating file '${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/hosts'...${RST}"
chmod u+rw "${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/hosts" >/dev/null 2>&1 || true
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}"
chmod u+rw "${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/passwd" \
"${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/shadow" \
"${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/group" \
"${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/gshadow" >/dev/null 2>&1 || true
echo "aid_$(id -un):x:$(id -u):$(id -g):Termux:/:/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 group_name group_id
while read -r group_name group_id; do
echo "aid_${group_name}:x:${group_id}:root,aid_$(id -un)" \
>> "${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/group"
if [ -f "${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/gshadow" ]; then
echo "aid_${group_name}:*::root,aid_$(id -un)" \
>> "${INSTALLED_ROOTFS_DIR}/${distro_name}/etc/gshadow"
fi
done < <(paste <(id -Gn | tr ' ' '\n') <(id -G | tr ' ' '\n'))
# 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 distribution-specific configuration steps...${RST}"
(cd "${INSTALLED_ROOTFS_DIR}/${distro_name}"
distro_setup
)
fi
# Reset trap for HUP/INT/TERM.
trap - EXIT
trap 'echo -e "\\r\\e[2K${BLUE}[${RED}!${BLUE}] ${CYAN}Exiting immediately as requested.${RST}"; exit 1;' HUP INT TERM
msg "${BLUE}[${GREEN}*${BLUE}] ${CYAN}Finished.${RST}"
msg
msg "${CYAN}Now run '${GREEN}${PROGRAM_NAME} login $distro_name${CYAN}' to log in.${RST}"
msg
return 0
else
# Reset trap for HUP/INT/TERM.
trap - EXIT
trap 'echo -e "\\r\\e[2K${BLUE}[${RED}!${BLUE}] ${CYAN}Exiting immediately as requested.${RST}"; exit 1;' HUP INT TERM
msg "${BLUE}[${RED}!${BLUE}] ${CYAN}Cannot find '${distro_plugin_script}' which is used to define a distribution properties.${RST}"
return 1
fi
}
# Special function for executing a command inside rootfs.
# Intended to be used inside plug-in distro_setup() function.
run_proot_cmd() {
if [ -z "${distro_name-}" ]; then
msg
msg "${BRED}Error: called run_proot_cmd() but \${distro_name} is not set. Make sure that run_proot_cmd() is used inside distro_setup() function.${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 [ "$DISTRO_ARCH" != "$DEVICE_CPU_ARCH" ]; then
local qemu_bin_path=""
# If CPU and host OS are 64bit, we can run 32bit guest OS without emulation.
# Everything else requires emulator (QEMU).
case "$DISTRO_ARCH" in
aarch64) qemu_bin_path="@TERMUX_PREFIX@/bin/qemu-aarch64";;
arm)
if [ "$DEVICE_CPU_ARCH" != "aarch64" ]; then
qemu_bin_path="@TERMUX_PREFIX@/bin/qemu-arm"
fi
;;
i686)
if [ "$DEVICE_CPU_ARCH" != "x86_64" ]; then
qemu_bin_path="@TERMUX_PREFIX@/bin/qemu-i386"
fi
;;
x86_64) qemu_bin_path="@TERMUX_PREFIX@/bin/qemu-x86_64";;
*)
msg
msg "${BRED}Error: DISTRO_ARCH has unknown value '$DISTRO_ARCH'. Valid values are: aarch64, arm, i686, x86_64."
msg
return 1
;;
esac
if [ -n "$qemu_bin_path" ]; then
if [ -x "$qemu_bin_path" ]; then
qemu_arg="-q ${qemu_bin_path}"
else
local qemu_user_pkg=""
case "$DISTRO_ARCH" in
aarch64) qemu_user_pkg="qemu-user-aarch64";;
arm) qemu_user_pkg="qemu-user-arm";;
i686) qemu_user_pkg="qemu-user-i386";;
x86_64) qemu_user_pkg="qemu-user-x86-64";;
*) qemu_user_pkg="qemu-user-${DISTRO_ARCH}";;
esac
msg
msg "${BRED}Error: package '${qemu_user_pkg}' is not installed.${RST}"
msg
return 1
fi
fi
fi
if [ -n "$qemu_arg" ]; then
if [ -d "/apex" ]; then
qemu_arg="${qemu_arg} --bind=/apex"
fi
if [ -e "/linkerconfig/ld.config.txt" ]; then
qemu_arg="${qemu_arg} --bind=/linkerconfig/ld.config.txt"
fi
qemu_arg="${qemu_arg} --bind=@TERMUX_PREFIX@"
qemu_arg="${qemu_arg} --bind=/system"
qemu_arg="${qemu_arg} --bind=/vendor"
if [ -f "/plat_property_contexts" ]; then
qemu_arg="${qemu_arg} --bind=/plat_property_contexts"
fi
if [ -f "/property_contexts" ]; then
qemu_arg="${qemu_arg} --bind=/property_contexts"
fi
fi
proot \
$qemu_arg -L \
--kernel-release="${DEFAULT_FAKE_KERNEL_VERSION}" \
--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 have restricted access on Android OS. All entries are based on
# values retrieved from Arch Linux (x86_64) running on a VM with 8 CPUs and 8
# GiB of memory. Date 2023.03.28, Linux 6.2.1. Some values edited to fit
# the PRoot-Distro.
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.12 0.07 0.02 2/165 765
EOF
fi
if [ ! -f "${INSTALLED_ROOTFS_DIR}/${distro_name}/proc/.stat" ]; then
cat <<- EOF > "${INSTALLED_ROOTFS_DIR}/${distro_name}/proc/.stat"
cpu 1957 0 2877 93280 262 342 254 87 0 0
cpu0 31 0 226 12027 82 10 4 9 0 0
cpu1 45 0 664 11144 21 263 233 12 0 0
cpu2 494 0 537 11283 27 10 3 8 0 0
cpu3 359 0 234 11723 24 26 5 7 0 0
cpu4 295 0 268 11772 10 12 2 12 0 0
cpu5 270 0 251 11833 15 3 1 10 0 0
cpu6 430 0 520 11386 30 8 1 12 0 0
cpu7 30 0 172 12108 50 8 1 13 0 0
intr 127541 38 290 0 0 0 0 4 0 1 0 0 25329 258 0 5777 277 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 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 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 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 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 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
ctxt 140223
btime 1680020856
processes 772
procs_running 2
procs_blocked 0
softirq 75663 0 5903 6 25375 10774 0 243 11685 0 21677
EOF
fi
if [ ! -f "${INSTALLED_ROOTFS_DIR}/${distro_name}/proc/.uptime" ]; then
cat <<- EOF > "${INSTALLED_ROOTFS_DIR}/${distro_name}/proc/.uptime"
124.08 932.80
EOF
fi
if [ ! -f "${INSTALLED_ROOTFS_DIR}/${distro_name}/proc/.version" ]; then
cat <<- EOF > "${INSTALLED_ROOTFS_DIR}/${distro_name}/proc/.version"
Linux version ${DEFAULT_FAKE_KERNEL_VERSION} (proot@termux) (gcc (GCC) 12.2.1 20230201, GNU ld (GNU Binutils) 2.40) #1 SMP PREEMPT_DYNAMIC Wed, 01 Mar 2023 00:00:00 +0000
EOF
fi
if [ ! -f "${INSTALLED_ROOTFS_DIR}/${distro_name}/proc/.vmstat" ]; then
cat <<- EOF > "${INSTALLED_ROOTFS_DIR}/${distro_name}/proc/.vmstat"
nr_free_pages 1743136
nr_zone_inactive_anon 179281
nr_zone_active_anon 7183
nr_zone_inactive_file 22858
nr_zone_active_file 51328
nr_zone_unevictable 642
nr_zone_write_pending 0
nr_mlock 0
nr_bounce 0
nr_zspages 0
nr_free_cma 0
numa_hit 1259626
numa_miss 0
numa_foreign 0
numa_interleave 720
numa_local 1259626
numa_other 0
nr_inactive_anon 179281
nr_active_anon 7183
nr_inactive_file 22858
nr_active_file 51328
nr_unevictable 642
nr_slab_reclaimable 8091
nr_slab_unreclaimable 7804
nr_isolated_anon 0
nr_isolated_file 0
workingset_nodes 0
workingset_refault_anon 0
workingset_refault_file 0
workingset_activate_anon 0
workingset_activate_file 0
workingset_restore_anon 0
workingset_restore_file 0
workingset_nodereclaim 0
nr_anon_pages 7723
nr_mapped 8905
nr_file_pages 253569
nr_dirty 0
nr_writeback 0
nr_writeback_temp 0
nr_shmem 178741
nr_shmem_hugepages 0
nr_shmem_pmdmapped 0
nr_file_hugepages 0
nr_file_pmdmapped 0
nr_anon_transparent_hugepages 1
nr_vmscan_write 0
nr_vmscan_immediate_reclaim 0
nr_dirtied 0
nr_written 0
nr_throttled_written 0
nr_kernel_misc_reclaimable 0
nr_foll_pin_acquired 0
nr_foll_pin_released 0
nr_kernel_stack 2780
nr_page_table_pages 344
nr_sec_page_table_pages 0
nr_swapcached 0
pgpromote_success 0
pgpromote_candidate 0
nr_dirty_threshold 356564
nr_dirty_background_threshold 178064
pgpgin 890508
pgpgout 0
pswpin 0
pswpout 0
pgalloc_dma 272
pgalloc_dma32 261
pgalloc_normal 1328079
pgalloc_movable 0
pgalloc_device 0