There is much conflicting and outdated info online regarding setting up a Debian chroot on Android. The following should work on any Debian-supported architecture with chroot
.
Tested (using a native /data/debian
folder without sdcard mounting) on:
stretch --arch=arm64
jessie --arch=armhf
Everything from jupyter
(accessible via localhost on any web browser app) to MATLAB should run.
busybox
and a terminal emulatordebootstrap
Check https://www.debian.org/releases/ to find the codename of the latest stable release. On a linux desktop:
sudo apt install debootstrap
mkdir debian
cd debian
sudo debootstrap --arch=arm64 --variant=minbase --foreign stretch \
. http://httpredir.debian.org/debian
--arch
may be armhf
(or even armel
) for older (ARMv7/32-bit) devicesstretch
is the current stable Debian release (change if required)There are now 2 options for the "second stage": either emulate the target (device) architecture, or actually do everything on the device.
Assuming working directory is the debian
folder:
sudo apt install qemu-user-static
cp /usr/bin/qemu-aarch64-static usr/bin/
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
chroot . /bin/bash
qemu
version may be qemu-arm-static
etc. for older devicesdebian
folder to an ext4
/ext3
system on the device (see copying)chroot
(see chroot
)Complete remaining setup:
/debootstrap/debootstrap --second-stage
Tweak and fix connectivity (particularly apt
internet access):
echo "contrib non-free" >> /etc/apt/sources.list
groupadd -g 3001 aid_net_bt_admin
groupadd -g 3002 aid_net_bt
groupadd -g 3003 aid_inet
groupadd -g 3004 aid_inet_raw
groupadd -g 3005 aid_inet_admin
usermod -aG aid_inet _apt
usermod -g aid_inet _apt
apt update
Install some basic apps:
apt upgrade -yqq
apt install less vim rsync openssh-server bash-completion
In fact, any packages/customisation can be done at this stage, e.g.:
apt install python3-dev python3-tk wget git gcc make cmake build-essential gnupg2
Clean up:
apt autoremove
apt clean
If on a desktop, exit
the chroot
, then rm usr/bin/qemu-*-static
before proceeding.
If already on the device, skip the following copying step.
Compress the debian
directory preserving permissions:
sudo tar -zcvf debian.tgz debian/
Copy to device:
adb push debian.tgz /mnt/sdcard/
, orUncompress on device to /data/debian
:
cd /data
su -c tar -zxvf /mnt/sdcard/debian.tgz
chroot
Superuser in a terminal emulator:
su
chroot
(probably put in a shell script):
mount -o remount,exec,dev,suid /data
for f in dev dev/pts proc sys
do mount -o bind /$f /data/debian/$f
done
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH \
HOME=/root \
chroot /data/debian /bin/bash -l
Make sure to exit
and umount
when done. This is easiest using a script (below).
Place in e.g. /sytem/xbin/debian
and execute as su -c debian
#!/system/bin/sh -x
function mounted() {
local m
while read _ m _; do
if [ x$m = x$1 ]; then
return 0
fi
done </proc/mounts
return 1
}
#sdcard partition ext3
#mounted /data/debian || mount -t ext3 /dev/block/mmcblk1p2 /data/debian
mount -o remount,exec,dev,suid /data
mounted /data/debian/proc || mount -t proc proc /data/debian/proc
mounted /data/debian/sys || mount -t sysfs sysfs /data/debian/sys
#mounted /data/debian/dev || mount -o bind /dev /data/debian/dev
#mounted /data/debian/tmp || mount -t tmpfs tmpfs /data/cache # TODO: ok?
mounted /data/debian/dev/pts || mount -t devpts devpts /data/debian/dev/pts
#sdcard storage
#mounted /data/debian/mnt/sdcard || busybox mount -o rbind /sdcard /data/debian/mnt/sdcard
for dns in `getprop net.dns1` `getprop net.dns2`; do
echo "nameserver $dns"
done > /data/debian/etc/resolv.conf
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
TERM=linux TMPDIR=/tmp HOME=/root \
chroot /data/debian/ /bin/bash -l
# if exit != 254 (special flag), unmount
if [ $? != 254 ]; then
while read _ m _; do
case "$m" in
/data/debian/*)
umount "$m"
;;
esac
done </proc/mounts
for f in dev dev/pts proc sys ; do
mounted /data/debian/$f && \
umount /data/debian/$f
done
fi