From 4f629c88abf921ab0d5cef93ab06a004f49b9e9d Mon Sep 17 00:00:00 2001 From: eriknordmark Date: Sat, 18 Jul 2026 08:44:18 -0700 Subject: [PATCH 1/2] Declarative Apply: grow/create/shrink, remove Run Apply is the single entry point for reconciling a GPT disk to a desired set of partitions in an idempotent, restart-safe pass. It grows a partition to at least its size, creates it (with an empty filesystem) when absent, and optionally shrinks one partition to free space -- to an explicit size, or (when the size is zero) only as much as the grows and creates need. An existing partition is matched by GUID or by name/label/uuid; with no disk given, the disk is discovered from the identifiers and kernel device names are resolved via sysfs. A created partition's GPT entry is published only by the final table write, so an interrupted run converges on re-run. Run is removed, and the cmd/resizer CLI collapses from resize/apply subcommands into a single bare `resizer [disk]`. Apply's Go signature is unchanged, so existing callers (e.g. EVE's storage-resizer) are unaffected. Implements #22. Signed-off-by: eriknordmark Co-Authored-By: Claude Opus 4.8 (1M context) --- README.md | 112 +++++-- apply.go | 475 +++++++++++++++++++++++++++ apply_chaos_test.go | 125 +++++++ apply_combined_test.go | 101 ++++++ apply_discovery_test.go | 167 ++++++++++ apply_exec_test.go | 120 +++++++ calculate.go | 29 +- cmd/resizer/grow_only_test.go | 8 +- cmd/resizer/main.go | 207 +++++++----- cmd/resizer/main_test.go | 107 ++++-- common_test.go | 23 +- convert.go | 83 ----- convert_test.go | 77 ----- discover_test.go | 2 +- run_helpers.go => fsops.go | 120 ------- run_helpers_test.go => fsops_test.go | 104 ------ layout_resume_test.go | 59 ++-- planspecs_test.go | 241 ++++++++++++++ resize.go | 94 ++++++ resume_stress_test.go | 32 +- resume_test.go | 96 +++--- run.go | 111 ------- run_test.go | 50 ++- types.go | 96 ++++-- 24 files changed, 1817 insertions(+), 822 deletions(-) create mode 100644 apply.go create mode 100644 apply_chaos_test.go create mode 100644 apply_combined_test.go create mode 100644 apply_discovery_test.go create mode 100644 apply_exec_test.go delete mode 100644 convert.go delete mode 100644 convert_test.go rename run_helpers.go => fsops.go (68%) rename run_helpers_test.go => fsops_test.go (62%) create mode 100644 planspecs_test.go delete mode 100644 run.go diff --git a/README.md b/README.md index ace71e5..5d4a13f 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,13 @@ # Partition Resizer -This is a tool to resize GPT disk partitions and their filesystems. It can grow multiple partitions, -primarily by copying the partitions to new, larger partitions in available free space on the disk. +This is a tool to reconcile a GPT disk to a desired set of partitions. It grows multiple +partitions (primarily by copying them to new, larger partitions in available free space), +creates partitions that are absent, and optionally shrinks one partition to free space — +all in a single, idempotent, restart-safe pass. + +You declare the partitions you want; each is grown to at least its size, or created if +absent, and one named partition may be shrunk to make room. A partition already at least +its size is left untouched — nothing is ever shrunk except the designated shrink partition. If insufficient free space is available, and you give it an optional shrink partition that is ext4, it will shrink the ext4 filesystem and its partition to find space, if it can. @@ -42,40 +48,74 @@ partition. ## Examples -Shrink partition named sda3 (ext4) to make space, grow partition named sda1 to 20G, grow partition labeled "Data" to 100G on /dev/sda: +Grow the partition labeled `sda1` to 20G and the one labeled "Data" to 100G, +shrinking `sda3` (ext4) to make space if needed, on /dev/sda: + +```sh +resizer \ + --partition match=name:sda1,minsize=20G \ + --partition match=label:Data,minsize=100G \ + --shrink name:sda3 \ + /dev/sda +``` + +`--shrink name:sda3` with no size is *shrink-to-fit*: sda3 is shrunk only if the +grows do not otherwise fit, and only by as much as they need. Give an explicit +size (`--shrink name:sda3:78G`) to always shrink to that size. + +Grow the partition labeled sda2 to 50G on a disk image file: ```sh -resizer --shrink-partition name:sda3 --grow-partition name:sda1:20G --grow-partition label:Data:100G /dev/sda +resizer --partition match=label:sda2,minsize=50G disk.img ``` -Grow partition named sda2 to 50G on disk image file disk.img: +### Creating a partition + +A `--partition` identified by `guid=` grows the partition with that GUID if it +exists, or **creates** it if absent, at `minsize` with an empty filesystem. +This example grows IMGA, creates a new 2G FAT32 "EFI System" partition (a second +ESP) at partition number 7, and shrinks "Data" to 100G to make room — all on +/dev/sda: ```sh -resizer --grow-partition name:sda2:50G disk.img +resizer \ + --partition "guid=AD6871EE-31F9-4CF3-9E09-6F7A25C30051,minsize=200M,label=IMGA,type=0FC63DAF-8483-4772-8E79-3D69D8477DE4,index=2" \ + --partition "guid=AD6871EE-31F9-4CF3-9E09-6F7A25C30056,minsize=2G,label=EFI System,type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B,index=7,fs=fat32" \ + --shrink label:Data:100G \ + /dev/sda ``` +The second `--partition` names a GUID not yet on the disk, so it is created: +`index=7` places it at partition slot 7, `fs=fat32` lays down an empty FAT32 +filesystem. Partitions not listed are left untouched; `--shrink` is optional and +is the only operation that ever reduces a partition. + ## Options ``` -resizer [flags] +resizer [flags] [disk] ``` -`` is the disk image file or block device to operate on. +`[disk]` is the disk image file or block device to operate on. It may be omitted, +in which case the disk is discovered from the `match=`/`--shrink` identifiers +(they must all resolve to the same disk). | Flag | Description | | --- | --- | -| `--grow-partition identifier:partition:size` | Partition to grow and its target size, in `identifier:partition:size` form (e.g. `name:sda1:20G`, `label:Data:100M`). Repeatable; at least one is required. | -| `--shrink-partition identifier:partition` | Optional ext4 partition to shrink to make space, used only if there is not enough free space for the grows. | +| `--partition ` | A desired partition, as comma-separated `key=value` pairs (repeatable). `minsize=` is required. Identify an existing partition to grow with either `guid=` or `match=` (`match=name:sda1`, `match=label:Data`, `match=uuid:`); a `guid=` not present on the disk is **created**. Optional: `label=`, `type=` (GPT type GUID, asserted on a match), `index=` (requested partition number), `fs=fat32\|ext4\|none` (filesystem for a create; default `none`). | +| `--shrink identifier:value[:size]` | Optional single partition to shrink to make room (e.g. `label:Data:100G`, `uuid::78G`, or `label:Data` for shrink-to-fit). The only operation that reduces a partition. | | `--fix-errors` | Repair filesystem errors found while checking the source filesystems (ext4 via `e2fsck -y`, FAT32 via `fsck.fat -a`) instead of aborting on an inconsistent source. Default is a read-only check that aborts on any inconsistency. | -| `--dry-run` | Plan the resize and log it, but make no changes. | -| `--preserve-numbers` | Renumber a relocated (grown) partition back to its original partition number, so consumers that reference it by number (e.g. `/dev/sda2`) still find it. | +| `--dry-run` | Plan the reconcile and log it, but make no changes. | -Partitions are identified by `name` (e.g. `name:sda1`) or `label` (e.g. -`label:EFI System`). Sizes accept `B`, `K`, `M`, `G`, or `T` suffixes. +Partitions are identified by `name` (e.g. `name:sda1`, resolved via sysfs on a +block device), `label` (e.g. `label:EFI System`), or `uuid`. Sizes accept `B`, +`K`, `M`, `G`, or `T` suffixes. Grown partitions keep their original partition +number, so consumers that reference a partition by number (e.g. `/dev/sda2`) +still find it after a resize. ## Library use -resizer is also importable as a Go package. The entry point is `Run`, which +resizer is also importable as a Go package. The entry point is `Apply`, which performs the same operation as the CLI: ```go @@ -86,40 +126,44 @@ import ( ) func main() { - // optional ext4 partition to shrink for space; pass nil to disable shrinking - shrink := resizer.NewPartitionIdentifier(resizer.IdentifierByName, "sda3") - - // partitions to grow, with their target sizes (in bytes) - grows := []resizer.PartitionChange{ - resizer.NewPartitionChange(resizer.IdentifierByName, "sda1", 20*resizer.GB), - resizer.NewPartitionChange(resizer.IdentifierByLabel, "Data", 100*resizer.GB), + // desired partitions: each is grown to at least MinSize, or created if the + // GUID is absent. An existing partition to grow is located by Match + // (name/label/uuid) or, when Match is nil, by GUID. + desired := []resizer.PartitionSpec{ + {Match: resizer.NewPartitionIdentifier(resizer.IdentifierByName, "sda1"), MinSize: 20 * resizer.GB}, + {Match: resizer.NewPartitionIdentifier(resizer.IdentifierByLabel, "Data"), MinSize: 100 * resizer.GB}, } - // Run(disk, shrink, grows, fixErrors, dryRun, preserveNumbers) - // disk -- image file path or block device - // fixErrors -- repair filesystem errors (e2fsck -y / fsck.fat -a) instead of read-only checks - // dryRun -- plan only, make no changes - // preserveNumbers -- renumber a relocated partition back to its original number - if err := resizer.Run("/dev/sda", &shrink, grows, false, false, true); err != nil { - log.Fatalf("resize failed: %v", err) + // optional single partition to shrink for space; nil disables shrinking. + // Size 0 == shrink-to-fit; a positive Size shrinks to exactly that size. + shrink := &resizer.ShrinkSpec{ID: resizer.NewPartitionIdentifier(resizer.IdentifierByName, "sda3")} + + // Apply(disk, desired, shrink, fixErrors, dryRun) + // disk -- image file path or block device; "" to auto-discover from the identifiers + // fixErrors -- repair filesystem errors (e2fsck -y / fsck.fat -a) instead of read-only checks + // dryRun -- plan only, make no changes + if err := resizer.Apply("/dev/sda", desired, shrink, false, false); err != nil { + log.Fatalf("apply failed: %v", err) } } ``` -Partitions are selected with `IdentifierByName`, `IdentifierByLabel`, or -`IdentifierByUUID`. Sizes passed to `NewPartitionChange` are in bytes; the -exported `KB`, `MB`, and `GB` constants are convenient multipliers. +To create a partition, give a `PartitionSpec` with a `GUID` not present on the +disk (leaving `Match` nil), plus `Label`, `TypeGUID`, `Index`, and `FS`. +Identifiers use `IdentifierByName`, `IdentifierByLabel`, or `IdentifierByUUID`; +sizes are in bytes, with the exported `KB`, `MB`, and `GB` constants as +convenient multipliers. Grown partitions keep their original partition number. ### Errors -`Run` returns a non-nil `error` for any failure. The error wraps the failing +`Apply` returns a non-nil `error` for any failure. The error wraps the failing tool's exit status and, for the filesystem tools, includes the tail of their stderr, so a caller gets the reason — not just `exit status N`. Tool output is also streamed live to the process's stdout/stderr. ### Pre-flight integrity checks -Before making any change, `Run` integrity-checks every source filesystem it will +Before making any change, `Apply` integrity-checks every source filesystem it will read or modify — the shrink partition and each grow source. ext4 sources are checked with `e2fsck` and FAT32 sources with `fsck.fat`. By default the checks are read-only and an inconsistent filesystem aborts the resize; pass `fixErrors` diff --git a/apply.go b/apply.go new file mode 100644 index 0000000..a7fd557 --- /dev/null +++ b/apply.go @@ -0,0 +1,475 @@ +package partitionresizer + +import ( + "errors" + "fmt" + "log" + "path/filepath" + "strings" + + diskfs "github.com/diskfs/go-diskfs" + "github.com/diskfs/go-diskfs/backend/file" + "github.com/diskfs/go-diskfs/disk" + "github.com/diskfs/go-diskfs/partition/gpt" +) + +// Apply reconciles the disk so that every PartitionSpec in desired exists with +// its declared identity and at least MinSize bytes, optionally shrinking one +// partition (shrink) to free space. +// +// desired is monotonically non-destructive: a partition that is absent is +// created (and, per its FS, formatted empty), one smaller than MinSize is grown +// in place-preserving fashion, and one already at least MinSize is left +// untouched -- it is never shrunk. shrink is the ONLY operation that reduces a +// partition; when nil, nothing can be shrunk, so a grow+create-only call cannot +// shrink anything by accident. +// +// An existing partition is located either by GUID (the default) or, when a spec +// sets Match, by name/label/uuid; a matched partition's type and label are +// asserted against the spec so Apply never operates on an unexpected disk. +// diskPath may be empty, in which case the disk is discovered by the Match and +// shrink identifiers (all of which must resolve to the same disk). +// +// A created partition's GPT entry is published only by the final table write, so +// an interrupted run is recovered by re-running: a partition present at its GUID +// is complete (filesystem included), otherwise it is (re)made. +func Apply(diskPath string, desired []PartitionSpec, shrink *ShrinkSpec, fixErrors, dryRun bool) error { + diskPath, desired, shrink, err := resolveDiskAndNames(diskPath, desired, shrink, "") + if err != nil { + return err + } + + // Open the whole disk read-write but NOT O_EXCL: partitionresizer shells out + // to e2fsck/resize2fs/fsck.fat on the child partitions, which open those + // O_EXCL; holding the parent O_EXCL would make those child opens fail on a + // real block device. The caller guarantees the partitions are unmounted. + backendFile, err := file.OpenFromPathWithExclusive(diskPath, false, false) + if err != nil { + return err + } + d, err := diskfs.OpenBackend(maybeWrapBackend(backendFile)) + if err != nil { + return err + } + tableRaw, err := d.GetPartitionTable() + if err != nil { + return err + } + table, ok := tableRaw.(*gpt.Table) + if !ok { + return fmt.Errorf("unsupported partition table type, only GPT is supported") + } + + resizes, err := planApply(d, table, desired, shrink) + if err != nil { + return err + } + if len(resizes) == 0 { + log.Printf("Apply: disk already matches desired state, nothing to do") + return nil + } + + if dryRun { + for _, r := range resizes { + switch { + case r.create: + log.Printf("Apply plan: create %q (GUID %s) partition %d size %d fs %v", r.target.label, r.target.uuid, r.target.number, r.target.size, r.fsType) + case r.target.size < r.original.size: + log.Printf("Apply plan: shrink %q (partition %d) %d -> %d", r.original.label, r.original.number, r.original.size, r.target.size) + default: + log.Printf("Apply plan: grow %q (partition %d) %d -> %d", r.original.label, r.original.number, r.original.size, r.target.size) + } + } + return nil + } + if err := checkSourceFilesystems(d, resizes, fixErrors); err != nil { + return err + } + return resize(d, resizes, fixErrors, true) +} + +// planApply diffs desired + shrink against the live GPT and returns the full, +// allocated resize plan. It grows/creates the desired partitions and, when a +// shrink is given, frees space by reducing that partition: to an explicit Size, +// or -- when Size is 0 -- only as much as the grows/creates need (shrink-to-fit), +// rounded up to a whole GB. Resumed grows (whose relocated "