扩展虚拟机linux硬盘大小,扩展/增加KVM虚拟机(VM)磁盘大小的方法

我个人将KVM用于所有Linux虚拟化项目,有时我需要为运行的VM(guest)扩展或添加磁盘空间,以满足不断增长的软件需求,KVM使用支持多种image类型的QEMU,其中包括raw、cow、qcow、qcow2、vmdk、vdi。native和最灵活的类型是qcow2,它支持写入、加密、压缩和VM快照上的复制。

一、关闭VM

在扩展客户机虚拟磁盘之前,需要先将其关闭:

$ sudo virsh list

d0ec69e98b51e7b72734959c5ba0cf7d.png

如果你的客户机处于运行状态,请使用其ID或名称将其关闭:

$ sudo virsh shutdown rhel8

Domain rhel8 is being shutdown

在继续管理其磁盘之前确认它确实已经关闭:

$  sudo virsh list

a66c3dee821d13803d34527edb799b6e.png

二、扩展客户操作系统磁盘

找到你的客户操作系统磁盘路径:

$ sudo virsh domblklist rhel8

或者运行以下命令:

$ sudo virsh dumpxml rhel8 | egrep 'disk type' -A 5

a15f69bd711d133c8e546506bd565e03.png

可以从Virtual Machine Manager 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

三、扩展guest VM磁盘

由于我们知道虚拟机磁盘的位置,因此我们将其扩展到我们所需的容量:

sudo qemu-img resize /var/lib/libvirt/images/rhel8.qcow2 +10G

请注意,qemu-img无法调整具有快照的image的大小,需要先删除所有VM快照,看这个例子:

$ sudo virsh snapshot-list rhel8

$ sudo virsh snapshot-delete --domain rhel8 --snapshotname snapshot1

$ sudo virsh snapshot-list rhel8

2d5a1aa736670289389d5431a38b5bf3.png

然后在磁盘容量之前使用“+”扩展磁盘:

$ sudo qemu-img resize /var/lib/libvirt/images/rhel8.qcow2 +10G

Image resized.

还可以使用virsh命令调整大小,这需要domain运行:

$ 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

四、增加VM分区

现在启动VM:

$ sudo virsh start rhel8

Domain rhel8 started

以root用户身份或使用具有sudo的用户帐户SSH到你的VM:

$ ssh rhel8

Last login: Sun Apr 21 13:11:19 2019 from 192.168.122.1

[jmutai@rhel8 ~]$

检查新磁盘布局:

$ lsblk

075e593e528dde86ffa2f1dd7f99a8f3.png

至此,操作成功,我的VM总磁盘容量现在是40GB,之前是30GB。

相关主题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值