Chroot’d Ubuntu on Samsung Galaxy S (Android)
So I got my hands on a Samsung Galaxy S (or SGS) a while ago, and it’s a really nice phone using Android 2.1 as it OS. Before I settled on the SGS the Nokia N900 had caught my eye. The 900 has great hardware and its OS is more of a “standard” Linux. Its Maemo OS is really just a Linux distro with all that entails (X11,QT,GTK), this means you can easily run any Linux app you can compile, and since it’s easy to install Debian or Ubuntu chroot’d on it you can even run things like OpenOffice etc (But the speed and screen real estate won’t be good). The problem is that Maemo is being merged into to Meego and there is only one phone, the N900, which means no apps what so ever except open source ones.
Android is linux based, but it doesn’t use X11 and most applications are written in Java and executed on a custom JVM called ‘Dalvik’. Therefore I was a bit sceptical of buying a android device, it felt a bit stupid to not take advantage of the massive amount of open source programs, libraries and toolkits out there. I still do, but android struck me as a much more mature OS, an it’s very nice to have lot’s of apps :-)
Anyway, having linux underneath means that we can easily install a chroot’d Ubuntu in Android, giving us access to many nice applications… like openssh, python, webservers, transsmission (with web gui) etc. This is nothing new, it has been done before, both with Debian and Ubuntu, but here is how I did it on my SGS. If you don’t know what chroot is, read up before you do anything. For X11 application there is a trick with VNC to get them running, I haven’t tried it yet though.
A couple of prerequisites:
- Root access (su)
- busybox
I got both by rooting my phone after this guide .
Partition
First of we need somewhere to put the system, android uses FAT32 for the SD card and that’s no good for us. We need a real file system, like ext4. Unfortunately the kernel in my SGS didn’t support it. You can get a list of supported filesystems with ‘cat /proc/filesystems’. This is from my SGS:
$ cat /proc/filesystems
nodev sysfs
nodev rootfs
nodev bdev
nodev proc
nodev cgroup
nodev binfmt_misc
nodev sockfs
nodev pipefs
nodev anon_inodefs
nodev tmpfs
nodev inotifyfs
nodev devpts
ext3
ext2
cramfs
nodev ramfs
vfat
msdos
romfs
rfs
j4fs
So we settle for ext3. The easiest way to partition the SD card is to connect it to you’re computer (not through the phone), and use gparted. I used the SD card reader on my laptop with the use of an adapter that came with the card when I bought it. It’s best to just shrink the vfat partition because if you remove it your phone is going to complain that the card is broken or needs formatting. If your starting from scratch just make sure the vfat partition is the first partition.
The size of the ext3 partition of course depends on the size of your SD card. But at least 1 gigabyte is probably a good idea. I used a 2 gigabyte card, 512 megabytes for the vfat, the rest for the ext3 partition. Don’t forget to tell gparted to format your new partition.
Create a rootfs with rootstock
Rootstock is a great tool for building a basic ubuntu image to use with your ARM based device. It uses good old debootstrap, running the second stage of the installation (the one you usually need to run on the device) under qemu. Pretty smart. This means that the tarball you get is a fully finished root base file system. You can even specify some extra packages that rootstock can install. A SSH server is a good idea, I like less, screen, ne (nice editor, like nano but with ordinary key-bindings, like ctrl-s etc), build-essential so that we can compile things.
Note that you specify a user and a password. You should choose a good password.
Create a rootfs tarball:
$ sudo rootstock --fqdn ubuntu --login sgs --password temporary --imagesize 1G --seed openssh-server,less,build-essential,screen,ne
Unpack the tarball and copy the content to your fine new partition on the SD card. Then set up googles DNS server (the ext3 partition is mounted on /mnt):
$ mv mv armel-rootfs-201008012234.tgz /mnt/ $ cd /mnt $ tar xvzf armel-rootfs-201008012234.tgz $ rm armel-rootfs-201008012234.tgz $ echo 'nameserver 8.8.8.8' > etc/resolv.conf
It’s time to put the card back into the phone…
Now on your phone
For the rest you need to drop into root:
$ su #
First mount the ext3 partition, I use a dir on the SD card called “ubuntu”, you can put it anywhere you want though. Since the SGS has internal storage too the SD card has the device /dev/block/mmcblk1p2
# busybox mount /dev/block/mmcblk1p2 /sdcard/sd/ubuntu
chroot by itself is not enough, we need to mount /proc,/dev and /sysfs too. An the path on android is a bit strange so we need to set that too.
Tap this on your phone for a glorious chrooted ubuntu:
# busybox mount -t proc proc /sdcard/sd/ubuntu/proc # busybox mount --bind /sys /sdcard/sd/ubuntu/sys # busybox mount --bind /dev /sdcard/sd/ubuntu/dev # export PATH=/bin:/usr/bin:/sbin:/usr/sbin:$PATH # busybox chroot /sdcard/sd/ubuntu
Log in via SSH
When your chroot’d you can start the SSH server and log into it that way. This will give you full terminal emulation (as opposed to “adb shell”), i.e.bash completion, emacs, vim etc.
Start server and check what ip nr your phone has
# /etc/init.d/ssh start # ifconfig | grep "inet addr"
Note that since this is Ubuntu the root user doesn’t have any password so you have to ssh to it with the user name and password you specified to rootstock. In my example above that’s “sgs” and “temporary”. To get root privileges use sudo, remember this is Ubuntu.
A script to help
If you think tapping all that mounts and path etc on your phone is a bit tiresome to tap you can make a script of it. Shell scripting is not my strong point so it can probably be improved, but it seems to work for me. It mounts the ext3 partition and sets up everything needed. Note that it does not drop you into a chroot’d ubuntu, it just starts the ssh server.
#!/bin/sh
#we need to setup test [
busybox ln -s /system/xbin/busybox /system/xbin/[
if [ -d /sdcard/sd/ubuntu/etc ] ; then
echo "ubuntu partition is already mounted"
else
echo "Mounting ext3 partition to /sdcard/sd/ubuntu"
busybox mount -o noatime /dev/block/mmcblk1p2 /sdcard/sd/ubuntu
echo 'Mounting proc,sys and dev'
busybox mount -t proc proc /sdcard/sd/ubuntu/proc
busybox mount --bind /sys /sdcard/sd/ubuntu/sys
busybox mount --bind /dev /sdcard/sd/ubuntu/dev
fi
echo 'Exporting a proper path'
export PATH=/bin:/usr/bin:/sbin:/usr/sbin:$PATH
echo 'Chrooting and starting ssh server'
busybox chroot /sdcard/sd/ubuntu /etc/init.d/ssh start
echo "And your ip is:"
busybox ifconfig | busybox grep "inet addr"
Put it on the sdcard or the on the internal storage and execute it like this:
$ su # sh /sdcard/sd/ubuntu.sh