Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion plugins/snapshots/erofs/erofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"path/filepath"
"runtime"
"strconv"
"strings"

"github.com/containerd/continuity/fs"
"github.com/containerd/errdefs"
Expand All @@ -39,6 +40,8 @@ import (
"github.com/containerd/containerd/v2/internal/userns"
)

const snapshotTempDirPrefix = "new-"

// SnapshotterConfig is used to configure the erofs snapshotter instance
type SnapshotterConfig struct {
// ovlOptions are the base options added to the overlayfs mount (defaults to [""])
Expand Down Expand Up @@ -293,7 +296,7 @@ func (s *snapshotter) lowerPath(id string) (string, error) {
}

func (s *snapshotter) prepareDirectory(ctx context.Context, snapshotDir string, kind snapshots.Kind, cacheBlob string) (string, error) {
td, err := os.MkdirTemp(snapshotDir, "new-")
td, err := os.MkdirTemp(snapshotDir, snapshotTempDirPrefix)
if err != nil {
return "", fmt.Errorf("failed to create temp dir: %w", err)
}
Expand Down Expand Up @@ -891,6 +894,12 @@ func (s *snapshotter) getCleanupDirectories(ctx context.Context) ([]string, erro

cleanup := []string{}
for _, d := range dirs {
// A new-* directory may belong to a concurrent snapshot creation. It is
// renamed to its metadata ID after the writer transaction is acquired, so
// Remove must not treat it as an orphan in the meantime.
if strings.HasPrefix(d, snapshotTempDirPrefix) {
continue
}
if _, ok := ids[d]; ok {
continue
}
Expand Down
28 changes: 28 additions & 0 deletions plugins/snapshots/erofs/erofs_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,34 @@ func TestErofsWithQuota(t *testing.T) {
testsuite.SnapshotterSuite(t, "erofs", newSnapshotter(t, WithDefaultSize(16*1024*1024)))
}

func TestGetCleanupDirectoriesSkipsSnapshotTempDirs(t *testing.T) {
ctx := context.Background()
root := t.TempDir()
snapshotDir := filepath.Join(root, "snapshots")
require.NoError(t, os.Mkdir(snapshotDir, 0700))

ms, err := storage.NewMetaStore(filepath.Join(root, "metadata.db"))
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, ms.Close()) })
s := &snapshotter{root: root, ms: ms}

_, err = os.MkdirTemp(snapshotDir, snapshotTempDirPrefix)
require.NoError(t, err)
orphanDir := filepath.Join(snapshotDir, "orphan")
require.NoError(t, os.Mkdir(orphanDir, 0700))
require.NoError(t, ms.WithTransaction(ctx, true, func(ctx context.Context) error {
_, err := storage.CreateSnapshot(ctx, snapshots.KindActive, "existing", "")
return err
}))

var cleanup []string
require.NoError(t, ms.WithTransaction(ctx, true, func(ctx context.Context) error {
cleanup, err = s.getCleanupDirectories(ctx)
return err
}))
assert.Equal(t, []string{orphanDir}, cleanup)
}

// TestWritableSize exercises the LabelSnapshotMaxSize override that the
// block-mode mkfs path passes to X-containerd.mkfs.size. Covers the
// happy path (label overrides default), fallback cases (missing, empty,
Expand Down
Loading