From 7a0c35674ca59fd96a5389ddebb3c7c14476d6a4 Mon Sep 17 00:00:00 2001 From: chrislu Date: Thu, 10 Feb 2022 20:46:53 -0800 Subject: [PATCH] clean up previously mounted folder --- go.mod | 7 +++++-- go.sum | 1 + weed/command/mount2_std.go | 7 +++++++ weed/mount/unmount/unmount.go | 6 ++++++ weed/mount/unmount/unmount_linux.go | 21 +++++++++++++++++++++ weed/mount/unmount/unmount_std.go | 18 ++++++++++++++++++ 6 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 weed/mount/unmount/unmount.go create mode 100644 weed/mount/unmount/unmount_linux.go create mode 100644 weed/mount/unmount/unmount_std.go diff --git a/go.mod b/go.mod index 62c09d8f2..31c7666a2 100644 --- a/go.mod +++ b/go.mod @@ -162,14 +162,17 @@ require ( modernc.org/token v1.0.0 // indirect ) -require github.com/fluent/fluent-logger-golang v1.8.0 +require ( + github.com/fluent/fluent-logger-golang v1.8.0 + github.com/hanwen/go-fuse v1.0.0 + github.com/hanwen/go-fuse/v2 v2.1.0 +) require ( cloud.google.com/go/kms v1.0.0 // indirect github.com/DataDog/zstd v1.3.6-0.20190409195224-796139022798 // indirect github.com/d4l3k/messagediff v1.2.1 // indirect github.com/gogo/protobuf v1.3.2 // indirect - github.com/hanwen/go-fuse/v2 v2.1.0 // indirect github.com/jcmturner/aescts/v2 v2.0.0 // indirect github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect github.com/jcmturner/goidentity/v6 v6.0.1 // indirect diff --git a/go.sum b/go.sum index b1db2fdbf..1054996bf 100644 --- a/go.sum +++ b/go.sum @@ -516,6 +516,7 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kurin/blazer v0.5.3 h1:SAgYv0TKU0kN/ETfO5ExjNAPyMt2FocO2s/UlCHfjAk= github.com/kurin/blazer v0.5.3/go.mod h1:4FCXMUWo9DllR2Do4TtBd377ezyAJ51vB5uTBjt0pGU= +github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348 h1:MtvEpTB6LX3vkb4ax0b5D2DHbNAUsen0Gx5wZoq3lV4= github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k= github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.10.0 h1:Zx5DJFEYQXio93kgXnQ09fXNiUKsqv4OUEu2UtGcB1E= diff --git a/weed/command/mount2_std.go b/weed/command/mount2_std.go index 795a7864c..60edb71b0 100644 --- a/weed/command/mount2_std.go +++ b/weed/command/mount2_std.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/mount" + "github.com/chrislusf/seaweedfs/weed/mount/unmount" "github.com/hanwen/go-fuse/v2/fs" "net/http" "os" @@ -43,6 +44,12 @@ func RunMount2(option *Mount2Options, umask os.FileMode) bool { opts := &fs.Options{} opts.Debug = true + + unmount.Unmount(*option.dir) + grace.OnInterrupt(func() { + unmount.Unmount(*option.dir) + }) + server, err := fs.Mount(*option.dir, &mount.WeedFS{}, opts) if err != nil { glog.Fatalf("Mount fail: %v", err) diff --git a/weed/mount/unmount/unmount.go b/weed/mount/unmount/unmount.go new file mode 100644 index 000000000..c481d8030 --- /dev/null +++ b/weed/mount/unmount/unmount.go @@ -0,0 +1,6 @@ +package unmount + +// Unmount tries to unmount the filesystem mounted at dir. +func Unmount(dir string) error { + return unmount(dir) +} diff --git a/weed/mount/unmount/unmount_linux.go b/weed/mount/unmount/unmount_linux.go new file mode 100644 index 000000000..e55d48f86 --- /dev/null +++ b/weed/mount/unmount/unmount_linux.go @@ -0,0 +1,21 @@ +package unmount + +import ( + "bytes" + "errors" + "os/exec" +) + +func unmount(dir string) error { + cmd := exec.Command("fusermount", "-u", dir) + output, err := cmd.CombinedOutput() + if err != nil { + if len(output) > 0 { + output = bytes.TrimRight(output, "\n") + msg := err.Error() + ": " + string(output) + err = errors.New(msg) + } + return err + } + return nil +} diff --git a/weed/mount/unmount/unmount_std.go b/weed/mount/unmount/unmount_std.go new file mode 100644 index 000000000..76267fb6a --- /dev/null +++ b/weed/mount/unmount/unmount_std.go @@ -0,0 +1,18 @@ +//go:build !linux +// +build !linux + +package unmount + +import ( + "os" + "syscall" +) + +func unmount(dir string) error { + err := syscall.Unmount(dir, 0) + if err != nil { + err = &os.PathError{Op: "unmount", Path: dir, Err: err} + return err + } + return nil +}