write moar
[www.git] / tutorials / debian_on_samsung_galaxy.shtml
1 <!--#include virtual="/doctype.txt"-->
2 <html>
3
4 <!--#include virtual="/head.shtml"-->
5
6 <body>
7
8 <!--#include virtual="/header.shtml"-->
9
10 <!--#include virtual="/navigation.shtml"-->
11
12 <div id="content">
13
14 <!--
15
16 NOTE TO SELF:
17
18 When updating this page, be sure to update the "Last Updated" field below,
19 AND on the tutorials index page!
20
21 -->
22
23 <h1>Installing Debian GNU/Linux in a chroot on a Samsung Galaxy S II</h1>
24 <p><em>(Last updated 2011-11-25)</em></p>
25
26 <p>
27 There are numerous how-tos on the web for installing a Debian chroot onto
28 a smartphone running Android, but most of them either give you an image
29 to install on your phone (which doesn't teach you anything) or have you
30 set up the Debian system on your desktop and copy it to the phone.
31 Neither of these options appeal to me, so I've done it my own way and
32 written this document to light the way for others, too.
33 </p>
34
35 <p>
36 Assumptions made:
37 </p>
38
39 <ul>
40 <li>
41 You have an Android smartphone with access to a root shell via SSH. I run
42 <a href="http://www.cyanogenmod.com/">CyanogenMod 7.1</a> on my Galaxy S
43 II, but any Android system with root SSH access will do.
44 </li>
45 <li>
46 You have a reasonably fast and high-capacity Internet connection
47 available on your Android phone. I will not be held responsible for any
48 excess usage charges you incur on your mobile Internet package while
49 downloading Debian onto your phone. ;)
50 </li>
51 <li>
52 You have a basic understanding of Debian and its packaging system. This
53 procedure does require manual invocation of <tt>dpkg</tt> if you run into
54 problems -- if you don't know what <tt>dpkg</tt> is or how to use it,
55 this method probably isn't for you.
56 </li>
57 </ul>
58
59 <p>
60 If not all of these assumptions are true, please don't contact me if
61 something doesn't go right for you when following this how-to.
62 </p>
63
64 <h2>Preparation</h2>
65
66 <p>
67 The first thing you need to do is install <tt>cdebootstrap</tt> on your
68 phone. <tt>cdebootstrap</tt> is a tool for bootstrapping a Debian system,
69 usually from another Debian system, but in this case we'll be doing it
70 from Android.
71 </p>
72
73 <p>
74 So, to install <tt>cdebootstrap</tt>:
75 </p>
76
77 <ul>
78 <li>
79 Download the <a href="http://packages.debian.org/sid/cdebootstrap-static">cdebootstrap-static</a>
80 package for your architecture. Both armel and armhf packages should work
81 on recent Android phones; if you have an older phone, you may need to
82 stick with armel. I'll be using armhf in this tutorial, but you are free
83 to choose whichever architecture suits you best.
84 </li>
85 <li>
86 Now, copy the files needed for <tt>cdebootstrap</tt> to work across to
87 the phone. It doesn't matter where you put them, as long as you can
88 execute the <tt>cdebootstrap</tt> binary -- so don't put it in
89 <tt>/mnt/sdcard</tt>, as this is mounted with the <tt>noexec</tt> mount
90 option. These files are nicely ready for us in a tarball located at
91 <tt>./usr/lib/cdebootstrap/cdebootstrap_0.5.8+b1_armhf.tar.gz</tt> inside
92 the package, so you can just pipe the contents of this tarball to ssh and
93 extract it on the phone. I'm going to put it into
94 <tt>/data/local/bin/</tt>, because that's already in the default
95 <tt>$PATH</tt> so I don't have to care about it again.
96 <code>
97 $ mkdir /tmp/cdebootstrap && cd /tmp/cdebootstrap<br>
98 $ ar x /path/to/cdebootstrap-static_0.5.8+b1_armhf.deb data.tar.gz<br>
99 $ tar xzOf data.tar.gz ./usr/lib/cdebootstrap/cdebootstrap_0.5.8+b1_armhf.tar.gz | ssh root@$PHONE 'tar xzC /data/local/bin/'
100 </code>
101 Obviously, replace <tt>$PHONE</tt> with the hostname of your phone.
102 </li>
103 <li>
104 Unfortunately, the version of <tt>cdebootstrap</tt> included in the
105 tarball appears to segfault on Android systems. I don't know why this
106 happens, but replacing it with the binary provided in the package seems
107 to fix this issue:
108 <code>
109 $ tar xzOf data.tar.gz ./usr/bin/cdebootstrap-static | ssh root@$PHONE 'cat >/data/local/bin/cdebootstrap'
110 </code>
111 </li>
112 </ul>
113
114 <h2>Bootstrapping the Debian system</h2>
115
116 <p>
117 Now that you have a working <tt>cdebootstrap</tt> on your phone, you can
118 use it to bootstrap Debian. First, however, you will need to create a
119 file system for Debian to live on. It may be possible to create such a
120 file system directly on the SD card, but as I don't really understand
121 Android partitioning, I decided not to try this and risk losing data. (If
122 someone knows of a way to do this, please
123 <a href="about.shtml">contact me</a> and let me know!) The reason you
124 need a separate file system for Debian is that Android mounts its own
125 file systems with options like <tt>noexec</tt>, which would disallow
126 executing all binaries placed on them (thus defeating the purpose of an
127 operating system).
128 </p>
129
130 <p>
131 The easiest way to set up a file system is to simply create a file in
132 <tt>/mnt/sdcard</tt> and format that -- but beware, if you make this file
133 larger than 2 GiB, the FAT file system it lives on will complain and you
134 will lose the image next time you reboot. This happened to me the first
135 time I tried it, and I hadn't made a backup, so I had to do it all over
136 again. :(
137 </p>
138
139 <p>
140 So, ssh to your phone, and run the following commands:
141 </p>
142
143 <code>
144 # dd if=/dev/zero of=/mnt/sdcard/debian.img bs=1024 count=2000000<br>
145 # mke2fs -Fj /mnt/sdcard/debian.img<br>
146 # mkdir /debian<br>
147 # mount -t ext3 -o noatime,errors=remount-ro /mnt/sdcard/debian.img /debian
148 </code>
149
150 <p>
151 One last thing you need to do before setting up Debian:
152 <tt>cdebootstrap</tt> expects the system shell to be at <tt>/bin/sh</tt>
153 and fails without telling you why if it isn't. Since Android's system
154 shell is <tt>/system/bin/sh</tt>, you have to appease
155 <tt>cdebootstrap</tt> like so:
156 </p>
157
158 <code>
159 # mkdir /bin && ln -s /system/bin/sh /bin/sh
160 </code>
161
162 <p>
163 Now comes the fun bit, where we actually set up a Debian system. Still on
164 your phone, run <tt>cdebootstrap</tt> to set up a file system tree in
165 <tt>/debian</tt>. You may want to tweak the options below; I'm using
166 <tt>ftp.debian-ports.org</tt> as my mirror because (at the time of
167 writing) the armhf architecture is not yet fully integrated into the
168 official archive. If you're using armel, or if you happen to live in the
169 future, you can use your normal Debian mirror for this. Obviously, feel
170 free to replace <tt>unstable</tt> with whichever suite you want to
171 install.
172 </p>
173
174 <code>
175 # cdebootstrap --allow-unauthenticated -c/data/local/bin/cdebootstrap-support/ unstable /debian http://ftp.debian-ports.org/debian/
176 </code>
177
178 <p>
179 We have to use <tt>--allow-unauthenticated</tt> because <tt>gpgv</tt>
180 isn't available to authenticate signatures on Android. This command can
181 take a while to complete, so go and have a coffee or something while you
182 wait for Debian to download and install.
183 </p>
184
185 <h2>Fixing broken packages</h2>
186
187 <p>
188 It was the case for me that some packages failed to install from
189 <tt>cdebootstrap</tt>. Fortunately, it had at least progressed to the
190 point where a shell and <tt>dpkg</tt> were installed and usable in the
191 chroot, so now we can leave <tt>cdebootstrap</tt> behind and work in
192 Debian:
193 </p>
194
195 <code>
196 # chroot /debian /bin/bash<br>
197 # cd /var/cache/bootstrap<br>
198 # dpkg -i *.deb
199 </code>
200
201 <p>
202 At this point, <tt>dpkg</tt> may run into some problems. As the package
203 archive changes over time and different people install different suites,
204 your problems will very likely not be the same as mine, so here's where
205 your familiarity with the Debian package tools comes in. ;)
206 </p>
207
208 <p>
209 The main issue I ran into here was circular dependencies. These can be
210 fixed by the use of the <tt>--force-depends</tt> option to <tt>dpkg</tt>,
211 to allow it to install and configure one of the packages in the
212 dependency loop, so that you can then install the others normally. For
213 example, I found that <tt>libc6</tt> depends on <tt>libgcc1</tt>, which
214 pre-depends on <tt>multiarch-support</tt>, which depends on
215 <tt>libc6</tt>. After running:
216 </p>
217
218 <code>
219 # dpkg --force-depends --configure multiarch-support
220 </code>
221
222 <p>
223 this circular dependency was no longer a problem, although there were
224 other remaining circular dependencies that could be fixed in a similar
225 way.
226 </p>
227
228 <h2>Installing a standard Debian package set</h2>
229
230 <p>
231 <tt>cdebootstrap</tt> will only install packages with a priority of
232 <tt>required</tt> or <tt>important</tt>. While this will give you a
233 powerful Unix-like base system, it doesn't include many utilities that
234 you would get with a standard Debian system. Once all of your package
235 dependency issues have been resolved, and everything in
236 <tt>/var/cache/bootstrap/</tt> is installed, you can use
237 <tt>aptitude</tt> to install a standard Debian system:
238 </p>
239
240 <code>
241 # aptitude install '~prequired|~pimportant|~pstandard'
242 </code>
243
244 <p>
245 What you choose to install beyond this depends on your wants and needs,
246 and how you like your Debian. ;)
247 </p>
248
249 </div>
250
251 <!--#include virtual="/footer.shtml"-->
252
253 </body>
254
255 </html>