Skip to content

Commit

Permalink
Add lazy unmount option (#34)
Browse files Browse the repository at this point in the history
* Add lazy unmount option
* Use lazy unmount by default in GUI on Linux
  • Loading branch information
foodprocessor authored Nov 2, 2023
1 parent 569e602 commit f9e66ae
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 11 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,13 @@ The general format of the Cloudfuse Linux commands is `cloudfuse [command] [argu
* `mount all` - Mounts all the containers in an S3 Account or Azure account supported by mount
- Example: cloudfuse mount all \<mount path> --config-file=\<config file>
* `mount list` - Lists all Cloudfuse filesystems.
- cloudfuse mount list
- Example: cloudfuse mount list
* `unmount` - Unmounts the Cloudfuse filesystem.
- Example: cloudfuse unmount \<mount path>
- Add "--lazy" (or "-z") flag to use lazy unmount (prevents busy errors)
- Example: cloudfuse unmount --lazy \<mount path>
* `unmount all` - Unmounts all Cloudfuse filesystems.
- Example: cloudfuse unmount all
- Add "--lazy" (or "-z") flag to use lazy unmount (prevents busy errors)
- Example: cloudfuse unmount all --lazy

### Windows:

Expand Down
9 changes: 7 additions & 2 deletions cmd/unmount_all_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,24 @@ var umntAllCmd = &cobra.Command{
Long: "Unmount all instances of Cloudfuse. Only available on Linux",
SuggestFor: []string{"al", "all"},
FlagErrorHandling: cobra.ExitOnError,
RunE: func(_ *cobra.Command, _ []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
lstMnt, err := common.ListMountPoints()
if err != nil {
return fmt.Errorf("failed to list mount points [%s]", err.Error())
}

lazy, err := cmd.Flags().GetBool("lazy")
if err != nil {
lazy = false
}

mountfound := 0
unmounted := 0
errMsg := "failed to unmount - \n"

for _, mntPath := range lstMnt {
mountfound += 1
err := unmountCloudfuse(mntPath)
err := unmountCloudfuse(mntPath, lazy)
if err == nil {
unmounted += 1
} else {
Expand Down
20 changes: 15 additions & 5 deletions cmd/unmount_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,26 @@ var unmountCmd = &cobra.Command{
SuggestFor: []string{"unmount", "unmnt"},
Args: cobra.ExactArgs(1),
FlagErrorHandling: cobra.ExitOnError,
RunE: func(_ *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
lazy, err := cmd.Flags().GetBool("lazy")
if err != nil {
lazy = false
}
if strings.Contains(args[0], "*") {
mntPathPrefix := args[0]

lstMnt, _ := common.ListMountPoints()
for _, mntPath := range lstMnt {
match, _ := regexp.MatchString(mntPathPrefix, mntPath)
if match {
err := unmountCloudfuse(mntPath)
err := unmountCloudfuse(mntPath, lazy)
if err != nil {
return fmt.Errorf("failed to unmount %s [%s]", mntPath, err.Error())
}
}
}
} else {
err := unmountCloudfuse(args[0])
err := unmountCloudfuse(args[0], lazy)
if err != nil {
return err
}
Expand All @@ -80,13 +84,18 @@ var unmountCmd = &cobra.Command{
}

// Attempts to unmount the directory and returns true if the operation succeeded
func unmountCloudfuse(mntPath string) error {
func unmountCloudfuse(mntPath string, lazy bool) error {
unmountCmd := []string{"fusermount3", "fusermount"}

var errb bytes.Buffer
var err error
for _, umntCmd := range unmountCmd {
cliOut := exec.Command(umntCmd, "-u", mntPath)
var args []string
if lazy {
args = append(args, "-z")
}
args = append(args, "-u", mntPath)
cliOut := exec.Command(umntCmd, args...)
cliOut.Stderr = &errb
_, err = cliOut.Output()

Expand All @@ -106,5 +115,6 @@ func unmountCloudfuse(mntPath string) error {

func init() {
rootCmd.AddCommand(unmountCmd)
unmountCmd.PersistentFlags().BoolP("lazy", "z", false, "Use lazy unmount")
unmountCmd.AddCommand(umntAllCmd)
}
34 changes: 34 additions & 0 deletions cmd/unmount_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,40 @@ func (suite *unmountTestSuite) TestUnmountCmd() {
suite.assert.Nil(err)
}

func (suite *unmountTestSuite) TestUnmountCmdLazy() {
defer suite.cleanupTest()

lazyFlags := []string{"--lazy", "-z"}
flagBeforePath := false
flagAfterPath := !flagBeforePath
possibleFlagPositions := []bool{flagBeforePath, flagAfterPath}
baseCommand := "unmount"

for _, lazyFlag := range lazyFlags {
for _, flagPosition := range possibleFlagPositions {
mountDirectory1, _ := os.MkdirTemp("", "TestUnMountTemp")
os.MkdirAll(mountDirectory1, 0777)
defer os.RemoveAll(mountDirectory1)

cmd := exec.Command("../cloudfuse", "mount", mountDirectory1, fmt.Sprintf("--config-file=%s", confFileUnMntTest))
_, err := cmd.Output()
suite.assert.Nil(err)

time.Sleep(time.Second)

args := []string{baseCommand}
if flagPosition == flagBeforePath {
args = append(args, lazyFlag, mountDirectory1)
} else {
args = append(args, mountDirectory1, lazyFlag)
}

_, err = executeCommandC(rootCmd, args...)
suite.assert.Nil(err)
}
}
}

func (suite *unmountTestSuite) TestUnmountCmdFail() {
defer suite.cleanupTest()

Expand Down
2 changes: 1 addition & 1 deletion gui/mountPrimaryWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def unmountBucket(self):
else:
# Create the mount command to send to subprocess. If shell=True is set and the command is not in one string
# the subprocess will interpret the additional arguments as separate commands.
cmd = "./cloudfuse unmount " + directory
cmd = "./cloudfuse unmount --lazy " + directory
unmount = subprocess.run([cmd], shell=True, capture_output=True)

# Print to the text edit window the results of the unmount
Expand Down

0 comments on commit f9e66ae

Please sign in to comment.