Friday, April 09, 2010

Installing Debian on a G1 (HTC Dream)

I bought a G1 in November having heard good reviews of them from the few people I knew who owned one and pleasantly surprised at how easy it was to type on the keyboard, which was the main problem I had had with iPhones.

Anyway, I haven't really done much messing with it until the last couple of weeks when I discovered myself very bored while at home for the Easter break. Having heard it was possible a while ago I started off by rooting the phone and installing CyanogenMod (instructions here for those of you who want to try it), a replacement for the default Android installation, adding a features that haven't been pushed down the line by vendors yet and a few extras such as the ability to store apps on the SD card, USB tethering and various Unix commands.

Having rooted the phone I was curious as to what I could now do and found it mentioned that it was possible to install Debian in a chroot since it already supports ARM EABI. I partially followed this post by saurik but came across several issues and shortcuts due to using CyanogenMod.

Here are some brief instructions to get Debian working on CyanogenMod:

Making a Debian image
To avoid having to partition my SD card (I use it for various other things) I created a mountable image as follows (assuming you are running Debian on your computer too), to avoid confusion I also add an sdcard directory here that the sdcard will later be mounted in:
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 http://ftp.de.debian.org/debian
mkdir debian/sdcard
umount debian
As CyanogenMod comes with the Linux filesystem drivers and busybox you don't need to worry about adding them which means a large portion of saurik's instructions can be skipped.

Mounting the Image
Mounting the loop device caused some problems but I eventually found that it was looking for the loop devices at /dev/block/loop* rather than /dev/loop* which was fixed by adding a few ifs to the start of the mounting script.
#!/system/bin/sh
if [ ! -e /dev/loop0 ]
then
ln /dev/block/loop0 /dev/loop0
fi
if [ ! -e /dev/loop1 ]
then
ln /dev/block/loop1 /dev/loop1
fi
if [ ! -e /dev/loop2 ]
then
ln /dev/block/loop2 /dev/loop2
fi
if [ ! -e /dev/loop3 ]
then
ln /dev/block/loop3 /dev/loop3
fi
if [ ! -e /dev/loop4 ]
then
ln /dev/block/loop4 /dev/loop4
fi
if [ ! -e /dev/loop5 ]
then
ln /dev/block/loop5 /dev/loop5
fi
if [ ! -e /dev/loop6 ]
then
ln /dev/block/loop6 /dev/loop6
fi
if [ ! -e /dev/loop7 ]
then
ln /dev/block/loop7 /dev/loop7
fi
mount -o loop,noatime -t ext2 /sdcard/debian/debian.img /data/local/debian/mnt
busybox mount -o bind /sdcard /data/local/debian/mnt/sdcard
export PATH=/usr/bin:/usr/sbin:/bin:$PATH
export TERM=linux
export HOME=/root
export USER=root
busybox mount -t proc none /data/local/debian/mnt/proc
busybox mount -t sysfs sysfs /data/local/debian/mnt/sys
busybox mount -t devpts devpts /data/local/debian/mnt/dev/pts
if [ ! -e /data/local/debian/installed ]
then
busybox chroot /data/local/debian/mnt/ /debootstrap/debootstrap --second-stage
touch /data/local/debian/installed
fi
I saved this script as mountdeb.sh and the debian.img file in a folder called "debian" at the root of the SD card and mounted it on my G1 again.
I used a terminal emulator (search Market for Terminal Emulator by ZTA Technologies). First to enable things like tab completion and command history I changed the initial command (done through the menu in the Terminal) to:
su -c "busybox sh"
which nicely drops you in busybox as root instead of the poor implementation of sh that android comes with by default.

Running:
/sdcard/debian/mountdeb.sh
chroot /data/local/debian/mnt/ /bin/bash
drops you into bash in your shiny new Debian installation from which you can use apt-get to install whatever you feel like.

Automating the Installation
Since I didn't want to have to type the above into the phone every time I wanted to use Debian on my phone and I also much preferred being able to use ConnectBot to access it I automated starting it as follows:

First I installed the dropbear ssh server onto the Debian install:
apt-get install dropbear
This allowed me to either ssh to localhost from the phone or ssh in from my computer, which I was quite glad of after typing on the small keyboard for a while. Next to automate it.

Cyanogenmod provides facilities for auto running scripts using /etc/init.d/ however these all get run before android mounts /sdcard which means that we can't run our script at this point unless we mount /sdcard ourselves (which does work but it means that none of the applications on the phone will be able to access /sdcard (I think there may be a way to change this by messing around with permissions but I haven't tried it yet)).

Since init.d ran things to early I found an autorun application called Phone Prioritizer (available here (put the .apk in the attached zip on your SD card and open it with the package installer. Unfortunately you have to register on the XDA forums to get it), not on Market for some reason) which allows a script to be executed once the system has started up (Broadcast the BOOT_COMPLETED intent), it also had some options for running cron like scripts but I disabled these for the moment. To start the Debian chroot I edited /sdcard/phonePrioritizer/afterboot.txt (the file it checks after booting) to read:
busybox sh /sdcard/debian/mountdeb.sh
busybox chroot /data/local/debian/mnt/ /usr/sbin/dropbear
Rebooting the phone I found I could immediately ssh in using ConnectBot on the phone. Some simple certificate setup later and I didn't even need to type in a password from the phone to itself. :)

If anyone tries to follow these instructions let me know if you have any problems.

12 comments:

Anonymous said...

After pouring over many, many webpages, I was finally able to get this working thanks to your blog. Much appreciated. BTW, for those who are not familiar with Dropbear, you will need to create a password in order to log on (use the passwd command). Thanks again.

Anonymous said...

A few issues that I encountered with the instructions (I am running CyanogenMod 6.1):

* You have to first create the directories "/data/local/debian" and "/data/local/debian/mnt" directories (/data/local/debian/chroot in my case).

* Also, the SD mount point has to be remounted with the "exec" option, otherwise the script will fail with a "permission denied" error. I have yet to investigate how to make this permanent.

mount -r remount,exec /mnt/sdcard

* The /etc/apt/sources.list was empty and I had to input it by hand.

Also, I modified the script as follows (some changes are aesthetic), keep in mind that some directories are named differently from the instructions:

-------------------------------

#!/system/xbin/bash -x

NATIVE_SD_DIR=/sdcard
IMG_DIR=$NATIVE_SD_DIR/debian
DEB_DATADIR=/data/local/debian
IMG_MPOINT=$DEB_DATADIR/chroot

for i in `seq 0 7`; do
if [ ! -e /dev/loop$i ]; then
ln /dev/block/loop$i /dev/loop$i
fi
done

mount -o loop,noatime -t ext4 $IMG_DIR/debian.img $IMG_MPOINT

busybox mount -o bind $NATIVE_SD_DIR $IMG_MPOINT/media/sdcard

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

busybox mount -t proc none $IMG_MPOINT/proc
busybox mount -t sysfs sysfs $IMG_MPOINT/sys
busybox mount -t dev devpts $IMG_MPOINT/dev

if [ ! -e $DEB_DATADIR/second_stage_installed ]; then
busybox chroot $IMG_MPOINT /debootstrap/debootstrap --second-stage
touch $DEB_DATADIR/second_stage_installed
fi

exit 0

-------------------------------

This is what I can think of right now, I'll post some more comments later.

Cheers

Anonymous said...

I made some glaring mistakes in the previous script. Here is a new version.

I removed the line that set the bash variables since they do not get carried over to the chroot... you know, Debian's bash being a separate process and all. So they should be added to /root/.bashrc. Here's mine:

----------------------------------

# ~/.bashrc: executed by bash(1) for non-login shells.

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

alias ls='ls -AhF --color'

----------------------------------

And here is the fixed script:

#!/system/xbin/bash -x

NATIVE_SD_DIR=/sdcard
IMG_DIR=$NATIVE_SD_DIR/debian
DEB_DATADIR=/data/local/debian
IMG_MPOINT=$DEB_DATADIR/chroot

for i in `seq 0 7`; do
if [ ! -e /dev/loop$i ]; then
ln /dev/block/loop$i /dev/loop$i
fi
done

mount -o loop,noatime -t ext4 $IMG_DIR/debian.img $IMG_MPOINT

mount -o bind $NATIVE_SD_DIR $IMG_MPOINT/media/sdcard

mount -o bind /proc none $IMG_MPOINT/proc
mount -o bind /sys $IMG_MPOINT/sys
mount -o bind /dev $IMG_MPOINT/dev
mount -t devpts devpts $IMG_MPOINT/dev/pts

if [ ! -e $DEB_DATADIR/second_stage_installed ]; then
chroot $IMG_MPOINT /debootstrap/debootstrap --second-stage
touch $DEB_DATADIR/second_stage_installed
fi

chroot $IMG_MPOINT /bin/bash

exit 0

----------------------------------

Cheers

Harm said...

I am halfway there and there are two issues I'd like to clarify:
In the instruction to create an image a line break is missing. I missed that when copy pasting it. It's in the line with mount, just after 'debian' and before 'debootstrap'.
The second is in the comments, you need to mount the sdcard with exec rights. That command is 'mount -o' instead of 'mount -r'.

One additional thing. Installing SSHDriod made it way easier typing in all those commands.

Harm said...

I can't seem to find apt-get in /bin, /usr/bin or /usr/sbin. Nor can I find an editor. What gives? Did I use the correct debootstrap thingy?

Anonymous said...

Fantastic beat ! I would like to apprentice whilst you amend your site, how can
i subscribe for a blog site? The account aided me a appropriate deal.
I were a little bit familiar of this your broadcast provided bright transparent concept

My web blog: Will Turmeric root extract Assist in Loosing Moobs?

Anonymous said...

Asking questions are really nice thing if you are not understanding anything entirely, however this paragraph provides good understanding even.


My web blog ... Latest medical breakthroughs regarding moobs therapy

Anonymous said...

If you are going for finest contents like myself, just go to see this website daily for the reason that it gives feature contents, thanks

my weblog - http://chestfatburner.com

Anonymous said...

Good day! Do you know if they make any plugins to safeguard against hackers?
I'm kinda paranoid about losing everything I've worked hard on.
Any recommendations?

my webpage Exercises to get a larger torso

Anonymous said...

Hello I am so thrilled I found your website, I really found you by error, while I was looking on Yahoo for something else, Anyhow I
am here now and would just like to say thank you for a marvelous post and a all
round interesting blog (I also love the theme/design), I don’t
have time to read through it all at the moment but I have bookmarked it and also included your RSS
feeds, so when I have time I will be back to read a great deal more, Please do keep up the fantastic jo.


my web site; Pectoralis major and also Musculus pectoralis minor

Anonymous said...

Howdy, i read your blog from time to time and i own a similar one and i was just curious if you get a lot of spam responses?
If so how do you stop it, any plugin or anything you can advise?
I get so much lately it's driving me mad so any assistance is very much appreciated.

my web blog Building a V Form entire physique ?

Anonymous said...

My partner and I absolutely love your blog and find
most of your post's to be what precisely I'm looking for.
Does one offer guest writers to write content to suit your needs?
I wouldn't mind writing a post or elaborating on a few of the subjects you write about here. Again, awesome site!

my weblog :: http://chestfatburner.com/high-body-fat-can-give-you-man-boobs