pages tagged schrootijc's bloghttp://www.hellion.org.uk/blog/tags/schroot/ijc's blogikiwiki2012-12-17T13:46:54ZForeign Chroots with schroot and qemuhttp://www.hellion.org.uk/blog/posts/foreign-chroots-with-schroot-and-qemu/2012-12-17T13:46:54Z2012-12-17T13:46:54Z
<p>Since I've been working on <a href="http://packages.debian.org/qcontrol">qcontrol</a> I've had need of a way
to build packages in an ARM environment. Since all my native ARM
hardware is "production" (at least so far as something can be
production in a home environment) I prefer to avoid installing
development tools on them. When I started looking into this all my ARM
systems ran from relatively small flash devices which also ruled out
the possibility of a development chroot. After search the web a bit I
discovered that it is possible to use <a href="http://packages.debian.org/qemu%2Duser%2Dstatic">qemu-user-static</a>
together with [[!binfmt-support ]] to create cross architecture chroots
which can be entered mostly transparently. It is also possible to
combine this with <a href="http://packages.debian.org/schroot">schroot</a> and <a href="http://packages.debian.org/sbuild">sbuild</a> which
makes the day to day use pretty much the same as a native or biarch
chroot.</p>
<p>Since I've now had to rediscover how to do this twice (once for Sid
and then again when I realized I also needed a Wheezy environment) I
thought I should write it down for the benefit of my future self.</p>
<p>First things first lets install the various packages which we need. As
well as <a href="http://packages.debian.org/debootstrap">debootstrap</a> to create the chroot we need
<code>qemu-user-static</code> and <code>binfmt-support</code> to allow us to run ARM
binaries:</p>
<pre><code>$ sudo apt-get install debootstrap qemu-user-static binfmt-support
</code></pre>
<p>I'm using <code>schroot</code>'s lvm-snapshot mode so we need an LVM volume,
formatted with a filesystem and mounted in a temporary location:</p>
<pre><code>$ sudo lvcreate -L8G -n sbuild-wheezy-armel /dev/VG
$ sudo mkfs.ext3 /dev/VG/sbuild-wheezy-armel
$ sudo mount /dev/VG/sbuild-wheezy-armel /mnt/
</code></pre>
<p>Next we need to create the chroot. For this we use <code>debootstrap</code> in
foreign mode which does a 2 stage setup, firstly by using the
<code>--foreign</code> option:</p>
<pre><code>$ sudo debootstrap --variant=buildd --include=fakeroot,build-essential \
--arch=armel --foreign \
wheezy /mnt http://<DEBIAN-MIRROR>/debian/
</code></pre>
<p>Now we (temporarily) copy the <code>qemu-arm-static</code> binary into the chroot
(at the place where <code>binfmt-support</code> expects it), change into the
chroot and run <code>debootstrap</code>'s second stage to finish the basic chroot
setup:</p>
<pre><code>$ sudo cp /usr/bin/qemu-arm-static /mnt/usr/bin/
$ sudo chroot /mnt
(chroot)# ./debootstrap/debootstrap --second-stage
(chroot)# exit
</code></pre>
<p>Now that we have a basic chroot in place it needs a little bit of
tailoring. I've found the easiest way to do this is to use
<code>sbuild-createchroot</code> and then tweak the result:</p>
<pre><code>$ sudo sbuild-createchroot --arch=armel --foreign --setup-only \
wheezy /mnt http://<DEBIAN-MIRROR>/debian/
</code></pre>
<p>If we were building a native chroot then we could have skipped the
<code>debootstrap</code> stuff and let <code>sbuild-createchroot</code> handle
things. Unfortunately this doesn't seem to play nicely with
<code>--foreign</code>.</p>
<p>Now we can remove the <code>qemu-arm-static</code> binary from the
chroot. <code>schroot</code> will automatically bind mount the version from the
host into the chroot which ensures it remains up to date. We can also
unmount the chroot since we will let <code>schroot</code> manage that from here
on:</p>
<pre><code>$ sudo rm /mnt/usr/bin/qemu-arm-static
$ sudo umount /mnt
</code></pre>
<p>Next we need to tweak the resulting <code>schroot</code>/<code>sbuild</code> configuration
since <code>sbuild-createchroot</code> assumes the use of schroot's directory
mode. First remove the symlink to /mnt which it created:</p>
<pre><code>$ sudo rm /etc/sbuild/chroot/wheezy-armel-sbuild
</code></pre>
<p>Lastly we need to edit <code>/etc/schroot/chroot.d/wheezy-armel-sbuild-<SUFFIX></code>
(where is randomly generated) and reconfigure it to use
lvm-snapshot mode, by apply the following changes:</p>
<pre><code> [wheezy-armel-sbuild]
-type=directory
-directory=/mnt
+type=lvm-snapshot
+device=/dev/VG/sbuild-wheezy-armel
+lvm-snapshot-options=--size 2G
description=Debian wheezy/armel autobuilder
groups=root,sbuild
root-groups=root,sbuild
profile=sbuild
</code></pre>
<p>That's it, we now have a chroot which we can trivially invoke via
<code>sbuild</code> to build armel packages:</p>
<pre><code>$ sbuild --arch=armel --dist=wheezy <... other options...>
</code></pre>
<p>We can also use <code>sbuild-update</code> to keep the chroot up to date:</p>
<pre><code>$ sudo sbuild-update -udcar wheezy-armel
</code></pre>
<p>You can also use <code>schroot</code> to login to the chroot directly, although
for that case you'll likely want to create a different chroot using
<code>schroot</code>'s <code>directory</code> or <code>block-device</code> mode instead <code>lvm-snapshot</code>
and drop some of the sbuild specific setup. I expect I'll blog about
this configuration in some more detail at some point in the future
since I'll want to document it to give a more convenient build
environment for <a href="http://wiki.xen.org/wiki/Xen_ARMv7_with_Virtualization_Extensions">Xen on
ARM</a>
than the <a href="https://plus.google.com/106815887686504011057/posts/Kgdakxs5cFt">build
farm</a>
which we have at work.</p>