Chroot into QEmu
Slides version
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 system emulation
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 user mode emulation
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)
- were building the Qemu m68k user mode from Laurent Vivier github repo https://github.com/vivier/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 theqemu-m68k
orqemu-m68k-static
binary, withcat /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
- this is required to be able to login into the m68k Linux when
booting the machine with
- 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
- Transparently running binaries from any architecture in Linux with QEMU and binfmt_misc https://ownyourbits.com/2018/06/13/transparently-running-binaries-from-any-architecture-in-linux-with-qemu-and-binfmt_misc/
- Setting up Debian/m68k on qemu-system-m68k https://wiki.debian.org/M68k/QemuSystemM68k
- Setting up sbuild with QEMU for m68k https://wiki.debian.org/M68k/sbuildQEMU
- QEmu User Mode Emulation https://wiki.debian.org/QemuUserEmulation
- Debian Cross-Bootstrap https://wiki.debian.org/EmDebian/CrossDebootstrap
- binfmt.d(5) — Linux manual page https://www.man7.org/linux/man-pages/man5/binfmt.d.5.html
- Kernel Support for miscellaneous Binary Formats (binfmt_misc) https://www.kernel.org/doc/html/latest/admin-guide/binfmt-misc.html
- QEMU User Mode Emulation User’s Guide https://qemu.readthedocs.io/en/latest/user/
- How to build and use QEMU User Mode on FreeBSD https://wiki.freebsd.org/QemuUserModeHowTo
- Video: QEMU user mode on FreeBSD by Sean Bruno https://invidious.13ad.de/watch?v=2J9Lz3pgnbA