下载安装qemu

#!/bin/bash

out='> /dev/null'
# out=

echo ${HOME}

gcc -v 
if [[ $? -ne 0 ]]; then
    sudo apt update ${out}
    sudo apt upgrade 
    eval sudo apt-get install -y make gcc g++ gdb ${out}
fi

ninja --version

if [[ $? -ne 0 ]]; then
    cd
    if [[ ! -d ${HOME}/ninja ]]; then
        git --version
        if [[ $? -ne 0 ]]; then
            eval sudo apt-get install -y git ${out}
        fi
        git clone https://gitee.com/gitmirror/ninja.git
        if [[ $? -ne 0 ]]; then
            echo " git clone ninja fail"
            exit 1
        fi
        eval sudo apt-get install -y re2c ${out}
    fi
    cd ${HOME}/ninja
    python3 ./configure.py --bootstrap  
    sudo cp ./ninja  /usr/bin
    # 测试
    ninja --version

    if [[ $? -ne 0 ]];then
    echo " ninja install fail"
        exit 1
    fi 
fi

cd
if [[ ! -d ${HOME}/qemu7 ]]; then
    wget https://download.qemu.org/qemu-7.0.0.tar.xz
    if [[ $? -ne 0 ]];then
        echo " qemu download fail"
        exit 1
    fi 
    # 安装依赖包
    eval sudo apt-get install -y build-essential zlib1g-dev pkg-config libglib2.0-dev ${out}
    eval sudo apt-get install -y binutils-dev libboost-all-dev autoconf libtool libssl-dev libpixman-1-dev ${out}
    # 支持enable-virtfs 共享文件
    eval sudo apt-get install -y libcap-ng-dev libattr1-dev ${out}
    tar xJf qemu-7.0.0.tar.xz
    mv qemu-7.0.0 qemu7
fi

${HOME}/qemu7/build/qemu-system-aarch64 -version

if [[ $? -ne 0 ]]; then
    echo "qemu not compile"
    cd ${HOME}/qemu7
    # arm64 编译
   ./configure --target-list=aarch64-softmmu --enable-debug --enable-debug-info  --enable-kvm  \
        --enable-trace-backends=simple --enable-virtfs
    if [[ $? -ne 0 ]];then
        echo " qemu configure fail"
        exit 1
    fi
    # 开始编译
    eval make -j$(nproc) ${out}
    if [[ $? -ne 0 ]];then
        echo " qemu make fail"
        exit 1
    fi
    ${HOME}/qemu7/build/qemu-system-aarch64 -version
fi

cd

测试环境

查看是否支持KVM

参考:

对于aarch64(arm64)架构,若支持KVM虚拟化,那么KVM代码会直接编译进内核

2020年3月,Linux 5.7 Kernel宣布将放弃支持 32位架构的 KVM虚拟化支持,所以目前来看,要想较好的在ARM架构上运行KVM虚拟化,需要使用现代化的64位ARM架构

# 如果/dev/kvm和/sys/module/kvm二者之一不存在说明KVM虚拟化是不支持的
ls -l /dev/kvm
ls -l /sys/module/kvm

cat /boot/config-`uname -r` | grep VIRTUAL
CONFIG_VIRTUALIZATION=y
# CONFIG_GKI_HIDDEN_VIRTUAL_CONFIGS is not set
# CONFIG_REGULATOR_VIRTUAL_CONSUMER is not set
CONFIG_FB_VIRTUAL=y
CONFIG_ARCH_HAS_DEBUG_VIRTUAL=y
# CONFIG_DEBUG_VIRTUAL is not set

机器配置

uname -a
Linux firefly 5.10.110 #217 SMP Wed Oct 12 17:59:18 CST 2022 aarch64 aarch64 aarch64 GNU/Linux

free -h
              total        used        free      shared  buff/cache   available
Mem:          3.4Gi       2.7Gi       295Mi       115Mi       445Mi       590Mi
Swap:            0B          0B          0B

lscpu
Architecture:                    aarch64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
CPU(s):                          8
On-line CPU(s) list:             0-7
Thread(s) per core:              1
Core(s) per socket:              2
Socket(s):                       3
Vendor ID:                       ARM
Model:                           0
Model name:                      Cortex-A55
Stepping:                        r2p0
Flags:                           fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrc
                                 pc dcpop asimddp

安装ubuntu

# 创建镜像 
${HOME}/qemu7/build/qemu-img create -f qcow2 ${HOME}/ubuntu.img 60G

sudo ${HOME}/qemu7/build/qemu-system-aarch64 \
  -m 1G -cpu host -smp 2 -M virt -enable-kvm\
  -bios ${HOME}/qemu7/build/pc-bios/edk2-aarch64-code.fd \
  -drive if=none,file=${HOME}/ubuntu-18.04-server-arm64.iso,id=cdrom,media=cdrom \
  -device virtio-scsi-device -device scsi-cd,drive=cdrom \
  -drive if=none,file=${HOME}/ubuntu.img,id=hd0 \
  -device virtio-blk-device,drive=hd0 \
  -vnc :1 \
  -monitor stdio

在这里插入图片描述
安装成功后

# 通过vnc 启动,并开启监控模式
sudo ${HOME}/qemu7/build/qemu-system-aarch64 \
  -m 1G -cpu host -smp 2 -M virt -enable-kvm\
  -bios ${HOME}/qemu7/build/pc-bios/edk2-aarch64-code.fd \
  -drive if=none,file=${HOME}/ubuntu.img,id=hd0 \
  -device virtio-blk-device,drive=hd0 \
  -vnc :1 \
  -monitor stdio

# 通过vnc 启动,不需要监控模式
sudo ${HOME}/qemu7/build/qemu-system-aarch64 \
  -m 1G -cpu host -smp 2 -M virt -enable-kvm\
  -bios ${HOME}/qemu7/build/pc-bios/edk2-aarch64-code.fd \
  -drive if=none,file=${HOME}/ubuntu.img,id=hd0 \
  -device virtio-blk-device,drive=hd0 \
  -vnc :1 \
  -monitor none

在这里插入图片描述

记录

运行qemu

参考

ubuntu-22.04.1-live-server-arm64.iso 下载

验证机器是否支持kvm
# 用户添加到多个次要组中
usermod -a -G kvm ostest

# 创建虚拟机硬盘
${HOME}/qemu7/build/qemu-img create -f qcow2 ${HOME}/disk/ubuntu.img 100G

sudo ${HOME}/qemu7/build/qemu-system-aarch64 \
     -m 2048M -smp 4 -M virt -enable-kvm  \
     -boot order=dc \
     -drive file=${HOME}/disk/ubuntu.img,index=0,media=disk,format=qcow2  \
     -cdrom ${HOME}ubuntu-22.04.1-live-server-arm64.iso  \
     -vnc :1
qemu-system-aarch64: KVM is not supported for this guest CPU type
qemu-system-aarch64: kvm_init_vcpu: kvm_arch_init_vcpu failed (0): Invalid argument

qemu-system-aarch64: The 'host' CPU type can only be used with KVM or HVF

# qemu 文档
-cpu model
	Select CPU model (-cpu help for list and additional feature selection)

sudo ${HOME}/qemu7/build/qemu-system-aarch64 \
    -M virt \
    -m 2048M -smp 4 \
    -cpu host \
    -enable-kvm  \
    -boot order=dc \
    -drive file=${HOME}/disk/ubuntu.img,index=0,media=disk,format=qcow2  \
    -cdrom ${HOME}/ubuntu-22.04.1-live-server-arm64.iso  \
    -vnc :1
qemu-system-aarch64: failed to find romfile "efi-virtio.rom"

# 查看qemu 文档
-L  path
	Set the directory for the BIOS, VGA BIOS and keymaps.
	To list all the data directories, use -L help.

sudo ${HOME}/qemu7/build/qemu-system-aarch64 \
    -M virt \
    -m 2048M -smp 4 \
    -cpu host \
    -enable-kvm  \
    -L /home/ostest/qemu7/pc-bios/ \
    -boot order=dc \
    -drive file=${HOME}/disk/ubuntu.img,index=0,media=disk,format=qcow2  \
    -cdrom ${HOME}/ubuntu-22.04.1-live-server-arm64.iso  \
    -vnc :1

在这里插入图片描述

在这里插入图片描述

-M
ostest@firefly:~$ ${HOME}/qemu7/build/qemu-system-aarch64  -M help | grep a7
ostest@firefly:~$ ${HOME}/qemu7/build/qemu-system-aarch64  -M help | grep A7
ast2600-evb          Aspeed AST2600 EVB (Cortex-A7)
bletchley-bmc        Facebook Bletchley BMC (Cortex-A7)
fuji-bmc             Facebook Fuji BMC (Cortex-A7)
mcimx6ul-evk         Freescale i.MX6UL Evaluation Kit (Cortex-A7)
mcimx7d-sabre        Freescale i.MX7 DUAL SABRE (Cortex-A7)
orangepi-pc          Orange Pi PC (Cortex-A7)
rainier-bmc          IBM Rainier BMC (Cortex-A7)
tacoma-bmc           OpenPOWER Tacoma BMC (Cortex-A7)
ostest@firefly:~$ ${HOME}/qemu7/build/qemu-system-aarch64  -M help | grep A5
xlnx-zcu102          Xilinx ZynqMP ZCU102 board with 4xA53s and 2xR5Fs based on the value of smp
ostest@firefly:~$ ${HOME}/qemu7/build/qemu-system-aarch64  -M help | grep a5
ostest@firefly:~$                                                                                                  

测试记录

ostest@firefly:~$ sudo ${HOME}/qemu7/build/qemu-system-aarch64 \
>     -M ast2600-evb \
>     -m 1G -smp 2 \
>     -cpu host \
>     --enable-kvm  \
>     -bios ${HOME}/QEMU_EFI.fd \
>     -drive if=none,file=${HOME}/ubuntu-22.04.1-live-server-arm64.iso,id=cdrom,media=cdrom\
>     -drive file=${HOME}/disk/ubuntu.img,index=1,media=disk,format=qcow2,id=ubuntuhd \
>     -vnc :1 \
>     -monitor none
Unexpected error in arm_cpu_realizefn() at ../target/arm/cpu.c:1471:
qemu-system-aarch64: Cannot enable KVM when guest CPU has EL3 enabled
Aborted

ostest@firefly:~$ sudo ${HOME}/qemu7/build/qemu-system-aarch64 \
>     -M orangepi-pc   \
>     -m 1G -smp 4 \
>     -cpu host \
>     --enable-kvm  \
>     -bios ${HOME}/QEMU_EFI.fd \
>     -drive if=none,file=${HOME}/ubuntu-22.04.1-live-server-arm64.iso,id=cdrom,media=cdrom\
>     -drive file=${HOME}/disk/ubuntu.img,index=1,media=disk,format=qcow2,id=ubuntuhd \
>     -vnc :1 \
>     -monitor none
qemu-system-aarch64: BIOS not supported for this machine

ostest@firefly:~$ sudo ${HOME}/qemu7/build/qemu-system-aarch64 \
>     -M orangepi-pc   \
>     -m 1G -smp 4 \
>     -cpu cortex-a7 \
>     --enable-kvm  \
>     -drive if=none,file=${HOME}/ubuntu-22.04.1-live-server-arm64.iso,id=cdrom,media=cdrom\
>     -drive file=${HOME}/disk/ubuntu.img,index=1,media=disk,format=qcow2,id=ubuntuhd \
>     -vnc :1 \
>     -monitor none
qemu-system-aarch64: Cannot enable KVM when guest CPU has EL3 enabled
安装ubuntu

参考

在arm64上启动qemu虚拟机有两种方式,一种是通过-kernel 的方式boot kernel,另一种是先启动uefi再boot kernel。

QEMU默认将会采用seabios的启动方式

# 下载 QEMU_EFI.fd
wget http://releases.linaro.org/components/kernel/uefi-linaro/16.02/release/qemu64/QEMU_EFI.fd

在这里插入图片描述

参考通过qemu-system-aarch64在x86上安装aarch64虚拟机

sudo ${HOME}/qemu7/build/qemu-system-aarch64 \
    -M virt \
    -m 2G -smp 6 \
    -cpu cortex-a57 \
    -bios ${HOME}/QEMU_EFI.fd \
    -drive if=none,file=${HOME}/ubuntu-22.04.1-live-server-arm64.iso,id=cdrom,media=cdrom \
    -device virtio-scsi-device -device scsi-cd,drive=cdrom \
    -drive if=none,file=${HOME}/disk/ubuntu.img,id=hd0 \
    -vnc :1 \
   	-monitor stdio
   
# 不知道为啥这个monitor 重定向到 vnc里去了

# qemu 文档
-monitor dev
	Redirect the monitor to host device dev (same devices as the serial port). 
	The default device is vc in graphical mode and stdio in non graphical mode. 
	Use -monitor none to disable the default monitor.

在这里插入图片描述
带上kvm 选项


sudo ${HOME}/qemu7/build/qemu-system-aarch64 \
    -M virt \
    -m 2G -smp 6 \
    -cpu host \
    -enable-kvm  \
    -bios ${HOME}/QEMU_EFI.fd \
    -drive if=none,file=${HOME}/ubuntu-22.04.1-live-server-arm64.iso,id=cdrom,media=cdrom \
    -device virtio-scsi-device -device scsi-cd,drive=cdrom \
    -drive if=none,file=${HOME}/disk/ubuntu.img,id=hd0 \
    -vnc :1 \
    -monitor stdio

在这里插入图片描述

改用ubuntu 18.04
ostest@firefly:~/disk$ sudo ${HOME}/qemu7/build/qemu-system-aarch64 \
    -m 2048 -cpu host -smp 2 -M virt -enable-kvm\
    -bios ${HOME}/QEMU_EFI.fd \
   -nographic \
     -drive if=none,file=${HOME}/disk/ubuntu-18.04-server-arm64.iso,id=cdrom,media=cdrom \
     -device virtio-scsi-device -device scsi-cd,drive=cdrom \
     -drive if=none,file=${HOME}/disk/ubuntu.img,id=hd0 \
     -device virtio-blk-device,drive=hd0
[=3h
[=3h
[=3h
[=3h
[=3h
[=3h
Killed
问题

在arm机器上安装安装

参考 virt-manage 使用 通过,virt-manage 安装成功后,然后
使用virt-manage 的UEFI 固件成功安装

sudo ${HOME}/qemu7/build/qemu-system-aarch64 \
    -m 1G -cpu host -smp 2 -M virt -enable-kvm\
    -bios /usr/share/AAVMF/AAVMF_CODE.fd \
    -nographic \
    -drive if=none,file=${HOME}/disk/ubuntu-18.04-server-arm64.iso,id=cdrom,media=cdrom \
    -device virtio-scsi-device -device scsi-cd,drive=cdrom \
    -drive if=none,file=${HOME}/disk/ubuntu.img,id=hd0 \
    -device virtio-blk-device,drive=hd0

安装成功后

sudo ${HOME}/qemu7/build/qemu-system-aarch64 \
    -m 1G -cpu host -smp 2 -M virt -enable-kvm\
    -bios /usr/share/AAVMF/AAVMF_CODE.fd \
    -drive if=none,file=${HOME}/disk/ubuntu.img,id=hd0 \
    -device virtio-blk-device,drive=hd0 \
    -nographic

在这里插入图片描述
在这里插入图片描述

后面查看了官网项目一些文档资料 , qemu 项目里面有UEFI固件
在这里插入图片描述

sudo ${HOME}/qemu7/build/qemu-system-aarch64 \
  -m 1G -cpu host -smp 2 -M virt -enable-kvm\
  -bios ${HOME}/qemu7/build/pc-bios/edk2-aarch64-code.fd \
  -nographic \
  -drive if=none,file=${HOME}/disk/ubuntu-18.04-server-arm64.iso,id=cdrom,media=cdrom \
  -device virtio-scsi-device -device scsi-cd,drive=cdrom \
  -drive if=none,file=${HOME}/disk/ubuntu1.img,id=hd0 \
  -device virtio-blk-device,drive=hd0

在这里插入图片描述

UEFI

参考详写 UEFI & BIOS 安装 Arch Linux

ostest@ubuntu:~$ ls /sys/firmware/efi/efivars
Boot0000-8be4df61-93ca-11d2-aa0d-00e098032b8c
Boot0001-8be4df61-93ca-11d2-aa0d-00e098032b8c
Boot0002-8be4df61-93ca-11d2-aa0d-00e098032b8c
Boot0003-8be4df61-93ca-11d2-aa0d-00e098032b8c
Boot0004-8be4df61-93ca-11d2-aa0d-00e098032b8c
Boot0005-8be4df61-93ca-11d2-aa0d-00e098032b8c
Boot0006-8be4df61-93ca-11d2-aa0d-00e098032b8c
Boot0007-8be4df61-93ca-11d2-aa0d-00e098032b8c
BootCurrent-8be4df61-93ca-11d2-aa0d-00e098032b8c
BootOptionSupport-8be4df61-93ca-11d2-aa0d-00e098032b8c
BootOrder-8be4df61-93ca-11d2-aa0d-00e098032b8c
certdb-d9bee56e-75dc-49d9-b4d7-b534210f637a
certdbv-d9bee56e-75dc-49d9-b4d7-b534210f637a
ConIn-8be4df61-93ca-11d2-aa0d-00e098032b8c
ConInDev-8be4df61-93ca-11d2-aa0d-00e098032b8c
ConOut-8be4df61-93ca-11d2-aa0d-00e098032b8c
ConOutDev-8be4df61-93ca-11d2-aa0d-00e098032b8c
ErrOut-8be4df61-93ca-11d2-aa0d-00e098032b8c
ErrOutDev-8be4df61-93ca-11d2-aa0d-00e098032b8c
Key0000-8be4df61-93ca-11d2-aa0d-00e098032b8c
Key0001-8be4df61-93ca-11d2-aa0d-00e098032b8c
Lang-8be4df61-93ca-11d2-aa0d-00e098032b8c
LangCodes-8be4df61-93ca-11d2-aa0d-00e098032b8c
MTC-eb704011-1402-11d3-8e77-00a0c969723b
OsIndicationsSupported-8be4df61-93ca-11d2-aa0d-00e098032b8c
PlatformLang-8be4df61-93ca-11d2-aa0d-00e098032b8c
PlatformLangCodes-8be4df61-93ca-11d2-aa0d-00e098032b8c
PlatformRecovery0000-8be4df61-93ca-11d2-aa0d-00e098032b8c
SecureBoot-8be4df61-93ca-11d2-aa0d-00e098032b8c
SetupMode-8be4df61-93ca-11d2-aa0d-00e098032b8c
SignatureSupport-8be4df61-93ca-11d2-aa0d-00e098032b8c
Timeout-8be4df61-93ca-11d2-aa0d-00e098032b8c
VarErrorFlag-04b37fe8-f6ae-480b-bdd5-37d98c5e89aa
VendorKeys-8be4df61-93ca-11d2-aa0d-00e098032b8c

内核编译

参考

Logo

更多推荐