3.1 qemu+kvm关联
链接
关联命令
kvm的默认文件夹
- xml文件目录:/etc/libvirt/qemu/
- 虚拟机镜像文件目录:/var/lib/libvirt/images/
镜像压缩命令:
- qemu-img convert -O qcow2 XXX.img XXX_.img
从xml文件定义虚拟机命令:
- virsh define xxx.xml
增加磁盘镜像容量:
- virsh domblklist 虚拟机名
查看虚拟机信息。
[root@lenovo312a ~]# virsh domblklist win7_4x
Target Source
---------------------------
sda /VM/win7_4x.img
[root@lenovo312a ~]#
- virsh dumpxml 虚拟机名 | egrep 'disk type' -A 5
虚拟机设定
[root@lenovo312a ~]# virsh dumpxml win7_4x | egrep 'disk type' -A 5
egrep: warning: egrep is obsolescent; using grep -E
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' discard='unmap'/>
<source file='/VM/win7_4x.img'/>
<target dev='sda' bus='sata'/>
<boot order='2'/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
[root@lenovo312a ~]#
- qemu-img info 磁盘
查看磁盘信息。
[root@lenovo312a ~]# qemu-img info /VM/win7_4x.img
image: /VM/win7_4x.img
file format: qcow2
virtual size: 40 GiB (42949672960 bytes)
disk size: 196 KiB
cluster_size: 65536
Format specific information:
compat: 1.1
compression type: zlib
lazy refcounts: true
refcount bits: 16
corrupt: false
extended l2: false
Child node '/file':
filename: /VM/win7_4x.img
protocol type: file
file length: 193 KiB (197632 bytes)
disk size: 196 KiB
[root@lenovo312a ~]#
- virsh snapshot-list 虚拟机名
查看和列出系统或虚拟机当前的快照(Snapshot)版本及状态。
[root@lenovo312a ~]# virsh snapshot-list win7_4x
Name Creation Time State
------------------------------------------------------
win7_4x_snap1 2026-07-20 10:18:42 +0900 shutoff
[root@lenovo312a ~]#
- virsh snapshot-delete --domain 虚拟机名 --snapshotname 快照名
删除快照
[root@lenovo312a ~]# virsh snapshot-delete --domain win7_4x --snapshotname win7_4x_snap1
Domain snapshot win7_4x_snap1 deleted
[root@lenovo312a ~]#
- qemu-img resize 磁盘 +增加的容量
离线(虚拟机关机)状态下增加磁盘容量
[root@lenovo312a ~]# qemu-img resize /VM/win7_4x.img +260G
Image resized.
[root@lenovo312a ~]#
[root@lenovo312a ~]# qemu-img info /VM/win7_4x.img
image: /VM/win7_4x.img
file format: qcow2
virtual size: 300 GiB (322122547200 bytes)
disk size: 204 KiB
cluster_size: 65536
Format specific information:
compat: 1.1
compression type: zlib
lazy refcounts: true
refcount bits: 16
corrupt: false
extended l2: false
Child node '/file':
filename: /VM/win7_4x.img
protocol type: file
file length: 320 KiB (328192 bytes)
disk size: 204 KiB
[root@lenovo312a ~]#
- virsh blockresize 虚拟机名 磁盘 增加后的容量
在线(虚拟机运行)状态下增加磁盘容量
[root@lenovo312a ~]# virsh start win7_4x
Domain 'win7_4x' started
[root@lenovo312a ~]# virsh blockresize win7_4x /VM/win7_4x.img 300G
Block device '/VM/win7_4x.img' is resized
[root@lenovo312a ~]#
[root@lenovo312a ~]# virsh start win7_4x
Domain 'win7_4x' started
[root@lenovo312a ~]# virsh blockresize win7_4x /VM/win7_4x.img 300G
Block device '/VM/win7_4x.img' is resized
[root@lenovo312a ~]#
[root@lenovo312a ~]#
[root@lenovo312a ~]#
[root@lenovo312a ~]# fdisk -l /VM/win7_4x.img
Disk /VM/win7_4x.img: 320 KiB, 327680 bytes, 640 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@lenovo312a ~]#
[root@lenovo312a ~]# qemu-img info /VM/win7_4x.img
image: /VM/win7_4x.img
file format: qcow2
virtual size: 300 GiB (322122547200 bytes)
disk size: 204 KiB
cluster_size: 65536
Format specific information:
compat: 1.1
compression type: zlib
lazy refcounts: true
refcount bits: 16
corrupt: false
extended l2: false
Child node '/file':
filename: /VM/win7_4x.img
protocol type: file
file length: 320 KiB (328192 bytes)
disk size: 204 KiB
[root@lenovo312a ~]#
增加磁盘镜像容量:
其他虚拟机命令:
- virsh snapshot-create-as 虚拟机名 快照名
生成快照
[root@lenovo312a ~]# virsh snapshot-create-as win7_4x win7_4x_snap1 Domain snapshot win7_4x_snap1 created [root@lenovo312a ~]#