kvm常用命令
修改虚机名称
$ virsh domrename jenkins new-jenkins
Domain successfully renamed
虚拟机处于关机状态才能修改虚拟机名称
克隆虚机
前提条件:需要有克隆模板,这个这里使用的 CentOS7 虚拟机作为克隆模板
$ virt-clone -o CentOS7 -n gitlab -f /var/lib/libvirt/images/gitlab.qcow2
WARNING The requested volume capacity will exceed the available pool space when the volume is fully allocated. (65536 M requested capacity > 6583 M available)
Allocating 'gitlab.qcow2' | 64 GB 00:00:24
Clone 'gitlab' created successfully.
$ virsh dominfo gitlab
Id: -
Name: gitlab
UUID: 3dbac285-416f-4da9-b7fb-8a1a2cecc07f
OS Type: hvm
State: shut off
CPU(s): 8
Max memory: 134217728 KiB
Used memory: 8388608 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: none
Security DOI: 0
上面告警可以忽略,是因为磁盘空间不足导致的
扩缩容内存
当最大内存限制,符合预期的扩容的内存则可以现实动态扩容。否则,需要先修改最大内存限制(需要重启主机),然后再进行扩容。
- 查看最大内存
$ virsh dominfo jenkins
Id: 15
Name: jenkins
UUID: 0693603b-5237-4a39-bec5-a9c0a69b7c4c
OS Type: hvm
State: running
CPU(s): 4
CPU time: 49.5s
Max memory: 4194304 KiB
Used memory: 4194304 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: none
Security DOI: 0
如上所示,最大内存
Max memory可以扩容到4194304 KiB即是 4G 内存。如果预期扩容内存到8G,则需要先关机,设置最大内存值
- 关闭虚拟机 (预期内存符合最大内存限制,跳过该步骤)
$ virsh shutdown jenkins
Domain jenkins is being shutdown
- 修改虚机可使用最大内存值 (预期内存符合最大内存限制,跳过该步骤)
# 修改最大内存值
$ virsh edit jenkins
<memory unit='KiB'>16777216</memory>
# 生效配置
$ virsh define /etc/libvirt/qemu/jenkins.xml
Domain jenkins defined from /etc/libvirt/qemu/jenkins.xml
说明:
- 修改
memory该标签的值,16777216KiB = 16G可以设置大一些,预防下次继续扩容内存需要关机。开机不是使用该大小内存,实际使用内存是currentMemory该标签下的数值- 该数值不能超过
free -h中的available数值,否则启动虚拟机会报错
- 启动虚拟机 (预期内存符合最大内存限制,跳过该步骤)
$ virsh start jenkins
Domain jenkins started
- 在线修改主机内存
$ virsh setmem jenkins 8G --config --live
# 验证修改后的内存
$ virsh dominfo jenkins
Id: 21
Name: jenkins
UUID: 0693603b-5237-4a39-bec5-a9c0a69b7c4c
OS Type: hvm
State: running
CPU(s): 4
CPU time: 75.1s
Max memory: 16777216 KiB # 该虚拟机最大可使用的内存
Used memory: 8388608 KiB # 当时虚拟机使用内存
Persistent: yes
Autostart: disable
Managed save: no
Security model: none
Security DOI: 0
扩缩容CPU
扩缩vCPU个数,不支持热插拔
# 关闭虚拟机
$ virsh shutdown jenkins
Domain jenkins is being shutdown
# 修改vcpu的个数
$ virsh edit jenkins
<vcpu placement='static'>2</vcpu>
# 生效配置文件
$ virsh define /etc/libvirt/qemu/jenkins.xml
Domain jenkins defined from /etc/libvirt/qemu/jenkins.xml
# 启动虚拟机
$ virsh start jenkins
Domain jenkins started
# 查看虚拟机信息
$ virsh dominfo jenkins
Id: 23
Name: jenkins
UUID: 0693603b-5237-4a39-bec5-a9c0a69b7c4c
OS Type: hvm
State: running
CPU(s): 2 # vCPU个数
CPU time: 42.5s
Max memory: 16777216 KiB
Used memory: 11444224 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: none
Security DOI: 0
动态增删 硬盘
# 查看虚拟机硬盘数量
$ virsh domblklist jenkins
Target Source
------------------------------------------------
vda /var/lib/libvirt/images/Jenkins.qcow2
hda -
# 创建虚拟硬盘
$ qemu-img create -f qcow2 /var/lib/libvirt/images/jenkins-vdb.qcow2 10G
Formatting '/var/lib/libvirt/images/jenkins-vdb.qcow2', fmt=qcow2 size=10737418240 encryption=off cluster_size=65536 lazy_refcounts=off
# 将硬盘挂载到虚拟机上
$ virsh attach-disk jenkins /var/lib/libvirt/images/jenkins-vdb.qcow2 vdb --subdriver=qcow2 --config --live --persistent
Disk attached successfully
# 查看虚拟机硬盘数量
$ virsh domblklist jenkins
Target Source
------------------------------------------------
vda /var/lib/libvirt/images/Jenkins.qcow2
vdb /var/lib/libvirt/images/jenkins-vdb.qcow2
hda -
# 登录主机查看是否有增加硬盘
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 253:0 0 64G 0 disk
├─vda1 253:1 0 500M 0 part /boot
├─vda2 253:2 0 4G 0 part [SWAP]
└─vda3 253:3 0 59.5G 0 part /
vdb 253:16 0 10G 0 disk
# 将硬盘从云主机分离
$ virsh detach-disk jenkins vdb --persistent --config --live
Disk detached successfully
注意:
- 挂载硬盘到虚拟机上,需要添加
--subdriver=qcow2参数,否则使用raw格式。

浙公网安备 33010602011771号