系列文章目录
提示:以下是本篇文章正文内容,下面命令可供参考
一、virsh常用命令
命令 | 解释 |
---|---|
virsh list | 列出正在运行的虚拟机,使用–all 参数列出所有虚拟机 |
virsh start 虚拟机名 | 启动虚拟机 |
virsh autostart 虚拟机名 | 开机自动启动虚拟机 |
virsh shutdown 虚拟机名 | 关闭虚拟机 |
virsh destroy 虚拟机名 | 强制关闭虚拟机 |
virsh console 虚拟机名 | 使用控制台连接到虚拟机 |
virsh suspend 虚拟机名 | 挂起虚拟机 |
virsh resumed 虚拟机名 | 虚拟机从挂起状态恢复 |
virsh edit 虚拟机名 | 修改虚拟机配置文件 |
virsh dumpxml 虚拟机名 | 查看虚拟机配置文件 |
virsh undefine 虚拟机名 | 删除虚拟机配置文件(等价于删除虚拟机,不会删除磁盘) |
virsh dominfo 虚拟机名 | 查看虚拟机基本信息 |
virsh domiflist 虚拟机名 | 查看虚拟机网卡信息 |
virsh vcpuinfo 虚拟机名 | 显示虚拟机的CPU信息 |
virsh domblklis 虚拟机名 | 显示虚拟机的硬盘信息 |
virsh domstate 虚拟机名 | 显示虚拟机的状态 |
virsh vncdisplay 虚拟机名 | 查看某个虚拟机对应的端口 |
virsh domrename 虚拟机名1 虚拟机名2 | 对虚拟机1重命名(前提是虚拟机1已关机) |
二、快照命令
只有qcow2的磁盘格式才支持快照
raw格式的磁盘文件并不支持快照
快照的存放位置跟它原本的文件放在一起了,每做一次,原磁盘文件就会变大,删除了快照,容量还是那么大,不会回收了,因为已经创建了。
1、创建快照
在这种创建方式中,快照的名称由KVM随机指定分配
[root@ops vmIsoDisk]# virsh snapshot-create kytest02
Domain snapshot 1741012768 created
如果想自己指定虚拟机的快照名称,则可以执行下方命令
[root@ops vmIsoDisk]# virsh snapshot-create-as 【虚拟机名】 【快照名】
2、查看快照列表
[root@ops vmIsoDisk]# virsh snapshot-list kytest02
Name Creation Time State
------------------------------------------------------------
1741012768 2025-03-03 22:39:28 +0800 running
3、删除快照
[root@ops vmIsoDisk]# virsh snapshot-delete centos7 --snapshotname 1741012768
Domain snapshot 1741012768 deleted
4、还原快照
[root@ops vmIsoDisk]# virsh snapshot-revert centos7.1 --snapshotname 1575798876
5、查看KVM虚拟机当前使用的快照
[root@ops vmIsoDisk]# virsh snapshot-current 【虚拟机名称】
三、克隆命令
1、完整克隆kytest02,-o后面是旧名字,-n后面是新的虚拟机的名字
[root@ops vmIsoDisk]# virt-clone --auto-clone -o kytest02 -n kytest02.clone -f /export/vmIsoDisk/KySP27.qcow2
#参数解释
-o kytest02 以kytest02为蓝本进行复制
-n kytest02.clone创建新的虚拟机名为kytest02.clone
-f 为虚拟机磁盘镜像文件指定位置
2、手动克隆
步骤:
1、复制虚拟磁盘文件
cp KySP27.qcow2 KySP27_bak.qcow2
2、备份源虚拟机的配置文件,另存为kytest02_bak.xml
virsh dumpxml kytest01 >kytest02_bak.xml
3、修改kytest02_bak.xml的配置文件
vim kytest02_bak.xml
修改name
删除uuid
删除mac
修改虚拟机磁盘路径
4、导入kytest02_bak.xml检查是否正常启动
virsh define kytest02_bak.xml
5、启动虚拟机
virsh start kytest02_bak
6、查看交换设备
brctl show
3、链接克隆
链接克隆的格式: qemu-img create -f 格式 -b 旧磁盘名 新磁盘名
四、存储管理命令
存储卷命令相关命令
vol-clone #克隆卷
vol-create-as #从一组变量中创建卷
vol-create #从一个 XML 文件创建一个卷
vol-create-from #生成卷,使用另一个卷作为输入
vol-delete #删除卷
vol-download #将卷内容下载到文件中
vol-dumpxml #保存卷信息到XML文档中
vol-info #查看存储卷信息
vol-key #根据卷名或路径返回卷的key
vol-list #列出卷
vol-name #根据给定卷key或者路径返回卷名
vol-path #根据卷名或key返回卷路径
vol-pool #为给定密钥或者路径返回存储池
vol-resize #重新定义卷大小
vol-upload #将文件内容上传到卷中
vol-wipe #擦除卷
存储池相关命令
pool-autostart #自动启动某个池pool-build 建立池
pool-create-as #从一组变量中创建一个池
pool-create #从一个 XML 文件中创建一个池
pool-define-as #在一组变量中定义池
pool-define #在一个XML文件中定义(但不启动)一个池或修改已有池
pool-delete #删除池
pool-destroy #销毁(删除)池
pool-dumpxml #将池信息保存到XML文档中
pool-edit #为存储池编辑 XML 配置
pool-info #查看存储池信息
pool-list #列出池
pool-name #将池 UUID 转换为池名称
pool-refresh #刷新池
pool-start #启动一个(以前定义的)非活跃的池
pool-undefine #取消定义一个不活跃的池
pool-uuid #把一个池名称转换为池 UUID
1、列出所有存储池
[root@ops ~]# virsh pool-list
Name State Autostart
-----------------------------------
img active yes
vmIsoDisk active yes
vmIsoDisk-1 active yes
查看存储池详细信息
[root@ops ~]# virsh pool-list --details
Name State Autostart Persistent Capacity Allocation Available
--------------------------------------------------------------------------------------
img running yes yes 19.99 GiB 12.82 GiB 7.17 GiB
vmIsoDisk running yes yes 19.99 GiB 12.82 GiB 7.17 GiB
vmIsoDisk-1 running yes yes 19.99 GiB 3.55 GiB 16.44 GiB
查看存储池中卷的信息
[root@ops ~]# virsh vol-list vmIsoDisk
Name Path
------------------------------------------------
KySP27.qcow2 /export/vmIsoDisk/KySP27.qcow
2、启动存储池
[root@ops ~]# virsh pool-start vmIsDisk
3、关闭存储池
[root@ops ~]# virsh pool-destroy <pool>
4、创建存储池
4.1、基于目录创建存储池,在本地创建一个测试目录,dir为存储池格式
[root@ops ~]# mkdir /test
[root@ops ~]# virsh pool-define-as test dir --target /test
Pool test defined
[root@ops ~]# virsh pool-list --all
Name State Autostart
-------------------------------------------
img active yes
vmIsoDisk active yes
vmIsoDisk-1 active yes
test inactive no
[root@ops ~]# virsh pool-start test
Pool test started
[root@ops ~]# virsh pool-autostart test
Pool test marked as autostarted
[root@ops ~]# virsh pool-list
Name State Autostart
-------------------------------------------
img active yes
vmIsoDisk active yes
vmIsoDisk-1 active yes
test active yes
4.2、创建基于LVM的存储池
基于LVM的存储池要求使用全部磁盘分区创建存储池时,首先准备一个VG,VG中不需要创建LV,有两种情况使用现有的VG,创建新的VG
参数示例
Target Path: 新的卷组名
Source Path: 存储设备的位置
Build Pool:会创建新的VG
先添加一块硬盘,然后创键VG,注意不要创建LV
[root@ops ~]# pvcreate /dev/sdc
Physical volume "/dev/sdc" successfully created.
[root@ops ~]# vgcreate vg01 /dev/sdc
Volume group "vg01" successfully created
创建存储池
[root@ops ~]# virsh pool-define-as vgpool logical --source-name=vg01 --target=/dev/vg01
Pool vgpool defined
[root@ops ~]# virsh pool-list --all
Name State Autostart
-------------------------------------------
img active yes
vmIsoDisk active yes
vmIsoDisk-1 active yes
test active yes
vgpool inactive no
[root@ops ~]# virsh pool-start vgpool
Pool vgpool started
[root@ops ~]# virsh pool-autostart vgpool
Pool vgpool marked as autostarted
[root@ops ~]# virsh pool-list
Name State Autostart
-------------------------------------------
img active yes
vmIsoDisk active yes
vmIsoDisk-1 active yes
test active yes
vgpool active yes
[root@ops ~]# ls /dev/vg01
vm_vg01.qcow2
4.3、基于NFS的存储池
打开一台服务器搭建nfs,先给nfs服务器部署逻辑卷
[root@nfs ~]# pvcreate /dev/sdb
Physical volume "/dev/sdb" successfully created.
[root@nfs ~]# vgcreate vg01 /dev/sdb
Volume group "vg01" successfully created
[root@nfs ~]# vgdisplay
--- Volume group ---
VG Name vg01
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size <50.00 GiB
PE Size 4.00 MiB
Total PE 12799
Alloc PE / Size 0 / 0
Free PE / Size 12799 / <50.00 GiB
VG UUID 4OMUgj-eMfD-wvHD-BJ0z-EsbF-uI8M-3b8dTf
#省略部分内容
[root@nfs ~]# lvcreate -n lv01 -l 12799 vg01
Logical volume "lv01" created.
[root@nfs ~]# mkfs.xfs /dev/vg01/lv01
meta-data=/dev/vg01/lv01 isize=512 agcount=4, agsize=3276544 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=13106176, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=6399, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@nfs ~]# mkdir /nfspool
[root@nfs ~]# mount /dev/vg01/lv01 /nfspool
[root@nfs ~]# vim /etc/fstab
#添加下面内容永久挂载
/dev/vg01/lv01 /nfspool xfs default 0 0
安装nfs服务,配置文件共享
[root@nfs ~]# yum -y install nfs-utils rpcbind
#省略安装部分
[root@nfs ~]# vim /etc/exports
/nfspool *(rw,sync,no_root_squash)
[root@nfs ~]# systemctl start nfs rpcbind
[root@nfs ~]# systemctl enable nfs rpcbind
使用kvm服务器查看nfs服务器
[root@localhost ~]# showmount -e 192.168.56.102
Export list for 192.168.56.102:
/nfspool *
创建存储池
需要先创建挂载目录,创建存储池时,指定池名称为vmpool类型为netfs
源主机为nfs服务器,源路径为nfs服务器的共享目录
目标目录为kvm服务器的挂载点
[root@localhost ~]# mkdir /nfspool
[root@localhost /]# virsh pool-define-as --name nfspool --type netfs --source-host 192.168.56.102 --source-path /nfspool --target /nfspool
Pool nfspool defined
[root@localhost /]# virsh pool-start nfspool
Pool nfspool started
[root@localhost /]# virsh pool-autostart nfspool
Pool nfspool marked as autostarted
[root@localhost /]# virsh pool-list
Name State Autostart
-------------------------------------------
img active yes
vmIsoDisk active yes
vmIsoDisk-1 active yes
test active yes
vgpool active yes
5、列出存储卷
[root@ops ~]# virsh vol-list --pool img(存储池名称)
Name Path
--------------------------------------------------------------------------------------------------------------------------------
Kylin-Server-10-SP2-Release-Build09-20210524-x86_64.iso /export/img/Kylin-Server-10-SP2-Release-Build09-20210524-x86_64.iso
6、创建存储卷
6.1、在本地目录创建一个qcow2格式的存储卷,不加–format参数默认为raw格式
[root@ops ~]# virsh pool-list
Name State Autostart
-------------------------------------------
img active yes
vmIsoDisk active yes
vmIsoDisk-1 active yes
nfspool active yes
test active yes
vgpool active yes
[root@ops ~]# virsh vol-create-as test test1.qcow2 8G --format qcow2
Vol test1.qcow2 created
[root@ops ~]# virsh vol-list test
Name Path
------------------------------------------------------------------------------
test1.qcow2 /test/test1.qcow2
6.2、在lvm目录创建一个qcow2格式的存储卷,不加–format参数默认为raw格式
[root@ops ~]# virsh pool-list
Name State Autostart
-------------------------------------------
img active yes
vmIsoDisk active yes
vmIsoDisk-1 active yes
nfspool active yes
test active yes
vgpool active yes
[root@ops ~]# virsh vol-create-as vgpool vm_vg01.qcow2 8G --format qcow2
Vol vm_vg01.qcow2 created
[root@ops ~]# virsh vol-list vgpool
Name Path
------------------------------------------------------------------------------
vm_vg01.qcow2 /dev/vg01/vm_vg01.qcow2
6.3、在nfs目录创建一个qcow2格式的存储卷,不加–format参数默认为raw格式
[root@ops ~]# virsh pool-list
Name State Autostart
-------------------------------------------
img active yes
vmIsoDisk active yes
vmIsoDisk-1 active yes
nfspool active yes
test active yes
vgpool active yes
[root@ops ~]# virsh vol-create-as nfspool nfs1.qcow2 5G --format qcow2
Vol nfs1.qcow2 created
[root@ops ~]# virsh vol-list nfspool
Name Path
------------------------------------------------------------------------------
nfs1.qcow2 /nfspool/nfs1.qcow2
五 、转换虚拟机硬盘格式
#进行格式转换前需要关闭虚拟机
[root@ops vmIsoDisk]# qemu-img convert -f raw -O qcow2 /export/vmIsoDisk/centos7.raw /export/vmIsoDisk/centos7.qcow2 将raw格式转换为qcow2
六、virt-top命令
virt-top是一个用于展示虚拟化客户机运行状态和资源使用率的工具,它与Linux系统上常用的“top”工具类似,而且它的许多快捷键和命令行参数的设置都与“top”工具相同
virt-top也是使用libvirt API来获取客户机的运行状态和资源使用情况的,所以只要是libvirt支持的Hypervisor,就可以用virt-top监控该Hypervisor上的客户机状态
[root@ops vmIsoDisk]# virt-top
七、热添加命令
1、临时添硬盘
[root@ops vmIsoDisk]# virsh attach-disk web01 /data/web01-add.qcow2 vdb --subdriver qcow2
#参数解释:
web01 虚机名称
/data/web01-add.qcow2 通过qemu-img新创建的qcow2格式的磁盘路径及类型
vdb 加入到虚机中的磁盘的盘符名称,默认以vd*开头
--subdriver qcow2 指定磁盘的类型
2、永久添加硬盘
[root@ops vmIsoDisk]# virsh attach-disk web01 /data/web01-add.qcow2 vdb --subdriver qcow2 --config
2.1、虚机磁盘扩容
KVM虚拟机扩容规范操作:
- 1.在KVM虚拟机中卸载需要扩容的挂载点;
- 2.在KVM宿主机中移除需要扩容的磁盘设备;
- 3.在KVM宿主机使用"qemu-img resize"工具扩容磁盘设备;
- 4.在KVM宿主机附加硬盘到虚拟机;
- 5.在KVM虚拟机重新挂载硬盘设备;
- 6.更新vd*文件系统的元数据;
操作示例: 将虚机中的vdc磁盘由20G扩容到40G
[root@ops vmIsoDisk]# virsh list
Id Name State
----------------------------------------------------
1 kytest03 running
#从宿主机登录到虚机中
[root@ops vmIsoDisk]# virsh console kytest03
Connected to domain kytest03
Escape character is ^]
[root@localhost ~]# df -h | grep mnt
/dev/vdc 20G 33M 20G 1% /mnt
[root@localhost ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b20d3
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20971519 10484736 83 Linux
Disk /dev/vdc: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
[root@localhost ~]# ll /mnt/
total 4
-rw-r--r--. 1 root root 393 Apr 9 18:17 os-release
2.1.1、KVM虚拟机卸载挂载点
如果有数据请先备份,防止数据丢失
[root@localhost ~]# df -h | grep mnt
/dev/vdc 20G 33M 20G 1% /mnt
[root@localhost ~]# umount /mnt/
2.1.2、在KVM宿主机中移除需要扩容的磁盘设备
[root@ops vmIsoDisk]# virsh list
Id Name State
----------------------------------------------------
1 kytest03 running
#剥离磁盘vdc
[root@ops vmIsoDisk]# virsh detach-disk kytest03 vdc
Disk detached successfully
[root@ops vmIsoDisk]# virsh list
Id Name State
----------------------------------------------------
1 kytest03 running
#从宿主机登录虚机
[root@ops vmIsoDisk]# virsh console kytest03
Connected to domain yinzhengjie-kvm01
Escape character is ^]
CentOS Linux 7 (Core)
Kernel 3.10.0-1160.el7.x86_64 on an x86_64
localhost login: root
Password:
Last login: Tue Apr 9 18:37:05 on ttyS0
[root@localhost ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b20d3
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20971519 10484736 83 Linux
2.1.3、 在KVM宿主机扩容磁盘设备
[root@ops vmIsoDisk]# qemu-img info /export/vmIsoDisk/kytest03_disk01.qcow2
image: /export/vmIsoDisk/kytest03_disk01.qcow2
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 11M
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
[root@ops vmIsoDisk]# qemu-img resize /export/vmIsoDisk/kytest03_disk01.qcow2 40G
Image resized.
[root@ops vmIsoDisk]# qemu-img info /export/vmIsoDisk/kytest03_disk01.qcow2
image: /export/vmIsoDisk/kytest03_disk01.qcow2
file format: qcow2
virtual size: 40G (42949672960 bytes)
disk size: 27M
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
[root@ops vmIsoDisk]# ll -h /export/vmIsoDisk/kytest03_disk01.qcow2
-rw-r--r--. 1 root root 12M Apr 9 18:46 /export/vmIsoDisk/kytest03_disk01.qcow2
2.1.4、在KVM宿主机附加硬盘到虚拟机
[root@ops vmIsoDisk]# virsh list
Id Name State
----------------------------------------------------
1 kytest03 running
[root@ops vmIsoDisk]# virsh attach-disk kytest03 /export/vmIsoDisk/kytest03_disk01.qcow2 vdc --subdriver qcow2 --config
Disk attached successfully
2.1.5、在KVM虚拟机重新挂载硬盘设备
[root@ops vmIsoDisk]# virsh list
Id Name State
----------------------------------------------------
1 kytest03 running
#从宿主机登录到虚拟中
[root@ops vmIsoDisk]# virsh console kytest03
Connected to domain kytest03
Escape character is ^]
[root@localhost ~]# fdisk -l
Disk /dev/vda: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000b20d3
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20971519 10484736 83 Linux
Disk /dev/vdc: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
[root@localhost ~]# mount /dev/vdc /mnt/
[root@localhost ~]# ll /mnt/
total 4
-rw-r--r--. 1 root root 393 Apr 9 18:17 os-release
挂载完成后再将其写到/etc/fstab文件中,实现永久挂载
2.1.6、更新虚机中vd*文件系统的元数据
[root@localhost ~]# df -h | grep mnt # 注意看,磁盘的大小貌似没有识别到磁盘扩容了
/dev/vdc 20G 33M 20G 1% /mnt
[root@localhost ~]# xfs_growfs /dev/vdc # 更新xfs文件系统的元数据(超级块)信息
meta-data=/dev/vdc isize=512 agcount=4, agsize=1310720 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=5242880, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 5242880 to 10485760
[root@localhost ~]# df -h | grep mnt # 不难发现,数据更新后,就可以识别到40GB磁盘啦~
/dev/vdc 40G 33M 40G 1% /mnt
3、临时剥离硬盘
[root@ops vmIsoDisk]# virsh detach-disk web01 vdb
4、永久剥离硬盘
[root@ops vmIsoDisk]# virsh detach-disk web01 vdb --config
5、临时热添加内存
[root@ops vmIsoDisk]# virsh setmem web04 1024M
6、永久增大内存
[root@ops vmIsoDisk]# virsh setmem web04 1024M --config
7、调整虚拟机内存最大值
[root@ops vmIsoDisk]# virsh setmaxmem web04 4G
八、热迁移
1、什么是虚机的热迁移?
相比KVM虚拟机冷迁移中需要拷贝虚拟机虚拟磁盘文件,kvm虚拟机热迁移无需拷贝虚拟磁盘文件,
但是需要迁移到的宿主机之间需要有相同的目录结构虚拟机磁盘文件,也就是共享存储,
可以通过熟悉的nfs来实现,当然也可以采用Glusterfs等分布式文件系统来实现.
2、热迁移命令
8.1临时迁移
[root@ops vmIsoDisk]# virsh migrate --live --verbose web04 qemu+ssh://10.0.0.11/system --unsafe
8.2永久迁移
[root@ops vmIsoDisk]# virsh migrate --live --verbose web03 qemu+ssh://10.0.0.100/system --unsafe --persistent --undefinesource