问题原文
原帖链接 · 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.
- Make sure that OS is aware of the disk size increase since the last scan(reboot):
# echo 1 > /sys/block/sda/device/rescan
- 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
- 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.
- 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
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
