方法一
**************************挂载硬盘****************
#创建硬盘
[root@idc img]# qemu-img create -f qcow2 vdb.qcow2 100G
Formatting 'vdb.qcow2', fmt=qcow2 size=107374182400 encryption=off cluster_size=65536 lazy_refcounts=off
[root@idc img]# ls
2008r2.qcow2 centos7.5.qcow2 centos7v1.5.qcow2 temp.xml vdb.img vdb.qcow2
#新建xml文件
[root@idc img]# vim temp.xml
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='writeback'/>
<source file='/home/img/vdb.qcow2'/>
<target dev='vdb' bus='virtio'/>
</disk>
#查看当前虚拟机
root@idc img]# virsh list
Id Name State
----------------------------------------------------
7 centos7.5 running
19 centos7v1.5 running
#查看虚拟机centos7.5的硬盘信息
[root@idc img]# virsh domblklist centos7.5
Target Source
------------------------------------------------
vda /home/img/centos7.5.qcow2
hda -
#加载xml文件 参数 --config 是写入配置文件 --live 是热添加
[root@idc img]# virsh attach-device centos7.5 temp.xml --config --live
Device attached successfully
#查看虚拟机centos7.5的硬盘信息
[root@idc img]# virsh domblklist centos7.5
Target Source
------------------------------------------------
vda /home/img/centos7.5.qcow2
vdb /home/img/vdb.img
hda -
**************************卸载载硬盘****************
#查看虚拟机centos7.5的硬盘信息
[root@idc img]# virsh domblklist centos7.5
Target Source
------------------------------------------------
vda /home/img/centos7.5.qcow2
vdb /home/img/vdb.img
hda
#卸载第二块硬盘 参数 --config 是写入配置文件 --live 是热删除
[root@idc img]# virsh detach-disk centos7.5 vdb --config --live
Disk detached successfully
[root@idc img]# virsh domblklist centos7.5
Target Source
------------------------------------------------
vda /home/img/centos7.5.qcow2
hda -
[root@idc img]#
*********这样下次添加就变得很容易,硬盘没有直接丢失
*********
*********
方法二
命令行添加 #--subdriver qcow2 未配置此选项默认使用:raw格式
virsh attach-disk 42.51.181.40 /date/nfs/img/42.51.181.40/vdb.qcow2 vdb --config --subdriver qcow2
virsh attach-disk 42.51.181.40 /date/nfs/img/42.51.181.40/vdb.qcow2 vdb --cache none --config --live --subdriver qcow2
原文链接:kvm虚拟机热添加硬盘的两种方法,转载请注明来源!