cosmetic changes
[www.git] / tutorials / debian_on_samsung_galaxy.shtml
index 4343caec2a222736b8a96a5f4f6c6ca83cbf78e0..8dfbac26f1d8b244e53db87df49722635007c0eb 100644 (file)
@@ -21,10 +21,270 @@ AND on the tutorials index page!
 -->
 
 <h1>Installing Debian GNU/Linux in a chroot on a Samsung Galaxy S II</h1>
-<h2>(will probably work for most other Android phones, too)</h2>
 <p><em>(Last updated 2011-11-25)</em></p>
 
+<p>
+There are numerous how-tos on the web for installing a Debian chroot onto
+a smartphone running Android, but most of them either give you an image
+to install on your phone (which doesn't teach you anything) or have you
+set up the Debian system on your desktop and copy it to the phone.
+Neither of these options appeals to me, so I've done it my own way and
+written this document to light the way for others, too.
+</p>
 
+<p>
+Assumptions made:
+</p>
+
+<ul>
+<li>
+You have an Android smartphone with access to a root shell via SSH. I run
+<a href="http://www.cyanogenmod.com/">CyanogenMod 7.1</a> on my Galaxy S
+II, but any Android system with root SSH access will do, provided it has
+the following utilities:
+  <ul>
+  <li><tt>cat</tt></li>
+  <li><tt>chroot</tt></li>
+  <li><tt>dd</tt></li>
+  <li><tt>ln</tt></li>
+  <li><tt>mkdir</tt></li>
+  <li><tt>mke2fs</tt></li>
+  <li><tt>mount</tt></li>
+  <li><tt>tar</tt></li>
+  <li><tt>wget</tt> (used by <tt>cdebootstrap</tt>)</li>
+  </ul>
+If you don't have these utilities available to you, you will either need
+to find a way of installing them, or else find alternative ways of
+performing some of the steps in this how-to. Doing either is outside the
+scope of this how-to.
+</li>
+<li>
+You have a reasonably fast and high-capacity Internet connection
+available on your Android phone. I will not be held responsible for any
+excess usage charges you incur on your mobile Internet package while
+downloading Debian onto your phone. ;-)
+</li>
+<li>
+You have a basic understanding of Debian and its packaging system. This
+procedure does require manual invocation of <tt>dpkg</tt> if you run into
+problems -- if you don't know what <tt>dpkg</tt> is or how to use it,
+this method probably isn't for you.
+</li>
+</ul>
+
+<p>
+If not all of these assumptions are true, please don't contact me if
+something doesn't go right for you when following this how-to.
+</p>
+
+<h2>Preparation</h2>
+
+<p>
+The first thing you need to do is install <tt>cdebootstrap</tt> on your
+phone. <tt>cdebootstrap</tt> is a tool for bootstrapping a Debian system,
+usually from another Debian system, but in this case we'll be doing it
+from Android.
+</p>
+
+<p>
+So, to install <tt>cdebootstrap</tt>:
+</p>
+
+<ul>
+<li>
+Download the <a href="http://packages.debian.org/sid/cdebootstrap-static">cdebootstrap-static</a>
+package for your architecture. Both <tt>armel</tt> and <tt>armhf</tt>
+packages should work on recent Android phones; if you have an older
+phone, you may need to stick with <tt>armel</tt>. I'll be using
+<tt>armhf</tt> in this tutorial, but you are free to choose whichever
+architecture suits you best.
+</li>
+<li>
+Now, copy the files needed for <tt>cdebootstrap</tt> to work across to
+the phone. It doesn't matter where you put them, as long as you can
+execute the <tt>cdebootstrap</tt> binary -- so don't put it in
+<tt>/mnt/sdcard</tt>, as this is mounted with the <tt>noexec</tt> mount
+option. These files are nicely ready for us in a tarball located at
+<tt>./usr/lib/cdebootstrap/cdebootstrap_0.5.8+b1_armhf.tar.gz</tt> inside
+the package, so you can just pipe the contents of this tarball to ssh and
+extract it on the phone. I'm going to put it into
+<tt>/data/local/bin/</tt>, because that's already in the default
+<tt>$PATH</tt> so I don't have to care about it again.
+<code>
+$ mkdir /tmp/cdebootstrap && cd /tmp/cdebootstrap<br>
+$ ar x /path/to/cdebootstrap-static_0.5.8+b1_armhf.deb data.tar.gz<br>
+$ tar xzOf data.tar.gz ./usr/lib/cdebootstrap/cdebootstrap_0.5.8+b1_armhf.tar.gz | ssh root@$PHONE 'tar xzC /data/local/bin/'
+</code>
+Obviously, replace <tt>$PHONE</tt> with the hostname of your phone.
+</li>
+<li>
+Unfortunately, the version of <tt>cdebootstrap</tt> included in the
+tarball appears to segfault on Android systems. I don't know why this
+happens, but replacing it with the binary provided in the package seems
+to fix this issue:
+<code>
+$ tar xzOf data.tar.gz ./usr/bin/cdebootstrap-static | ssh root@$PHONE 'cat >/data/local/bin/cdebootstrap'
+</code>
+</li>
+</ul>
+
+<h2>Bootstrapping the Debian system</h2>
+
+<p>
+Now that you have a working <tt>cdebootstrap</tt> on your phone, you can
+use it to bootstrap Debian. First, however, you will need to create a
+file system for Debian to live on. It may be possible to create such a
+file system directly on the SD card, but as I don't really understand
+Android partitioning, I decided not to try this and risk losing data. (If
+someone knows of a way to do this, please
+<a href="about.shtml">contact me</a> and let me know!) The reason you
+need a separate file system for Debian is that Android mounts its own
+file systems with options like <tt>noexec</tt>, which would disallow
+executing all binaries placed on them (thus defeating the purpose of an
+operating system).
+</p>
+
+<p>
+The easiest way to set up a file system is to simply create a file in
+<tt>/mnt/sdcard</tt> and format that -- but beware, if you make this file
+larger than 2 GiB, the FAT file system it lives on will complain and you
+will lose the image next time you reboot. This happened to me the first
+time I tried it, and I hadn't made a backup, so I had to do it all over
+again. :(
+</p>
+
+<p>
+So, ssh to your phone, and run the following commands:
+</p>
+
+<code>
+# dd if=/dev/zero of=/mnt/sdcard/debian.img bs=1024 count=2000000<br>
+# mke2fs -Fj /mnt/sdcard/debian.img<br>
+# mkdir /debian<br>
+# mount -t ext3 -o noatime,errors=remount-ro /mnt/sdcard/debian.img /debian
+</code>
+
+<p>
+One last thing you need to do before setting up Debian:
+<tt>cdebootstrap</tt> expects the system shell to be at <tt>/bin/sh</tt>
+and fails without telling you why if it isn't. Since Android's system
+shell is <tt>/system/bin/sh</tt>, you have to appease
+<tt>cdebootstrap</tt> like so:
+</p>
+
+<code>
+# mkdir /bin && ln -s /system/bin/sh /bin/sh
+</code>
+
+<p>
+Now comes the fun bit, where we actually set up a Debian system. Still on
+your phone, run <tt>cdebootstrap</tt> to set up a file system tree in
+<tt>/debian</tt>. You may want to tweak the options below; I'm using
+<tt>ftp.debian-ports.org</tt> as my mirror because (at the time of
+writing) the <tt>armhf</tt> architecture is not yet fully integrated into
+the official archive. If you're using <tt>armel</tt>, or if you happen to
+live in the future, you can use your normal Debian mirror for this.
+Obviously, feel free to replace <tt>unstable</tt> with whichever suite
+you want to install.
+</p>
+
+<code>
+# cdebootstrap --allow-unauthenticated -c/data/local/bin/cdebootstrap-support/ unstable /debian http://ftp.debian-ports.org/debian/
+</code>
+
+<p>
+We have to use <tt>--allow-unauthenticated</tt> because <tt>gpgv</tt>
+isn't available to authenticate signatures on Android. This command can
+take a while to complete, so go and have a coffee or something while you
+wait for Debian to download and install.
+</p>
+
+<h2>Fixing broken packages</h2>
+
+<p>
+It was the case for me that some packages failed to install from
+<tt>cdebootstrap</tt>. Fortunately, it had at least progressed to the
+point where a shell and <tt>dpkg</tt> were installed and usable in the
+chroot, so now we can leave <tt>cdebootstrap</tt> behind and work in
+Debian:
+</p>
+
+<code>
+# chroot /debian /bin/bash<br>
+# cd /var/cache/bootstrap<br>
+# dpkg -i *.deb
+</code>
+
+<p>
+At this point, <tt>dpkg</tt> may run into some problems. As the package
+archive changes over time and different people install different suites,
+your problems will very likely not be the same as mine, so here's where
+your familiarity with the Debian package tools comes in. ;-)
+</p>
+
+<p>
+The main issue I ran into here was circular dependencies. These can be
+fixed by the use of the <tt>--force-depends</tt> option to <tt>dpkg</tt>,
+to allow it to install and configure one of the packages in the
+dependency loop, so that you can then install the others normally. For
+example, I found that <tt>libc6</tt> depends on <tt>libgcc1</tt>, which
+pre-depends on <tt>multiarch-support</tt>, which depends on
+<tt>libc6</tt>. After running:
+</p>
+
+<code>
+# dpkg --force-depends --configure multiarch-support
+</code>
+
+<p>
+this circular dependency was no longer a problem, although there were
+other remaining circular dependencies that could be fixed in a similar
+way.
+</p>
+
+<p>
+Once you have resolved a few circular dependencies and want to try again,
+it is a good idea to run:
+</p>
+
+<code>
+# dpkg --configure -a
+</code>
+
+<p>
+before telling <tt>dpkg</tt> to try installing packages again, as the
+shell glob <tt>*.deb</tt> will simply present the packages in
+alphabetical order, which is usually not a sensible order in terms of
+satisyfing dependencies. <tt>dpkg --configure -a</tt> will make sure that
+packages which are already installed (and thus don't depend on any which
+aren't installed yet) get configured before those which aren't already
+installed (and might depend on installed packages being configured).
+</p>
+
+<h2>Installing a standard Debian package set</h2>
+
+<p>
+<tt>cdebootstrap</tt> will only install packages with a priority of
+<tt>required</tt> or <tt>important</tt>. While this will give you a
+powerful Unix-like base system, it doesn't include many utilities that
+you would get with a standard Debian system. Once all of your package
+dependency issues have been resolved, and everything in
+<tt>/var/cache/bootstrap/</tt> is installed, you can use
+<tt>aptitude</tt> to install a standard Debian system:
+</p>
+
+<code>
+# echo 'deb http://ftp.debian-ports.org/debian unstable main' >/etc/apt/sources.list<br>
+# echo 'nameserver 8.8.8.8' >/etc/resolv.conf<br>
+# aptitude update<br>
+# aptitude install debian-ports-archive-keyring<br>
+# aptitude install '~prequired|~pimportant|~pstandard'
+</code>
+
+<p>
+What you choose to install beyond this depends on your wants and needs,
+and how you like your Debian. ;-)
+</p>
 
 </div>