<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>64k &#187; boot</title>
	<atom:link href="http://www.64k-tec.de/tag/boot/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.64k-tec.de</link>
	<description>Software development and more ...</description>
	<lastBuildDate>Tue, 10 Jan 2012 21:58:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>FRITZ!Box tuning part 4: Cross-building and installing additional applications</title>
		<link>http://www.64k-tec.de/2011/07/fritzbox-tuning-part-4-cross-building-and-installing-additional-applications/</link>
		<comments>http://www.64k-tec.de/2011/07/fritzbox-tuning-part-4-cross-building-and-installing-additional-applications/#comments</comments>
		<pubDate>Tue, 26 Jul 2011 20:14:36 +0000</pubDate>
		<dc:creator>cp</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Root]]></category>
		<category><![CDATA[administration]]></category>
		<category><![CDATA[boot]]></category>
		<category><![CDATA[Buildroot]]></category>
		<category><![CDATA[Console]]></category>
		<category><![CDATA[cross-building]]></category>
		<category><![CDATA[cross-compiling]]></category>
		<category><![CDATA[dropbear]]></category>
		<category><![CDATA[firmware]]></category>
		<category><![CDATA[FritzBox]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[glibc]]></category>
		<category><![CDATA[installation]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[MIPS]]></category>
		<category><![CDATA[OpenSSH]]></category>
		<category><![CDATA[overlay]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[tmpfs]]></category>
		<category><![CDATA[Toolchain]]></category>

		<guid isPermaLink="false">http://www.64k-tec.de/?p=2338</guid>
		<description><![CDATA[The articles I wrote about the FRITZ!Box are pretty popular. They are creating the most traffic on my website. I understand this, cause the FRITZ!Box is a really great piece of hardware and AVM is also a company which knows how to make their users happy by serving regular updates to the firmware. Although I [...]]]></description>
			<content:encoded><![CDATA[<p>The articles I wrote about the <a href="/tag/fritzbox/" target="_blank">FRITZ!Box</a> are pretty popular. They are creating the most traffic on my website. I understand this, cause the FRITZ!Box is a really great piece of hardware and AVM is also a company which knows how to make their users happy by serving regular updates to the firmware. Although I didn't tuned my FRITZ!Box any further, I updated it with the latest <a href="http://www.avm.de/de/Service/Service-Portale/Labor/index.php" target="_blank">Labor</a> firmware version regularly. At some point the sshd setup (with dropbear) doesn't worked anymore and I decided it is the time to update my software as well. Beside that it didn't work anymore it is always a good idea to update software which allow access to a host from everywhere very regularly. Anyway, it turned out this isn't as simple as I initial thought. Therefore here is the next post in the FRITZ!Box tuning series, which shows how to cross-build software for the MIPS32 architecture used in the FRITZ!Box and in particular get the sshd software to life again. I use a FRITZ!Box Fon WLAN 7270 v2 and the firmware is 54.05.05. Please make sure you read the other <a href="/tag/fritzbox/" target="_blank">FRITZ!Box articles</a> as well, cause some of the information given there still applies.</p>
<span class="sb_alert">As already mentioned in the other articles, there is no warranty that this will work on your side and I am not responsible for any damage which may happen. You have been warned!</span>
<h2>Cross-building for the MIPS32 architecture</h2>
<p>The FRITZ!Box itself doesn't include any build tools. Why should it! It's an end-consumer product which is usually never changed, beside the user is updating it with an official firmware update. So we have to do it ourself. Creating a build chain for another architecture is usually <a href="http://www.landley.net/writing/docs/cross-compiling.html" target="_blank">hard</a>. There are several things you have to consider. This includes problems with the configuration, endianess differences between the host and the target, bit depths and depending shared libraries which have to be available in the target format. Fortunately this is a common problem, especially for embedded systems, which means there is a proper solution called <a href="http://buildroot.uclibc.org/" target="_blank">Buildroot</a> (look at the first line in <code class="cmd">dmsg</code>, when the FRITZ!Box has booted and you know what I mean). Buildroot is a build system which allows, similar to the Linux kernel, configuring and generating complete embedded Linux systems. Regardless how configured, Buildroot builds a complete Linux system, always. This is of course a little bit overhead for us, but we just pick out the relevant files. First we need to know which architecture we target. You have to login into the FRITZ!Box either using <a href="http://www.64k-tec.de/2010/01/fritzbox-tuning-part-1-enable-remote-access-over-ssh/" target="_blank">telnet or a working ssh daemon</a>. To get some information about the hardware used in your FRITZ!Box execute <code class="cmd">cat /proc/cpuinfo</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;"># cat /proc/cpuinfo
system type             : TI UR8 (7270)
processor               : 0
cpu model               : MIPS 4KEc V6.8
BogoMIPS                : 359.62
wait instruction        : yes
microsecond timers      : yes
tlb_entries             : 16
extra interrupt vector  : yes
hardware watchpoint     : no
ASEs implemented        :
shadow register sets    : 1
core                    : 0
VCED exceptions         : not available
VCEI exceptions         : not available</pre></div></div>

<p>Here we learn that the FRITZ!Box is using a <a href="http://www.mips.com/products/cores/hard-ip-cores/4kec-hard-ip-cores/" target="_blank">MIPS</a> processor. In this case it uses the <a href="http://en.wikipedia.org/wiki/Endianness" target="_blank">little-endian</a> format. Next you need some information about the system C library. On normal Linux (desktop) systems the GNU C Library is used. This library is heavyweight and therefore the <a href="http://uclibc.org/" target="_blank">uClibc</a> was invented for embedded systems. By executing <code class="cmd">ls /lib/libuClibc*</code> you get the version installed. In my case it is 0.9.31. Now we have all information needed to configure Buildroot. <a href="http://buildroot.uclibc.org/download.html" target="_blank">Download</a> and extract the source of Buildroot.</p>
<span class="sb_tip">If you are working on a Mac like me (where Buildroot doesn't work), <a href="http://www.virtualbox.org/" target="_blank">VirtualBox</a> with a Linux system as a guest is a good alternative <img src='http://www.64k-tec.de/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</span>
<p>By executing <code class="cmd">make menuconfig</code> in the root directory of Buildroot you are able to configure it. The most important options are <strong>Target Architecture</strong>, set this to <code>mipsel</code> and <strong>Target Architecture Variant</strong>, set this to <code>mips 32r2</code>. In <strong>Toolchain -&gt; uClibc C library Version</strong> select a version which is close to the one on your FRITZ!Box. Next you have to select which packages should be build in <strong>Package Selection for the target</strong>. Choose whatever you want but for the beginning <strong>Networking applications -&gt; openssh</strong> might be enough. If you prefer a more lightweight ssh implementation <strong>dropbear</strong> may also an option. In the following we use OpenSSH. You can also tweak the build options, like the <code class="cmd">gcc</code> version to use or set the gcc optimization level to <code>-O3</code> in favor of size. After saving the configuration and executing <code class="cmd">make</code>, it is time for a coffee. When the build has been finished all relevant files are in <code class="path">output/target/</code>. We only pack the directories and files which are necessary. For example the <code>libuClibc</code> was built by Buildroot, but we will use the one from the FRITZ!Box. The following create a zip archive of the files for OpenSSH.<a name="zip"></a></p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">zip -r ../../local.zip &quot;lib/libutil* lib/libnsl* lib/libresolv* \
 lib/libcrypt* usr/lib usr/share usr/sbin usr/bin etc/ssh_config \
 etc/sshd_config&quot;</pre></div></div>

<h2>Creating overlay directories on the root filesystem</h2>
<p>The following is based on ideas invented by the guys at <a href="http://www.spblinux.de/fbox.new/" target="_blank">http://www.spblinux.de/</a>. Our aim is it to have as little persistent changes on the FRITZ!Box as possible. This allows us to go back to the original state with a simple reboot. Therefore all writeable directories will be on a <a href="http://en.wikipedia.org/wiki/Tmpfs" target="_blank">tmpfs</a> filesystem. tmpfs filesystems are created in RAM and the content will be removed when the filesystem is unmounted. Because the FRITZ!Box has only limited RAM, we will not copy our applications into the tmpfs filesystem, but just create symbolic links to the files on the USB stick (where usually much more space is available). Because we want the applications from the FRITZ!Box and our own one be accessible from the root filesystem, we need to merge both into the tmpfs directory. Therefore first symbolic links into the original filesystem and second symbolic links to the USB stick are created. The second step is only performed when there isn't a file already. This makes sure the original files are always preferred over our own one. The last step is binding the directories to the root filesystem. Summarized the following will be done:</p>
<ol>
<li>remount <code class="path">/</code> readonly</li>
<li>mount <code class="path">/</code> into <code class="path">/var/_ro_</code></li>
<li>create tmpfs in <code class="path">/var/_overlay_</code></li>
<li>symlink the files from <code class="path">/var/_ro_</code> into <code class="path">/var/_overlay_</code></li>
<li>symlink the files from the USB stick into <code class="path">/var/_overlay_</code></li>
<li>bind the directories in <code class="path">/var/_overlay_</code> to <code class="path">/</code></li>
</ol>
<p>This script does the previous steps. It uses helper methods which are defined in <code class="path">common.sh</code>. You will find all the necessary files in the package you can download at the end of this article.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
&nbsp;
. common.sh
&nbsp;
<span style="color: #007800;">MNT</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>_overlay_
<span style="color: #007800;">BASE_RO</span>=<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>_ro_
<span style="color: #007800;">BASE_OVL</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${BASE}</span>/local&quot;</span>
&nbsp;
<span style="color: #007800;">DIRS</span>=<span style="color: #ff0000;">&quot;lib etc usr/bin usr/sbin usr/lib usr/share&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># remount root readonly</span>
<span style="color: #ff0000;">&quot;<span style="color: #007800;">${MOUNT}</span>&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${GREP}</span>&quot;</span> <span style="color: #ff0000;">&quot;/dev/root&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${GREP}</span>&quot;</span> <span style="color: #660033;">-q</span> ro
<span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$?</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> _lmt <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>root <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #ff0000;">&quot;-o remount -r&quot;</span>
<span style="color: #666666; font-style: italic;"># bind root into var</span>
_lchkmnt <span style="color: #ff0000;">&quot;<span style="color: #007800;">${BASE_RO}</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$?</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
  _lmkdir <span style="color: #ff0000;">&quot;<span style="color: #007800;">${BASE_RO}</span>&quot;</span>
  _lmnt <span style="color: #000000; font-weight: bold;">/</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${BASE_RO}</span>&quot;</span> <span style="color: #ff0000;">&quot;-o bind&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># create our overlay dir</span>
_lchkmnt <span style="color: #ff0000;">&quot;<span style="color: #007800;">${MNT}</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$?</span> <span style="color: #660033;">-eq</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
  _lmkdir <span style="color: #ff0000;">&quot;<span style="color: #007800;">${MNT}</span>&quot;</span>
  _lmnt tmpfs <span style="color: #ff0000;">&quot;<span style="color: #007800;">${MNT}</span>&quot;</span> <span style="color: #ff0000;">&quot;-t tmpfs&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># link/copy the base stuff</span>
<span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #800000;">${DIRS}</span>; <span style="color: #000000; font-weight: bold;">do</span>
  _lsymlnk_dir <span style="color: #ff0000;">&quot;<span style="color: #007800;">${BASE_RO}</span>&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${i}</span>&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${MNT}</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># link/copy our own stuff</span>
<span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #800000;">${DIRS}</span>; <span style="color: #000000; font-weight: bold;">do</span>
  _lsymlnk_dir <span style="color: #ff0000;">&quot;<span style="color: #007800;">${BASE_OVL}</span>&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${i}</span>&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${MNT}</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># now bind all overlay dirs to root</span>
<span style="color: #666666; font-style: italic;"># (this is critical don't interupt)</span>
<span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #800000;">${DIRS}</span>; <span style="color: #000000; font-weight: bold;">do</span>
  _lmnt <span style="color: #ff0000;">&quot;<span style="color: #007800;">${MNT}</span>/<span style="color: #007800;">${i}</span>&quot;</span> <span style="color: #ff0000;">&quot;/<span style="color: #007800;">${i}</span>&quot;</span> <span style="color: #ff0000;">&quot;-o bind&quot;</span>
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">0</span></pre></div></div>

<p>Now copy the zip archive you have created <a href="#zip">above</a> to the FRITZ!Box and unpack it on the USB stick in <code class="path">addons/local/</code>. The scripts have to be placed into <code class="path">addons/</code>. You need to configure the following in <code class="path">common.sh</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;"># the base of the usb stick goes here:
BASE=&quot;/var/media/ftp/FLASH-DISK-01/addons&quot;
# add the encrypted root password here:
PASS=&quot;&quot;</pre></div></div>

<p>Point to the base directory within the USB stick in <code>BASE</code>. Also add your encrypted root password into <code>PASS</code>. This will create a root user in <code class="path">/etc/passwd</code> automatically.</p>
<p>If you believe all is correct, you can start the installation by executing <code class="cmd">./install.sh</code>. If no errors are shown, you can try ssh. <code class="cmd">ssh -v</code> should output something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;"># ssh -v
OpenSSH_5.8p2, OpenSSL 1.0.0d 8 Feb 2011
...</pre></div></div>

<p>Additional to the standard filesystems mounted, you should see the following when executing <code class="cmd">mount</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;"># mount
...
/dev/root on /var/_ro_ type squashfs (ro,relatime)
tmpfs on /var/_overlay_ type tmpfs (rw,relatime)
tmpfs on /lib type tmpfs (rw,relatime)
tmpfs on /etc type tmpfs (rw,relatime)
tmpfs on /usr/bin type tmpfs (rw,relatime)
tmpfs on /usr/sbin type tmpfs (rw,relatime)
tmpfs on /usr/lib type tmpfs (rw,relatime)
tmpfs on /usr/share type tmpfs (rw,relatime)</pre></div></div>

<h2>Configure and start the OpenSSH daemon</h2>
<p>Now that we have our brand new OpenSSH on the FRITZ!Box working, we need some last steps to let the daemon running correctly. First create a private/public RSA host key pair for the server process by executing:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">ssh-keygen -t rsa -f addons/config/ssh_host_rsa_key</pre></div></div>

<p><code class="path">addons/config/</code> have to be an existing directory below the USB stick base. Next create a file <code class="path">addons/config/sshd_config</code> in the same directory with the following content:</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">Protocol               2
UsePrivilegeSeparation no
Subsystem              sftp /usr/lib/sftp-server</pre></div></div>

<p>The following script could be use to start the <code class="cmd">sshd</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
&nbsp;
. common.sh
&nbsp;
<span style="color: #007800;">SSHD</span>=<span style="color: #ff0000;">&quot;/usr/sbin/sshd&quot;</span>
<span style="color: #007800;">SSH_SPKEY</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${BASE}</span>/config/ssh_host_rsa_key&quot;</span>
<span style="color: #007800;">SSH_CFG</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">${BASE}</span>/config/sshd_config&quot;</span>
<span style="color: #007800;">SSH_TPKEY</span>=<span style="color: #ff0000;">&quot;/var/tmp/ssh_host_rsa_key&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># sanity check</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${SSHD}</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> _lce <span style="color: #007800;">$?</span> <span style="color: #ff0000;">&quot;sshd not installed&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># start in tmp</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>tmp   
&nbsp;
<span style="color: #c20cb9; font-weight: bold;">mount</span> <span style="color: #660033;">-o</span> remount devpts <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>pts <span style="color: #660033;">-t</span> devpts
&nbsp;
<span style="color: #666666; font-style: italic;"># cp key and make them user readable only</span>
_lcp_file <span style="color: #ff0000;">&quot;<span style="color: #007800;">${SSH_SPKEY}</span>&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${SSH_TPKEY}</span>&quot;</span>
_lchmod <span style="color: #000000;">400</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${SSH_TPKEY}</span>&quot;</span>             
&nbsp;
<span style="color: #666666; font-style: italic;"># start sshd</span>
<span style="color: #ff0000;">&quot;<span style="color: #007800;">${SSHD}</span>&quot;</span> <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${SSH_CFG}</span>&quot;</span> <span style="color: #660033;">-h</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${SSH_TPKEY}</span>&quot;</span></pre></div></div>

<p>Please note that you have to set up the root user first and maybe adjust your port forwarding rules to allow access from the Internet as described in this <a href="http://www.64k-tec.de/2010/01/fritzbox-tuning-part-1-enable-remote-access-over-ssh/" target="_blank">post</a>. The package will contain a script <code class="cmd">startup.sh</code> which will add the root user automatically.</p>
<p>Here is the obligatory screenshot which shows <code class="cmd">htop</code> with color terminal support. You see OpenSSH and OpenVPN in the list of the running processes.</p>
<p style="text-align: center;"><a href="/wordpress/wp-content/uploads/htop-e1311594537898.png"><img class="aligncenter size-medium wp-image-2427" title="htop" src="/wordpress/wp-content/uploads/htop-e1311594537898-300x176.png" alt="" width="300" height="176" /></a></p>
<h2>Conclusion</h2>
<p>Thanks to Buildroot it is pretty easy to cross-build for embedded machines these days. With some tricky directory rebinding new applications could be injected into the FRITZ!Box root filesystem without overwriting the existing firmware. Now you should be able to build any software already bundled with Buildroot or even <a href="http://buildroot.uclibc.org/buildroot.html#add_packages" target="_blank">add new one to the build process</a>. Happy cross-building!</p>
<span class="sb_download">You can download the scripts and binaries discussed in this post:<br />
<a href="/downloads/FRITZBox/FRITZBox_OpenSSH_5.8p2.tar.gz">FRITZBox_OpenSSH_5.8p2.tar.gz</a> (3.8MB, <a href="/downloads/FRITZBox/FRITZBox_OpenSSH_5.8p2.tar.gz-sha1.txt">SHA1</a>)</span>
]]></content:encoded>
			<wfw:commentRss>http://www.64k-tec.de/2011/07/fritzbox-tuning-part-4-cross-building-and-installing-additional-applications/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Installing Linux on a USB hard disk for the MacBook Pro</title>
		<link>http://www.64k-tec.de/2010/04/installing-linux-on-a-usb-hard-disk-for-the-macbook-pro/</link>
		<comments>http://www.64k-tec.de/2010/04/installing-linux-on-a-usb-hard-disk-for-the-macbook-pro/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 02:29:11 +0000</pubDate>
		<dc:creator>cp</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Root]]></category>
		<category><![CDATA[administration]]></category>
		<category><![CDATA[boot]]></category>
		<category><![CDATA[Boot Camp]]></category>
		<category><![CDATA[EFI]]></category>
		<category><![CDATA[FireWire]]></category>
		<category><![CDATA[Gentoo]]></category>
		<category><![CDATA[hard disk]]></category>
		<category><![CDATA[installation]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[MacBook Pro]]></category>
		<category><![CDATA[removable media]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[USB]]></category>

		<guid isPermaLink="false">http://www.64k-tec.de/?p=885</guid>
		<description><![CDATA[One of the features of Mac OS X I love, is the possibility to install Mac OS X on any attached removable media, like a FireWire or USB hard disk. This makes it really easy for me to test VirtualBox on the several versions of Mac OS X we support (formerly Tiger, now Leopard and [...]]]></description>
			<content:encoded><![CDATA[<p>One of the features of Mac OS X I love, is the possibility to install Mac OS X on any attached removable media, like a FireWire or USB hard disk. This makes it really easy for me to test VirtualBox on the several versions of Mac OS X we support (formerly Tiger, now Leopard and Snow Leopard). The advantage of this setup is that I don't waste disk space for operation systems I usually don't use very often. Currently I have a 150GB hard disk in my MacBook Pro which is really not that much if you deal in the virtualization business. There are several test VM's of any kind of guest operation systems and of course the ISO's to install them. The second main OS, I do much of my work, is Linux. For this I have a standard PC with Gentoo on it, which have all that I need. Unfortunately this doesn't really help when I on travel. As I soon be away for some time, I decided I need, at least for testing, the same flexibility mentioned above for a Linux installation. And here the problems start to arise. Of course Apple didn't really support installing other OS's than Mac OS X on Apple hardware. Yes, there is Boot Camp, but this is mainly for Windows, is very inflexible and doesn't really help if you try to install something on another place than the integrated hard disk. There are projects like <a href="http://refit.sourceforge.net/" target="_blank">rEFIt</a>, which even makes Boot Camp superfluous, but this project has really bad USB boot support. In the following I will explain how it is even possible to install Ubuntu 9.10 on a USB hard disk.</p>
<p>The hardware used, as already said, is a MacBook Pro 3,1 and a Western Digital My Passport Essential 500GB USB hard disk. Other combination may work, but I don't guarantee this, as always. Also you should be warned that anything I describe here could destroy your existing installation and I'm not responsible for that. Doing some kind of backup might be a good idea. Time Machine is easy to use <img src='http://www.64k-tec.de/wordpress/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p>Before we start, as usual, the credits for some sites I get my information from. This is mainly the <a href="http://www.produnis.de/blog/?p=678" target="_blank">Produnis blog</a>, <a href="http://blog.christophersmart.com/2009/07/23/linux-on-an-apple-xserve-efi-only-machine/" target="_blank">the Blog of Chris</a>, the <a href="https://wiki.ubuntu.com/MactelSupportTeam/CommunityHelpPages" target="_blank">Ubuntu wiki</a> and of course the guys around the <a href="http://grub.enbug.org/TestingOnMacbook" target="_blank">Grub2</a> development.</p>
<h2>Creating the EFI boot loader</h2>
<p>Apple doesn't use the legacy BIOS to boot their machines, they use the <a href="http://en.wikipedia.org/wiki/Extensible_Firmware_Interface" target="_blank"><em>Extensible Firmware Interface</em></a> (EFI). This new way of booting operation systems is very flexible, as the name suggest, but has several drawbacks, like most of the standard operation system doesn't speaks it language. Although Linux can be configured to use EFI directly we will emulate a legacy BIOS in the following. For this we need a connector which makes EFI and Linux happy and let them both work smoothly together. This connector is <a href="http://www.gnu.org/software/grub/" target="_blank">Grub2</a>, which is in development for several years now. It's the successor of Grub and is the standard in many popular Linux distributions these days. We have to build a version our self, for which an existing Linux installation is really helpful. I used my 64-bit Gentoo installation. First you have to find out if the EFI installation on your target Apple machine is 64 or 32-bit. You can do this by executing:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">ioreg <span style="color: #660033;">-l</span> <span style="color: #660033;">-p</span> IODeviceTree <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> firmware-abi</pre></div></div>

<p>This will return EFI64 or EFI32 respectively. In my case I need the 64-bit version, which is a little bit surprising when I consider that my MacBook Pro isn't able to boot a 64-bit Snow Leopard. Anyway, grab the <a href="ftp://alpha.gnu.org/gnu/grub/grub-1.98.tar.gz">latest version</a> of Grub2 and unpack it on the Linux machine. Please note that you need a gcc with multilib support if you are targeting an architecture which isn't the same as the host one. Use the following to configure Grub2 and to build it. Of course you have to adjust the target architecture if it is a different one.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>configure <span style="color: #660033;">--with-platform</span>=efi <span style="color: #660033;">--target</span>=x86_64 <span style="color: #660033;">--disable-werror</span>
<span style="color: #c20cb9; font-weight: bold;">make</span></pre></div></div>

<p>When this is finished you create the EFI package by executing<a name="mkimage"></a></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>grub-mkimage <span style="color: #660033;">-d</span> . <span style="color: #660033;">-o</span> bootx86.efi <span style="color: #000000; font-weight: bold;">*</span>.mod</pre></div></div>

<p>Here I included all modules which are available. If size matter for you, you could of course make a selective choice on the modules included. I didn't test this myself, so you have to find out yourself which one are important.</p>
<h2>Whipping the USB hard disk into shape</h2>
<p style="text-align: left;">Next we have to prepare the USB hard disk for the new installation. In the following I assume your USB hard disk doesn't contain any valid data and could be reformatted without data lose. Make a backup of your data first if this isn't the case on your side. Apple uses the <a href="http://en.wikipedia.org/wiki/GUID_Partition_Table" target="_blank">GUID Partition Table</a> scheme to organize their partitions on a hard disk. This specification is part of EFI and remove many limitations of the <em><a href="http://en.wikipedia.org/wiki/Master_Boot_Record" target="_blank">Master boot record</a></em> (MBR) scheme, which is widely used in the PC world. That is e.g. the disk size limitation of 2TB or the maximum of 4 primary partitions. You reformat your disk, using the Disk Utility application of Mac OS X. Make sure all existing partitions on the disk are unmounted. When necessary, change the partition scheme from MBR to GUID in the Options dialog of the Partitions window. Select the partitions count you want to use. You need at least 3 partitions to make Linux works fine. My partition scheme looks like in the following: <a href="/wordpress/wp-content/uploads/Disk-Utility.png"><img class="aligncenter size-full wp-image-972" title="GUID Partition Table scheme" src="/wordpress/wp-content/uploads/Disk-Utility.png" alt="" width="188" height="390" /></a>As you can see I have 5 partitions configured. The first one is an additional Snow Leopard installation for testing. I also added a Data partition at the end for making the data transfer between the different operation systems as easy as possibly. LINUXBOOT is a small partition which will contain the EFI boot loader (size it 50MB or something like that). Linux Swap, obviously, will become the swap partition of the Linux installation. DISK1S5 is the Linux root partition itself. The Data partition has to be formated as Mac OS Extended. Don't use the Journaled version of HFS+, cause this makes trouble on the Linux side. The other partitions have to be formatted as MS-DOS (FAT).</p>
<p>After applying the changes we can add the EFI boot loader to the LINUXBOOT partition. The Apple EFI implementation is searching for a file with the efi extensions on all bootable hard disks. Mount LINUXBOOT and create a <code class="path">efi/boot</code> directory on the root path. Copy the <a href="#mkimage">bootx86.efi</a> file into the boot/ directory. As bootx86.efi is a Grub2 boot loader we need a valid Grub2 configuration file. The following <code class="path">grub.cfg</code> shows the configuration for a Ubuntu 9.10 i386 installation. For the 64-bit version or any other version of Ubuntu the settings might be slightly different.</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000099;">menuviewer</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #933;">&quot;text&quot;</span>
<span style="color: #000099;">timeout</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">10</span>
<span style="color: #000099;">default</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">0</span>
set F1<span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">ctrl-x</span>
menuentry <span style="color: #933;">&quot;ubuntu-9.10-desktop-i386&quot;</span>
<span style="">&#123;</span>
 fakebios
 search --set -f /boot/vmlinuz-2.6.31-<span style="">14</span>-generic
 linux /boot/vmlinuz-2.6.31-<span style="">14</span>-generic root<span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">UUID=4e140981-4ab3-41a2-a2fb-26b1287beb87 ro quiet splash noefi video=efifb</span>
 initrd /boot/initrd.img-2.6.31-<span style="">14</span>-generic
<span style="">&#125;</span>
menuentry <span style="color: #933;">&quot;ubuntu-9.10-desktop-i386 single&quot;</span>
<span style="">&#123;</span>
 fakebios
 search --set -f /boot/vmlinuz-2.6.31-<span style="">14</span>-generic
 linux /boot/vmlinuz-2.6.31-<span style="">14</span>-generic root<span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">UUID=4e140981-4ab3-41a2-a2fb-26b1287beb87 ro noefi video=efifb single</span>
 initrd /boot/initrd.img-2.6.31-<span style="">14</span>-generic
<span style="">&#125;</span>
menuentry <span style="color: #933;">&quot;ubuntu-9.10-desktop-i386 text&quot;</span>
<span style="">&#123;</span>
 fakebios
 search --set -f /boot/vmlinuz-2.6.31-<span style="">14</span>-generic
 linux /boot/vmlinuz-2.6.31-<span style="">14</span>-generic root<span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">UUID=4e140981-4ab3-41a2-a2fb-26b1287beb87 ro noefi vga=normal</span>
 initrd /boot/initrd.img-2.6.31-<span style="">14</span>-generic
<span style="">&#125;</span>
menuentry <span style="color: #933;">&quot;Mac OS X&quot;</span>
<span style="">&#123;</span>
 search --set -f /usr/standalone/i386/boot.efi
 chainloader /usr/standalone/i386/boot.efi
<span style="">&#125;</span>
menuentry <span style="color: #933;">&quot;CD&quot;</span>
<span style="">&#123;</span>
 appleloader CD
<span style="">&#125;</span>
menuentry <span style="color: #933;">&quot;mbr&quot;</span>
<span style="">&#123;</span>
 appleloader HD
<span style="">&#125;</span>
menuentry <span style="color: #933;">&quot;reboot&quot;</span>
<span style="">&#123;</span>
 reboot
<span style="">&#125;</span></pre></div></div>

<p>You have to change the root UUID to the one the Ubuntu installer will assign to your hard disk after installation. Just check the fstab file when the installation has finished. The first entry boots Linux with a splash image enabled. The second one is for the single user mode in the case something went wrong. Please note the <strong>video=efifb</strong> option, which enables the graphical mode in the boot phase.</p>
<h2>Installing Ubuntu</h2>
<p>Most of the installation process is straight forward and doesn't need any special attention. Download the version of your choice from one of the mirrors, burn it on CD and start the installation. You can select the CD as boot medium by pressing <code class="key">Alt</code> when your Mac starts. When the installer ask for the partition scheme, you have to switch to "manual choice". Select the DISK1S5 (your what it is in your case) as the root / partition and change the filesystem type to ext3. Also remember the path to the system partition, cause you will need it later again. Select the swap partition and change its type to swap. Proceed with the rest of the installation until the last dialog. There select "advanced settings" and change the boot loader target from <code class="path">hd0</code> to <code class="path">/dev/sdXX</code>, where you replace XX to the path you used previously in the partition tool.</p>
<p>If all went right you should be able to select the LINUXBOOT partition by pressing <code class="key">Alt</code> when your Mac starts. After that Grub2 should shows up, you will be able to boot into your freshly installed Ubuntu.</p>
<h2>Conclusion</h2>
<p>In this post I showed how to easily add the possibility to boot Linux on your MacBook Pro. With the external USB hard disk solution, no internal valuable space is wasted. Of course the speed isn't the same as if the OS would be installed on the internal drive, but for testing software on different operation systems this is satisfactory. To increase the speed a little bit more, an external FireWire hard disk could be used.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.64k-tec.de/2010/04/installing-linux-on-a-usb-hard-disk-for-the-macbook-pro/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

