Skip to content
100644 152 lines (97 sloc) 7.18 KB
70aa05b @creationix Add initial arm chroot article
authored Jun 26, 2011
1 Title: Fun Putting Node on Mobile Devices
2 Author: Tim Caswell
f3e24f5 @creationix Update the article to publish
authored Jul 1, 2011
3 Date: Fri Jul 01 2011 10:04:02 GMT-0700 (PDT)
4 Node: v0.4.9
70aa05b @creationix Add initial arm chroot article
authored Jun 27, 2011
f3e24f5 @creationix Update the article to publish
authored Jul 1, 2011
6 This article will walk you through creating an Ubuntu image that can be chrooted inside a mobile device like the recently released [TouchPad][]. Once the Ubuntu environment is setup we'll learn how to compile and install node for fun and/or profit.
70aa05b @creationix Add initial arm chroot article
authored Jun 27, 2011
8 ## Create the Image
10 The first step is to create the image file that will be our virtual partition. This is best done using `dd`. We can resize this later using `resize2fs`, so for now make a nice small 512MB image.
12 sudo dd if=/dev/zero of=UbuntuNatty_armel.img bs=1M count=512
14 Then we want to put a filesystem on it.
16 sudo mkfs.ext3 UbuntuNatty_armel.img
18 Now we have a fully functional filesystem. Let's mount it.
20 mkdir build
21 sudo mount UbuntuNatty_armel.img build
23 Ok, here is where the magic happens. In this step I'm putting the Ubuntu Natty userspace in this new filesystem. I have also done this with Debian Sid, but v8 doesn't compile without patches on arm Debian.
25 sudo qemu-debootstrap --arch armel --foreign natty build
8ace1d7 @creationix Fix type
authored Jul 1, 2011
27 At this point your image is ready to be put onto your mobile device, but let's explore for the magic for some fun. First we want to `chroot` into this system to interact with it.
70aa05b @creationix Add initial arm chroot article
authored Jun 27, 2011
29 sudo mount -t proc none build/proc
30 sudo mount -t sysfs none build/sys
31 sudo mount -o bind /dev build/dev
32 sudo cp /etc/resolv.conf build/etc/resolv.conf
33 sudo chroot build
35 The `qemu-debootstrap` process didn't seed this image with any apt repos, so lets add one.
37 echo "deb natty main universe" > /etc/apt/sources.list
39 Now let's see what kind of binaries live in this world.
41 apt-get install file -y
42 cd /usr/bin
43 ./file ./file
f1932c1 @creationix Fix another typo
authored Jul 1, 2011
45 We just installed `file`, a program that tells us stuff about files. Then we told it to tell us about itself. I got as output:
70aa05b @creationix Add initial arm chroot article
authored Jun 27, 2011
47 file: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.16, stripped
49 So the file we just executed on our intel laptop was an arm executable. This magic is thanks to
50 file that `qemu-debootstrap` installed for us. The file `/usr/bin/qemu-arm-static` is used to interpret arm binaries on the fly for us!
52 Anyway, enough fun, let's put this on some real arm hardware. In this article I'll use webOS since it encourages homebrew exerimentation unlike certain competitors. It should work on any arm device where you have linux and a root login.
54 To keep our host machine happy, we should logout of the chroot and clean things up.
56 exit
57 sudo umount build/sys
58 sudo umount build/proc
59 sudo umount build/dev
60 sudo umount build
61 rmdir build
63 Now we have a fully functional Ubuntu system in a single file.
3a37924 @creationix Add note about where to get novacom
authored Jul 1, 2011
65 On webOS the easiest way to put a file on the drive is to use `novacom put` (part of the webOS SDK).
70aa05b @creationix Add initial arm chroot article
authored Jun 27, 2011
67 novacom put file://media/internal/UbuntuNatty_armel.img < UbuntuNatty_armel.img
69 Then log into the device and mount it on the internal drive.
71 novaterm
72 mkdir /media/chroot
73 mount /media/internal/UbuntuNatty_armel.img /media/chroot
74 mount -t proc none /media/chroot/proc
75 mount -t sysfs none /media/chroot/sys
76 mount -o bind /dev /media/chroot/dev
77 cp /etc/resolv.conf /media/chroot/etc/resolv.conf
79 Now we're ready to enter this chroot. I don't like being root all the time, so I created a local user. Since the chroot shares user id's with the chroot, but has it's own user listing, we have to add the user twice. First in the host system, add a new user.
81 adduser tim
82 id tim
84 Note the uid of the new user and group. My was:
86 uid=1002(tim) gid=1002(tim) groups=1002(tim)
88 Now let's enter the chroot and mirror the user there.
90 chroot /media/chroot
91 adduser tim --uid 1002
93 Then we should add sudo access to this user. Read up on `visudo` if you don't know how to do this. On Ubuntu, this is as simple as installing sudo and adding our user to it's group.
95 apt-get install sudo
96 adduser tim sudo
97 echo " "`hostname` >> /etc/hosts
99 Now we can logout and login as our user this time. Note I use `login` instead of the default shell to make the chroot a more full experience.
101 exit
102 chroot /media/chroot login
104 The first thing I do is then install some useful stuff I use all the time. These are optional, but I would recommend looking into them and seeing if they work for you.
106 sudo apt-get install vim tree strace htop dstat
108 Since this is a nodeJS blog, we'll download and compile node. Note that these instructions will work for any recent ubuntu system. First let's install the dependencies to install node.
110 sudo apt-get install git curl build-essential libssl-dev
112 Then I like to use nvm so I can compare multiple versions of node on the same system.
114 git clone
115 . nvm/ # Load the nvm function into this environment
116 export JOBS=2 # This should match the number of CPUs you have.
f3e24f5 @creationix Update the article to publish
authored Jul 1, 2011
117 nvm install v0.4.9 # Download, build, and install node and npm
118 nvm alias default v0.4.9 # Make this the default upon nvm load (login)
70aa05b @creationix Add initial arm chroot article
authored Jun 27, 2011
120 I would then add the `. $HOME/nvm/` line to my `.bashrc` so I get the nvm environment on every login.
122 Now that we have a normal node environment, we can do things like install http_trace and watch traffic over wifi or the modem.
124 sudo apt-get install libpcap-dev
125 npm install -g http_trace
126 sudo su
127 . /home/tim/nvm/
128 http_trace --headers
130 To make setup and teardown of the chroot easier, I'd recommend making shell scripts that contain the following. For setup. (this runs outside the chroot)
132 mount /media/internal/UbuntuNatty_armel.img /media/chroot
133 mount -t proc none /media/chroot/proc
134 mount -t sysfs none /media/chroot/sys
135 mount -o bind /dev /media/chroot/dev
136 cp /etc/resolv.conf /media/chroot/etc/resolv.conf
137 chroot /media/chroot login
139 And for cleanup.
141 umount /media/chroot/proc
142 umount /media/chroot/sys
143 umount /media/chroot/dev
144 umount /media/chroot
146 You now have a nice portable Ubuntu system complete with working apt-get, nvm, node, and npm. If you run out of space you can reclaim some by running `apt-get clean` from within the chroot. Also you can resize the partition when it's not mounted using `resize2fs`. This image can be copied to other devices and reused there.
f3e24f5 @creationix Update the article to publish
authored Jul 1, 2011
148 You'd be surprised what you can do in this environment that works in the host environment at well. For example, I've found that Ubuntu's `htop` program will run as is in the host webOS environment. I just copied the one in `/media/chroot/usr/bin/htop` to `/usr/bin/htop`. Also I found that SDL programs build in the Ubuntu environment will successfully link with the sdl libraries that are part of the PDK system and they will launch as cards when run in the host environment. With things like SDL bindings for node, you would be able to develop PDK apps in JavaScript on your TouchPad. See <> for a start on such a library. This library was developed, compiled and tested 100% on my TouchPad using my laptop as a bigger screen and keyboard.
70aa05b @creationix Add initial arm chroot article
authored Jun 27, 2011
f3e24f5 @creationix Update the article to publish
authored Jul 1, 2011
151 [TouchPad]:
Something went wrong with that request. Please try again.