zig_parted — Disk Partitioning in Zig
I recently published zig_parted, a pure-Zig library for reading and writing MBR and GPT partition tables. It supports creating, resizing, and managing partitions directly through disk I/O — no external commands or dependencies required.
Why?
Most partitioning tools (fdisk, parted, gdisk) are C programs called via CLI. That works, but when you're building system-level tooling in Zig — like an installer or disk utility — you either shell out or link against C libraries. This library eliminates that gap.
What it does
- Read and write MBR (Master Boot Record) partition tables
- Read and write GPT (GUID Partition Table) partition tables
- Create new partitions with a variety of filesystem types
- Resize existing partitions
- Direct disk I/O via
zp.Disk
Usage
Add it to your build.zig.zon and import:
A minimal example: create a 50 MB disk image, write an MBR with two partitions (Ext4 and Fat32), then inspect the result.
Partition types
The library includes partition type constants for:
| Type | Description |
|---|---|
.Ext4 | Linux ext4 |
.Fat32 | FAT32 |
.Ntfs | NTFS |
.LinuxSwap | Linux swap |
.LinuxLVM | Linux LVM |
.LinuxRAID | Linux RAID |
.Efi | EFI System Partition |
.HfsPlus | Apple HFS/HFS+ |
.Apfs | Apple APFS |
.FreeBSD | FreeBSD |
.OpenBSD | OpenBSD |
.NetBSD | NetBSD |
.Solaris | Solaris |
Caveats
Direct disk operations can modify partition tables. As the README says — always backup important data before use. This library is meant for controlled environments (installers, disk utilities) where you know what device you're targeting.