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