如何在linux上使用qemu执行镜像文件

如何在linux上使用qemu执行镜像文件,第1张

1. 编译kernel

mkdir -p ~/misc/qemu

cd ~/misc/qemu

wget http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.3.7.tar.bz2

tar xvfj linux-3.3.7.tar.bz2

cd linux-3.3.7

make defconfig

make

cp arch/x86/boot/bzImage ..

2. 制作根文件系统

cd ~/misc/qemu

dd if=/dev/zero of=rootfs.img bs=1M count=10

mkfs.ext3 rootfs.img

mkdir rootfs

sudo mount -t ext3 -o loop rootfs.img rootfs

cd rootfs

mkdir dev proc sys

3. 编译busybox

cd ~/misc/qemu

wget http://busybox.net/downloads/busybox-1.20.1.tar.bz2

tar xvfj busybox-1.20.1.tar.bz2

cd busybox-1.20.1

make defconfig

make menuconfig

(将busybox编译为静态连接方式)

- Busybox Settings

- Build options

- Build Busybox as a static binary

make

make install CONFIG_PREFIX=~/misc/qemu/rootfs

cd ~/misc/qemu

sudo umount rootfs

4. 运行

图形界面下:

qemu -kernel bzImage -hda rootfs.img -append "root=/dev/sda init=/bin/ash"

命令行界面:

qemu -kernel bzImage -hda rootfs.img -append "root=/dev/sda init=/bin/ash" -curses

通过vnc运行:

qemu -kernel bzImage -hda rootfs.img -append "root=/dev/sda init=/linuxrc" -curses -vnc 192.168.1.237:1

上面192.168.1.237为本机的一个IP地址。在另外一台机器上运行vnc client,填入192.168.1.237:1,即可通过vnc连接到linux系统

改进1:mount /proc和/sys,以及创建/dev/下的设备节点

启动后,/dev下无设备文件,而且/proc, /sys都没有mount上,导致有些命令无法运行(如top)。为了解决这个问题,可以修改启动脚本/linuxrc为:

#!/bin/ash

/bin/mount -t proc proc /proc

/bin/mount -t sysfs sysfs /sys

/bin/echo /sbin/mdev >/proc/sys/kernel/hotplug

mdev -s

/bin/ash

然后chmod a+x /linuxrc,umount后再执行

qemu -kernel bzImage -hda rootfs.img -append "root=/dev/sda init=/linuxrc"

有关mdev的介绍和使用可以参考:

http://hi.baidu.com/kebey2004/blog/item/3692f6079b8e9dda7a894721.html

改进2:编一个最小kernel

make allnoconfig

make menuconfig

选择

- Executable file formats / Emulations

- 选择Kernel support for ELF binaries

- Device Drivers

- 选择ATA/ATAPI/MFM/RLL support

- 选择generic/default IDE chipset support

- File systems

- 选择Ext3 journalling file system support

make

qemu -kernel linux-3.3.7/arch/x86/boot/bzImage -hda rootfs.img -append "root=/dev/hda init=/linuxrc" -curses

busybox工具包

静态编译设置好后,

报错一般是export变量没有起作用,修改Makefile的内容就好了

在另外一个terminal输入 killall qemu-system-arm 关闭QEMU平台

要确保内核要包含调试信息

Ubuntu18没有添加到官方仓库,需要手动安装

参考: https://zhuanlan.zhihu.com/p/134031693

重编译内核,在超级终端中输入

-S:中断CPU相应GDB

-s:在1234端口接受GDB调试连接

在调试窗口

在gdb中

eabi标准的要好些,可能arm-linux-gcc就是arm-none-linux-gnueabi的一个链接

终于,郁闷已久的问题攻破了,用了三种配置交叉编译的方法,最终在开发板上实现成功了,现在想一想,有的时候真的也是运气。

之前已经试验过使用arm-linux-gcc-3.4.1配置交叉编译编译环境,配置成功了,在开发板上失败了~

后来使用脚本创建交叉编译环境(crosstool-0.43),配置成功了(这个用了相当长的时间),在开发板上失败了~


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/8359100.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-15
下一篇2023-04-15

发表评论

登录后才能评论

评论列表(0条)

    保存