QEMU + KVM 搭建ARM64 Linux开发环境

QEMU + KVM 搭建ARM64 Linux开发环境,第1张

QEMU + KVM 搭建ARM64 Linux开发环境 参考方法QEMU介绍(1) 用户模式(User Mode)(2)系统模式(System Mode) 3 QEMU快速使用

参考方法

QEMU搭建arm64 Linux调试环境
gdb 调试 Linux 内核网络源码
编译Linux内核镜像和dtb文件
QEMU!用它模拟开发板能替代真开发板

QEMU介绍

QEMU 是一款开源的模拟器(官网:https://www.qemu.org/),它能够模拟 Arm、MIPS、RISC-V 等各种 CPU 和开发板,以及网卡、声卡、键盘、sdcard、emmc、usb等各种外设。可以把它当作一块召之即来的开发板,在上面运行 U-Boot、Linux Kernel、甚至 Ubuntu 等各种软件和 *** 作系统。
QEMU有两种模式:

(1) 用户模式(User Mode)

一个使用arm-xxx-gcc编译出来的程序,是给ARM板子使用的,它无法在PC机上运行,只能放到ARM板子上去运行。
借助qemu,可以在PC机上运行ARM程序。比如:

$ gcc -o hello hello.c -static
$ ./hello     #这个hello程序是使用gcc给PC机编译的,可以直接运行
Hello, world!
$ arm-linux-gnueabihf-gcc -o hello hello.c -static  #它是给ARM板子编译的
$ ./hello                                     #所以无法在PC上运行
bash: ./hello: cannot execute binary file: Exec format error
$ ./qemu-arm ./hello                     #我们可以用QEMU在PC上运行它
Hello, world!

在PC上使用qemu运行单个ARM程序时,这就是使用QEMU的用户模式。
它会把ARM指令翻译为PC的指令去运行。
你根据下章《QEMU快速使用》安装QEMU后,就可以进行上述实验了。

(2)系统模式(System Mode)

若想模拟出整个ARM单板:在这个模拟出来的虚拟ARM单板上,运行Linux系统,在其中运行各种APP。这时需要使用QEMU的系统模式。

3 QEMU快速使用

Install and Run QEMU on Linux hosts
QEMU aarch64 virt
Minimal, pure and up-to-date vanilla Debian/Ubuntu Linux SD card image for QEMU aarch64 virt.

在Ubuntu上可使用apt-get安装QEMU,但是往往版本较低。所以,可采用从源码编译的方式进行安装
3.1 从源码安装QEMU

#在Ubuntu中执行如下命令,初次使用GIT时,需要配置配置GIT的邮箱帐号和用户名,可以随意指定???。
$ git config --global user.email "[email protected]"
$ git config --global user.name "Your Name"
# 获取QEMU镜像
$ git clone https://gitlab.com/qemu-project/qemu.git
$ cd qemu
$ git submodule init
$ git submodule update --recursive
# 如果只需要安装一种平台的QEMU,可以通过configure配置如下:
$ ./configure –target-list=aarch64-softmmu
$ ./configure
$ make
# 如果使用GIT命令失败,有如下提示时:
# fatal: Out of memory, malloc failed (tried to allocate 314572801 bytes)
# fatal: index-pack failed
# 先执行下列命令增加swap分区:
sudo dd if=/dev/zero of=/root/myswapfile bs=1M count=1024
sudo chmod 600 /root/myswapfile
sudo mkswap /root/myswapfile
sudo swapon /root/myswapfile
# 然后修改/etc/fstab,增加如下一行:
/root/myswapfile  swap   swap    defaults  0 0
# 最后重新执行上面的“git clone”命令。

3.2 运行QEMU

进入QEMU的目录,执行下列命令。

# boots a PC BIOS.
$ qemu-system-x86_64 -L pc-bios
# Create a disk for the VM
./qemu-img create -f qcow2 test.qcow2 16G
# Download an install ISO - I have Fedora 20
ls -la Fedora-Live-Desktop-x86_64-20-1.iso
-rwxr-xr-x. 1 xxxxx xxxxx 999292928 May  4 16:32
# Run QEMU with KVM enabled (w/o VNC):If you have gtk2-devel installed, this will launch a simple UI and you can install your OS.
x86_64-softmmu/qemu-system-x86_64 -m 1024 -enable-kvm \
-drive if=virtio,file=test.qcow2,cache=none \
-cdrom Fedora-Live-Desktop-x86_64-20-1.iso

运行qemu-system-arm程序,参数说明

-M mcimx6ul-evk                指定需要模拟的单板型号。
-m 512M                       指定板子的内存大小。
-kernel zImage                  指定使用的内核镜像文件。
-dtb   100ask_imx6ull_qemu.dtb  指定使用的设备树文件。
-display sdl                      指定使用那种图形显示输出。
-serial mon:stdio                  指定串口信息输出。
-drive file=rootfs.img,format=raw,id=mysdcard 名为mysdcard的drive,源为rootfs.img
-device sd-card,drive=mysdcard 添加一个sd-card设备,内容来自名为mysdcard的drive
-append “console=ttymxc0,115200 rootfstype=ext4 root=/dev/mmcblk1 rw rootwait init=/sbin/init loglevel=8”   指定内核的命令行参数
-nic user                 指定网卡为user mode

有了内核zImage、设备树、文件系统(rootfs.img),这就是一个完整的Linux系统。

3.3 快捷键
如果在终端下要退出QEMU,可以同时按住ctrl+a,松开后再输入’x’。
如果点击QEMU的GUI窗口后无法移出鼠标,可以同时按住ctrl+a,松开后再输入’g’。

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

原文地址: http://outofmemory.cn/yw/928661.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-17
下一篇 2022-05-17

发表评论

登录后才能评论

评论列表(0条)

保存