如何使用 QEMU 模拟 Raspberry Pi Raspbian? [英] How to emulate Raspberry Pi Raspbian with QEMU?

查看:17
本文介绍了如何使用 QEMU 模拟 Raspberry Pi Raspbian?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 QEMU 模拟带有 Raspian 操作系统的 Raspberry Pi.我尝试了互联网上描述的几种方法,但都没有成功.

我发现我需要修补适合所需操作系统的 Linux 内核.就我而言,我选择了带有内核 4.4 的 Rasbian Lite:

wget https://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2016-05-31/2016-05-27-raspbian-jessie-lite.zip解压缩 2016-05-27-raspbian-jessie-lite.ziprm 2016-05-27-raspbian-jessie-lite.zip

接下来我从 这对我来说很有意义,因为内核是 3.10.25 并且比内核 4.4.16 的 Raspbian Jessie 更旧.

使用来自

新内核-qemu 4.4.16 的新尝试:

解决方案

你应该在开始之前展开 Raspbian Image 文件

使用 kpartx 挂载 Raspbian Image 文件(可能需要安装 kpartx)

$ sudo kpartx -av your-image.img添加地图 loop0p1 (252:5): 0 117187 linear/dev/loop0 1添加地图 loop0p2 (252:6): 0 3493888 linear/dev/loop0 118784$ sudo mount/dev/mapper/loop0p2/mnt/img1$ cd/mnt/img1

修改/etc/fstab并注释掉MMCBLK挂载

$ sudo nano etc/fstabproc/proc proc 默认值 0 0#/dev/mmcblk0p1/boot vfat 默认值 0 2#/dev/mmcblk0p2/ext4 默认值,noatime 0 1# 交换文件不是交换分区,这里没有行# 为此使用 dphys-swapfile swap[on|off]

修改/etc/ld.so.preload并注释掉几行...

$ sudo nano etc/ld.so.preload#/usr/lib/arm-linux-gnueabihf/libarmmem.so

卸载并销毁 kpartx 中的循环

$ sudo umount/mnt/img1$ sudo kpartx -d your-image.img

在此处获取与 Raspbian 映像匹配的 Qemu 内核...

https://github.com/dhruvvyas90/qemu-rpi-kernel

我用这个命令成功模拟了 Raspbian Jessie

qemu-system-arm -kernel kernel-qemu-4.4.12-jessie -cpu arm1176 -m 256 -M多功能pb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -redir tcp:5022::22 -hda 2016-05-27-raspbian-jessie-lite.img

I'm trying to emulate a Raspberry Pi with the Raspian OS using QEMU. I've tried several approaches described on the internet but without any success.

I figured out that I need to patch a Linux Kernel fitting the desired OS. In my case I chose Rasbian Lite with Kernel 4.4:

wget https://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2016-05-31/2016-05-27-raspbian-jessie-lite.zip
unzip 2016-05-27-raspbian-jessie-lite.zip
rm 2016-05-27-raspbian-jessie-lite.zip

Next I load a Kernel from https://www.kernel.org/:

wget https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.4.16.tar.gz
tar -xzf linux-4.4.16.tar.gz
rm linux-4.4.16.tar.gz

Now cross compiling the Kernel:

export ARCH=arm
export CROSS_COMPILE=arm-linux-gnueabi-
cd linux-4.4.16
make vexpress_defconfig
make all
cd ..

Now I can copy the boot image, that is a compressed kernel image that auto-extracts in RAM, to my workspace:

cp linux-4.4.16/arch/arm/boot/zImage zImage

and run QEMU

qemu-system-arm -kernel zImage -M vexpress-a9 -m 1024 -cpu cortex-a9 -no-reboot -serial stdio -hda 016-05-27-raspbian-jessie-lite.img -append "root=/dev/sda2 rootfstype=ext4"

But all I see is a black filled QEMU-window. :(

I think that the problem is to get the right Kernel. Copying some of the Kernels from the Internet never led to success because they are not fit for the OS.

How can I build/patch a Kernel fitting the OS (without downloading an existing one) and how to run QEMU properly?

Thanks in advance
Alex


2nd approach

I load a kernel-qemu from here https://www.dropbox.com/s/g8u93xblz1v1ly0/kernel-qemu?dl=0 and run QEMU with it:

qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2" -hda 2016-05-27-raspbian-jessie-lite.img

This brings me the following output: And it makes sense to me because the Kernel is 3.10.25 and older than Raspbian Jessie with Kernel 4.4.16.

With the qemu-kernel from https://github.com/dhruvvyas90/qemu-rpi-kernel/blob/master/kernel-qemu-4.4.12-jessie

qemu-system-arm -kernel kernel-qemu-4.4.12-jessie -cpu arm1176 -m 256 -M versatilepb -serial stdio -append "root=/dev/sda2 rootfstype=ext4 rw" -hda 2016-05-27-raspbian-jessie-lite.img

I got this similar result:

A new try with a new kernel-qemu 4.4.16:

  1. Copy build-kernel-qemu from https://github.com/dhruvvyas90/qemu-rpi-kernel/tree/master/tools and add the following line to checkout the version for Kernel 4.4.16:

    git checkout b05965f284db3e086022f4e318e46cb5bffb1376
    

  2. Run build-kernel-qemu to build the kernel

    sh build-kernel-qemu
    

  3. Run QEMU

    qemu-system-arm -kernel kernel-qemu -m 256 -M versatilepb -serial stdio -append "root=/dev/sda2 rootfstype=ext4 rw" -hda 2016-05-27-raspbian-jessie-lite.img
    

    The result:

解决方案

You should expand the Raspbian Image file before starting

Mount the Raspbian Image file with kpartx (might have to install kpartx)

$ sudo kpartx -av your-image.img
add map loop0p1 (252:5): 0 117187 linear /dev/loop0 1
add map loop0p2 (252:6): 0 3493888 linear /dev/loop0 118784

$ sudo mount /dev/mapper/loop0p2 /mnt/img1
$ cd /mnt/img1

Modify the /etc/fstab and comment out the MMCBLK mounts

$ sudo nano etc/fstab

proc            /proc           proc    defaults          0       0
#/dev/mmcblk0p1  /boot           vfat    defaults          0       2
#/dev/mmcblk0p2  /               ext4    defaults,noatime  0       1
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for that

Modify /etc/ld.so.preload and comment out lines ...

$ sudo nano etc/ld.so.preload

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

Unmount and destroy loops from kpartx

$ sudo umount /mnt/img1
$ sudo kpartx -d your-image.img

Get the Qemu kernel matching the Raspbian image here...

https://github.com/dhruvvyas90/qemu-rpi-kernel

I used this command to successfully emulate Raspbian Jessie

qemu-system-arm -kernel kernel-qemu-4.4.12-jessie -cpu arm1176 -m 256 -M versatilepb 
-no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" 
-redir tcp:5022::22 
-hda 2016-05-27-raspbian-jessie-lite.img

这篇关于如何使用 QEMU 模拟 Raspberry Pi Raspbian?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆