Commit | Line | Data |
---|---|---|
5a979ce6 FF |
1 | #!/bin/sh |
2 | ||
2f90522a OS |
3 | SCRIPTNAME=termux-chroot |
4 | show_usage () { | |
5 | echo "Usage: $SCRIPTNAME [command]" | |
5a979ce6 FF |
6 | echo "termux-chroot: Setup a chroot to mimic a normal Linux file system" |
7 | echo "" | |
2f90522a OS |
8 | echo "Execute a command in a chroot with traditional file system hierarchy" |
9 | echo "(having e.g. the folders /bin, /etc and /usr) within Termux." | |
10 | echo "If run without argument, the default shell will be executed" | |
11 | exit 0 | |
12 | } | |
13 | ||
14 | while getopts :h option | |
15 | do | |
16 | case "$option" in | |
17 | h) show_usage;; | |
18 | ?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1; | |
19 | esac | |
20 | done | |
21 | shift $(($OPTIND-1)) | |
5a979ce6 FF |
22 | |
23 | # For the /system/bin/linker(64) to be found: | |
24 | ARGS="-b /system:/system" | |
25 | ||
4480f511 | 26 | # On some devices /vendor is required for termux packages to work correctly |
27 | # See https://github.com/termux/proot/issues/2#issuecomment-303995382 | |
28 | ARGS="$ARGS -b /vendor:/vendor" | |
29 | ||
293296c4 FF |
30 | # Bind /data to include system folders such as /data/misc. Also $PREFIX |
31 | # and $HOME so that Termux programs with hard-coded paths continue to work: | |
32 | ARGS="$ARGS -b /data:/data" | |
33 | ||
4d85b114 | 34 | |
35 | # Bind Magisk binary directories so root works, closing per Issue #2100. | |
36 | if [ -d /sbin ] && [ -d /root ]; then | |
37 | # Both of these directories exist under Android even without Magisk installed, | |
38 | # The existence check is to ensure that it doesn't break if this changes. | |
39 | ARGS="$ARGS -b /sbin:/sbin -b /root:/root" | |
40 | fi | |
41 | ||
d32d5a28 FF |
42 | if [ -f /property_contexts ]; then |
43 | # Used by getprop (see https://github.com/termux/termux-packages/issues/1076) | |
44 | # but does not exist on Android 8. | |
45 | ARGS="$ARGS -b /property_contexts:/property_contexts" | |
46 | fi | |
5a979ce6 | 47 | |
c491cc49 FF |
48 | # Expose external and internal storage: |
49 | if [ -d /storage ]; then | |
50 | ARGS="$ARGS -b /storage:/storage" | |
51 | fi | |
52 | ||
5a979ce6 FF |
53 | # Mimic traditional Linux file system hierarchy - /usr: |
54 | ARGS="$ARGS -b $PREFIX:/usr" | |
55 | ||
56 | # Mimic traditional Linux file system hierarchy - other Termux dirs: | |
57 | for f in bin etc lib share tmp var; do | |
58 | ARGS="$ARGS -b $PREFIX/$f:/$f" | |
59 | done | |
60 | ||
61 | # Mimic traditional Linux file system hierarchy- system dirs: | |
62 | for f in dev proc; do | |
63 | ARGS="$ARGS -b /$f:/$f" | |
64 | done | |
65 | ||
66 | # Set /home as current directory: | |
67 | ARGS="$ARGS --cwd=/home" | |
68 | ||
69 | # Root of the file system: | |
70 | ARGS="$ARGS -r $PREFIX/.." | |
71 | ||
2f90522a OS |
72 | export HOME=/home |
73 | ||
74 | # Shell to execute: | |
5a979ce6 FF |
75 | PROGRAM=/bin/bash |
76 | if [ -x $HOME/.termux/shell ]; then | |
77 | PROGRAM=`readlink -f $HOME/.termux/shell` | |
78 | fi | |
2f90522a OS |
79 | # Execute shell if no command has been supplied |
80 | if [ -z "$1" ];then | |
81 | ARGS="$ARGS $PROGRAM -l" | |
82 | exec $PREFIX/bin/proot $ARGS | |
83 | else | |
84 | # When a command is executed directly instead of opening a shell run | |
85 | # the command in the current working directory instead of in /home | |
86 | ARGS="$ARGS --cwd=." | |
87 | # Start supplied command with "sh -c" so things like these work: | |
88 | # termux-chroot "make;make install" | |
89 | # termux-chroot "SOME_CONFIG=value ./configure" | |
90 | exec $PREFIX/bin/proot $ARGS sh -c "$*" | |
91 | fi |