无需重启即可从虚拟机调整分区大小

无需重启即可从虚拟机调整分区大小

我在 proxmox 服务器上运行了多台虚拟机。它们都有一个没有 LVM 的磁盘。当磁盘空间用完时,我会关闭虚拟机,然后扩大分区,挂载 gparted ISO,从此 ISO 启动,调整分区大小,然后再次启动虚拟机。

现在我想知道是否有一种更简单的方法,而不需要重新启动虚拟机。

答案1

切换有关根文件系统的一些信息:

df /
# Filesystem     1K-blocks    Used Available Use% Mounted on
# /dev/sda2       31365948 5249140  24500424  18% /

/dev/sda2是块设备的第二个分区/dev/sda。要调整此分区的大小,可以使用工具parted

parted /dev/sda
# (parted) resizepart 2
# Warning: Partition /dev/sda2 is being used. Are you sure you want to continue?
# Yes/No? yes
# End?  [41.0GB]? 82GB
# (parted) q
# Information: You may need to update /etc/fstab.

更改分区大小后,您还必须调整文件系统的大小:

resize2fs /dev/sda2
# resize2fs 1.45.5 (07-Jan-2020)
# Filesystem at /dev/sda2 is mounted on /; on-line resizing required
# old_desc_blocks = 4, new_desc_blocks = 9
# The filesystem on /dev/sda2 is now 18019403 (4k) blocks long.

这是为了检查修改:

df /
# Filesystem     1K-blocks    Used Available Use% Mounted on
# /dev/sda2       70817420 5253016  62349464   8% /

答案2

以下是如何调整到最大尺寸

echo 1 > /sys/block/sda/device/rescan
echo 1 > /sys/block/sdb/device/rescan

partprobe

use gdisk or parted

gdisk fix warning and extend disk manually

parted change partion size

## parted can executed as command line. but this is very dangerous
parted -s /dev/sdb "resizepart 2 -1" quit
parted -s /dev/sdb "resizepart 3 100%" quit
resizepart 3 100%

pvresize /dev/sda3

lvextend -l +100%FREE cs/root

xfs_growfs /dev/cs/root

相关内容