Skip to content
Newer
Older
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
5
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
7
8 ## Create the Image
9
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.
11
12 sudo dd if=/dev/zero of=UbuntuNatty_armel.img bs=1M count=512
13
14 Then we want to put a filesystem on it.
15
16 sudo mkfs.ext3 UbuntuNatty_armel.img
17
18 Now we have a fully functional filesystem. Let's mount it.
19
20 mkdir build
21 sudo mount UbuntuNatty_armel.img build
22
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.
24
25 sudo qemu-debootstrap --arch armel --foreign natty build
26
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
28
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
34
35 The `qemu-debootstrap` process didn't seed this image with any apt repos, so lets add one.
36
37 echo "deb http://ports.ubuntu.com/ubuntu-ports/ natty main universe" > /etc/apt/sources.list
38
39 Now let's see what kind of binaries live in this world.
40
41 apt-get install file -y
42 cd /usr/bin
43 ./file ./file
44
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
46
47 file: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.16, stripped
48
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!
51
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.
53
54 To keep our host machine happy, we should logout of the chroot and clean things up.
55
56 exit
57 sudo umount build/sys
58 sudo umount build/proc
59 sudo umount build/dev
60 sudo umount build
61 rmdir build
62
63 Now we have a fully functional Ubuntu system in a single file.
64
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
66
67 novacom put file://media/internal/UbuntuNatty_armel.img < UbuntuNatty_armel.img
68
69 Then log into the device and mount it on the internal drive.
70
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
78
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.
80
81 adduser tim
82 id tim
83
84 Note the uid of the new user and group. My was:
85
86 uid=1002(tim) gid=1002(tim) groups=1002(tim)
87
88 Now let's enter the chroot and mirror the user there.
89
90 chroot /media/chroot
91 adduser tim --uid 1002
92
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.
94
95 apt-get install sudo
96 adduser tim sudo
97 echo "127.0.0.1 "`hostname` >> /etc/hosts
98
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.
100
101 exit
102 chroot /media/chroot login
103
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.
105
106 sudo apt-get install vim tree strace htop dstat
107
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.
109
110 sudo apt-get install git curl build-essential libssl-dev
111
112 Then I like to use nvm so I can compare multiple versions of node on the same system.
113
114 git clone http://github.com/creationix/nvm.git
115 . nvm/nvm.sh # 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
119
120 I would then add the `. $HOME/nvm/nvm.sh` line to my `.bashrc` so I get the nvm environment on every login.
121
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.
123
124 sudo apt-get install libpcap-dev
125 npm install -g http_trace
126 sudo su
127 . /home/tim/nvm/nvm.sh
128 http_trace --headers
129
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)
131
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
138
139 And for cleanup.
140
141 umount /media/chroot/proc
142 umount /media/chroot/sys
143 umount /media/chroot/dev
144 umount /media/chroot
145
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.
147
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 <https://github.com/creationix/node-sdl> 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
149
150
f3e24f5 @creationix Update the article to publish
authored Jul 1, 2011
151 [TouchPad]: http://www.hpwebos.com/us/products/pads/touchpad/index.html
Something went wrong with that request. Please try again.