Change The World

沉默是一种力量,嘶吼就不是力量了?

0%

QEMU虚拟机支持UEFI

本文主要出现的原因其实很简单,就是让目前创建的虚拟机支持UEFI引导。话不多说开始吧😳

环境信息

1
2
3
4
[root@dev ~]# cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)
[root@dev ~]# uname -a
Linux dev 2.6.78-147.8.1.el8_1.x86_64 #1 SMP Thu Apr 9 13:49:54 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

查看当前主机Qemu支持机器类型

可以通过以下两种查询方法,或其他有效的方法

  1. 通过qemu-kvm进行查询

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    [root@dev ~]# /usr/libexec/qemu-kvm -M help
    Supported machines are:
    pc RHEL 7.6.0 PC (i440FX + PIIX, 1996) (alias of pc-i440fx-rhel7.6.0)
    pc-i440fx-rhel7.6.0 RHEL 7.6.0 PC (i440FX + PIIX, 1996) (default)
    pc-i440fx-rhel7.5.0 RHEL 7.5.0 PC (i440FX + PIIX, 1996)
    pc-i440fx-rhel7.4.0 RHEL 7.4.0 PC (i440FX + PIIX, 1996)
    pc-i440fx-rhel7.3.0 RHEL 7.3.0 PC (i440FX + PIIX, 1996)
    pc-i440fx-rhel7.2.0 RHEL 7.2.0 PC (i440FX + PIIX, 1996)
    pc-i440fx-rhel7.1.0 RHEL 7.1.0 PC (i440FX + PIIX, 1996)
    pc-i440fx-rhel7.0.0 RHEL 7.0.0 PC (i440FX + PIIX, 1996)
    q35 RHEL-7.6.0 PC (Q35 + ICH9, 2009) (alias of pc-q35-rhel7.6.0)
    pc-q35-rhel7.6.0 RHEL-7.6.0 PC (Q35 + ICH9, 2009)
    pc-q35-rhel7.5.0 RHEL-7.5.0 PC (Q35 + ICH9, 2009)
    pc-q35-rhel7.4.0 RHEL-7.4.0 PC (Q35 + ICH9, 2009)
    pc-q35-rhel7.3.0 RHEL-7.3.0 PC (Q35 + ICH9, 2009)
    none empty machine
  2. 通过libvirt进行查询

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    [root@dev ~]# virsh capabilities
    <capabilities>
    ...
    <guest>
    ...
    <machine maxCpus='240'>pc-i440fx-rhel7.6.0</machine>
    <machine canonical='pc-i440fx-rhel7.6.0' maxCpus='240'>pc</machine>
    <machine maxCpus='240'>pc-i440fx-rhel7.0.0</machine>
    <machine maxCpus='384'>pc-q35-rhel7.6.0</machine>
    <machine canonical='pc-q35-rhel7.6.0' maxCpus='384'>q35</machine>
    <machine maxCpus='240'>pc-i440fx-rhel7.5.0</machine>
    <machine maxCpus='240'>pc-i440fx-rhel7.1.0</machine>
    <machine maxCpus='240'>pc-i440fx-rhel7.2.0</machine>
    <machine maxCpus='255'>pc-q35-rhel7.3.0</machine>
    <machine maxCpus='384'>pc-q35-rhel7.4.0</machine>
    <machine maxCpus='240'>pc-i440fx-rhel7.3.0</machine>
    <machine maxCpus='240'>pc-i440fx-rhel7.4.0</machine>
    <machine maxCpus='384'>pc-q35-rhel7.5.0</machine>
    ...
    </guest>
    ...
    </capabilities>

通过以上内容可以看出,如果我们只是需要使用最新的机器类型只要选择 pcq35

配置虚拟机使用Q35

1
2
3
4
5
6
[root@dev ~]# virsh edit --domain alpinelinux3.8
...
<os>
<type arch='x86_64' machine='q35'>hvm</type>
</os>
...

将以上内容调整为 machine='q35'即为完成

配置虚拟机支持UEFI

如果需要虚拟机支持 uefi 还需要 edk2-ovmf 软件包的支持

  1. 安装edk2-ovmf软件包
1
2
3
4
5
6
[root@dev ~]# yum search edk2-ovmf
FATAL: kernel too old
上次元数据过期检查:2:35:19 前,执行于 2020年07月28日 星期二 13时01分20秒。
===================================== 名称 精准匹配:edk2-ovmf ===================================================
edk2-ovmf.noarch : UEFI firmware for x86_64 virtual machines
[root@dev ~]# yum install -y edk2-ovmf
  1. 编辑虚拟机配置
1
2
3
4
5
6
7
8
9
10
11
[root@ocloud-dev ~]# virsh edit alpinelinux3.8
<os>
...
<loader readonly='yes' secure='yes' type='pflash'>/usr/share/OVMF/OVMF_CODE.secboot.fd</loader>
...
</os>
<features>
...
<smm state='on'/>
...
</features>

找到以上节点添加对应的内容即可

注意:如果loader节点属性secure='yes’则必须开启smm

参考

  1. A New Chipset For Qemu - Intel’s Q35
  2. Features/Q35
  3. UEFI支持
  4. OVMF
不如请我吃根冰棒吧