@@ -508,7 +508,7 @@ func (m *Manager) CleanupSnapshots(retentionLimit int) ([]string, error) {
508508 busySnapshots := m .getBusySnapshotList (clonesOutput )
509509
510510 cleanupCmd := fmt .Sprintf (
511- "zfs list -t snapshot -H -o name -s %s -s creation -r %s | grep -v clone | head -n -%d %s" +
511+ "zfs list -t snapshot -H -o name -s %s -s creation -r %s | grep -v clone | grep _pre$ | head -n -%d %s" +
512512 "| xargs -n1 --no-run-if-empty zfs destroy -R " ,
513513 dataStateAtLabel , m .config .Pool .Name , retentionLimit , excludeBusySnapshots (busySnapshots ))
514514
@@ -527,10 +527,10 @@ func (m *Manager) CleanupSnapshots(retentionLimit int) ([]string, error) {
527527}
528528
529529func (m * Manager ) getBusySnapshotList (clonesOutput string ) []string {
530- systemClones , userClones := make (map [string ]string ), make ( map [ string ] struct {} )
531- branchingSnapshots := []string {}
530+ systemClones := make (map [string ]string )
531+ branchingSnapshotDatasets := []string {}
532532
533- userClonePrefix := m .config .Pool .Name + "/"
533+ systemDatasetPrefix := fmt . Sprintf ( "%s/%s/%s/clone_pre_" , m .config .Pool .Name , branching . BranchDir , branching . DefaultBranch )
534534
535535 for _ , line := range strings .Split (clonesOutput , "\n " ) {
536536 cloneLine := strings .FieldsFunc (line , unicode .IsSpace )
@@ -539,39 +539,32 @@ func (m *Manager) getBusySnapshotList(clonesOutput string) []string {
539539 continue
540540 }
541541
542- // Keep the user-defined snapshot and the snapshot it is based on.
543- if strings .HasPrefix (cloneLine [0 ], userClonePrefix + "branch" ) {
544- branchingSnapshots = append (branchingSnapshots , cloneLine [0 ], cloneLine [1 ])
545-
542+ // Make dataset-snapshot map for system snapshots.
543+ if strings .HasPrefix (cloneLine [0 ], systemDatasetPrefix ) {
544+ systemClones [cloneLine [0 ]] = cloneLine [1 ]
546545 continue
547546 }
548547
549- //nolint:lll
550- if cloneName , _ := strings .CutPrefix (cloneLine [0 ], userClonePrefix ); strings .HasPrefix (cloneLine [0 ], userClonePrefix ) && ! strings .Contains (cloneName , m .config .PreSnapshotSuffix ) {
551- origin := cloneLine [1 ]
552-
553- if idx := strings .Index (origin , "@" ); idx != - 1 {
554- origin = origin [:idx ]
548+ // Keep snapshots related to the user-defined datasets.
549+ if strings .HasPrefix (cloneLine [1 ], systemDatasetPrefix ) {
550+ systemDataset , _ , found := strings .Cut (cloneLine [1 ], "@" )
551+ if found {
552+ branchingSnapshotDatasets = append (branchingSnapshotDatasets , systemDataset )
555553 }
556554
557- userClones [origin ] = struct {}{}
558-
559555 continue
560556 }
561-
562- systemClones [cloneLine [0 ]] = cloneLine [1 ]
563557 }
564558
565- busySnapshots := make ([]string , 0 , len (userClones ))
559+ busySnapshots := make ([]string , 0 , len (branchingSnapshotDatasets ))
566560
567- for userClone := range userClones {
568- if systemClones [userClone ] != "" {
569- busySnapshots = append (busySnapshots , systemClones [userClone ])
561+ for _ , busyDataset := range branchingSnapshotDatasets {
562+ busySnapshot , ok := systemClones [busyDataset ]
563+ if ok {
564+ busySnapshots = append (busySnapshots , busySnapshot )
570565 }
571566 }
572567
573- busySnapshots = append (busySnapshots , branchingSnapshots ... )
574-
575568 return busySnapshots
576569}
577570
0 commit comments