From 85d04939aa6efca11fa78b1e9e5a6b6e10414f48 Mon Sep 17 00:00:00 2001 From: eriknordmark Date: Wed, 10 Jun 2026 17:19:42 +0200 Subject: [PATCH 01/13] test: scaled mixed-layout resume tests Add a scaled-down sample disk built in-test -- FAT32 ESP, squashfs IMGA/IMGB, a placeholder CONFIG, and an ext4 P9 at index 9 -- and drive two operations through the interrupt/resume harness: Case 1 (TestRunResumeShrink): shrink P9 in place to free space at the end of the disk. Case 2 (TestRunResumeGrow): on the post-shrink disk, grow ESP/IMGA/IMGB into the freed space, copy their content, and finalize with updatePartitions, relocating them to the end of the disk under their original labels/indices. Each interruption point is re-run to completion and the final layout and content are verified (squashfs raw images by hash, the ESP/P9 marker files by read-back). The squashfs images are produced with mksquashfs and written raw, since go-diskfs cannot create squashfs and ext4 on a single 512-byte-sector disk; go-diskfs detects them as unknown and raw-copies them, the same branch it uses for squashfs. The tests are guarded by testing.Short and require mksquashfs. Signed-off-by: eriknordmark Co-Authored-By: Claude Opus 4.8 --- layout_resume_test.go | 465 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 465 insertions(+) create mode 100644 layout_resume_test.go diff --git a/layout_resume_test.go b/layout_resume_test.go new file mode 100644 index 0000000..1327a9a --- /dev/null +++ b/layout_resume_test.go @@ -0,0 +1,465 @@ +package partitionresizer + +import ( + "crypto/sha256" + "fmt" + "io" + "os" + "os/exec" + "path/filepath" + "testing" + + "github.com/diskfs/go-diskfs" + "github.com/diskfs/go-diskfs/backend/file" + "github.com/diskfs/go-diskfs/disk" + "github.com/diskfs/go-diskfs/filesystem" + "github.com/diskfs/go-diskfs/partition/gpt" +) + +// Scaled-down sample layout used by the resume tests. It mirrors the partition +// shape of an A/B-image appliance disk -- the layout EVE-OS uses, which +// motivated this tool: a FAT32 ESP, two read-only squashfs image slots, a small +// config partition, and an ext4 data partition that fills the rest at a +// non-contiguous index. Every size is scaled down from the real ~64 GB device +// so the tests run in seconds while exercising the same partition types and +// code paths: +// +// ESP FAT32, "tiny" 48 MB index 1 +// IMGA squashfs image 100 MB index 2 +// IMGB squashfs image 100 MB index 3 +// CONFIG placeholder ("1MB") 1 MB index 4 (untouched by the resize) +// P9 ext4, fills the rest 900 MB index 9 +// +// FAT32 cannot be represented at 1 MB (it needs tens of MB of clusters), and +// partitionresizer never touches CONFIG, so CONFIG is left as a 1 MB +// placeholder partition. +const ( + sectorMB = MB / 512 // sectors per MB at 512-byte sectors + defaultDiskMB = 1152 + espStartMB = 1 + espMB = 48 + imgaMB = 100 + imgbMB = 100 + configMB = 1 + defaultP9MB = 900 + + espIndex = 1 + imgaIndex = 2 + imgbIndex = 3 + configIndex = 4 + p9Index = 9 +) + +// sampleLayout records the built disk and the content fingerprints needed to +// verify that a resize preserves data. +type sampleLayout struct { + path string + espStart uint64 // sectors + imgaStart uint64 + imgbStart uint64 + configStart uint64 + p9Start uint64 + imgaSum [32]byte // sha256 of the IMGA partition image region + imgbSum [32]byte +} + +// buildSampleLayout writes the scaled sample disk image into a temp file and +// returns its description. ESP and P9 get real FAT32/ext4 filesystems with +// marker files; IMGA/IMGB get real squashfs images written raw (built with +// mksquashfs, as the real read-only image slots are), since go-diskfs cannot create squashfs and ext4 on +// the same 512-byte-sector disk. +func buildSampleLayout(t *testing.T) sampleLayout { + t.Helper() + if _, err := exec.LookPath("mksquashfs"); err != nil { + t.Skip("mksquashfs not available") + } + + dir := t.TempDir() + imgPath := filepath.Join(dir, "disk.img") + + espStart := uint64(espStartMB * sectorMB) + imgaStart := espStart + uint64(espMB*sectorMB) + imgbStart := imgaStart + uint64(imgaMB*sectorMB) + configStart := imgbStart + uint64(imgbMB*sectorMB) + p9Start := configStart + uint64(configMB*sectorMB) + + f, err := os.Create(imgPath) + if err != nil { + t.Fatalf("create disk image: %v", err) + } + if err := f.Truncate(int64(defaultDiskMB) * MB); err != nil { + t.Fatalf("truncate disk image: %v", err) + } + _ = f.Close() + + f, err = os.OpenFile(imgPath, os.O_RDWR, 0) + if err != nil { + t.Fatalf("reopen disk image: %v", err) + } + defer func() { _ = f.Close() }() + d, err := diskfs.OpenBackend(file.New(f, false), diskfs.WithOpenMode(diskfs.ReadWrite)) + if err != nil { + t.Fatalf("open disk: %v", err) + } + + table := &gpt.Table{ + Partitions: []*gpt.Partition{ + {Index: espIndex, Start: espStart, Size: espMB * MB, Type: gpt.EFISystemPartition, Name: "ESP"}, + {Index: imgaIndex, Start: imgaStart, Size: imgaMB * MB, Type: gpt.LinuxFilesystem, Name: "IMGA"}, + {Index: imgbIndex, Start: imgbStart, Size: imgbMB * MB, Type: gpt.LinuxFilesystem, Name: "IMGB"}, + {Index: configIndex, Start: configStart, Size: configMB * MB, Type: gpt.LinuxFilesystem, Name: "CONFIG"}, + {Index: p9Index, Start: p9Start, Size: defaultP9MB * MB, Type: gpt.LinuxFilesystem, Name: "P9"}, + }, + } + if err := d.Partition(table); err != nil { + t.Fatalf("write partition table: %v", err) + } + + // ESP: real FAT32 with a marker file + espFS, err := d.CreateFilesystem(disk.FilesystemSpec{Partition: espIndex, FSType: filesystem.TypeFat32, VolumeLabel: "ESP"}) + if err != nil { + t.Fatalf("create ESP fat32: %v", err) + } + writeMarker(t, espFS, "/esp-marker.txt", "esp content") + + // P9: real ext4 with a marker file + p9FS, err := d.CreateFilesystem(disk.FilesystemSpec{Partition: p9Index, FSType: filesystem.TypeExt4, VolumeLabel: "P9"}) + if err != nil { + t.Fatalf("create P9 ext4: %v", err) + } + writeMarker(t, p9FS, "/p9-marker.txt", "persist content") + + // IMGA/IMGB: real squashfs images built with mksquashfs and written raw + writeSquashfsImage(t, d, imgaStart, "IMGA") + writeSquashfsImage(t, d, imgbStart, "IMGB") + if err := f.Sync(); err != nil { + t.Fatalf("sync disk image: %v", err) + } + + return sampleLayout{ + path: imgPath, + espStart: espStart, + imgaStart: imgaStart, + imgbStart: imgbStart, + configStart: configStart, + p9Start: p9Start, + // fingerprint the leading 1 MB of each IMG region (squashfs blob plus + // trailing zeros) so a raw relocation can be checked for fidelity + imgaSum: partitionRegionSum(t, imgPath, imgaStart, 1*MB), + imgbSum: partitionRegionSum(t, imgPath, imgbStart, 1*MB), + } +} + +func writeMarker(t *testing.T, fs filesystem.FileSystem, name, content string) { + t.Helper() + fh, err := fs.OpenFile(name, os.O_CREATE|os.O_RDWR) + if err != nil { + t.Fatalf("open %s: %v", name, err) + } + if _, err := fh.Write([]byte(content)); err != nil { + t.Fatalf("write %s: %v", name, err) + } + if err := fh.Close(); err != nil { + t.Fatalf("close %s: %v", name, err) + } +} + +// writeSquashfsImage builds a small squashfs with mksquashfs and writes it raw +// at the given partition start; returns the sha256 of the written image bytes +// so the test can confirm a raw copy preserved it. +func writeSquashfsImage(t *testing.T, d *disk.Disk, startSector uint64, label string) [32]byte { + t.Helper() + srcDir := t.TempDir() + if err := os.WriteFile(filepath.Join(srcDir, "marker"), []byte(label+" squashfs payload"), 0o644); err != nil { + t.Fatalf("write squashfs source: %v", err) + } + sqfs := filepath.Join(t.TempDir(), label+".sqfs") + out, err := exec.Command("mksquashfs", srcDir, sqfs, "-noappend", "-no-progress", "-quiet").CombinedOutput() + if err != nil { + t.Fatalf("mksquashfs: %v\n%s", err, out) + } + data, err := os.ReadFile(sqfs) + if err != nil { + t.Fatalf("read squashfs: %v", err) + } + w, err := d.Backend.Writable() + if err != nil { + t.Fatalf("writable backend: %v", err) + } + if _, err := w.WriteAt(data, int64(startSector)*512); err != nil { + t.Fatalf("write squashfs image: %v", err) + } + return sha256.Sum256(data) +} + +// partitionRegionSum hashes the leading len bytes of the partition that begins +// at startSector, for raw-copy content verification. +func partitionRegionSum(t *testing.T, path string, startSector uint64, length int) [32]byte { + t.Helper() + f, err := os.Open(path) + if err != nil { + t.Fatalf("open for hashing: %v", err) + } + defer func() { _ = f.Close() }() + buf := make([]byte, length) + if _, err := f.ReadAt(buf, int64(startSector)*512); err != nil { + t.Fatalf("read region: %v", err) + } + return sha256.Sum256(buf) +} + +// gptByName reads the partition table at path and returns name->partition. +func gptByName(t *testing.T, path string) map[string]*gpt.Partition { + t.Helper() + f, err := os.Open(path) + if err != nil { + t.Fatalf("open disk: %v", err) + } + defer func() { _ = f.Close() }() + d, err := diskfs.OpenBackend(file.New(f, true)) + if err != nil { + t.Fatalf("open backend: %v", err) + } + tr, err := d.GetPartitionTable() + if err != nil { + t.Fatalf("get table: %v", err) + } + m := make(map[string]*gpt.Partition) + for _, p := range tr.(*gpt.Table).Partitions { + if p.Type == gpt.Unused { + continue + } + m[p.Name] = p + } + return m +} + +// TestSampleLayoutSmoke is a non-interrupted end-to-end check of the scaled sample +// layout: build it, shrink P9 (Case 1), then grow ESP/IMGA/IMGB into the freed +// space with the updatePartitions finalize (Case 2), asserting the layout and +// that IMG content survives the raw relocation. +func TestSampleLayoutSmoke(t *testing.T) { + if testing.Short() { + t.Skip("slow end-to-end resize test") + } + fx := buildSampleLayout(t) + + // the 1 MB region fingerprint captures the (tiny) squashfs blob plus zeros + const hashLen = 1 * MB + + // confirm filesystem detection on the source partitions + report := describeLayout(t, fx.path) + t.Logf("fixture filesystems:\n%s", report) + + // Case 1: shrink P9 by 600 MB (free space at the end of the disk) + p9Shrink := []PartitionChange{NewPartitionChange(IdentifierByLabel, "P9", (defaultP9MB-600)*MB)} + if err := Run(fx.path, nil, p9Shrink, false, false, false); err != nil { + t.Fatalf("Case 1 (shrink P9) failed: %v", err) + } + after1 := gptByName(t, fx.path) + if got := int64(after1["P9"].GetSize()); got != (defaultP9MB-600)*MB { + t.Errorf("after Case 1: P9 size = %d, want %d", got, (defaultP9MB-600)*MB) + } + + // Case 2: grow ESP/IMGA/IMGB into the freed space; preserveNumbers keeps + // their original indices/labels via updatePartitions + grow := []PartitionChange{ + NewPartitionChange(IdentifierByLabel, "ESP", 96*MB), + NewPartitionChange(IdentifierByLabel, "IMGA", 200*MB), + NewPartitionChange(IdentifierByLabel, "IMGB", 200*MB), + } + if err := Run(fx.path, nil, grow, false, false, true); err != nil { + t.Fatalf("Case 2 (grow + updatePartitions) failed: %v", err) + } + + after2 := gptByName(t, fx.path) + // ESP/IMGA/IMGB keep their labels and indices, now at the end of the disk + for _, name := range []string{"ESP", "IMGA", "IMGB", "CONFIG", "P9"} { + if _, ok := after2[name]; !ok { + t.Errorf("after Case 2: partition %q missing", name) + } + } + if after2["ESP"].Index != espIndex || after2["IMGA"].Index != imgaIndex || after2["IMGB"].Index != imgbIndex { + t.Errorf("after Case 2: indices not preserved: ESP=%d IMGA=%d IMGB=%d", after2["ESP"].Index, after2["IMGA"].Index, after2["IMGB"].Index) + } + // the grown ESP/IMGA/IMGB must now start after the (shrunken) P9 + if after2["IMGA"].Start <= after2["P9"].Start { + t.Errorf("after Case 2: IMGA (start %d) should be relocated past P9 (start %d)", after2["IMGA"].Start, after2["P9"].Start) + } + if int64(after2["IMGA"].GetSize()) != 200*MB || int64(after2["IMGB"].GetSize()) != 200*MB || int64(after2["ESP"].GetSize()) != 96*MB { + t.Errorf("after Case 2: grown sizes wrong: ESP=%d IMGA=%d IMGB=%d", after2["ESP"].GetSize(), after2["IMGA"].GetSize(), after2["IMGB"].GetSize()) + } + // IMG content survived the raw relocation (compare the relocated region to + // the build-time fingerprint of the source region) + if got := partitionRegionSum(t, fx.path, after2["IMGA"].Start, hashLen); got != fx.imgaSum { + t.Errorf("IMGA content not preserved across relocation") + } + if got := partitionRegionSum(t, fx.path, after2["IMGB"].Start, hashLen); got != fx.imgbSum { + t.Errorf("IMGB content not preserved across relocation") + } +} + +func describeLayout(t *testing.T, path string) string { + t.Helper() + f, err := os.Open(path) + if err != nil { + t.Fatalf("open: %v", err) + } + defer func() { _ = f.Close() }() + d, err := diskfs.OpenBackend(file.New(f, true)) + if err != nil { + t.Fatalf("open backend: %v", err) + } + var s string + for _, idx := range []int{espIndex, imgaIndex, imgbIndex, p9Index} { + fs, err := d.GetFilesystem(idx) + if err != nil { + s += fmt.Sprintf(" partition %d: GetFilesystem error: %v\n", idx, err) + continue + } + s += fmt.Sprintf(" partition %d: %v\n", idx, fs.Type()) + } + return s +} + +// readPartitionFile reads a file from the filesystem on the given partition. +func readPartitionFile(t *testing.T, path string, partIndex int, name string) string { + t.Helper() + f, err := os.Open(path) + if err != nil { + t.Fatalf("open disk: %v", err) + } + defer func() { _ = f.Close() }() + d, err := diskfs.OpenBackend(file.New(f, true)) + if err != nil { + t.Fatalf("open backend: %v", err) + } + fs, err := d.GetFilesystem(partIndex) + if err != nil { + t.Fatalf("get filesystem on partition %d: %v", partIndex, err) + } + fh, err := fs.OpenFile(name, os.O_RDONLY) + if err != nil { + t.Fatalf("open %s on partition %d: %v", name, partIndex, err) + } + defer func() { _ = fh.Close() }() + data, err := io.ReadAll(fh) + if err != nil { + t.Fatalf("read %s: %v", name, err) + } + return string(data) +} + +// shrinkP9 is the Case 1 operation: shrink the P9 ext4 partition in place by +// 600 MB, freeing space at the end of the disk. It is expressed as a +// PartitionChange to a smaller size (calculateResizes treats target < original +// as a shrink in place). +var shrinkP9 = []PartitionChange{NewPartitionChange(IdentifierByLabel, "P9", (defaultP9MB-600)*MB)} + +// growImages is the Case 2 operation: grow ESP/IMGA/IMGB into the freed space. +var growImages = []PartitionChange{ + NewPartitionChange(IdentifierByLabel, "ESP", 96*MB), + NewPartitionChange(IdentifierByLabel, "IMGA", 200*MB), + NewPartitionChange(IdentifierByLabel, "IMGB", 200*MB), +} + +// dummyShrink is an unused shrink identifier required by runResizeStepsUpTo's +// signature; both cases drive their shrink/grow entirely through the grow +// list (P9-in-place for Case 1, free-space grows for Case 2), so planResizes +// never consults shrinkPartition. +func dummyShrink() PartitionIdentifier { return NewPartitionIdentifier(IdentifierByLabel, "P9") } + +// TestRunResumeShrink is Case 1 through the interrupt/resume harness: on the +// scaled sample layout, interrupt the in-place P9 shrink after each step, re-run to +// completion, and confirm P9 is shrunk (with its content and the other +// partitions intact). Exercises the non-contiguous P9 index (index 9) that the +// shrinkPartitions fix addresses. +func TestRunResumeShrink(t *testing.T) { + if testing.Short() { + t.Skip("slow end-to-end resize test") + } + for _, stopAfter := range []int{stepShrinkFilesystems, stepShrinkPartitions, stepUpdatePartitions} { + t.Run(fmt.Sprintf("stopAfter=%d", stopAfter), func(t *testing.T) { + fx := buildSampleLayout(t) + runResizeStepsUpTo(t, fx.path, dummyShrink(), shrinkP9, false, stopAfter, false, false) + if err := Run(fx.path, nil, shrinkP9, false, false, false); err != nil { + t.Fatalf("resume Run failed: %v", err) + } + after := gptByName(t, fx.path) + if got := int64(after["P9"].GetSize()); got != (defaultP9MB-600)*MB { + t.Errorf("P9 size = %d, want %d", got, (defaultP9MB-600)*MB) + } + // P9 data preserved, other partitions untouched + if got := readPartitionFile(t, fx.path, p9Index, "/p9-marker.txt"); got != "persist content" { + t.Errorf("P9 marker = %q, want %q", got, "persist content") + } + for _, name := range []string{"ESP", "IMGA", "IMGB", "CONFIG"} { + if _, ok := after[name]; !ok { + t.Errorf("partition %q missing after shrink", name) + } + } + if int64(after["ESP"].GetSize()) != espMB*MB || after["IMGA"].Start != fx.imgaStart { + t.Errorf("non-P9 partitions changed: ESP size=%d IMGA start=%d", after["ESP"].GetSize(), after["IMGA"].Start) + } + }) + } +} + +// TestRunResumeGrow is Case 2 through the interrupt/resume harness: starting +// from the Case 1 result (P9 already shrunk), interrupt the grow of +// ESP/IMGA/IMGB after each step, re-run to completion, and confirm the grown +// partitions are relocated to the end of the disk under their original +// labels/indices with content intact, while P9/CONFIG are untouched. +func TestRunResumeGrow(t *testing.T) { + if testing.Short() { + t.Skip("slow end-to-end resize test") + } + // build the fixture and run Case 1 once to produce the post-shrink base disk + base := buildSampleLayout(t) + if err := Run(base.path, nil, shrinkP9, false, false, false); err != nil { + t.Fatalf("Case 1 (shrink) setup failed: %v", err) + } + + for _, stopAfter := range []int{stepCreatePartitions, stepCopyFilesystems, stepUpdatePartitions} { + t.Run(fmt.Sprintf("stopAfter=%d", stopAfter), func(t *testing.T) { + diskCopy := filepath.Join(t.TempDir(), "disk.img") + if err := testCopyFile(base.path, diskCopy); err != nil { + t.Fatalf("copy base disk: %v", err) + } + runResizeStepsUpTo(t, diskCopy, dummyShrink(), growImages, true, stopAfter, false, false) + if err := Run(diskCopy, nil, growImages, false, false, true); err != nil { + t.Fatalf("resume Run failed: %v", err) + } + + after := gptByName(t, diskCopy) + // grown partitions keep label+index, relocated past P9, at target size + if after["ESP"].Index != espIndex || after["IMGA"].Index != imgaIndex || after["IMGB"].Index != imgbIndex { + t.Errorf("indices not preserved: ESP=%d IMGA=%d IMGB=%d", after["ESP"].Index, after["IMGA"].Index, after["IMGB"].Index) + } + if after["IMGA"].Start <= after["P9"].Start || after["IMGB"].Start <= after["P9"].Start || after["ESP"].Start <= after["P9"].Start { + t.Errorf("grown partitions not relocated past P9: ESP=%d IMGA=%d IMGB=%d P9=%d", + after["ESP"].Start, after["IMGA"].Start, after["IMGB"].Start, after["P9"].Start) + } + if int64(after["ESP"].GetSize()) != 96*MB || int64(after["IMGA"].GetSize()) != 200*MB || int64(after["IMGB"].GetSize()) != 200*MB { + t.Errorf("grown sizes wrong: ESP=%d IMGA=%d IMGB=%d", after["ESP"].GetSize(), after["IMGA"].GetSize(), after["IMGB"].GetSize()) + } + // content preserved: IMG raw images and the ESP marker + if got := partitionRegionSum(t, diskCopy, after["IMGA"].Start, 1*MB); got != base.imgaSum { + t.Errorf("IMGA content not preserved") + } + if got := partitionRegionSum(t, diskCopy, after["IMGB"].Start, 1*MB); got != base.imgbSum { + t.Errorf("IMGB content not preserved") + } + if got := readPartitionFile(t, diskCopy, espIndex, "/esp-marker.txt"); got != "esp content" { + t.Errorf("ESP marker = %q, want %q", got, "esp content") + } + // P9 untouched (still shrunk, content intact) + if got := int64(after["P9"].GetSize()); got != (defaultP9MB-600)*MB { + t.Errorf("P9 size changed: %d", got) + } + if got := readPartitionFile(t, diskCopy, p9Index, "/p9-marker.txt"); got != "persist content" { + t.Errorf("P9 marker = %q", got) + } + }) + } +} From 12f95d8265cfda4ad79a17861851aa991e89f724 Mon Sep 17 00:00:00 2001 From: eriknordmark Date: Wed, 10 Jun 2026 18:26:13 +0200 Subject: [PATCH 02/13] test: log GPT partition table at each resize stage Add TestLayoutStagesDump, which drives the shrink+grow scenario and logs the GPT partition table (sorted by on-disk start, with free-space gaps) at four points: initial layout, after the P9 ext4 shrink, after the *_resized2 partitions are created and filled, and after the final updatePartitions. Useful for seeing how the resize rearranges the disk. Signed-off-by: eriknordmark Co-Authored-By: Claude Opus 4.8 --- layout_resume_test.go | 82 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/layout_resume_test.go b/layout_resume_test.go index 1327a9a..e1ff13f 100644 --- a/layout_resume_test.go +++ b/layout_resume_test.go @@ -7,6 +7,8 @@ import ( "os" "os/exec" "path/filepath" + "sort" + "strings" "testing" "github.com/diskfs/go-diskfs" @@ -463,3 +465,83 @@ func TestRunResumeGrow(t *testing.T) { }) } } + +// gptDump renders the GPT partition table at path, sorted by on-disk start, +// in MB with free-space gaps called out — for human inspection of how a resize +// rearranges the disk. +func gptDump(t *testing.T, path string) string { + t.Helper() + f, err := os.Open(path) + if err != nil { + t.Fatalf("open disk: %v", err) + } + defer func() { _ = f.Close() }() + d, err := diskfs.OpenBackend(file.New(f, true)) + if err != nil { + t.Fatalf("open backend: %v", err) + } + tr, err := d.GetPartitionTable() + if err != nil { + t.Fatalf("get table: %v", err) + } + type row struct { + idx int + name string + start, size int64 + } + var rows []row + for _, p := range tr.(*gpt.Table).Partitions { + if p.Type == gpt.Unused { + continue + } + rows = append(rows, row{p.Index, p.Name, p.GetStart(), int64(p.GetSize())}) + } + sort.Slice(rows, func(i, j int) bool { return rows[i].start < rows[j].start }) + + var b strings.Builder + fmt.Fprintf(&b, " %-3s %-10s %10s %10s %10s\n", "#", "name", "startMB", "endMB", "sizeMB") + var prevEndMB int64 + for _, r := range rows { + startMB := r.start / MB + endMB := (r.start + r.size) / MB + sizeMB := r.size / MB + if gap := startMB - prevEndMB; gap > 0 { + fmt.Fprintf(&b, " %-3s %-10s %10s %10s %10d <- free\n", "", "(free)", "", "", gap) + } + fmt.Fprintf(&b, " %-3d %-10s %10d %10d %10d\n", r.idx, r.name, startMB, endMB, sizeMB) + prevEndMB = endMB + } + if tail := int64(defaultDiskMB) - prevEndMB; tail > 0 { + fmt.Fprintf(&b, " %-3s %-10s %10s %10s %10d <- free\n", "", "(free)", "", "", tail) + } + return b.String() +} + +// TestLayoutStagesDump logs the GPT partition table at the four stages of the resize +// scenario, by actually driving the resize. Run with -v to see the tables. +func TestLayoutStagesDump(t *testing.T) { + if testing.Short() { + t.Skip("slow end-to-end resize test") + } + fx := buildSampleLayout(t) + t.Logf("Stage 1 - initial layout (disk %d MB):\n%s", defaultDiskMB, gptDump(t, fx.path)) + + // Stage 2: Case 1 shrinks P9 in place, freeing space at the end + if err := Run(fx.path, nil, shrinkP9, false, false, false); err != nil { + t.Fatalf("Case 1 (shrink P9): %v", err) + } + t.Logf("Stage 2 - after P9 ext4 shrink (freed 600 MB at end):\n%s", gptDump(t, fx.path)) + + // Stage 3: Case 2 up to and including copyFilesystems -- the *_resized2 + // partitions have been created in the freed space and filled, originals + // still present + runResizeStepsUpTo(t, fx.path, dummyShrink(), growImages, true, stepCopyFilesystems, false, false) + t.Logf("Stage 3 - ESP/IMGA/IMGB_resized2 created and content copied (pre-updatePartitions):\n%s", gptDump(t, fx.path)) + + // Stage 4: resume to completion -- the single updatePartitions relabels and + // reindexes the *_resized2 partitions to the originals and removes the old + if err := Run(fx.path, nil, growImages, false, false, true); err != nil { + t.Fatalf("Case 2 resume to completion: %v", err) + } + t.Logf("Stage 4 - after updatePartitions (final):\n%s", gptDump(t, fx.path)) +} From 59ba7d8e0becd8c4fd3aab8e39e5597e29bdce91 Mon Sep 17 00:00:00 2001 From: eriknordmark Date: Wed, 10 Jun 2026 22:54:03 +0200 Subject: [PATCH 03/13] test: resize stress tests (kill/resume chaos + invariants) Add a suite of stress tests on the scaled fixture: - TestChaosKill: runs the real resizer binary doing the grow and SIGKILLs it at random points 1-4 times, then runs once to completion, asserting the final layout, IMG content, and -- critically -- that persist (P9) and CONFIG are never corrupted. Exercises resume from arbitrary interruption (partial GPT writes, half-copied content) and repeated crashes; it is what surfaced the grow-only CLI panic. - TestUntouchedPartitionsByteIntegrity: persist and CONFIG are byte-identical across a grow. - TestInsufficientSpaceIsAtomic: a grow that cannot fit fails without modifying the disk. - TestShrinkBelowUsedAborts: shrinking below the ext4 used size is refused, partition and data intact. - TestCombinedShrinkGrow: a single Run that shrinks P9 and grows ESP/IMGA/ IMGB (larger P9 fixture, since the shrink is rounded up to a whole GB). - TestShrinkPreservesP9Content: P9 filled with many files survives the shrink (block relocation). The fixture builder is parameterized by disk/P9 size and gptDump now reads the disk size from the file. All guarded by testing.Short; the chaos test also needs the resizer binary (built in-test) and mksquashfs. Signed-off-by: eriknordmark Co-Authored-By: Claude Opus 4.8 --- layout_resume_test.go | 22 ++- resume_stress_test.go | 341 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 360 insertions(+), 3 deletions(-) create mode 100644 resume_stress_test.go diff --git a/layout_resume_test.go b/layout_resume_test.go index e1ff13f..85d49be 100644 --- a/layout_resume_test.go +++ b/layout_resume_test.go @@ -56,6 +56,8 @@ const ( // verify that a resize preserves data. type sampleLayout struct { path string + diskMB int64 + p9MB int64 espStart uint64 // sectors imgaStart uint64 imgbStart uint64 @@ -71,6 +73,13 @@ type sampleLayout struct { // mksquashfs, as the real read-only image slots are), since go-diskfs cannot create squashfs and ext4 on // the same 512-byte-sector disk. func buildSampleLayout(t *testing.T) sampleLayout { + return buildSampleLayoutSized(t, defaultDiskMB, defaultP9MB) +} + +// buildSampleLayoutSized is buildSampleLayout with an explicit disk size and P9 +// size, for tests that need a larger persist partition (e.g. a combined +// shrink+grow in one Run, where go-diskfs rounds the shrink up to a whole GB). +func buildSampleLayoutSized(t *testing.T, diskMB, p9MB int64) sampleLayout { t.Helper() if _, err := exec.LookPath("mksquashfs"); err != nil { t.Skip("mksquashfs not available") @@ -89,7 +98,7 @@ func buildSampleLayout(t *testing.T) sampleLayout { if err != nil { t.Fatalf("create disk image: %v", err) } - if err := f.Truncate(int64(defaultDiskMB) * MB); err != nil { + if err := f.Truncate(diskMB * MB); err != nil { t.Fatalf("truncate disk image: %v", err) } _ = f.Close() @@ -110,7 +119,7 @@ func buildSampleLayout(t *testing.T) sampleLayout { {Index: imgaIndex, Start: imgaStart, Size: imgaMB * MB, Type: gpt.LinuxFilesystem, Name: "IMGA"}, {Index: imgbIndex, Start: imgbStart, Size: imgbMB * MB, Type: gpt.LinuxFilesystem, Name: "IMGB"}, {Index: configIndex, Start: configStart, Size: configMB * MB, Type: gpt.LinuxFilesystem, Name: "CONFIG"}, - {Index: p9Index, Start: p9Start, Size: defaultP9MB * MB, Type: gpt.LinuxFilesystem, Name: "P9"}, + {Index: p9Index, Start: p9Start, Size: uint64(p9MB) * MB, Type: gpt.LinuxFilesystem, Name: "P9"}, }, } if err := d.Partition(table); err != nil { @@ -140,6 +149,8 @@ func buildSampleLayout(t *testing.T) sampleLayout { return sampleLayout{ path: imgPath, + diskMB: diskMB, + p9MB: p9MB, espStart: espStart, imgaStart: imgaStart, imgbStart: imgbStart, @@ -476,6 +487,11 @@ func gptDump(t *testing.T, path string) string { t.Fatalf("open disk: %v", err) } defer func() { _ = f.Close() }() + fi, err := f.Stat() + if err != nil { + t.Fatalf("stat disk: %v", err) + } + diskMB := fi.Size() / MB d, err := diskfs.OpenBackend(file.New(f, true)) if err != nil { t.Fatalf("open backend: %v", err) @@ -511,7 +527,7 @@ func gptDump(t *testing.T, path string) string { fmt.Fprintf(&b, " %-3d %-10s %10d %10d %10d\n", r.idx, r.name, startMB, endMB, sizeMB) prevEndMB = endMB } - if tail := int64(defaultDiskMB) - prevEndMB; tail > 0 { + if tail := diskMB - prevEndMB; tail > 0 { fmt.Fprintf(&b, " %-3s %-10s %10s %10s %10d <- free\n", "", "(free)", "", "", tail) } return b.String() diff --git a/resume_stress_test.go b/resume_stress_test.go new file mode 100644 index 0000000..71a1b9c --- /dev/null +++ b/resume_stress_test.go @@ -0,0 +1,341 @@ +package partitionresizer + +import ( + "bytes" + "crypto/sha256" + "fmt" + "io" + "math/rand" + "os" + "os/exec" + "path/filepath" + "testing" + "time" + + "github.com/diskfs/go-diskfs" + "github.com/diskfs/go-diskfs/backend/file" +) + +// wholeDiskSum streams a sha256 over the entire disk image. +func wholeDiskSum(t *testing.T, path string) [32]byte { + t.Helper() + f, err := os.Open(path) + if err != nil { + t.Fatalf("open disk: %v", err) + } + defer func() { _ = f.Close() }() + h := sha256.New() + if _, err := io.Copy(h, f); err != nil { + t.Fatalf("hash disk: %v", err) + } + var out [32]byte + copy(out[:], h.Sum(nil)) + return out +} + +// --- #3: untouched partitions stay byte-identical ---------------------------- + +// TestUntouchedPartitionsByteIntegrity verifies that the partitions a grow +// must not touch -- persist (P9, user data) and CONFIG -- are byte-for-byte +// unchanged across Case 2, and that P9's filesystem data survives. +func TestUntouchedPartitionsByteIntegrity(t *testing.T) { + if testing.Short() { + t.Skip("slow end-to-end resize test") + } + fx := buildSampleLayout(t) + if err := Run(fx.path, nil, shrinkP9, false, false, false); err != nil { + t.Fatalf("Case 1 (shrink P9): %v", err) + } + p9Len := int((defaultP9MB - 600) * MB) + cfgLen := int(configMB * MB) + p9Before := partitionRegionSum(t, fx.path, fx.p9Start, p9Len) + cfgBefore := partitionRegionSum(t, fx.path, fx.configStart, cfgLen) + + if err := Run(fx.path, nil, growImages, false, false, true); err != nil { + t.Fatalf("Case 2 (grow): %v", err) + } + + if partitionRegionSum(t, fx.path, fx.p9Start, p9Len) != p9Before { + t.Error("P9 (persist) region changed during the grow") + } + if partitionRegionSum(t, fx.path, fx.configStart, cfgLen) != cfgBefore { + t.Error("CONFIG region changed during the grow") + } + if got := readPartitionFile(t, fx.path, p9Index, "/p9-marker.txt"); got != "persist content" { + t.Errorf("P9 data lost: marker = %q", got) + } +} + +// --- #4: a failure leaves the disk unmodified -------------------------------- + +// TestInsufficientSpaceIsAtomic verifies that a grow which cannot fit (no +// free space and no shrink partition) fails without modifying the disk, so a +// reboot inherits the original state, not a half-applied one. +func TestInsufficientSpaceIsAtomic(t *testing.T) { + if testing.Short() { + t.Skip("slow end-to-end resize test") + } + fx := buildSampleLayout(t) + before := wholeDiskSum(t, fx.path) + + // full disk, grows that need ~496 MB, no shrink partition supplied + err := Run(fx.path, nil, growImages, false, false, true) + if err == nil { + t.Fatal("expected an insufficient-space error, got nil") + } + if wholeDiskSum(t, fx.path) != before { + t.Error("disk was modified despite the planning failure (not atomic)") + } + t.Logf("aborted cleanly: %v", err) +} + +// --- #5: refuse to shrink below the filesystem's used size ------------------- + +// TestShrinkBelowUsedAborts verifies that asking to shrink P9 below its ext4 +// used size makes e2fsck/resize2fs refuse, and the resize aborts with the +// partition and its data untouched. +func TestShrinkBelowUsedAborts(t *testing.T) { + if testing.Short() { + t.Skip("slow end-to-end resize test") + } + fx := buildSampleLayout(t) + p9Before := partitionRegionSum(t, fx.path, fx.p9Start, int(defaultP9MB*MB)) + + // 16 MB is well below P9's used size; resize2fs must refuse + err := Run(fx.path, nil, []PartitionChange{NewPartitionChange(IdentifierByLabel, "P9", 16*MB)}, false, false, false) + if err == nil { + t.Fatal("expected resize2fs to refuse shrinking below used size, got nil") + } + after := gptByName(t, fx.path) + if got := int64(after["P9"].GetSize()); got != defaultP9MB*MB { + t.Errorf("P9 size changed to %d despite the failed shrink (want %d)", got, defaultP9MB*MB) + } + if partitionRegionSum(t, fx.path, fx.p9Start, int(defaultP9MB*MB)) != p9Before { + t.Error("P9 region changed despite the failed shrink") + } + if got := readPartitionFile(t, fx.path, p9Index, "/p9-marker.txt"); got != "persist content" { + t.Errorf("P9 data lost: marker = %q", got) + } + t.Logf("aborted cleanly: %v", err) +} + +// --- #6: combined shrink + grow in a single Run ------------------------------ + +// TestCombinedShrinkGrow exercises the natural one-shot usage: a single +// Run that shrinks P9 to make room and grows ESP/IMGA/IMGB. go-diskfs rounds +// the shrink up to a whole GB, so this uses a larger P9 (1400 MB) than the +// default fixture. +func TestCombinedShrinkGrow(t *testing.T) { + if testing.Short() { + t.Skip("slow end-to-end resize test") + } + const p9MB = 1400 + const diskMB = p9MB + 252 // front partitions (~250 MB) + margin + fx := buildSampleLayoutSized(t, diskMB, p9MB) + + shrink := NewPartitionIdentifier(IdentifierByLabel, "P9") + if err := Run(fx.path, &shrink, growImages, false, false, true); err != nil { + t.Fatalf("combined shrink+grow Run: %v", err) + } + + after := gptByName(t, fx.path) + // P9 shrunk by the GB-rounded total grow (496 MB -> 1 GB) + if got := int64(after["P9"].GetSize()); got != (p9MB-1024)*MB { + t.Errorf("P9 size = %d, want %d", got, (p9MB-1024)*MB) + } + if after["ESP"].Index != espIndex || after["IMGA"].Index != imgaIndex || after["IMGB"].Index != imgbIndex { + t.Errorf("indices not preserved: ESP=%d IMGA=%d IMGB=%d", after["ESP"].Index, after["IMGA"].Index, after["IMGB"].Index) + } + if after["IMGA"].Start <= after["P9"].Start { + t.Errorf("IMGA (%d) not relocated past P9 (%d)", after["IMGA"].Start, after["P9"].Start) + } + if int64(after["ESP"].GetSize()) != 96*MB || int64(after["IMGA"].GetSize()) != 200*MB || int64(after["IMGB"].GetSize()) != 200*MB { + t.Errorf("grown sizes wrong: ESP=%d IMGA=%d IMGB=%d", after["ESP"].GetSize(), after["IMGA"].GetSize(), after["IMGB"].GetSize()) + } + if got := partitionRegionSum(t, fx.path, after["IMGA"].Start, 1*MB); got != fx.imgaSum { + t.Error("IMGA content not preserved") + } +} + +// --- #7: realistic P9 content survives the shrink ---------------------------- + +// TestShrinkPreservesP9Content fills P9 with many files (so resize2fs has +// real data to relocate during the shrink), shrinks it, then verifies every +// file is intact. +func TestShrinkPreservesP9Content(t *testing.T) { + if testing.Short() { + t.Skip("slow end-to-end resize test") + } + fx := buildSampleLayout(t) + want := populateP9(t, fx.path, 30) + + // shrink P9 from 900 MB to 200 MB (above used; forces block relocation) + if err := Run(fx.path, nil, []PartitionChange{NewPartitionChange(IdentifierByLabel, "P9", 200*MB)}, false, false, false); err != nil { + t.Fatalf("shrink P9: %v", err) + } + after := gptByName(t, fx.path) + if got := int64(after["P9"].GetSize()); got != 200*MB { + t.Errorf("P9 size = %d, want %d", got, 200*MB) + } + for name, content := range want { + if got := readPartitionFile(t, fx.path, p9Index, name); got != content { + t.Errorf("P9 file %s content mismatch after shrink (len got=%d want=%d)", name, len(got), len(content)) + } + } +} + +// populateP9 writes n distinct files into the P9 filesystem and returns their +// expected contents. +func populateP9(t *testing.T, path string, n int) map[string]string { + t.Helper() + f, err := os.OpenFile(path, os.O_RDWR, 0) + if err != nil { + t.Fatalf("open disk: %v", err) + } + defer func() { _ = f.Close() }() + d, err := diskfs.OpenBackend(file.New(f, false), diskfs.WithOpenMode(diskfs.ReadWrite)) + if err != nil { + t.Fatalf("open disk: %v", err) + } + fs, err := d.GetFilesystem(p9Index) + if err != nil { + t.Fatalf("get P9 filesystem: %v", err) + } + want := make(map[string]string, n) + for i := 0; i < n; i++ { + name := fmt.Sprintf("/p9file-%02d.dat", i) + // a few KB of deterministic, per-file content + content := fmt.Sprintf("file %d:", i) + for len(content) < 4096 { + content += fmt.Sprintf("%d-%x;", i, len(content)) + } + fh, err := fs.OpenFile(name, os.O_CREATE|os.O_RDWR) + if err != nil { + t.Fatalf("create %s: %v", name, err) + } + if _, err := fh.Write([]byte(content)); err != nil { + t.Fatalf("write %s: %v", name, err) + } + if err := fh.Close(); err != nil { + t.Fatalf("close %s: %v", name, err) + } + want[name] = content + } + if err := f.Sync(); err != nil { + t.Fatalf("sync: %v", err) + } + return want +} + +// --- #1 + #2: SIGKILL chaos -------------------------------------------------- + +// TestChaosKill runs the real resizer binary doing the sample-layout grow and SIGKILLs +// it at random moments, re-running until it completes. This exercises resume +// from arbitrary interruption points -- including partial GPT writes and +// half-copied partition content -- across repeated kills, and asserts that the +// final disk is correct and that persist/CONFIG are never corrupted. +// +// SIGKILL models a process crash (writes already issued survive); it is not a +// full power-loss model (no torn sectors / lost un-fsync'd writes). +func TestChaosKill(t *testing.T) { + if testing.Short() { + t.Skip("slow chaos test") + } + if _, err := exec.LookPath("mksquashfs"); err != nil { + t.Skip("mksquashfs not available") + } + // build the resizer binary + bin := filepath.Join(t.TempDir(), "resizer") + if out, err := exec.Command("go", "build", "-o", bin, "./cmd/resizer").CombinedOutput(); err != nil { + t.Fatalf("build resizer: %v\n%s", err, out) + } + + // base disk: P9 pre-shrunk so the grow has free space to work in + base := buildSampleLayout(t) + if err := Run(base.path, nil, shrinkP9, false, false, false); err != nil { + t.Fatalf("pre-shrink: %v", err) + } + p9Len := int((defaultP9MB - 600) * MB) + cfgLen := int(configMB * MB) + p9Sum := partitionRegionSum(t, base.path, base.p9Start, p9Len) + cfgSum := partitionRegionSum(t, base.path, base.configStart, cfgLen) + + growArgs := []string{ + "--preserve-numbers", + "--grow-partition", "label:ESP:96M", + "--grow-partition", "label:IMGA:200M", + "--grow-partition", "label:IMGB:200M", + } + rng := rand.New(rand.NewSource(1)) + const trials = 5 + for trial := 0; trial < trials; trial++ { + disk := filepath.Join(t.TempDir(), "disk.img") + if err := testCopyFile(base.path, disk); err != nil { + t.Fatalf("copy base disk: %v", err) + } + kills := runGrowWithRandomKills(t, bin, append(growArgs, disk), rng) + + after := gptByName(t, disk) + if after["ESP"].Index != espIndex || after["IMGA"].Index != imgaIndex || after["IMGB"].Index != imgbIndex { + t.Errorf("trial %d: indices not preserved", trial) + } + if int64(after["IMGA"].GetSize()) != 200*MB || int64(after["IMGB"].GetSize()) != 200*MB || int64(after["ESP"].GetSize()) != 96*MB { + t.Errorf("trial %d: grown sizes wrong", trial) + } + if partitionRegionSum(t, disk, after["IMGA"].Start, 1*MB) != base.imgaSum || + partitionRegionSum(t, disk, after["IMGB"].Start, 1*MB) != base.imgbSum { + t.Errorf("trial %d: IMG content not preserved", trial) + } + if partitionRegionSum(t, disk, base.p9Start, p9Len) != p9Sum { + t.Errorf("trial %d: P9 (persist) corrupted by interrupted resize", trial) + } + if partitionRegionSum(t, disk, base.configStart, cfgLen) != cfgSum { + t.Errorf("trial %d: CONFIG corrupted by interrupted resize", trial) + } + t.Logf("trial %d converged after %d kill(s)", trial, kills) + } +} + +// runGrowWithRandomKills spawns the resizer, SIGKILLs it after a random delay, +// and re-runs until it completes successfully; returns the number of kills. A +// non-zero exit that we did NOT cause is a real failure (resume must always be +// able to finish). +func runGrowWithRandomKills(t *testing.T, bin string, args []string, rng *rand.Rand) int { + t.Helper() + // SIGKILL a random number of times at varied points, then run once + // un-killed; that final run must complete (the idempotency assertion). + killsWanted := rng.Intn(4) + 1 + kills := 0 + for attempt := 0; attempt < 80; attempt++ { + cmd := exec.Command(bin, args...) + var stderr bytes.Buffer + cmd.Stderr = &stderr + if err := cmd.Start(); err != nil { + t.Fatalf("start resizer: %v", err) + } + done := make(chan error, 1) + go func() { done <- cmd.Wait() }() + + if kills >= killsWanted { + // guaranteed completion run + if werr := <-done; werr != nil { + t.Fatalf("resume run failed after %d kill(s) (resume not safe): %v\nstderr:\n%s", kills, werr, stderr.String()) + } + return kills + } + delay := time.Duration(rng.Intn(2500)+50) * time.Millisecond + select { + case werr := <-done: + if werr == nil { + return kills // finished before we killed it + } + t.Fatalf("resizer exited with error without being killed (resume not safe): %v\nstderr:\n%s", werr, stderr.String()) + case <-time.After(delay): + _ = cmd.Process.Kill() + <-done + kills++ + } + } + t.Fatalf("resize did not converge within the attempt cap") + return kills +} From 90419f58e6c76635b923dde288aa4ea6fc9cfe24 Mon Sep 17 00:00:00 2001 From: eriknordmark Date: Wed, 10 Jun 2026 23:31:28 +0200 Subject: [PATCH 04/13] test: chaos test does full two-step shrink+grow with kill recording TestChaosKill now drives the full shrink+grow pattern -- shrink P9 (Case 1) then grow ESP/IMGA/IMGB (Case 2) -- each through random SIGKILLs, so kills can land in every pipeline step (shrinkFilesystems/shrinkPartitions/createPartitions/ copyFilesystems/updatePartitions). It kills the whole process group (so a resize2fs child dies too) and points the resizer's TMPDIR at a scratch dir cleaned after each kill, so an interrupted shrink can't leak its temp copy. Seeds from CHAOS_SEED (else the clock) and logs KILL_STEP= per kill so a long run can show whether kills hit all intermediate points. Signed-off-by: eriknordmark Co-Authored-By: Claude Opus 4.8 --- resume_stress_test.go | 97 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 77 insertions(+), 20 deletions(-) diff --git a/resume_stress_test.go b/resume_stress_test.go index 71a1b9c..a5f0c49 100644 --- a/resume_stress_test.go +++ b/resume_stress_test.go @@ -9,6 +9,9 @@ import ( "os" "os/exec" "path/filepath" + "strconv" + "strings" + "syscall" "testing" "time" @@ -250,32 +253,51 @@ func TestChaosKill(t *testing.T) { t.Fatalf("build resizer: %v\n%s", err, out) } - // base disk: P9 pre-shrunk so the grow has free space to work in + // fresh base (NOT pre-shrunk): the chaos performs the full two-step + // resize -- shrink P9, then grow ESP/IMGA/IMGB -- so kills can land in the + // shrink steps (resize2fs) as well as create/copy/update. base := buildSampleLayout(t) - if err := Run(base.path, nil, shrinkP9, false, false, false); err != nil { - t.Fatalf("pre-shrink: %v", err) - } - p9Len := int((defaultP9MB - 600) * MB) cfgLen := int(configMB * MB) - p9Sum := partitionRegionSum(t, base.path, base.p9Start, p9Len) cfgSum := partitionRegionSum(t, base.path, base.configStart, cfgLen) - growArgs := []string{ + shrinkArgs := []string{"--grow-partition", "label:P9:300M"} // Case 1: shrink P9 in place + growArgs := []string{ // Case 2: grow into the freed space "--preserve-numbers", "--grow-partition", "label:ESP:96M", "--grow-partition", "label:IMGA:200M", "--grow-partition", "label:IMGB:200M", } - rng := rand.New(rand.NewSource(1)) + // Seed from CHAOS_SEED if set (so a failing run is reproducible), else from + // the clock so repeated runs explore different kill timings. + seed := time.Now().UnixNano() + if s := os.Getenv("CHAOS_SEED"); s != "" { + if v, err := strconv.ParseInt(s, 10, 64); err == nil { + seed = v + } + } + t.Logf("CHAOS_SEED=%d", seed) + rng := rand.New(rand.NewSource(seed)) const trials = 5 for trial := 0; trial < trials; trial++ { - disk := filepath.Join(t.TempDir(), "disk.img") + dir := t.TempDir() + disk := filepath.Join(dir, "disk.img") if err := testCopyFile(base.path, disk); err != nil { t.Fatalf("copy base disk: %v", err) } - kills := runGrowWithRandomKills(t, bin, append(growArgs, disk), rng) + scratch := filepath.Join(dir, "tmp") + if err := os.MkdirAll(scratch, 0o755); err != nil { + t.Fatalf("mkdir scratch: %v", err) + } + + // Phase 1: shrink P9, then Phase 2: grow -- each driven through random + // SIGKILLs and re-run to completion. + ks := runPhaseWithRandomKills(t, bin, scratch, append(append([]string{}, shrinkArgs...), disk), rng) + kg := runPhaseWithRandomKills(t, bin, scratch, append(append([]string{}, growArgs...), disk), rng) after := gptByName(t, disk) + if got := int64(after["P9"].GetSize()); got != (defaultP9MB-600)*MB { + t.Errorf("trial %d: P9 size = %d, want %d", trial, got, (defaultP9MB-600)*MB) + } if after["ESP"].Index != espIndex || after["IMGA"].Index != imgaIndex || after["IMGB"].Index != imgbIndex { t.Errorf("trial %d: indices not preserved", trial) } @@ -286,21 +308,23 @@ func TestChaosKill(t *testing.T) { partitionRegionSum(t, disk, after["IMGB"].Start, 1*MB) != base.imgbSum { t.Errorf("trial %d: IMG content not preserved", trial) } - if partitionRegionSum(t, disk, base.p9Start, p9Len) != p9Sum { - t.Errorf("trial %d: P9 (persist) corrupted by interrupted resize", trial) - } if partitionRegionSum(t, disk, base.configStart, cfgLen) != cfgSum { t.Errorf("trial %d: CONFIG corrupted by interrupted resize", trial) } - t.Logf("trial %d converged after %d kill(s)", trial, kills) + if got := readPartitionFile(t, disk, p9Index, "/p9-marker.txt"); got != "persist content" { + t.Errorf("trial %d: P9 data lost: marker = %q", trial, got) + } + t.Logf("trial %d converged: %d shrink-kill(s), %d grow-kill(s)", trial, ks, kg) } } -// runGrowWithRandomKills spawns the resizer, SIGKILLs it after a random delay, -// and re-runs until it completes successfully; returns the number of kills. A -// non-zero exit that we did NOT cause is a real failure (resume must always be -// able to finish). -func runGrowWithRandomKills(t *testing.T, bin string, args []string, rng *rand.Rand) int { +// runPhaseWithRandomKills spawns the resizer for one phase, SIGKILLs its whole +// process group after a random delay (so any resize2fs child dies too), and +// re-runs until it completes; returns the number of kills. TMPDIR is pointed at +// scratch and cleaned after each kill so a killed shrink can't leak its ~GB temp +// copy. A non-zero exit that we did NOT cause is a real failure (resume must +// always be able to finish). +func runPhaseWithRandomKills(t *testing.T, bin, scratch string, args []string, rng *rand.Rand) int { t.Helper() // SIGKILL a random number of times at varied points, then run once // un-killed; that final run must complete (the idempotency assertion). @@ -308,6 +332,9 @@ func runGrowWithRandomKills(t *testing.T, bin string, args []string, rng *rand.R kills := 0 for attempt := 0; attempt < 80; attempt++ { cmd := exec.Command(bin, args...) + cmd.Env = append(os.Environ(), "TMPDIR="+scratch) + // own process group, so a SIGKILL can take down any resize2fs child too + cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} var stderr bytes.Buffer cmd.Stderr = &stderr if err := cmd.Start(); err != nil { @@ -331,11 +358,41 @@ func runGrowWithRandomKills(t *testing.T, bin string, args []string, rng *rand.R } t.Fatalf("resizer exited with error without being killed (resume not safe): %v\nstderr:\n%s", werr, stderr.String()) case <-time.After(delay): - _ = cmd.Process.Kill() + // kill the whole process group (negative pid) so resize2fs dies too + _ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) <-done kills++ + // record how far the killed run got, so the long run can show + // whether kills land at every intermediate pipeline step + t.Logf("KILL_STEP=%s", chaosStepFromLog(stderr.String())) + // drop any partial resize2fs temp the killed run left behind + if matches, _ := filepath.Glob(filepath.Join(scratch, "partresizer-shrinkfs-*")); matches != nil { + for _, m := range matches { + _ = os.RemoveAll(m) + } + } } } t.Fatalf("resize did not converge within the attempt cap") return kills } + +// chaosStepFromLog reports the furthest pipeline step the resizer had reached, +// from its log output, so we can see where a SIGKILL interrupted it. Steps are +// checked in pipeline order; the last one present is the furthest reached. +func chaosStepFromLog(s string) string { + markers := []struct{ sub, step string }{ + {"Resizing filesystem on partition", "shrinkFilesystems"}, + {"Resizing partition", "shrinkPartitions"}, + {"creating new partition", "createPartitions"}, + {"copying data from", "copyFilesystems"}, + {"finalizing partition", "updatePartitions"}, + } + step := "preflight" + for _, m := range markers { + if strings.Contains(s, m.sub) { + step = m.step + } + } + return step +} From c5acac150d452cb72469e80979302532c175d565 Mon Sep 17 00:00:00 2001 From: eriknordmark Date: Thu, 11 Jun 2026 13:53:44 +0200 Subject: [PATCH 05/13] test: chaos GPT-write-delay hook to hit updatePartitions updatePartitions (and createPartitions) are single fast GPT-table writes, so random-timed SIGKILLs essentially never land inside them -- in 8h of chaos, updatePartitions was hit 0 times. Add an optional, build-tagged backend wrapper (`-tags chaos` + RESIZER_GPT_WRITE_DELAY, e.g. "5s") that sleeps after each write touching a GPT metadata sector (LBA 0..33 or the last 33 sectors), widening the window between go-diskfs's backup and primary GPT writes. run.go calls maybeWrapBackend (a no-op in normal builds, so production carries none of this). TestChaosKill builds with -tags chaos and widens its kill-timing window when CHAOS_GPT_DELAY is set. With this, kills reliably reach createPartitions/shrinkPartitions/ updatePartitions, and the resume still recovers cleanly from a kill inside updatePartitions' table write -- confirming the idempotent finalize plus go-diskfs's backup-first GPT write are crash-safe at the table-write level. Signed-off-by: eriknordmark Co-Authored-By: Claude Opus 4.8 --- delaybackend_chaos.go | 79 +++++++++++++++++++++++++++++++++++++++++ delaybackend_default.go | 10 ++++++ resume_stress_test.go | 29 ++++++++++++--- run.go | 7 ++-- 4 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 delaybackend_chaos.go create mode 100644 delaybackend_default.go diff --git a/delaybackend_chaos.go b/delaybackend_chaos.go new file mode 100644 index 0000000..bd0ae5a --- /dev/null +++ b/delaybackend_chaos.go @@ -0,0 +1,79 @@ +//go:build chaos + +package partitionresizer + +import ( + "os" + "time" + + "github.com/diskfs/go-diskfs/backend" +) + +// maybeWrapBackend, in -tags chaos builds, wraps the backend so that a write +// touching a GPT metadata sector is followed by a delay +// (RESIZER_GPT_WRITE_DELAY, e.g. "10s"). go-diskfs writes the table as several +// WriteAt calls (backup entries, backup header, primary entries, primary +// header, protective MBR); the delay widens the window between them so a +// crash-injection test can land between the backup and primary writes -- in +// particular inside updatePartitions, which is otherwise a single fast table +// write that random-timed kills almost never catch. +func maybeWrapBackend(b backend.Storage) backend.Storage { + s := os.Getenv("RESIZER_GPT_WRITE_DELAY") + if s == "" { + return b + } + d, err := time.ParseDuration(s) + if err != nil || d <= 0 { + return b + } + var sectors int64 + if fi, serr := b.Stat(); serr == nil { + sectors = fi.Size() / 512 + } + return &delayBackend{Storage: b, delay: d, diskSectors: sectors} +} + +type delayBackend struct { + backend.Storage + delay time.Duration + diskSectors int64 +} + +func (d *delayBackend) Writable() (backend.WritableFile, error) { + wf, err := d.Storage.Writable() + if err != nil { + return nil, err + } + return &delayWritable{WritableFile: wf, delay: d.delay, diskSectors: d.diskSectors}, nil +} + +type delayWritable struct { + backend.WritableFile + delay time.Duration + diskSectors int64 +} + +func (d *delayWritable) WriteAt(p []byte, off int64) (int, error) { + n, err := d.WritableFile.WriteAt(p, off) + if err == nil && isGPTWrite(off, len(p), d.diskSectors) { + time.Sleep(d.delay) + } + return n, err +} + +// isGPTWrite reports whether a write at byte offset off of the given length +// touches a GPT metadata sector (512-byte LBAs): the protective MBR + primary +// header + primary entry array (LBA 0..33), or the backup entry array + backup +// header (the last 33 sectors). +func isGPTWrite(off int64, length int, diskSectors int64) bool { + const sec = 512 + start := off / sec + end := (off + int64(length) - 1) / sec + if start <= 33 { // overlaps the primary region (LBA 0..33) + return true + } + if diskSectors > 0 && end >= diskSectors-33 { // overlaps the backup region + return true + } + return false +} diff --git a/delaybackend_default.go b/delaybackend_default.go new file mode 100644 index 0000000..1f4b574 --- /dev/null +++ b/delaybackend_default.go @@ -0,0 +1,10 @@ +//go:build !chaos + +package partitionresizer + +import "github.com/diskfs/go-diskfs/backend" + +// maybeWrapBackend returns the backend unchanged in normal builds. The delaying +// wrapper exists only in -tags chaos builds (see delaybackend_chaos.go), so +// production binaries carry none of that code. +func maybeWrapBackend(b backend.Storage) backend.Storage { return b } diff --git a/resume_stress_test.go b/resume_stress_test.go index a5f0c49..6e12b39 100644 --- a/resume_stress_test.go +++ b/resume_stress_test.go @@ -247,9 +247,19 @@ func TestChaosKill(t *testing.T) { if _, err := exec.LookPath("mksquashfs"); err != nil { t.Skip("mksquashfs not available") } - // build the resizer binary + // build the resizer binary. If CHAOS_GPT_DELAY is set, build with -tags + // chaos and have the resizer delay around GPT-sector writes, so kills land + // between the backup/primary GPT writes (e.g. inside updatePartitions, a + // single fast table write that random-timed kills otherwise never catch). bin := filepath.Join(t.TempDir(), "resizer") - if out, err := exec.Command("go", "build", "-o", bin, "./cmd/resizer").CombinedOutput(); err != nil { + buildArgs := []string{"build", "-o", bin} + gptDelay := os.Getenv("CHAOS_GPT_DELAY") + if gptDelay != "" { + buildArgs = append(buildArgs, "-tags", "chaos") + t.Setenv("RESIZER_GPT_WRITE_DELAY", gptDelay) + } + buildArgs = append(buildArgs, "./cmd/resizer") + if out, err := exec.Command("go", buildArgs...).CombinedOutput(); err != nil { t.Fatalf("build resizer: %v\n%s", err, out) } @@ -277,7 +287,10 @@ func TestChaosKill(t *testing.T) { } t.Logf("CHAOS_SEED=%d", seed) rng := rand.New(rand.NewSource(seed)) - const trials = 5 + trials := 5 + if gptDelay != "" { + trials = 2 // GPT-write delays make each trial much slower + } for trial := 0; trial < trials; trial++ { dir := t.TempDir() disk := filepath.Join(dir, "disk.img") @@ -350,7 +363,15 @@ func runPhaseWithRandomKills(t *testing.T, bin, scratch string, args []string, r } return kills } - delay := time.Duration(rng.Intn(2500)+50) * time.Millisecond + // Spread the kill across the operation. With GPT-write delays active the + // pipeline takes tens of seconds, so widen the range; otherwise early + // kills only ever land in the first (delayed) step and never reach + // updatePartitions. + maxKillMs := 2500 + if os.Getenv("CHAOS_GPT_DELAY") != "" { + maxKillMs = 60000 + } + delay := time.Duration(rng.Intn(maxKillMs)+50) * time.Millisecond select { case werr := <-done: if werr == nil { diff --git a/run.go b/run.go index c3d1550..fed1583 100644 --- a/run.go +++ b/run.go @@ -63,11 +63,14 @@ func Run(disk string, shrinkPartition *PartitionIdentifier, growPartitions []Par // now we have the desired disk, either passed explicitly or found by discovery - backend, err := file.OpenFromPath(disk, false) + backendFile, err := file.OpenFromPath(disk, false) if err != nil { return err } - d, err := diskfs.OpenBackend(backend) + // maybeWrapBackend is a no-op in normal builds; in -tags chaos builds it can + // inject delays around GPT-sector writes (see RESIZER_GPT_WRITE_DELAY) so + // crash-injection tests can land between the backup/primary GPT writes. + d, err := diskfs.OpenBackend(maybeWrapBackend(backendFile)) if err != nil { return err } From 0799e7c7a3703065706443e01405b22f7586dc9c Mon Sep 17 00:00:00 2001 From: eriknordmark Date: Thu, 11 Jun 2026 15:43:21 +0200 Subject: [PATCH 06/13] test: record GPT integrity after each chaos kill After a SIGKILL the chaos test now inspects the on-disk GPT before the next resume run heals it, logging whether the primary (LBA 1) and backup (last LBA) header self-CRCs are valid, whether each header's entry-array CRC matches, and whether the primary and backup entry arrays are byte-identical. Each table spans a header plus 32 entry sectors, so a kill mid-write can leave them disagreeing or an entry array half-written with a failing CRC; this captures exactly that for the steps that write the table -- in particular a kill inside updatePartitions. Signed-off-by: eriknordmark Co-Authored-By: Claude Opus 4.8 --- gpt_raw_test.go | 92 +++++++++++++++++++++++++++++++++++++++++++ resume_stress_test.go | 9 ++++- 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 gpt_raw_test.go diff --git a/gpt_raw_test.go b/gpt_raw_test.go new file mode 100644 index 0000000..783850f --- /dev/null +++ b/gpt_raw_test.go @@ -0,0 +1,92 @@ +package partitionresizer + +import ( + "bytes" + "encoding/binary" + "fmt" + "hash/crc32" + "io" + "os" +) + +// The helpers in this file parse GPT structures by reading raw disk sectors +// rather than going through go-diskfs. That is deliberate: the chaos tests +// inspect the disk immediately after a SIGKILL, when the GPT may be torn -- a +// half-written entry array whose CRC fails, or a backup that is newer than the +// primary. go-diskfs's gpt.Read validates the header and entry-array CRCs and +// silently falls back to the backup GPT when the primary fails +// (Table.RecoveredFromBackup), so it would heal away exactly the torn and +// primary/backup-divergent states these tests exist to observe. Reading the +// sectors ourselves is the only way to see them. Tests that operate on a +// known-good disk (gptByName, gptDump) use go-diskfs as normal. + +// readAt reads n bytes at off, returning nil on a short read or non-EOF error, +// so callers can treat a torn or truncated table as simply absent. +func readAt(f *os.File, off, n int64) []byte { + if off < 0 || n <= 0 { + return nil + } + b := make([]byte, n) + if _, e := f.ReadAt(b, off); e != nil && e != io.EOF { + return nil + } + return b +} + +// gptHeaderInfo parses a 512-byte GPT header sector and reports whether its +// signature and self-CRC are valid, plus the fields needed to locate and check +// its partition-entry array. A GPT header stores a CRC32 of itself (with the +// CRC field zeroed) and a separate CRC32 of the entry array. +func gptHeaderInfo(hdr []byte) (sigOK, crcOK bool, entriesCRC uint32, entriesLBA int64, num, esize uint32) { + if len(hdr) < 92 || string(hdr[0:8]) != "EFI PART" { + return false, false, 0, 0, 0, 0 + } + sigOK = true + hsize := binary.LittleEndian.Uint32(hdr[12:16]) + if hsize < 92 || int(hsize) > len(hdr) { + hsize = 92 + } + stored := binary.LittleEndian.Uint32(hdr[16:20]) + tmp := make([]byte, hsize) + copy(tmp, hdr[:hsize]) + binary.LittleEndian.PutUint32(tmp[16:20], 0) // header CRC is computed with this field zeroed + crcOK = crc32.ChecksumIEEE(tmp) == stored + entriesCRC = binary.LittleEndian.Uint32(hdr[88:92]) + entriesLBA = int64(binary.LittleEndian.Uint64(hdr[72:80])) + num = binary.LittleEndian.Uint32(hdr[80:84]) + esize = binary.LittleEndian.Uint32(hdr[84:88]) + return +} + +// gptIntegrity inspects the on-disk GPT of a 512-byte-sector image and returns a +// one-line summary: whether the primary (LBA 1) and backup (last LBA) header +// CRCs are valid, whether each header's entry-array CRC matches, and whether the +// primary and backup entry arrays are byte-identical. After a mid-updatePartitions +// kill this shows whether the kill left a CRC error or a primary/backup mismatch +// (each table spans multiple sectors: 1 header + 32 entry sectors). +func gptIntegrity(path string) string { + f, err := os.Open(path) + if err != nil { + return "gpt:open-error:" + err.Error() + } + defer func() { _ = f.Close() }() + fi, err := f.Stat() + if err != nil { + return "gpt:stat-error" + } + nSec := fi.Size() / 512 + pSig, pCRC, pECRC, pELBA, pN, pE := gptHeaderInfo(readAt(f, 1*512, 512)) // primary header at LBA 1 + bSig, bCRC, bECRC, bELBA, bN, bE := gptHeaderInfo(readAt(f, (nSec-1)*512, 512)) // backup header at last LBA + var pent, bent []byte + if pSig { + pent = readAt(f, pELBA*512, int64(pN)*int64(pE)) + } + if bSig { + bent = readAt(f, bELBA*512, int64(bN)*int64(bE)) + } + pEntCRCok := pent != nil && crc32.ChecksumIEEE(pent) == pECRC + bEntCRCok := bent != nil && crc32.ChecksumIEEE(bent) == bECRC + entriesMatch := pent != nil && bent != nil && bytes.Equal(pent, bent) + return fmt.Sprintf("gpt: primHdrCRC=%v backupHdrCRC=%v primEntCRC=%v backupEntCRC=%v entriesMatch=%v", + pCRC, bCRC, pEntCRCok, bEntCRCok, entriesMatch) +} diff --git a/resume_stress_test.go b/resume_stress_test.go index 6e12b39..ee1755a 100644 --- a/resume_stress_test.go +++ b/resume_stress_test.go @@ -385,7 +385,14 @@ func runPhaseWithRandomKills(t *testing.T, bin, scratch string, args []string, r kills++ // record how far the killed run got, so the long run can show // whether kills land at every intermediate pipeline step - t.Logf("KILL_STEP=%s", chaosStepFromLog(stderr.String())) + step := chaosStepFromLog(stderr.String()) + // Inspect the on-disk GPT immediately after the kill, before the next + // resume run heals it. A mid-updatePartitions kill can leave the + // primary and backup tables (each header + 32 entry sectors) + // disagreeing, or an entry array half-written so its CRC fails; this + // records exactly that. The disk image is the last positional arg. + diskPath := args[len(args)-1] + t.Logf("KILL_STEP=%s %s", step, gptIntegrity(diskPath)) // drop any partial resize2fs temp the killed run left behind if matches, _ := filepath.Glob(filepath.Join(scratch, "partresizer-shrinkfs-*")); matches != nil { for _, m := range matches { From 79439a73886ed6e6ac10602f1737713c20a6df07 Mon Sep 17 00:00:00 2001 From: eriknordmark Date: Thu, 11 Jun 2026 17:56:49 +0200 Subject: [PATCH 07/13] test: optional copy-dest state check after a kill Add copyDestState: after a kill, classify each grow target's data region against its source to distinguish "nothing written" from a partial or complete copy -- a certainty the KILL_STEP marker alone cannot give for a copyFilesystems interruption. Raw-copied (squashfs) targets compare head and tail bytes to the source; filesystem-copied (FAT32/ext4) targets are checked for the on-disk signature that CreateFilesystem writes before any file data. Gated behind CHAOS_COPY_STATE so it is off unless explicitly requested. Signed-off-by: eriknordmark Co-Authored-By: Claude Opus 4.8 --- copyprogress_test.go | 80 +++++++++++++++++++++++++++++++++++++++++++ gpt_raw_test.go | 48 ++++++++++++++++++++++++++ resume_stress_test.go | 6 ++++ 3 files changed, 134 insertions(+) create mode 100644 copyprogress_test.go diff --git a/copyprogress_test.go b/copyprogress_test.go new file mode 100644 index 0000000..05a3071 --- /dev/null +++ b/copyprogress_test.go @@ -0,0 +1,80 @@ +package partitionresizer + +import ( + "bytes" + "os" + "sort" + "strings" +) + +// copyDestState classifies each "