如何扩展/增加KVM虚拟机(VM)磁盘大小
时间:2020-02-23 14:45:10 来源:igfitidea点击:
如何在KVM中扩展/增加/延长虚拟磁盘?
我个人使用KVM来所有Linux虚拟化项目。
有时我需要将磁盘空间扩展或者添加到我的运行VM(Guest)以满足不断增长的软件要求。
KVM使用QEMU,它支持多种图像类型,其中RAW,COW,QCOW,QCOW2,VMDK,VDI 等。
"本机"和最灵活的类型是QCOW2,支持编写写入,加密,压缩和VM快照。
第1步:关闭VM
在扩展Guest Machib虚拟磁盘之前,我们需要先将其关闭。
$sudo virsh list Id Name State ---------------------- 4 rhel8 running
如果宾客机器处于运行状态,请使用其ID或者名称关闭IT。
$sudo virsh shutdown rhel8 Domain rhel8 is being shutdown
确认在继续管理其磁盘之前确实如此。
$ sudo virsh list Id Name State -------------------
第2步:扩展客户端磁盘
找到客户操作系统磁盘路径。
$sudo virsh domblklist rhel8 Target Source ---------------------------------------------- vda /var/lib/libvirt/images/rhel8.qcow2 sda OR use: $sudo virsh dumpxml rhel8 | egrep 'disk type' -A 5 <disk type='file' device='disk'> <driver name='qemu' type='qcow2' <source file='/var/lib/libvirt/images/rhel8.qcow2' <backingStore <target dev='vda' bus='virtio' <address type='pci' domain='0x0000' bus='0x04' slot='0x00' function='0x0' - <disk type='file' device='cdrom'> <driver name='qemu' type='raw' <target dev='sda' bus='sata' <readonly <address type='drive' controller='0' bus='0' target='0' unit='0' </disk>
我们可以从虚拟机管理器GUI获取相同的信息。
我的VM磁盘位于'/var/lib/libvirt/images/rhel8.qcow2'中。
$sudo qemu-img info /var/lib/libvirt/images/rhel8.qcow2 image: /var/lib/libvirt/images/rhel8.qcow2 file format: qcow2 virtual size: 30G (42949672960 bytes) disk size: 2.0G cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: true refcount bits: 16 corrupt: false
第3步:扩展Guest VM磁盘
自从我们知道虚拟机磁盘的位置以来,让我们将其扩展到所需的容量。
sudo qemu-img resize /var/lib/libvirt/images/rhel8.qcow2 +10G
请注意,QEMU-IMG无法调整具有快照的图像大小。
我们需要先删除所有VM快照。
看到这个例子:
$sudo virsh snapshot-list rhel8 Name Creation Time State ------------------------------------------------- snapshot1 2019-04-16 08:54:24 +0300 shutoff $sudo virsh snapshot-delete --domain rhel8 --snapshotname snapshot1 Domain snapshot snapshot1 deleted $sudo virsh snapshot-list rhel8 Name Creation Time State ------------------------------
然后在磁盘容量之前使用"+"扩展磁盘。
$sudo qemu-img resize /var/lib/libvirt/images/rhel8.qcow2 +10G Image resized.
我们还可以使用virsh命令调整大小。
这需要域名运行。
$sudo qemu-img info /var/lib/libvirt/images/rhel8.qcow2 image: /var/lib/libvirt/images/rhel8.qcow2 file format: qcow2 virtual size: 30G (42949672960 bytes) disk size: 2.0G cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: true refcount bits: 16 corrupt: false $sudo virsh start rhel8 $sudo virsh blockresize rhel8 /var/lib/libvirt/images/rhel8.qcow2 40G Block device '/var/lib/libvirt/images/rhel8.qcow2' is resized
使用fdisk命令确认磁盘大小。
$sudo fdisk -l /var/lib/libvirt/images/rhel8.qcow2 Disk /var/lib/libvirt/images/rhel8.qcow2: 30.2 GiB, 32399818752 bytes, 63280896 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
第4步:生长VM分区
现在上电了VM
$sudo virsh start rhel8 Domain rhel8 started
将VM作为root用户或者使用具有sudo的用户帐户ssh。
$ssh rhel8 Last login: Fri Apr 19 06:11:19 2019 from 192.168.122.1 [Hyman@theitroad ~]$
检查新磁盘布局。
$lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 1024M 0 rom vda 252:0 0 40G 0 disk ├─vda1 252:1 0 1G 0 part /boot └─vda2 252:2 0 29G 0 part ├─rhel-root 253:0 0 26.9G 0 lvm / └─rhel-swap 253:1 0 2.1G 0 lvm [SWAP]
我的VM总磁盘容量现在 40GB
以前这是30GB。