0

I can use sgdisk -b /dev/sdx to save the GPT partition map of a disk to a file. I want to generate such a file for a GPT with one partition of a certain size. The only way I can see to do this is to actually format a disk in a tool like GParted then use gdisk to save the GPT.

Is there a way, preferably in GParted, to set up a GPT table with my desired partitions then save it to a file instead of writing it to disk?

Other non-desireable options:

  • Manually edit a dump from sgdisk. Too hard to get things right.
  • Create a VirtualBox disk of the desired size, format it, then export with sgdisk. Would take forever for the 440GiB disk I want. Edit: Actually, this is fast if I use a dynamically allocated virtual disk, but it's a clunky solution.
sudo
  • 342

1 Answers1

0

I understand your question is related to the partition table and the partitions and not the data. If this is correct then your answer is already in the man pages for parted and here.

In essence create a script of the parted commands necessary to create your gpt scheme and then

parted --script /dev/sdx [script commands]

This can be put into a shell script and executed as you need, perhaps passing the device as an argument to the script.

Edit after comment: According to the man sgdisk page, to get the sgdisk output for a 'phantom' disk, use the '-P' option which performs the task(s) in memory but does not commit changes to disk.

I just tried this with a usb and it appears to work

sudo sgdisk -p /dev/sdb

Disk /dev/sdb: 31653888 sectors, 15.1 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 2016C547-548B-482E-8810-A5E7A1466CED
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 31653854
Partitions will be aligned on 2048-sector boundaries
Total free space is 4029 sectors (2.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        31651839   15.1 GiB    0700  IDEA    #old name


sudo sgdisk -P -c 1:random -p /dev/sdb                           #change the name

Setting name!
partNum is 0
REALLY setting name!
Disk /dev/sdb: 31653888 sectors, 15.1 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 2016C547-548B-482E-8810-A5E7A1466CED
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 31653854
Partitions will be aligned on 2048-sector boundaries
Total free space is 4029 sectors (2.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        31651839   15.1 GiB    0700  random   #name changed

re-executing

sudo sgdisk -p /dev/sdb


Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        31651839   15.1 GiB    0700  IDEA    #change not written

The change is also reflected in the output from

sudo sgdisk -P -c 1:random --backup=phantomDisk /dev/sdb
bu5hman
  • 4,756
  • Doesn't this still apply the changes to the /dev/sdx disk, erasing all data? I was looking to output the resulting GPT table rather than writing it to disk. – sudo Nov 27 '17 at 05:21