termux-chroot: Specify command to execute
[termux-packages] / packages / proot / termux-chroot
1 #!/bin/sh
2
3 SCRIPTNAME=termux-chroot
4 show_usage () {
5 echo "Usage: $SCRIPTNAME [command]"
6 echo "termux-chroot: Setup a chroot to mimic a normal Linux file system"
7 echo ""
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))
22
23 # For the /system/bin/linker(64) to be found:
24 ARGS="-b /system:/system"
25
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
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
34 # Used by getprop (see https://github.com/termux/termux-packages/issues/1076):
35 ARGS="$ARGS -b /property_contexts:/property_contexts"
36
37 # Expose external and internal storage:
38 if [ -d /storage ]; then
39 ARGS="$ARGS -b /storage:/storage"
40 fi
41
42 # Mimic traditional Linux file system hierarchy - /usr:
43 ARGS="$ARGS -b $PREFIX:/usr"
44
45 # Mimic traditional Linux file system hierarchy - other Termux dirs:
46 for f in bin etc lib share tmp var; do
47 ARGS="$ARGS -b $PREFIX/$f:/$f"
48 done
49
50 # Mimic traditional Linux file system hierarchy- system dirs:
51 for f in dev proc; do
52 ARGS="$ARGS -b /$f:/$f"
53 done
54
55 # Set /home as current directory:
56 ARGS="$ARGS --cwd=/home"
57
58 # Root of the file system:
59 ARGS="$ARGS -r $PREFIX/.."
60
61 export HOME=/home
62
63 # Shell to execute:
64 PROGRAM=/bin/bash
65 if [ -x $HOME/.termux/shell ]; then
66 PROGRAM=`readlink -f $HOME/.termux/shell`
67 fi
68 # Execute shell if no command has been supplied
69 if [ -z "$1" ];then
70 ARGS="$ARGS $PROGRAM -l"
71 exec $PREFIX/bin/proot $ARGS
72 else
73 # When a command is executed directly instead of opening a shell run
74 # the command in the current working directory instead of in /home
75 ARGS="$ARGS --cwd=."
76 # Start supplied command with "sh -c" so things like these work:
77 # termux-chroot "make;make install"
78 # termux-chroot "SOME_CONFIG=value ./configure"
79 exec $PREFIX/bin/proot $ARGS sh -c "$*"
80 fi