(813)-421-4338

Mobile Rails Dev

I promise this won’t turn into an android blog, but I discovered something neat and I wanted to share it.

The goal for any mobile phone I use is to allow me to leave the office. Being a freelancer I work every day, but sometimes us developers want a social life. The real problem is when ever we go out, there’s a good chance that a client will call. Most of the time, they just need to ask a question or get some help, but every once in a while there is an emergency, that needs your attention right away. The ability to respond quickly in those circumstances can make a huge difference. No one wants to here, “I will get yo your site in 6 hours when I get home.”, when what they need is “Ok, its back up. I will look into what caused the problem A.S.A.P.

Keeping that level of customer service used to mean dragging my laptop everywhere I went. Sure SSHing to a server to fix a problem from your cell is nice, but what if you need to do something more then restarting a service. What if you need to cap deploy, or make a change then git push it.

While I had my blackberry, the solution I chose was to keep my laptop with me at all times, then I could tether when the need arose, fix the problem and be done. But this also limited where I could go.

With the G1 I can go where I please, their 3G network makes SSHing fast and with the instructions below I can even do dev from my G1. It’s of course no where near as swift as my laptop, but in those few emergency cases, it works and most importantly, I can leave my laptop at home.

Prep Work
Your going to need four things to complete this projects.

  1. A larger SD Card (I used the 4GB version of this one, you will also need a way to see it on your PC)
  2. A game (Here ya go)
  3. A linux
  4. A G1

Lets install some applications to get the Android side ready to go. Of course this is the swt I run with, you can use what you like.

  • Time Tracker
  • Task Manager for Root
  • Skype (if you use skype)
  • ConnectBot
  • Linda File Manager
  • Task Switcher
  • Terminal Emulator
  • Text Edit

Root Your Phone (Full Directions)

  1. Downgrade to RC29
  2. Gain root access using the telnetd trick
  3. Upgrade to RC33

Prepair The SD Card

  1. Change the partition type to FAT32 (some may not need this change). Make sure you change the device correctly. Also note that I am running this from my Linux box. You can format from your G1 as well.
    sudo fdisk /dev/mmcblk0
  2. Format the card. You should do this to the card rather you had to change the partition type or not. It helps remove any junk put on by the manufacturer.
     sudo mkfs.vfat -F 32 /dev/mmcblk0p1 

Prepair the phone a little

  1. Enable Wifi, it’s not really needed but it will make this a lot easier.
  2. Plug into power source, again just to make life easy.
  3. Install the applications above if you haven’t already. Most importantly the terminal editor.

Building the Debian Image

  1. get the sd card mounted on the computer again
  2. Make a 1GB disk image
    dd if=/dev/zero of=mobilerails.img bs=1024 count=1048576
     1048576+0 records in
     1048576+0 records out
     1073741824 bytes (1.1 GB) copied, 18.9066 s, 56.8 MB/
    
  3. get debootstrap
    sudo apt-get install debootstrap
  4. format the image
    mke2fs -F mobilerails.img
  5. mount the image
    mkdir mobrails
    sudo mount -o loop mobilerails.img mobrails/
    
  6. Build the boot strap
    sudo apt-get install debootstrap
    sudo debootstrap --verbose --arch armel --foreign lenny mobrails http://ftp.de.debian.org/debian
    
  7. Play a game until that finishes
  8. Unmount the image
    sudo umount mobrails
    
  9. Create the following directory structure on the root of the sdcard
    • /mobilerails
    • /mobilerails/image
    • /mobilerails/share
    • /mobilerails/bin
  10. copy the mobilerails.img to sdcard/mobilerails/image

Create the following files in /mobilerails/bin/

  • create a new file on the sd card /mobilerails/bin/fsrw.sh
    mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system
    echo "Filesytem remounted as read/write"
    echo "Courtesy of http://www.androidfanatic.com"
    echo "Installer by Mark Walker (ghostwalker)"
    #Based on Saurik's remount.sh  - modified by Mark Walker of http://www.androidfanatic.com
    
  • create a new file /mobilerails/bin/install.sh
    sh fsrw.sh
    clear
    cat fsrw.sh > /data/local/bin/fsrw.sh
    cat prepsys.sh > /data/local/bin/prepsys.sh
    cat stopsys.sh > /data/local/bin/stopsys.sh
    cat entersys.sh > /data/local/bin/entersys.sh
    cat firsttime.sh > /data/local/bin/firsttime.sh
    cd /sdcard/mobilerails
    chmod 4777 *
    cd /data/local/bin/
    chmod 4777 *
    echo " "
    echo "Make sure you run firsttime AFTER you run prepsys"
    echo " "
    
  • create the file /mobilerails/bin/prepsys.sh
    clear
    mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system
    echo "Android Filesytem remounted as read/write"
    export kit=/sdcard/mobilerails
    export bin=/data/local/bin
    export mnt=/data/local/mnt
    export PATH=$bin:/usr/bin:/usr/sbin:/bin:$PATH
    export TERM=linux
    export HOME=/root
    modprobe ext2
    mknod /dev/loop2 b 7 0
    mount -o loop,noatime $kit/image/mobilerails.img $mnt
    mount -t devpts devpts $mnt/dev/pts
    mount -t proc proc $mnt/proc
    mount -t sysfs sysfs $mnt/sys
    mount $kit/share $mnt/share -o bind
    #no idea why we need this
    sysctl -w net.ipv4.ip_forward=1
    
  • create sdcard/mobilerails/bin/entersys.sh
    export TERM=linux
    export mnt=/data/local/mnt
    chroot $mnt /bin/login
    
  • create sdcard/mobilerails/bin/stopsys.sh
    busybox umount -f /data/local/mnt/dev/pts /data/local/mnt/proc /data/local/mnt/sys /data/local/mnt/share /data/local/mnt
    busybox losetup -d /dev/block/loop2
    
  • create sdcard/mobilerails/bin/firsttime.sh
    export mnt=/data/local/mnt
    chroot $mnt /debootstrap/debootstrap --second-stage
    echo 'deb http://ftp.de.debian.org/debian lenny main' > $mnt/etc/apt/sources.list
    echo 'nameserver 4.2.2.2' > $mnt/etc/resolv.conf
    chroot $mnt passwd
    chroot $mnt /bin/bash
    

The actual Install

  1. umount the sdcard and get it back over to the G1
  2. open terminal
  3. run some commands
    cd /sdcard/mobilerails/bin
    sh install.sh
    prepsys.sh
    #fix the link up errors
    cd /data/local/mnt
    mkdir share
    mkdir dev/pts
    cd /sdcard
    stopsys.sh
    
  4. The last 10% of the debian install. You should now be su under the “normal” terminal. i.e. not inside debian.
    prepsys.sh
    firsttime.sh
    
  5. play a game
  6. When the bootstrapping is done enter a root password (just for the debain system)

Post Install System Configuration

Ok now we are at a bash prompt. We don’t want to do too much though, just get some stuff setup inside the image.

  1. First we are going to need some swap space, but we want it to exist outside our image.
    dd if=/dev/zero of=/share/swap bs=1M count=256
    mkswap /share/swap
    
  2. Just because your in a chroot deb environment doesn’t mean you won’t effect the android kernel. Lets get those two sharing
    rm -f /etc/mtab
    mount -t devpts devpts /dev/pts
    mount -t proc proc /proc
    mount -t sysfs sysfs /sys
    ln -s /proc/mounts /etc/mtab
    
  3. Now we can mount our swap image
    swapon /share/swap
  4. check and make sure we are sill sane
    mount
    free -m
    
  5. Ok, now lets add a normal user
    adduser coteyr
    exit
    
  6. Now we are back in our android environment, lets fix it so that swap is turned on and off ever time we stop and start the deb environment.
    vi /sdcard/mobilerails/bin/prepsys.sh
    #add
    chroot $mnt swapon /share/swap
    #somewhere twords the end
    
    vi /sdcard/mobilerails/bin/stopsys.sh
    
    #add
    export mnt=/data/local/mnt
    chroot $mnt swapoff /share/swap
    # at the very top
    
  7. Reinstall the scripts
    cd  /sdcard/mobilerails/bin/
    sh install.sh
    
  8. Stop the system
    stopsys.sh
    
  9. Make sure that the swap file exists outside the image
    cd /sdcard/mobilerails/share
    ls
    
  10. check that we have no left over mounts (that aren’t supposed to be there. umount them if you do)
    mount
    
  11. Now, just to be really nice reboot the phone and make a backup of the mobilerails directory on your computer

Now for Rails

Ok phone is back up. Next we need to get a dev enviroment up and running.

  1. Now lets get the sdcard back in the phone
  2. start up the terminal
  3. become root
    su
  4. mount the debian system
    prepsys.sh
  5. At this point the debian system is running along side the android system the android can’t really see debian and debian can’t really see the android (at least not without browsing by hand). We want to keep it this way, though there are other ways, but keeping our dev environment separate is safer. lets entersys.sh
    entersys.sh
  6. login and then the fun starts
  7. lets get us some sudo
    su
    apt-get update
    apt-get install sudo
    visudo
    
  8. make your changes then exit(if you don’t know how to setup sudoers read the comments in that file)
  9. lets leave root….
    exit
  10. and start installing ruby at el.
    sudo apt-get install ruby irb rdoc git-core libopenssl-ruby
    
  11. play your game again
  12. lets install rubygems the right way*That would be an opinion*
    wget http://rubyforge.org/frs/download.php/45905/rubygems-1.3.1.tgz
    tar -xzf rubygems-1.3.1.tgz
    cd rubygems-1.3.1
    sudo ruby setup.rb
    #ri and rdoc take forever so just play a game
    sudo gem1.8 install rails --no-ri --no-rdoc
    
  13. Next were going to want to generate some ssh keys for our git repo
    cd
    ssh-keygen
    
  14. get them into your repo (I will leave this step up to you, everyone has diffrent methods)
  15. Lets make our Projects directory and lets git a project
    cd
    mkdir Projects
    cd Projects
    git clone ssh://git@someserver.com/ReallyCoolProject
    cd ReallyCoolProject
    script/server
    
  16. now we can go over to our browser and hit localhost:3000 (you installed task switcher right?)

Final Thoughts

Editing can be done from the terminal via vi or nano or you can use one of the editors from the android side. If you use the android side you should be able to see the debian system in /data/local/mnt/.

Few quick things to remember.

  • The loop device is a bit fragel. Don’t yank out your sdcard wiout unmounting it first.
  • Make frequent backups of the mobilerails directory. It will corupt if the phone crashes(or is turned off) and it is mounted.
  • I wouldn’t run with the system prep-ed all the time because of the above problems.
  • Keep your debian system and your android system separate unless your really sure you know what your doing.
  • Protect your phone and sdcard, you just gave whoever stole your phone read and write access to your git repo
  • The system wiill run slower when your running webrick. Kill it if you don’t need it for a while.
  • Sqllite is fine, mysql would be fun (bad fun).
  • Your G1 can see that swap space, be aware of that.
  • Permissions wont copy across the two systems. Make sure that you remember that if you edit or create a file outside the debian system.
  • Your not running a VM, it’s just a chrooted environment. That means any kernel level things will be seen by everyone. Try not to insmod or such inside the image, unless you really really know what your doing.
  • If your tenminal exits, then you loose your chrooted environment. but you can just entersys.sh again.













Powered by WordPress