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
Best Answer
HINT: this script is compatible with parted v3+ OOTB, if you have parted 2, you need to change
parted resizepart
toparted resize
lets put this into a script, the acualy command is a onliner, we just add a lot more to ensure the first 2 parameters are set:
usage
Save the script above as
resize.sh
and make it executableFor example, in case you have a vg vgdata with a lv 'data' on /dev/sdb1 while using LVM, the whole story would look like
Thats it, resized logical volume including resized filesystem ( -r ) - all done, check it with
df -h
:)Explanation
What we use to find the disk size is
a) So get the disk-informations of our device in question
printf %s\\n 'unit MB print list' | parted | grep "Disk /dev/sda
we useprintf %s\\n 'unit MB print list'
to ensure the units are displayed as MB, since otherwise it will vary by disk size ( MB, G, T ) and there is no better way to do this with parted 3 or 4 yetb) then use the 3rd column of the output (disk size)
cut -d' ' -f3
(divided by space)c) and finally cut off the unit 'MB' with blanc using
tr -d MB
Follow ups
I published the script on https://github.com/EugenMayer/parted-auto-resize so if anything is to improved feature wise, use pull requests there ( anything not in scope of this question )