Skip to content

Commit

Permalink
Automatically unmount on termination
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaumgarten committed Jul 23, 2018
1 parent 9638921 commit 4121203
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
17 changes: 15 additions & 2 deletions afs/addfs.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package afs

import (
"errors"
"flag"
"os"
"os/exec"
"path/filepath"
"regexp"
"time"
Expand All @@ -17,6 +19,7 @@ type AddFS struct {
pathfs.FileSystem
opts AddFSOpts
mutableFilesRegexes []*regexp.Regexp
mountpoint string
}

type AddFSOpts struct {
Expand All @@ -30,6 +33,7 @@ func NewAddFS(source string, opts AddFSOpts) (*AddFS, error) {
pathfs.NewLoopbackFileSystem(source),
opts,
make([]*regexp.Regexp, len(opts.MutableFiles)),
"",
}
for i, v := range opts.MutableFiles {
var err error
Expand All @@ -41,7 +45,7 @@ func NewAddFS(source string, opts AddFSOpts) (*AddFS, error) {
return fs, nil
}

func (fs *AddFS) Run() error {
func (fs *AddFS) Mount(mountpoint string) error {
origAbs, _ := filepath.Abs(flag.Arg(0))
mOpts := &fuse.MountOptions{
Options: []string{"default_permissions"},
Expand All @@ -57,14 +61,23 @@ func (fs *AddFS) Run() error {
}
pathNode := pathfs.NewPathNodeFs(fs, &pathfs.PathNodeFsOptions{})
conn := nodefs.NewFileSystemConnector(pathNode.Root(), opts)
server, err := fuse.NewServer(conn.RawFS(), flag.Arg(1), mOpts)
server, err := fuse.NewServer(conn.RawFS(), mountpoint, mOpts)
if err != nil {
return err
}
fs.mountpoint = mountpoint
server.Serve()
return nil
}

func (fs *AddFS) Unmount() error {
if fs.mountpoint == "" {
return errors.New("cannot unmount before successfully mounting")
}
umcmd := exec.Command("fusermount", "-u", fs.mountpoint)
return umcmd.Run()
}

func (fs *AddFS) rootUserPermit(context *fuse.Context) bool {
return fs.opts.AllowRootMutation && context.Uid == 0
}
Expand Down
25 changes: 23 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"os"
"os/signal"
"syscall"

"github.com/dbaumgarten/addfs/afs"
Expand All @@ -21,6 +22,7 @@ Possible flags:

var allowRootMutation = flag.Bool("allowRootMutation", false, "Allow the root-user to mutate files and folders")
var ignoreWarnings = flag.Bool("ignoreWarnings", false, "Ignore all errors about file-ownership. ONLY USE IF YOU KNOW WHAT YOU ARE DOING!")
var keepMounted = flag.Bool("keepMounted", false, "Do not attempt to unmount on exiting")
var mutableFiles arrayFlags

func main() {
Expand All @@ -37,7 +39,7 @@ func main() {

if !*ignoreWarnings {
if os.Getegid() != 0 {
fmt.Println("WARNING!: Not running. Only directories that are write-accessible for the user can be mounted. This defeats addfs' write-protection. Aborting for safety-reasons.")
fmt.Println("WARNING!: Not running as root. Only directories that are write-accessible for the user can be mounted. This defeats addfs' write-protection. Aborting for safety-reasons.")
os.Exit(1)
}
err := checkSourceDirectory(flag.Arg(0))
Expand All @@ -56,14 +58,33 @@ func main() {
fmt.Println("Error: ", err)
os.Exit(1)
}

if !*keepMounted {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go unmountOnInterrupt(afs, c)
}

fmt.Println("Starting FUSE-Filesystem!")
err = afs.Run()
err = afs.Mount(flag.Arg(1))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func unmountOnInterrupt(afs *afs.AddFS, c chan os.Signal) {
for _ = range c {
fmt.Println("Unmounting...")
err := afs.Unmount()
if err != nil {
fmt.Println("Error when unmounting:", err)
os.Exit(1)
}
fmt.Println("Successfully unmounted!")
}
}

func checkSourceDirectory(path string) error {
stat, err := os.Stat(path)
if err != nil {
Expand Down

0 comments on commit 4121203

Please sign in to comment.