Skip to content

Commit

Permalink
create soft link /sbin/mount.juicefs
Browse files Browse the repository at this point in the history
  • Loading branch information
timfeirg committed Aug 10, 2022
1 parent c573f29 commit 7c03731
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
7 changes: 5 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
26 changes: 26 additions & 0 deletions cmd/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/mount_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)...)
Expand Down

0 comments on commit 7c03731

Please sign in to comment.