一、虚拟磁盘管理
1. 虚拟磁盘格式
- raw
raw : 也称为裸格式,不支持虚拟机快照功能;立即分配所有磁盘空间,不方便传输;磁盘读写性能较好; |
- qcow2(qemu copy on write)
qcow2 : 占用空间小,方便传输;支持快照,性能比raw差一点; |
2. 虚拟磁盘工具的常用命令
qemu-img info 磁盘文件 #查看磁盘文件属性信息 qemu-img create -f 磁盘格式 /目录/磁盘文件名称 容量 #创建磁盘文件(默认创建raw格式磁盘) qemu-img resize 磁盘文件 +容量 #增加指定的磁盘容量 qemu-img resize 磁盘文件 容量 #调整磁盘至指定容量 qemu-img convert -f 原磁盘格式 -O 目标磁盘格式 原磁盘文件 目标磁盘文件 #转换磁盘格式 |
注意:不建议缩减磁盘容量;容易丢失数据。
[root@kvm01 ~]# qemu-img info /opt/centos7-web01.raw
image: /opt/centos7-web01.raw
file format: raw
virtual size: 10G (10737418240 bytes)
disk size: 1.5G
[root@kvm01 ~]# qemu-img create -f qcow2 /opt/test_create.qcow2 1G
Formatting '/opt/test_create.qcow2', fmt=qcow2 size=1073741824 encryption=off cluster_size=65536 lazy_refcounts=off
[root@kvm01 ~]# qemu-img info /opt/test_create.qcow2
image: /opt/test_create.qcow2
file format: qcow2
virtual size: 1.0G (1073741824 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
[root@kvm01 ~]# qemu-img resize /opt/test_create.qcow2 +9G
Image resized.
[root@kvm01 ~]# qemu-img info /opt/test_create.qcow2
image: /opt/test_create.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 260K
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
[root@kvm01 ~]# qemu-img resize /opt/test_create.qcow2 20G
Image resized.
[root@kvm01 ~]# qemu-img info /opt/test_create.qcow2
image: /opt/test_create.qcow2
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 260K
cluster_size: 65536
Format specific information:
compat: 1.1
lazy refcounts: false
[root@kvm01 ~]# qemu-img resize /opt/test_create.qcow2 10G
qemu-img: qcow2 doesn't support shrinking images yet
qemu-img: This image does not support resize
[root@kvm01 ~]# qemu-img info /opt/test.raw
image: /opt/test.raw
file format: raw
virtual size: 10G (10737418240 bytes)
disk size: 0
[root@kvm01 ~]# qemu-img resize /opt/test.raw 1G
Image resized.
[root@kvm01 ~]# rm -f /opt/test*
3. 磁盘格式转换
qemu-img convert -f 原磁盘格式 -O 目标磁盘格式 原磁盘文件 目标磁盘文件 #转换磁盘格式 |
#修改虚拟机磁盘文件类型
[root@kvm01 ~]# ls /opt/
centos7-web01.raw CentOS-7-x86_64-Minimal-2009.iso
[root@kvm01 ~]# time qemu-img convert -f raw -O qcow2 /opt/centos7-web01.raw /opt/centos7-web01.qcow2
real 0m30.077s
user 0m0.020s
sys 0m29.867s
[root@kvm01 ~]# ll -h /opt/centos7-web01.*
-rw-r--r-- 1 root root 1.5G Mar 22 18:33 /opt/centos7-web01.qcow2
-rw------- 1 qemu qemu 10G Mar 22 18:33 /opt/centos7-web01.raw
[root@kvm01 ~]# virsh shutdown centos7-web01
Domain centos7-web01 is being shutdown
[root@kvm01 ~]# virsh list
Id Name State
----------------------------------------------------
[root@kvm01 ~]# virsh edit centos7-web01
Domain centos7-web01 XML configuration edited.
[root@kvm01 ~]# grep -i 'qcow2' /etc/libvirt/qemu/centos7-web01.xml
<driver name='qemu' type='qcow2'/>
<source file='/opt/centos7-web01.qcow2'/>
[root@kvm01 ~]# virsh start centos7-web01
Domain centos7-web01 started
[root@kvm01 ~]# virsh console centos7-web01
Connected to domain centos7-web01
Escape character is ^]
CentOS Linux 7 (Core)
Kernel 3.10.0-1160.el7.x86_64 on an x86_64
localhost login: root
密码:
Last login: Tue Mar 22 14:29:15 on ttyS0
[root@localhost ~]#
二、 快照管理
raw 不支持虚拟机快照功能; qcow2 支持虚拟机快照功能,并且快照就保存在qcow2的磁盘文件中; |
virsh snapshot-create 虚拟机名称 #创建虚拟机快照(以UNIX时间戳命名快照) virsh snapshot-create-as --name 快照名称 虚拟机名称 #创建虚拟机快照 virsh snapshot-list 虚拟机名称 #查看虚拟机快照 virsh snapshot-revert 虚拟机名称 --snapshotname 快照名称 #还原虚拟机快照(注意时间同步问题) virsh snapshot-delete 虚拟机名称 --snapshotname 快照名称 #删除虚拟机快照 |
[root@kvm01 ~]# virsh snapshot-create centos7-web01
Domain snapshot 1647998735 created
[root@kvm01 ~]# virsh snapshot-create-as --name install_centos7 centos7-web01
Domain snapshot install_centos7 created
[root@kvm01 ~]# virsh snapshot-list centos7-web01
Name Creation Time State
------------------------------------------------------------
1647998735 2022-03-23 09:25:35 +0800 running
install_centos7 2022-03-23 09:25:58 +0800 running
[root@kvm01 ~]# virsh snapshot-delete centos7-web01 --snapshotname 1647998735
Domain snapshot 1647998735 deleted
[root@kvm01 ~]# qemu-img info /opt/centos7-web01.qcow2
image: /opt/centos7-web01.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 1.8G
cluster_size: 65536
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK
2 install_centos7 285M 2022-03-23 09:25:59 00:24:59.925
Format specific information:
compat: 1.1
lazy refcounts: false
# 还原快照
[root@kvm01 ~]# virsh console centos7-web01
Connected to domain centos7-web01
Escape character is ^]
CentOS Linux 7 (Core)
Kernel 3.10.0-1160.el7.x86_64 on an x86_64
localhost login: root
密码:
Last login: Tue Mar 22 14:29:15 on ttyS0
[root@localhost ~]# env | grep ORACLE_HOME
[root@localhost ~]# rm -fr $ORACLE_HOME/ *
......
rm: 无法删除"/sys/hypervisor": 不允许的操作
rm: 无法删除"/tmp": 设备或资源忙
rm: 无法删除"/usr": 设备或资源忙
rm: 无法删除"/var/tmp": 设备或资源忙
[root@localhost ~]# ls
-bash: ls: command not found
[root@localhost ~]#
[root@kvm01 ~]# virsh snapshot-list centos7-web01
Name Creation Time State
------------------------------------------------------------
install_centos7 2022-03-23 09:25:58 +0800 running
[root@kvm01 ~]# virsh snapshot-revert centos7-web01 --snapshotname install_centos7
[root@kvm01 ~]# virsh console centos7-web01
Connected to domain centos7-web01
Escape character is ^]
CentOS Linux 7 (Core)
Kernel 3.10.0-1160.el7.x86_64 on an x86_64
localhost login: root
密码:
Last login: Tue Mar 22 14:29:15 on ttyS0
[root@localhost ~]# ls
anaconda-ks.cfg