Managing Partitions with sgdisk

Roderick W. Smith‘s sgdisk command can be used to manage the partitioning of your hard disk drive from the command line. The basics that you need to get started with it are demonstrated below.

The following six parameters are all that you need to know to make use of sgdisk’s most basic features:

  1. -p
    Print the partition table:
    # sgdisk -p /dev/sda
  2. -d x
    Delete partition x:
    # sgdisk -d 1 /dev/sda
  3. -n x:y:z
    Create a new partition numbered x, starting at y and ending at z:
    # sgdisk -n 1:1MiB:2MiB /dev/sda
  4. -c x:y
    Change the name of partition x to y:
    # sgdisk -c 1:grub /dev/sda
  5. -t x:y
    Change the type of partition x to y:
    # sgdisk -t 1:ef02 /dev/sda
  6. –list-types
    List the partition type codes:
    # sgdisk --list-types

The SGDisk Command

As you can see in the above examples, most of the commands require that the device file name of the hard disk drive to operate on be specified as the last parameter.

The parameters shown above can be combined so that you can completely define a partition with a single run of the sgdisk command:

# sgdisk -n 1:1MiB:2MiB -t 1:ef02 -c 1:grub /dev/sda

Relative values can be specified for some fields by prefixing the value with a + or symbol. If you use a relative value, sgdisk will do the math for you. For example, the above example could be written as:

# sgdisk -n 1:1MiB:+1MiB -t 1:ef02 -c 1:grub /dev/sda

The value 0 has a special-case meaning for several of the fields:

  • In the partition number field, 0 indicates that the next available number should be used (numbering starts at 1).
  • In the starting address field, 0 indicates that the start of the largest available block of free space should be used. Some space at the start of the hard drive is always reserved for the partition table itself.
  • In the ending address field, 0 indicates that the end of the largest available block of free space should be used.

By using 0 and relative values in the appropriate fields, you can create a series of partitions without having to pre-calculate any absolute values. For example, the following sequence of sgdisk commands would create all the basic partitions that are needed for a typical Linux installation if in run sequence against a blank hard drive:

# sgdisk -n 0:0:+1MiB -t 0:ef02 -c 0:grub /dev/sda
# sgdisk -n 0:0:+1GiB -t 0:ea00 -c 0:boot /dev/sda
# sgdisk -n 0:0:+4GiB -t 0:8200 -c 0:swap /dev/sda
# sgdisk -n 0:0:0 -t 0:8300 -c 0:root /dev/sda

The above example shows how to partition a hard disk for a BIOS-based computer. The grub partition is not needed on a UEFI-based computer. Because sgdisk is calculating all the absolute values for you in the above example, you can just skip running the first command on a UEFI-based computer and the remaining commands can be run without modification. Likewise, you could skip creating the swap partition and the remaining commands would not need to be modified.

There is also a short-cut for deleting all the partitions from a hard disk with a single command:

# sgdisk --zap-all /dev/sda

For the most up-to-date and detailed information, check the man page:

$ man sgdisk

Fedora Project community


  1. Leslie Satenstein

    Clear explanation. Thank you.

  2. The man page gives a good acount of using sgdisk to convert MBR to GPT. It involves a risk of making the system unbootable – but nothing a live USB stick can’t fix. A backup is probably a good idea, though.

    • Thanks for pointing that out Stuart. I probably should have mentioned the dangers of converting an existing MBR partition table in the article. Thankfully, it looks like the sgdisk command includes a bit of a safty:

      -g, –mbrtogpt
      Convert an MBR or BSD disklabel disk to a GPT disk. As a safety measure, use of this option is required on MBR or BSD disklabel disks if you intend to save your changes, in order to prevent accidentally damaging such disks.

      The following would probably be a good idea for anyone thinking about converting an MBR disk to GPT to run:

      $ sudo sfdisk -d /dev/sda > partition-table-backup.dat

      That could then be restored from a rescue disk with:

      # cat partition-table-backup.dat | sfdisk /dev/sda

      • When I try converting to GPT on a VM, sgdisk complains that the last partition overlaps secondary partition table by 33 sectors. The same complaint is given on any MBR disk I’ve looked at with sgdisk. I’m guessing that a second copy of the GPT is kept at the end of the disk, and the last partition (usually a PV) needs to be made 33 sectors smaller. Is this correct?

        • You are exactly correct. The backup copy is referred to as the “second-header” in the man page. It is just a backup and is only used in case the original gets corrupted somehow. So you could safely ignore the error if you want. The older MBR never kept a backup copy of its data.

          I’m not sure that I would recommend people convert their partition tables though. Its not something that I ever do. That said, I think you should be able to get away with it on UEFI systems because UEFI should fall back to using the /efi/boot/bootx64.efi (more accurately /efi/boot/boot{machine type short-name}.efi) when the partition IDs change.

          If you want to tell Anaconda to use GPT by default when you first install your OS, you can pass the inst.gpt option on the kernel command line when you boot your installation disk: Anaconda Boot Options

          See also: An Introduction to Disk Partitions

          P.S. I would always recommend rounding your partition alignments to the nearest 1MiB to reduce the changes of hitting the double-write problem: Disk Partition Alignment Is Still Important

  3. Cool way of explaining the ideas. Loved it.

  4. Anil

    Why you used sfdisk instead of sgdisk to take the backup?

    You can use sgdisk itself to take backup.

    sgdisk –backup=/root/sda-part.bkp /dev/sda

    To restore

    sgdisk –load-backup=/root/sda-part.bkp /dev/sda

    • As a general rule, sfdisk is used to manage MBR partition tables and sgdisk is used to manage GPT partition tables. Admittedly though, sgdisks backup option is probably an exception to that rule and it probably backs up and restores MBR data (I’m not 100% certain of that though).

      Even better, of course, would be to keep a backup copy of your entire hard drive. In fact, I’m working on an article to demonstrate how to do that right now.

  5. commenter

    what are the pros when compared to fdisk/gdisk?

    • The only real advantage of sgdisk over gdisk is that it can be scripted. The reason that I wrote this post is because I am using sgdisk in an upcoming article on how to convert your system drive to use software RAID and the editors requested that I write individual posts for several of the commands I used in that article — dracut, mdadm, and sgdisk (Fedora Magazine Meeting 2019-03-27 time index 20:27:09 if you are curious). The upcoming article will link back to this one for further details.

Comments are Closed

The opinions expressed on this website are those of each author, not of the author's employer or of Red Hat. Fedora Magazine aspires to publish all content under a Creative Commons license but may not be able to do so in all cases. You are responsible for ensuring that you have the necessary permission to reuse any work on this site. The Fedora logo is a trademark of Red Hat, Inc. Terms and Conditions