Skip to content

Commit

Permalink
add cvmfs filesystem implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
siscia committed Dec 17, 2019
1 parent 04dcb27 commit eee2b40
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ DOCKER_ARGS ?=
GO111MODULE_VALUE=off
PREFIX ?= out/

PLUGINS=stargzfs-linux-amd64.so
PLUGINS=stargzfs-linux-amd64.so cvmfs-linux-amd64.so
CMD=rsnapshotd

PLUGIN_BINARIES=$(addprefix $(PREFIX),$(PLUGINS))
Expand All @@ -37,6 +37,10 @@ FORCE:
stargzfs-linux-amd64.so: FORCE
GO111MODULE=$(GO111MODULE_VALUE) go build -buildmode=plugin -o $(PREFIX)$@ -v ./filesystems/stargz

cvmfs-linux-amd64.so: FORCE
GO111MODULE=$(GO111MODULE_VALUE) go build -buildmode=plugin -o $(PREFIX)$@ -v ./filesystems/cvmfs


rsnapshotd: FORCE
GO111MODULE=$(GO111MODULE_VALUE) go build -o $(PREFIX)$@ -v ./cmd/rsnapshotd

Expand Down
95 changes: 95 additions & 0 deletions filesystems/cvmfs/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// +build linux

package main

import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"syscall"

"github.com/containerd/containerd/log"
"github.com/containerd/containerd/plugin"
fsplugin "github.com/ktock/remote-snapshotter/filesystems"
)

func init() {
plugin.Register(&plugin.Registration{
Type: fsplugin.RemoteFileSystemPlugin,
Config: &Config{},
ID: "cvmfs",
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
config, ok := ic.Config.(*Config)
if !ok {
return nil, fmt.Errorf("invalid cvmfs-remote-snapshotter configuration")
}
ic.Meta.Exports["root"] = ic.Root

return NewFilesystem(ic.Root, config)
},
})
}

type filesystem struct {
repository string
mountedLayers map[string]string
}

type Config struct {
Repository string `toml:"repository" default:"unpacked.cern.ch"`
}

func NewFilesystem(root string, config *Config) (*filesystem, error) {
repository := config.Repository
if repository == "" {
repository = "unpacked.cern.ch"
}
return &filesystem{repository: repository, mountedLayers: make(map[string]string)}, nil
}

func (fs *filesystem) Mount(ctx context.Context, ref, digest, mountpoint string) error {
digest = strings.Split(digest, ":")[1]
firstTwo := digest[0:2]
path := filepath.Join("/", "cvmfs", fs.repository, ".layers", firstTwo, digest, "layerfs")
if _, err := os.Stat(path); os.IsNotExist(err) {
err = fmt.Errorf("layer %s not in the cvmfs repository", digest)
log.G(ctx).WithError(err).WithField("layer digest", digest).WithField("path", path).Debug("cvmfs: Layer not found")
return err
}
log.G(ctx).WithField("layer digest", digest).Debug("cvmfs: Layer present in CVMFS")
err := syscall.Mount(path, mountpoint, "", syscall.MS_BIND, "")
if err != nil {
log.G(ctx).WithError(err).WithField("layer digest", digest).WithField("mountpoint", mountpoint).Debug("cvmfs: Error in bind mounting the layer.")
return err
}
fs.mountedLayers[mountpoint] = path
return nil
}

func (fs *filesystem) Check(ctx context.Context, mountpoint string) error {
path, ok := fs.mountedLayers[mountpoint]
if !ok {
err := fmt.Errorf("Mountpoint: %s was not mounted", mountpoint)
log.G(ctx).WithError(err).WithField("mountpoint", mountpoint).Error("cvmfs: the requested mountpoint does not seem to be mounted")
return err
}

_, statErr := os.Stat(path)
if statErr == nil {
return nil
}
if statErr != nil {
if os.IsNotExist(statErr) {
err := fmt.Errorf("Layer from path: %s does not seems to be in the CVMFS repository")
log.G(ctx).WithError(err).WithField("mountpoint", mountpoint).WithField("layer path", path).Error("cvmfs: the mounted layer does not seem to exist.")
return err
} else {
err := fmt.Errorf("Error in stat-ing the layer: %s", statErr)
log.G(ctx).WithError(err).WithField("mountpoint", mountpoint).WithField("layer path", path).Error("cvmfs: unknow error in stating the file.")
return err
}
}
return statErr
}
1 change: 1 addition & 0 deletions script/demo/config.rsnapshotd.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
lru_max_entry = 5000
http_cache_type = "directory"
filesystem_cache_type = "directory"
[plugins.cvmfs]
1 change: 1 addition & 0 deletions script/demo/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ services:
- /tmp:exec,mode=777
volumes:
- /dev/fuse:/dev/fuse
- /cvmfs:/cvmfs:shared
- "${GOPATH}/src/github.com/ktock/remote-snapshotter:/go/src/github.com/ktock/remote-snapshotter:ro"
registry2:
image: registry:2
Expand Down

0 comments on commit eee2b40

Please sign in to comment.