Faster Partitioning With sgdisk

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 man page 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.
  2. Create a new GPT partition table.
  3. Create an efi partition of 512MB by specifying the end of the partition (relative) and the partition type, 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.
  5. Format the efi partition fat 32.
  6. Format the “/” partition ext4.
  1. wipefs --all /dev/sdx
  2. sgdisk /dev/sdx -o
  3. sgdisk /dev/sdx -n 1::+512MiB -t 1:ef00
  4. sgdisk /dev/sdx -n 2
  5. mkfs.vfat -F32 /dev/sdx1
  6. 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.

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

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

  1. Wipe any leftover filesystem metadata with wipefs.
  2. Create a new GPT partition table.
  3. Create an efi partition of 512MB by specifying the end of the partition (relative) and the partition type, 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.
  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.
  6. Format the efi partition fat 32.
  7. Format the “/boot” partition ext4.
  8. Encrypt the “/” partition.
  9. Decrypt the “/” device.
  10. Format the “/” device.
  1. wipefs --all /dev/sdx
  2. sgdisk /dev/sdx -o
  3. sgdisk /dev/sdx -n 1::+512MiB -t 1:ef00
  4. sgdisk /dev/sdx -n 2::+1GiB
  5. sgdisk /dev/sdx -n 3
  6. mkfs.vfat -F32 /dev/sdx1
  7. mkfs.ext4 /dev/sdx2
  8. cryptsetup -y -v luksFormat --type luks2 /dev/sdx3
  9. cryptsetup open /dev/sdx3 cryptroot
  10. mkfs.xfs /dev/mapper/cryptroot

What About Swap?

If 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.
  2. Create a new GPT partition table.
  3. Create an EFI partition.
  4. Create a “/boot” partition.
  5. Create a “/” partition with a relative negative end.
  6. Create a swap partion type 8200.
  7. format the partitions.
  1. wipefs --all /dev/sdx
  2. sgdisk /dev/sdx -o
  3. sgdisk /dev/sdx -n 1::+512MiB -t 1:ef00
  4. sgdisk /dev/sdx -n 2::+1GiB
  5. sgdisk /dev/sdx -n 3::-2GiB
  6. sgdisk /dev/sdx -n 4 -t 4:8200
  7. mkfs.vfat -F32 /dev/sdx1
  8. mkfs.ext4 /dev/sdx2
  9. mkfs.xfs /dev/sdx3
  10. mkswap /dev/sdx4

Conclusion

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