KVM虚拟机磁盘在线扩容的两种方法:
virsh blockresize 扩容
1,查看虚拟机磁盘信息
[root@kvm-node1 ~]# virsh domblklist win10
Target Source
------------------------------------------------
vda /kvm/img/win10.img
hda /kvm/iso/virtio-win-0.1.215.iso
2,磁盘扩容
(1)查看当前磁盘信息
(2)win10虚拟机磁盘80G扩容100G
virsh blockresize win10 /kvm/img/win10.img 100G
3,登录虚拟机,磁盘扩容
(1)查看扩容磁盘和C盘空间
(2)C盘扩容
(3)验证磁盘扩容空间
virsh attach-disk扩容
1,查看虚拟机,磁盘
[root@kvm-node1 opt]# virsh list --all
Id Name State
----------------------------------------------------
14 win10 running
45 vm01 running
- node01 shut off
[root@kvm-node1 opt]# virsh domblklist vm01
Target Source
------------------------------------------------
vda /kvm/img/vm01.qcow2
2,创建一块qcow2虚拟磁盘
[root@kvm-node1]# qemu-img create -f qcow2 /kvm/img/vm01_add01.qcow2 5G
Formatting '/kvm/img/vm01_add01.qcow2', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 lazy_refcounts=off
3,在线添加qcow2虚拟磁盘
[root@kvm-node1 /]# virsh attach-disk vm01 /kvm/img/vm01_add01.qcow2 vdb --cache=none --subdriver=qcow2
Disk attached successfully
[root@kvm-node1 /]# virsh domblklist vm01
Target Source
------------------------------------------------
vda /kvm/img/vm01.qcow2
vdb /kvm/img/vm01_add01.qcow2
4,进入虚拟机验证
virsh console vm01
5,进入虚拟机创建分区
(1)创建分区
[root@localhost ~]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x87b41dc1.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-10485759, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759):
Using default value 10485759
Partition 1 of type Linux and of size 5 GiB is set
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
[358593.363604] vdb: vdb1
Syncing disks.
(2)创建PV
将物理硬盘格式化成PV(物理卷) 使用pvcreate命令
[root@localhost ~]# pvcreate /dev/vdb1
Physical volume "/dev/vdb1" successfully created.
查看当前vg信息
(3)扩容VG
把转换好的物理卷并到vg(卷组)中, 使用vgcreate命令
[root@localhost ~]# vgextend centos /dev/vdb1
Volume group "centos" successfully extended
(4)扩容LV
查看逻辑卷,选择对应逻辑卷路径
(5)扩容当前逻辑卷空间
[root@localhost ~]# lvextend /dev/centos/root /dev/vdb1
Size of logical volume centos/root changed from 10.00 GiB (2560 extents) to <15.00 GiB (3839 extents).
Logical volume centos/root successfully resized.
(6)写入文件系统,使扩容生效
使用ext文件系统,可以使用resize2fs命令替换xfs_growfs命令
(7)查看LVM信息
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vda2 centos lvm2 a-- <19.51g <5.51g
/dev/vdb1 centos lvm2 a-- <5.00g 0
(8)查看根分区扩容效果
6,修改虚拟机配置文件
把新增的磁盘配置写到虚拟机磁盘配置,避免重启丢失
(1)查看当前虚拟机vm01的磁盘配置
[root@kvm-node1 /]# virsh dumpxml vm01
(2)编辑虚拟机配置文件virsh edit vm01
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none'/>
<source file='/kvm/img/vm01_add01.qcow2'/>
<target dev='vdb' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</disk>
7,重启验证
[root@kvm-node1 /]# virsh console vm01