brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: parted -s partitions a new disk in one aligned line

Disk
~/tips/tip-disk-parted-mkpart-new-disk

sudo parted -s -a optimal /dev/sdX mklabel gpt mkpart data ext4 1MiB 100%

Creates a GPT partition table and one aligned partition on a new disk in a single command.

A fresh disk needs a partition table and a partition before you can put a filesystem on it. parted has an interactive mode for that, but I like the script mode better: it’s one line that you can read back calmly before you press Enter.

lsblk -o NAME,SIZE,MODEL
sudo parted -s -a optimal /dev/sdX mklabel gpt mkpart data ext4 1MiB 100%
sudo parted /dev/sdX align-check optimal 1

-s is script mode, so parted never prompts. -a optimal aligns new partitions to the physical block size of the disk, which the man page says guarantees optimal performance. mklabel gpt writes an empty GPT partition table, and mkpart data ext4 1MiB 100% makes one partition named data from 1 MiB to the end of the disk. GPT needs a name, the ext4 is only a hint for the partition type, and % means a percentage of the device size. The last command checks that partition 1 is aligned, and it prints whether it is.

Starting at 1MiB is on purpose. The man page explains that IEC units like MiB are taken as exact locations, while units like MB get a range of slack. After this, the partition still needs a filesystem, for example sudo mkfs.ext4 /dev/sdX1.

The big caveat: mklabel replaces the partition table of the disk you name, and in script mode nobody asks if you’re sure. On the wrong disk that wipes access to everything on it. So the lsblk line comes first for a reason: compare the size and model with the disk you just plugged in, and only then fill in sdX. If the disk might have something on it, look first with parted /dev/sdX print.

Further reading: parted(8) manual page

More Disk tips sudo e2freefrag /dev/sdb1 sudo smartctl -a --json /dev/sda | jq '.smart_status.passed' lsscsi -s udisksctl power-off -b /dev/sdX du -sh — .[!.]* * 2>/dev/null | sort -h All Disk tips (44) All topics

Link