qemu学习记录

所使用环境

Ubuntu-18.04.1

或:

PC系统:Windows10
虚拟机:VM
虚拟机系统:ubuntu
模拟的开发板:vexpress

搭建环境时使用的工具

qemu-4.2.0
linux-4.14.157(Linux Kernel)
u-boot-2020.01
busybox-1.31.1
arm-linux-gnueabi-gcc (Linaro 7.5.0-3)

可能需要安装的库
sudo apt-get install build-essential pkg-config zlib1g-dev libglib2.0-0 libglib2.0-dev  libsdl1.2-dev libpixman-1-dev libfdt-dev autoconf automake libtool librbd-dev libaio-dev flex bison -y libncurses5-dev bison flex
#交叉编译链 安装
sudo apt-get install gcc-arm-linux-gnueabihf
sudo apt-get install g++-arm-linux-gnueabihf


安装qemu 版本为4.2.0如果需要别的版本打开网址Index of / ,找到对应版本更改最后面即可

wget https://download.qemu.org/qemu-4.2.0.tar.xz
tar xf qemu-4.2.0.tar.xz
cd qemu-4.2.0
./configure --prefix=/usr/local/qemu --target-list=arm-softmmu --audio-drv-list=
sudo make && sudo make install
sudo ln -s /usr/local/qemu/bin/* /usr/local/bin/

测试安装是否成功

qemu-img -V


输出:

qemu-img version 4.2.0
Copyright © 2003-2020 Fabrice Bellard and the QEMU Project developers

查看qemu支持的板子

qemu-system-arm -M help

配置并且编译内核

下载内核

wget www.kernel.org/pub/linux/kernel/v4.x/linux-4.4.157.tar.xz

解压内核

tar -xvf linux-4.4.157.tar.xz

进入内核文件在进行下一步
cd linux-4.4.157
make vexpress_defconfig ARCH=arm O=./object
编译
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- -j4 O=./object

得到编译文件

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

分别将编译生成的zImage和vexpress-v2p-ca9.dtb文件放到~/qemu目录;

# cp object/arch/arm/boot/zImage ~/qemu
# cp object/arch/arm/boot/dts/vexpress-v2p-ca9.dtb ~/qemu

上述任务完成以后就可以开始启动qemu了

这里要根据自己文件所在目录进行更改一些文件的位置如果严格按照上面的步骤来,执行下面命令就会出现下面情况

qemu启动命令

qemu-system-arm -M vexpress-a9 -m 512M -kernel linux-4.4.157/object/arch/arm/boot/zImage -dtb linux-4.4.157/object/arch/arm/boot/dts/vexpress-v2p-ca9.dtb -nographic -append "console=ttyAMA0"
Booting Linux on physical CPU 0x0
Initializing cgroup subsys cpuset
Linux version 4.4.157 (root@ubuntu) (gcc version 7.5.0 (Ubuntu/Linaro 7.5.0-3ubuntu1~18.04) ) #1 SMP Tue Jul 23 18:51:38 PDT 2024
CPU: ARMv7 Processor [410fc090] revision 0 (ARMv7), cr=10c5387d
CPU: PIPT / VIPT nonaliasing data cache, VIPT nonaliasing instruction cache
Machine model: V2P-CA9
Memory policy: Data cache writeback
CPU: All CPU(s) started in SVC mode.
PERCPU: Embedded 12 pages/cpu @9fbbc000 s18060 r8192 d22900 u49152
Built 1 zonelists in Zone order, mobility grouping on.  Total pages: 130048
Kernel command line: console=ttyAMA0
log_buf_len individual max cpu contribution: 4096 bytes
log_buf_len total cpu_extra contributions: 12288 bytes
log_buf_len min size: 16384 bytes
log_buf_len: 32768 bytes
early log buf free: 14976(91%)
PID hash table entries: 2048 (order: 1, 8192 bytes)
Dentry cache hash table entries: 65536 (order: 6, 262144 bytes)
Inode-cache hash table entries: 32768 (order: 5, 131072 bytes)
Memory: 512540K/524288K available (4890K kernel code, 156K rwdata, 1400K rodata, 284K init, 152K bss, 11748K reserved, 0K cma-reserved)
Virtual kernel memory layout:
    vector  : 0xffff0000 - 0xffff1000   (   4 kB)
    fixmap  : 0xffc00000 - 0xfff00000   (3072 kB)
    vmalloc : 0xa0800000 - 0xff800000   (1520 MB)
    lowmem  : 0x80000000 - 0xa0000000   ( 512 MB)
    modules : 0x7f000000 - 0x80000000   (  16 MB)
      .text : 0x80008000 - 0x8062cac4   (6291 kB)
      .init : 0x8062d000 - 0x80674000   ( 284 kB)
      .data : 0x80674000 - 0x8069b3a0   ( 157 kB)
       .bss : 0x8069e000 - 0x806c43f8   ( 153 kB)
SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=4, Nodes=1
Hierarchical RCU implementation.
	Build-time adjustment of leaf fanout to 32.
	RCU restricting CPUs from NR_CPUS=8 to nr_cpu_ids=4.
RCU: Adjusting geometry for rcu_fanout_leaf=32, nr_cpu_ids=4
NR_IRQS:16 nr_irqs:16 16
GIC CPU mask not found - kernel will fail to boot.
GIC CPU mask not found - kernel will fail to boot.
L2C: platform modifies aux control register: 0x02020000 -> 0x02420000
L2C: DT/platform modifies aux control register: 0x02020000 -> 0x02420000
L2C-310 enabling early BRESP for Cortex-A9
L2C-310 full line of zeros enabled for Cortex-A9
L2C-310 dynamic clock gating disabled, standby mode disabled
L2C-310 cache controller enabled, 8 ways, 128 kB
L2C-310: CACHE_ID 0x410000c8, AUX_CTRL 0x46420001
smp_twd: clock not found -2
sched_clock: 32 bits at 24MHz, resolution 41ns, wraps every 89478484971ns
clocksource: arm,sp804: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 1911260446275 ns
Console: colour dummy device 80x30
Calibrating local timer... 92.15MHz.
Calibrating delay loop... 766.77 BogoMIPS (lpj=3833856)
pid_max: default: 32768 minimum: 301
Mount-cache hash table entries: 1024 (order: 0, 4096 bytes)
Mountpoint-cache hash table entries: 1024 (order: 0, 4096 bytes)
CPU: Testing write buffer coherency: ok
CPU0: thread -1, cpu 0, socket 0, mpidr 80000000
Setting up static identity map for 0x60008280 - 0x600082d8
Brought up 1 CPUs
SMP: Total of 1 processors activated (766.77 BogoMIPS).
CPU: All CPU(s) started in SVC mode.
devtmpfs: initialized
VFP support v0.3: implementor 41 architecture 3 part 30 variant 9 rev 0
clocksource: jiffies: mask: 0xffffffff max_cycles: 0xffffffff, max_idle_ns: 19112604462750000 ns
futex hash table entries: 1024 (order: 4, 65536 bytes)
NET: Registered protocol family 16
DMA: preallocated 256 KiB pool for atomic coherent allocations
cpuidle: using governor ladder
cpuidle: using governor menu
of_amba_device_create(): amba_device_add() failed (-19) for /memory-controller@100e0000
of_amba_device_create(): amba_device_add() failed (-19) for /memory-controller@100e1000
of_amba_device_create(): amba_device_add() failed (-19) for /watchdog@100e5000
of_amba_device_create(): amba_device_add() failed (-19) for /smb/motherboard/iofpga@7,00000000/sysctl@01000
of_amba_device_create(): amba_device_add() failed (-19) for /smb/motherboard/iofpga@7,00000000/wdt@0f000
hw-breakpoint: debug architecture 0x4 unsupported.
Serial: AMBA PL011 UART driver
10009000.uart: ttyAMA0 at MMIO 0x10009000 (irq = 38, base_baud = 0) is a PL011 rev1
console [ttyAMA0] enabled
1000a000.uart: ttyAMA1 at MMIO 0x1000a000 (irq = 39, base_baud = 0) is a PL011 rev1
1000b000.uart: ttyAMA2 at MMIO 0x1000b000 (irq = 40, base_baud = 0) is a PL011 rev1
1000c000.uart: ttyAMA3 at MMIO 0x1000c000 (irq = 41, base_baud = 0) is a PL011 rev1
SCSI subsystem initialized
usbcore: registered new interface driver usbfs
usbcore: registered new interface driver hub
usbcore: registered new device driver usb
Advanced Linux Sound Architecture Driver Initialized.
clocksource: Switched to clocksource arm,sp804
NET: Registered protocol family 2
TCP established hash table entries: 4096 (order: 2, 16384 bytes)
TCP bind hash table entries: 4096 (order: 3, 32768 bytes)
TCP: Hash tables configured (established 4096 bind 4096)
UDP hash table entries: 256 (order: 1, 8192 bytes)
UDP-Lite hash table entries: 256 (order: 1, 8192 bytes)
NET: Registered protocol family 1
RPC: Registered named UNIX socket transport module.
RPC: Registered udp transport module.
RPC: Registered tcp transport module.
RPC: Registered tcp NFSv4.1 backchannel transport module.
hw perfevents: enabled with armv7_cortex_a9 PMU driver, 5 counters available
squashfs: version 4.0 (2009/01/31) Phillip Lougher
jffs2: version 2.2. (NAND) © 2001-2006 Red Hat, Inc.
9p: Installing v9fs 9p2000 file system support
io scheduler noop registered (default)
clcd-pl11x 10020000.clcd: PL111 rev2 at 0x10020000
clcd-pl11x 10020000.clcd: /clcd@10020000 hardware, 1024x768@59 display
Console: switching to colour frame buffer device 128x48
clcd-pl11x 1001f000.clcd: PL111 rev2 at 0x1001f000
clcd-pl11x 1001f000.clcd: /smb/motherboard/iofpga@7,00000000/clcd@1f000 hardware, 640x480@59 display
40000000.flash: Found 2 x16 devices at 0x0 in 32-bit bank. Manufacturer ID 0x000000 Chip ID 0x000000
Intel/Sharp Extended Query Table at 0x0031
Using buffer write method
40000000.flash: Found 2 x16 devices at 0x0 in 32-bit bank. Manufacturer ID 0x000000 Chip ID 0x000000
Intel/Sharp Extended Query Table at 0x0031
Using buffer write method
Concatenating MTD devices:
(0): "40000000.flash"
(1): "40000000.flash"
into device "40000000.flash"
libphy: smsc911x-mdio: probed
smsc911x 4e000000.ethernet eth0: attached PHY driver [Generic PHY] (mii_bus:phy_addr=4e000000.ethernet-ffffffff:01, irq=-1)
smsc911x 4e000000.ethernet eth0: MAC Address: 52:54:00:12:34:56
isp1760 4f000000.usb: bus width: 32, oc: digital
isp1760 4f000000.usb: NXP ISP1760 USB Host Controller
isp1760 4f000000.usb: new USB bus registered, assigned bus number 1
isp1760 4f000000.usb: Scratch test failed.
isp1760 4f000000.usb: can't setup: -19
isp1760 4f000000.usb: USB bus 1 deregistered
usbcore: registered new interface driver usb-storage
mousedev: PS/2 mouse device common for all mice
rtc-pl031 10017000.rtc: rtc core: registered pl031 as rtc0
mmci-pl18x 10005000.mmci: Got CD GPIO
mmci-pl18x 10005000.mmci: Got WP GPIO
mmci-pl18x 10005000.mmci: No vqmmc regulator found
mmci-pl18x 10005000.mmci: mmc0: PL181 manf 41 rev0 at 0x10005000 irq 34,35 (pio)
input: AT Raw Set 2 keyboard as /devices/platform/smb/smb:motherboard/smb:motherboard:iofpga@7,00000000/10006000.kmi/serio0/input/input0
ledtrig-cpu: registered to indicate activity on CPUs
usbcore: registered new interface driver usbhid
usbhid: USB HID core driver
aaci-pl041 10004000.aaci: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 33
aaci-pl041 10004000.aaci: FIFO 512 entries
oprofile: using arm/armv7-ca9
NET: Registered protocol family 17
9pnet: Installing 9P2000 support
Registering SWP/SWPB emulation handler
rtc-pl031 10017000.rtc: setting system clock to 2024-07-24 02:11:25 UTC (1721787085)
ALSA device list:
  #0: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 33
input: ImExPS/2 Generic Explorer Mouse as /devices/platform/smb/smb:motherboard/smb:motherboard:iofpga@7,00000000/10007000.kmi/serio1/input/input2
VFS: Cannot open root device "(null)" or unknown-block(0,0): error -6
Please append a correct "root=" boot option; here are the available partitions:
1f00          131072 mtdblock0  (driver?)
1f01           32768 mtdblock1  (driver?)
Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)
CPU: 0 PID: 1 Comm: swapper/0 Not tainted 4.4.157 #1
Hardware name: ARM-Versatile Express
[<800164b4>] (unwind_backtrace) from [<80012e9c>] (show_stack+0x10/0x14)
[<80012e9c>] (show_stack) from [<80246d50>] (dump_stack+0x88/0x9c)
[<80246d50>] (dump_stack) from [<800a774c>] (panic+0x9c/0x1f4)
[<800a774c>] (panic) from [<8062e2b8>] (mount_block_root+0x1d4/0x274)
[<8062e2b8>] (mount_block_root) from [<8062e478>] (mount_root+0x120/0x128)
[<8062e478>] (mount_root) from [<8062e5d0>] (prepare_namespace+0x150/0x198)
[<8062e5d0>] (prepare_namespace) from [<8062debc>] (kernel_init_freeable+0x254/0x264)
[<8062debc>] (kernel_init_freeable) from [<804a6c24>] (kernel_init+0x8/0xe8)
[<804a6c24>] (kernel_init) from [<8000f4d0>] (ret_from_fork+0x14/0x24)
---[ end Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)

最后的Kernel panic是因为没有挂载根文件系统,下一步制作一个简易文件系统。

制作简易根文件系统

下载busybox工具

解压busybox

 tar -xvf busybox-1.31.1.tar.bz2

切进这个目录在执行以下操作

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- menuconfig
编译
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi-
安装
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- install

提示下面内容表示安装成功

--------------------------------------------------
You will probably need to make your busybox binary
setuid root to ensure all configured applets will
work properly.
--------------------------------------------------

安装完成之后,生成的目标文件默认在./_install目录,这个目标文件目录就是下边要制作根文件系统需要用到的工具

生成简易根文件系统

编译并安装busybox

将busybox编译生成的_install目录下的文件全部拷贝到根文件系统目标rootfs/目录:

mkdir rootfscp /.../busybox-1.29.3/_install/* rootfs/ -rfd

添加glibc库

在根文件系统中添加加载器和动态库:

mkdir rootfs/lib
cp /usr/arm-linux-gnueabi/lib/* rootfs/lib/ -rfp

静态创建设备文件

# mkdir rootfs/dev
# cd rootfs/dev
# mknod -m 666 tty1 c 4 1
# mknod -m 666 tty2 c 4 2
# mknod -m 666 tty3 c 4 3
# mknod -m 666 tty4 c 4 4
# mknod -m 666 console c 5 1
# mknod -m 666 null c 1 3

至此,简易版根文件系统就制作完成,该根文件系统只含有最基本的功能

制作SD卡文件系统镜像

在名为自己创建的 SDCard (可以是其他地方)的文件夹中进行

生成一个空的SD卡镜像

dd if=/dev/zero of=uboot.disk bs=1M count=250
# 注意自己控制 count 大小 ,总大小 1*250 = 250 M

创建分区

 创建两个分区(一个用来存放kernel和设备树,另一个存放根文件系统)

sgdisk -n 0:0:+10M -c 0:kernel uboot.disk
sgdisk -n 0:0:0 -c 0:rootfs uboot.disk

查看分区: 

sgdisk -p uboot.disk

映射SD卡镜像到空闲loop设备

LOOPDEV=`losetup -f`   # 查找空闲的loop设备
echo $LOOPDEV	# 我这边时 /dev/loop10
sudo losetup $LOOPDEV  uboot.disk
sudo partprobe $LOOPDEV
sudo losetup -l
ls /dev/loop*
# 如果需要解映射 : sudo losetup -d /dev/loop10

会看到/dev/loop10p1 和/dev/loop10p2 两个节点

格式化并挂载

# 格式化 
sudo mkfs.ext4 /dev/loop10p1
sudo mkfs.ext4 /dev/loop10p2
# 挂载
mkdir p1 p2
sudo mount -t ext4 /dev/loop10p1 p1   # 存放kernel和设备树
sudo mount -t ext4 /dev/loop10p2 p2   # 存放根文件系统
# 查看挂载情况
 df -h

拷贝文件

# 将 zImage 和 dtb 拷贝到 p1  
# 之前编译内核的内核目录(linux-5.4.95 文件夹中)
sudo cp /home/jianghao/linux-4.4.157/object/arch/arm/boot/zImage /home/jianghao/SDCard/p1

sudo /home/jianghao/linux-4.4.157/object/arch/arm/boot/dts/vexpress-v2p-ca9.dtb /home/jianghao/SDCard/p1


# 将 文件系统中的文件拷贝到 p2
# 之前制作rootfs的 busybox-1.32.1 目录中 文件夹 rootfs 下为 文件系统(未打包直接拷贝,如果制作的文件系统在镜像中需挂载)
# busybox 根目录 busybox-1.32.1 中执行
sudo cp /home/jianghao/rootfs/* /home/jianghao/SDCard/p2 -arf
# 最后 查看 p1 p2 是否有对应的文件

准备u-boot

下载u-boot

解压u-boot

编译u-boot

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

生成配置文件

make vexpress_ca9x4_defconfig ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- O=./object

编译u-boot:

make vexpress_ca9x4_defconfig
make -j4

如果编译出现问题参考这篇文章

Ubuntu U-boot移植编译错误 [bad value (‘generic-armv7-a’) for ‘-mtune=’ switch]_cc1: error: bad value (‘generic-armv7-a’) for ‘-mt-CSDN博客具体的解决办法就是全局添加ARCH 和 CROSS_COMPILE


准备内核

指定加载地址

 make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- LOADADDR=0x60003000 uImage -j4 O=./object

如果出现问题参考这个

mkimage command not found - U-Boot images will not be built_mkimage" command not found - u-boot images will no-CSDN博客

解决办法其实就是寻找内核编译的mkimage 这个文件在u-boot-版本号/tools/,把这个文件放入系统的/bin文件夹再执行上述命令就行

启动uboot 查看sd卡情况

从 之前编译u-boot的文件夹(u-boot-master)中拷贝 u-boot 文件到 名为 SDCard 的文件夹中

# 在名为 SDCard (可以是其他地方)的文件夹中进行 ,因为上面制作的 SD卡镜像 在这个文件夹中
qemu-system-arm -M vexpress-a9 -m 1024M -smp 1 -nographic -kernel u-boot -sd ./uboot.disk

在倒计时结束之前打断它,不要让其进入自主模式

CTRL+C打断

U-Boot 2021.04-rc1 (Feb 05 2021 - 09:02:59 +0800)

DRAM:  1 GiB
WARNING: Caches not enabled
Flash: 128 MiB
MMC:   MMC: 0
*** Warning - bad CRC, using default environment

In:    serial
Out:   serial
Err:   serial
Net:   smc911x-0
Hit any key to stop autoboot:  0 
=> 

加载kernel、设备树

加载sd卡分区中指定的文件(内核)到指定的内存: load mmc 0:1 0x60008000 zImage或者ext4load mmc 0:1 0x60008000 zImage

=> load mmc 0:1 0x61000000 vexpress-v2p-ca9.dtb
14360 bytes read in 68 ms (206.1 KiB/s)

加载sd卡分区中指定的文件(设备树)到指定的内存:load mmc 0:1 0x61000000 vexpress-v2p-ca9.dtb 或者 ext4load mmc 0:1 0x61000000 vexpress-v2p-ca9.dtb

=> load mmc 0:1 0x60008000 zImage
3487608 bytes read in 2918 ms (1.1 MiB/s)
设置bootargs
setenv bootargs 'root=/dev/mmcblk0p2 rw rootfstype=ext4 rootwait earlycon console=tty0 console=ttyAMA0 init=/linuxrc ignore_loglevel'
# rootwait是无限期等待,而rootdelay可以指定等待的时间
引导内核
bootz 0x60008000 - 0x61000000
# bootz是启动zImage
rtc-pl031 10017000.rtc: setting system clock to 2018-09-24 13:22:14 UTC (1537795334)
ALSA device list:
  #0: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 33
input: ImExPS/2 Generic Explorer Mouse as /devices/platform/smb/smb:motherboard/smb:motherboard:iofpga@7,00000000/10007000.kmi/serio1/input/input2
EXT4-fs (mmcblk0): mounting ext3 file system using the ext4 subsystem
EXT4-fs (mmcblk0): recovery complete
EXT4-fs (mmcblk0): mounted filesystem with ordered data mode. Opts: (null)
VFS: Mounted root (ext3 filesystem) on device 179:0.
Freeing unused kernel memory: 284K
random: nonblocking pool is initialized
can't run '/etc/init.d/rcS': No such file or directory

出现最后这个可以不用管,打印中提示的不能运行/etc/init.d/rcS问题,只需要添加/etc/init.d/rcS文件即可,文件内容可以是提示语句。这个文件再写的时候要给可执行权限

# vim /etc/init.d/rcS
Hello Qemu Linux!
# chmod +x rcS

固化启动命令

如果上面 引导内核 没有问题的话,那么可以在uboot的代码中将上面的命令固化进程序中

同样也是include/configs/vexpress_common.h中修改CONFIG_BOOTCOMMAND(注意备份)
改成

#define CONFIG_BOOTCOMMAND  "load mmc 0:1 0x60008000 zImage ;load mmc 0:1 0x61000000 vexpress-v2p-ca9.dtb; setenv bootargs \"root=/dev/mmcblk0p2 earlycon console=ttyAMA0\"; bootz 0x60008000 - 0x61000000"

拷贝上面新生成的 u-boot 到 之前 存放 SD 卡镜像 (uboot.disk)的目录(我这边就是名为 SDCard的文件夹 )下面,执行 以下命令 测试 uboot通过sd卡加载uImage

测试完整启动

qemu-system-arm -M vexpress-a9 -m 1024M -smp 1 -nographic -kernel u-boot -sd uboot.disk

我的还是出现can't run '/etc/init.d/rcS': No such file or directory这个问题,这个其实就是环境变量的配置问题,具体可以参考linux下arm环境启动脚本/etc/init.d/rcS执行命令失败,踩坑记录_arm linux 启动脚本-CSDN博客,实际不影响使用我就没管了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值