Skip to content
Closed
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
45 changes: 45 additions & 0 deletions packages/orchestrator/pkg/nfsproxy/chroot/nfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import (
"errors"
"fmt"
"net"
"os"
"path/filepath"
"regexp"
"strings"
"sync"

"github.com/go-git/go-billy/v5"
Expand Down Expand Up @@ -101,6 +104,8 @@ func (h *NFSHandler) OnNetworkRelease(ctx context.Context, sbx *sandbox.Sandbox)
h.mu.Unlock()

for _, chroot := range chroots {
h.removeNFSOrphans(ctx, sbx.Runtime.SandboxID, chroot.Root())

err := chroot.Close()
if err != nil {
logger.L().Warn(ctx, "failed to close chroot",
Expand All @@ -114,6 +119,46 @@ func (h *NFSHandler) OnNetworkRelease(ctx context.Context, sbx *sandbox.Sandbox)
}
}

// removeNFSOrphans removes .nfs* files left by the NFS silly-rename mechanism.
// When an NFS client deletes a file that still has open fds, it renames the file
// to .nfsXXXX instead of removing it, and sends a REMOVE only after the last fd
// is closed. If the client VM is killed before closing its fds, the .nfs* file
// remains on the server permanently. This function cleans them up at sandbox teardown.
func (h *NFSHandler) removeNFSOrphans(ctx context.Context, sandboxID, dir string) {
entries, err := os.ReadDir(dir)
if err != nil {
if !os.IsNotExist(err) {
logger.L().Warn(ctx, "failed to read volume dir for NFS orphan cleanup",
logger.WithSandboxID(sandboxID),
zap.String("dir", dir),
zap.Error(err),
)
}

return
}

for _, entry := range entries {
if !strings.HasPrefix(entry.Name(), ".nfs") {
continue
}

path := filepath.Join(dir, entry.Name())
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
logger.L().Warn(ctx, "failed to remove NFS orphan file",
logger.WithSandboxID(sandboxID),
zap.String("path", path),
zap.Error(err),
)
} else {
logger.L().Info(ctx, "removed NFS orphan file",
logger.WithSandboxID(sandboxID),
zap.String("path", path),
)
}
}
}

func (h *NFSHandler) Mount(
ctx context.Context,
conn net.Conn,
Expand Down