I'm currently writing a script which is expected to repartition the /dev/sda. It will be running an OS from a usb stick which in our case would be /dev/sdb so I won't have issues of the system "currently being used".

Number  Start   End     Size    File system     Name  Flags
        17.4kB  1049kB  1031kB  Free Space
 1      1049kB  538MB   537MB   fat32                 boot, esp
 2      538MB   1343MB  805MB   linux-swap(v1)
 3      1343MB  3221MB  1878MB  ext4
        3221MB  248GB   245GB   Free Space

First I need to use the free space and then resize the sda3 to a bigger size. I then want to create an sda4 and sda5 of equal size.

My current problem is that no tool gives me the option to automate via a script the allocation of free space where I can then create the partitions.

  • Have you looked into using sfdisk? Check out this stack exchange post
    – kemotep
    Oct 4, 2019 at 17:11
  • It does not look like it can even recognise the "Free space".
    – kirts
    Oct 4, 2019 at 17:27
  • Is your goal only to re-partition this 1 disk or to partition many disks? Re-partitioning a disk is quite destructive so I would caution against scripting this unless you need to do this for many many computers and not just this one. What issues are you specifically encountering and what have you tried so far? Please edit your post to include these details. Thank you.
    – kemotep
    Oct 4, 2019 at 17:43
  • This is for a commissioning tool (to flash images to many devices). Bootloader solution is unfortunately not an option so the only solution around it, is to boot an OS from a drive which will flash an image to the device's SSD. I'm fully aware this is a very risky operation but I am not worried if things go wrong as I'm in a test environment currently. I just need to automate this process and to be reliable once I do. In terms of tools, I have been through most of them so its a question of if someone has done that before and what they used to do so.
    – kirts
    Oct 7, 2019 at 9:02
  • So the hard drives you will be flashing currently do not have an operating system on them? You need to be able to deploy an image to many computers? That is straightforward. If you need to take existing operating systems on disks and restructure their partition scheme that is a lot harder. In my experience it is much faster to make back ups, install a new operating system with the partition scheme you want, and then restore from the backups then do restructure the partitioning.
    – kemotep
    Oct 7, 2019 at 12:12

1 Answer 1


The solution to this problem is to use the following command:

parted /dev/sda print free Fix

Apparently the script mode of parted will not allow you to "Fix" to the correct size. Most other tools will not allow you to automate that via a script either, in fact, this is the only solution that I found. It is worth noting that this is not a proper solution but it looks like the only way to achieve that.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .