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

Commit 9b40193

Browse files
authored
Merge pull request #1 from cvmfs/upgrade_fs
Upgrade the snapshotter to support also unmount
2 parents 972e533 + 41cf87a commit 9b40193

File tree

6 files changed

+756
-16
lines changed

6 files changed

+756
-16
lines changed

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@ all: cvmfs-snapshotter
44

55
cvmfs-snapshotter:
66
go build -o $(PREFIX)$@
7+
8+
9+
release: cvmfs-snapshotter
10+
mkdir -p release/usr/local/bin
11+
cp -r out/* release/usr/local/bin
12+
cp -r script/config/* release/

cvmfs/cvmfs.go

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
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-
2626
type filesystem struct {
27-
repository string
28-
mountedLayers map[string]string
27+
fsAbsoluteMountpoint string
28+
mountedLayers map[string]string
29+
mountedLayersLock sync.Mutex
2930
}
3031

3132
type 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

3537
func 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

4456
func (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+
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ go 1.14
44

55
require (
66
github.com/BurntSushi/toml v0.3.1
7-
github.com/containerd/containerd v1.3.1-0.20200507183255-990076b731ec
8-
github.com/containerd/stargz-snapshotter v0.0.0-20200609024515-95c85650d51d
7+
github.com/containerd/containerd v1.4.0
8+
github.com/containerd/stargz-snapshotter v0.0.0-20201007053350-9827a0b90aaa
99
github.com/sirupsen/logrus v1.6.0
1010
google.golang.org/grpc v1.29.1
1111
)

0 commit comments

Comments
 (0)