155 lines
4.5 KiB
Markdown
155 lines
4.5 KiB
Markdown
---
|
||
title: "Faster Partitioning With sgdisk"
|
||
date: 2019-02-11T04:23:52-08:00
|
||
draft: false
|
||
tags: ["command-line","gdisk","partition","sgdisk"]
|
||
authors: ["trent"]
|
||
post: 8
|
||
---
|
||
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](https://manpages.debian.org/stretch/gdisk/sgdisk.8.en.html){target=_blank}**
|
||
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.
|
||
```console
|
||
# For New Partition:
|
||
-n, --new=partnum:start:end
|
||
```
|
||
```console
|
||
# 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.
|
||
```console
|
||
wipefs --all /dev/sdx
|
||
```
|
||
1. Create a new GPT partition table.
|
||
```console
|
||
sgdisk /dev/sdx -o
|
||
```
|
||
1. Create an efi partition of 512MB by specifying the end of the partition (relative) and the partition type, `ef00`.
|
||
```console
|
||
sgdisk /dev/sdx -n 1::+512MiB -t 1:ef00
|
||
```
|
||
1. Create an `/` partition using the remainder of the disk, by not specifying the end or the beginning or partition type, which defaults to 8300.
|
||
```console
|
||
sgdisk /dev/sdx -n 2
|
||
```
|
||
1. Format the efi partition fat 32.
|
||
```console
|
||
mkfs.vfat -F32 /dev/sdx1
|
||
```
|
||
1. Format the `/` partition ext4.
|
||
```console
|
||
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.
|
||
```console
|
||
# 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`.
|
||
```console
|
||
wipefs --all /dev/sdx
|
||
```
|
||
1. Create a new GPT partition table.
|
||
```console
|
||
sgdisk /dev/sdx -o
|
||
```
|
||
1. Create an efi partition of 512MB by specifying the end of the partition (relative) and the partition type, `ef00`.
|
||
```console
|
||
sgdisk /dev/sdx -n 1::+512MiB -t 1:ef00
|
||
```
|
||
1. Create a `/boot` partition of 1GB, by specifying the end of the partition (relative), but not specifying the partition type which defaults to `8300`.
|
||
```console
|
||
sgdisk /dev/sdx -n 2::+1GiB
|
||
```
|
||
1. Create an `/` partition using the remainder of the disk, by not specifying the end or the beginning or partition type, which defaults to `8300`.
|
||
```console
|
||
sgdisk /dev/sdx -n 3
|
||
```
|
||
1. Format the efi partition fat 32.
|
||
```console
|
||
mkfs.vfat -F32 /dev/sdx1
|
||
```
|
||
1. Format the `/boot` partition ext4.
|
||
```console
|
||
mkfs.ext4 /dev/sdx2
|
||
```
|
||
1. Encrypt the `/` partition.
|
||
```console
|
||
cryptsetup -y -v luksFormat --type luks2 /dev/sdx3
|
||
```
|
||
1. Decrypt the `/` device.
|
||
```console
|
||
cryptsetup open /dev/sdx3 cryptroot
|
||
```
|
||
1. Format the `/` device.
|
||
```console
|
||
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.
|
||
```console
|
||
wipefs --all /dev/sdx
|
||
```
|
||
1. Create a new GPT partition table.
|
||
```console
|
||
sgdisk /dev/sdx -o
|
||
```
|
||
1. Create an EFI partition.
|
||
```console
|
||
sgdisk /dev/sdx -n 1::+512MiB -t 1:ef00
|
||
```
|
||
1. Create a `/boot` partition.
|
||
```console
|
||
sgdisk /dev/sdx -n 2::+1GiB
|
||
```
|
||
1. Create a `/` partition with a relative negative end.
|
||
```console
|
||
sgdisk /dev/sdx -n 3::-2GiB
|
||
```
|
||
1. Create a swap partion type `8200`.
|
||
```console
|
||
sgdisk /dev/sdx -n 4 -t 4:8200
|
||
```
|
||
1. format the partitions.
|
||
```console
|
||
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
|