Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
956 views
in Technique[技术] by (71.8m points)

linux - How to emulate Raspberry Pi Raspbian with QEMU?

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: enter image description here 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: enter image description here

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: enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...