2017-11-01 19:37:53 -04:00
|
|
|
// +build linux freebsd
|
2015-05-14 18:56:00 -04:00
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package zfs // import "github.com/docker/docker/daemon/graphdriver/zfs"
|
2014-09-03 10:26:19 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-05-12 07:00:06 -04:00
|
|
|
"sync"
|
2014-09-03 10:26:19 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/daemon/graphdriver"
|
2017-08-03 20:22:00 -04:00
|
|
|
"github.com/docker/docker/pkg/containerfs"
|
2015-10-08 11:51:41 -04:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2015-04-16 07:06:44 -04:00
|
|
|
"github.com/docker/docker/pkg/mount"
|
2014-09-03 10:26:19 -04:00
|
|
|
"github.com/docker/docker/pkg/parsers"
|
|
|
|
zfs "github.com/mistifyio/go-zfs"
|
2017-04-18 09:26:36 -04:00
|
|
|
"github.com/opencontainers/selinux/go-selinux/label"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-05-23 10:22:32 -04:00
|
|
|
"golang.org/x/sys/unix"
|
2014-09-03 10:26:19 -04:00
|
|
|
)
|
|
|
|
|
2015-08-03 16:47:19 -04:00
|
|
|
type zfsOptions struct {
|
2014-09-03 10:26:19 -04:00
|
|
|
fsName string
|
|
|
|
mountPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
graphdriver.Register("zfs", Init)
|
|
|
|
}
|
|
|
|
|
2015-12-13 11:00:39 -05:00
|
|
|
// Logger returns a zfs logger implementation.
|
2014-09-03 10:26:19 -04:00
|
|
|
type Logger struct{}
|
|
|
|
|
2015-08-03 16:47:19 -04:00
|
|
|
// Log wraps log message from ZFS driver with a prefix '[zfs]'.
|
2014-09-03 10:26:19 -04:00
|
|
|
func (*Logger) Log(cmd []string) {
|
2015-07-28 22:58:50 -04:00
|
|
|
logrus.Debugf("[zfs] %s", strings.Join(cmd, " "))
|
2014-09-03 10:26:19 -04:00
|
|
|
}
|
|
|
|
|
2015-08-03 16:47:19 -04:00
|
|
|
// Init returns a new ZFS driver.
|
2016-05-07 21:36:10 -04:00
|
|
|
// It takes base mount path and an array of options which are represented as key value pairs.
|
2015-08-03 16:47:19 -04:00
|
|
|
// Each option is in the for key=value. 'zfs.fsname' is expected to be a valid key in the options.
|
2015-10-08 11:51:41 -04:00
|
|
|
func Init(base string, opt []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
|
2014-09-03 10:26:19 -04:00
|
|
|
var err error
|
2015-07-20 07:50:53 -04:00
|
|
|
|
|
|
|
if _, err := exec.LookPath("zfs"); err != nil {
|
2015-07-28 22:58:50 -04:00
|
|
|
logrus.Debugf("[zfs] zfs command is not available: %v", err)
|
2015-07-20 07:50:53 -04:00
|
|
|
return nil, graphdriver.ErrPrerequisites
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.OpenFile("/dev/zfs", os.O_RDWR, 600)
|
|
|
|
if err != nil {
|
2015-07-28 22:58:50 -04:00
|
|
|
logrus.Debugf("[zfs] cannot open /dev/zfs: %v", err)
|
2015-07-20 07:50:53 -04:00
|
|
|
return nil, graphdriver.ErrPrerequisites
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2014-09-03 10:26:19 -04:00
|
|
|
options, err := parseOptions(opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-13 08:21:46 -04:00
|
|
|
options.mountPath = base
|
2014-09-03 10:26:19 -04:00
|
|
|
|
|
|
|
rootdir := path.Dir(base)
|
|
|
|
|
|
|
|
if options.fsName == "" {
|
|
|
|
err = checkRootdirFs(rootdir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.fsName == "" {
|
|
|
|
options.fsName, err = lookupZfsDataset(rootdir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-13 08:21:46 -04:00
|
|
|
zfs.SetLogger(new(Logger))
|
2014-09-03 10:26:19 -04:00
|
|
|
|
2015-04-17 18:14:47 -04:00
|
|
|
filesystems, err := zfs.Filesystems(options.fsName)
|
2014-09-03 10:26:19 -04:00
|
|
|
if err != nil {
|
2015-04-17 18:14:47 -04:00
|
|
|
return nil, fmt.Errorf("Cannot find root filesystem %s: %v", options.fsName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
filesystemsCache := make(map[string]bool, len(filesystems))
|
|
|
|
var rootDataset *zfs.Dataset
|
|
|
|
for _, fs := range filesystems {
|
|
|
|
if fs.Name == options.fsName {
|
|
|
|
rootDataset = fs
|
|
|
|
}
|
|
|
|
filesystemsCache[fs.Name] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if rootDataset == nil {
|
2015-06-09 17:26:07 -04:00
|
|
|
return nil, fmt.Errorf("BUG: zfs get all -t filesystem -rHp '%s' should contain '%s'", options.fsName, options.fsName)
|
2014-09-03 10:26:19 -04:00
|
|
|
}
|
|
|
|
|
2016-07-18 00:11:00 -04:00
|
|
|
rootUID, rootGID, err := idtools.GetRootUIDGID(uidMaps, gidMaps)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to get root uid/guid: %v", err)
|
|
|
|
}
|
2017-11-18 18:52:47 -05:00
|
|
|
if err := idtools.MkdirAllAndChown(base, 0700, idtools.IDPair{rootUID, rootGID}); err != nil {
|
2016-07-18 00:11:00 -04:00
|
|
|
return nil, fmt.Errorf("Failed to create '%s': %v", base, err)
|
|
|
|
}
|
|
|
|
|
2015-03-19 19:28:17 -04:00
|
|
|
d := &Driver{
|
2015-04-17 18:14:47 -04:00
|
|
|
dataset: rootDataset,
|
|
|
|
options: options,
|
|
|
|
filesystemsCache: filesystemsCache,
|
2015-10-08 11:51:41 -04:00
|
|
|
uidMaps: uidMaps,
|
|
|
|
gidMaps: gidMaps,
|
2016-05-17 16:20:28 -04:00
|
|
|
ctr: graphdriver.NewRefCounter(graphdriver.NewDefaultChecker()),
|
2015-03-19 19:28:17 -04:00
|
|
|
}
|
2015-10-08 11:51:41 -04:00
|
|
|
return graphdriver.NewNaiveDiffDriver(d, uidMaps, gidMaps), nil
|
2014-09-03 10:26:19 -04:00
|
|
|
}
|
|
|
|
|
2015-08-03 16:47:19 -04:00
|
|
|
func parseOptions(opt []string) (zfsOptions, error) {
|
|
|
|
var options zfsOptions
|
2014-09-03 10:26:19 -04:00
|
|
|
options.fsName = ""
|
|
|
|
for _, option := range opt {
|
|
|
|
key, val, err := parsers.ParseKeyValueOpt(option)
|
|
|
|
if err != nil {
|
|
|
|
return options, err
|
|
|
|
}
|
|
|
|
key = strings.ToLower(key)
|
|
|
|
switch key {
|
|
|
|
case "zfs.fsname":
|
|
|
|
options.fsName = val
|
|
|
|
default:
|
2015-04-13 08:21:46 -04:00
|
|
|
return options, fmt.Errorf("Unknown option %s", key)
|
2014-09-03 10:26:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return options, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func lookupZfsDataset(rootdir string) (string, error) {
|
2017-05-23 10:22:32 -04:00
|
|
|
var stat unix.Stat_t
|
|
|
|
if err := unix.Stat(rootdir, &stat); err != nil {
|
2014-09-03 10:26:19 -04:00
|
|
|
return "", fmt.Errorf("Failed to access '%s': %s", rootdir, err)
|
|
|
|
}
|
|
|
|
wantedDev := stat.Dev
|
|
|
|
|
2015-04-16 07:06:44 -04:00
|
|
|
mounts, err := mount.GetMounts()
|
2015-04-13 08:21:46 -04:00
|
|
|
if err != nil {
|
2015-04-16 07:06:44 -04:00
|
|
|
return "", err
|
2014-09-03 10:26:19 -04:00
|
|
|
}
|
2015-04-16 07:06:44 -04:00
|
|
|
for _, m := range mounts {
|
2017-05-23 10:22:32 -04:00
|
|
|
if err := unix.Stat(m.Mountpoint, &stat); err != nil {
|
2015-07-28 22:58:50 -04:00
|
|
|
logrus.Debugf("[zfs] failed to stat '%s' while scanning for zfs mount: %v", m.Mountpoint, err)
|
2014-09-03 10:26:19 -04:00
|
|
|
continue // may fail on fuse file systems
|
|
|
|
}
|
|
|
|
|
2015-04-16 07:06:44 -04:00
|
|
|
if stat.Dev == wantedDev && m.Fstype == "zfs" {
|
|
|
|
return m.Source, nil
|
2014-09-03 10:26:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-13 08:21:46 -04:00
|
|
|
return "", fmt.Errorf("Failed to find zfs dataset mounted on '%s' in /proc/mounts", rootdir)
|
2014-09-03 10:26:19 -04:00
|
|
|
}
|
|
|
|
|
2015-08-03 16:47:19 -04:00
|
|
|
// Driver holds information about the driver, such as zfs dataset, options and cache.
|
2014-09-03 10:26:19 -04:00
|
|
|
type Driver struct {
|
2015-04-17 18:14:47 -04:00
|
|
|
dataset *zfs.Dataset
|
2015-08-03 16:47:19 -04:00
|
|
|
options zfsOptions
|
2015-05-12 07:00:06 -04:00
|
|
|
sync.Mutex // protects filesystem cache against concurrent access
|
2015-04-17 18:14:47 -04:00
|
|
|
filesystemsCache map[string]bool
|
2015-10-08 11:51:41 -04:00
|
|
|
uidMaps []idtools.IDMap
|
|
|
|
gidMaps []idtools.IDMap
|
2016-04-18 19:41:29 -04:00
|
|
|
ctr *graphdriver.RefCounter
|
2014-09-03 10:26:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) String() string {
|
|
|
|
return "zfs"
|
|
|
|
}
|
|
|
|
|
2018-01-17 21:17:26 -05:00
|
|
|
// Cleanup is called on daemon shutdown, it is used to clean up any remaining mounts
|
2014-09-03 10:26:19 -04:00
|
|
|
func (d *Driver) Cleanup() error {
|
2018-01-17 21:17:26 -05:00
|
|
|
return mount.RecursiveUnmount(d.options.mountPath)
|
2014-09-03 10:26:19 -04:00
|
|
|
}
|
|
|
|
|
2015-08-03 16:47:19 -04:00
|
|
|
// Status returns information about the ZFS filesystem. It returns a two dimensional array of information
|
|
|
|
// such as pool name, dataset name, disk usage, parent quota and compression used.
|
|
|
|
// Currently it return 'Zpool', 'Zpool Health', 'Parent Dataset', 'Space Used By Parent',
|
|
|
|
// 'Space Available', 'Parent Quota' and 'Compression'.
|
2014-09-03 10:26:19 -04:00
|
|
|
func (d *Driver) Status() [][2]string {
|
|
|
|
parts := strings.Split(d.dataset.Name, "/")
|
|
|
|
pool, err := zfs.GetZpool(parts[0])
|
|
|
|
|
2015-04-13 08:21:46 -04:00
|
|
|
var poolName, poolHealth string
|
|
|
|
if err == nil {
|
|
|
|
poolName = pool.Name
|
|
|
|
poolHealth = pool.Health
|
|
|
|
} else {
|
|
|
|
poolName = fmt.Sprintf("error while getting pool information %v", err)
|
|
|
|
poolHealth = "not available"
|
2014-09-03 10:26:19 -04:00
|
|
|
}
|
2015-04-13 08:21:46 -04:00
|
|
|
|
|
|
|
quota := "no"
|
|
|
|
if d.dataset.Quota != 0 {
|
2014-09-03 10:26:19 -04:00
|
|
|
quota = strconv.FormatUint(d.dataset.Quota, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
return [][2]string{
|
2015-04-13 08:21:46 -04:00
|
|
|
{"Zpool", poolName},
|
|
|
|
{"Zpool Health", poolHealth},
|
2014-09-03 10:26:19 -04:00
|
|
|
{"Parent Dataset", d.dataset.Name},
|
|
|
|
{"Space Used By Parent", strconv.FormatUint(d.dataset.Used, 10)},
|
|
|
|
{"Space Available", strconv.FormatUint(d.dataset.Avail, 10)},
|
|
|
|
{"Parent Quota", quota},
|
|
|
|
{"Compression", d.dataset.Compression},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-11 21:11:41 -04:00
|
|
|
// GetMetadata returns image/container metadata related to graph driver
|
2015-06-15 14:05:10 -04:00
|
|
|
func (d *Driver) GetMetadata(id string) (map[string]string, error) {
|
2016-10-31 09:44:11 -04:00
|
|
|
return map[string]string{
|
|
|
|
"Mountpoint": d.mountPath(id),
|
|
|
|
"Dataset": d.zfsPath(id),
|
|
|
|
}, nil
|
2015-06-15 14:05:10 -04:00
|
|
|
}
|
|
|
|
|
2015-05-12 07:00:06 -04:00
|
|
|
func (d *Driver) cloneFilesystem(name, parentName string) error {
|
2014-09-03 10:26:19 -04:00
|
|
|
snapshotName := fmt.Sprintf("%d", time.Now().Nanosecond())
|
2015-04-16 08:05:43 -04:00
|
|
|
parentDataset := zfs.Dataset{Name: parentName}
|
2014-09-03 10:26:19 -04:00
|
|
|
snapshot, err := parentDataset.Snapshot(snapshotName /*recursive */, false)
|
2015-04-13 08:21:46 -04:00
|
|
|
if err != nil {
|
2014-09-03 10:26:19 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-16 08:05:43 -04:00
|
|
|
_, err = snapshot.Clone(name, map[string]string{"mountpoint": "legacy"})
|
2015-05-12 07:00:06 -04:00
|
|
|
if err == nil {
|
|
|
|
d.Lock()
|
|
|
|
d.filesystemsCache[name] = true
|
|
|
|
d.Unlock()
|
|
|
|
}
|
|
|
|
|
2014-09-03 10:26:19 -04:00
|
|
|
if err != nil {
|
|
|
|
snapshot.Destroy(zfs.DestroyDeferDeletion)
|
|
|
|
return err
|
|
|
|
}
|
2015-04-13 08:21:46 -04:00
|
|
|
return snapshot.Destroy(zfs.DestroyDeferDeletion)
|
2014-09-03 10:26:19 -04:00
|
|
|
}
|
|
|
|
|
2015-08-11 21:00:36 -04:00
|
|
|
func (d *Driver) zfsPath(id string) string {
|
2014-09-03 10:26:19 -04:00
|
|
|
return d.options.fsName + "/" + id
|
|
|
|
}
|
|
|
|
|
2015-08-11 21:00:36 -04:00
|
|
|
func (d *Driver) mountPath(id string) string {
|
2015-05-29 09:33:04 -04:00
|
|
|
return path.Join(d.options.mountPath, "graph", getMountpoint(id))
|
2015-04-16 08:05:43 -04:00
|
|
|
}
|
|
|
|
|
2016-02-18 20:24:59 -05:00
|
|
|
// CreateReadWrite creates a layer that is writable for use as a container
|
|
|
|
// file system.
|
2016-11-09 15:59:58 -05:00
|
|
|
func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error {
|
|
|
|
return d.Create(id, parent, opts)
|
2016-02-18 20:24:59 -05:00
|
|
|
}
|
|
|
|
|
2015-08-03 16:47:19 -04:00
|
|
|
// Create prepares the dataset and filesystem for the ZFS driver for the given id under the parent.
|
2016-11-09 15:59:58 -05:00
|
|
|
func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) error {
|
|
|
|
var storageOpt map[string]string
|
|
|
|
if opts != nil {
|
|
|
|
storageOpt = opts.StorageOpt
|
|
|
|
}
|
|
|
|
|
2016-04-11 11:51:40 -04:00
|
|
|
err := d.create(id, parent, storageOpt)
|
2014-09-03 10:26:19 -04:00
|
|
|
if err == nil {
|
2015-04-16 08:05:43 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if zfsError, ok := err.(*zfs.Error); ok {
|
|
|
|
if !strings.HasSuffix(zfsError.Stderr, "dataset already exists\n") {
|
2015-04-13 08:21:46 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-04-16 08:05:43 -04:00
|
|
|
// aborted build -> cleanup
|
2015-04-13 08:21:46 -04:00
|
|
|
} else {
|
|
|
|
return err
|
2014-09-03 10:26:19 -04:00
|
|
|
}
|
|
|
|
|
2015-08-11 21:00:36 -04:00
|
|
|
dataset := zfs.Dataset{Name: d.zfsPath(id)}
|
2015-04-16 08:05:43 -04:00
|
|
|
if err := dataset.Destroy(zfs.DestroyRecursiveClones); err != nil {
|
2014-09-03 10:26:19 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-04-16 08:05:43 -04:00
|
|
|
|
|
|
|
// retry
|
2016-04-11 11:51:40 -04:00
|
|
|
return d.create(id, parent, storageOpt)
|
2014-09-03 10:26:19 -04:00
|
|
|
}
|
|
|
|
|
2016-04-11 11:51:40 -04:00
|
|
|
func (d *Driver) create(id, parent string, storageOpt map[string]string) error {
|
2015-08-11 21:00:36 -04:00
|
|
|
name := d.zfsPath(id)
|
2016-04-11 11:51:40 -04:00
|
|
|
quota, err := parseStorageOpt(storageOpt)
|
2016-05-12 17:21:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-16 08:05:43 -04:00
|
|
|
if parent == "" {
|
|
|
|
mountoptions := map[string]string{"mountpoint": "legacy"}
|
2015-05-12 07:00:06 -04:00
|
|
|
fs, err := zfs.CreateFilesystem(name, mountoptions)
|
|
|
|
if err == nil {
|
2016-04-11 11:51:40 -04:00
|
|
|
err = setQuota(name, quota)
|
|
|
|
if err == nil {
|
|
|
|
d.Lock()
|
|
|
|
d.filesystemsCache[fs.Name] = true
|
|
|
|
d.Unlock()
|
|
|
|
}
|
2015-05-12 07:00:06 -04:00
|
|
|
}
|
2014-09-03 10:26:19 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-04-11 11:51:40 -04:00
|
|
|
err = d.cloneFilesystem(name, d.zfsPath(parent))
|
|
|
|
if err == nil {
|
|
|
|
err = setQuota(name, quota)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseStorageOpt(storageOpt map[string]string) (string, error) {
|
|
|
|
// Read size to change the disk quota per container
|
|
|
|
for k, v := range storageOpt {
|
|
|
|
key := strings.ToLower(k)
|
|
|
|
switch key {
|
|
|
|
case "size":
|
|
|
|
return v, nil
|
|
|
|
default:
|
|
|
|
return "0", fmt.Errorf("Unknown option %s", key)
|
2015-05-12 07:00:06 -04:00
|
|
|
}
|
2016-04-11 11:51:40 -04:00
|
|
|
}
|
|
|
|
return "0", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setQuota(name string, quota string) error {
|
|
|
|
if quota == "0" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
fs, err := zfs.GetDataset(name)
|
|
|
|
if err != nil {
|
2014-09-03 10:26:19 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-04-11 11:51:40 -04:00
|
|
|
return fs.SetProperty("quota", quota)
|
2015-04-16 08:05:43 -04:00
|
|
|
}
|
2014-09-03 10:26:19 -04:00
|
|
|
|
2015-08-03 16:47:19 -04:00
|
|
|
// Remove deletes the dataset, filesystem and the cache for the given id.
|
2015-04-16 08:05:43 -04:00
|
|
|
func (d *Driver) Remove(id string) error {
|
2015-08-11 21:00:36 -04:00
|
|
|
name := d.zfsPath(id)
|
2015-05-12 07:00:06 -04:00
|
|
|
dataset := zfs.Dataset{Name: name}
|
|
|
|
err := dataset.Destroy(zfs.DestroyRecursive)
|
|
|
|
if err == nil {
|
|
|
|
d.Lock()
|
|
|
|
delete(d.filesystemsCache, name)
|
|
|
|
d.Unlock()
|
|
|
|
}
|
|
|
|
return err
|
2014-09-03 10:26:19 -04:00
|
|
|
}
|
|
|
|
|
2015-08-03 16:47:19 -04:00
|
|
|
// Get returns the mountpoint for the given id after creating the target directories if necessary.
|
2017-12-01 15:36:18 -05:00
|
|
|
func (d *Driver) Get(id, mountLabel string) (_ containerfs.ContainerFS, retErr error) {
|
2015-08-11 21:00:36 -04:00
|
|
|
mountpoint := d.mountPath(id)
|
2016-05-02 18:44:20 -04:00
|
|
|
if count := d.ctr.Increment(mountpoint); count > 1 {
|
2017-08-03 20:22:00 -04:00
|
|
|
return containerfs.NewLocalContainerFS(mountpoint), nil
|
2016-04-18 19:41:29 -04:00
|
|
|
}
|
2017-12-01 15:36:18 -05:00
|
|
|
defer func() {
|
|
|
|
if retErr != nil {
|
|
|
|
if c := d.ctr.Decrement(mountpoint); c <= 0 {
|
|
|
|
if mntErr := unix.Unmount(mountpoint, 0); mntErr != nil {
|
|
|
|
logrus.Errorf("Error unmounting %v: %v", mountpoint, mntErr)
|
|
|
|
}
|
|
|
|
if rmErr := unix.Rmdir(mountpoint); rmErr != nil && !os.IsNotExist(rmErr) {
|
|
|
|
logrus.Debugf("Failed to remove %s: %v", id, rmErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2016-04-18 19:41:29 -04:00
|
|
|
|
2015-08-11 21:00:36 -04:00
|
|
|
filesystem := d.zfsPath(id)
|
2015-06-10 18:34:18 -04:00
|
|
|
options := label.FormatMountLabel("", mountLabel)
|
2015-07-28 22:58:50 -04:00
|
|
|
logrus.Debugf(`[zfs] mount("%s", "%s", "%s")`, filesystem, mountpoint, options)
|
2015-04-16 08:05:43 -04:00
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
|
|
|
|
if err != nil {
|
2017-08-03 20:22:00 -04:00
|
|
|
return nil, err
|
2015-10-08 11:51:41 -04:00
|
|
|
}
|
2015-04-16 08:05:43 -04:00
|
|
|
// Create the target directories if they don't exist
|
2017-11-18 18:52:47 -05:00
|
|
|
if err := idtools.MkdirAllAndChown(mountpoint, 0755, idtools.IDPair{rootUID, rootGID}); err != nil {
|
2017-08-03 20:22:00 -04:00
|
|
|
return nil, err
|
2014-09-03 10:26:19 -04:00
|
|
|
}
|
2015-04-16 08:05:43 -04:00
|
|
|
|
2016-02-05 14:37:33 -05:00
|
|
|
if err := mount.Mount(filesystem, mountpoint, "zfs", options); err != nil {
|
2017-08-03 20:22:00 -04:00
|
|
|
return nil, fmt.Errorf("error creating zfs mount of %s to %s: %v", filesystem, mountpoint, err)
|
2015-04-16 08:05:43 -04:00
|
|
|
}
|
2016-04-18 19:41:29 -04:00
|
|
|
|
2016-02-05 14:37:33 -05:00
|
|
|
// this could be our first mount after creation of the filesystem, and the root dir may still have root
|
|
|
|
// permissions instead of the remapped root uid:gid (if user namespaces are enabled):
|
|
|
|
if err := os.Chown(mountpoint, rootUID, rootGID); err != nil {
|
2017-08-03 20:22:00 -04:00
|
|
|
return nil, fmt.Errorf("error modifying zfs mountpoint (%s) directory ownership: %v", mountpoint, err)
|
2016-02-05 14:37:33 -05:00
|
|
|
}
|
2015-04-16 08:05:43 -04:00
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
return containerfs.NewLocalContainerFS(mountpoint), nil
|
2014-09-03 10:26:19 -04:00
|
|
|
}
|
|
|
|
|
2015-08-03 16:47:19 -04:00
|
|
|
// Put removes the existing mountpoint for the given id if it exists.
|
2014-09-03 10:26:19 -04:00
|
|
|
func (d *Driver) Put(id string) error {
|
2016-05-02 18:44:20 -04:00
|
|
|
mountpoint := d.mountPath(id)
|
|
|
|
if count := d.ctr.Decrement(mountpoint); count > 0 {
|
2016-04-18 19:41:29 -04:00
|
|
|
return nil
|
|
|
|
}
|
2016-02-11 01:00:54 -05:00
|
|
|
|
2016-03-09 16:23:04 -05:00
|
|
|
logrus.Debugf(`[zfs] unmount("%s")`, mountpoint)
|
2016-02-11 01:00:54 -05:00
|
|
|
|
2017-12-01 15:36:18 -05:00
|
|
|
if err := unix.Unmount(mountpoint, unix.MNT_DETACH); err != nil {
|
|
|
|
logrus.Warnf("Failed to unmount %s mount %s: %v", id, mountpoint, err)
|
2015-04-16 08:05:43 -04:00
|
|
|
}
|
2017-12-01 15:36:18 -05:00
|
|
|
if err := unix.Rmdir(mountpoint); err != nil && !os.IsNotExist(err) {
|
|
|
|
logrus.Debugf("Failed to remove %s mount point %s: %v", id, mountpoint, err)
|
|
|
|
}
|
|
|
|
|
2014-09-03 10:26:19 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-03 16:47:19 -04:00
|
|
|
// Exists checks to see if the cache entry exists for the given id.
|
2014-09-03 10:26:19 -04:00
|
|
|
func (d *Driver) Exists(id string) bool {
|
2016-03-09 16:23:04 -05:00
|
|
|
d.Lock()
|
|
|
|
defer d.Unlock()
|
2017-09-11 14:55:05 -04:00
|
|
|
return d.filesystemsCache[d.zfsPath(id)]
|
2014-09-03 10:26:19 -04:00
|
|
|
}
|