2014-05-10

Let's run (chrooted) Debian Wheezy on LG Optimus Prime 4X HD (P880)

I have been struggling quite a bit to find a lightweight ROM for my phone, but now that I have finally settled down I believe it's time to start playing for real with this smartphone.

This is one of the official pictures provided by vendor, probably photoshopped. Gotta love those glares! Imagine this: you are going to pay extra $$$ to have good photos made for your nice shiny new product, and then you photoshop them to add glare. Wonderful times we live in :)

My experiment will be to have a running Debian Wheezy 7 inside my Android, and I will use this article as basis; I had some issues with those instructions, thus I created this updated post. I know there is the nice Lil'Debi out there, but that would spoil most of the fun :)

In order to build this Debian I am giving up a 4GB external microSD card that is plugged in my phone already, so first thing unmount it by going to Settings -> Storage:


Make a backup before continuing because we are going to completely replace its content.

So, let's recap the needed ingredients:
- a rooted Android phone, I am using LG Optimus Prime 4x HD
- an external microSD card, minimum 2GB
- debootstrap command on your Linux system (it's available almost everywhere)
- adb & your phone's USB configured to receive adb commands

Ready to go!
Let's build a tarball of the base Debian system for our target architecture. I have added my scripts (from this gist) to ease setup, but I advise you go through each of them at least for learning purposes.

debootstrap --arch armel --foreign wheezy wheezy http://http.debian.net/debian/
cd wheezy
wget https://gist.github.com/neagix/bbfed4f91f8821bed7e7/download -O gist.tgz
tar xf gist.tgz
mv gist*/* root/
rm -r gist.tgz gist*
chmod +x root/*.sh
sudo tar cf ../debian-wheezy-armel.tgz .
cd ..

At this point upload the archive to your P880 and let's start hacking our way through Android.
We will individuate our external SD by excluding the internal block device marked for boot. This is a hack, so always double-check results before wiping out the block device.

sudo adb push debian-wheezy-armel.tgz /data/local/
sudo adb shell
su
cd /dev/block/vold/
for D in `ls`; do fdisk -l $D | grep ^`echo $D | awk '{ print substr($1, 1, 3) }'`; done | grep '*' | awk '{ print substr($1, 1, length($1)-2) }' > /data/local/boot-device.txt
clear && echo -e "**********************\nThe individuated device of your external SD is: $PWD/`ls | grep -v $(</data/local/boot-device.txt )`\nPlease double check with output of fdisk -l before proceeding!!!\n**********************\n" && rm /data/local/boot-device.txt

(commands in blue are run in the adb shell, available also as gist here)
Example output you will see:
**********************
The individuated device of your external SD is: /dev/block/vold/179:49
Please double check with output of fdisk -l before proceeding!!!
**********************
Remember to double-check for correctness with fdisk, then take note of the individuated device. On my P880 it is:
/dev/block/vold/179:49
Now we will format the external SD, use the default mountpoint /storage/sdcard1 and extract the Debian base system on the external SD. The mount command will allow mounting the SD with dev and exec permissions, which are necessary for our Debian chroot to work.

NOTE: do not copy/paste these commands but run them one by one, so that you can take action in case of errors.

On the adb shell (replace device in bold with what you got from above, and remember to escape the ':'):

mkfs.ext2 -L AndyWheezy /dev/block/vold/179\:XXX
mount -o rw,noatime,nodiratime,user_xattr,acl,barrier=0,data=ordered,noauto_da_alloc -t ext4 /dev/block/vold/179\:XXX /storage/sdcard1/
cd /storage/sdcard1

mkdir rootfs && cd rootfs && tar xf /data/local/debian-wheezy-armel.tgz

Once this operation is complete successfully, you are half-way :) Feel free to remove the archive with:
rm /data/local/debian-wheezy-armel.tgz

Now let's enter the chroot environment with:
busybox chroot /storage/sdcard1/rootfs /bin/bash

Thanks to the correct permissions on our external SD, you now have the wonderful and powerful bash prompt available at your fingers' range.
I have no name!@localhost:/#
In our new chroot, run these commands (these are safer to copy/paste):
export PATH=/usr/bin:/usr/sbin:/bin:$PATH
export TERM=linux
export HOME=/root
export USER=root
mkdir /dev/pts
mount -t devpts devpts /dev/pts
mount -t proc proc /proc
mount -t sysfs sysfs /sys
/debootstrap/debootstrap --second-stage 

The last one will complete the debootstrap process, and will take some time (about 10 minutes for me).

Once it's complete you are basically done with the preparation of the Debian chroot :)

Now let's put the mount/umount scripts outside the chroot, so that you can easily call them in an Android shell:

mkdir /data/local/debian
mv /storage/sdcard1/rootfs/root/*.sh /data/local/debian/

From now on you can mount/umount your Debian chroot by calling one of these:

/data/local/debian/mount.sh # OR
/data/local/debian/umount.sh

Tips:
- set hostname in chroot: hostname AndyWheezy && hostname > /etc/hostname
- set APT sources in /etc/apt/sources.list (in chroot: mv /root/sources.list /etc/apt/)
- install an SSH server
- set your root password or better use certificates for remote login
- automate chroot setup/teardown with some scripts and use gscript to start them, for example: su -c '/data/local/debian/run.sh /storage/sdcard1 /etc/init.d/ssh start'


Here you see Debian Wheezy on all its glory, running from within a P880 Android device


Enjoy experimenting!

No comments:

Post a Comment