Translation(s): none


Debian install apps on Android

There are a number of free and non-free apps and scripts for Android that allow you to run a Debian chroot on an Android device.

Maintained

Unmaintained

Manual installation in a chroot

Root your device

Install community Android variant

Vendor Android builds often have proprietary drivers/apps, anti-features, spyware or telemetry preinstalled. It is recommended to switch to a community maintained open source variant of Android.

Potentially useful tweaks

Choose Android devices architecture

Almost all Android devices have ARM processors and can use one of the Debian ports to ARM.

Modern high-end devices have 64-bit ARMv8 processors and can use the Debian arm64 architecture.

Some lower-end or older devices have 32-bit ARMv7 processors and can use the armhf architecture.

Some very old devices have very old 32-bit ARM processors and can only use the armel architecture.

Some rare devices have 64-bit x86 processors and can use the Debian amd64 architecture.

To find out which Debian architecture to use, look up the device, chip and processor on Wikipedia to see which version of ARM it supports and chose the appropriate port from the list above.

Choose install device, tool and location

The Debian install can be created directly on the Android device or on a second device running Linux such as a laptop or desktop.

You can use debootstrap, mmdebstrap or other system build tools to create a minimal Debian installation.

Most modern Android devices do not have a µSD card slot so the install will probably be placed on the internal storage.

Preparation

If creating the install on a second device, install the install creation tool that you chose above, using the Debian packages if the second device is running Debian.

If creating the install from a second device that doesn't use an ARM processor and then transferring it to an Android device that uses an ARM processor, install the qemu-user-static package to enable emulation of ARM programs. If the device you are running on cannot run binaries for the processor of the Android device natively or emulated, then you will need to use the debootstrap tool, specify the --foreign option when creating the install, transfer the install to the Android device and finalise the install in a second stage on the Android device before using the install.

Root

Most methods of creating a Debian install require root. Gain root using the right tool for the device where you will be creating the install. On Debian and Linux systems that is probably sudo -i. When using adb to connect to an Android device, use adb root and then adb shell to get a root shell.

Parameters

Set the parameters for the install based on the choices made above:

ARCH=arm64
TOOL=debootstrap

Set the appropriate Debian mirror. If you have a local mirror, replace the URL below with your local mirror.

MIRROR=http://deb.debian.org/debian

Set the appropriate Debian release:

RELEASE=stable

Set the appropriate Debian install variant. Use minbase for the most minimal option. For a slightly larger install, omit the variant option when creating the install.

VARIANT=minbase

Set the directory that will contain the Debian install and the location of the Debian install itself.

SDCARD=/mnt/sdcard
ROOT=$SDCARD/debian

Create install

The tools to create Debian installs are usually run like this:

$TOOL --arch=$ARCH --variant=$VARIANT $RELEASE $ROOT $MIRROR

Read the documentation for the tool you are using to ensure this is correct.

If the device you are running on cannot run binaries for the processor of the Android device natively or emulated, then the --foreign option will be needed here and after the transfer is done, a second-stage step will be needed.

Transfer install to internal storage

Create an archive of the Debian install:

tar -C $ROOT -czplf debian.tar.gz

Transfer the Debian install over to the device:

adb push debian.tar.gz debian.tar.gz

Create the directory where the install will be stored:

DEVSDCARD=/external_sd
DEVROOT=$DEVSDCARD/debian
adb shell mkdir $DEVROOT 

Unpack the Debian install archive:

adb shell tar -C $DEVROOT debian.tar.gz

Clean up:

rm -rf debian.tar.gz $ROOT

Advanced option using less temporary storage

If you don't have enough space on either the Android device or the second device to archive, transfer and unpack the Debian archive, you can use this section to transfer without using additional storage.

Set the location for the scripts and Debian install:

DEVSDCARD=/external_sd
DEVROOT=$DEVSDCARD/debian

Create a shell script called adb_shell.sh containing this:

adb forward tcp:12345 tcp:12345
adb shell busybox nc -lp 12345 -e "$@" &
sleep 1s
exec nc -q 1 localhost 12345

Create a shell script called unpack using this command:

cat > unpack <<EOF
#!/sbin/sh
mkdir $DEVROOT &&
tar -C $DEVROOT -xzf -
EOF

Transfer it to the device:

adb push unpack $DEVSDCARD/unpack

Transfer the chroot using tar:

tar -C $ROOT -czplf - . | ./adb_shell.sh $DEVSDCARD/unpack

Convenience run-debian tool

Create a script called run-debian for entering the Debian chroot, running a command and cleaning up afterwards:

cat > run-debian <<EOF
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:\$PATH
export HOME=/root
mount -o remount,exec,dev,suid $DEVSDCARD
for f in dev dev/pts proc sys ; do mount -o bind /\$f $DEVROOT/\$f ; done
test \$# -eq 0 && set -- /bin/bash -l
chroot $DEVROOT "\$@"
for f in sys proc dev/pts dev ; do umount $DEVROOT/\$f ; done
EOF

Transfer it to the device:

adb push unpack $DEVSDCARD/run-debian

Finalise install

If you used the debootstrap tool and the --foreign option above, you will need to finalise the install with a second stage. Most of the time this isn't needed.

adb shell $DEVROOT/run-debian debootstrap/debootstrap --second-stage

Using the installation

Use the run-debian tool to get a bash shell:

adb shell $DEVROOT/run-debian

Then you can install needed Debian packages as you normally would with a minimal Debian install.

Using SSHDroid you can get a chroot from the Android user interface rather than via adb shell.

Tips

Running Debian binaries outside the chroot

If you need to run binaries from inside the chroot outside the chroot, you can use ld.so:

export SDCARD=/mnt/sdcard
export ROOT=$SDCARD/debian
export LD_LIBRARY_PATH=$ROOT/lib:$ROOT/lib/arm-linux-gnueabi:$ROOT/lib/arm-linux-gnueabihf:$ROOT/usr/lib:$ROOT/usr/lib/arm-linux-gnueabi:$ROOT/usr/lib/arm-linux-gnueabihf
cd $ROOT
./lib/ld-linux-*.so* bin/ls

Available memory

Android pre-loads applications (in some case that the user has never started) when there is free memory. This reduces the memory available to applications in a chroot.

It looks like the *_MEM properties in /init.rc along with the /sys/module/lowmemorykiller/parameters/minfree could help.

Zygote starts SystemServer and SystemServer restarts zygote, so simply killing one of them won't work. The Android-native way of getting rid of zygote and all that descends from it is to just use the 'stop' command (in a script or through a remote (root) shell), to restart the whole Android environment you'd use the 'start' command:

stop # to stop zygote
# now do whatever you want without Android getting in the way. Once you're ready just use:
start # to start zygote

The display is now blank and ready for SDL. The input devices only partly work with SDL on the 8120 (write your own code to read /dev/input/event*) but graphics work well.

Group privileges on Android

Android uses predefined groups to control permissions. You will likely have to add these groups within your virtualized Linux environment to grant permission for your user to do useful things. For example, if you want to access the /sdcard storage area on your device, you will need to add yourself to group 1015 aid_sdcard_rw.

Refer to the official AOSP source for the complete list of users and groups. Here is the list for Marshmallow. The package android-permissions automatically sets up all groups used in an Android system (although you still have to manually add users to groups).

AF_INET privileges

On Android, you will need to add at least one group 3003 aid_inet for those processes which require access to creating sockets (other security guarded systems particular to Android may need addressing for other applications, search for 3003 aid_inet on the web for more detail).

APT privilege dropping on Android

The program apt-get (and all the other APT programs) drops privileges when doing network requests, thus fails because it does not have the inet group. One workaround is to add the user _apt to the group inet (3003) and also set it as the primary group for user _apt (because APT deliberately relinquish all groups except the primary one).

exim4 and mailman chroot on Android

As well as altering inet access, the Debian-exim user will have to be added to group 3003. Further, if you experience trouble in the exim mainlog for creating sockets during DNS, try dropping privileges by adding "deliver_drop_privilege=true" to the exim4.conf.template file. For mailman, the standard setup is required, as per the README.Debian file in /usr/share/doc. However the user list must also be added to the group 3003 to allow it to send mail.

Running GUI along side Android

You can also try running GUI apps and desktop environments along side Android. There are a number of X11 Server apps on the Google Play store that do a great job. One such X11 app is "XServer XSDL" which is free in the Google Play store.

An example of how to get this working. Start the chroot, start the X11 Server app, add "export DISPLAY=127.0.0.1:0" to your running chroot, then start your app or desktop environment, at this point you should see it open in the X11 Server app. YMMV though, not all Android devices have the ram or cpu to run this.

Integrate the Debian boot process

Debian gives you a huge array of server software to install and run on your Android device in a chroot. It is possible to start and stop everything using the rc scripts that all daemons in Debian install. If you don't want the shutdown procedure to halt/power-off your device, you need to remove some rc init scripts. This doesn't always work, so when you run stop in Debian, it might poweroff your device. We got it working on Blandroid by running these commands in the debian shell (via ssh is probably the easiest):

 update-rc.d -f halt remove
 update-rc.d -f reboot remove

On ?CyanogenMod, we had to remove a lot more scripts to prevent it from shutting down, like sendsigs. But then /etc/init.d/rc 0 no longer shutdown all of the Debian services.

 update-rc.d -f halt remove
 update-rc.d -f reboot remove
 update-rc.d -f sendsigs remove
 update-rc.d -f umountfs remove
 update-rc.d -f umountroot remove

You probably want to remove all of the networking stuff from your Debian chroot and let Android handle it, otherwise you might have Debian and Android fighting over the network config. Also, you need to replace the Debian call to kill all processes, because it will also kill all Android processes. Instead projects like Lil' Debi and Crouton (Debian chroot for ChromeOS) have a custom script to kill all processes running in the chroot.

The scripts to call to start and stop everything automatically:

handling /dev

As of version Lil' Debi v0.4.4, Android /dev/ is not bind-mounted in chroot. This means no /dev/block/, /dev/log/, /dev/graphics/ and such. Bind-mounting it there results in conflict between Android logger and syslog, so syslog users should not do that, unless they have workaround. Additionally, if /dev/ isn't bind-mounted some Android executables, such as *am*, *dalvikvm*, *logcat* and many others won't run from inside chroot.

Important Android Environment Variables

Android is a very limited environment, so there are some odd hacks in it. For example, Android does not support rpath for finding shared libraries. Android will only look in the hard-coded system path, i.e. /system/lib and /data/app-lib/com.myapp.packagename. You can make Android look for shared libraries in other paths using the env var LD_LIBRARY_PATH, and some Android apps with native executables rely on that hack.

Using **su -l** to login into the chroot may result in unsetting of some important Android variables, such as BOOTCLASSPATH and LD_LIBRARY_PATH.

Booting into Debian instead of Android

Download the zImage for your version of the Linux kernel that runs on your Android device to your laptop and run fastboot something like this:

fastboot -c "root=/dev/mmcblk0p3 rootwait" boot ./zImage