4

I would like to emulate the exact copy of my real RPi on another virtual machine. I noticed that the only working OS to build a virtual machine is the version RPdesktop. I need the specific OS version of my raspberry pi, is that possible? I tried with converting .img of my rpi on .ISO but the virtual machine doesn't found the OS. Any suggestion? Thanks

2
  • You'd need to look at hardware emulation. The RPi uses ARM based chips whereas a standard desktop/laptop typically uses x86 one wont run on the other unless you emulate the hardware. Try looking at something like QEMU that can do hardware emulation as well as software ( if I remember rightly.) And that should work for you.
    – rohtua
    Mar 20, 2020 at 13:26
  • Thanks @rohtua, you're right! I already tried with QEMU but the Raspbian stretch version did not loaded on QEMU emulator. Do you have any tutorial to suggest?
    – Leo94
    Mar 20, 2020 at 13:56

1 Answer 1

5

You can relatively easily do this using Docker:

1. Download and extract Raspbian

Go to the download page and download Raspbian Lite.
Once the file has finished downloading, unzip it:

cd Downloads/
unzip 2020-02-13-raspbian-buster-lite.zip 

2. Mount the image

First, create a loop device from the image file:

udisksctl loop-setup --file 2020-02-13-raspbian-buster-lite.img --read-only

It'll tell you what device it was mapped to. In my case, this was /dev/loop16.
Most distributions will now automatically mount the partitions on the device. For example, Ubuntu will mount them to /media/$USER/boot and /media/$USER/rootfs.

If your distribution doesn't do this automatically, mount the rootfs partition manually:

# 1. create a directory to mount the filesystem to
sudo mkdir /media/$USER/rootfs
# 2. show the partitions of the loop device we just created
lsblk -o name,label /dev/loop16 
# 3. use the name of the `rootfs` partition (loop16p2 in my case)
sudo mount -o ro /dev/loop16p2 /media/$USER/rootfs

3. Create a Docker image from Raspbian's root filesystem

sudo tar c -C /media/$USER/rootfs . | docker image import - raspbian-lite:buster

4. Allow your computer to emulate and run ARM binaries

docker run --rm --privileged docker/binfmt:820fdd95a9972a5308930a2bdfb8573dd4447ad3 

(See https://www.docker.com/blog/getting-started-with-docker-for-arm-on-linux for more details)

5. Create a Docker container from the Raspbian image

docker create -it --name raspbian_test raspbian-lite:buster bash

5. Run the Docker container

docker start -ai raspbian_test

To get out of the container, you can use the exit command.

Extra: running graphical applications

This is the same as with native docker containers.

docker create -it --name raspbian_test_graphical \
    -v /tmp/.X11-unix:/tmp/.X11-unix \
    -e DISPLAY=unix$DISPLAY \
    raspbian-lite:buster bash
xhost +local:root # tell X server to accept the connection
docker start -ai raspbian_test_graphical

Inside of the container, you can then verify that it works using:

sudo apt install -y x11-apps
xeyes

It should open a window with two eyes following your mouse pointer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.