-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtermux-distro.sh
1700 lines (1601 loc) · 59.2 KB
/
termux-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
#!/data/data/com.termux/files/usr/bin/bash
################################################################################
# #
# Termux Distro Template. #
# #
# Template for installing Linux Distro in Termux. #
# #
# Copyright (C) 2023-2025 Jore <https://github.com/jorexdeveloper> #
# #
# 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 <https://www.gnu.org/licenses/>. #
# #
################################################################################
# shellcheck disable=SC2155
################################################################################
# Prevents running this program in the wrong environment. #
################################################################################
safety_check() {
if [ "${EUID}" = "0" ] || [ "$(id -u)" = "0" ]; then
msg -ae "Nope, I can't let you run this program with root permissions!"
msg -an "The least of your problems will be damaging system files."
msg -aq ""
fi
local pid="$(grep TracerPid "/proc/$$/status" | cut -d $'\t' -f 2)"
if [ "${pid}" != 0 ]; then
local name="$(grep Name "/proc/${pid}/status" | cut -d $'\t' -f 2)"
if [ "$name" = "proot" ]; then
msg -ae "Nope, I can't let you run this program within proot!"
msg -an "The least of your problems will be a very slow environment."
msg -aq ""
fi
fi
}
################################################################################
# Prints the distro an introducing message. #
################################################################################
print_intro() {
msg -t "Hey there, I'm ${AUTHOR}."
msg "I am here to help you to ${action:-install} ${DISTRO_NAME} in Termux."
}
################################################################################
# Checks if the device architecture is supported #
# Sets global variables: SYS_ARCH LIB_GCC_PATH #
################################################################################
check_arch() {
msg -t "First, lemme check if your device architecture is supported."
local arch
if [ -x "$(command -v getprop)" ]; then
arch="$(getprop ro.product.cpu.abi 2>>"${LOG_FILE}")"
elif [ -x "$(command -v uname)" ]; then
arch="$(uname -m 2>>"${LOG_FILE}")"
else
msg -q "Sorry, have I failed to get your device architecture."
fi
case "${arch}" in
"arm64-v8a" | "armv8l")
SYS_ARCH="arm64"
LIB_GCC_PATH="/usr/lib/aarch64-linux-gnu/libgcc_s.so.1"
;;
"armeabi" | "armv7l" | "armeabi-v7a")
SYS_ARCH="armhf"
LIB_GCC_PATH="/usr/lib/arm-linux-gnueabihf/libgcc_s.so.1"
;;
*) msg -q "Sorry, '${Y}${arch}${R}' is currently not supported." ;;
esac
msg -s "Yup, '${Y}${arch}${G}' is supported!"
}
################################################################################
# Updates installed packages and checks if the required commands that are not #
# pre-installed are installed, if not, attempts to install them #
################################################################################
check_pkgs() {
msg -t "Now lemme make sure that all your packages are up to date."
if [ -x "$(command -v pkg)" ] && pkg update -y < <(echo -e "y\ny\ny\ny\ny") &>>"${LOG_FILE}" && pkg upgrade -y < <(echo -e "y\ny\ny\ny\ny") &>>"${LOG_FILE}"; then # || apt-get -qq -o=Dpkg::Use-Pty=0 update -y &>>"${LOG_FILE}" || apt-get -qq -o=Dpkg::Use-Pty=0 -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confnew" dist-upgrade -y &>>"${LOG_FILE}"; then
msg -s "Yup, Everything looks good!"
else
msg -qm0 "Sorry, have I failed to update your packages."
fi
msg -t "Lemme also check if all the required packages are installed."
for package in awk basename curl du proot pulseaudio readlink realpath sed tar unzip xz; do
if ! [ -x "$(command -v "${package}")" ]; then
msg "Oops, looks like '${Y}${package}${C}' is missing! Let me install it now."
if pkg install -y "${package}" < <(echo -e "y\ny\ny\ny\ny") &>>"${LOG_FILE}"; then # || apt-get -qq -o=Dpkg::Use-Pty=0 install -y "${package}" &>>"${LOG_FILE}"; then
msg -s "Done, '${Y}${package}${G}' is now installed!"
else
msg -qm0 "Sorry, have I failed to install '${Y}${package}${R}'."
fi
fi
done
msg -s "Yup, you have all the required packages!"
unset package
}
################################################################################
# Checks if there is an existing rootfs directory, or a file with similar name #
# Sets global variables: KEEP_ROOTFS_DIRECTORY #
################################################################################
check_rootfs_directory() {
unset KEEP_ROOTFS_DIRECTORY
if [ -e "${ROOTFS_DIRECTORY}" ]; then
if [ -d "${ROOTFS_DIRECTORY}" ]; then
if [ -n "$(ls -UA "${ROOTFS_DIRECTORY}" 2>>"${LOG_FILE}")" ]; then
msg -tn "Wait, There is an existing rootfs directory of size: ..."
msg -a "\b\b\b${Y}$(du -sh "${ROOTFS_DIRECTORY}" 2>>"${LOG_FILE}" | awk '{print $1}' 2>>"${LOG_FILE}")${C}!"
msg "What should I do with it?"
msg -l "Use" "Delete" "Abort (default)"
msg -n "Select action: "
read -ren 1 reply
case "${reply}" in
1 | u | U)
msg "Okay, I shall proceed with it."
KEEP_ROOTFS_DIRECTORY=1
return
;;
2 | d | D) ;;
*) msg -q "Alright, aborting!" ;;
esac
unset reply
else
rmdir "${ROOTFS_DIRECTORY}" &>>"${LOG_FILE}"
return
fi
else
msg -t "Wait, There is a file of size (${Y}$(du -sh "${ROOTFS_DIRECTORY}" 2>>"${LOG_FILE}" | awk '{print $1}' 2>>"${LOG_FILE}")${C}) with the same name as the rootfs directory!"
if ! ask -n "Should I delete the it and proceed?"; then
msg -q "Alright, aborting!"
fi
fi
msg "Okay, deleting '${Y}${ROOTFS_DIRECTORY}${C}'!"
chmod 777 -R "${ROOTFS_DIRECTORY}" &>>"${LOG_FILE}"
if rm -rf "${ROOTFS_DIRECTORY}" &>>"${LOG_FILE}"; then
msg -s "Done, let's proceed."
else
msg -q "Sorry, have I failed to delete '${Y}${ROOTFS_DIRECTORY}${R}'."
fi
fi
}
################################################################################
# Downloads the rootfs archive if it does not exist in the current directory #
# Sets global variables: KEEP_ROOTFS_ARCHIVE #
################################################################################
download_rootfs_archive() {
unset KEEP_ROOTFS_ARCHIVE
if [ -z "${KEEP_ROOTFS_DIRECTORY}" ]; then
if [ -e "${ARCHIVE_NAME}" ]; then
if [ -f "${ARCHIVE_NAME}" ]; then
msg -t "Wait, There is an existing rootfs archive!"
if ! ask -n "Should I delete it and download a new one?"; then
msg "Okay, lemme use it."
KEEP_ROOTFS_ARCHIVE=1
return
fi
else
msg -t "Wait, There is a non-file with the same name as the rootfs archive!"
if ! ask -n "Should I delete it and proceed?"; then
msg -q "Alright, aborting!"
fi
fi
msg "Okay, deleting '${Y}${ARCHIVE_NAME}${C}'."
chmod 777 -R "${ARCHIVE_NAME}" &>>"${LOG_FILE}"
if rm -rf "${ARCHIVE_NAME}" &>>"${LOG_FILE}"; then
msg -s "Done, let's proceed."
else
msg -q "Sorry, have I failed to delete '${Y}${ARCHIVE_NAME}${R}'."
fi
fi
local tmp_dload="${ARCHIVE_NAME}.pending"
msg -t "Alright, now lemme download the rootfs archive. This might take a while."
if curl --disable --fail --location --progress-meter --retry-connrefused --retry 0 --retry-delay 3 --continue-at - --output "${tmp_dload}" "${BASE_URL}/${ARCHIVE_NAME}"; then
mv "${tmp_dload}" "${ARCHIVE_NAME}" &>>"${LOG_FILE}"
msg -s "Great, the rootfs download is complete!"
else
chmod 777 -R "${tmp_dload}" &>>"${LOG_FILE}"
rm -rf "${tmp_dload}" &>>"${LOG_FILE}"
msg -qm0 "Sorry, have I failed to download the rootfs archive."
fi
fi
}
################################################################################
# Checks the integrity of the rootfs archive #
################################################################################
verify_rootfs_archive() {
if [ -z "${KEEP_ROOTFS_DIRECTORY}" ]; then
msg -t "Give me a sec to make sure the rootfs archive is ok."
if grep --regexp="${ARCHIVE_NAME}$" <<<"${TRUSTED_SHASUMS}" 2>>"${LOG_FILE}" | "${SHASUM_CMD}" --quiet --check &>>"${LOG_FILE}"; then
msg -s "Yup, the rootfs archive looks fine!"
return
else
msg -q "Sorry, the rootfs archive is corrupted and not safe for installation."
fi
fi
}
################################################################################
# Extracts the contents of the rootfs archive #
################################################################################
extract_rootfs_archive() {
if [ -z "${KEEP_ROOTFS_DIRECTORY}" ]; then
msg -t "Now, grab a coffee while I extract the rootfs archive. This will take a while."
trap 'msg -e "Extraction process interupted. Clearing cache. "; echo -ne "${N}${V}"; chmod 777 -R "${ROOTFS_DIRECTORY}" &>>"${LOG_FILE}"; rm -rf "${ROOTFS_DIRECTORY}" &>>"${LOG_FILE}"; exit 1' HUP INT TERM
mkdir -p "${ROOTFS_DIRECTORY}"
set +e
if proot --link2symlink tar --strip="${ARCHIVE_STRIP_DIRS}" --delay-directory-restore --preserve-permissions --warning=no-unknown-keyword --extract --auto-compress --exclude="dev" --file="${ARCHIVE_NAME}" --directory="${ROOTFS_DIRECTORY}" --checkpoint=1 --checkpoint-action=ttyout="${I}${Y} I have extracted %{}T in %ds so far.%*\r${N}${V}" &>>"${LOG_FILE}"; then
msg -s "Finally, I am done extracting the rootfs archive!."
else
chmod 777 -R "${ROOTFS_DIRECTORY}" &>>"${LOG_FILE}"
rm -rf "${ROOTFS_DIRECTORY}" &>>"${LOG_FILE}"
msg -q "Sorry, have I failed to extract the rootfs archive."
fi
set -e
trap - HUP INT TERM
fi
}
################################################################################
# Creates a script used to login into the distro #
################################################################################
create_rootfs_launcher() {
msg -t "Lemme create a command to launch ${DISTRO_NAME}."
mkdir -p "$(dirname "${DISTRO_LAUNCHER}")" &>>"${LOG_FILE}" && cat >"${DISTRO_LAUNCHER}" 2>>"${LOG_FILE}" <<-EOF
#!${TERMUX_FILES_DIR}/usr/bin/bash
################################################################################
# #
# ${DISTRO_NAME} launcher, version ${VERSION_NAME} #
# #
# Launches ${DISTRO_NAME}. #
# #
# Copyright (C) 2023-2025 ${AUTHOR} <${GITHUB}> #
# #
################################################################################
custom_ids=""
login_name="${DEFAULT_LOGIN}"
distro_command=""
custom_bindings=""
share_tmp_dir=false
no_sysvipc=false
no_kill_on_exit=false
no_link2symlink=false
isolated_env=false
protect_ports=false
use_termux_ids=false
kernel_release="${KERNEL_RELEASE}"
# Process command line arguments
while [ "\${#}" -gt 0 ]; do
case "\${1}" in
--command*)
optarg="\${1//--command/}"
optarg="\${optarg//=/}"
if [ -z "\${optarg}" ]; then
shift
optarg="\${1}"
fi
if [ -z "\${optarg}" ]; then
echo "Option '--command' requires an argument."
exit 1
fi
distro_command="\${optarg}"
unset optarg
;;
--bind*)
optarg="\${1//--bind/}"
optarg="\${optarg//=/}"
if [ -z "\${optarg}" ]; then
shift
optarg="\${1}"
fi
if [ -z "\${optarg}" ]; then
echo "Option '--bind' requires an argument."
exit 1
fi
custom_bindings+=" --bind=\${optarg}"
unset optarg
;;
--share-tmp-dir)
share_tmp_dir=true
;;
--no-sysvipc)
no_sysvipc=true
;;
--no-link2symlink)
no_link2symlink=true
;;
--no-kill-on-exit)
no_kill_on_exit=true
;;
--isolated)
isolated_env=true
;;
--protect-ports)
protect_ports=true
;;
--use-termux-ids)
use_termux_ids=true
;;
--id*)
optarg="\${1//--id/}"
optarg="\${optarg//=/}"
if [ -z "\${optarg}" ]; then
shift
optarg="\${1}"
fi
if [ -z "\${optarg}" ]; then
echo "Option '--id' requires an argument."
exit 1
fi
custom_ids="\${optarg}"
unset optarg
;;
--kernel-release*)
optarg="\${1//--kernel-release/}"
optarg="\${optarg//=/}"
if [ -z "\${optarg}" ]; then
shift
optarg="\${1}"
fi
if [ -z "\${optarg}" ]; then
echo "Option '--kernel-release' requires an argument."
exit 1
fi
kernel_release="\${optarg}"
unset optarg
;;
-h | --help)
echo "Usage: $(basename "${DISTRO_LAUNCHER}") [OPTION]... [USERNAME]"
echo ""
echo "Login or execute a comand in ${DISTRO_NAME} as USERNAME (default=${DEFAULT_LOGIN})."
echo ""
echo "Options:"
echo " --command[=COMMAND] Execute COMAND in distro."
echo " --bind[=PATH] Make the content of PATH accessible in the"
echo " guest rootfs."
echo " --share-tmp-dir Bind TMPDIR (${TERMUX_FILES_DIR}/usr/tmp"
echo " if not set) to /tmp in the guest rootfs."
echo " --no-sysvipc Do not handle System V IPC syscalls in proot"
echo " (WARNING: use with caution)."
echo " --no-link2symlink Do not fake hard links with symbolic links"
echo " (WARNING: prevents hard link support in distro)."
echo " --no-kill-on-exit Do not kill running processes on command exit."
echo " --isolated Do not include host specific variables and"
echo " directories."
echo " --protect-ports Modify bindings to protected ports to use a"
echo " higher port number."
echo " --use-termux-ids Make the current user and group appear as that"
echo " of termux. (ignores '--id')"
echo " --id[=UID:GID] Make the current user and group appear as UID"
echo " and GID."
echo " --kernel-release[=STRING]"
echo " Make current kernel release appear as"
echo " STRING. (default='${KERNEL_RELEASE}')"
echo " -v, --version Print distro version and exit."
echo " -h, --help Print this information and exit."
echo ""
echo "Documentation: ${GITHUB}/${DISTRO_REPOSITORY}"
exit
;;
-v | --version)
echo "${DISTRO_NAME} launcher, version ${VERSION_NAME}."
echo "Copyright (C) 2023-2025 ${AUTHOR} <${GITHUB}>."
echo "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>."
echo ""
echo "This is free software, you are free to change and redistribute it."
echo "There is NO WARRANTY, to the extent permitted by law."
exit
;;
-*)
echo "Unrecognized argument/option '\${1}'."
echo "Try '$(basename "${DISTRO_LAUNCHER}") --help' for more information"
exit 1
;;
*) login_name="\${1}" ;;
esac
shift
done
# Prevent running as root
if [ "\${EUID}" = "0" ] || [ "\$(id -u)" = "0" ]; then
echo "Nope, I can't let you start ${DISTRO_NAME} with root permissions!"
echo "This can cause several issues and potentially damage your phone."
exit 1
fi
# Prevent running within a chroot environment
pid="\$(grep TracerPid "/proc/\$\$/status" | cut -d \$'\t' -f 2)"
if [ "\${pid}" != 0 ]; then
name="\$(grep Name "/proc/\${pid}/status" | cut -d \$'\t' -f 2)"
if [ "\$name" = "proot" ]; then
echo "Nope, I can't let you start ${DISTRO_NAME} within a chroot environment!"
echo "This can cause performance and other issues."
exit 1
fi
fi
unset pid name
# Check for login command
if [ -z "\${distro_command}" ]; then
# Prefer su as login command
if [ -x "${ROOTFS_DIRECTORY}/bin/su" ]; then
distro_command="su --login \${login_name}"
elif [ -x "${ROOTFS_DIRECTORY}/bin/login" ]; then
distro_command="login \${login_name}"
else
echo "Couldn't find any login command in the guest rootfs."
echo "Use '$(basename "${DISTRO_LAUNCHER}") --command[=COMMAND]'."
echo "See '$(basename "${DISTRO_LAUNCHER}") --help' for more information."
exit 1
fi
fi
# unset LD_PRELOAD in case termux-exec is installed
unset LD_PRELOAD
# Create directory where proot stores all hard link info
export PROOT_L2S_DIR="${ROOTFS_DIRECTORY}/.l2s"
if ! [ -d "\${PROOT_L2S_DIR}" ]; then
mkdir -p "\${PROOT_L2S_DIR}"
fi
# Create fake /root/.version required by some apps i.e LibreOffice
if [ ! -f "${ROOTFS_DIRECTORY}/root/.version" ]; then
mkdir -p "${ROOTFS_DIRECTORY}/root" && touch "${ROOTFS_DIRECTORY}/root/.version"
fi
# Launch command
launch_command="proot"
# Correct the size returned from lstat for symbolic links
launch_command+=" -L"
launch_command+=" --cwd=/root"
launch_command+=" --rootfs=${ROOTFS_DIRECTORY}"
# Turn off proot errors
# launch_command+=" --verbose=-1"
# Use termux UID/GID
if \${use_termux_ids}; then
launch_command+=" --change-id=\$(id -u):\$(id -g)"
elif [ -n "\${custom_ids}" ]; then
launch_command+=" --change-id=\${custom_ids}"
else
launch_command+=" --root-id"
fi
# Fake hard links using symbolic links
if ! "\${no_link2symlink}"; then
launch_command+=" --link2symlink"
fi
# Kill all processes on command exit
if ! "\${no_kill_on_exit}"; then
launch_command+=" --kill-on-exit"
fi
# Handle System V IPC syscalls in proot
if ! "\${no_sysvipc}"; then
launch_command+=" --sysvipc"
fi
# Make current kernel appear as kernel release
launch_command+=" --kernel-release=\${kernel_release}"
# Core file systems that should always be present.
launch_command+=" --bind=/dev"
launch_command+=" --bind=/dev/urandom:/dev/random"
launch_command+=" --bind=/proc"
launch_command+=" --bind=/proc/self/fd:/dev/fd"
launch_command+=" --bind=/proc/self/fd/0:/dev/stdin"
launch_command+=" --bind=/proc/self/fd/1:/dev/stdout"
launch_command+=" --bind=/proc/self/fd/2:/dev/stderr"
launch_command+=" --bind=/sys"
# Fake /proc/loadavg if necessary
if ! [ -r /proc/loadavg ]; then
launch_command+=" --bind=${ROOTFS_DIRECTORY}/proc/.loadavg:/proc/loadavg"
fi
# Fake /proc/stat if necessary
if ! [ -r /proc/stat ]; then
launch_command+=" --bind=${ROOTFS_DIRECTORY}/proc/.stat:/proc/stat"
fi
# Fake /proc/uptime if necessary
if ! [ -r /proc/uptime ]; then
launch_command+=" --bind=${ROOTFS_DIRECTORY}/proc/.uptime:/proc/uptime"
fi
# Fake /proc/version if necessary
if ! [ -r /proc/version ]; then
launch_command+=" --bind=${ROOTFS_DIRECTORY}/proc/.version:/proc/version"
fi
# Fake /proc/vmstat if necessary
if ! [ -r /proc/vmstat ]; then
launch_command+=" --bind=${ROOTFS_DIRECTORY}/proc/.vmstat:/proc/vmstat"
fi
# Fake /proc/sys/kernel/cap_last_cap if necessary
if ! [ -r /proc/sys/kernel/cap_last_cap ]; then
launch_command+=" --bind=${ROOTFS_DIRECTORY}/proc/.sysctl_entry_cap_last_cap:/proc/sys/kernel/cap_last_cap"
fi
# Bind /tmp to /dev/shm
launch_command+=" --bind=${ROOTFS_DIRECTORY}/tmp:/dev/shm"
if [ ! -d "${ROOTFS_DIRECTORY}/tmp" ]; then
mkdir -p "${ROOTFS_DIRECTORY}/tmp"
fi
chmod 1777 "${ROOTFS_DIRECTORY}/tmp"
# Add host system specific variables and directories
if ! "\${isolated_env}"; then
for dir in /apex /data/app /data/dalvik-cache /data/misc/apexdata/com.android.art/dalvik-cache /product /system /vendor; do
[ ! -d "\${dir}" ] && continue
dir_mode="\$(stat --format='%a' "\${dir}")"
if [[ \${dir_mode:2} =~ ^[157]$ ]]; then
launch_command+=" --bind=\${dir}"
fi
done
unset dir dir_mode
# Required by termux-api Android 11
if [ -e "/linkerconfig/ld.config.txt" ]; then
launch_command+=" --bind=/linkerconfig/ld.config.txt"
fi
# Used by getprop
if [ -f /property_contexts ]; then
launch_command+=" --bind=/property_contexts"
fi
launch_command+=" --bind=/data/data/com.termux/cache"
launch_command+=" --bind=${TERMUX_FILES_DIR}/home"
launch_command+=" --bind=${TERMUX_FILES_DIR}/usr"
if [ -d "${TERMUX_FILES_DIR}/apps" ]; then
launch_command+=" --bind=${TERMUX_FILES_DIR}/apps"
fi
if [ -r /storage ]; then
launch_command+=" --bind=/storage"
launch_command+=" --bind=/storage/emulated/0:/sdcard"
else
if [ -r /storage/self/primary/ ]; then
storage_path="/storage/self/primary"
elif [ -r /storage/emulated/0/ ]; then
storage_path="/storage/emulated/0"
elif [ -r /sdcard/ ]; then
storage_path="/sdcard"
else
storage_path=""
fi
if [ -n "\${storage_path}" ]; then
launch_command+=" --bind=\${storage_path}:/sdcard"
launch_command+=" --bind=\${storage_path}:/storage/emulated/0"
launch_command+=" --bind=\${storage_path}:/storage/self/primary"
fi
unset storage_path
fi
if [ -n "\${EXTERNAL_STORAGE}" ]; then
launch_command+=" --bind=\${EXTERNAL_STORAGE}"
fi
fi
# Bind the tmp folder of the host system to the guest system (ignores --isolated)
if \${share_tmp_dir}; then
launch_command+=" --bind=\${TMPDIR:-${TERMUX_FILES_DIR}/usr/tmp}:/tmp"
fi
# Bind custom directories
launch_command+="\${custom_bindings}"
# Modify bindings to protected ports to use a higher port number.
if \${protect_ports}; then
launch_command+=" -p"
fi
# Setup the default environment
launch_command+=" /bin/env -i HOME=/root LANG=C.UTF-8 TERM=\${TERM:-xterm-256color} PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/games:/usr/local/bin:/usr/local/sbin:/usr/local/games:/system/bin:/system/xbin"
# Kill all running pulseaudio servers
if [ -x "\$(command -v killall)" ]; then
killall -qw -9 pulseaudio || true
fi
# Enable audio support in distro (for root users, add option '--system')
pulseaudio --start --load="module-native-protocol-tcp auth-ip-acl=127.0.0.1 auth-anonymous=1" --exit-idle-time=-1
# Execute launch command (exec replaces current shell)
exec \${launch_command} \${distro_command}
EOF
if ln -sfT "${DISTRO_LAUNCHER}" "${DISTRO_SHORTCUT}" &>>"${LOG_FILE}" && termux-fix-shebang "${DISTRO_LAUNCHER}" &>>"${LOG_FILE}" && chmod 700 "${DISTRO_LAUNCHER}" &>>"${LOG_FILE}"; then
msg -s "Done, launcher created successfully!"
else
msg -q "Sorry, have I failed to create the ${DISTRO_NAME} launcher."
fi
}
################################################################################
# Creates a script used to launch the vnc server in the distro #
################################################################################
create_vnc_launcher() {
msg -t "Lemme create a vnc wrapper in ${DISTRO_NAME}."
local vnc_launcher="${ROOTFS_DIRECTORY}/usr/local/bin/vnc"
mkdir -p "${ROOTFS_DIRECTORY}/usr/local/bin" &>>"${LOG_FILE}" && cat >"${vnc_launcher}" 2>>"${LOG_FILE}" <<-EOF
#!/bin/bash
################################################################################
# #
# vnc wrapper. #
# #
# This script starts the vnc server. #
# #
# Copyright (C) 2023-2025 ${AUTHOR} <${GITHUB}> #
# #
################################################################################
root_check() {
if [ "\${EUID}" = "0" ] || [ "\$(whoami)" = "root" ]; then
echo "Some applications may not work properly if you run as root."
echo -n "Continue anyway? (y/N) "
read -r reply
case "\${reply}" in
y | Y) return ;;
esac
echo "Abort."
return 1
fi
}
clean_tmp() {
if [ -n "\${DISPLAY}" ]; then
rm -rf "\${TMPDIR:-/tmp}/.X\${DISPLAY}-lock" "/tmp/.X11-unix/X\${DISPLAY}"
fi
}
set_geometry() {
case "\${ORIENTATION}" in
p) geometry="\${HEIGHT}x\${WIDTH}" ;;
*) geometry="\${WIDTH}x\${HEIGHT}" ;;
esac
}
start_session() {
if [ -e "\${HOME}/.vnc/passwd" ] || [ -e "\${HOME}/.config/tigervnc/passwd" ]; then
export HOME="\${HOME:-/root}"
export USER="\${USER:-root}"
LD_PRELOAD="${LIB_GCC_PATH}"
vncserver "\${DISPLAY}" -geometry "\${geometry}" -depth "\${DEPTH}" "\${@}"
else
vncpasswd && start_session
fi
}
check_status() {
vncserver -list "\${@}"
}
kill_session() {
vncserver -clean -kill "\${DISPLAY}" "\${@}" && clean_tmp
}
print_usage() {
echo "Usage \$(basename "\${0}") [<command>]."
echo ""
echo "Start vnc session."
echo ""
echo "Commands include:"
echo " kill Kill vnc session."
echo " status List active vnc sessions."
echo " landscape Use landscape (\${HEIGHT}x\${WIDTH}) orientation. (default)"
echo " potrait Use potrait (\${WIDTH}x\${HEIGHT}) orientation."
echo " help Print this message and exit."
echo ""
echo "Extra options are parsed to the installed vnc server, see vncserver(1)."
}
#############
# Entry point
#############
DEPTH=24
WIDTH=1440
HEIGHT=720
ORIENTATION="l"
opts=()
while [ "\${#}" -gt 0 ]; do
case "\${1}" in
p | potrait)
ORIENTATION=p
;;
l | landscape)
ORIENTATION=l
;;
s | status)
action=s
;;
k | kill)
action=k
;;
h | help)
print_usage
exit
;;
*) opts=("\${opts[@]}" "\${1}") ;;
esac
shift
done
if ! { [ -x "\$(command -v vncserver)" ] && [ -x "\$(command -v vncpasswd)" ]; }; then
echo "No vnc server found."
exit 1
fi
case "\${action}" in
k) kill_session "\${opts[@]}" ;;
s) check_status "\${opts[@]}" ;;
*) root_check && clean_tmp && set_geometry && start_session "\${opts[@]}" ;;
esac
EOF
if chmod 700 "${vnc_launcher}" &>>"${LOG_FILE}"; then
msg -s "Done, wrapper created successfully!"
else
msg -e "Sorry, have I failed to create the vnc wrapper."
fi
}
################################################################################
# Makes all the required configurations in the distro #
################################################################################
make_configurations() {
msg -t "Now, lemme make some configurations for you."
for config in fake_proc_setup android_ids_setup settings_configurations environment_variables_setup; do
status="$(${config} 2>>"${LOG_FILE}")"
if [ -n "${status//-0/}" ]; then
msg -e "Oops, ${config//_/ } failed with error code: (${status})"
fi
done
msg -s "Hopefully, that fixes some startup issues."
unset config status
}
################################################################################
# Sets a custom login shell in distro #
################################################################################
set_user_shell() {
if [ -x "${ROOTFS_DIRECTORY}/bin/chsh" ] && {
if [ -z "${shell}" ]; then
[ -f "${ROOTFS_DIRECTORY}/etc/passwd" ] && local default_shell="$(grep root "${ROOTFS_DIRECTORY}/etc/passwd" | cut -d: -f7)"
[ -z "${default_shell}" ] && default_shell="unknown"
ask -n -- -t "Do you want to change the default login shell from '${Y}${default_shell}${C}'?"
fi
}; then
local shells
mapfile -t shells < <(grep '^/bin' "${ROOTFS_DIRECTORY}"/etc/shells 2>>"${LOG_FILE}" | cut -d'/' -f3 2>>"${LOG_FILE}")
msg "Installed shells: ${Y}${shells[*]}${C}"
msg -n "Enter shell name:"
[ "${default_shell}" = "unknown" ] && default_shell="${shells[0]}"
read -rep " " -i "$(basename "${default_shell}")" shell
shell="$(basename "${shell}")"
if [[ ${shells[*]} == *"${shell}"* ]] && [ -x "${ROOTFS_DIRECTORY}/bin/${shell}" ] && {
distro_exec /bin/chsh -s "/bin/${shell}" root &>>"${LOG_FILE}"
if [ "${DEFAULT_LOGIN}" != "root" ]; then
distro_exec /bin/chsh -s "/bin/${shell}" "${DEFAULT_LOGIN}" &>>"${LOG_FILE}"
fi
}; then
msg -s "The default login shell is now '${Y}/bin/${shell}${G}'."
else
msg -e "Sorry, have I failed to set the default login shell to '${Y}${shell}${R}'."
ask -n -- "Wanna try again?" && set_user_shell
fi
unset shell
fi
}
################################################################################
# Sets a custom time zone in distro #
################################################################################
set_zone_info() {
if [ -x "${ROOTFS_DIRECTORY}/bin/ln" ] && {
if [ -z "${zone}" ]; then
local default_localtime="$(cat "${ROOTFS_DIRECTORY}/etc/timezone" 2>>"${LOG_FILE}")"
[ -z "${default_localtime}" ] && default_localtime="unknown"
ask -n -- -t "Do you want to change the local time from '${Y}${default_localtime}${C}'?"
fi
}; then
msg -n "Enter time zone (format='Country/City'):"
[ "${default_localtime}" = "unknown" ] && default_localtime="Etc/UTC"
read -rep " " -i "${default_localtime}" zone
if [ -f "${ROOTFS_DIRECTORY}/usr/share/zoneinfo/${zone}" ] && echo "${zone}" >"${ROOTFS_DIRECTORY}/etc/timezone" 2>>"${LOG_FILE}" && distro_exec "/bin/ln" -fs -T "/usr/share/zoneinfo/${zone}" "/etc/localtime" 2>>"${LOG_FILE}"; then
msg -s "The default time zone is now '${Y}${zone}${G}'."
else
msg -e "Sorry, have I failed to set the local time to '${Y}${zone}${R}'."
ask -n -- "Wanna try again?" && set_zone_info
fi
unset zone
fi
}
################################################################################
# Makes the necessary clean ups #
################################################################################
clean_up() {
if [ -z "${KEEP_ROOTFS_DIRECTORY}" ] && [ -z "${KEEP_ROOTFS_ARCHIVE}" ] && [ -f "${ARCHIVE_NAME}" ]; then
if ask -n -- -t "Can I remove the downloaded the rootfs archive to save space?"; then
msg "Okay, removing '!{Y}${ARCHIVE_NAME}${C}'"
chmod 777 -R "${ARCHIVE_NAME}" &>>"${LOG_FILE}"
if rm -rf "${ARCHIVE_NAME}" &>>"${LOG_FILE}"; then
msg -s "Done, the rootfs archive is gone!"
else
msg -e "Sorry, have I failed to remove the rootfs archive."
fi
else
msg "Alright, lemme leave the rootfs archive."
fi
fi
}
################################################################################
# Prints a message for successful installation with other useful information #
################################################################################
complete_msg() {
# Just for customizing message
if [ "${action}" = "install" ]; then
local name="installed"
else
local name="configured"
fi
msg -st "That's it, we have now successfuly ${name} ${DISTRO_NAME}."
msg "You can launch it by executing '${Y}$(basename "${DISTRO_LAUNCHER}")${C}' to login as '${Y}${DEFAULT_LOGIN}${C}'."
msg "If you want to login as another user, add the user name as an argument."
msg -t "I also think you might need a short form for '${Y}$(basename "${DISTRO_LAUNCHER}")${C}'."
msg "So have I created '${Y}$(basename "${DISTRO_SHORTCUT}")${C}' which is shorter."
msg -t "If you have further inquiries, read the documentation at:"
msg "${B}${U}${GITHUB}/${DISTRO_REPOSITORY}${L}${C}"
}
################################################################################
# Uninstalls the rootfs #
################################################################################
uninstall_rootfs() {
if [ -d "${ROOTFS_DIRECTORY}" ] && [ -n "$(ls -AU "${ROOTFS_DIRECTORY}" 2>>"${LOG_FILE}")" ]; then
msg -at "You are about to uninstall ${DISTRO_NAME} from '${Y}${ROOTFS_DIRECTORY}${C}'."
msg -ae "This action will delete all files (including valuable ones if any) in this directory!"
if ask -n0 -- -a "Confirm action."; then
msg -a "Uninstalling ${DISTRO_NAME}, just a sec."
chmod 777 -R "${ROOTFS_DIRECTORY}" &>>"${LOG_FILE}"
if rm -rf "${ROOTFS_DIRECTORY}" &>>"${LOG_FILE}"; then
msg -as "Done, ${DISTRO_NAME} uninstalled successfully!"
msg -a "Removing commands."
chmod 777 -R "${DISTRO_LAUNCHER}" "${DISTRO_SHORTCUT}" &>>"${LOG_FILE}"
if rm -rf "${DISTRO_LAUNCHER}" "${DISTRO_SHORTCUT}" &>>"${LOG_FILE}"; then
msg -as "Done, commands removed successfully!"
else
msg -ae "Sorry, have I failed to remove:"
msg -l "${DISTRO_LAUNCHER}" "${DISTRO_SHORTCUT}"
fi
else
msg -aq "Sorry, have I failed to uninstall ${DISTRO_NAME}."
fi
else
msg -a "Uninstallation aborted."
fi
else
msg -a "No rootfs found in '${ROOTFS_DIRECTORY}'."
fi
}
################################################################################
# Prints the program version information #
################################################################################
print_version() {
msg -a "${DISTRO_NAME} installer, version ${Y}${VERSION_NAME}${C}."
msg -a "Copyright (C) 2023-2025 ${AUTHOR} <${B}${U}${GITHUB}${L}${C}>."
msg -a "License GPLv3+: GNU GPL version 3 or later <${B}${U}https://gnu.org/licenses/gpl.html${L}${C}>."
msg -aN "This is free software, you are free to change and redistribute it."
msg -a "There is NO WARRANTY, to the extent permitted by law."
}
################################################################################
# Prints the program usage information #
################################################################################
print_usage() {
msg -a "Usage: ${Y}${PROGRAM_NAME}${C} [OPTION]... [DIRECTORY]"
msg -aN "Install ${DISTRO_NAME} in DIRECTORY."
msg -a "(default='${Y}${DEFAULT_ROOTFS_DIR}${C}')"
msg -aN "Options:"
msg -a " -d, --directory[=PATH] Change directory to PATH before execution."
msg -a " --install-only Installation only (use with caution)."
msg -a " --config-only Configurations only (if already installed)."
msg -a " -u, --uninstall Uninstall ${DISTRO_NAME}."
msg -a " --color[=WHEN] Enable/Disable color output if supported"
msg -a " (default='${Y}on${C}'). Valid arguments are:"
msg -a " [always|on] or [never|off]"
msg -a " -v, --version Print program version and exit."
msg -a " -h, --help Print this information and exit."
msg -a " -l, --log Create log file (${Y}${PROGRAM_NAME%.sh}.log${C})."
msg -aN "The install directory must be within '${Y}${TERMUX_FILES_DIR}${C}'"
msg -a "(or its sub-directories) to prevent permission issues."
msg -aN "Documentation: ${B}${U}${GITHUB}/${DISTRO_REPOSITORY}${L}${C}"
}
################################################################################
# Prepares fake content for certain /proc entries #
# Entries are based on values retrieved from Arch Linux (x86_64) running a VM #
# with 8 CPUs and 8GiB memory (some values edited to fit the distro) #
# Date: 2023.03.28, Linux 6.2.1 #
################################################################################
fake_proc_setup() {
local status=""
mkdir -p "${ROOTFS_DIRECTORY}/proc"
chmod 700 "${ROOTFS_DIRECTORY}/proc"
if [ ! -f "${ROOTFS_DIRECTORY}/proc/.loadavg" ]; then
cat <<-EOF >"${ROOTFS_DIRECTORY}/proc/.loadavg"
0.12 0.07 0.02 2/165 765
EOF
fi
status+="-${?}"
if [ ! -f "${ROOTFS_DIRECTORY}/proc/.stat" ]; then
cat <<-EOF >"${ROOTFS_DIRECTORY}/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
status+="-${?}"
if [ ! -f "${ROOTFS_DIRECTORY}/proc/.uptime" ]; then
cat <<-EOF >"${ROOTFS_DIRECTORY}/proc/.uptime"
5400.0 0.0
EOF
fi
status+="-${?}"
if [ ! -f "${ROOTFS_DIRECTORY}/proc/.version" ]; then
cat <<-EOF >"${ROOTFS_DIRECTORY}/proc/.version"
Linux version ${KERNEL_RELEASE} (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
status+="-${?}"
if [ ! -f "${ROOTFS_DIRECTORY}/proc/.vmstat" ]; then
cat <<-EOF >"${ROOTFS_DIRECTORY}/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