.. title: Quick and easy bootstrap of Debian 8 (Jessie) for armhf
.. date: 2015-08-25 19:38
.. tags: linux, debian, arm
.. slug: debian-armhf-bootstrap
.. type: text

We all know the vast amount of software packages available for Debian
so when I could not quickly find a required package for a test on a
socfpga board, I wondered how much effort it would be to bootstrap a
`Debian Jessie <https://www.debian.org/releases/jessie/>`_ root
file system that can also be used over NFS.  As it turns out it took
barely half an hour and worked exceptionally well.

.. image:: /images/jessie_debian.png
   :width: 400
   :alt: Debian Jessie
   :align: center
      
.. TEASER_END

The key to bootstrap a Debian system is of course ``debootstrap`` and
getting this on a Ubuntu system is a no-brainer:

.. code-block:: console

  [dzu@harry tmp]$ sudo apt-get install debootstrap
  
As I remember that a boostrap usually involves doing some initial
steps in a ``chroot``, I wasn't sure how feasible it would be to do
this for a different target architecture.  With the help of ``qemu``
it turned out to be trivial, but let's take it one step after the
other.  Start out with downloading and installing the base system:

.. code-block:: console

  [dzu@harry tmp]$ sudo debootstrap --foreign --arch=armhf jessie jessie-armhf
  I: Keyring file not available at /usr/share/keyrings/debian-archive-keyring.gpg; switching to https mirror https://mirrors.kernel.org/debian
  I: Retrieving Release 
  I: Retrieving Packages 
  I: Validating Packages 
  I: Resolving dependencies of required packages...
  ...
  I: Extracting mount...
  I: Extracting util-linux...
  I: Extracting liblzma5...
  I: Extracting zlib1g...
  [dzu@harry tmp]$

Note the ``--foreign`` option that prevents debootstrap from running
the second stage as this needs to be run in a chroot and thus cannot
run without further magic on our Intel development machine.  Let's try
it anyway and see what happens:

.. code-block:: console

  [dzu@harry tmp]$ sudo chroot jessie-armhf/
  chroot: failed to run command ‘/bin/bash’: No such file or directory
  [dzu@harry tmp]$ file jessie-armhf/bin/bash 
  jessie-armhf/bin/bash: ELF 32-bit LSB  executable, ARM, EABI5 version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=1a8601b954c83a01d91298d0f2f8f61ca033ebdd, stripped
  [dzu@harry tmp]$

Thanks to the amazing `QEMU project
<http://wiki.qemu.org/Main_Page>`_, we can make this work anyway.
Just install the following package:

.. code-block:: console

  [dzu@harry tmp]$ sudo apt-get install qemu-user-static

There is a nice Debian wiki page on `QEMU user emulation
<https://wiki.debian.org/QemuUserEmulation>`_ explaining in more
detail on what is going on here, but effectively the package
installation has registered several emulators for non-Intel ELF file
formats.  It's easy to see this also for the arm case:

.. code-block:: console

  [dzu@harry tmp]$ sudo update-binfmts --display
  ...
  qemu-arm (enabled):
       package = qemu-user-static
          type = magic
        offset = 0
         magic = \x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00
          mask = \xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff
   interpreter = /usr/bin/qemu-arm-static
      detector = 
  ...
  [dzu@harry tmp]$

We want to use this inside the chroot, so we have to copy it there.
As the second stage bootstrap also needs network access, we also copy
the DNS setup from the host system:

.. code-block:: console

  [dzu@harry tmp]$ sudo cp /usr/bin/qemu-arm-static jessie-armhf/usr/bin/
  [dzu@harry tmp]$ sudo cp /etc/resolv.conf jessie-armhf/etc/
  [dzu@harry tmp]$ 

That's all there is to it, so now we can enter the (emulated) chroot
and do the second stage bootstrap.  Don't forget to define a password
for root - the example clears it:

.. code-block:: console

  [dzu@harry tmp]$ sudo chroot jessie-armhf/
  I have no name!@harry:/# /debootstrap/debootstrap --second-stage
  I: Keyring file not available at /usr/share/keyrings/debian-archive-keyring.gpg; switching to https mirror https://mirrors.kernel.org/debian
  I: Installing core packages...
  ...
  I: Configuring systemd...
  I: Configuring ca-certificates...
  I: Base system installed successfully.
  Ihavenoname!@harry:/# passwd -d root
  passwd: password expiry information changed.
  Ihavenoname!@harry:/#

That's it.  Export the newly-created directory via NFS and boot into
it with a kernel supporting systemd (See Kernel Config Options in the
`systemd README
<http://cgit.freedesktop.org/systemd/systemd/tree/README>`_):

.. code-block:: console

  ...
  [  OK  ] Started Update UTMP about System Runlevel Changes.
  
  Debian GNU/Linux 8 harry ttyS0
  
  harry login: root
  Linux harry 4.0.0-00185-gec403a3-dirty #26 SMP Fri Aug 21 10:01:59 CEST 2015 armv7l
  
  The programs included with the Debian GNU/Linux system are free software;
  the exact distribution terms for each program are described in the
  individual files in /usr/share/doc/*/copyright.
  
  Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
  permitted by applicable law.
  root@harry:~# 

Now you can tweak the system to your liking.  Setting host name would
probably be a first step.  Next point ``/etc/apt/sources.list`` to a
repo near you and update the system, for example like this:

.. code-block:: console

  root@harry:~# cat << EOT > /etc/apt/sources.list
  deb http://ftp.de.debian.org/debian jessie main contrib non-free
  deb http://ftp.de.debian.org/debian jessie-updates main contrib non-free
  deb http://security.debian.org/debian-security jessie/updates main contrib non-free
  EOT
  root@harry:~# apt-get update  && apt-get dist-upgrade
  Get:1 http://ftp.de.debian.org jessie InRelease [134 kB]
  Get:2 http://security.debian.org jessie/updates InRelease [63.1 kB]
  ...
