📜 ⬆️ ⬇️

Raspbian and QEMU

image
Hello everybody! They gave me a raspberry pi recently, and I decided to use it. But, as is usually the case, at home there is very little time for this, and there’s nothing to connect it to at work. Therefore, I decided to put raspbian on a virtual machine in order to use it at work. Who cares how I did ask to join
To achieve my goal, I used a variety of articles and posts on forums in various parts of the Internet, links to those articles that I recall will be given at the end of my story. Also, please do not kick for mistakes and style of narration, but indicate all the shortcomings in the comments, since This is my first publication on Habré in particular, and somewhere else in general.

So, let's begin! To make raspbian work on QEMU, we first need the Linux kernel under the arm-platform. There are already ready cores, but for general development I decided to try to assemble myself.
To do this, you will need a machine with an operating system from the Linux family (in fact, I just don’t know if it can be done on other operating systems). I used one of the varieties of Debian, namely Kali Linux, since He was at that moment at hand.
1. From the beginning, download the packages that we need to build the kernel by running the following command:
sudo apt-get install git libncurses5-dev gcc-arm-linux-gnueabihf 

If you are using a 32-bit system, then you will need to install the x86 libraries:
 sudo apt-get install ia32-libs 


2. You also need to install a cross-compiler, for this you need to add a repository:
deb www.emdebian.org/debian unstable main
And execute the following commands:
 apt-get update apt-get install gcc-4.7-arm-linux-gnueabihf 

And for compatibility create a symbolic label on the compiler:
 ln -s 'which arm-linux-gnueabihf-gcc-4.7' /usr/local/bin/arm-linux/gnueabihf-gcc 

')
3. The next step is to download the kernel sources and the patch to support the core of the ARM11 architecture and apply this patch to the sources. To do this, run the following commands:
 mkdir /git cd /git git clone https://github.com/raspberrypi/linux.git wget http://xecdesign.com/downloads/linux-qemu/linux-arm.patch patch -p1 -d linux/ < linux-arm.patch 


4. Next we will need to configure the kernel for the needs of Raspbian.
To do this, execute the following commands:
 cd linux make ARCH=arm versatile_defconfig make ARCH=arm menuconfig 

And in the menu that appears, select the following settings.
IMPORTANT!!! If you select an option, if you have appeared opposite the option, press the space once more, so that <*> appears.
a) Specify the cross compiler:
General Setup -> Cross-compiler tool prefix
arm-linux-gnueabihf-

IMPORTANT! Be sure to include a '-' at the end.

b) CPU settings:
System Type->
[*] Support ARMV6 processor
[*] ARM errata: Invalidation of the Instruction Cache Operation fail
[*] ARM errata: Possible cache data corruption with hit-under-miss enabled


c) Enable support for hard-float binaries:
Floating point emulation ->
[*] VFP-format floating point maths


d) Enabling EABI for ARM:
Kernel Features ->
[*] Use the ARM EABI to compile the kernel
[*] Allow old ABI binaries to run with this kernel


e) Enable QEMU disk support:
Bus Support->
[*] PCI Support
Device Drivers ->
SCSI Device Support
[*] SCSI Device Support
[*] SCSI Disk Support
[*] SCSI CDROM Support
[*] SCSI low-level drivers ->
[*] SYM53C8XX Version 2 SCSI support


f) Enable devtmpfs:
Device Drivers ->
Generic Driver Options ->
[*] Maintain a devtmpfs filesystem to mount at /dev
[*] Automount devtmpfs at /dev, after the kernel mounted the root


g) Inclusion of important file systems:
File Systems ->
<*> Ext3 journaling file system support
<*> The Extended 4 (ext4) filesystem
DOS/FAT/NT Filesystems ->
<*> VFAT (Windows-95) fs support


g) Enable tmpfs:
File Systems ->
Pseudo filesystems->
[*] Tmpfs Virtual Memory file system support (former shm fs)


h) Enable message interface:
Device Drivers ->
Input device support ->
[*] Event interface


i) (optional) Enable /proc/config.gz
General Setup ->
[*] Kernel .config support
[*] Enable access to .config through /proc/config.gz


j) (optional) Use larger fonts and incorporate the logo:
Device Drivers ->
Graphics Support ->
[*] Bootup logo
Console display driver support ->
[*] Select compiled-in fonts
[*] VGA 8x16 font


Save and exit.

Next, we go to the compilation of the kernel itself. To do this, run the following commands:
 make ARCH=arm make arch=arm INSTALL_MOD_PATH=../modules modules_install 


Well, I immediately copy it to the root, because then I will write the parameters from the root.
 cp arch/arm/boot/zImage /kernel-qemu 


This completes the kernel build for QEMU. If you do not want to repeat all this, then at the end I will post links to all files.

The next step is to install QEMU.
For users of Windows OS, everything is very simple. Go to QEMU ON WINDOWS and download the archive with the latest version of QEMU.
IMPORTANT!!! The QEMU version should be at least 1.5.0, since it fixed a critical bug for emulating Raspbian
For Windows users I will not describe in detail the process of starting Raspbian, since it is very similar with the startup process on the Linux family of OS. If you have questions, we will meet in comments.

So, for starters, copy the source code for QEMU:
 cd /git git clone git://git.qemu-project.org/qemu.git 

Also install the dependencies:
 apt-get install libsdl1.2-dev 

Go to the setup and installation:
 cd qemu ./configure -target-list="arm-softmmu arm-linux-user" -enable-sdl -static make sudo make install 

If you get the message Error: DTC not present , then run the following command:
 git submodule update --init dtc 

Now you have QEMU and the kernel, it remains to download the Raspbian image and perform a “setup” boot.

To run Raspbian for the first time, execute the following command:
 qemu-system-arm -kernel /kernel-qemu -cpu arm1176 -m 256 -M versalitepb -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw init=/bin/bash" -hda <___Rapbian> 

After downloading you will see the bash prompt:
image
Enter:
 nano /etc/ld.so.preload 

Comment out the line, press [Ctrl] + [x], save and exit
image
Also execute:
 nano /etc/udev/rules.d/90-qemu.rules 

And enter:
KERNEL=="sda",SYMLINK+="mmcblk0"
KERNEL=="sda?",SYMLINK+="mmcblk0p%n",

Save, close and turn off virtualku:
 shutdown -h now 


Now everything is ready to launch a virtual Raspbian. Run the command:
 qemu-system-arm -kernel /kernel-qemu -cpu arm1176 -m 256 -M versalitepb -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -hda <___Rapbian> 

And if everything went well, then you will see the standard Raspbian prompt:
image

It may also be useful to resize the disk. To do this on the host:
 qemu-img resize <__Raspbian> +6G 

Then download Raspbian and do the following:
 sudo fdisk /dev/sda 

Press [p] and note the Start value for sda2.
Then press [d] and delete the 2 section.
Then press [n] - to create a new section.
Press [p] to indicate that it will be primary.
Indicate that he will be the second by pressing [2].
Set the First sector to Start.
For Last Sector, just press [Enter] to use all the free space.
Press [w] to exit, saving changes to the partition table.
Reboot by running the command:
 sudo shutdown -r now 

After reboot, execute the command:
 sudo resize2fs /dev/sda2 

That's all. Having executed the command
 df -h 

You will see the new size of your disk.

On this, probably all I wanted to say. Please do not kick much for the style of presentation, but rather point out specific errors in the comments.

Links to materials:
1. Compiling the kernel - xecdesign.com/compiling-a-kernel
2. QEMU - wiki.qemu.org
3. My core

Source: https://habr.com/ru/post/216205/


All Articles