Skip to content

Instantly share code, notes, and snippets.

@xeffyr xeffyr/fakechroot
Last active Jul 12, 2018

Embed
What would you like to do?
Enter chroot environment without root in Termux
#!/data/data/com.termux/files/usr/bin/bash
##
## Simulate chroot in Termux (via proot)
##
## Leonid Plyushch (C) 2017-2018
##
## 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 <http://www.gnu.org/licenses/>.
##
## Preloaded libraries could cause some negative effects so LD_PRELOAD
## should be disabled.
unset LD_PRELOAD
usage()
{
echo
echo " Usage: fakechroot [OPTIONS] ROOTFS [COMMAND]"
echo
echo " Run command in fake chroot environment."
echo
echo " Options:"
echo " --help Show this help."
echo
echo " --mount-home Mount home directory of the host"
echo " as '/mnt/host-home'."
echo
echo " --mount-storage Mount internal storage (sdcard) of"
echo " the host as '/mnt/sdcard'."
echo
echo " --userspec UID:GID Make current user and group appear"
echo " as specified UID and GID. Use"
echo " numeric uids/gids that will be valid"
echo " on the target. (e.g. 1000:1000)"
echo
echo " --kernel-release VER Make current kernel version appear"
echo " as specified VER. Useful if you are"
echo " getting error 'FATAL: kernel too old'."
echo
}
## Disable mounts by default.
MOUNT_HOST_HOME=false
MOUNT_HOST_STORAGE=false
while getopts ":-:" opt; do
case "${opt}" in
-)
case "${OPTARG}" in
help)
usage
exit 0
;;
mount-home)
MOUNT_HOST_HOME=true
;;
mount-storage)
MOUNT_HOST_STORAGE=true
;;
userspec)
USERSPEC="${!OPTIND}"
OPTIND=$((OPTIND + 1))
if [ -z "${USERSPEC}" ]; then
echo
echo " [!] Option '--${OPTARG}' requires argument."
usage
exit 1
fi
;;
kernel-version)
KERNEL_VERSION="${!OPTIND}"
OPTIND=$((OPTIND + 1))
if [ -z "${KERNEL_VERSION}" ]; then
echo
echo " [!] Option '--${OPTARG}' requires argument."
usage
exit 1
fi
;;
*)
echo
echo " [!] Invalid option '${OPTARG}'."
usage
exit 1
;;
esac
;;
*)
echo
echo " [!] Invalid option '-${OPTARG}'."
usage
exit 1
;;
esac
done
shift $((OPTIND - 1))
if [ -z "${1}" ]; then
usage
exit 1
else
if [ ! -e "${1}" ]; then
echo
echo " [!] Directory '${1}' does not exist."
usage
exit 1
fi
if [ ! -d "${1}" ]; then
echo
echo " [!] File '${1}' is not a directory."
usage
exit 1
fi
ROOTFS="${1}"
shift 1
fi
## Compatibility with SELinux-enabled devices.
## If you are sill having errors, try to set
## environment variable PROOT_NO_SECCOMP.
USE_LINK2SYMLINK=true
## Proot can't work without temporary directory.
if [ -z "${TMPDIR}" ]; then
export TMPDIR="$(realpath "${PREFIX}/tmp")"
else
export TMPDIR="$(realpath "${TMPDIR}")"
fi
export PROOT_TMP_DIR="${TMPDIR}"
if ! mkdir -p "${TMPDIR}" > /dev/null 2>&1; then
echo " [!] Failed to create temporary directory."
echo " TMPDIR: ${TMPDIR}"
exit 1
fi
export PATH="${PATH}:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
## Use 'bash' as default command that will be executed
## in chrooted environment.
if [ ${#} -lt 1 ]; then
if [ ! -e "${ROOTFS}/bin/bash" ]; then
echo "[!] Cannot find '/bin/bash'. Perhaps rootfs is broken or"
echo " bash is not installed. Try to specify a different command."
exit 1
fi
set -- "/bin/bash"
fi
## Path to new root filesystem directory.
set -- "--rootfs=${ROOTFS}" "${@}"
## Populate /dev.
if [ -r "/dev/full" ]; then
set -- "--bind=/dev/full:/dev/full" "${@}"
fi
set -- "--bind=/dev/null:/dev/null" "${@}"
set -- "--bind=/dev/ptmx:/dev/ptmx" "${@}"
set -- "--bind=/dev/pts:/dev/pts" "${@}"
set -- "--bind=/dev/random:/dev/random" "${@}"
set -- "--bind=/dev/tty:/dev/tty" "${@}"
set -- "--bind=/dev/urandom:/dev/urandom" "${@}"
set -- "--bind=/dev/zero:/dev/zero" "${@}"
set -- "--bind=/proc/self/fd:/dev/fd" "${@}"
set -- "--bind=/proc/self/fd/0:/dev/stdin" "${@}"
set -- "--bind=/proc/self/fd/1:/dev/stdout" "${@}"
set -- "--bind=/proc/self/fd/2:/dev/stderr" "${@}"
## Bind procfs and sysfs.
set -- "--bind=/proc:/proc" "${@}"
set -- "--bind=/sys:/sys" "${@}"
## Mount home directory of the host to the specified
## location.
if ${MOUNT_HOST_HOME}; then
set -- "--bind=${HOME}:/mnt/host-home" "${@}"
fi
## Same as above but for internal storage (aka sdcard).
if ${MOUNT_HOST_STORAGE}; then
set -- "--bind=/storage/emulated/0:/mnt/sdcard" "${@}"
fi
## Set user/group.
if [ -n "${USERSPEC}" ]; then
set -- "--change-id=${USERSPEC}" "${@}"
else
set -- '--root-id' "${@}"
fi
## Set kernel version.
if [ -n "${KERNEL_VERSION}" ]; then
set -- "--kernel-release=${KERNEL_VERSION}" "${@}"
fi
set -- '--cwd=/' "${@}"
## Use link2symlink on SELinux-enabled devices.
if ${USE_LINK2SYMLINK}; then
set -- '--link2symlink' "${@}"
fi
proot --kill-on-exit "${@}"
exit "${?}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.