Chroot into QEmu

Based on

  • Great Debian "m68k Qemu sbuild" documentation by John Paul Adrian Glaubitz
  • m68k Qemu version by Laurent Vivier
  • Qemu by Fabrice Bellard and the Qemu team
  • Linux Kernel by Linus Torvals and the Linux development community

Qemu emulation modes

  • Qemu can run in two modes
    • full system emulation
    • user mode emulation

Qemu system emulation

  • emulates a full computer system
    • including CPU, Memory, storage and other peripherals
    • runs native operating systems
      • the operating systems do not need to be changed to run inside Qemu

Qemu system emulation

qemu-m68k-01.png

Qemu system emulation

qemu-m68k-02.png

Qemu user mode emulation

  • available for Linux and FreeBSD
  • emulates a Linux/FreeBSD user mode environment
    • traps system calls and forwards them to the native kernel
    • user mode "sees" an emulated Linux/FreeBSD system
    • runs unmodified Linux/FreeBSD binaries
    • similar to the older user mode Linux, but runs binaries from an different architecture

Qemu user mode emulation

qemu-m68k-03.png

Qemu user mode emulation

qemu-m68k-04.png

Benefits of Qemu user mode

  • seamless integration into host system (copy 'n paste, scripting, data exchange)
  • better CPU and memory utilization
    • every process runs inside it's own Qemu process
    • Linux scheduler can distribute the Qemu processes onto different CPU cores
  • less overhead (no hardware emulation, no emulated kernel)

setting up Qemu user mode

  • Instructions for Debian
    • other Linux distros should work as well
    • also works in a Linux container (My setup: Fedora amd64 -> Debian amd64 (systemd-nspawn) -> qemu m68k)
  • Instructions also available at http://m68k.info/qemu-user-mode

Install dependencies

apt install libaio-dev libbluetooth-dev libbrlapi-dev \
    libcapstone-dev libcurl4-gnutls-dev libfdt-dev libgbm-dev \
    libgfchangelog0 libibumad-dev libibverbs-dev libiscsi-dev \
    libnfs-dev  libnl-3-dev libnl-route-3-dev libnuma-dev \
    librados-dev librbd-dev librdmacm-dev libsasl2-dev \
    libspice-protocol-dev libspice-server-dev libssh-dev \
    libusb-1.0-0-dev libusbredirparser-dev libvdeplug-dev \
    libvte-2.91-dev libxen-dev xfslibs-dev git build-essential

Clone and build qemu (m68k)

mkdir -p ~/src
cd ~/src
git clone https://github.com/vivier/qemu-m68k
cd qemu-m68k/
./configure --target-list=m68k-linux-user --static --disable-glusterfs
make

Install Qemu and the binfmt rules

apt -y install qemu-user-static
strip ./build/qemu-m68k
cp -av build/qemu-m68k /usr/bin/qemu-m68k-static

Install Debian m68k Linux into a directory

  • the full path of the installed Debian m68k will be /srv/chroot/qemu-m68k
apt -y install debootstrap debian-ports-archive-keyring
mkdir /srv/chroot
cd /srv/chroot
debootstrap --no-check-gpg --variant=buildd --foreign \
   --arch=m68k unstable qemu-m68k http://ftp.ports.debian.org/debian-ports/

Copy the Qemu user mode binary into the m68k chroot directory

  • yes, this is the amd64 binary inside the m68k Linux
    • the amd64 Linux kernel need to find it there, depending on the bindfmt configuration
    • check the binfmt configuration of your (native) Linux kernel, esp. the path to the qemu-m68k or qemu-m68k-static binary, with cat /proc/sys/fs/binfmt_misc/qemu-m68k

Copy the Qemu user mode binary into the m68k chroot directory

# cp -av /usr/bin/qemu-m68k-static qemu-m68k/usr/bin/

# cp -av /usr/bin/qemu-m68k-static qemu-m68k/usr/bin/qemu-m68k

enter the chroot and finish Debian installation

  • the moment of truth!
chroot /srv/chroot/qemu-m68k/

uname -a
# prints: Linux debian-m68k-dev 4.19.0-12-amd64 #1 SMP 
#         Debian 4.19.152-1 (2020-10-18) m68k GNU/Linux

./debootstrap/debootstrap --second-stage

Prepare for Linux Container

  • set a password for the root account
    • this is required to be able to login into the m68k Linux when booting the machine with systemd-nspawn
  • install systemd (or any Init-System you prefer)
  • exit the chroot
apt install systemd
passwd root
exit

Use the m68k system

chroot

  • with chroot
mount -t proc proc     /srv/chroot/qemu-m68k/proc/
mount -t sysfs sys     /srv/chroot/qemu-m68k/sys/
mount -o bind /dev     /srv/chroot/qemu-m68k/dev/
mount -o bind /dev/pts /srv/chroot/qemu-m68k/dev/pts
chroot /srv/chroot/qemu-m68k

systemd-nspawn

  • Enter with systemd-nspawn
systemd-nspawn -D /srv/chroot/qemu-m68k

systemd-nspawn boot

  • Boot the system with systemd-nspawn
systemd-nspawn -bD /srv/chroot/qemu-m68k

Ready

  • now use the m68k Linux system like a normal Linux installation
    • install packages
    • develop software
    • play games
    • enjoy life and have fun ;)

References and Links