1. 博客/

使用QEMU虚拟NVMe设备

·330 字·2 分钟
Kvm Nvme Qemu

本文主要介绍在没有NVMe磁盘的测试机器时,如何通过QEMU启动一个虚拟机并虚拟出NVMe磁盘

测试机使用的系统是CentOS7.7。yum源安装的qemu-kvm是不支持虚拟nvme设备的,因为编译时./configure 指定了参数–without-default-devices

[root@localhost ~]# /usr/libexec/qemu-kvm -device help |& grep nvme

编译qemu-5.2.0
#

[root@localhost] /data$ wget https://download.qemu.org/qemu-5.2.0.tar.xz
[root@localhost] /data$ tar xf qemu-5.2.0.tar.xz
[root@localhost] /data$ yum install gcc gcc-c++ python3 meson glib2-devel pixman-devel zlib-devel bzip2 bzip2-devel libuuid-devel spice-protocol spice-server-devel usbredir-devel libaio-devel libcurl-devel -y -q

[root@localhost] /data$ cd qemu-5.2.0
[root@localhost] /data/qemu-5.2.0$ ./configure --enable-kvm --target-list=x86_64-softmmu --enable-debug  --cpu=x86_64
[root@localhost] /data/qemu-5.2.0$ make -j 4
[root@localhost] /data/qemu-5.2.0$ make DESTDIR=/data0/qemu-5.2.0-build install

编译的二进制文件已支持虚拟化nvme设备

[root@localhost] /data/qemu-5.2.0$ cd /data0/qemu-5.2.0-build/
[root@localhost] /data0/qemu-5.2.0-build$ ./usr/local/bin/qemu-system-x86_64 -device help |& grep nvme
name "nvme", bus PCI, desc "Non-Volatile Memory Express"
name "nvme-ns", bus nvme-bus, desc "Virtual NVMe namespace"

替换测试机上老版本qemu-kvm

[root@localhost] ~$ cd /usr/libexec/
[root@localhost] /usr/libexec/$ mv qemu-kvm qemu-kvm-bak
[root@localhost] /usr/libexec/$ ln -sf /usr/local/bin/qemu-system-x86_64 qemu-kvm
[root@localhost] /usr/libexec/$ ./qemu-kvm --version
QEMU emulator version 5.2.0
Copyright (c) 2003-2020 Fabrice Bellard and the QEMU Project developers

启动虚拟机
#

本文使用virt-install工具启动

–network bridge=bridge0 测试机已创建好bridge0设备

–disk path=img-nvme-em.qcow2 已创建好的Centos7 base镜像

–qemu-commandline 把libvirt不支持的参数透传给QEMU虚拟化引擎

[root@localhost] /data/nesting/nvme-em$ qemu-img create -f raw nvme.raw 100G
[root@localhost] /data/nesting/nvme-em$ virt-install \
    --virt-type=kvm \
    --name=nvme-em \
    --vcpus 2 \
    --memory=4096 \
    --disk path=img-nvme-em.qcow2,size=50,format=qcow2,cache=none,bus='virtio' \
    --network bridge=bridge0,model='virtio',driver_name='vhost',driver_queues="2" \
    --nographics \
    --qemu-commandline="-drive file=/data/nesting/nvme-em/nvme.raw,if=none,id=nvm,format=raw" \
    --qemu-commandline="-device nvme,drive=nvm,serial=1234" \
    --print-xml  >   nvme-em.xml
[root@localhost] /data/nesting/nvme-em$ virsh define nvme-em.xml
[root@localhost] /data/nesting/nvme-em$ virsh start nvme-em

登录虚拟机
#

登录到虚拟机已经可以看到NVMe磁盘

[root@localhost] /data/nesting/nvme-em$ virsh console nvme-em

[root@nestate] ~$ lspci -nn|grep -i NVM
00:07.0 Non-Volatile memory controller [0108]: Red Hat, Inc. QEMU NVM Express Controller [1b36:0010] (rev 02)

[root@nestate] ~$ lspci -Dvs 00:07
0000:00:07.0 Non-Volatile memory controller: Red Hat, Inc. QEMU NVM Express Controller (rev 02) (prog-if 02 [NVM Express])
	Subsystem: Red Hat, Inc. Device 1100
	Physical Slot: 7
	Flags: bus master, fast devsel, latency 0, IRQ 11, NUMA node 0
	Memory at febc0000 (64-bit, non-prefetchable) [size=8K]
	Memory at febc6000 (32-bit, non-prefetchable) [size=4K]
	Capabilities: [40] MSI-X: Enable+ Count=65 Masked-
	Capabilities: [80] Express Endpoint, MSI 00
	Kernel driver in use: nvme
	Kernel modules: nvme
	
[root@nestate] ~$ nvme list
Node                  Generic               SN                   Model                                    Namespace  Usage                      Format           FW Rev
--------------------- --------------------- -------------------- ---------------------------------------- ---------- -------------------------- ---------------- --------
/dev/nvme0n1          /dev/ng0n1            1234                 QEMU NVMe Ctrl                           0x1        107.37  GB / 107.37  GB    512   B +  0 B   1.0

Related

httpd+svn服务器部署
·1765 字·9 分钟
Svn Httpd Openssl
kvm虚拟机磁盘扩容
·2036 字·10 分钟
Kvm Linux WindowsServer