1

I'm using sgdisk in a bash script similar to this:

sgdisk --clear /dev/vda --set-alignment=1 --new 1:34:2047 --typecode 1:EF02 -c 1:"grub"  -g /dev/vda
sgdisk --new 2:2048:16779263 --typecode 2:8300 -g /dev/vda
sgdisk --new 3:16779264:20971486 --typecode 3:8200 -g /dev/vda

That works only when the devices are well known in advance and the sectors are hard-coded.

I want to drop the hardcode the sector values. Instead, I want the script to work when the disk size is not known until the script runs. After making partition 1, I will set aside a known fixed amount to partition 3 for swap, and give the rest to partition 2.

The easy way would be to make the swap partition #2. I know how to do that. However, I want to see if I can instead do this while keeping swap on partition 3. It means sgdisk will have to calculate a size or end sector value for partition 2 taking into account the size that will be allocated for partition 3 in the next step.

Reading through the sgdisk man page hasn't given me the clues about how to do this (or even if it can be done).

BugBuddy
  • 618
  • 1
  • 10
  • 19

1 Answers1

1

The following will work:

sgdisk --clear /dev/vda --set-alignment=1 --new 1:34:2047 --typecode 1:EF02 -c 1:"grub"  -g /dev/vda
sgdisk --new 2:0:-2G --typecode 2:8300 -g /dev/vda
sgdisk --new 3:0:0 --typecode 3:8200 -g /dev/vda

It's much simpler than I thought. sgdisk does all the calculations. The key is the minus sign, which is explained in the man page (which I had missed earlier).

You can specify locations relative to the start or end of the specified default range by preceding the number by a '+' or '-' symbol, as in +2G to specify a point 2GiB after the default start sector, or -200M to specify a point 200MiB before the last available sector. A start or end value of 0 specifies the default value,

BugBuddy
  • 618
  • 1
  • 10
  • 19