Building Packages in Raspbian with sbuild

Posted by Nathan Osman on November 21, 2015

Getting sbuild up and running in Raspbian can be a tricky process. This article will describe the process from start to finish.

  1. Assuming you have downloaded Raspbian Jessie (I haven’t tested these instructions in Wheezy), flash the image to an SD card using dd:

     dd bs=4M if=2015-09-24-raspbian-jessie.img of=<device>
    

    …where <device> is the device node for your SD card (such as /dev/sdc).

  2. Safely remove the SD card and insert it into the Pi. This article assumes your Pi is connected to the Internet, so you will need to either connect an ethernet cable to the LAN port or attach a wireless adapter to one of the USB ports.

  3. Boot the Pi and connect to a wireless network if necessary. The remaining steps assume a working Internet connection.

  4. Open a terminal and launch the raspi-config script using the following command:

     sudo raspi-config
    
  5. Make the following adjustments:
    • “Expand the Filesystem” - allows you to take advantage of the full capacity of your SD card
    • “Boot Options” -> “B1 Console” - prevents X from starting
    • “Advanced Options” -> “Memory Split” -> “16” - reduces the amount of memory set aside for the GPU, which won’t be needed
    • “Advanced Options” -> “SSH” -> “Enable” - allows you to connect remotely to the Pi
  6. If you want to get extra performance out of your Pi, you can also adjust “Overclock” to “Medium”. This does come with a risk - your Pi will generate more heat. I recommend purchasing a small copper heatsink if you choose to go this route.

  7. Once these adjustments have been made, you will be prompted to restart the Pi, which you must now do.

  8. Use ifconfig to obtain your Pi’s IP address:

     ifconfig <interface> | grep 'inet addr'
    

    …where <interface> is the name of the interface connecting your Pi to the Internet (such as eth0 or wlan0).

  9. Open a terminal on another machine and verify that you can connect to the Pi over SSH:

     ssh pi@<ip address>
    

    (The default password is “raspberry”.)

  10. At this point, you can disconnect keyboards, mice, and monitors from the Pi since everything done from this point on will be over SSH.

  11. Using the SSH connection, run the following commands:

     sudo apt-get update
     sudo apt-get upgrade -y
     sudo apt-get install -y screen sbuild ubuntu-dev-tools
     sudo sbuild-adduser pi
     sudo sbuild-update --keygen
    

    The last command may take a few minutes to complete.

  12. The secret key used for signing packages is assigned incorrect permissions. You can fix it with this command:

     sudo chmod 640 /var/lib/sbuild/apt-keys/sbuild-key.sec
    
  13. Exit from the SSH connection and reconnect.

  14. Run screen to create a new screen session. This step is optional but it will allow you to disconnect from the screen session while sbuild runs.

     screen
    
  15. Create a chroot for Raspbian Jessie:

     mk-sbuild --skip-updates --skip-proposed jessie
    
  16. This process will take a lot of time. If you used screen, you can press Ctrl+A and then D to disconnect from the screen session. You can resume later with:

     screen -r
    
  17. For some unknown reason, sbuild will try to use AuFS, which the Raspbian kernel does not support out of the box. You will need to open /etc/schroot/chroot.d/sbuild-jessie-armhf and change the following line:

     union-type=aufs
    

    …to…

     union-type=overlayfs
    
  18. Newer kernel versions have renamed the overlayfs module to overlay. You will need to do some quick patching in order to account for this. Open /etc/schroot/setup.d/10mount and change line 123:

     CHROOT_UNION_MOUNT_OPTIONS="lowerdir=${CHROOT_UNION_UNDERLAY_DIRECTORY},upperdir=${CHROOT_UNION_OVERLAY_DIRECTORY}"
    

    …to…

     mkdir -p ${CHROOT_UNION_OVERLAY_DIRECTORY}/upper ${CHROOT_UNION_OVERLAY_DIRECTORY}/work
     CHROOT_UNION_MOUNT_OPTIONS="lowerdir=${CHROOT_UNION_UNDERLAY_DIRECTORY},upperdir=${CHROOT_UNION_OVERLAY_DIRECTORY}/upper,workdir=${CHROOT_UNION_OVERLAY_DIRECTORY}/work"
     CHROOT_UNION_TYPE="overlay"
    
  19. Enable the overlay module by appending it to /etc/modules:

     echo "overlay" | sudo tee -a /etc/modules
    
  20. If you try to build a package at this point, you will receive an error:

     W: GPG error: http://security.debian.org jessie/updates InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 9D6D8F6BC857C906 NO_PUBKEY 8B48AD6246925553
    

    You will need to import the specified keys. Run the following command to enter the chroot:

     sudo sbuild-shell jessie
    

    Then run the following commands to import the keys:

     apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 9D6D8F6BC857C906
     apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 8B48AD6246925553
    

You now have a working sbuild chroot! To build a package, simply run:

sbuild -d jessie <name>.dsc