Skip to content

Faster Partitioning with Sgdisk

date: 2019-02-11T04:23:52-08:00

Disclaimer

If any of this is wrong, let me know so I can fix it. No actual hard drives were harmed in the production of this blog post.

The examples are easier to read if you turn your smart phone sideways.

Command Line Is Faster

Sure you can partition your discs using a GUI disk management application or an interactive, menu-driven terminal interface. But the command line is faster.

gdisk vs sgdisk

sgdisk is the scriptable version of gdisk (gptfdisk).

what the manpage says

If you’re familiar with gdisk, you probably know how to interactively set the partition size and type. If you look at the man page for sgdisk you see that the relevant flags are -n and -t. The beginning and ending numbers are absolute, unless you prepend them with a + or - sign, in which case they become relative.

# For New Partition:
-n, --new=partnum:start:end
# Change partition type:
-t, --typecode=partnum:{hexcode|GUID}

Example with Separate EFI and / Partitions

BTW, gdisk is a partitioning tool intended to be used with a gpt partition table, so the assumption is that you would want an efi partition,

(although the efi partition does not have to be on the disk you are partitioning or even on the same disk where your other system partitions are).

  1. Wipe any leftover filesystem metadata with wipefs.
    wipefs --all /dev/sdx
    
  2. Create a new GPT partition table.
    sgdisk /dev/sdx -o
    
  3. Create an efi partition of 512MB by specifying the end of the partition (relative) and the partition type, ef00.
    sgdisk /dev/sdx -n 1::+512MiB -t 1:ef00
    
  4. Create an / partition using the remainder of the disk, by not specifying the end or the beginning or partition type, which defaults to 8300.
    sgdisk /dev/sdx -n 2
    
  5. Format the efi partition fat 32.
    mkfs.vfat -F32 /dev/sdx1
    
  6. Format the / partition ext4.
    mkfs.ext4 /dev/sdx2
    

Practice With A Sparse Image

If you don’t want to partition a real hard drive, you can practice using an sparse image file, instead.

# create a sparse image file
truncate -S 100G practiceImage.img
# partition the image file with sgdisk
sgdisk practiceImage.img -o
# etc

Example with Separate /boot, EFI, and luks-encrypted / Partitions

  1. Wipe any leftover filesystem metadata with wipefs.
    wipefs --all /dev/sdx
    
  2. Create a new GPT partition table.
    sgdisk /dev/sdx -o
    
  3. Create an efi partition of 512MB by specifying the end of the partition (relative) and the partition type, ef00.
    sgdisk /dev/sdx -n 1::+512MiB -t 1:ef00
    
  4. Create a /boot partition of 1GB, by specifying the end of the partition (relative), but not specifying the partition type which defaults to 8300.
    sgdisk /dev/sdx -n 2::+1GiB
    
  5. Create an / partition using the remainder of the disk, by not specifying the end or the beginning or partition type, which defaults to 8300.
    sgdisk /dev/sdx -n 3
    
  6. Format the efi partition fat 32.
    mkfs.vfat -F32 /dev/sdx1
    
  7. Format the /boot partition ext4.
    mkfs.ext4 /dev/sdx2
    
  8. Encrypt the / partition.
    cryptsetup -y -v luksFormat --type luks2 /dev/sdx3
    
  9. Decrypt the / device.
    cryptsetup open /dev/sdx3 cryptroot
    
  10. Format the / device.
    mkfs.xfs /dev/mapper/cryptroot
    

What About Swap?

I prefer to use a swap file inside the luks-encrypted / partition. But you can make a separate swap partition if you like.

Example with 2GB swap partition

  1. Wipe the disc.
    wipefs --all /dev/sdx
    
  2. Create a new GPT partition table.
    sgdisk /dev/sdx -o
    
  3. Create an EFI partition.
    sgdisk /dev/sdx -n 1::+512MiB -t 1:ef00
    
  4. Create a /boot partition.
    sgdisk /dev/sdx -n 2::+1GiB
    
  5. Create a / partition with a relative negative end.
    sgdisk /dev/sdx -n 3::-2GiB
    
  6. Create a swap partion type 8200.
    sgdisk /dev/sdx -n 4 -t 4:8200
    
  7. format the partitions.
    mkfs.vfat -F32 /dev/sdx1
    mkfs.ext4 /dev/sdx2
    mkfs.xfs /dev/sdx3
    mkswap /dev/sdx4
    

Conclusion

  • Good luck to you.
  • Backup your data first.
  • Kind Regards, Trent