diff --git a/cmd/main.go b/cmd/main.go index 4d30e291009a2..1e9c30717ce6b 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -76,14 +76,17 @@ func Main(args []string) error { }, } - // Called via mount or fstab. - if strings.HasSuffix(args[0], "/mount.juicefs") { + if calledViaMount(args) { args = handleSysMountArgs(args) } return app.Run(reorderOptions(app, args)) } +func calledViaMount(args []string) bool { + return strings.HasSuffix(args[0], "/mount.juicefs") +} + func handleSysMountArgs(args []string) []string { optionToCmdFlag := map[string]string{ "attrcacheto": "attr-cache", diff --git a/cmd/mount.go b/cmd/mount.go index cf244accb5fce..db3d29dbb0a29 100644 --- a/cmd/mount.go +++ b/cmd/mount.go @@ -441,6 +441,24 @@ func tellFstabOptions(c *cli.Context) string { var fstab = "/etc/fstab" +func tryToInstallMountExec() error { + src, err := filepath.Abs(os.Args[0]) + if err != nil { + return err + } + err = os.Symlink(src, "/sbin/mount.juicefs") + if err != nil { + if strings.Contains(fmt.Sprint(err), "file exists") { + // safely continue if link already exists + return nil + } + if strings.Contains(fmt.Sprint(err), "permission denied") { + return err + } + } + return nil +} + func updateFstab(c *cli.Context) error { if runtime.GOOS != "linux" { logger.Infof("--update-fstab is ignored in %s", runtime.GOOS) @@ -450,6 +468,9 @@ func updateFstab(c *cli.Context) error { logger.Infoln("--update-fstab is ignored in container") return nil } + if calledViaMount(os.Args) { + return nil + } addr := expandPathForEmbedded(c.Args().Get(0)) mp := c.Args().Get(1) f, err := os.Open(fstab) @@ -535,6 +556,11 @@ func mount(c *cli.Context) error { logger.Infof("Data use %s", blob) if c.Bool("update-fstab") { + if !calledViaMount(os.Args) { + if err = tryToInstallMountExec(); err != nil { + return fmt.Errorf("error creating /sbin/mount.juicefs: %s", err) + } + } err = updateFstab(c) if err != nil { logger.Fatalf("failed to update fstab: %s", err) diff --git a/cmd/mount_unix.go b/cmd/mount_unix.go index d8664003ba61f..7fda912ceb3c6 100644 --- a/cmd/mount_unix.go +++ b/cmd/mount_unix.go @@ -138,7 +138,7 @@ func mount_flags() []cli.Flag { }, &cli.BoolFlag{ Name: "update-fstab", - Usage: "add / update entry in /etc/fstab", + Usage: "add / update entry in /etc/fstab, will create soft link /sbin/mount.juicefs if not exists", }, } return append(selfFlags, cacheFlags(1.0)...)