66 "os"
77 "path/filepath"
88 "strings"
9+ "sync"
910 "syscall"
1011
1112 "github.com/containerd/containerd/log"
@@ -22,23 +23,34 @@ const (
2223 targetImageLayersLabel = "containerd.io/snapshot/cri.image-layers"
2324)
2425
25-
2626type filesystem struct {
27- repository string
28- mountedLayers map [string ]string
27+ fsAbsoluteMountpoint string
28+ mountedLayers map [string ]string
29+ mountedLayersLock sync.Mutex
2930}
3031
3132type Config struct {
32- Repository string `toml:"repository" default:"unpacked.cern.ch"`
33+ Repository string `toml:"repository" default:"unpacked.cern.ch"`
34+ AbsoluteMountpoint string `toml:"absolute-mountpoint" default:""`
3335}
3436
3537func NewFilesystem (ctx context.Context , root string , config * Config ) (snapshot.FileSystem , error ) {
36- log .G (ctx ).WithField ("root" , root ).Warning ("New fs" )
37- repository := config .Repository
38- if repository == "" {
39- repository = "unpacked.cern.ch"
38+ var absolutePath string
39+ mountedLayersMap := make (map [string ]string )
40+ if config .AbsoluteMountpoint == "" {
41+ repository := config .Repository
42+ if repository == "" {
43+ repository = "unpacked.cern.ch"
44+ }
45+ absolutePath = filepath .Join ("/" , "cvmfs" , repository )
46+ } else {
47+ absolutePath = config .AbsoluteMountpoint
48+ }
49+ log .G (ctx ).WithField ("root" , root ).WithField ("absolutePath" , absolutePath ).Info ("Mounting new filesystem" )
50+ if _ , err := os .Stat (absolutePath ); err != nil {
51+ log .G (ctx ).WithField ("absolutePath" , absolutePath ).Warning ("Impossible to stat the absolute path, is the filesystem mounted properly? Error: " , err )
4052 }
41- return & filesystem {repository : repository , mountedLayers : make ( map [ string ] string ) }, nil
53+ return & filesystem {fsAbsoluteMountpoint : absolutePath , mountedLayers : mountedLayersMap }, nil
4254}
4355
4456func (fs * filesystem ) Mount (ctx context.Context , mountpoint string , labels map [string ]string ) error {
@@ -51,7 +63,7 @@ func (fs *filesystem) Mount(ctx context.Context, mountpoint string, labels map[s
5163 }
5264 digest = strings .Split (digest , ":" )[1 ]
5365 firstTwo := digest [0 :2 ]
54- path := filepath .Join ("/" , "cvmfs" , fs .repository , ".layers" , firstTwo , digest , "layerfs" )
66+ path := filepath .Join (fs .fsAbsoluteMountpoint , ".layers" , firstTwo , digest , "layerfs" )
5567 if _ , err := os .Stat (path ); os .IsNotExist (err ) {
5668 err = fmt .Errorf ("layer %s not in the cvmfs repository" , digest )
5769 log .G (ctx ).WithError (err ).WithField ("layer digest" , digest ).WithField ("path" , path ).Debug ("cvmfs: Layer not found" )
@@ -63,13 +75,17 @@ func (fs *filesystem) Mount(ctx context.Context, mountpoint string, labels map[s
6375 log .G (ctx ).WithError (err ).WithField ("layer digest" , digest ).WithField ("mountpoint" , mountpoint ).Debug ("cvmfs: Error in bind mounting the layer." )
6476 return err
6577 }
78+ fs .mountedLayersLock .Lock ()
79+ defer fs .mountedLayersLock .Unlock ()
6680 fs .mountedLayers [mountpoint ] = path
6781 return nil
6882}
6983
70- func (fs * filesystem ) Check (ctx context.Context , mountpoint string ) error {
84+ func (fs * filesystem ) Check (ctx context.Context , mountpoint string , labels map [ string ] string ) error {
7185 log .G (ctx ).WithField ("snapshotter" , "cvmfs" ).WithField ("mountpoint" , mountpoint ).Warning ("checking layer" )
86+ fs .mountedLayersLock .Lock ()
7287 path , ok := fs .mountedLayers [mountpoint ]
88+ fs .mountedLayersLock .Unlock ()
7389 if ! ok {
7490 err := fmt .Errorf ("Mountpoint: %s was not mounted" , mountpoint )
7591 log .G (ctx ).WithError (err ).WithField ("mountpoint" , mountpoint ).Error ("cvmfs: the requested mountpoint does not seem to be mounted" )
@@ -92,3 +108,18 @@ func (fs *filesystem) Check(ctx context.Context, mountpoint string) error {
92108 }
93109 return statErr
94110}
111+
112+ func (fs * filesystem ) Unmount (ctx context.Context , mountpoint string ) error {
113+ // maybe we lost track of something somehow, does not hurt to try to unmount the mountpoint anyway
114+
115+ fs .mountedLayersLock .Lock ()
116+ _ , ok := fs .mountedLayers [mountpoint ]
117+ delete (fs .mountedLayers , mountpoint )
118+ fs .mountedLayersLock .Unlock ()
119+
120+ if ! ok {
121+ err := fmt .Errorf ("Trying to unmount mountpoint that does not seems mounted: %s" , mountpoint )
122+ log .G (ctx ).WithError (err ).Error ("Layer does not seems mounted." )
123+ }
124+ return syscall .Unmount (mountpoint , syscall .MNT_FORCE )
125+ }
0 commit comments