Ubuntu下qemu环境搭建vexpress开发平台

前提是你 提交于 2019-12-26 15:21:14

在查找资料过程中,发现自己搭建虚拟的arm环境的话,有一个比较好的软件就是qemu了,当然还有其他的,大家各投所好就好。

接下来说一下qemu环境搭建过程。

其实搭建很简单,作为小白,我还是捣鼓了两三天才真正的安装成功,正在尝试着怎么使用。

上篇已经讲了安装Ubuntu系统后root密码的设置,这里就要用到root密码进行软件安装了。

可以在命令行模式下先进行update,输入:sudo apt-get update

在保证电脑联网的情况下,Ubuntu会自动进行文件更新,这个操作并不会更新整个文件系统,

他只是同步le  /etc/apt/sources.list 和 /etc/apt/sources.list.d 中列出的源的索引,这样才能获取到最新的软件包。

并且只有在更新了资源列表后才能使用apt-get install进行成功安装(我当时就是没有update就去安装程序,失败了好多次,各种欲哭无泪,

另:一定要保证自己的网络环境良好,更新的文件都不大但是我想你不会想看着电脑以100B的速度去下载6000K+的文件的555555)

在update完成之后就可以进行qemu的安装了,输入指令:sudo apt-get install qemu(如果失败并提示你需要尝试修复什么文件的话用sudo apt-get -f install qemu代替)

在连着输入两个表示同意的‘y’之后等着系统自己安装就好了。

安装完毕,输入qemu并联系按几下table,就会显示当前qemu支持的架构(还是内核?不是很清楚这个概念)

qemu安装到此结束。

========================================和上方震惊体标题后脑残的内容划清界限,下面搭建Vexpress的qemu环境==========================================================================

1. 安装qemu,参考上方脑残内容,安装完毕后查看qemu版本可出现如下内容表示安装成功

sly@ubuntu:~/develop$ qemu-arm --version
qemu-arm version 2.11.1(Debian 1:2.11+dfsg-1ubuntu7.19)
Copyright (c) 2003-2017 Fabrice Bellard and the QEMU Project developers

 

PS: 交叉编译工具链也需要安装  sudo apt-get install gcc-arm-linux-gnueabi

 

2. 编译Linux内核

生成vexpress开发板子的config文件:

sly@ubuntu:~/develop/linux-5.3.7$ make CROSS_COMPILE=arm-linux-gnueabi- ARCH=arm vexpress_defconfig
#
# configuration written to .config
#

修改Makfile如下两个变量值

ARCH ?= arm
CROSS_COMPILE ?= arm-linux-gnueabi-

 

编译:

sly@ubuntu:~/develop/linux-5.3.7$ make sly@ubuntu:~/develop/linux-5.3.7$ make modulessly@ubuntu:~/develop/linux-5.3.7$ make dtbs

生成的内核镱像位于arch/arm/boot/zImage, qemu启动时须要使用该镜像。

sly@ubuntu:~/develop/linux-5.3.7$ make
scripts/kconfig/conf  --syncconfig Kconfig
  CALL    scripts/checksyscalls.sh
  CALL    scripts/atomic/check-atomics.sh
  CHK     include/generated/compile.h
  GZIP    kernel/config_data.gz
  CC      kernel/configs.o
  AR      kernel/built-in.a
  GEN     .version
  CHK     include/generated/compile.h
  UPD     include/generated/compile.h
  CC      init/version.o
  AR      init/built-in.a
  LD      vmlinux.o
  MODPOST vmlinux.o
  MODINFO modules.builtin.modinfo-
  KSYM    .tmp_kallsyms1.o
  KSYM    .tmp_kallsyms2.o
  LD      vmlinux
  SORTEX  vmlinux
  SYSMAP  System.map
  OBJCOPY arch/arm/boot/Image
  Kernel: arch/arm/boot/Image is ready
  GZIP    arch/arm/boot/compressed/piggy_data
  AS      arch/arm/boot/compressed/piggy.o
  LD      arch/arm/boot/compressed/vmlinux
  OBJCOPY arch/arm/boot/zImage
  Kernel: arch/arm/boot/zImage is ready
  Building modules, stage 2.
  MODPOST 1 modules

 

3. 測试qemu和内核是否能执行成功

qemu已经安装好了。内核也编译成功了,到这里最好是測试一下,编译出来的内核是否OK,或者qemu对vexpress单板支持是否够友好。

执行命令:qemu-system-arm -M vexpress-a9 -m 512m -kernel /home/sly/develop/linux-5.3.7/arch/arm/boot/zImage -dtb linux-5.3.7/arch/arm/boot/dts/vexpress-v2p-ca9.dtb -nographic -append "console=ttyAMA0

sly@ubuntu:~/develop$ qemu-system-arm -M vexpress-a9 -m 512m -kernel /home/sly/develop/linux-5.3.7/arch/arm/boot/zImage -dtb linux-5.3.7/arch/arm/boot/dts/vexpress-v2p-ca9.dtb -nographic -append "console=ttyAMA0"
pulseaudio: set_sink_input_volume() failed
pulseaudio: Reason: Invalid argument
pulseaudio: set_sink_input_mute() failed
pulseaudio: Reason: Invalid argument
Booting Linux on physical CPU 0x0
Linux version 5.3.7 (sly@ubuntu) (gcc version 7.4.0 (Ubuntu/Linaro 7.4.0-1ubuntu1~18.04.1)) #4 SMP Mon Dec 9 08:31:07 PST 2019
--省略甚多log----------

Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0)
CPU: 0 PID: 1 Comm: swapper/0 Not tainted 5.3.7 #4
Hardware name: ARM-Versatile Express
[<80110808>] (unwind_backtrace) from [<8010c680>] (show_stack+0x10/0x14)
[<8010c680>] (show_stack) from [<8074c7cc>] (dump_stack+0x88/0x9c)
[<8074c7cc>] (dump_stack) from [<80120e64>] (panic+0x110/0x310)
[<80120e64>] (panic) from [<80a01640>] (mount_block_root+0x1e8/0x2d4)
[<80a01640>] (mount_block_root) from [<80a01870>] (mount_root+0x144/0x160)
[<80a01870>] (mount_root) from [<80a019dc>] (prepare_namespace+0x150/0x198)
[<80a019dc>] (prepare_namespace) from [<80763874>] (kernel_init+0x8/0x114)
[<80763874>] (kernel_init) from [<801010e8>] (ret_from_fork+0x14/0x2c)
Exception stack(0x9e4a1fb0 to 0x9e4a1ff8)
1fa0: 00000000 00000000 00000000 00000000
1fc0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
1fe0: 00000000 00000000 00000000 00000000 00000013 00000000
---[ end Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(0,0) ]---

假设看到内核启动过程中的打印,说明前的搭建是成功的。

这里简介下qemu命令的參数:

-M vexpress-a9 模拟vexpress-a9单板,你能够使用-M ?參数来获取该qemu版本号支持的全部单板

-m 512M 单板执行物理内存512M

-kernel /home/ivan/kernel_git/linux/arch/arm/boot/zImage  告诉qemu单板执行内核镜像路径

-nographic 不使用图形化界面,仅仅使用串口

-append "console=ttyAMA0" 内核启动參数。这里告诉内核vexpress单板执行。串口设备是哪个tty。从生成的.config文件CONFIG_CONSOLE宏找到

 

4. 利用busybox构建最小文件系统(下载地址:https://busybox.net/

  不加载文件系统穷的那个qemu会产生上面贴的log中的panic信息,下面先制作一个可用的最小文件系统。

a. 编译busybox

  a1. 下载源文件后进行配置

sly@ubuntu:~/develop$ cd busybox-1.24.2/
sly@ubuntu:~/develop/busybox-1.24.2$ export ARCH=arm
sly@ubuntu:~/develop/busybox-1.24.2$ export CROSS_COMPILE=arm-linux-gnueabi-
sly@ubuntu:~/develop/busybox-1.24.2$ make menuconfig

  a2.menuconfig配置成静态

   Busybox Settings  --->    Build Options  --->       [*] Build BusyBox as a static binary (no shared libs) 

  a3. 开始编译, 编译完成后会在busybox目录下生成一个_install的目录,该目录是编译好的文件系统需要使用的一些命令集合。

sly@ubuntu:~/develop/busybox-1.24.2$ make defconfigsly@ubuntu:~/develop/busybox-1.24.2$ make CROSS_COMPILE=arm-linux-gnueabi-sly@ubuntu:~/develop/busybox-1.24.2$ make install CROSS_COMPILE=arm-linux-gnueabi-

  生成的_install目录包含如下内容

sly@ubuntu:~/develop/busybox-1.24.2/_install$ ll
total 20
drwxrwxr-x  5 sly sly 4096 Dec  9 08:14 ./
drwxr-xr-x 36 sly sly 4096 Dec  9 21:08 ../
drwxrwxr-x  2 sly sly 4096 Dec  9 08:14 bin/
lrwxrwxrwx  1 sly sly   11 Dec  9 08:14 linuxrc -> bin/busybox*
drwxrwxr-x  2 sly sly 4096 Dec  9 08:14 sbin/
drwxrwxr-x  4 sly sly 4096 Oct 30 22:18 usr/

b. 制作根文件系统

  b1. 新建一个根文件系统的文件夹

sly@ubuntu:~/develop$ mkdir rootfssly@ubuntu:~/develop$ cd rootfs/

 

  b2. 拷贝install目录的命令集到文件夹中

sly@ubuntu:~/develop/rootfs$ cp -rf ../busybox-1.24.2/_install/* .
sly@ubuntu:~/develop/rootfs$ ls
bin  dev  lib  linuxrc  sbin  usr

  b3. 新建lib目录,从工具链中拷贝执行库到lib文件夹下

sly@ubuntu:~/develop/rootfs$ mkdir libsly@ubuntu:~/develop/rootfs$ cp -p /usr/arm-linux-gnueabi/lib/* ./lib

 

  b4. 创建设备文件, 创建4个串口设备,和控制台

sly@ubuntu:~/develop/rootfs$ sudo mknod dev/tty1 c 4 1
sly@ubuntu:~/develop/rootfs$ sudo mknod dev/tty2 c 4 2
sly@ubuntu:~/develop/rootfs$ sudo mknod dev/tty3 c 4 3
sly@ubuntu:~/develop/rootfs$ sudo mknod dev/tty4 c 4 4
sly@ubuntu:~/develop/rootfs$sudo mknod -m 666 console c 5 1

 

  b5.  制作SD根文件系统镜像, 生成虚拟sd卡并格式化为ext格式:

dd if=/dev/zero of=rootfs.ext3 bs=1M count=32mkfs.ext3 rootfs.ext3
sly@ubuntu:~/develop$ dd if=/dev/zero of=rootfs.ext3 bs=1M count=32
32+0 records in
32+0 records out
33554432 bytes (34 MB, 32 MiB) copied, 0.0941612 s, 356 MB/s
sly@ubuntu:~/develop$ mkfs.ext3 rootfs.ext3
mke2fs 1.44.1 (24-Mar-2018)
Discarding device blocks: done                            
Creating filesystem with 32768 1k blocks and 8192 inodes
Filesystem UUID: fe2c8081-588f-4e0c-b0c7-09489d0fded5
Superblock backups stored on blocks: 
        8193, 24577

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

sly@ubuntu:~/develop$ ls
busybox-1.24.2 linux-5.3.7 linux-5.3.7.tar rootfs rootfs.ext3 tmpfs

 

  b6. 将虚拟sd卡挂载到/tmpfs,拷贝rootfs的所有文件到sd,卸载sd(块设备不能直接读写)

sudo mkdir tmpfssudo mount -t ext3 rootfs.ext3 tmpfs/ -o loop
sudo cp -r rootfs/*  tmpfs/
sudo umount tmpfs

sly@ubuntu:~/develop$ sudo mount -t ext3 rootfs.ext3 tmpfs/ -o loop
sly@ubuntu:~/develop$ sudo cp -r rootfs/* tmpfs/
sly@ubuntu:~/develop$ ls tmpfs/
bin dev lib linuxrc lost+found sbin usr
sly@ubuntu:~/develop$ sudo umount tmpfs
sly@ubuntu:~/develop$ ls tmpfs/
sly@ubuntu:~/develop$

 5. 启动内核,挂载rootfs

 qemu-system-arm -M vexpress-a9 -m 512m -kernel /home/sly/develop/linux-5.3.7/arch/arm/boot/zImage -dtb linux-5.3.7/arch/arm/boot/dts/vexpress-v2p-ca9.dtb -nographic -append "root=/dev/mmcblk0 console=ttyAMA0"  -sd rootfs.ext3

 

sly@ubuntu:~/develop$  qemu-system-arm -M vexpress-a9 -m 512m -kernel /home/sly/develop/linux-5.3.7/arch/arm/boot/zImage -dtb linux-5.3.7/arch/arm/boot/dts/vexpress-v2p-ca9.dtb -nographic -append "root=/dev/mmcblk0 console=ttyAMA0"  -sd rootfs.ext3
WARNING: Image format was not specified for 'rootfs.ext3' and probing guessed raw.
         Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted.
         Specify the 'raw' format explicitly to remove the restrictions.
pulseaudio: set_sink_input_volume() failed
pulseaudio: Reason: Invalid argument
pulseaudio: set_sink_input_mute() failed
pulseaudio: Reason: Invalid argument
Booting Linux on physical CPU 0x0
Linux version 5.3.7 (sly@ubuntu) (gcc version 7.4.0 (Ubuntu/Linaro 7.4.0-1ubuntu1~18.04.1)) #4 SMP Mon Dec 9 08:31:07 PST 2019=====================================================......=========================================================================
ALSA device list:
  #0: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 24
input: ImExPS/2 Generic Explorer Mouse as /devices/platform/smb@4000000/smb@4000000:motherboard/smb@4000000:motherboard:iofpga@7,00000000/10007000.kmi/serio1/input/input2
Error: Driver 'vexpress-muxfpga' is already registered, aborting...
drm-clcd-pl111 10020000.clcd: initializing Versatile Express PL111
drm-clcd-pl111 10020000.clcd: DVI muxed to daughterboard 1 (core tile) CLCD
EXT4-fs (mmcblk0): mounting ext3 file system using the ext4 subsystem
random: fast init done
EXT4-fs (mmcblk0): mounted filesystem with ordered data mode. Opts: (null)
VFS: Mounted root (ext3 filesystem) readonly on device 179:0.
Freeing unused kernel memory: 1024K
Run /sbin/init as init process
random: crng init done
can't run '/etc/init.d/rcS': No such file or directory

Please press Enter to activate this console. 
/ # 

 / # ls
  bin dev lib linuxrc lost+found sbin usr

 6. 编写简单的模块进行加载

文件目录结构如下

sly@ubuntu:~/develop/module/first$ ls
first.c  Makefile

 

编写源文件

#include <linux/module.h>
#include <linux/kernel.h>

int init_hello_module(void)
{
    printk("init hello_moudle\n");
    return 0;
}

void exit_hello_module(void)
{
    printk("exit hello_module\n");
}

module_init(init_hello_module);
module_exit(exit_hello_module);

MODULE_LICENSE("GPL");

 

makefile文件

KERN_DIR=../../linux-5.3.7
  
all:
        make -C $(KERN_DIR) M=`pwd` modules 

clean:
        make -C $(KERN_DIR) M=`pwd` modules clean
        rm -rf modules.order

obj-m   += first.o

 

执行编译,编译出ko文件

sly@ubuntu:~/develop/module/first$ make
make -C ../../linux-5.3.7 M=`pwd` modules 
make[1]: Entering directory '/home/sly/develop/linux-5.3.7'
  CC [M]  /home/sly/develop/module/first/first.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/sly/develop/module/first/first.mod.o
  LD [M]  /home/sly/develop/module/first/first.ko
make[1]: Leaving directory '/home/sly/develop/linux-5.3.7'
sly@ubuntu:~/develop/module/first$ ls
first.c  first.ko  first.mod  first.mod.c  first.mod.o  first.o  Makefile  modules.order  Module.symvers
rootfs根文件系统下面创建一个modules目录用来存放ko文件
拷贝rootfs的内容到rootfs.ext3虚拟SD中

mount -t ext3 rootfs.ext3 /mnt/ -o loop
cp -r rootfs/* /mnt
umount /mnt

启动开发板,加载KO
/ # ls
bin         lib         lost+found  sbin
dev         linuxrc     modules     usr
/ # cd te
-/bin/sh: cd: can't cd to te
/ # cd modules/
/modules # ls
first.ko
/modules # insmod first.ko 
first: loading out-of-tree module taints kernel.
init hello_moudle
/modules # rmmod first.ko 
rmmod: can't change directory to '/lib/modules': No such file or directory
/lib # lsmod
lsmod: can't open '/proc/modules': No such file or directory

 

 7. 继续完善文件系统

 a. 创建etc及inittable文件,语句含义参考: https://blog.csdn.net/u014089899/article/details/80622607

sly@ubuntu:~/develop/rootfs$ mkdir etc
sly@ubuntu:~/develop/rootfs$ cd etc/
sly@ubuntu:~/develop/rootfs/etc$ sudo vi inittab

 

inittab内容如下

::sysinit:/etc/init.d/rcS
#::respawn:-/bin/sh
#tty2::askfirst:-/bin/sh
#::ctrlaltdel:/bin/umount -a -r

console::askfirst:-/bin/sh
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r

 

创建fstab,相应文件夹需要自行创建好  含义参考:https://blog.csdn.net/xyajia/article/details/79165655

sly@ubuntu:~/develop/rootfs/etc$ sudo vi fstab
# /etc/fstab: static file system information.
# 
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/sda1 during installation
proc            /proc           proc    defaults                0       0
tmpfs           /tmp            tmpfs   defaults                0       0
sysfs           /sys            sysfs   defaults                0       0
tmpfs           /dev            tmpfs   defaults                0       0
var             /dev            tmpfs   defaults                0       0
ramfs           /dev            ramfs   defaults                0       0

创建init.d/rcS

sly@ubuntu:~/develop/rootfs/etc$ sudo mkdir init.d
sly@ubuntu:~/develop/rootfs/etc$ cd init.d/
sly@ubuntu:~/develop/rootfs/etc/init.d$ sudo vi rcS

#! /bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
LD_LIBRARY_PATH=/lib
export PATH LD_LIBRARY_PATH

mount -a
mkdir -p /dev/pts
mount -t devpts devpts /dev/pts
mdev -s
mkdir -p /var/lock
echo "----------------------------------------"

echo " welcome debugging on A9 vexpress board"

echo "----------------------------------------"
sly@ubuntu:~/develop/rootfs/etc/init.d$ sudo chmod 777 rcS

用最新的rootfs覆盖rootfs.ext3虚拟SD卡,启动开发板验证,效果如下:(出错部分不知道问什么挂在到mnt后在mnt中没有可执行权限,添加下可执行权限就行

Error: Driver 'vexpress-muxfpga' is already registered, aborting...
drm-clcd-pl111 10020000.clcd: initializing Versatile Express PL111
drm-clcd-pl111 10020000.clcd: DVI muxed to daughterboard 1 (core tile) CLCD
input: AT Raw Set 2 keyboard as /devices/platform/smb@4000000/smb@4000000:motherboard/smb@4000000:motherboard:iofpga@7,00000000/10006000.kmi/serio0/input/input0
mmc0: new SD card at address 4567
Error: Driver 'vexpress-muxfpga' is already registered, aborting...
drm-clcd-pl111 10020000.clcd: initializing Versatile Express PL111
drm-clcd-pl111 10020000.clcd: DVI muxed to daughterboard 1 (core tile) CLCD
mmcblk0: mmc0:4567 QEMU! 32.0 MiB 
Error: Driver 'vexpress-muxfpga' is already registered, aborting...
drm-clcd-pl111 10020000.clcd: initializing Versatile Express PL111
drm-clcd-pl111 10020000.clcd: DVI muxed to daughterboard 1 (core tile) CLCD
Error: Driver 'vexpress-muxfpga' is already registered, aborting...
drm-clcd-pl111 10020000.clcd: initializing Versatile Express PL111
drm-clcd-pl111 10020000.clcd: DVI muxed to daughterboard 1 (core tile) CLCD
rtc-pl031 10017000.rtc: setting system clock to 2019-12-24T05:46:23 UTC (1577166383)
ALSA device list:
  #0: ARM AC'97 Interface PL041 rev0 at 0x10004000, irq 24
input: ImExPS/2 Generic Explorer Mouse as /devices/platform/smb@4000000/smb@4000000:motherboard/smb@4000000:motherboard:iofpga@7,00000000/10007000.kmi/serio1/input/input2
Error: Driver 'vexpress-muxfpga' is already registered, aborting...
drm-clcd-pl111 10020000.clcd: initializing Versatile Express PL111
drm-clcd-pl111 10020000.clcd: DVI muxed to daughterboard 1 (core tile) CLCD
EXT4-fs (mmcblk0): mounting ext3 file system using the ext4 subsystem
random: fast init done
EXT4-fs (mmcblk0): mounted filesystem with ordered data mode. Opts: (null)
VFS: Mounted root (ext3 filesystem) readonly on device 179:0.
Freeing unused kernel memory: 1024K
Run /sbin/init as init process
random: crng init done
can't run '/etc/init.d/rcS': Permission denied

Please press Enter to activate this console. 

/ # ls
bin etc linuxrc proc sys usr
dev lib modules sbin tmp var

 进行insmod和rmmod操作,发现还需要创建几个文件夹,根据提示在lib下创建lib/modules和lib/modules/5.3.7,就此告一段落

/modules # ls
first.ko  hello
/modules # cat hello 
hello
/modules # vi hello 
/modules # 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!