11 KVM虚拟机配置-配置虚拟设备(存储)

11.1 存储设备概述

虚拟机XML配置文件使用devices元素配置虚拟设备,包括存储设备、网络设备、总线、鼠标等。

XML配置文件可以配置虚拟存储设备信息,包括软盘、磁盘、光盘等存储介质及其存储类型等信息,本节介绍存储设备的配置方法。

11.2 存储设备元素介绍

XML配置文件使用disk元素配置存储设备,disk常见的属性如表1所示,常见子元素及子元素属性如表2所示。

表 1 元素disk的常用属性

元素 属性 含义 属性值及其含义
disk type 指定后端存储介质类型 block:块设备、file:文件设备、dir: 目录路径、network:网络磁盘
device 指定呈现给虚拟机的存储介质 disk:磁盘(默认)、floppy:软盘、cdrom:光盘

表 2 元素disk的常用子元素及属性说明

子元素 子元素含义 属性说明
source 指定后端存储介质,与disk元素的属性“type”指定类型相对应 · file:对应file类型,值为对应文件的完全限定路径。· dev:对应block类型,值为对应主机设备的完全限定路径。· dir:对应dir类型,值为用作磁盘目录的完全限定路径。· protocol:使用的协议。· name:rbd磁盘名称,格式为:$pool/$volume· host name:mon地址· port:mon地址的端口
driver 指定后端驱动的详细信息 · type:磁盘格式的类型,常用的有“raw”和“qcow2”,需要与source的格式一致。· io:磁盘IO模式,支持“native”和“threads”选项。· cache:磁盘的cache模式,可选项有“none”、“writethrough”、“writeback”、“directsync”等。· iothread:指定为磁盘分配的IO线程。· error_policy:IO写错误发生时的处理策略,可选项有“stop”、“report”、“ignore”、“enospace"、"retry"等。· rerror_policy:IO读错误发生时的处理策略,可选项有“stop”、“report”、“ignore”、“enospac”、“retry"等。· retry_interval:IO错误重试间隔,范围为0-MAX_INT,单位为毫秒,仅error_policy=“retry”或rerror_policy=“retry”时可配置。· retry_timeout:IO错误重试超时时间,范围为0-MAX_INT,单位为毫秒,仅error_policy=“retry”或rerror_policy=“retry”时可配置。
target 指磁盘呈现给虚拟机的总线和设备 · dev:指定磁盘的逻辑设备名称,如SCSI、SATA、USB类型总线常用命令习惯为sd[a-p],IDE类型设备磁盘常用命名习惯为hd[a-d]。· bus:指定磁盘设备的类型,常见的有“scsi”、“usb”、“sata”、“virtio”等类型。
boot 表示此磁盘可以作为启动盘使用 · order:指定磁盘的启动顺序。
readonly 表示磁盘具有只读属性,磁盘内容不可以被虚拟机修改,通常与光驱结合使用 -

11.3 存储设备配置示例

按照“准备虚拟机镜像”操作完成虚拟机镜像准备后,可以使用如下XML配置文件示例,为虚拟机配置虚拟磁盘。

例如,该示例为虚拟机配置了两个IO线程,一个块磁盘设备,一个光盘设备和一个rbd磁盘,第一个IO线程分配给块磁盘设备使用。该块磁盘设备的后端介质为qcow2格式,且被作为优先启动盘。 在使用rbd磁盘前请确保已经安装qemu-block-rbd驱动,如未安装,请在root下使用如下命令进行安装:

# yum install -y qemu-block-rbd

示例命令如下:

[root@superman-21 ~]# yum install -y qemu-block-rbd
OS                                                                                          6.2 MB/s | 3.4 MB     00:00    
everything                                                                                  2.9 MB/s |  16 MB     00:05    
EPOL                                                                                        1.7 MB/s | 2.6 MB     00:01    
debuginfo                                                                                   2.7 MB/s | 3.9 MB     00:01    
source                                                                                      1.1 MB/s | 1.7 MB     00:01    
update                                                                                       11 MB/s |  33 MB     00:02    
Last metadata expiration check: 0:00:01 ago on 2023年02月14日 星期二 10时38分01秒.
Dependencies resolved.
============================================================================================================================
 Package                         Architecture            Version                              Repository               Size
============================================================================================================================
Installing:
 qemu-block-rbd                  x86_64                  10:6.2.0-60.oe2203                   update                   41 k
Installing dependencies:
 fmt                             x86_64                  8.0.1-1.oe2203                       OS                      101 k
 librados2                       x86_64                  2:16.2.7-14.oe2203                   update                  3.6 M
 librbd1                         x86_64                  2:16.2.7-14.oe2203                   update                  3.8 M
 lttng-ust                       x86_64                  2.10.1-11.oe2203                     OS                      188 k

Transaction Summary
============================================================================================================================
Install  5 Packages

Total download size: 7.7 M
Installed size: 28 M
Downloading Packages:
(1/5): fmt-8.0.1-1.oe2203.x86_64.rpm                                                        389 kB/s | 101 kB     00:00    
(2/5): lttng-ust-2.10.1-11.oe2203.x86_64.rpm                                                617 kB/s | 188 kB     00:00    
(3/5): qemu-block-rbd-6.2.0-60.oe2203.x86_64.rpm                                            755 kB/s |  41 kB     00:00    
(4/5): librados2-16.2.7-14.oe2203.x86_64.rpm                                                7.1 MB/s | 3.6 MB     00:00    
(5/5): librbd1-16.2.7-14.oe2203.x86_64.rpm                                                  3.9 MB/s | 3.8 MB     00:00    
----------------------------------------------------------------------------------------------------------------------------
Total                                                                                       6.2 MB/s | 7.7 MB     00:01     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                    1/1 
  Installing       : lttng-ust-2.10.1-11.oe2203.x86_64                                                                  1/5 
  Running scriptlet: lttng-ust-2.10.1-11.oe2203.x86_64                                                                  1/5 
  Installing       : fmt-8.0.1-1.oe2203.x86_64                                                                          2/5 
  Installing       : librados2-2:16.2.7-14.oe2203.x86_64                                                                3/5 
  Running scriptlet: librados2-2:16.2.7-14.oe2203.x86_64                                                                3/5 
  Installing       : librbd1-2:16.2.7-14.oe2203.x86_64                                                                  4/5 
  Running scriptlet: librbd1-2:16.2.7-14.oe2203.x86_64                                                                  4/5 
  Installing       : qemu-block-rbd-10:6.2.0-60.oe2203.x86_64                                                           5/5 
  Running scriptlet: qemu-block-rbd-10:6.2.0-60.oe2203.x86_64                                                           5/5 
  Verifying        : fmt-8.0.1-1.oe2203.x86_64                                                                          1/5 
  Verifying        : lttng-ust-2.10.1-11.oe2203.x86_64                                                                  2/5 
  Verifying        : librados2-2:16.2.7-14.oe2203.x86_64                                                                3/5 
  Verifying        : librbd1-2:16.2.7-14.oe2203.x86_64                                                                  4/5 
  Verifying        : qemu-block-rbd-10:6.2.0-60.oe2203.x86_64                                                           5/5 

Installed:
  fmt-8.0.1-1.oe2203.x86_64             librados2-2:16.2.7-14.oe2203.x86_64          librbd1-2:16.2.7-14.oe2203.x86_64    
  lttng-ust-2.10.1-11.oe2203.x86_64     qemu-block-rbd-10:6.2.0-60.oe2203.x86_64    

Complete!
[root@superman-21 ~]# 

配置实例:

<domain type='kvm'>
    ...
    <iothreads>2</iothreads>
    <devices>
        <disk type='file' device='disk'>    
	    <driver name='qemu' type='qcow2' cache='none' io='native' iothread="1"/>
	    <source file='/mnt/openEuler-image.qcow2'/>
	    <target dev='vda' bus='virtio'/>
	    <boot order='1'/>
	</disk>
	<disk type='file' device='cdrom'>
	    <driver name='qemu' type='raw' cache='none' io='native'/>
	    <source file='/mnt/openEuler-22.03-LTS-aarch64-dvd.iso'/>
	    <target dev='sdb' bus='scsi'/>
	    <readonly/>
	    <boot order='2'/>
	</disk>
	<disk type='network' device='disk'>
	    <driver name='qemu' type='raw' cache='none' />
	    <source protocol="rbd" name="rbd/vol2"/>
	    <host name="192.168.0.2" port="6789" />
	    <target dev='sdc' bus='scsi'/>
	    <boot order='3'/>
	</disk>
         ...
    </devices>
</domain>

👍 点赞,你的认可是我创作的动力!

⭐️ 收藏,你的青睐是我努力的方向!

✏️ 评论,你的意见是我进步的财富!

image-20230506180918407