I do not see anything in the sfdisk
documentation that suggests it supports a negative relative value. Would it work to calculate the partition start position?
I'm going to demonstrate using a sparse loopback file simulating a 2GB disk
root@ubuntu:~# fallocate -l 2G filesystem.img
root@ubuntu:~# losetup -f filesystem.img
root@ubuntu:~# losetup -a | grep filesystem.img
/dev/loop6: [2049]:20 (/root/filesystem.img)
The start position of your 650MB partition can be found by subtracting 650MB from the end of the disk. $(blockdev --getsize64 /dev/loop6) / 1024
provides the size of the disk in KiB. 650 * 1024
is 650MiB in KiB. Put it together to find how many KiB to use for the first partition.
root@ubuntu:~# echo $(( $(blockdev --getsize64 /dev/loop6) / 1024 - (650 * 1024) ))
1431552
This value can be used directly when creating the partitions with sfdisk
root@ubuntu:~# sfdisk /dev/loop6 <<EOF
> label: gpt
> 1431552KiB,,,-
> ,,,-
> EOF
...
New situation:
Disklabel type: gpt
Disk identifier: 77816CA7-8A39-974B-A78D-CFCB2A5D66EC
Device Start End Sectors Size Type
/dev/loop6p1 2863104 4194270 1331167 650M Linux filesystem
/dev/loop6p2 2048 2863103 2861056 1.4G Linux filesystem
...
You could also calculate the value on the fly
root@ubuntu:~# wipefs -a /dev/loop6
...
root@ubuntu:~# sfdisk /dev/loop6 <<EOF
> label: gpt
> $(( $(blockdev --getsize64 /dev/loop6) / 1024 - (650 * 1024) ))KiB,,,-
> ,,,-
> EOF
...
New situation:
Disklabel type: gpt
Disk identifier: 661C7E48-3342-3842-81BE-1AF4CB51BC6E
Device Start End Sectors Size Type
/dev/loop6p1 2863104 4194270 1331167 650M Linux filesystem
/dev/loop6p2 2048 2863103 2861056 1.4G Linux filesystem
...
The previous examples created your 650MB partition first. That can be a bit confusing since the partitions are not in the typical order. You can create the "rest of the disk" partition first to avoid confusion.
root@ubuntu:~# wipefs -a /dev/loop6
...
root@ubuntu:~# sfdisk /dev/loop6 <<EOF
> label: gpt
> ,$(( $(blockdev --getsize64 /dev/loop6) / 1024 - (650 * 1024) ))KiB,,-
> ,,,-
> EOF
...
New situation:
Disklabel type: gpt
Disk identifier: 3EF08C46-AF4F-1F48-B8AF-A65D67C438B7
Device Start End Sectors Size Type
/dev/loop6p1 2048 2865151 2863104 1.4G Linux filesystem
/dev/loop6p2 2865152 4194270 1329119 649M Linux filesystem
...