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

Commit 06d64d6

Browse files
committed
unmount all when exiting form the container
1 parent 41cf87a commit 06d64d6

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

cvmfs/cvmfs.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const (
2323
targetImageLayersLabel = "containerd.io/snapshot/cri.image-layers"
2424
)
2525

26-
type filesystem struct {
26+
type Filesystem struct {
2727
fsAbsoluteMountpoint string
2828
mountedLayers map[string]string
2929
mountedLayersLock sync.Mutex
@@ -50,10 +50,10 @@ func NewFilesystem(ctx context.Context, root string, config *Config) (snapshot.F
5050
if _, err := os.Stat(absolutePath); err != nil {
5151
log.G(ctx).WithField("absolutePath", absolutePath).Warning("Impossible to stat the absolute path, is the filesystem mounted properly? Error: ", err)
5252
}
53-
return &filesystem{fsAbsoluteMountpoint: absolutePath, mountedLayers: mountedLayersMap}, nil
53+
return &Filesystem{fsAbsoluteMountpoint: absolutePath, mountedLayers: mountedLayersMap}, nil
5454
}
5555

56-
func (fs *filesystem) Mount(ctx context.Context, mountpoint string, labels map[string]string) error {
56+
func (fs *Filesystem) Mount(ctx context.Context, mountpoint string, labels map[string]string) error {
5757
log.G(ctx).Info("Mount layer from cvmfs")
5858
digest, ok := labels[targetDigestLabelCRI]
5959
if !ok {
@@ -81,7 +81,7 @@ func (fs *filesystem) Mount(ctx context.Context, mountpoint string, labels map[s
8181
return nil
8282
}
8383

84-
func (fs *filesystem) Check(ctx context.Context, mountpoint string, labels map[string]string) error {
84+
func (fs *Filesystem) Check(ctx context.Context, mountpoint string, labels map[string]string) error {
8585
log.G(ctx).WithField("snapshotter", "cvmfs").WithField("mountpoint", mountpoint).Warning("checking layer")
8686
fs.mountedLayersLock.Lock()
8787
path, ok := fs.mountedLayers[mountpoint]
@@ -109,7 +109,7 @@ func (fs *filesystem) Check(ctx context.Context, mountpoint string, labels map[s
109109
return statErr
110110
}
111111

112-
func (fs *filesystem) Unmount(ctx context.Context, mountpoint string) error {
112+
func (fs *Filesystem) Unmount(ctx context.Context, mountpoint string) error {
113113
// maybe we lost track of something somehow, does not hurt to try to unmount the mountpoint anyway
114114

115115
fs.mountedLayersLock.Lock()
@@ -123,3 +123,18 @@ func (fs *filesystem) Unmount(ctx context.Context, mountpoint string) error {
123123
}
124124
return syscall.Unmount(mountpoint, syscall.MNT_FORCE)
125125
}
126+
127+
func (fs *Filesystem) UnmountAll() {
128+
m := make([]string, 0)
129+
fs.mountedLayersLock.Lock()
130+
for mountpoint := range fs.mountedLayers {
131+
m = append(m, mountpoint)
132+
delete(fs.mountedLayers, mountpoint)
133+
}
134+
fs.mountedLayersLock.Unlock()
135+
for _, mountpoint := range m {
136+
if err := syscall.Unmount(mountpoint, syscall.MNT_FORCE); err != nil {
137+
log.G(context.TODO()).WithError(err).WithField("mountpoint", mountpoint).Error("Error in unmounting before to exit")
138+
}
139+
}
140+
}

main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,13 @@ func main() {
120120
log.G(ctx).WithError(err).Fatalf("error on serving via socket %q", *address)
121121
}
122122
}()
123-
waitForSIGINT()
123+
waitForSIGINT(fs.(*cvmfs.Filesystem))
124124
log.G(ctx).Info("Got SIGINT")
125125
}
126126

127-
func waitForSIGINT() {
127+
func waitForSIGINT(fs *cvmfs.Filesystem) {
128128
c := make(chan os.Signal, 1)
129129
signal.Notify(c, os.Interrupt)
130130
<-c
131+
fs.UnmountAll()
131132
}

0 commit comments

Comments
 (0)