//go:build linux || darwin || freebsd package command import ( "fmt" "net" "os" "runtime" "strings" "time" "github.com/seaweedfs/go-fuse/v2/fuse" "github.com/seaweedfs/seaweedfs/weed/glog" "github.com/seaweedfs/seaweedfs/weed/mount" ) // handoverSocketPath is where a mount listens for a successor: next to its // local gRPC socket, /tmp/seaweedfs-mount-.handover.sock by default. A // deployment that keeps the local socket where the next mount process can // reach it gets the same for this one. func handoverSocketPath(localSocket string) string { return strings.TrimSuffix(localSocket, ".sock") + ".handover.sock" } // handoverSupported tells whether this mount can be handed over, or take a // mount over: on Linux, and neither with -peer.enable, whose listen port has a // single owner, nor with -filer.tokenFile, as read tokens and chunk proofs do // not cross. Such a mount does not listen, so a new mount on its directory // replaces it as before. func handoverSupported(option *MountOptions) bool { if runtime.GOOS != "linux" { return false } if option.peerEnabled != nil && *option.peerEnabled { return false } return option.filerTokenFile == nil || *option.filerTokenFile == "" } // handoverGoTimeout is how long a taking-over process waits for go after it // said ready; the running mount answers at once. const handoverGoTimeout = 60 * time.Second // listenMountSocket binds the local gRPC socket. With handover enabled the // listener does not unlink the socket file when it is closed: after a handover // the successor binds the same path, and only the cleanup func, which the // caller skips once it handed over, removes it. func listenMountSocket(path string, handover bool) (net.Listener, func(), error) { if err := os.Remove(path); err != nil && !os.IsNotExist(err) { return nil, nil, fmt.Errorf("remove %s: %w", path, err) } listener, err := net.Listen("unix", path) if err != nil { return nil, nil, fmt.Errorf("listen on %s: %w", path, err) } if handover { listener.(*net.UnixListener).SetUnlinkOnClose(false) } return listener, mountSocketCleanup(path), nil } // takeOverMount takes the mount over from the running process: it receives // the FUSE connection and the state, builds the server, answers ready and // waits for go. Until go any failure ends the process and leaves the running // mount as it was; the caller binds its sockets after this returns. The // returned server already reads requests. func takeOverMount(wfs *mount.WFS, dir, handoverDir, socket string, opts *fuse.MountOptions) *fuse.Server { fail := func(format string, args ...any) { wfs.ClearCacheDir() reason := fmt.Sprintf(format, args...) if !mount.HandoverAnswers(socket) { // It answered the probe a moment ago: it has gone since. glog.Exitf("take over %s failed: %s. The weed mount that served it no longer answers; start this mount again to mount it anew", dir, reason) } glog.Exitf("take over %s failed, the running weed mount keeps serving it: %s. To mount it anew instead, stop that weed mount or unmount %s first", dir, reason, dir) } // What can fail in starting the background tasks is done before the // running mount pauses: after go there is no mount to give back. if err := wfs.PrepareBackgroundTasks(); err != nil { fail("prepare the background tasks: %v", err) } tk, err := wfs.BeginTakeover(mount.TakeoverOptions{SocketPath: socket, MountDir: handoverDir}) if err != nil { fail("%v", err) } server, err := fuse.NewServerFromDev(wfs, tk.Dev(), dir, opts) if err != nil { tk.Abort("build the FUSE server: " + err.Error()) fail("build the FUSE server: %v", err) } if err := tk.Ready(); err != nil { tk.Close() fail("answer ready: %v", err) } if err := tk.WaitGo(handoverGoTimeout); err != nil { tk.Close() fail("%v", err) } // From here the mount is this process's. if err := server.Start(); err != nil { glog.Exitf("take over %s: the running mount is gone and the FUSE server cannot start: %v", dir, err) } glog.V(0).Infof("handover: serving unix_ns=%d state_bytes=%d", time.Now().UnixNano(), tk.StateBytes) tk.Close() return server }