12

How does one create a partition of exact size not to specific distance?

I have tried:

parted -a optimal /dev/sdd mkpart primary 0% 0%+1GB
# and...
parted -a optimal /dev/sdd mkpart primary 0% +1GB

To no avail.

How do I make it accept size instead of end distance?

tomsseisums
  • 417
  • 1
  • 5
  • 13

1 Answers1

8

I think parted only accepts absolutes, not x+y.

You could do it like this: (+ interpreted by shell)

# start=1
# size=512
# parted /dev/loop0 unit mib mkpart primary $start $(($start+$size))
# parted /dev/loop0 unit mib print free
Model: Loopback device (loopback)
Disk /dev/loop0: 1000MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start    End      Size     File system  Name     Flags
        0.02MiB  1.00MiB  0.98MiB  Free Space
 1      1.00MiB  513MiB   512MiB                primary
        513MiB   1000MiB  487MiB   Free Space

...and if you want to append more, then

# start=$(($start+$size+1))
# size=128
# parted /dev/loop0 unit mib mkpart primary $start $(($start+$size))
# parted /dev/loop0 unit mib print free                           
Model: Loopback device (loopback)
Disk /dev/loop0: 1000MiB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start    End      Size     File system  Name     Flags
        0.02MiB  1.00MiB  0.98MiB  Free Space
 1      1.00MiB  513MiB   512MiB                primary
        513MiB   514MiB   1.00MiB  Free Space
 2      514MiB   642MiB   128MiB                primary
        642MiB   1000MiB  358MiB   Free Space

The +1 is entirely optional, I just like leaving 1MiB free "bumpers" between partitions, for some reason.

If you want to deal with percentages, you can obtain the device size from the shell also, using blockdev --getsize64 /dev/device, so you can move all the math out of parted into a shell script. I do this to guarantee MiB-aligned partitions, as I'm never too sure about what parted means exactly by -a optimal.

frostschutz
  • 47,228
  • 5
  • 112
  • 159