2015-05-14 18:56:00 -04:00
|
|
|
// +build linux
|
|
|
|
|
2014-09-03 10:26:19 -04:00
|
|
|
package zfs
|
|
|
|
|
|
|
|
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
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/daemon/graphdriver"
|
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"
|
2015-06-10 18:34:18 -04:00
|
|
|
"github.com/docker/libcontainer/label"
|
2014-09-03 10:26:19 -04:00
|
|
|
zfs "github.com/mistifyio/go-zfs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ZfsOptions struct {
|
|
|
|
fsName string
|
|
|
|
mountPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
graphdriver.Register("zfs", Init)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Logger struct{}
|
|
|
|
|
|
|
|
func (*Logger) Log(cmd []string) {
|
|
|
|
log.Debugf("[zfs] %s", strings.Join(cmd, " "))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Init(base string, opt []string) (graphdriver.Driver, error) {
|
|
|
|
var err error
|
|
|
|
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 _, err := exec.LookPath("zfs"); err != nil {
|
2015-04-13 08:21:46 -04:00
|
|
|
return nil, fmt.Errorf("zfs command is not available: %v", err)
|
2014-09-03 10:26:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.OpenFile("/dev/zfs", os.O_RDWR, 600)
|
|
|
|
if err != nil {
|
2015-04-13 08:21:46 -04:00
|
|
|
return nil, fmt.Errorf("cannot open /dev/zfs: %v", err)
|
2014-09-03 10:26:19 -04:00
|
|
|
}
|
2015-04-13 08:21:46 -04:00
|
|
|
defer file.Close()
|
2014-09-03 10:26:19 -04:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-03-19 19:28:17 -04:00
|
|
|
}
|
|
|
|
return graphdriver.NaiveDiffDriver(d), nil
|
2014-09-03 10:26:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseOptions(opt []string) (ZfsOptions, error) {
|
|
|
|
var options ZfsOptions
|
|
|
|
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) {
|
|
|
|
var stat syscall.Stat_t
|
|
|
|
if err := syscall.Stat(rootdir, &stat); err != nil {
|
|
|
|
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 {
|
|
|
|
if err := syscall.Stat(m.Mountpoint, &stat); err != nil {
|
|
|
|
log.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
|
|
|
}
|
|
|
|
|
|
|
|
type Driver struct {
|
2015-04-17 18:14:47 -04:00
|
|
|
dataset *zfs.Dataset
|
|
|
|
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
|
2014-09-03 10:26:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) String() string {
|
|
|
|
return "zfs"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) Cleanup() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-15 14:05:10 -04:00
|
|
|
func (d *Driver) GetMetadata(id string) (map[string]string, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) ZfsPath(id string) string {
|
|
|
|
return d.options.fsName + "/" + id
|
|
|
|
}
|
|
|
|
|
2015-04-16 08:05:43 -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
|
|
|
}
|
|
|
|
|
2014-09-03 10:26:19 -04:00
|
|
|
func (d *Driver) Create(id string, parent string) error {
|
2015-04-16 08:05:43 -04:00
|
|
|
err := d.create(id, parent)
|
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-04-16 08:05:43 -04:00
|
|
|
dataset := zfs.Dataset{Name: d.ZfsPath(id)}
|
|
|
|
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
|
|
|
|
return d.create(id, parent)
|
2014-09-03 10:26:19 -04:00
|
|
|
}
|
|
|
|
|
2015-04-16 08:05:43 -04:00
|
|
|
func (d *Driver) create(id, parent string) error {
|
|
|
|
name := d.ZfsPath(id)
|
|
|
|
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 {
|
|
|
|
d.Lock()
|
|
|
|
d.filesystemsCache[fs.Name] = true
|
|
|
|
d.Unlock()
|
|
|
|
}
|
2014-09-03 10:26:19 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-05-12 07:00:06 -04:00
|
|
|
return d.cloneFilesystem(name, d.ZfsPath(parent))
|
2015-04-16 08:05:43 -04:00
|
|
|
}
|
2014-09-03 10:26:19 -04:00
|
|
|
|
2015-04-16 08:05:43 -04:00
|
|
|
func (d *Driver) Remove(id string) error {
|
2015-05-12 07:00:06 -04:00
|
|
|
name := d.ZfsPath(id)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) Get(id, mountLabel string) (string, error) {
|
2015-04-16 08:05:43 -04:00
|
|
|
mountpoint := d.MountPath(id)
|
|
|
|
filesystem := d.ZfsPath(id)
|
2015-06-10 18:34:18 -04:00
|
|
|
options := label.FormatMountLabel("", mountLabel)
|
|
|
|
log.Debugf(`[zfs] mount("%s", "%s", "%s")`, filesystem, mountpoint, options)
|
2015-04-16 08:05:43 -04:00
|
|
|
|
|
|
|
// Create the target directories if they don't exist
|
|
|
|
if err := os.MkdirAll(mountpoint, 0755); err != nil && !os.IsExist(err) {
|
2014-09-03 10:26:19 -04:00
|
|
|
return "", err
|
|
|
|
}
|
2015-04-16 08:05:43 -04:00
|
|
|
|
2015-06-10 18:34:18 -04:00
|
|
|
err := mount.Mount(filesystem, mountpoint, "zfs", options)
|
2015-04-16 08:05:43 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error creating zfs mount of %s to %s: %v", filesystem, mountpoint, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return mountpoint, nil
|
2014-09-03 10:26:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) Put(id string) error {
|
2015-04-16 08:05:43 -04:00
|
|
|
mountpoint := d.MountPath(id)
|
|
|
|
log.Debugf(`[zfs] unmount("%s")`, mountpoint)
|
|
|
|
|
|
|
|
if err := mount.Unmount(mountpoint); err != nil {
|
|
|
|
return fmt.Errorf("error unmounting to %s: %v", mountpoint, err)
|
|
|
|
}
|
2014-09-03 10:26:19 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) Exists(id string) bool {
|
2015-04-17 18:14:47 -04:00
|
|
|
return d.filesystemsCache[d.ZfsPath(id)] == true
|
2014-09-03 10:26:19 -04:00
|
|
|
}
|