qemu运行编译的linux内核

2022-07-25, updated 2022-07-31

使用qemu模拟arm环境运行编译好的内核

内核编译

环境安装(ubuntu)

1
sudo apt install build-essential libssl-dev libncurses5-dev bison flex

arm内核默认配置位置,其它平台类似

1
arch/arm/configs

make 后添加O=out可以将编译生成的一些.o等文件放到out目录中 配置.config

1
make  vexpress_defconfig ARCH=arm CROSS_COMPILE=arm-linux-gnueabi-

编译

1
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi-

编译生成内核位置以及相关设备树生成文件

1
2
arch/arm/boot/zImage
arch/arm/boot/dts/vexpress-v2p-ca9.dtb 

使用qemu 启动编译好的内核

1
qemu-system-arm -M vexpress-a9 -m 512m -kernel ./qemu-image/zImage -dtb boot/vexpress-v2p-ca9.dtb -nographic -append console=ttyAMA0

制作文件系统

下载、编译和安装busybox

1
wget http://www.busybox.net/downloads/busybox-1.20.2.tar.bz2
1
2
3
make defconfig
make CROSS_COMPILE=arm-linux-gnueabi- O=out
make install CROSS_COMPILE=arm-linux-gnueabi- O=out

make menuconfig 进入 Settings 勾选 Build static binary (no shared libs)

安装完毕后。会在busybox文件夹下生成_install文件夹。该文件夹下的程序就是单板执行所须要的命令。

形成根文件夹结构

先在Ubuntu主机环境下,形成文件夹结构,里面存放的文件和文件夹与单板上执行所须要的文件夹结构全然一样。然后再打包成镜像(在开发板看来就是SD卡)。这个暂时的文件夹结构称为根文件夹

目录下就会生成一个_install 文件夹,就是编译结果,进入这个文件夹进行配置

1
2
3
4
5
cd _install
mkdir proc
mkdir sys
touch init
gedit init

编辑init:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#!/bin/sh
echo "{==DBG==} INIT SCRIPT"
mkdir /tmp
mount -t proc none /proc
mount -t sysfs none /sys
mount -t debugfs none /sys/kernel/debug
mount -t tmpfs none /tmp
#mount指令 挂载某个分区到某个文件,这样就将分区与文件建立联系从而访问文件时就可以访问分区。
# insmod /xxx.ko # 加载模块
mdev -s 
# We need this to find /dev/sda later
echo -e "{==DBG==} Boot took $(cut -d' ' -f1 /proc/uptime) seconds"
setsid /bin/cttyhack setuidgid 1000 /bin/sh #normal user
# exec /bin/sh #root

在_install目录下find . | cpio -o –format=newc > ./rootfs.img将文件夹里的内容打包成一个文件系统。

启动

1
qemu-system-arm -M vexpress-a9 -m 512m -kernel zImage -dtb vexpress-v2p-ca9.dtb -nographic -append console=ttyAMA0 -initrd rootfs.img

退出qemu

在没有图形界面的情况下,可以使用 Ctrl+A 再按X的方式退出 也可以使用如下命令彻底退出

1
ps -A | grep qemu-system-arm | awk '{print $1}' | xargs sudo kill -9

问题

1
2
3
4
can't open /dev/tty3: No such file or directory
can't open /dev/tty2: No such file or directory
can't open /dev/tty4: No such file or directory
can't open /dev/tty3: No such file or directory
words: 726 tags: