Installing Debian Lenny on Android

Original author: Jay Freeman (saurik)
  • Transfer
imageWell, Android has come out and even the source codes have been laid out. And we got the first Android device. What we really did not have was a game change in which we would suddenly gain control of our phones. You remember what Google promised us last year.
The reality is that even Google cannot influence the world of mobile devices. Those who have already received their G1 and have played with it for some time, were upset by the fact that we are still just ordinary users on their phone: T-Mobile did not allow us to use the root user, fearing that we wouldn’t do anything .

If you want to try this instruction in action, keep in mind that everything you do, you do at your own peril and risk.

Obtaining superuser privileges (root) / Jailbreaking

For me, restrictions of this kind turn the device into nothing more than a toy: those who have followed my adventures in the iPhone world know that I consider it a shame to wear a phone with a high-speed ARM processor running a modern operating system, a large screen, with many ways control / input, just to use it as another cell phone.

Fortunately, this changed a few days ago when someone found a serious vulnerability in the Android firmware. It was possible to get privileged rights. Just by typing telnetd in the console and connecting using the telnet client, you get root.

Unfortunately, it’s still hard to do anything worthwhile with Google’s replacement of busybox - toolbox. What we need is a full-fledged Unix environment. This phone is powerful enough, even to develop applications directly on it.

Installing Debian ARMEL

There have already been several attempts to install busybox on android. I think that we can go further and use this instruction to install the full Debian distribution, one of the highly respected Linux distributions that allows you to install whatever you want.
To do this, we need to think through several questions related to G1. First, where do we keep it? The phone has built-in flash memory, but there is only 128MB for the operating system and other applications.

Therefore, we drew attention to the more capacious part of the phone - a microSD card, which allows you to store up to 16GB of data. Unfortunately, for the ability to read it by all card readers, this card has a FAT file system, which is useless for storing Unix programs and data.
But here is what we came up with - we can store the Debian image on a memory card and then simply mount it.

Looking at the contents of / proc / filesystems, we can see all types of file systems that we can use: vfat, yaffs, yaffs2. On T-Mobile, only the minimum part of the drivers for the file systems required for the functioning of Android is installed. This means that we need to load a driver that would allow us to mount our image, since the kernel is built with module support.

If necessary, I can write a manual, because how to assemble modules for G1. I encourage people who are keen on discussing G1 development / modification topics. For discussion, you can subscribe to the G1-Hackers mailing list, which I keep on Telesphoreo.
I also support IRC on (the same server where Phone-oriented channels are located) on the #android channel, together with several other interested people.

Creating a Debian Disk Image

So, stop talking, let's get started! First, we need a Debian file system image, which we put on the phone’s memory card. To create it, we can use the existing Debian system. Fortunately, Debian already has full support for ARM EABI and even hasUseful instructions on how to install.

For people who do not want to mess with creating an image, I have already created a ready for 750MB, you can download the full image here (RapidShare). This image was packaged using bzip2 and takes ~ 85MB.

RapidShare is a rather slow server, so the guys from uploaded an image mirror on their servers. Details can be read here .

If you want to create an image yourself (maybe you need a different file system size for Debian other than 750MB, or just want to control the whole process), you can do this with the following commands: Build our Debian toolkit (kit)

apt-get install debootstrap
dd if=/dev/zero of=debian.img seek=749999999 bs=1 count=1
mke2fs -F debian.img
mkdir debian
mount -o loop debian.img debian
debootstrap --verbose --arch armel --foreign lenny debian
umount debian

Now we have the image of a debian, we need to copy it to a memory card. Also there you need to copy additional files. One important point of this step is that you need to install a driver that matches your firmware that you are using.

* ext2.ko ( RC19 ) ( RC29 / 30 ) - a standard Linux file system module
* unionfs.ko ( RC19 ) ( RC29 / 30 ) - allows you to combine folders (advanced)
* busybox - a set of system utilities

Copy this all (and debian.img) into a folder on the microSD card (I used a USB connection, which is simpler and faster than the rest). If you downloaded the finished image, then it makes sense to rename it to debian.img, so that it would be easier to use it later.

Installation and mounting

Where do we better put all this? I copied the tool kit (kit) to the root directory of the microSD card and put everything else in / data / local (a very useful folder where you can write normally).

We export the paths so that it would be possible not to collect constantly long paths. It will also make this instruction look semantically much easier to read.

export kit=/sdcard/kit
export bin=/data/local/bin
export mnt=/data/local/mnt

Next, you need to export several more paths, they will be needed by the programs that we will use in the future. This will save time when loading the driver for ext2. This may look strange (at least the HOME variable), but if we don't ask them later, major problems may arise. The next step is to copy busybox somewhere for future use. Since G1 does not contain the cp command, we will use cat for this. Note, the mkdir command does not have the -p option (since toolbox does not support it), so if you select a directory with a lot of nesting (or an existing one) you will have to ignore some errors or solve them differently.

export PATH=$bin:/usr/bin:/usr/sbin:/bin:$PATH
export TERM=linux
export HOME=/root

insmod $kit/ext2.ko

mkdir $bin #-p
cat $kit/busybox >$bin/busybox
chmod 755 $bin/busybox

busybox is installed, now we can use it to create a device node for the loopback driver. We need busybox, since G1 does not contain the mknod utility, which is needed to create a node. We also create aliases (short names) for busybox to make it easier to type commands. If you get an error at startup: _ not found, just try to reset it: Finally, the loopback device is created, we can connect the debian image! Note, as the debian.img image name, use the name of your image if it is different. Several basic errors can occur here: if the error is / etc / fstab, then apparently the $ kit / debian.img file does not exist, or the $ mnt variable may not be set. Final installation phase

alias _=busybox
_ mknod /dev/loop0 b 7 0

unalias _
alias _=busybox

_ mkdir -p $mnt
_ mount -o loop $kit/debian.img $mnt

At this point, we must end up with what debootstrap is unable to do. Firstly, you need to run several scripts and adjust the URL to the Debian repositories (for some reason, debootstrap confuses them, for some reason). If you have already downloaded the finished image, then you do not need to do these two commands. After these commands are executed (it may take almost 10 minutes), we can change the environment to debian using the chroot command. We get a shell and a complete debian environment, with all the cool stuff we can do. This command is performed by everyone, even those who downloaded the finished image. Next, we connect the devpts, proc and sysfs file systems to our environment. It is also necessary to set a password for the root user, so that we can remotely connect to the phone. Install OpenSSH
_ chroot $mnt /debootstrap/debootstrap --second-stage
echo 'deb lenny main' >$mnt/etc/apt/sources.list

_ chroot $mnt /bin/bash

mount -t devpts devpts /dev/pts
mount -t proc proc /proc
mount -t sysfs sysfs /sys

passwd root

This is where the full power of Debian is manifested: the quick installation of new programs on the phone. Well, let's install an SSH server in order to get a full console.
To do this, we will use APT (package management system in Debian). The first command is to update the package information in the repositories and install the updates. The second will install the ssh server itself. If you later want to start OpenSSH again (since the normal launch of the Debian services is not used), you can do this with the following command (or using “restart” instead of “start”). What next?
apt-get update
apt-get install openssh-server

/etc/init.d/ssh start

Have some fun! The first thing I did was install subversion, gcc and vim so that you could start developing. I repeat, people who want to learn more about the insides of Android (kernel drivers, access to hardware, the flashing process) can join the G1-Hackers mailing list .

After you restart the phone you will need to execute, if not all, then most of these commands again. For those who used the same paths as me, they can use scripts to reconnect ( script ).

It is also worth mentioning that while the image is mounted, Android will not be accessible via a USB cable. To do this, you need to unmount everything that was mounted before

_ umount $mnt/dev/pts $mnt/proc $mnt/sys $mnt

Although Android comes with the umount unmount utility, which works as it were, it still leaves a loopback device that will continue to block the microSD card for USB connection. You can delete it manually using the following command:

_ losetup -d /dev/loop0

Running Debian in /

The thing that upsets the most in this process is that we have to choose between android and debian. Here unionfs comes to the rescue.

Note, all subsequent commands must be executed outside the debian environment. To do this, type in the current telnet session:

or open another session. All commands can be downloaded as a finished script .
To run this script, you must already have exported all the standard variables. If you copied the script to $ kit, then you can execute it using the following command.

. $kit/

You can install this script using wget: For those who have not used a ready-made script: Now we have / etc contains files from Android and Debian. When changing / creating files in / etc, all modifications will be stored in the Debian section - this allows us to get full access to / etc!

_ chroot $mnt wget -O /tmp/
cat $mnt/tmp/ >$kit/
rm $mnt/tmp/

insmod $kit/unionfs.ko
mount -t unionfs -o dirs=$mnt/etc=rw:/etc=ro unionfs /etc

The next problem is that Android and Linux use different naming conventions for their dynamic linker. For Android, we have / system / bin / linker, and for Linux we have /lib/ This means that we will receive an error the file was not found when starting working programs. This can be easily fixed using symbolic links. At first glance, this may look dangerous, but it is not. If you change the phone’s root file system, it will change to “rootfs”: special Linux ramfs state (in-memory file system). This means that any changes that we make after rebooting the phone will disappear.

_ mount -o remount,rw /
_ ln -s $mnt/lib /

Now we can run most Debian programs without using chroot, just by running programs from $ mnt. Unfortunately, not everything will work, since most files are not where they should be. Let's fix this with the help of symbolic links. There are still a few directories that need to be fixed. First: / root is empty, so we can replace it with a link. Since we finished modifying the files in /, I highly recommend remounting the / root as read-only. There are two more / sbin and / dev left. There is no big deal with / dev. Since / dev / pts is mounted inside this folder, it cannot be merged using unionfs. To solve this problem, simply mount / dev / pts on top of unionfs:

for x in
bin boot home media mnt
opt selinux srv usr var
_ ln -s $mnt/$x /

rmdir /root
_ ln -s $mnt/root /
_ mount -o remount,ro /

mount -t unionfs -o dirs=$mnt/sbin=rw:/sbin=ro unionfs /sbin
mount -t unionfs -o dirs=$mnt/dev=rw:/dev=rw unionfs /dev
mount -t devpts devpts /dev/pts

Everything is ready for us, even for programs like openssh to work correctly, we will restart the service:

/etc/init.d/ssh restart

In conclusion,

this all looks good, but there are still a few problems that deserve attention:
First, it is not yet clear where the usernames and authentication information are stored. If you look at the file owners using ls, which is part of the debian, you will notice that they are not there.

The second, using the symlink for / instead of using chroot (so far it has not been possible to transparently load all services) means that any package trying to add a directory to / will fail. To install programs through apt-get you will have to use chroot.

And the last, RC30 update is coming soon, after which the jailbreak via Telnetd will no longer be valid. So until you play enough, do not install it.
(I hope I understand why telephone companies are so fond of selling devices that are deliberately limited in functionality.

Does this somehow improve their business?) If you find inconsistencies or any other errors in the article, please report.
The article with the translation is also uploaded to

Also popular now: