Setting up sbuild with QEMU for m68k

NOTE: As upstream QEMU currently does not provide full m68k CPU support, you need to use a patched version of QEMU which has full m68k CPU support developed by Laurent Vivier. Laurent's version can be found on github here.

Download, compile and install qemu-m68k from source

First of all, install the necessary build dependencies for qemu on Debian:

root@z6:~> apt-get update && apt-get build-dep qemu && apt-get install git binfmt-support

Then clone qemu-m68k into a directory, for example in your home:

glaubitz@z6:/tmp> git clone
Cloning into 'qemu-m68k'...
X11 forwarding request failed on channel 0
remote: Counting objects: 248386, done.
remote: Compressing objects: 100% (169/169), done.
remote: Total 248386 (delta 2970), reused 2935 (delta 2935), pack-reused 245282
Receiving objects: 100% (248386/248386), 78.09 MiB | 16.76 MiB/s, done.
Resolving deltas: 100% (201197/201197), done.
Checking connectivity... done.

As of November 29th, 2016, we need to use the m68k-dev branch, the default branch is too old:

glaubitz@z6:/tmp> cd qemu-m68k/
glaubitz@z6:..tmp/qemu-m68k> git checkout m68k-dev
Branch 680x0-master-dev set up to track remote branch m68k-dev from origin.
Switched to a new branch 'm68k-dev'

Note: The list of available branches can be shown with git branch -a.

Now configure qemu-m68k to compile the m68k-linux-user target only (we only need this target and other targets actually failed to compile when I tried it):

glaubitz@z6:/tmp/qemu-m68k> ./configure --target-list=m68k-linux-user --static --disable-glusterfs
Install prefix    /usr/local
BIOS directory    /usr/local/share/qemu
binary directory  /usr/local/bin
library directory /usr/local/lib
module directory  /usr/local/lib/qemu
libexec directory /usr/local/libexec
include directory /usr/local/include
config directory  /usr/local/etc
local state directory   /usr/local/var
Manual directory  /usr/local/share/man
ELF interp prefix /usr/gnemul/qemu-%M
Source path       /tmp/qemu-m68k
C compiler        cc
Host C compiler   cc
C++ compiler      c++
Objective-C compiler cc
ARFLAGS           rv
CFLAGS            -O2 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -pthread -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -g 
QEMU_CFLAGS       -I/usr/include/pixman-1  -Werror -DHAS_LIBSSH2_SFTP_FSYNC -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -Wstrict-prototypes -Wredundant-decls -Wall -Wundef -Wwrite-strings -Wmissing-prototypes -fno-strict-aliasing -fno-common  -Wendif-labels -Wmissing-include-dirs -Wempty-body -Wnested-externs -Wformat-security -Wformat-y2k -Winit-self -Wignored-qualifiers -Wold-style-declaration -Wold-style-definition -Wtype-limits -fstack-protector-strong   -I/usr/include/libpng12 -I/usr/include/libusb-1.0 
LDFLAGS           -Wl,--warn-common -m64 -static -g 
make              make
install           install
python            python -B
smbd              /usr/sbin/smbd
module support    no
host CPU          x86_64
host big endian   no
target list       m68k-linux-user
tcg debug enabled no
emulop enabled    no
gprof enabled     no
sparse enabled    no
strip binaries    yes
profiler          no
static build      yes
pixman            system
SDL support       no
GTK support       no
VTE support       no
curses support    yes
curl support      no
mingw32 support   no
Audio drivers     oss
Block whitelist (rw) 
Block whitelist (ro) 
VirtFS support    
VNC support       yes
VNC TLS support   no
VNC SASL support  no
VNC JPEG support  yes
VNC PNG support   yes
VNC WS support    no
xen support       no
brlapi support    no
bluez  support    yes
Documentation     yes
GUEST_BASE        yes
PIE               no
vde support       yes
netmap support    no
Linux AIO support yes
ATTR/XATTR support yes
Install blobs     yes
KVM support       yes
RDMA support      no
TCG interpreter   no
fdt support       yes
preadv support    yes
fdatasync         yes
madvise           yes
posix_madvise     yes
sigev_thread_id   yes
uuid support      yes
libcap-ng support yes
vhost-net support yes
vhost-scsi support yes
Trace backends    nop
spice support     no
rbd support       no
xfsctl support    yes
nss used          no
libusb            yes
usb net redir     yes
OpenGL support    no
libiscsi support  yes
libnfs support    no
build guest agent yes
QGA VSS support   no
seccomp support   yes
coroutine backend ucontext
coroutine pool    yes
GlusterFS support no
Archipelago support no
gcov              gcov
gcov enabled      no
TPM support       yes
libssh2 support   yes
TPM passthrough   yes
QOM debugging     yes
vhdx              yes
Quorum            no
lzo support       no
snappy support    no
bzip2 support     no
NUMA host support yes

Now run make to compile qemu-m68k:

glaubitz@z6:/tmp/qemu-m68k> make
  GEN   m68k-linux-user/config-devices.mak.tmp
  GEN  m68k-linux-user/config-devices.mak
  GEN   config-all-devices.mak
  GEN   config-host.h
  GEN   qemu-options.def
  GEN   qmp-commands.h
  GEN   qapi-types.h
  GEN   qapi-visit.h
  GEN   qapi-event.h
  GEN   trace/generated-events.h
  GEN   trace/generated-tracers.h
  GEN   trace/generated-tcg-tracers.h
  GEN   trace/generated-helpers-wrappers.h
  GEN   trace/generated-helpers.h
  GEN   tests/test-qapi-types.h
  CC    m68k-linux-user/target-m68k/op_helper.o
  CC    m68k-linux-user/target-m68k/helper.o
  CC    m68k-linux-user/target-m68k/cpu.o
  CC    m68k-linux-user/target-m68k/gdbstub.o
  GEN   trace/generated-helpers.c
  CC    m68k-linux-user/trace/generated-helpers.o
  LINK  m68k-linux-user/qemu-m68k
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libglib-2.0.a(libglib_2_0_la-gutils.o): In function `g_get_user_database_entry':
(.text+0x249): warning: Using 'getpwuid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libglib-2.0.a(libglib_2_0_la-gutils.o): In function `g_get_user_database_entry':
(.text+0xcf): warning: Using 'getpwnam_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libglib-2.0.a(libglib_2_0_la-gutils.o): In function `g_get_user_database_entry':
(.text+0x106): warning: Using 'getpwuid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking

Now run strip to strip out the debug symbols which will size down the binary:

glaubitz@z6:/tmp/qemu-m68k> cd m68k-linux-user/
glaubitz@z6:..qemu-m68k/m68k-linux-user> ls -lh qemu-m68k 
-rwxr-xr-x 1 glaubitz glaubitz 7.4M Nov 19 13:56 qemu-m68k
glaubitz@z6:..qemu-m68k/m68k-linux-user> strip qemu-m68k 
glaubitz@z6:..qemu-m68k/m68k-linux-user> ls -lh qemu-m68k 
-rwxr-xr-x 1 glaubitz glaubitz 2.5M Nov 19 13:56 qemu-m68k

Install qemu-user-static as it sets the binformat handlers correctly, then overwrite the supplied qemu-m68k-static that you just compiled:

root@z6:~> apt-get install qemu-user-static
root@z6:~> cp -av /tmp/qemu-m68k/m68k-linux-user/qemu-m68k /usr/bin/qemu-m68k-static

Set up a chroot environment for m68k

First of all, install all the necessary tools:

root@z6:~> apt install sbuild schroot debootstrap debian-ports-archive-keyring

Then run debootstrap with --foreign to download an m68k but not to configure it yet:

root@z6:~> cd /srv/chroots/
root@z6:/srv/chroots> debootstrap --variant=buildd --foreign --arch=m68k unstable sid-m68k-sbuild

Now copy the qemu-m68k static binary created above into the chroot using the target name qemu-m68k-static (this is very important as otherwise the dynamic loader cannot find the qemu binary):

root@z6:/srv/chroots> cp -av /tmp/qemu-m68k/m68k-linux-user/qemu-m68k sid-m68k-sbuild/usr/bin/qemu-m68k-static 
‘/tmp/qemu-m68k/m68k-linux-user/qemu-m68k’ -> ‘sid-m68k-sbuild/usr/bin/qemu-m68k-static’

You can now chroot into the m68k chroot and run ./debootstrap/debootstrap --second-stage to fully configure your new sbuild chroot:

root@z6:/srv/chroots/sid-m68k-sbuild> chroot .
bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
I have no name!@z6:/# ./debootstrap/debootstrap --second-stage
I: Keyring file not available at /usr/share/keyrings/debian-archive-keyring.gpg; switching to https mirror
I: Installing core packages...
I: Unpacking required packages...
I: Unpacking libacl1:m68k...
I: Unpacking adduser...
I: Unpacking libapparmor1:m68k...
I: Unpacking libattr1:m68k...
I: Unpacking libaudit-common...
I: Unpacking libaudit1:m68k...
I: Unpacking base-files...
I: Unpacking base-passwd...
I: Unpacking bash...
I: Unpacking libbz2-1.0:m68k...
I: Unpacking libdebconfclient0:m68k...
I: Unpacking coreutils...
I: Unpacking libcryptsetup4:m68k...
I: Unpacking dash...
I: Unpacking libdb5.3:m68k...
I: Configuring dpkg-dev...
I: Configuring libgcc-5-m68k...
I: Configuring libapt-pkg4.16:m68k...
I: Configuring apt...
I: Configuring gcc-5...
I: Configuring libstdc++-5-dev:m68k...
I: Configuring gcc...
I: Configuring g++-5...
I: Configuring g++...
I: Configuring build-essential...
I: Configuring libc-bin...
I: Base system installed successfully.
I have no name!@z6:/# 

Exit the chroot and add a usabable /etc/apt/sources.list for m68k:

I have no name!@z6:/# exit
root@z6:/srv/chroots> cat sid-m68k-sbuild/etc/apt/sources.list
# binary default
deb unstable main
deb unstable main
deb unreleased main

# source
deb-src unstable main
deb-src buildd-unstable main


Re-enter the chroot and add the signing key for Debian ports:

(sid-m68k-sbuild)root@z6:/# apt install debian-ports-archive-keyring

Exit the chroot again and configure schroot on your host machine for m68k:

(sid-m68k-sbuild)root@z6:/# exit
root@z6:/srv/chroots> cat /etc/schroot/chroot.d/sid-m68k-sbuild
description=Debian sid chroot for m68k

Cross-building packages using sbuild for m68k

glaubitz@z6:~> sbuild --source --arch-all -d sid --arch=m68k xterm_318-2.dsc
sbuild (Debian sbuild) 0.66.0 (04 Oct 2015) on

| xterm 318-2.dsc (m68k)                                     19 Nov 2015 14:26 |

Package: xterm
Version: 318-2.dsc
Source Version: 318-2.dsc
Distribution: sid
Machine Architecture: amd64
Host Architecture: m68k
Build Architecture: m68k