KVM常用的热添加设备

柳三千

文章最后更新时间:2025年06月13日

KVM热添加硬盘

[root@KVM ~]# cd /data/
[root@KVM data]# ls
Centos7-clone.qcow2  Centos7.qcow2  Centos7-test.qcow2  test.xml  web  web.qcow2  web.xml

[root@KVM data]# qemu-img create -f qcow2 web.add.qcow2 10G
Formatting 'web.add.qcow2', fmt=qcow2 size=10737418240 encryption=off cluster_size=65536 lazy_refcounts=off 
[root@KVM data]# ls
Centos7-clone.qcow2  Centos7.qcow2  Centos7-test.qcow2  test.xml  web  web.add.qcow2  web.qcow2  web.xml
# 即时生效
[root@KVM data]# virsh attach-disk web_mysql /data/web.add.qcow2 vdb --subdriver qcow2
成功附加磁盘

# 开机后生效
[root@KVM data]# virsh attach-disk web_mysql /data/web.add.qcow2 vdb --subdriver qcow2 --config
成功附加磁盘

# 参数说明:
# --config:开机后生效
# --subdriver qcow2:挂载硬盘为qcow2格式


KVM硬盘剥离

# 临时剥离
[root@KVM data]# virsh detach-disk web_mysql vdb 
成功分离磁盘

# 永久剥离
[root@KVM data]# virsh detach-disk web_mysql vdb --config 
成功分离磁盘

KVM热添加网卡

# 桥接模式
[root@KVM ~]# virsh attach-interface web_mysql --type bridge --source br0 --model virtio
成功附加接口

# NAT模式
[root@KVM ~]# virsh attach-interface web_mysql --type network --source default --model virtio

KVM剥离网卡

# 桥接模式剥离(需指定MAC地址)
[root@KVM ~]# virsh detach-interface web_mysql --type bridge --mac 00:00:00:00:00:00
成功分离接口

# 网络模式剥离(多网卡时需指定MAC地址)
[root@KVM ~]# virsh detach-interface web_mysql --type network
错误:域有 2 个接口。请使用 --mac 指定要分离的接口。
错误:分离接口失败

[root@KVM ~]# virsh detach-interface web_mysql --type network --mac 52:54:00:91:19:bb
成功分离接口

KVM热添加内存

# 临时修改
[root@KVM ~]# virsh setmem web_mysql 256M

# 恢复
[root@KVM ~]# virsh setmem web_mysql 1024M

# 参数说明:
# --config:永久生效

KVM热添加CPU

环境配置

# 创建链接克隆磁盘
[root@KVM data]# qemu-img create -f qcow2 -b Centos7.qcow2 web88.qcow2
Formatting 'web88.qcow2', fmt=qcow2 size=10737418240 backing_file='Centos7.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off
# 创建支持CPU热插拔的虚拟机
[root@KVM data]# virt-install  \
--virt-type kvm  \
--os-type=linux  \
--os-variant rhel7  \
--name web88  \
--memory 1024  \
--vcpus 5,maxvcpus=10  \
--disk /data/web88.qcow2  \
--boot hd \
--network network=default  \
--graphics vnc,listen=0.0.0.0 \
--noautoconsole

# 热添加CPU(临时生效)
[root@KVM data]# virsh setvcpus web88 6

# 参数说明:
# --config:永久生效

文章版权声明:除非注明,否则均为柳三千运维录原创文章,转载或复制请以超链接形式并注明出处。

取消
微信二维码
微信二维码
支付宝二维码