feat: remove cephmig snapshots during sync and add cleanup command

Users of a subvolume should not see traces of the migration. Each sync
round now deletes the older cephmig-* round snapshots at source and
target and keeps only the newest one as base for the next round. The new
command `cephmig cleanup` deletes the remaining one after the cutover.
Application snapshots are never touched.

The journal marks a snapshot as removed before deleting it, so an
interrupted run finishes the deletion instead of reporting a vanished
snapshot.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-12-03 19:52:13 +01:00
1 parent cc2dff46a0
commit 907d400765
9 files changed
+322 -19

No files matched your search

+120
View File
@@ -0,0 +1,120 @@
package migration
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"slices"
)
// removeOwnedSnapshots deletes the completed snapshots that cephmig created
// itself, except keep, at source and target. Snapshots of the application
// are never touched.
//
// Every snapshot is first marked Removed in the journal and deleted after
// that, so an interrupted run finishes the deletion next time instead of
// reporting a vanished snapshot.
func removeOwnedSnapshots(ctx context.Context, src, dst ceph, statePath string, s *State, keep string) error {
marked := false
for i := range s.Snapshots {
snap := &s.Snapshots[i]
if snap.Owned && snap.Done && !snap.Removed && snap.Name != keep {
snap.Removed = true
marked = true
}
}
if marked {
if err := SaveState(statePath, s); err != nil {
return err
}
}
for _, side := range []ceph{dst, src} {
names, err := side.names(ctx)
if err != nil {
return err
}
for _, snap := range s.Snapshots {
if !snap.Removed || !slices.Contains(names, snap.Name) {
continue
}
if _, err = side.snap(ctx, "rm", snap.Name); err != nil {
return err
}
}
}
return nil
}
// Cleanup deletes all snapshots cephmig created, at source and target, after
// the job is ready for cutover. Afterwards the subvolumes hold only the
// snapshots of the application. This includes the final round snapshot, so
// run it only after the cutover has been checked.
func (e Engine) Cleanup(ctx context.Context, c Config, statePath string) error {
if err := c.Validate(); err != nil {
return err
}
if e.Exec == nil {
return errors.New("missing executor")
}
abs, err := filepath.Abs(statePath)
if err != nil {
return err
}
lock, err := lockFile(abs + ".lock")
if err != nil {
return err
}
defer lock.Close()
s, err := LoadState(abs)
if errors.Is(err, os.ErrNotExist) {
return errors.New("no migration state; nothing to clean up")
}
if err != nil {
return err
}
if s.Phase != "ready-for-cutover" {
return fmt.Errorf("cleanup requires phase ready-for-cutover, not %q; run finalize first", s.Phase)
}
inspection, err := e.Inspect(ctx, c)
if err != nil {
return err
}
if !sameMigrationIdentity(s.Config, c) || s.SourceFSID != inspection.SourceFSID || s.TargetFSID != inspection.TargetFSID ||
s.SourcePath != inspection.SourcePath || s.TargetPath != inspection.TargetPath {
return errors.New("config or cluster identity changed; refusing to use migration state")
}
claim, err := e.access().Claim(ctx, c, s.TargetFSID, s.ID)
if err != nil {
return err
}
defer claim.Close()
native := c.Backend == "native"
src, dst := ceph{endpoint: c.Source, exec: e.Exec, native: native}, ceph{endpoint: c.Target, exec: e.Exec, native: native}
s.LiveSnapshot = ""
if err = removeOwnedSnapshots(ctx, src, dst, abs, s, ""); err != nil {
return err
}
if err = SaveState(abs, s); err != nil {
return err
}
e.log("cleanup: removed cephmig snapshots at source and target; state=%s", abs)
return nil
}