Mastering Virtualization: How to Run a Raspberry Pi Environment on your Computer

TJ. Podobnik, @dorkamotorka
4 min readApr 10, 2022

--

As a Raspberry Pi developer, you may have found yourself in the tedious process of testing and making changes to your disk image. The process of updating system packages, modifying source code, and flashing the image to an SD card can be time-consuming and frustrating. But what if there was a way to streamline this process and make it more efficient?

In this post, we will explore a method for accessing and modifying your Raspberry Pi’s disk image locally on your machine without the need to spin up the device. This can save you time and hassle, allowing you to focus on the important task of developing your software. We will dive into the details of how this can be achieved and the benefits it provides for your development process.

Additionally, we will also discuss how this method can be applied to other disk images, as long as they contain only two partitions — a boot and a root file system partition. This guide is suitable for both seasoned Raspberry Pi developers and those just starting out, looking to simplify their development process and save time. With the knowledge gained from this post, you’ll be able to work more efficiently and effectively with your Raspberry Pi disk images.

QEMU + chroot

“Under the hood” the script is using chroot and QEMU tools. In the next two sections, I will give a brief introduction to both, but in general, you can get away without knowing how to use them since the script I wrote already automate the steps for you.

Chroot overview

The chroot Linux tool is a powerful command that allows developers to change the root file system for the current running process and its children. This operation, known as chrooting, enables you to set an arbitrary directory as the new root file system, effectively isolating the running process from the rest of the system.

By using chroot, you can create a safe and secure environment for testing and experimenting with your code, without the risk of affecting other parts of the system. This makes it a valuable tool for developers who need to test their code in a simulated environment before deployment.

Whether you’re working on a critical project or simply want to improve your development workflow, chroot is a must-have tool for any Linux developer. If you scroll through the internet you will find, that there are many different ways to chroot (commands):

  • chrootthis command may be used directly as root, but normal users cannot use this command
  • schrootallows access to chroots for normal users using the same mechanism, but with permissions checking and allowing the additional automated setup of the chroot environments, such as mounting additional filesystems and other configuration tasks
  • dchrootan earlier version and is being deprecated in favor of schroot.
  • arch-chrootshell script that prepares a new “root directory” with the necessary system mounts (/proc, /sys, /dev, etc.) and files (/etc/resolv.conf), then does a chroot into it. Once you exit from the environment system mounts (mounted before) are automatically unmounted.

chroot command should be available by default, while schroot and arch-chroot can be installed with:

apt install arch-install-scripts schroot

So far we’ve looked at, how we can pretend that a certain directory can act as a root file system on its own, but since we will be working with a Raspberry Pi file system that usually runs on a totally different CPU architecture than our PC, we still need a way for our CPU architecture to understand such system calls on our local machine. This is where QEMUQuick EMUlator comes into play.

QEMU overview

QEMU is a powerful processor emulator that allows developers to run virtual machines on their computers. As a type 2 hypervisor, it acts as a software layer between the hardware and the virtual machine, providing an efficient and stable environment for virtualization. QEMU has two operating modes:

  • User mode emulation: QEMU can launch Linux processes compiled for one CPU on another CPU, translating syscalls on the fly. Consider it as a Lite version of VM.
  • Full system emulation: QEMU emulates a full system (virtual machine), including a processor and various peripherals such as a disk, ethernet controller, etc.

QEMU is also widely used for embedded development, IoT, and embedded system testing, and can be integrated with other tools to create a comprehensive development environment.

In the following script, I am using user-mode emulation since it is a friendly and fast solution when it works, but is not a perfect abstraction. This mode does not cover all syscalls, therefore as a user, you might encounter some syscall errors, which in general you can disregard, but if it affects your operation to an extent that it does not finish the job, you should consider using full system emulation mode.

Run the Script

Enough about the theory, let’s see the script and how you can use them. Do note that this script mounts a disk image for aarch-64 architecture by default, but you can also enable it for armhf architecture — read the comments in the script.

You can run this script, by typing:

sudo ./chrooting.sh <disk-image>

It’s as simple as that. Let me know in the comments if you have any more questions, I will be happy to help.

Thanks for reading! :) If you enjoyed this article, hit that clap button below 👏

Would mean a lot to me and it helps other people see the story. Say Hello on Instagram | Linkedin | Twitter

Do you want to start using Medium? Use this referral link 🔗

If you liked my post you can buy me a Hot dog 🌭

Follow me for more related content, cheers!

--

--

TJ. Podobnik, @dorkamotorka
TJ. Podobnik, @dorkamotorka

No responses yet