KVM虚拟机磁盘在线扩容
12-KVM虚拟机磁盘在线扩容
虚拟机磁盘在线扩容的两种方法:
方法一:通过virsh blockresize扩容当前磁盘,然后通过虚拟机系统LVM逻辑卷管理方式进行管理,扩展磁盘空间。
方法二:新创建一块磁盘,virsh attach-disk命令在线添加到当前KVM虚拟机,然后通过虚拟机系统lvm逻辑卷管理方式进行管理,扩展磁盘空间。(不推荐)
blockresize方式在线扩容
查看虚拟机状态
virsh domstate VM6
![](https://pic1.zhimg.com/80/v2-4bf85a1512aa3f2d0cc7b35b74db2ba4_720w.webp)
查看磁盘信息
查看虚拟机VM6磁盘列表:virsh domblklist VM6
查看VM6.qcow2的磁盘信息:qemu-img info VM6.qcow2
![](https://pic3.zhimg.com/80/v2-197c514623d8faf9ba6eda1f97854f82_720w.webp)
磁盘扩容
VM6从60G扩容到100G:virsh blockresize VM6 /data-cyj/VM6.qcow2 100G
virsh blockresize命令用法:
virsh blockresize <domain> <path> <size>
domain 虚拟机名称
path 磁盘路径,可填 vda或者/data-cyj/VM6.qcow2
size 扩容后容量
![](https://pic4.zhimg.com/80/v2-2cb2dff02ec123145f6ece08bc7b4557_720w.webp)
登录虚拟机配置LVM磁盘扩展
- vnc登录
![](https://pic2.zhimg.com/80/v2-3cb14ba7e63abf42a6229b8799df23d5_720w.webp)
- 打开计算机管理->磁盘管理器,当前C:盘容量为59.46G,出现新增未分配的盘40G就是刚刚扩容的容量。
![](https://pic4.zhimg.com/80/v2-eed4eae390a7d569ca73f4ee41d13ad7_720w.webp)
- 选中C:盘,右键单击扩展卷,出现扩展向导,点击下一步
![](https://pic4.zhimg.com/80/v2-9937006563ce14a18d364cf47bd7ac6b_720w.webp)
- 选择磁盘0,点击下一步
![](https://pic1.zhimg.com/80/v2-c922d56687a5c72c0dc43857beae4f08_720w.webp)
- 完成扩容
![](https://pic2.zhimg.com/80/v2-a7bcf6209929e310c010900bbdbeb631_720w.webp)
- 查看C:盘最新容量,为99.46G,扩容40G成功。
![](https://pic2.zhimg.com/80/v2-fb6a3de3fa1875a728ce40eb72a7c025_720w.webp)
attach-disk方式在线扩容
查看磁盘信息
qemu-img info VM3.qcow2
![](https://pic3.zhimg.com/80/v2-cd6349ff590ea65f8aaba5e617a6f38e_720w.webp)
创建磁盘
创建相同格式的磁盘:qemu-img create -f qcow2 /data-cyj/VM3-add.qcow2 10G
![](https://pic3.zhimg.com/80/v2-6ee0618abc9cb87749f5543c02f1e092_720w.webp)
在线添加磁盘
查看虚拟机现有磁盘:virsh domblklist VM3
![](https://pic3.zhimg.com/80/v2-92d456bf15f6db1cd80945cf553d78b6_720w.webp)
在线添加磁盘:
virsh attach-disk VM3 /data-cyj/VM3-add.qcow2 vdb --cache=none --subdriver=qcow2
注意:--subdriver必须使用,否则默认为raw格式
----persistent 可以在线永久添加磁盘,如果使用此参数,直接跳过第5步修改配置的步骤
![](https://pic1.zhimg.com/80/v2-90a38ceb7b9d8814c3da06a9c09afba8_720w.webp)
查看添加后虚拟机磁盘列表:virsh domblklist VM3
![](https://pic2.zhimg.com/80/v2-9b1a2ba9d418cb1fb35a6d2b5424dc45_720w.webp)
查看虚拟机磁盘情况
Console登录虚拟机查看磁盘情况
![](https://pic2.zhimg.com/80/v2-9f7f2da0887e53e016f78b748802fd35_720w.webp)
修改配置
查看当前虚拟机的配置,并将新增加的磁盘信息添加到VM3.xml中。
如果第3步在线添加磁盘时使用了persistent 参数,那么此步骤跳过。
- 查看磁盘配置
[root@localhost data-cyj]# virsh dumpxml VM3
<domain type='kvm' id='9'>
<name>VM3</name>
<uuid>5fa71db7-f304-4100-bc0e-01876fc2a84b</uuid>
<memory unit='KiB'>524288</memory>
<currentMemory unit='KiB'>524288</currentMemory>
<vcpu placement='static'>1</vcpu>
<resource>
<partition>/machine</partition>
</resource>
<os>
<type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
<boot dev='hd'/>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='custom' match='exact' check='full'>
<model fallback='forbid'>IvyBridge-IBRS</model>
<feature policy='require' name='md-clear'/>
<feature policy='require' name='spec-ctrl'/>
<feature policy='require' name='ssbd'/>
<feature policy='require' name='hypervisor'/>
<feature policy='require' name='xsaveopt'/>
</cpu>
<clock offset='utc'>
<timer name='rtc' tickpolicy='catchup'/>
<timer name='pit' tickpolicy='delay'/>
<timer name='hpet' present='no'/>
</clock>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>destroy</on_crash>
<pm>
<suspend-to-mem enabled='no'/>
<suspend-to-disk enabled='no'/>
</pm>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2'/>
<source file='/data-cyj/VM3.qcow2'/>
<backingStore/>
<target dev='vda' bus='virtio'/>
<alias name='virtio-disk0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</disk>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none'/>
<source file='/data-cyj/VM3-add.qcow2'/>
<backingStore/>
<target dev='vdb' bus='virtio'/>
<alias name='virtio-disk1'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</disk>
<disk type='file' device='cdrom'>
<driver name='qemu'/>
<target dev='hda' bus='ide'/>
<readonly/>
<alias name='ide0-0-0'/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
<controller type='usb' index='0' model='ich9-ehci1'>
<alias name='usb'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci1'>
<alias name='usb'/>
<master startport='0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci2'>
<alias name='usb'/>
<master startport='2'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci3'>
<alias name='usb'/>
<master startport='4'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/>
</controller>
<controller type='pci' index='0' model='pci-root'>
<alias name='pci.0'/>
</controller>
<controller type='ide' index='0'>
<alias name='ide'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<interface type='bridge'>
<mac address='52:54:00:21:a0:6f'/>
<source bridge='br0'/>
<target dev='vnet4'/>
<model type='virtio'/>
<alias name='net0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<serial type='pty'>
<source path='/dev/pts/7'/>
<target type='isa-serial' port='0'>
<model name='isa-serial'/>
</target>
<alias name='serial0'/>
</serial>
<console type='pty' tty='/dev/pts/7'>
<source path='/dev/pts/7'/>
<target type='serial' port='0'/>
<alias name='serial0'/>
</console>
<input type='mouse' bus='ps2'>
<alias name='input0'/>
</input>
<input type='keyboard' bus='ps2'>
<alias name='input1'/>
</input>
<graphics type='vnc' port='5903' autoport='no' listen='0.0.0.0'>
<listen type='address' address='0.0.0.0'/>
</graphics>
<video>
<model type='cirrus' vram='16384' heads='1' primary='yes'/>
<alias name='video0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<memballoon model='virtio'>
<alias name='balloon0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</memballoon>
</devices>
<seclabel type='dynamic' model='dac' relabel='yes'>
<label>+107:+107</label>
<imagelabel>+107:+107</imagelabel>
</seclabel>
</domain>
- 编辑虚拟机
virsh edit VM3,编辑虚拟机,将vdb 磁盘的配置(①中已标红),添加到配置文件。
![](https://pic2.zhimg.com/80/v2-9a8e4d7747212a4f8f54ec9231936f35_720w.webp)
登录虚拟机,配置LVM磁盘扩展
- console登录VM3:
![](https://pic3.zhimg.com/80/v2-2b2772aa5c5b7362d58d7239d47c6e62_720w.webp)
- 开始分区
fdisk -l 查询
fdisk /dev/vdb
n创建分区,其他默认
t选择格式8e
w保存退出
执行partprobe 通知系统分区表的变化
![](https://pic1.zhimg.com/80/v2-924251040a3db1b043bb69ded8eba0a8_720w.webp)
/dev/vdb1创建成功
![](https://pic1.zhimg.com/80/v2-c8b983334d672af4f7bab368a1433a34_720w.webp)
- LVM磁盘扩展
df -h 查看扩容前的容量
![](https://pic3.zhimg.com/80/v2-e556b2d2b7e372a9678058c1f9a866b2_720w.webp)
pvcreate /dev/vdb1 创建物理卷
pvs查询pv
vgextend centos /dev/vdb1 扩容
vgs查询group
#Lvextend 只是扩展了卷的大小,执行xfs_growfs 后才能将扩展空间加到文件系统
lvextend -l +100%FREE /dev/centos/root 扩容逻辑卷大小
xfs_growfs /dev/centos/root 更新修改到文件系统
df -h查询扩容后容量
![](https://pic3.zhimg.com/80/v2-940126e567a485c762ccd3eb2f33426e_720w.webp)
df -h查询扩容后,发现容量从17G扩展到27G,扩容成功
![](https://pic1.zhimg.com/80/v2-58c8b5bf953bc09594636a7ceb398900_720w.webp)