除了直接使用裸设备,存储于文件系统中的各种格式镜像文件,并不支持在线伸缩。我们只能使用在线追加镜像文件的方式扩展磁盘。
## list all domain blocks,这里sda、sdb设备名称是XML里定义的,并不是guest真实的磁盘名称。
# virsh domblklist LDAP05
sda /var/lib/libvirt/images/LDAP05.qcow2
sdb /var/lib/libvirt/images/CentOS-7.7-x86_64-Minimal-1908.iso
# qemu-img create -f raw LDAP05-02.img 10G
## virsh attach-disk <domain> <source> <target>
# virsh attach-disk LDAP05 /var/lib/libvirt/images/LDAP05-02.img sdc \
--cache=none --targetbus=scsi \
--live --config
--cache:指定磁盘缓存模式
--targetbus:指定磁盘驱动类型
镜像格式qcow2或raw等可以使用--subdriver=qcow2指定,不指定,对于QEMU默认为raw。(出于安全原因,它被配置为不检测磁盘格式。)
注释:这里闹了一个笑话。。查看virsh的man文件时,没仔细看,还以为是会检测磁盘格式,不用指定--subdriver。但是,实际情况刚好相反。
其他参数,请自行查询帮助:virsh attach-disk –help
--live 对domain生效
--config 将配置写XML,下次启动生效
验证
# lsblk
sdb 8:16 0 10G 0 disk
# virsh edit LDAP05
<disk type='file' device='disk'>
<driver name='qemu' type='raw' cache='none'/>
<source file='/var/lib/libvirt/images/LDAP05-02.img'/>
<target dev='sdc' bus='scsi'/>
<address type='drive' controller='0' bus='0' target='0' unit='2'/>
</disk>
客户机中扩展文件系统
# pvcreate /dev/sdb
# vgdisplay
VG Name centos_ldap-01
# vgextend centos_ldap-01 /dev/sdb
# vgdisplay
VG Name centos_ldap-01
Free PE / Size 2559 / <10.00 GiB
# lvdisplay
LV Path /dev/centos_ldap-01/root
# lvextend -l+2559 /dev/centos_ldap-01/root
# df -hT
/dev/mapper/centos_ldap--01-root xfs 57G 1.5G 56G 3% /
# xfs_growfs /dev/centos_ldap-01/root
# df -h
/dev/mapper/centos_ldap--01-root 67G 1.5G 66G 3% /
对--subdriver的补充,下面英文是man文档原文:
Further details to the driver can be passed using subdriver.For Xen subdriver can be aio, while for QEMU subdriver should match the format of the disk source, such as raw or qcow2.Hypervisor default will be used if subdriver is not specified.However, the default may not be correct, esp. for QEMU as for security reasons it is configured not to detect disk formats.
可以使用子驱动程序传递驱动程序的更多细节。对于Xen子驱动程序可以是aio,而对于QEMU子驱动程序应该匹配磁盘源的格式,比如raw或qcow2。如果没有指定子驱动程序,将使用Hypervisor默认值。但是,默认值可能不正确,特别是对于QEMU,出于安全原因,它被配置为不检测磁盘格式。