Long story short, I need to perform this all automatically on boot (embedded system).

Our engineers will flash images to production devices. These images will contain a small partition table. On boot, I need to automatically expand the last partition (#3) to use all the available space on the disk.

Here is what I get when I look at the free space on my disk.

> parted /dev/sda print free
Model: Lexar JumpDrive (scsi)
Disk /dev/sda: 32.0GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
        17.4kB  1049kB  1031kB  Free Space
 1      1049kB  25.3MB  24.2MB  fat16        primary  legacy_boot
        25.3MB  26.2MB  922kB   Free Space
 2      26.2MB  475MB   449MB   ext4         primary
 3      475MB   1549MB  1074MB  ext4         primary
        1549MB  32.0GB  30.5GB  Free Space

I need to expand partition 3 by N (30.5GB) number of bytes

How do I perform this step automatically, with no prompt? This needs to work with a dynamic size of space available after the 3rd partition.

  • Dont expect to receive a ready to go script, without a try! So can you show to us what did you try until now? Another thing the partition that you are resizing must be not mounted during the operation.... Jun 23, 2017 at 20:22
  • I have tried resizepart command, and it works, but it requires an exact size. I need it to be dynamic.
    – Paul Knopf
    Jun 23, 2017 at 20:28
  • resize2fs might be what I am looking for. linux.die.net/man/8/resize2fs
    – Paul Knopf
    Jun 23, 2017 at 20:29

8 Answers 8


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.

  • 9
    This is the most convenient way I could find for this right now. It worked wonderfully.
    – SebiF
    Sep 13, 2018 at 19:16
  • This was the solution for me - parted didn't like ext4 as the file system, and there seemed to be no way of avoiding it attempting resizing the file system... Feb 13, 2019 at 14:00
  • not working for Ubuntu 18.04 Apr 23, 2019 at 12:39
  • @DreamFlasher I can't tell why it did not work for you. As far as I can see you can just install the cloud-guest-utils package and since this is the only function provided by growpart it should work unless there's a bug, it's used incorrectly or something out of its control is going wrong. Note that the last argument (3 in the example) is the partition to be resized, counting from 1, and that sufficient space needs to be available right after the partition to be able to resize it this way.
    – Ivo Smits
    Apr 24, 2019 at 17:26
  • This is the answer. So simple, and growpart is installed by default on ubuntu 21.
    – jonesy19
    May 31, 2021 at 12:19

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" ]
    parted -s /dev/mmcblk0 unit s resize 3 $start $newend

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*.

  • Side question, what's with the 8 bytes at the end?
    – Paul Knopf
    Jun 24, 2017 at 23:11
  • Avoid resizing by a single sector... you'd have to change that to -40 or something (GPT backup header at end of disk). Jun 25, 2017 at 1:29
  • Does parted not support automatic extension from the command line? Because, it does support it interactively. If run interactively, it'll ask if you want to fix the GPT partition table to match the size of the disk (type fix), and then you can run resizepart 3 -1. I've been trying to figure out how to get it to do that from the command line.
    – Harvey
    Oct 9, 2017 at 21:10
  • 1
    Not so fast: The resize command has been removed in parted 3.0. Try sfdisk or gparted.
    – GregD
    Mar 14, 2018 at 2:14
  • 7
    For those looking for the actual resizepart command, I think this is what the OP had in mind: sudo parted /dev/mmcblk0 resizepart 3 100% where /dev/mmcblk0 is the drive and 3 is the partition. May 12, 2020 at 10:48

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

parted /dev/sda resize 3 100%
  • 8
    outdated, not working with Ubuntu 18.04 and later Apr 23, 2019 at 12:38
  • 1
    Slightly the resize command has gone with parted version 3, you now have to use the resizepart command and then separately resize the underlying file system or PV if that is what you are using. For ext4 the command is resize2fs, or for XFS it is xfs_growfs. Both of which are normally done when the file system is mounted and the argument is the mount point in both cases. Apr 26, 2019 at 13:35
  • Plus resizepart does not understand 100%
    – ChrisWue
    Jul 24, 2020 at 2:33
  • resizepart is happy with 100% in RHEL7 (Parted 3.1). Jun 14, 2021 at 18:20
  • 1
    parted /dev/sda resizepart 3 100% worked well
    – farinspace
    Dec 26, 2021 at 19:46

In Ubuntu 18.04, I am able to resize to fill unallocated space in a script with the following:

sudo parted -s /dev/sdb "resizepart 2 -1" quit
sudo mount -av
sudo umount /dev/sdb2
sudo mount /dev/sdb2 /mnt
sudo resize2fs /dev/sdb2

It may not be necessary to mount before resizing. I did it to suppress a query from resize2fs for user input.

  • 1
    I was able to resize a root partition without mounting or umounting it.
    – lepe
    Nov 10, 2020 at 7:04

Alternatively, you can do a one liner with sfdisk:

echo ", +" | sfdisk -N 3 /dev/sda

See Karel Zak's blog post for more details.

  • don't forget to run then partprobe and resize2fs /dev/sda3
    – bebbo
    Mar 15, 2022 at 20:40

i know the question mentioned using parted, but here is a bash script using fdisk and resize2fs.

I use it on Raspberry Pi, but you can use it anywhere those 2 tools are available, just change the target disk (sda, sdb, ...).

Always be careful while manipulating partitions: you might end up breaking your system !


#   This bash script extends the last partition of the specified disk
#   To the maximum size available.

# target disk

# get last partition
part=$(grep  "${disk}" /proc/partitions | tail -1 | awk '{print $4}' | xargs)
partN=$(echo $part | tail -c 2)    

# fdisk: delete and recreate the last partition with the largest size possible.
echo d # Delete partition
echo $partN # Last partition
echo n # Add a new partition
echo p # Primary partition
echo $partN # Last partition
echo   # First sector (Accept default: 1)
echo   # Last sector (Accept default: varies)
echo w # Write changes
) | fdisk /dev/$disk 

# update filesystem to match new partition size
resize2fs /dev/$part

Just to summarize a few of the comments into a method: (for in my case sda2)

parted /dev/sda resizepart 2 100%

resize2fs /dev/sda2

In the short video step-by-step tutorial "How to Resize a Live Filesystem on Linux" is shown, how to achieve this a very simple way. I've just extended the /dev/sda2 on a VirtualBox VM after resizing the VDI (virtual hard drive). It works pretty well:

  1. Back up the data.
  2. Start fdisk: sudo fdisk /dev/sda.
  3. Print the partitions: p.
  4. Delete the partition, you want to resize (it's a kind of unlinking, the data is not removed): d. If you have multiple partitions, you'll be asked, which one should be deleted.
  5. Create a new partition: n. Follow the dialog for setting the partition type, the partition number, the first sector, and the last sector. The last two parameters define the size of the new partition. By default the space between the first and the last sector will cover the complete available free space. The last option: Choose whether the signature should be removed.
  6. See the changes (and compare the result with the state before): p.
  7. Save the changes and quit fdisk: w (use q to exit without saving).
  • 1
    Nope. The question is how to extend an existing partition not how to add another one. Jun 26, 2021 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.