[Desire Android Development] Deathisfatal:
Gentoo on HTC Desire

Prerequisites:

Rooted Desire

Any flavour of Linux (Live or installed)

A decent sized MicroSD card (>2Gb recommended)

MicroSD USB adapter (or SD card reader and adapter)

Some knowledge of Linux command line and Gentoo is recommended

Patience...

Things you should know first:

This is NOT a fully independent Linux - it runs parallel to Android inside a chroot environment

Some applications can be very slow until they are compiled properly for the Desire hardware

Compilation with a 1GHz single core CPU is slow

You can run just about any Linux program; X programs and desktop environments can be used via VNC

Many custom ROMs and kernels do not include swap support for performance reasons, and so if you require swap you may need to compile your own kernel (I will upload my custom kernel at a later date)

Due to Android security settings, only root can use the network. This causes some problems as anything that requires network access must run as root (again, if anyone has any insight for a way around this, please contact me)

Some ROMs underclock the CPU when the screen is off - this will lead to VERY long compilation times if you leave your phone like that

Step one - Prepare MicroSD

To install Gentoo you will need an ext2, 3, or 4 filesystem on your MicroSD card. This can be done two ways: partitioning your card (recommended), or by creating a file and creating the filesystem within that (not covered here).

Start up GParted (or something similar) on your computer and insert your MicroSD card; if your card is fully allocated, you will need to shrink the existing partition(s) first (back up your data! This can go wrong...) and leave a space at the end of the card for your new partition. The absolute minimum I would recommend to dedicate to Gentoo would be 1GB, but that doesn’t give you much wiggle room and you will have a very hard time compiling anything big (like GCC); I would recommend making the partition >2GB. Make sure you create the Gentoo partition at the end of the card.

Note:

APP2SD+ requires its own partition - if you have it enabled, you must have at least three partitions, with the second partition on the card dedicated to it and the third for Gentoo.

Due to the large number of files in the Portage tree, small partitions can sometimes run out of inodes. You can get around this by decreasing the size of the inodes and blocks, and therefore creating more:

~ $ sudo mkfs.ext3 /dev/(partition) -i 2048 -b 2048

Step two - install base system:

Download the latest stage 3 tarball for armv7a-unknown-linux-gnueabi from the Gentoo mirror closest to you (Google "Gentoo Mirrors", I can't post links yet..), mount the Gentoo partition and extract the contents of the tarball to it.

Example:

~ $ wget (your selected mirror)/linux/gentoo/releases/arm/autobuilds/current-stage3/armv7a-unknown-linux-gnueabi/stage3-armv7a-20100917.tar.bz2
~ $ sudo mkdir /mnt/sd
~ $ sudo mount /dev/(partition) /mnt/sd
~ $ sudo tar xjvfp stage3-armv7a-20100917.tar.bz2 -C /mnt/sd/

Download the latest portage snapshot (from the Gentoo mirror) and extract:

~ $ wget (your selected mirror)/linux/gentoo/snapshots/portage-latest.tar.bz2
~ $ sudo tar xjvf portage-latest.tar.bz2 -C /mnt/sd/usr/

Now modify the make.conf file using your favourite text editor:

~ $ sudo nano /mnt/sd/etc/make.conf

Sample make.conf:

CFLAGS="-march=armv7-a -mtune=cortex-a8 -mfpu=vfpv3 -O2" # if anyone has any suggestions to improving these, please let me know
CXXFLAGS=${CFLAGS}
CHOST="armv7a-unknown-linux-gnueabi"
USE="unicode X -gnome -kde -firefox wifi syslog -udev -mdev -wifi -gpm threads nptl networking wxwidgets -ipv6 sqlite jpeg png dbus" # Have a look at the Gentoo USE flag description page on the web for others you may want/don't want
VIDEO_CARDS="none"
ALSA_CARDS="none"
INPUT_DEVICES="none"
SYNC="rsync://(your selected mirror)"
GENTOO_MIRRORS="(your selected mirror)"
ACCEPT_LICENSE="*"
MAKEOPTS="-j1"
PORTAGE_NICENESS="19"
AUTOCLEAN="yes"
FEATURES="noman nodoc noinfo -userfetch" # Getting rid of the man, doc and info pages saves a lot of space. Userfetch is disabled due to the issue described above
PORTAGE_RSYNC_EXTRA_OPTS="--exclude ChangeLog" # Again, this saves some space

Edit locale.gen with the locale values you require, e.g.:

en_US.UTF-8 UTF-8

Unmount your MicroSD card and insert it back into your phone.

Step three - configuring the base system

Using ConnectBot, another terminal emulator app, or adb, connect to the local console and setup the mounts, then enter Gentoo (using busybox as your shell (type "busybox sh") is recommended as the tab completion (trackball + i in connectbot) helps a lot):

# mkdir /data/gentoo
# mount -t ext3 (or whatever fs you set it to) /dev/block/mmcblk0p3 (change the 3 to whatever partition you need) /data/gentoo
# mount -t proc none /data/gentoo/proc
# mount -o bind /dev /data/gentoo/dev
# mount -t devpts none /dev/pts
# chroot /data/gentoo /bin/bash
# source /etc/profile

Congratulations! You are now inside your own Gentoo environment. If you don't need your phone for a while you can now recompile and update the system (this will take quite a long time and will use a lot of data). This doesn't have to be done now, or at all, but it is highly recommended to do so at some point:

# emerge -DuNav world

Once that's done (or you don't want to do it), install some base programs:

# emerge -av screen sudo

If you want VNC, tightvnc is recommended:

# emege -av tightvnc

Web server:

#emerge -av nginx

Set a root password:

# passwd

For some reason running SSHD with IPv6 enabled causes the phone to restart. To fix this, make the following changes to /etc/ssh/sshd_config:

Change:

#AddressFamily any

To:

AddressFamily inet

To start SSHD:

# ssh-keygen -t rsa (save to /etc/ssh/ssh_host_rsa_key with no passkey)
# ssh-keygen -t dsa (save to /etc/ssh/ssh_host_dsa_key with no passkey)
# /usr/sbin/sshd

If your kernel supports swap, it is recommended to create and enable it:

# dd if=/dev/zero of=/swap bs=10M count=20 (change this to alter the size of the swap file)
# mkswap /swap
# swapon /swap (you will need to run this command with the other mount commands each time you boot)

Boot script:

Start your phone in recovery mode and mount /system using the partitions menu in your recovery ROM, then create a file on your computer with the following contents:

#!/system/bin/sh
#
#	Linux startup script for rooted Android systems
#	© Deathisfatal @ xdaforums.com
#
#	TODO:
#	-	Error handling
#

# Edit the following variables as required

HOSTNAME="Android"
LINUX="/data/gentoo/"
BB="/system/xbin/busybox"
CHROOT="${BB} chroot ${LINUX}"
ROOT="/dev/block/mmcblk0p3"
SWAP="/swap"

${BB} mount -o noatime -t ext3 ${ROOT} ${LINUX} # Edit -t as required by your filesystem
${BB} mount -o bind /dev ${LINUX}dev
${CHROOT} /bin/mkdir /dev/shm
${CHROOT} /bin/mount -t tmpfs -o size=32m none /dev/shm
${CHROOT} /bin/mkdir /dev/fd
${BB} /bin/mount -o bind /proc/self/fd /dev/fd
${CHROOT} /bin/mount -t devpts none /dev/pts
${BB} mount -t proc none ${LINUX}proc
${CHROOT} /sbin/swapon ${SWAP}
${CHROOT} /bin/mount -t tmpfs -o size=32m none /tmp
${CHROOT} /bin/mkdir /tmp/run
${CHROOT} /bin/mkdir /tmp/run/portage
${CHROOT} /bin/touch /tmp/run/utmp
${CHROOT} /bin/chmod 777 -R /tmp
${BB} mount -o bind /data ${LINUX}mnt/data
${BB} mount -o bind /sdcard ${LINUX}mnt/sd
${CHROOT} /bin/hostname ${HOSTNAME}

# Uncomment the following line to enable sshd on boot
#${CHROOT} /usr/sbin/sshd

Connect your phone and computer with the USB cable, then use the following command on your computer:

~ $ adb push (File you created earlier) /system/etc/init.d/05linux
~ $ adb shell chmod 755 /system/etc/init.d/05linux

Now reboot your phone and your Gentoo partition should automatically mount.

Notes:

If you restart your phone, you will need to issue the necessary mount commands as you did earlier until I (or someone else) get around to making a proper script that runs at boot time

For information on how to set up programs and servers on Gentoo visit the Gentoo wiki

Please contact me or post here with any questions you may have regarding this

Changelog:

Updated CFLAGS and bootscript

Thanks to sfjuocekr for his/her contributions.