burning broccoli

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:

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
comments    Add a comment    Posted 1 year ago

PHPUnit 3.4 in Karmic

The PHPUnit package in karmic is only version 3.3.* and if you need the Yaml support, as I do, you need 3.4. A good thing lucid lynx has an appropriate package.

Just download and install with gdebi.

comments    Add a comment    Posted 1 year ago

Re-inventing the wheel: A logging development SMTP server

I found myself in a spot of trouble while trying to debug some newsletter sending plugin in Plone called Singing & Dancing. The problem was that I had made a local installation of the application server and the Python version I had to run it on had no support for TLS/SSL. This meant my ordinary outgoing mailserver wouldn’t work.

Well anyway that would have been a bad solution to send “for real” since I just needed to see that the mails looked good and where addressed to the right people. So now I probably should have done the obvious and googled a bit, there seems to be quite a few simple SMTP servers, but I alas I did not. What I did was to re-invent the wheel a bit.

Python has some nice SMTP handling modules both for sending and receiving. There is already a debugging SMTP server that just prints any received message. You can start it directly with:

$ python -m smtpd -n -c DebuggingServer localhost:2525

This was nearly what I needed, the only problem was that the mails where Base64 encoded so it wasn’t obvious that they where correct.

Wouldn’t it be nice if we could view the mails directly in a email application? Well with a little tinkering we can. Again, the standard library in Python to the rescue. This time the mailbox module. The mailbox module can write email messages to mbox files or maildirs, both old and tried and ancient formats for mail box storage. The great thing about a maildir is that several mail readers can parse it, like evolution (but not Thunderbird! AFAIK)

To read mails in a maildir in evolution you just add a new account (it’s under settings) and then select the maildir format. It will then ask you for a folder, the script takes arguments to define it but the default is ~/.devbox

To start the server (I called it devbox, as in a “development inbox”):

$ devbox --port=2525 --maildir=foobar/mymail

Use -h to get help of the options and remember that if you need to bind against port 25 you need to be root or use sudo. Heres the code, and as you can see one third is GPL preamble, the other third is the option parsing and it’s only really one small class that does the trivial job of passing on the mail between the SMTPServer and the Maildir. I love Python standard library!

#!/usr/bin/python
#    Copyright 2010 David Jensen 
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <>.

import mailbox
import smtpd
import asyncore
from optparse import OptionParser

class DevBox(smtpd.SMTPServer):

    def __init__(self,port,maildir):
        smtpd.SMTPServer.__init__(self,('localhost',port),('localhost',25))
        self.mbox = mailbox.Maildir(maildir)

    def process_message(self, peer, mailfrom, rcpttos, data):
        self.mbox.lock()
        self.mbox.add(data)
        self.mbox.flush()
        self.mbox.unlock()
        print "Added a msg from ",mailfrom

if __name__ == '__main__':
    usage=""" %prog [options]
    
%prog is debugging SMTP server bound to localhost. Useful
for when you need to test outgoing mail but do not want any
mail actually sent. All incoming  messages are saved in a maildir 
for later perusal regardless of any possible address. 
What comes in won't come out."""
    
    parser = OptionParser(usage,version="%prog 0.1")
    parser.add_option("-m", "--maildir",action="store", type="string", 
                      dest="maildir",default='~/.devbox',
                      help='sets the path to the maildir [default: %default]')
    parser.add_option("-p", "--port",action="store", type="int", 
                      dest="port",default=25, 
                      help='sets the port the server listens to [default: %default]')
    (options, args) = parser.parse_args()

    print "Starting server on localhost:%i, with maildir %s" % (options.port,options.maildir)
    dn = DevBox(options.port,options.maildir)
    
    try:
        asyncore.loop()
    except KeyboardInterrupt:
        pass
comments    Add a comment    Posted 2 years ago

keyboard shortcuts to focus applications using python-wnck

In a previous post I fiddled a bit with python-wnck and made a small script that bound <Super>P key to activating previous focused window, i.e. last app you where looking at. This was kind of useful since I often find myself spending way to much time and effort jumping between applications. Window lists,pagers and good looking compiz effects that does the same thing all have one thing in common, I have to identify a name, image or icon and select it. In short, I have to search for it.

You may think I have very little patience, pressing TAB a couple of times to find the window of an app can’t take that much time, can it? Well, no, not really - but since I’m programmer I tend to work with multiple applications at the same time (email,browser,eclipse,spotify,terminal(s),nautilus etc) and I switch between them a lot in one day. Actually it’s not the speed that bothers me, more that I already know which window/application I want and it’s frustrating to have to search for it every damn time!

So what’s my brilliant solution to this? Well my first thought (obviously) was some kind of direct neural interface so that the computer could just infer what I wanted before I even knew wanted it. But since that is a bit too much to engineer on one lunch break I opted for a simpler solution: creating a couple of shortcuts for my most used apps.

The script below does half of the job, it tries to find a window matching the supplied application name. If it does gives it focus. If not it will try to start it.

The script takes one or two arguments, the first is the application name as it is in the window list. If that is the same as the executable your’re all set. Otherwise the second argument is the executables name.

A couple of examples

$ ./activate.py evolution
$ ./activate.py Terminal gnome-terminal

I use CompizConfig Settings Manager and to set global key bindings, so now whenever I tap <Super>T the gnome-terminal springs to life.

Here’s the script, the only requirement is python-wnck.

#!/usr/bin/python2.6
import wnck
import sys
import time
import subprocess as sub

if len(sys.argv)  []"
    sys.exit(1)

name = sys.argv[1]
timestamp = int(time.time())

screen = wnck.screen_get_default();
screen.force_update()
wins = screen.get_windows()
for win in wins:
    app = win.get_application()
    if app is not None:
        if name == app.get_name():
            geo = win.get_geometry()
            screen.move_viewport(max(geo[0],0),max(geo[1],0)) #bugs a bit, unclear why
            win.activate(timestamp)
            sys.exit(0)
            
#if we get this far we didn't find the program, let's start it
if len(sys.argv) >= 3:
    sub.Popen(sys.argv[2],shell=True)
else:
    sub.Popen(sys.argv[1],shell=True)
comments    Add a comment    Posted 2 years ago

listing windows with python-wnck

So as I said in my previous post I’ve recently found the nice litte libwnck and it’s python bindings.

There are lots of fun things you can do with it, try exploring it with ipython and you’ll see what I mean.

Anyways I wanted to share a little snippet of code with you, this will list all windows and print out their name in the form application name:window name

#!/usr/bin/python2.6
import wnck

screen = wnck.screen_get_default()
screen.force_update() #updates the window list
wins = screen.get_windows()
for win in wins:
    app = win.get_application()
    win_name  = win.get_name() or ''
    app_name = app and app.get_name() or ''
    print '%s: %s' % (app_name,win_name)

comments    Add a comment    Posted 2 years ago