Auto expand last partition to use all unallocated space, using parted in batch mode

In current versions of parted, resizepart should work for the partition (parted understands 100% or things like -1s, the latter also needs -- to stop parsing options on the cmdline). To determine the exact value you can use unit s, print free. resize2fs comes afterwards for the filesystem.


Old versions of parted had a resize command that would resize both partition and filesystem in one go, it even worked for vfat.

In a Kobo ereader modification I used this to resize the 3rd partition of internal memory to the maximum: (it blindly assumes there to be no 4th partition and msdos table and things)

start=$(cat /sys/block/mmcblk0/mmcblk0p3/start)
end=$(($start+$(cat /sys/block/mmcblk0/mmcblk0p3/size)))
newend=$(($(cat /sys/block/mmcblk0/size)-8))

if [ "$newend" -gt "$end" ]
then
    parted -s /dev/mmcblk0 unit s resize 3 $start $newend
fi

So you can also obtain the values from /sys/block/.../ if the kernel supports it. But parted removed the resize command so you have to do two steps now, resizepart to grow the partition, and whatever tool your filesystem provides to grow that, like resize2fs for ext*.


Being unable to properly script parted (it asked for confirmation because the partition was mounted and contrary to other answers I found did not understand -1s or 100%), I just found the growpart tool which does exactly this.

Usage is simple: growpart /dev/sda 3 (and then resize2fs /dev/sda3, or another appropriate command for the used filesystem type).

In Debian and Ubuntu it is packaged as cloud-guest-utils.


The right way to do this is use the fact that parted has the notion of percentages. So

parted /dev/sda resize 3 100%

Tags:

Parted