2016-03-22 02:39:26 +01:00
|
|
|
#!/bin/sh
|
|
|
|
|
2017-08-16 09:08:12 +02:00
|
|
|
SCRIPTNAME=termux-chroot
|
|
|
|
show_usage () {
|
|
|
|
echo "Usage: $SCRIPTNAME [command]"
|
2016-03-22 02:39:26 +01:00
|
|
|
echo "termux-chroot: Setup a chroot to mimic a normal Linux file system"
|
|
|
|
echo ""
|
2017-08-16 09:08:12 +02:00
|
|
|
echo "Execute a command in a chroot with traditional file system hierarchy"
|
|
|
|
echo "(having e.g. the folders /bin, /etc and /usr) within Termux."
|
|
|
|
echo "If run without argument, the default shell will be executed"
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
|
|
|
while getopts :h option
|
|
|
|
do
|
|
|
|
case "$option" in
|
|
|
|
h) show_usage;;
|
|
|
|
?) echo "$SCRIPTNAME: illegal option -$OPTARG"; exit 1;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $(($OPTIND-1))
|
2016-03-22 02:39:26 +01:00
|
|
|
|
|
|
|
# For the /system/bin/linker(64) to be found:
|
|
|
|
ARGS="-b /system:/system"
|
|
|
|
|
2017-05-25 22:15:42 +02:00
|
|
|
# On some devices /vendor is required for termux packages to work correctly
|
|
|
|
# See https://github.com/termux/proot/issues/2#issuecomment-303995382
|
|
|
|
ARGS="$ARGS -b /vendor:/vendor"
|
|
|
|
|
2017-06-13 16:21:56 +02:00
|
|
|
# Bind /data to include system folders such as /data/misc. Also $PREFIX
|
|
|
|
# and $HOME so that Termux programs with hard-coded paths continue to work:
|
|
|
|
ARGS="$ARGS -b /data:/data"
|
|
|
|
|
2018-03-10 22:35:53 +01:00
|
|
|
|
|
|
|
# Bind Magisk binary directories so root works, closing per Issue #2100.
|
|
|
|
if [ -d /sbin ] && [ -d /root ]; then
|
|
|
|
# Both of these directories exist under Android even without Magisk installed,
|
|
|
|
# The existence check is to ensure that it doesn't break if this changes.
|
|
|
|
ARGS="$ARGS -b /sbin:/sbin -b /root:/root"
|
|
|
|
fi
|
|
|
|
|
2020-03-02 16:50:47 +01:00
|
|
|
# Android 10 needs /apex for /system/bin/linker:
|
|
|
|
# https://github.com/termux/proot/issues/95#issuecomment-584779998
|
|
|
|
if [ -d /apex ]; then
|
|
|
|
ARGS="$ARGS -b /apex:/apex"
|
|
|
|
fi
|
|
|
|
|
2021-02-01 17:57:09 +01:00
|
|
|
# Android 11.
|
|
|
|
if [ -e "/linkerconfig/ld.config.txt" ]; then
|
|
|
|
ARGS="$ARGS -b /linkerconfig/ld.config.txt:/linkerconfig/ld.config.txt"
|
|
|
|
fi
|
|
|
|
|
2017-11-21 05:13:54 +01:00
|
|
|
if [ -f /property_contexts ]; then
|
|
|
|
# Used by getprop (see https://github.com/termux/termux-packages/issues/1076)
|
|
|
|
# but does not exist on Android 8.
|
|
|
|
ARGS="$ARGS -b /property_contexts:/property_contexts"
|
|
|
|
fi
|
2016-03-22 02:39:26 +01:00
|
|
|
|
2016-03-28 00:10:22 +02:00
|
|
|
# Expose external and internal storage:
|
|
|
|
if [ -d /storage ]; then
|
|
|
|
ARGS="$ARGS -b /storage:/storage"
|
|
|
|
fi
|
|
|
|
|
2016-03-22 02:39:26 +01:00
|
|
|
# Mimic traditional Linux file system hierarchy - /usr:
|
|
|
|
ARGS="$ARGS -b $PREFIX:/usr"
|
|
|
|
|
|
|
|
# Mimic traditional Linux file system hierarchy - other Termux dirs:
|
|
|
|
for f in bin etc lib share tmp var; do
|
|
|
|
ARGS="$ARGS -b $PREFIX/$f:/$f"
|
|
|
|
done
|
|
|
|
|
|
|
|
# Mimic traditional Linux file system hierarchy- system dirs:
|
|
|
|
for f in dev proc; do
|
|
|
|
ARGS="$ARGS -b /$f:/$f"
|
|
|
|
done
|
|
|
|
|
|
|
|
# Set /home as current directory:
|
|
|
|
ARGS="$ARGS --cwd=/home"
|
|
|
|
|
|
|
|
# Root of the file system:
|
|
|
|
ARGS="$ARGS -r $PREFIX/.."
|
|
|
|
|
2017-08-16 09:08:12 +02:00
|
|
|
# Shell to execute:
|
2016-03-22 02:39:26 +01:00
|
|
|
PROGRAM=/bin/bash
|
|
|
|
if [ -x $HOME/.termux/shell ]; then
|
|
|
|
PROGRAM=`readlink -f $HOME/.termux/shell`
|
|
|
|
fi
|
2018-06-24 08:38:02 +02:00
|
|
|
|
|
|
|
export HOME=/home
|
|
|
|
|
2017-08-16 09:08:12 +02:00
|
|
|
# Execute shell if no command has been supplied
|
|
|
|
if [ -z "$1" ];then
|
|
|
|
ARGS="$ARGS $PROGRAM -l"
|
|
|
|
exec $PREFIX/bin/proot $ARGS
|
|
|
|
else
|
|
|
|
# When a command is executed directly instead of opening a shell run
|
|
|
|
# the command in the current working directory instead of in /home
|
|
|
|
ARGS="$ARGS --cwd=."
|
|
|
|
# Start supplied command with "sh -c" so things like these work:
|
|
|
|
# termux-chroot "make;make install"
|
|
|
|
# termux-chroot "SOME_CONFIG=value ./configure"
|
|
|
|
exec $PREFIX/bin/proot $ARGS sh -c "$*"
|
|
|
|
fi
|