I want to create partition 2 with a specified size, while partition 1 occupies the rest of the disk:
echo -en 'g\nw' | fdisk "$2" #create gpt table
echo -en ',1331200\n,,U' | sudo sfdisk "/dev/sda" -W alwaysThe last command creates the left partition of 650MB and the right one occupying the rest of the disk, but as I have already specified, this is not what I need. To create the right partition of 650MB and the left one occupying the rest of the disk, I thought I'd do something like this:
echo -en ',-1331200\n,,U' | sudo sfdisk "/dev/sda" -W alwayswith the thought that the negative values started from the right, but instead the - sign is completely ignored and the command has the same effect as the one without the - sign.
How do I create a partition starting from the right using sfdisk?
1 Answer
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) ))
1431552This 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
... 1