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?

  • it's not json but there is --machine, displays machine parseable output. or you can also go through /sys/block/sdx/sdx*/{start,size} – frostschutz Sep 9 '19 at 21:24
  • Thank you, @frostschutz! Is it available and has the same format on all major Unix based Operating Systems? – Vic K Sep 9 '19 at 23:46

Your Answer

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

Browse other questions tagged or ask your own question.