14

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.... – Luciano Andress Martini Jun 23 '17 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 '17 at 20:28
  • resize2fs might be what I am looking for. linux.die.net/man/8/resize2fs – Paul Knopf Jun 23 '17 at 20:29
18

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

  • This is exactly what I was looking for. Thanks for your help! – Paul Knopf Jun 24 '17 at 23:10
  • Side question, what's with the 8 bytes at the end? – Paul Knopf Jun 24 '17 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). – frostschutz Jun 25 '17 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 '17 at 21:10
  • Not so fast: The resize command has been removed in parted 3.0. Try sfdisk or gparted. – GregD Mar 14 '18 at 2:14
12

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.

  • This is the most convenient way I could find for this right now. It worked wonderfully. – SebiF Sep 13 '18 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... – Antti Haapala Feb 13 at 14:00
  • not working for Ubuntu 18.04 – DreamFlasher Apr 23 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 at 17:26
5

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

parted /dev/sda resize 3 100%
  • outdated, not working with Ubuntu 18.04 and later – DreamFlasher Apr 23 at 12:38
  • 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. – Jonathan Buzzard Apr 26 at 13:35

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.