termux-packages/packages/proot/termux-chroot
Oliver Schmidhauser 8ceb581ba6 termux-chroot: Specify command to execute
Allow a command to be specified when running termux-chroot.
Example:
```
$ termux-chroot ls /
bin  data  dev  etc  home  lib  proc  property_contexts  share  storage  system  tmp  usr  var  vendor
```
This makes it possible to run scripts that include a part that needs to
be run in a chrooted environment, like a compile script.
```
./configure
make
termux-chroot make install
```

Previously it had to be done like this:
```
./configure
make
termux-chroot
cd program
make install
exit
```
2017-08-16 23:47:26 +02:00

81 lines
2.3 KiB
Bash
Executable File

#!/bin/sh
SCRIPTNAME=termux-chroot
show_usage () {
echo "Usage: $SCRIPTNAME [command]"
echo "termux-chroot: Setup a chroot to mimic a normal Linux file system"
echo ""
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))
# For the /system/bin/linker(64) to be found:
ARGS="-b /system:/system"
# 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"
# 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"
# Used by getprop (see https://github.com/termux/termux-packages/issues/1076):
ARGS="$ARGS -b /property_contexts:/property_contexts"
# Expose external and internal storage:
if [ -d /storage ]; then
ARGS="$ARGS -b /storage:/storage"
fi
# 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/.."
export HOME=/home
# Shell to execute:
PROGRAM=/bin/bash
if [ -x $HOME/.termux/shell ]; then
PROGRAM=`readlink -f $HOME/.termux/shell`
fi
# 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