I'm writing a program (in java) that slices a clean device onto a set of partitions, using
parted mkpart to create the partitions. In order to create a next partition, I want to know a partition number that the program have just created. Current implementation is reading all existing partitions (using sfdisk) and calculates the last one - this is very inefficient and becomes very slow after creating a few dozen partitions.
I found this answer that suggests using
parted print free, but that doesn't fit quite well to my case: 1) there can be holes of free spaces, so I may actually get not the last created partition; 2) because the partitioning is being done programmatically (in java) it's tricky to parse the
parted print free output (unless there is a json option, which I didn't find).
Is there a way to know right away what partition number has just been created?