Please use a browser that supports javascript

Bogeskov.dk

How to build a Debian chroot for Android devices

The goal for this exercise is to create a chrooted environment where you can run linux on a Android tablet or telephone.

Requirements:

All you need to accomplice this is:

  • Rooted Android device
    • With busybox installed
  • Linux installation (debian or ubuntu)
    • With debootstrap, qemu-user-static, gparted and adb
  • A MicroSD card to install it on

Process

Building chroot

On your debian-based linux installation you have to become root:

sudo -i

You then need to get the required packages:

apt-get install debootstrap qemu-user-static gparted

And make a directory where you can build the chroot. I choose to do this on the linux harddrive instead of the MicroSD card, since it's usually considerable faster.

mkdir /var/tmp/android

Install a Arm based debian image in said directory

debootstrap --foreign --arch=armel squeeze .
cp /usr/bin/qemu-arm-static usr/bin/
chroot . /bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
/debootstrap/debootstrap --second-stage

Then customization: Where to get packages from and, don't install recommended additional packages (to keep the installation tight):

echo "deb http://ftp.dk.debian.org/debian stable main contrib non-free" >  /etc/apt/sources.list
echo 'APT::Install-Recommends "false";' > /etc/apt/apt.conf

Then it's time to create a user and set passwords:

useradd -u 2000 -g users -d /home/$SUDO_USER -m -s /bin/bash $SUDO_USER
passwd $SUDO_USER
passwd

Give your user security clearence (http://elinux.org/Android_Security#Paranoid_network-ing):

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

gpasswd -a $SUDO_USER aid_net_bt_admin
gpasswd -a $SUDO_USER aid_net_bt
gpasswd -a $SUDO_USER aid_inet
gpasswd -a $SUDO_USER aid_inet_raw
gpasswd -a $SUDO_USER aid_inet_admin

Now the initial image is ready. You can the add packages you think you need like these:

Update apt sources (thanks bhumpa):

apt-get update

Install packages:

apt-get install less vim rsync openssh-client bash-completion

Cleanup and completion of chroot image:

mkdir /mnt/sdcard
exit
rm usr/bin/qemu-arm-static

Preparing MicroSD

  • Plug in the MicroSD card
  • Start gparted "sudo gparted"
  • resize vfat partition to make room for linux
  • add partition for linux
  • format partition with ext3
  • apply

This should make room on the MicroSD card for a linux chroot

and "dmesg" should yield something like this:

??? kernel: [ ???] mmc0: new SDHC card at address 0002
??? kernel: [ ???] mmcblk0: mmc0:0002 00000 14.9 GiB
??? kernel: [ ???]  mmcblk0: p1 p2

Then mount the newly created filesystem:

mkdir /var/tmp/sd
mount /dev/mmcblk0p2 /var/tmp/sd

Copy the chroot to the new partition, eject MicroSD card and clean up:

cp -a /var/tmp/android/. /var/tmp/sd
umount /var/tmp/sd
rm -rf /var/tmp/android /var/tmp/sd

Configure the Android Device

Plug the MicroSD cart into the device

connect to the device using:

sudo adb shell

locate the linux partition using

dmesg | grep mmc

This sould yield something like this:

<6>[ ???] mmc1: new high speed SDHC card at address 0002
<6>[ ???] mmcblk1: mmc1:0002 00000 14.9 GiB
<6>[ ???]  mmcblk1: p1 p2

Mount the /system partition read/write:

busybox mount -o remount,rw /system

And install the script:debian, (this script has the feature - if you type "exit 254" it does not unmount the filesystem, letting you run services in the background, like apache & openvpn)

Make it executable

chmod 755 /system/xbin/debian

And restore readonly of /system:

busybox mount -o remount,ro /system

Now you should be ready to run "debian"