Emulating The Raspbian Using QEMU

QEMU is a generic and open source machine emulator and virtualizer, while using it as a machine emulator it can run OSes and programs.QEMU is a hosted virtual machine monitor: It emulates CPUs through dynamic binary translation and provides a set of device models, enabling it to run a variety of unmodified guest operating systems. It also can be used together with KVM in order to run virtual machines at near-native speed. Moreover, QEMU can also be used purely for CPU emulation for user-level processes, allowing applications compiled for one architecture to be run on another.

This tool was written by Fabrice Bellard and as we said it is free software and mainly licensed under GNU General Public License (GPL). Various parts are released under BSD license, GNU Lesser General Public License (LGPL) or other GPL-compatible licenses.

Regarding Raspbian is a free operating system based on Debian optimized for the Raspberry Pi hardware. An operating system is the set of basic programs and utilities that make your Raspberry Pi run. However, Raspbian provides more than a pure OS: it comes with over 35,000 packages, pre-compiled software bundled in a nice format for easy installation on your Raspberry.

In this article we will explain for you how Rasbian (based on Debian) is emulating under QEMU.

Prerequisites

As prerequisites, we assume that QEMU is installed on your host system. And we will verify that the ARM processor of Raspberry is supported by  QEMU with the following command:

$ qemu-system-arm -cpu ?

The result will list the type of the supported processor. We ensure that ARM1176 is mentioned.

Basic Installation

We will create a working directory in which we will download the necessary files:

  • Linux Kernel
  • the image of the Raspbian from its official site

The modification of a file is necessary for the functionality of the distribution with Qemu. Therefore we will make a particular first step with BASH in INIT process to achieve it, and we will use the following command:

$ qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw init=/bin/bash" -hda 2015-05-07-wheezy-raspbian.img

When QEMU BASH is restarted we will modify the file /etc/ld.so.preload using the following command:

$ nano /etc/ld.so.preload

We will comment the first line of the file using the following command:

#/usr/lib/arm-linux-gnueabihf/libcofi_rpi.so

Then save (using CTRL-X for the nano editor) and stop emulation:

exit

At this stage, the emulation of Raspbian under QEMU is functional and you can make a real start with the following command:

$ qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -hda 2015-05-07-wheezy-raspbian.img

Widen  the Partition

Unfortunately, there is not a lot of space on the root partition. But you can expand this partition, otherwise we will not be able to update Raspbian with the command apt-get. To do this we will follow the following steps:

First we widen the disc with the QEMU-resize utility using the following command:

$ qemu-img resize 2015-05-07-wheezy-raspbian.img +2G

Then we will restart the Raspbian with QEMU using the following command:

$ qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -hda 2015-05-07-wheezy-raspbian.img

We connect  to the Raspberry with  pi user and the raspberry password. Be careful if you connect from the QEMU window so the keyboard will be set in QWERTY.

Then we run the fdisk utility to modify the partitions using the following command:

$ fdisk /dev/sda
  • delete the partition 2 which begins at the offset 122880: d command and then specify the partition 2
  • recreate a partition 2 that starts at offset 122 880 and uses the entire disk: n command
  •  save the changes: w command
  • Stop the system with the reboot command.

We start again Raspbian with QEMU and launch resize2fs order to expand the partition 2 using the following command:

$ resize2fs /dev/sda2
$ reboot

By default, we have a resolution at 640×480 when we launch LXDE with startx. We can be mounted in 800×600 by creating a xorg.conf.

# sudo nano /etc/X11/xorg.conf

Then add those lines to the file :

Section "Screen"
Identify "Default Screen"
SubSection "Display"
Depth 16
Modes "800x600" "640x480"
EndSubSection
EndSection

Save and restart X to see the result

We faced a problem with the QEMU configuration. The mouse is blocked or certain portions of the screens become inaccessible. To solve it we used the following command:

qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -usbdevice tablet -display sdl -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -hda 2015-05-07-wheezy-raspbian.img

Conclusion

The QEMU emulation allows you to get a good idea of the distribution. Of course, there are some missing details to improve the utilization of this tool. But even at this stage it is helpful and beneficial.