KVM虚拟机磁盘管理

1.KVM虚拟机虚拟磁盘格式转换

  • 创建一块qcow2的虚拟硬盘
#创建一块qcow2的虚拟硬盘(仅测试使用,无实际意义)
[root@kvm-server-01 tmp]# qemu-img create -f qcow2 centos7.qcow2 1G
Formatting 'centos7.qcow2', fmt=qcow2 size=1073741824 encryption=off cluster_size=65536 lazy_refcounts=off 
[root@kvm-server-01 /tmp]# ll -h centos7.qcow2 
-rw-r--r-- 1 root root 193K Jul 30 20:04 centos7.qcow2

#查看虚拟机硬盘信息
[root@kvm-server-01 /tmp]# qemu-img info centos7.qcow2 
image: centos7.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@kvm-server-01 ~]# qemu-img --help | grep convert
  convert [-c] [-p] [-q] [-n] [-f fmt] [-t cache] [-T src_cache] [-O output_fmt] [-o options] [-s snapshot_name] [-S sparse_size] filename [filename2 [...]] output_filename

#转换原有的raw格式为qcow2格式
-f      #指定原来格式 
-O      #转换成什么格式
[root@kvm-server-01 opt]# qemu-img convert -f raw -O qcow2 /opt/centos7.6.raw /opt/centos7.6.qcow2

#转化之后,要想使用,要修改kVM虚拟机的主配置文件
[root@kvm-server-01 opt]# virsh edit centos7.6
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>         #将原来的raw改成qcow2
      <source file='/opt/centos7.6.qcow2'/>     #将原来的raw改成qcow2
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
    </disk>

#启动kvm虚拟主机centos7.6
[root@kvm-server-01 opt]# virsh start centos7.6
[root@kvm-server-02 opt]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 9     centos7.6                      running

2.KVM快照管理

  • raw格式与qcow2格式比较
raw         #性能特别好   不支持快照   占用磁盘空间大  
qcow2       #支持快照    实际使用多少大小,就占用多少大小
  • 创建快照
#创建快照 
[root@kvm-server-01 ~]# virsh snapshot-create centos7.6 
Domain snapshot 1596113325 created

#创建一个关机快照
[root@kvm-server-01 ~]# virsh shutdown centos7.6 
Domain centos7.6 is being shutdown
[root@kvm-server-01 ~]# virsh snapshot-create centos7.6 
Domain snapshot 1596113410 created

#查看主机快照列表
[root@kvm-server-01 ~]# virsh snapshot-list centos7.6 
 Name                 Creation Time             State
------------------------------------------------------------
 1596113325           2020-07-30 20:48:45 +0800 running
 1596113410           2020-07-30 20:50:10 +0800 shutoff

#查看快照信息
[root@kvm-server-01 ~]# virsh snapshot-info centos7.6 --snapshotname 1596113325 
Name:           1596113325
Domain:         centos7.6
Current:        no
State:          running
Location:       internal
Parent:         1596092718
Children:       1
Descendants:    1
Metadata:       yes
  • 模拟虚拟机故障,登陆虚拟机,进行删除操作。
#进入centos7.6虚拟主机,进行删根,
[root@kvm-server-01 ~]# virsh console centos7.6
[root@localhost ~]# rm -rf /*
[root@localhost ~]# ls
-bash: /bin/ls: No such file or directory

#还原快照
[root@kvm-server-01 ~]# virsh snapshot-revert centos7.6 1596113325

#登录测试
[root@kvm-server-01 ~]# virsh console centos7.6
Connected to domain centos7.6
Escape character is ^]

CentOS Linux 7 (Core)
Kernel 3.10.0-957.el7.x86_64 on an x86_64

localhost login: root
Password: 
Last login: Thu Jul 30 03:21:12 on ttyS0

[root@localhost ~]# ll
total 20
-rw-------. 1 root root 1243 Jul 28 04:52 anaconda-ks.cfg
  • 快照配置文件位置
[root@kvm-server-01 ~]# tree /var/lib/libvirt/qemu/snapshot/
/var/lib/libvirt/qemu/snapshot/
└── centos7.6
    ├── 1596113325.xml
    └── 1596113410.xml

1 directory, 2 files
  • 删除快照
[root@kvm-server-01 ~]# virsh snapshot-delete centos7.6 1596113325
Domain snapshot 1596113325 deleted

3.KVM在线热添加硬盘

  • 创建一块硬盘
#创建硬盘
[root@kvm-server-01 ~]# mkdir /opt/disk/
[root@kvm-server-01 ~]# cd /opt/disk/
[root@kvm-server-01 disk]# qemu-img create -f qcow2 /opt/disk/centos7.6-add01.qcow2 5G
Formatting '/opt/disk/centos7.6-add01.qcow2', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 lazy_refcounts=off

#查看硬盘信息
[root@kvm-server-01 disk]# qemu-img info centos7.6-add01.qcow2 
image: centos7.6-add01.qcow2
file format: qcow2
virtual size: 5.0G (5368709120 bytes)
disk size: 196K
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
  • 给kvm虚拟机添加硬盘
#添加硬盘
[root@kvm-server-01 disk]# virsh attach-disk centos7.6 /opt/disk/centos7.6-add01.qcow2 vdb --live --cache=none --subdriver=qcow2
Disk attached successfully

#选项说明
vdb             #第二块硬盘
--live          #热添加
--cache=none    #宿主机对客户机的镜像的读写cache开启,off表示关闭
--subdriver     #驱动类型

#进入虚拟主机,查看磁盘信息
[root@kvm-server-01 disk]# virsh console centos7.6
[root@localhost ~]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0              11:0    1 1024M  0 rom  
vda             252:0    0   10G  0 disk 
├─vda1          252:1    0    1G  0 part /boot
└─vda2          252:2    0    9G  0 part 
  ├─centos-root 253:0    0    8G  0 lvm  /
  └─centos-swap 253:1    0    1G  0 lvm  [SWAP]
vdb             252:16   0    5G  0 disk        #新添加的硬盘vdb,可通过对其分区格式化后挂载使用
  • 剥离此块硬盘
[root@kvm-server-01 disk]# virsh detach-disk centos7.6 vdb
Disk detached successfully
点赞

发表回复