Stop Hand-Partitioning Disks: Practical systemd-repart on Linux
Stop Hand-Partitioning Disks: Practical systemd-repart on Linux You ship a minimal OS image. The target disk is 64 GB, 256 GB, or 2 TB. Root is still 8 GB. Swap does not exist. /home is not a partition yet. The usual fix is a one-off parted / gdisk script, a fragile installer hook, or “remember to resize after first boot.” That does not scale across VMs, bare metal, and image-based fleets. systemd-repart turns partition layout into declarative config: GPT definitions under repart.d/*.conf, incremental grow/add on every boot, optional format/encrypt/populate while building disk images (DDIs), and no shrink/move/delete of existing data by default. This post is a practical operator guide: lab image build, first-boot grow, swap/home/srv addition, filesystem growth, verification, and rollback boundaries. What systemd-repart actually does From systemd-repart(8): Reads repart.d/*.conf partition definitions. Operates on a block device or image file (or the disk backing / / /sysroot). Adds missing partitions and grows existing ones to satisfy size/weight constraints. Is incremental and idempotent: if the table already matches config, it is a no-op. Does not shrink, delete, or reorder partitions in normal mode. By default only changes the partition table, unless you set Format=, CopyFiles=, CopyBlocks=, Encrypt=, or Verity=. Matching is by GPT type UUID (friendly names like root, home, swap, esp), not by partition number. Filenames sort the definition order. First existing partition of type T binds to the first conf of type T, and so on. GPT only. MBR is out of scope. Mental model: three jobs, one tool Job Typical invocation Notes First-boot disk takeover systemd-repart.service in initrd Grow root; create swap/home/srv on free space Offline image build systemd-repart --image=... / --empty=create Build a DDI from scratch with Format/CopyFiles Safe preview default --dry-run=yes Always dry-run before --dry-run=no Filesystem growth is a sibling concern: GrowFileSystem= GPT flag + systemd-growfs / x-systemd.growfs, not “repart magically resizes ext4 by default.” Prerequisites GPT disk (or you are creating a new GPT image). Package providing the tool (Debian/Ubuntu: systemd-repart; many images already ship it with systemd). Root for real devices; unprivileged builds often use loop files + userns. Enough free space after the last partition you care about (repart appends; it does not defragment the table). # Presence check (paths vary by distro) command -v systemd-repart man 8 systemd-repart man 5 repart.d Lab 1: Build a minimal GPT image without touching real disks This is the safest way to learn. Create a sparse file, declare ESP + root + swap, format them, and inspect the result. mkdir -p /tmp/repart-lab/{defs,out,rootfs} # Tiny fake root tree to copy into the root partition mkdir -p /tmp/repart-lab/rootfs/{etc,usr/bin,var} echo "repart-lab" > /tmp/repart-lab/rootfs/etc/hostname printf '#!/bin/sh\necho ok\n' > /tmp/repart-lab/rootfs/usr/bin/hello chmod +x /tmp/repart-lab/rootfs/usr/bin/hello Partition definitions Drop files under a dedicated definitions directory (do not write these to a production host’s /etc/repart.d yet): cat >/tmp/repart-lab/defs/00-esp.conf
This is a summary aggregated from Dev.to. Read the complete article on the original site:
Read full article at Dev.to