September 2019, updated April 2023
There is much conflicting and outdated info online regarding settingup a Debian chroot on Android. The followingshould work on anyDebian-supported architecture withchroot
.
Tested (using a native/data/debian
folder withoutsdcard mounting) on:
bullseye --arch=arm64
stretch --arch=arm64
jessie --arch=armhf
Everything fromjupyter
(accessible via localhost on anyweb browser app) to MATLAB should run.
busybox
and a terminalemulatordebootstrap
Checkhttps://www.debian.org/releases to find the codename ofthe latest stable release. On a linux desktop:
sudo apt install debootstrapsudo debootstrap --arch=arm64 --variant=minbase --foreign stable \ debian http://ftp.debian.org/debian
--arch
may bearmhf
(or evenarmel
) for older (ARMv7/32-bit) devicesstable
is the current stable Debian release (change ifrequired)There are now 2 options for the “second stage”: either emulate thetarget (device) architecture, or actually do everything on thedevice.
Assuming working directory is thedebian
folder:
sudo apt install qemu-user-staticcp /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 beqemu-arm-static
etc.for older devicesdebian
folder to anext4
/ext3
system on the device (seecopying)chroot
(seechroot
)Complete remaining setup:
/debootstrap/debootstrap --second-stage
Tweak and fix connectivity (particularlyapt
internetaccess):
echo "contrib non-free" >> /etc/apt/sources.listgroupadd -g 1015 aid_sdcard_rwgroupadd -g 3001 aid_net_bt_admingroupadd -g 3002 aid_net_btgroupadd -g 3003 aid_inetgroupadd -g 3004 aid_inet_rawgroupadd -g 3005 aid_inet_adminusermod -aG aid_inet _aptusermod -g aid_inet _aptapt update
Install some basic apps:
apt upgrade -yqqapt 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 curl git gcc g++ make cmake build-essential gnupg2
Clean up:
apt autoremoveapt clean
If on a desktop,exit
thechroot
, thenrm usr/bin/qemu-*-static
before proceeding.
If already on the device, skip the followingcopying step.
Compress thedebian
directory preservingpermissions:
sudo tar -zcvf debian.tgz debian/
Copy to device:
adb push debian.tgz /mnt/sdcard/
, orUncompress on device to/data/debian
:
cd /datasu -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 /datafor f in dev dev/pts proc sys do mount -o bind /$f /data/debian/$fdonePATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH \ HOME=/root \ chroot /data/debian /bin/bash -l
Make sure toexit
andumount
when done.This is easiest using a script (below).
Place in e.g. /system/bin/debian
and execute assu -c debian
#!/system/bin/sh -xfunction 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/debianmount -o remount,exec,dev,suid /datamounted /data/debian/proc || mount -t proc proc /data/debian/procmounted /data/debian/sys || mount -t sysfs sysfs /data/debian/sysmounted /data/debian/dev || mount -o bind /dev /data/debian/devmounted /data/debian/tmp || mount -t tmpfs tmpfs /data/cachemounted /data/debian/dev/pts || mount -t devpts devpts /data/debian/dev/pts#sdcard storagemounted /data/debian/mnt/sdcard || mount -o rbind /sdcard /data/debian/mnt/sdcardfor dns in `getprop net.dns1` `getprop net.dns2`; do echo "nameserver $dns"done > /data/debian/etc/resolv.confPATH=/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), unmountif [ $? != 254 ]; then while read _ m _; do case "$m" in /data/debian/*) umount "$m" ;; esac done </proc/mounts for f in dev dev/pts tmp proc sys ; do mounted /data/debian/$f && \ umount /data/debian/$f donefi