在給磁盤映像擴容前需要關閉虛擬機
1. 關閉虛擬機
[root@kvm1 opt]# virsh shutdown centos7.9
[root@kvm1 opt]# virsh list --all
Id Name State
----------------------------- centos7.9 shut off
[root@kvm1 opt]# qemu-img info /var/lib/libvirt/images/centos7.9.qcow2
image: /var/lib/libvirt/images/centos7.9.qcow2
file format: qcow2
virtual size: 20 GiB (21474836480 bytes) 磁盤映像容量是20G
disk size: 1.62 GiB
cluster_size: 65536
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK
1 1753416051 0 B 2025-07-25 00:00:51 00:00:00.000
Format specific information:compat: 1.1lazy refcounts: truerefcount bits: 16corrupt: false
2. 擴容磁盤映像
如果虛擬機有快照,需要先刪除快照才能擴容
[root@kvm1 opt]# qemu-img resize /var/lib/libvirt/images/centos7.9.qcow2 +10G
[root@kvm1 opt]# qemu-img info /var/lib/libvirt/images/centos7.9.qcow2
image: /var/lib/libvirt/images/centos7.9.qcow2
file format: qcow2
virtual size: 30 GiB (32212254720 bytes) # 可以看出已經增加了10G
disk size: 1.61 GiB
cluster_size: 65536
Format specific information:compat: 1.1lazy refcounts: truerefcount bits: 16corrupt: false
resize
:擴容磁盤,支持絕對大小,例如: 100G,將磁盤擴容到 100G大小,支持相對大小,例如:+100G,將磁盤加100G.
3. 擴容文件系統
啟動虛擬機,進入到虛擬機內部擴容文件系統。
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 253:0 0 30G 0 disk
├─vda1 253:1 0 300M 0 part /boot
└─vda2 253:2 0 19.7G 0 part /
擴容/分區:
growpart
命令來自cloud-utils-growpart
工具包,使用前需要安裝它
[root@localhost yum.repos.d]# growpart /dev/vda 2
CHANGED: partition=2 start=616448 old: size=41326592 end=41943040 new: size=62298079 end=62914527
[root@localhost yum.repos.d]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
vda 253:0 0 30G 0 disk
├─vda1 253:1 0 300M 0 part /boot
└─vda2 253:2 0 29.7G 0 part / # / 分區已經被擴容了
[root@localhost yum.repos.d]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 909M 0 909M 0% /dev
tmpfs tmpfs 919M 0 919M 0% /dev/shm
tmpfs tmpfs 919M 8.6M 911M 1% /run
tmpfs tmpfs 919M 0 919M 0% /sys/fs/cgroup
/dev/vda2 xfs 20G 1.7G 19G 9% / # 可以看出文件系統還未擴容
/dev/vda1 xfs 297M 125M 173M 42% /boot
tmpfs tmpfs 184M 0 184M 0% /run/user/0
可以看出/ 分區已經被擴容可,但是文件系統還沒有擴容。
擴容文件系統:
根據文件系統的不同擴容分區的命令也不相同:
| 文件系統 | 命令 |
| --------- | ------------------------------------ |
| ext4 |resize2fs /dev/vda2
|
| xfs |xfs_growfs /
|
| btrfs |btrfs filesystem resize max /
|
[root@localhost yum.repos.d]# xfs_growfs /
meta-data=/dev/vda2 isize=512 agcount=4, agsize=1291456 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=5165824, imaxpct=25= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 5165824 to 7787259
[root@localhost yum.repos.d]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 909M 0 909M 0% /dev
tmpfs tmpfs 919M 0 919M 0% /dev/shm
tmpfs tmpfs 919M 8.6M 911M 1% /run
tmpfs tmpfs 919M 0 919M 0% /sys/fs/cgroup
/dev/vda2 xfs 30G 1.7G 29G 6% / # 可以看出文件系統已經擴容了。
/dev/vda1 xfs 297M 125M 173M 42% /boot
tmpfs tmpfs 184M 0 184M 0% /run/user/0