2022-02-09 19:42:28 +03:00
|
|
|
/*
|
|
|
|
_____ _____ _____ ____ ______ _____ ------
|
|
|
|
| | | | | | | | | | | | |
|
|
|
|
| | | | | | | | | | | | |
|
|
|
|
| --- | | | | |-----| |---- | | |-----| |----- ------
|
|
|
|
| | | | | | | | | | | | |
|
|
|
|
| ____| |_____ | ____| | ____| | |_____| _____| |_____ |_____
|
|
|
|
|
|
|
|
|
|
|
|
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
|
|
|
|
|
2024-01-02 13:10:39 +03:00
|
|
|
Copyright © 2020-2024 Microsoft Corporation. All rights reserved.
|
2022-02-09 19:42:28 +03:00
|
|
|
Author : <blobfusedev@microsoft.com>
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2023-05-25 13:18:42 +03:00
|
|
|
"bytes"
|
2022-02-09 19:42:28 +03:00
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2022-07-27 09:49:36 +03:00
|
|
|
"github.com/Azure/azure-storage-fuse/v2/common"
|
2023-05-25 13:18:42 +03:00
|
|
|
"github.com/Azure/azure-storage-fuse/v2/common/log"
|
2022-07-27 09:49:36 +03:00
|
|
|
|
2022-02-09 19:42:28 +03:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var unmountCmd = &cobra.Command{
|
2022-04-16 02:35:57 +03:00
|
|
|
Use: "unmount <mount path>",
|
|
|
|
Short: "Unmount Blobfuse2",
|
|
|
|
Long: "Unmount Blobfuse2",
|
|
|
|
SuggestFor: []string{"unmount", "unmnt"},
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
FlagErrorHandling: cobra.ExitOnError,
|
2022-10-05 19:03:52 +03:00
|
|
|
RunE: func(_ *cobra.Command, args []string) error {
|
2022-02-09 19:42:28 +03:00
|
|
|
if strings.Contains(args[0], "*") {
|
|
|
|
mntPathPrefix := args[0]
|
|
|
|
|
2022-10-05 19:03:52 +03:00
|
|
|
lstMnt, _ := common.ListMountPoints()
|
2022-02-09 19:42:28 +03:00
|
|
|
for _, mntPath := range lstMnt {
|
2022-10-05 19:03:52 +03:00
|
|
|
match, _ := regexp.MatchString(mntPathPrefix, mntPath)
|
2022-02-09 19:42:28 +03:00
|
|
|
if match {
|
2022-09-08 23:09:32 +03:00
|
|
|
err := unmountBlobfuse2(mntPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to unmount %s [%s]", mntPath, err.Error())
|
|
|
|
}
|
2022-02-09 19:42:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-09-08 23:09:32 +03:00
|
|
|
err := unmountBlobfuse2(args[0])
|
|
|
|
if err != nil {
|
2023-08-01 13:53:54 +03:00
|
|
|
return err
|
2022-09-08 23:09:32 +03:00
|
|
|
}
|
2022-02-09 19:42:28 +03:00
|
|
|
}
|
2022-09-08 23:09:32 +03:00
|
|
|
|
2022-02-09 19:42:28 +03:00
|
|
|
return nil
|
|
|
|
},
|
2022-10-05 19:03:52 +03:00
|
|
|
ValidArgsFunction: func(_ *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
2022-02-09 19:42:28 +03:00
|
|
|
if toComplete == "" {
|
2022-10-05 19:03:52 +03:00
|
|
|
mntPts, _ := common.ListMountPoints()
|
2022-02-09 19:42:28 +03:00
|
|
|
return mntPts, cobra.ShellCompDirectiveNoFileComp
|
|
|
|
}
|
|
|
|
return nil, cobra.ShellCompDirectiveDefault
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempts to unmount the directory and returns true if the operation succeeded
|
2022-09-08 23:09:32 +03:00
|
|
|
func unmountBlobfuse2(mntPath string) error {
|
2023-08-01 13:53:54 +03:00
|
|
|
unmountCmd := []string{"fusermount3", "fusermount"}
|
|
|
|
|
2023-05-25 13:18:42 +03:00
|
|
|
var errb bytes.Buffer
|
2023-08-01 13:53:54 +03:00
|
|
|
var err error
|
|
|
|
for _, umntCmd := range unmountCmd {
|
|
|
|
cliOut := exec.Command(umntCmd, "-u", mntPath)
|
|
|
|
cliOut.Stderr = &errb
|
|
|
|
_, err = cliOut.Output()
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
fmt.Println("Successfully unmounted", mntPath)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(err.Error(), "executable file not found") {
|
|
|
|
log.Err("unmountBlobfuse2 : failed to unmount (%s : %s)", err.Error(), errb.String())
|
|
|
|
break
|
|
|
|
}
|
2022-02-09 19:42:28 +03:00
|
|
|
}
|
2023-08-01 13:53:54 +03:00
|
|
|
|
|
|
|
return fmt.Errorf("%s", errb.String()+" "+err.Error())
|
2022-02-09 19:42:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(unmountCmd)
|
|
|
|
unmountCmd.AddCommand(umntAllCmd)
|
|
|
|
}
|