Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #4 from cvmfs/unmountall_when_exiting
Browse files Browse the repository at this point in the history
unmount on main defer
  • Loading branch information
siscia authored Jan 29, 2021
2 parents cf6d051 + 94dfaaa commit 04bb5fd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
5 changes: 4 additions & 1 deletion cvmfs/cvmfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,18 @@ func (fs *Filesystem) Unmount(ctx context.Context, mountpoint string) error {
return syscall.Unmount(mountpoint, syscall.MNT_FORCE)
}

func (fs *Filesystem) UnmountAll() {
func (fs *Filesystem) UnmountAll(ctx context.Context) {
log.G(ctx).Info("Unmounting all the layers")
m := make([]string, 0)
fs.mountedLayersLock.Lock()
for mountpoint := range fs.mountedLayers {
m = append(m, mountpoint)
delete(fs.mountedLayers, mountpoint)
}
fs.mountedLayersLock.Unlock()
log.G(ctx).WithField("layers", m).Info("Unmounting the layers")
for _, mountpoint := range m {
log.G(ctx).WithField("layer", mountpoint).Info("Unmounting the layer")
if err := syscall.Unmount(mountpoint, syscall.MNT_FORCE); err != nil {
log.G(context.TODO()).WithError(err).WithField("mountpoint", mountpoint).Error("Error in unmounting before to exit")
}
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func main() {

// Configure filesystem and snapshotter
fs, err := cvmfs.NewFilesystem(ctx, filepath.Join(*rootDir, "cvmfs"), config)
defer fs.(*cvmfs.Filesystem).UnmountAll(ctx)
if err != nil {
log.G(ctx).WithError(err).Fatalf("failed to configure filesystem")
}
Expand Down Expand Up @@ -120,13 +121,12 @@ func main() {
log.G(ctx).WithError(err).Fatalf("error on serving via socket %q", *address)
}
}()
waitForSIGINT(fs.(*cvmfs.Filesystem))
waitForSIGINT()
log.G(ctx).Info("Got SIGINT")
}

func waitForSIGINT(fs *cvmfs.Filesystem) {
func waitForSIGINT() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
fs.UnmountAll()
}

0 comments on commit 04bb5fd

Please sign in to comment.