Score:0

sfdisk: create a partition from the right

it flag

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 always

The 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 always

with 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?

waltinator avatar
it flag
What environment are you running this fragment in? Would text processing of `sfdisk --list` help? Consider using `expect` to automate your conversation with `sfdisk`. Read `man expect`
it flag
In what sense which environment?
Score:1
jp flag

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
...
it flag
Thanks to you my problem has been solved.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.