Running Raspbian in image on Ubuntu desktop


While the title says Raspbian on Ubuntu, the steps will be nearly the same for any ARM linux distribution on any AMD64 Linux desktop. For me, and I suspect most people, what combination is likely to equal Raspbian on Ubuntu.

Download and extract your Raspbian image. In this case I will be using Raspbian Lite and this is the filename of the image: 2019-04-08-raspbian-stretch.img

First, setup: sudo apt install qemu binfmt-support qemu-user-static systemd-container

After the setup, run the following steps every time you want to run a Raspbian image on Ubuntu.

sudo kpartx -v -a ./2019-04-08-raspbian-stretch.img

That will print something like:

add map loop11p1 (253:0): 0 89698 linear 7:11 8192
add map loop11p2 (253:1): 0 8392704 linear 7:11 98304

The first line is the raspbian boot partition, the second will be the rootfs partition.

udisksctl mount -b /dev/mapper/loop11p2

After the prior command, a rootfs mount should appear in /media/<your_username>/. You can copy files into either and they will be placed into the image file you mounted.

sudo systemd-nspawn -D /media/<your_username>/rootfs/

Ctrl-D to exit.

udisksctl unmount -b /dev/mapper/loop11p2

sudo kpartx -d ./2019-04-08-raspbian-stretch.img

Note, a more advanced use might be to copy some sort of initialization script into the image after mounting the image and before running systemd-nspawn. Then, invoke that command something like: sudo systemd-nspawn -D /media/<your_username>/rootfs/ /bin/bash /home/pi/your_script.sh


Leave a Reply