大网数据广告活动
服务器运维问答

Resize partition to maximum using parted in non-interactive mode

· 52CE 我爱测

Since using fdisk in this case is fairly complicated due to the fact that the non-interactive usage is probably not possible or at leasr very complicated (using printf), i want to use parted resizepart for resizing a partition to…

问题原文

原帖链接 · Eugen Mayer · CC BY-SA 4.0

Since using fdisk in this case is fairly complicated due to the fact that the non-interactive usage is probably not possible or at leasr very complicated (using printf), i want to use parted resizepart for resizing a partition to it's maximum size.

This can be utilized in scenarios like actual disk-resize in you hypervisor / cloud and then you need to adjust your logical volume / pv to the new size (LVM case) or you want to adjust the partition size of a normal partition to it's maximum.

Let's assume i want to resize partition /dev/sda1 on obvisouly disk /dev/sda1 to its maximum possible size - how would i do this without getting asked any questions at all.

Eventhough parted /dev/sda resizepart 1 exists, it needs me to calculate and enter the maximum disk size - and that is the actual clue I working on here

社区高分解答

原帖链接 · Timur Bakeyev · CC BY-SA 4.0

It seems that different versions of parted behave quite differently, so this is applicable to the parted version 3.*, that comes at least with CentOS7 and CentOS8.

  1. Make sure that OS is aware of the disk size increase since the last scan(reboot):
# echo 1 > /sys/block/sda/device/rescan
  1. Check that there is free space available after the last partition:
# parted -s -a opt /dev/sda "print free"
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  1075MB  1074MB  primary  xfs          boot
 2      1075MB  32.2GB  31.1GB  primary               lvm
        32.2GB  53.7GB  21.5GB           Free Space
  1. Resize last partition to the maximum available disk space:
# parted -s -a opt /dev/sda "resizepart 2 100%"
# echo $?
0

This operation doesn't have any indication of it's success except the exit code.

  1. You can verify that partition size was increased by repeating:
# parted -s -a opt /dev/sda "print free"
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  1075MB  1074MB  primary  xfs          boot
 2      1075MB  53.7GB  52.6GB  primary               lvm

As a bonus you can fit all this into one command line(don't miss step 0):

# parted -s -a opt /dev/sda "print free" "resizepart 2 100%" "print free"
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  1075MB  1074MB  primary  xfs          boot
 2      1075MB  32.2GB  31.1GB  primary               lvm
        32.2GB  53.7GB  21.5GB           Free Space

Model: VMware Virtual disk (scsi)
Disk /dev/sda: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  1075MB  1074MB  primary  xfs          boot
 2      1075MB  53.7GB  52.6GB  primary               lvm

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-08-26)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。