2014-08-02 03:46:15 -04:00
|
|
|
// +build linux
|
2013-11-21 15:45:48 -05:00
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package btrfs // import "github.com/docker/docker/daemon/graphdriver/btrfs"
|
2013-11-21 15:45:48 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <dirent.h>
|
2014-02-01 23:40:51 -05:00
|
|
|
#include <btrfs/ioctl.h>
|
2015-08-05 03:10:40 -04:00
|
|
|
#include <btrfs/ctree.h>
|
2016-04-01 10:58:29 -04:00
|
|
|
|
|
|
|
static void set_name_btrfs_ioctl_vol_args_v2(struct btrfs_ioctl_vol_args_v2* btrfs_struct, const char* value) {
|
|
|
|
snprintf(btrfs_struct->name, BTRFS_SUBVOL_NAME_MAX, "%s", value);
|
|
|
|
}
|
2013-11-21 15:45:48 -05:00
|
|
|
*/
|
|
|
|
import "C"
|
2014-02-01 23:40:51 -05:00
|
|
|
|
2013-11-21 15:45:48 -05:00
|
|
|
import (
|
|
|
|
"fmt"
|
2017-05-24 21:00:02 -04:00
|
|
|
"io/ioutil"
|
2016-12-14 16:21:18 -05:00
|
|
|
"math"
|
2013-11-21 15:45:48 -05:00
|
|
|
"os"
|
|
|
|
"path"
|
2015-08-05 03:10:40 -04:00
|
|
|
"path/filepath"
|
2017-05-24 21:00:02 -04:00
|
|
|
"strconv"
|
2016-03-23 21:14:38 -04:00
|
|
|
"strings"
|
2016-12-14 16:21:18 -05:00
|
|
|
"sync"
|
2013-11-21 15:45:48 -05:00
|
|
|
"unsafe"
|
2014-06-05 15:50:53 -04:00
|
|
|
|
2014-07-24 18:19:50 -04:00
|
|
|
"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"
|
2018-10-12 02:37:28 -04:00
|
|
|
"github.com/docker/docker/pkg/mount"
|
2016-03-23 21:14:38 -04:00
|
|
|
"github.com/docker/docker/pkg/parsers"
|
2017-02-14 13:35:20 -05:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2016-03-23 21:14:38 -04:00
|
|
|
"github.com/docker/go-units"
|
2017-04-18 09:26:36 -04:00
|
|
|
"github.com/opencontainers/selinux/go-selinux/label"
|
2018-10-12 02:37:28 -04:00
|
|
|
"github.com/pkg/errors"
|
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"
|
2013-11-21 15:45:48 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
graphdriver.Register("btrfs", Init)
|
|
|
|
}
|
|
|
|
|
2016-03-23 21:14:38 -04:00
|
|
|
type btrfsOptions struct {
|
|
|
|
minSpace uint64
|
|
|
|
size uint64
|
|
|
|
}
|
|
|
|
|
2015-07-22 16:51:35 -04:00
|
|
|
// Init returns a new BTRFS driver.
|
|
|
|
// An error is returned if BTRFS is not supported.
|
2015-10-08 11:51:41 -04:00
|
|
|
func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
|
2013-11-21 15:45:48 -05:00
|
|
|
|
2017-12-04 17:45:26 -05:00
|
|
|
// Perform feature detection on /var/lib/docker/btrfs if it's an existing directory.
|
|
|
|
// This covers situations where /var/lib/docker/btrfs is a mount, and on a different
|
|
|
|
// filesystem than /var/lib/docker.
|
|
|
|
// If the path does not exist, fall back to using /var/lib/docker for feature detection.
|
|
|
|
testdir := home
|
|
|
|
if _, err := os.Stat(testdir); os.IsNotExist(err) {
|
|
|
|
testdir = filepath.Dir(testdir)
|
|
|
|
}
|
|
|
|
|
|
|
|
fsMagic, err := graphdriver.GetFSMagic(testdir)
|
2016-02-01 02:50:16 -05:00
|
|
|
if err != nil {
|
2013-11-21 15:45:48 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-02-01 02:50:16 -05:00
|
|
|
if fsMagic != graphdriver.FsMagicBtrfs {
|
2014-05-29 09:50:52 -04:00
|
|
|
return nil, graphdriver.ErrPrerequisites
|
2013-11-21 15:45:48 -05:00
|
|
|
}
|
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
rootUID, rootGID, err := idtools.GetRootUIDGID(uidMaps, gidMaps)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-16 01:20:33 -05:00
|
|
|
if err := idtools.MkdirAllAndChown(home, 0700, idtools.Identity{UID: rootUID, GID: rootGID}); err != nil {
|
2014-06-05 15:50:53 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-01-02 19:50:59 -05:00
|
|
|
opt, userDiskQuota, err := parseOptions(options)
|
2016-03-23 21:14:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-12 02:37:28 -04:00
|
|
|
// For some reason shared mount propagation between a container
|
|
|
|
// and the host does not work for btrfs, and a remedy is to bind
|
|
|
|
// mount graphdriver home to itself (even without changing the
|
|
|
|
// propagation mode).
|
|
|
|
err = mount.MakeMount(home)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to make %s a mount", home)
|
|
|
|
}
|
|
|
|
|
2014-09-10 23:30:52 -04:00
|
|
|
driver := &Driver{
|
2015-10-08 11:51:41 -04:00
|
|
|
home: home,
|
|
|
|
uidMaps: uidMaps,
|
|
|
|
gidMaps: gidMaps,
|
2016-03-23 21:14:38 -04:00
|
|
|
options: opt,
|
2014-09-10 23:30:52 -04:00
|
|
|
}
|
|
|
|
|
2017-01-02 19:50:59 -05:00
|
|
|
if userDiskQuota {
|
|
|
|
if err := driver.subvolEnableQuota(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
return graphdriver.NewNaiveDiffDriver(driver, uidMaps, gidMaps), nil
|
2013-11-21 15:45:48 -05:00
|
|
|
}
|
|
|
|
|
2017-01-02 19:50:59 -05:00
|
|
|
func parseOptions(opt []string) (btrfsOptions, bool, error) {
|
2016-03-23 21:14:38 -04:00
|
|
|
var options btrfsOptions
|
2017-01-02 19:50:59 -05:00
|
|
|
userDiskQuota := false
|
2016-03-23 21:14:38 -04:00
|
|
|
for _, option := range opt {
|
|
|
|
key, val, err := parsers.ParseKeyValueOpt(option)
|
|
|
|
if err != nil {
|
2017-01-02 19:50:59 -05:00
|
|
|
return options, userDiskQuota, err
|
2016-03-23 21:14:38 -04:00
|
|
|
}
|
|
|
|
key = strings.ToLower(key)
|
|
|
|
switch key {
|
|
|
|
case "btrfs.min_space":
|
|
|
|
minSpace, err := units.RAMInBytes(val)
|
|
|
|
if err != nil {
|
2017-01-02 19:50:59 -05:00
|
|
|
return options, userDiskQuota, err
|
2016-03-23 21:14:38 -04:00
|
|
|
}
|
|
|
|
userDiskQuota = true
|
|
|
|
options.minSpace = uint64(minSpace)
|
|
|
|
default:
|
2017-01-02 19:50:59 -05:00
|
|
|
return options, userDiskQuota, fmt.Errorf("Unknown option %s", key)
|
2016-03-23 21:14:38 -04:00
|
|
|
}
|
|
|
|
}
|
2017-01-02 19:50:59 -05:00
|
|
|
return options, userDiskQuota, nil
|
2016-03-23 21:14:38 -04:00
|
|
|
}
|
|
|
|
|
2015-07-22 16:51:35 -04:00
|
|
|
// Driver contains information about the filesystem mounted.
|
2013-11-21 15:45:48 -05:00
|
|
|
type Driver struct {
|
2015-07-22 16:51:35 -04:00
|
|
|
//root of the file system
|
2017-01-02 19:50:59 -05:00
|
|
|
home string
|
|
|
|
uidMaps []idtools.IDMap
|
|
|
|
gidMaps []idtools.IDMap
|
|
|
|
options btrfsOptions
|
|
|
|
quotaEnabled bool
|
2016-12-14 16:21:18 -05:00
|
|
|
once sync.Once
|
2013-11-21 15:45:48 -05:00
|
|
|
}
|
|
|
|
|
2015-07-22 16:51:35 -04:00
|
|
|
// String prints the name of the driver (btrfs).
|
2013-11-21 15:45:48 -05:00
|
|
|
func (d *Driver) String() string {
|
|
|
|
return "btrfs"
|
|
|
|
}
|
|
|
|
|
2015-07-22 16:51:35 -04:00
|
|
|
// Status returns current driver information in a two dimensional string array.
|
|
|
|
// Output contains "Build Version" and "Library Version" of the btrfs libraries used.
|
|
|
|
// Version information can be used to check compatibility with your kernel.
|
2013-11-21 15:45:48 -05:00
|
|
|
func (d *Driver) Status() [][2]string {
|
2014-11-06 15:56:13 -05:00
|
|
|
status := [][2]string{}
|
2015-07-22 16:51:35 -04:00
|
|
|
if bv := btrfsBuildVersion(); bv != "-" {
|
2015-03-25 13:26:41 -04:00
|
|
|
status = append(status, [2]string{"Build Version", bv})
|
|
|
|
}
|
2015-07-22 16:51:35 -04:00
|
|
|
if lv := btrfsLibVersion(); lv != -1 {
|
2014-11-06 15:56:13 -05:00
|
|
|
status = append(status, [2]string{"Library Version", fmt.Sprintf("%d", lv)})
|
|
|
|
}
|
|
|
|
return status
|
2013-11-21 15:45:48 -05:00
|
|
|
}
|
|
|
|
|
2015-07-22 16:51:35 -04:00
|
|
|
// GetMetadata returns empty metadata for this driver.
|
2015-06-15 14:05:10 -04:00
|
|
|
func (d *Driver) GetMetadata(id string) (map[string]string, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-07-22 16:51:35 -04:00
|
|
|
// Cleanup unmounts the home directory.
|
2013-11-21 15:45:48 -05:00
|
|
|
func (d *Driver) Cleanup() error {
|
2018-10-12 02:37:28 -04:00
|
|
|
err := d.subvolDisableQuota()
|
|
|
|
umountErr := mount.Unmount(d.home)
|
|
|
|
|
|
|
|
// in case we have two errors, prefer the one from disableQuota()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if umountErr != nil {
|
2018-10-22 21:30:34 -04:00
|
|
|
return umountErr
|
2018-10-12 02:37:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2013-11-21 15:45:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func free(p *C.char) {
|
|
|
|
C.free(unsafe.Pointer(p))
|
|
|
|
}
|
|
|
|
|
|
|
|
func openDir(path string) (*C.DIR, error) {
|
|
|
|
Cpath := C.CString(path)
|
|
|
|
defer free(Cpath)
|
|
|
|
|
|
|
|
dir := C.opendir(Cpath)
|
|
|
|
if dir == nil {
|
|
|
|
return nil, fmt.Errorf("Can't open dir")
|
|
|
|
}
|
|
|
|
return dir, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func closeDir(dir *C.DIR) {
|
|
|
|
if dir != nil {
|
|
|
|
C.closedir(dir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDirFd(dir *C.DIR) uintptr {
|
|
|
|
return uintptr(C.dirfd(dir))
|
|
|
|
}
|
|
|
|
|
2014-04-17 19:47:27 -04:00
|
|
|
func subvolCreate(path, name string) error {
|
2013-11-21 15:45:48 -05:00
|
|
|
dir, err := openDir(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closeDir(dir)
|
|
|
|
|
|
|
|
var args C.struct_btrfs_ioctl_vol_args
|
|
|
|
for i, c := range []byte(name) {
|
|
|
|
args.name[i] = C.char(c)
|
|
|
|
}
|
|
|
|
|
2017-05-23 10:22:32 -04:00
|
|
|
_, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_SUBVOL_CREATE,
|
2013-11-21 15:45:48 -05:00
|
|
|
uintptr(unsafe.Pointer(&args)))
|
|
|
|
if errno != 0 {
|
2014-02-02 01:52:52 -05:00
|
|
|
return fmt.Errorf("Failed to create btrfs subvolume: %v", errno.Error())
|
2013-11-21 15:45:48 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func subvolSnapshot(src, dest, name string) error {
|
|
|
|
srcDir, err := openDir(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closeDir(srcDir)
|
|
|
|
|
|
|
|
destDir, err := openDir(dest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closeDir(destDir)
|
|
|
|
|
|
|
|
var args C.struct_btrfs_ioctl_vol_args_v2
|
|
|
|
args.fd = C.__s64(getDirFd(srcDir))
|
2016-04-01 10:58:29 -04:00
|
|
|
|
|
|
|
var cs = C.CString(name)
|
|
|
|
C.set_name_btrfs_ioctl_vol_args_v2(&args, cs)
|
|
|
|
C.free(unsafe.Pointer(cs))
|
2013-11-21 15:45:48 -05:00
|
|
|
|
2017-05-23 10:22:32 -04:00
|
|
|
_, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(destDir), C.BTRFS_IOC_SNAP_CREATE_V2,
|
2013-11-21 15:45:48 -05:00
|
|
|
uintptr(unsafe.Pointer(&args)))
|
|
|
|
if errno != 0 {
|
2014-02-02 01:52:52 -05:00
|
|
|
return fmt.Errorf("Failed to create btrfs snapshot: %v", errno.Error())
|
2013-11-21 15:45:48 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-24 19:18:01 -04:00
|
|
|
func isSubvolume(p string) (bool, error) {
|
2017-05-23 10:22:32 -04:00
|
|
|
var bufStat unix.Stat_t
|
|
|
|
if err := unix.Lstat(p, &bufStat); err != nil {
|
2015-08-24 19:18:01 -04:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// return true if it is a btrfs subvolume
|
|
|
|
return bufStat.Ino == C.BTRFS_FIRST_FREE_OBJECTID, nil
|
|
|
|
}
|
|
|
|
|
2016-12-14 16:21:18 -05:00
|
|
|
func subvolDelete(dirpath, name string, quotaEnabled bool) error {
|
2015-08-05 03:10:40 -04:00
|
|
|
dir, err := openDir(dirpath)
|
2013-11-21 15:45:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closeDir(dir)
|
2015-12-15 15:16:06 -05:00
|
|
|
fullPath := path.Join(dirpath, name)
|
2013-11-21 15:45:48 -05:00
|
|
|
|
|
|
|
var args C.struct_btrfs_ioctl_vol_args
|
2015-08-05 03:10:40 -04:00
|
|
|
|
2015-08-24 19:18:01 -04:00
|
|
|
// walk the btrfs subvolumes
|
|
|
|
walkSubvolumes := func(p string, f os.FileInfo, err error) error {
|
2015-12-15 15:16:06 -05:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) && p != fullPath {
|
|
|
|
// missing most likely because the path was a subvolume that got removed in the previous iteration
|
|
|
|
// since it's gone anyway, we don't care
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("error walking subvolumes: %v", err)
|
|
|
|
}
|
2015-08-24 19:18:01 -04:00
|
|
|
// we want to check children only so skip itself
|
|
|
|
// it will be removed after the filepath walk anyways
|
2015-12-15 15:16:06 -05:00
|
|
|
if f.IsDir() && p != fullPath {
|
2015-08-24 19:18:01 -04:00
|
|
|
sv, err := isSubvolume(p)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to test if %s is a btrfs subvolume: %v", p, err)
|
|
|
|
}
|
|
|
|
if sv {
|
2016-12-14 16:21:18 -05:00
|
|
|
if err := subvolDelete(path.Dir(p), f.Name(), quotaEnabled); err != nil {
|
2015-08-24 19:18:01 -04:00
|
|
|
return fmt.Errorf("Failed to destroy btrfs child subvolume (%s) of parent (%s): %v", p, dirpath, err)
|
2015-08-05 03:10:40 -04:00
|
|
|
}
|
|
|
|
}
|
2015-08-24 19:18:01 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := filepath.Walk(path.Join(dirpath, name), walkSubvolumes); err != nil {
|
|
|
|
return fmt.Errorf("Recursively walking subvolumes for %s failed: %v", dirpath, err)
|
|
|
|
}
|
2015-08-05 03:10:40 -04:00
|
|
|
|
2016-12-14 16:21:18 -05:00
|
|
|
if quotaEnabled {
|
|
|
|
if qgroupid, err := subvolLookupQgroup(fullPath); err == nil {
|
|
|
|
var args C.struct_btrfs_ioctl_qgroup_create_args
|
|
|
|
args.qgroupid = C.__u64(qgroupid)
|
|
|
|
|
2017-05-23 10:22:32 -04:00
|
|
|
_, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QGROUP_CREATE,
|
2016-12-14 16:21:18 -05:00
|
|
|
uintptr(unsafe.Pointer(&args)))
|
|
|
|
if errno != 0 {
|
2018-03-05 10:09:41 -05:00
|
|
|
logrus.WithField("storage-driver", "btrfs").Errorf("Failed to delete btrfs qgroup %v for %s: %v", qgroupid, fullPath, errno.Error())
|
2016-12-14 16:21:18 -05:00
|
|
|
}
|
|
|
|
} else {
|
2018-03-05 10:09:41 -05:00
|
|
|
logrus.WithField("storage-driver", "btrfs").Errorf("Failed to lookup btrfs qgroup for %s: %v", fullPath, err.Error())
|
2016-12-14 16:21:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-24 19:18:01 -04:00
|
|
|
// all subvolumes have been removed
|
|
|
|
// now remove the one originally passed in
|
2013-11-21 15:45:48 -05:00
|
|
|
for i, c := range []byte(name) {
|
|
|
|
args.name[i] = C.char(c)
|
|
|
|
}
|
2017-05-23 10:22:32 -04:00
|
|
|
_, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_SNAP_DESTROY,
|
2013-11-21 15:45:48 -05:00
|
|
|
uintptr(unsafe.Pointer(&args)))
|
|
|
|
if errno != 0 {
|
2015-08-24 19:18:01 -04:00
|
|
|
return fmt.Errorf("Failed to destroy btrfs snapshot %s for %s: %v", dirpath, name, errno.Error())
|
2013-11-21 15:45:48 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-14 16:21:18 -05:00
|
|
|
func (d *Driver) updateQuotaStatus() {
|
|
|
|
d.once.Do(func() {
|
|
|
|
if !d.quotaEnabled {
|
|
|
|
// In case quotaEnabled is not set, check qgroup and update quotaEnabled as needed
|
|
|
|
if err := subvolQgroupStatus(d.home); err != nil {
|
|
|
|
// quota is still not enabled
|
|
|
|
return
|
|
|
|
}
|
|
|
|
d.quotaEnabled = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-01-02 19:50:59 -05:00
|
|
|
func (d *Driver) subvolEnableQuota() error {
|
2016-12-14 16:21:18 -05:00
|
|
|
d.updateQuotaStatus()
|
|
|
|
|
2017-01-02 19:50:59 -05:00
|
|
|
if d.quotaEnabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, err := openDir(d.home)
|
2016-03-23 21:14:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closeDir(dir)
|
|
|
|
|
|
|
|
var args C.struct_btrfs_ioctl_quota_ctl_args
|
|
|
|
args.cmd = C.BTRFS_QUOTA_CTL_ENABLE
|
2017-05-23 10:22:32 -04:00
|
|
|
_, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QUOTA_CTL,
|
2016-03-23 21:14:38 -04:00
|
|
|
uintptr(unsafe.Pointer(&args)))
|
|
|
|
if errno != 0 {
|
|
|
|
return fmt.Errorf("Failed to enable btrfs quota for %s: %v", dir, errno.Error())
|
|
|
|
}
|
|
|
|
|
2017-01-02 19:50:59 -05:00
|
|
|
d.quotaEnabled = true
|
|
|
|
|
2016-03-23 21:14:38 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-02 19:50:59 -05:00
|
|
|
func (d *Driver) subvolDisableQuota() error {
|
2016-12-14 16:21:18 -05:00
|
|
|
d.updateQuotaStatus()
|
|
|
|
|
2017-01-02 19:50:59 -05:00
|
|
|
if !d.quotaEnabled {
|
2016-12-14 16:21:18 -05:00
|
|
|
return nil
|
2017-01-02 19:50:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
dir, err := openDir(d.home)
|
2016-03-23 21:14:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closeDir(dir)
|
|
|
|
|
|
|
|
var args C.struct_btrfs_ioctl_quota_ctl_args
|
|
|
|
args.cmd = C.BTRFS_QUOTA_CTL_DISABLE
|
2017-05-23 10:22:32 -04:00
|
|
|
_, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QUOTA_CTL,
|
2016-03-23 21:14:38 -04:00
|
|
|
uintptr(unsafe.Pointer(&args)))
|
|
|
|
if errno != 0 {
|
|
|
|
return fmt.Errorf("Failed to disable btrfs quota for %s: %v", dir, errno.Error())
|
|
|
|
}
|
|
|
|
|
2017-01-02 19:50:59 -05:00
|
|
|
d.quotaEnabled = false
|
|
|
|
|
2016-03-23 21:14:38 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-02 19:50:59 -05:00
|
|
|
func (d *Driver) subvolRescanQuota() error {
|
2016-12-14 16:21:18 -05:00
|
|
|
d.updateQuotaStatus()
|
|
|
|
|
2017-01-02 19:50:59 -05:00
|
|
|
if !d.quotaEnabled {
|
2016-12-14 16:21:18 -05:00
|
|
|
return nil
|
2017-01-02 19:50:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
dir, err := openDir(d.home)
|
2016-03-23 21:14:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closeDir(dir)
|
|
|
|
|
|
|
|
var args C.struct_btrfs_ioctl_quota_rescan_args
|
2017-05-23 10:22:32 -04:00
|
|
|
_, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QUOTA_RESCAN_WAIT,
|
2016-03-23 21:14:38 -04:00
|
|
|
uintptr(unsafe.Pointer(&args)))
|
|
|
|
if errno != 0 {
|
|
|
|
return fmt.Errorf("Failed to rescan btrfs quota for %s: %v", dir, errno.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func subvolLimitQgroup(path string, size uint64) error {
|
|
|
|
dir, err := openDir(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closeDir(dir)
|
|
|
|
|
|
|
|
var args C.struct_btrfs_ioctl_qgroup_limit_args
|
|
|
|
args.lim.max_referenced = C.__u64(size)
|
|
|
|
args.lim.flags = C.BTRFS_QGROUP_LIMIT_MAX_RFER
|
2017-05-23 10:22:32 -04:00
|
|
|
_, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QGROUP_LIMIT,
|
2016-03-23 21:14:38 -04:00
|
|
|
uintptr(unsafe.Pointer(&args)))
|
|
|
|
if errno != 0 {
|
|
|
|
return fmt.Errorf("Failed to limit qgroup for %s: %v", dir, errno.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-14 16:21:18 -05:00
|
|
|
// subvolQgroupStatus performs a BTRFS_IOC_TREE_SEARCH on the root path
|
|
|
|
// with search key of BTRFS_QGROUP_STATUS_KEY.
|
|
|
|
// In case qgroup is enabled, the retuned key type will match BTRFS_QGROUP_STATUS_KEY.
|
|
|
|
// For more details please see https://github.com/kdave/btrfs-progs/blob/v4.9/qgroup.c#L1035
|
|
|
|
func subvolQgroupStatus(path string) error {
|
|
|
|
dir, err := openDir(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closeDir(dir)
|
|
|
|
|
|
|
|
var args C.struct_btrfs_ioctl_search_args
|
|
|
|
args.key.tree_id = C.BTRFS_QUOTA_TREE_OBJECTID
|
|
|
|
args.key.min_type = C.BTRFS_QGROUP_STATUS_KEY
|
|
|
|
args.key.max_type = C.BTRFS_QGROUP_STATUS_KEY
|
|
|
|
args.key.max_objectid = C.__u64(math.MaxUint64)
|
|
|
|
args.key.max_offset = C.__u64(math.MaxUint64)
|
|
|
|
args.key.max_transid = C.__u64(math.MaxUint64)
|
|
|
|
args.key.nr_items = 4096
|
|
|
|
|
2017-05-23 10:22:32 -04:00
|
|
|
_, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_TREE_SEARCH,
|
2016-12-14 16:21:18 -05:00
|
|
|
uintptr(unsafe.Pointer(&args)))
|
|
|
|
if errno != 0 {
|
|
|
|
return fmt.Errorf("Failed to search qgroup for %s: %v", path, errno.Error())
|
|
|
|
}
|
|
|
|
sh := (*C.struct_btrfs_ioctl_search_header)(unsafe.Pointer(&args.buf))
|
|
|
|
if sh._type != C.BTRFS_QGROUP_STATUS_KEY {
|
|
|
|
return fmt.Errorf("Invalid qgroup search header type for %s: %v", path, sh._type)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-02 19:50:59 -05:00
|
|
|
func subvolLookupQgroup(path string) (uint64, error) {
|
|
|
|
dir, err := openDir(path)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer closeDir(dir)
|
|
|
|
|
|
|
|
var args C.struct_btrfs_ioctl_ino_lookup_args
|
|
|
|
args.objectid = C.BTRFS_FIRST_FREE_OBJECTID
|
|
|
|
|
2017-05-23 10:22:32 -04:00
|
|
|
_, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_INO_LOOKUP,
|
2017-01-02 19:50:59 -05:00
|
|
|
uintptr(unsafe.Pointer(&args)))
|
|
|
|
if errno != 0 {
|
|
|
|
return 0, fmt.Errorf("Failed to lookup qgroup for %s: %v", dir, errno.Error())
|
|
|
|
}
|
|
|
|
if args.treeid == 0 {
|
|
|
|
return 0, fmt.Errorf("Invalid qgroup id for %s: 0", dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
return uint64(args.treeid), nil
|
|
|
|
}
|
|
|
|
|
2013-11-21 15:45:48 -05:00
|
|
|
func (d *Driver) subvolumesDir() string {
|
|
|
|
return path.Join(d.home, "subvolumes")
|
|
|
|
}
|
|
|
|
|
2015-07-22 16:51:35 -04:00
|
|
|
func (d *Driver) subvolumesDirID(id string) string {
|
2013-11-21 15:45:48 -05:00
|
|
|
return path.Join(d.subvolumesDir(), id)
|
|
|
|
}
|
|
|
|
|
2017-05-24 21:00:02 -04:00
|
|
|
func (d *Driver) quotasDir() string {
|
|
|
|
return path.Join(d.home, "quotas")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) quotasDirID(id string) string {
|
|
|
|
return path.Join(d.quotasDir(), id)
|
|
|
|
}
|
|
|
|
|
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-07-22 16:51:35 -04:00
|
|
|
// Create the filesystem with given id.
|
2016-11-09 15:59:58 -05:00
|
|
|
func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) error {
|
2017-05-24 21:00:02 -04:00
|
|
|
quotas := path.Join(d.home, "quotas")
|
2013-11-21 15:45:48 -05:00
|
|
|
subvolumes := path.Join(d.home, "subvolumes")
|
2015-10-08 11:51:41 -04:00
|
|
|
rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-16 01:20:33 -05:00
|
|
|
if err := idtools.MkdirAllAndChown(subvolumes, 0700, idtools.Identity{UID: rootUID, GID: rootGID}); err != nil {
|
2013-11-21 15:45:48 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if parent == "" {
|
2014-04-17 19:47:27 -04:00
|
|
|
if err := subvolCreate(subvolumes, id); err != nil {
|
2013-11-21 15:45:48 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2016-02-04 13:25:24 -05:00
|
|
|
parentDir := d.subvolumesDirID(parent)
|
|
|
|
st, err := os.Stat(parentDir)
|
2013-11-21 15:45:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-04 13:25:24 -05:00
|
|
|
if !st.IsDir() {
|
2016-02-16 01:59:56 -05:00
|
|
|
return fmt.Errorf("%s: not a directory", parentDir)
|
2016-02-04 13:25:24 -05:00
|
|
|
}
|
2013-11-21 15:45:48 -05:00
|
|
|
if err := subvolSnapshot(parentDir, subvolumes, id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-10-28 09:19:51 -04:00
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
var storageOpt map[string]string
|
|
|
|
if opts != nil {
|
|
|
|
storageOpt = opts.StorageOpt
|
|
|
|
}
|
|
|
|
|
2016-03-23 21:14:38 -04:00
|
|
|
if _, ok := storageOpt["size"]; ok {
|
|
|
|
driver := &Driver{}
|
|
|
|
if err := d.parseStorageOpt(storageOpt, driver); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-24 21:00:02 -04:00
|
|
|
|
2016-03-23 21:14:38 -04:00
|
|
|
if err := d.setStorageSize(path.Join(subvolumes, id), driver); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-16 01:20:33 -05:00
|
|
|
if err := idtools.MkdirAllAndChown(quotas, 0700, idtools.Identity{UID: rootUID, GID: rootGID}); err != nil {
|
2017-05-24 21:00:02 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(path.Join(quotas, id), []byte(fmt.Sprint(driver.options.size)), 0644); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-23 21:14:38 -04:00
|
|
|
}
|
|
|
|
|
2016-01-07 23:05:28 -05:00
|
|
|
// if we have a remapped root (user namespaces enabled), change the created snapshot
|
|
|
|
// dir ownership to match
|
|
|
|
if rootUID != 0 || rootGID != 0 {
|
|
|
|
if err := os.Chown(path.Join(subvolumes, id), rootUID, rootGID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 15:59:58 -05:00
|
|
|
mountLabel := ""
|
|
|
|
if opts != nil {
|
|
|
|
mountLabel = opts.MountLabel
|
|
|
|
}
|
|
|
|
|
2015-10-28 09:19:51 -04:00
|
|
|
return label.Relabel(path.Join(subvolumes, id), mountLabel, false)
|
2013-11-21 15:45:48 -05:00
|
|
|
}
|
|
|
|
|
2016-03-23 21:14:38 -04:00
|
|
|
// Parse btrfs storage options
|
|
|
|
func (d *Driver) parseStorageOpt(storageOpt map[string]string, driver *Driver) error {
|
|
|
|
// Read size to change the subvolume disk quota per container
|
|
|
|
for key, val := range storageOpt {
|
|
|
|
key := strings.ToLower(key)
|
|
|
|
switch key {
|
|
|
|
case "size":
|
|
|
|
size, err := units.RAMInBytes(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
driver.options.size = uint64(size)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unknown option %s", key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set btrfs storage size
|
|
|
|
func (d *Driver) setStorageSize(dir string, driver *Driver) error {
|
|
|
|
if driver.options.size <= 0 {
|
|
|
|
return fmt.Errorf("btrfs: invalid storage size: %s", units.HumanSize(float64(driver.options.size)))
|
|
|
|
}
|
|
|
|
if d.options.minSpace > 0 && driver.options.size < d.options.minSpace {
|
|
|
|
return fmt.Errorf("btrfs: storage size cannot be less than %s", units.HumanSize(float64(d.options.minSpace)))
|
|
|
|
}
|
2017-01-02 19:50:59 -05:00
|
|
|
if err := d.subvolEnableQuota(); err != nil {
|
|
|
|
return err
|
2016-03-23 21:14:38 -04:00
|
|
|
}
|
2018-01-14 18:42:25 -05:00
|
|
|
return subvolLimitQgroup(dir, driver.options.size)
|
2016-03-23 21:14:38 -04:00
|
|
|
}
|
|
|
|
|
2015-07-22 16:51:35 -04:00
|
|
|
// Remove the filesystem with given id.
|
2013-11-21 15:45:48 -05:00
|
|
|
func (d *Driver) Remove(id string) error {
|
2015-07-22 16:51:35 -04:00
|
|
|
dir := d.subvolumesDirID(id)
|
2013-11-21 15:45:48 -05:00
|
|
|
if _, err := os.Stat(dir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-24 21:00:02 -04:00
|
|
|
quotasDir := d.quotasDirID(id)
|
|
|
|
if _, err := os.Stat(quotasDir); err == nil {
|
|
|
|
if err := os.Remove(quotasDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-14 16:21:18 -05:00
|
|
|
|
|
|
|
// Call updateQuotaStatus() to invoke status update
|
|
|
|
d.updateQuotaStatus()
|
|
|
|
|
|
|
|
if err := subvolDelete(d.subvolumesDir(), id, d.quotaEnabled); err != nil {
|
2013-11-21 15:45:48 -05:00
|
|
|
return err
|
|
|
|
}
|
2017-02-14 13:35:20 -05:00
|
|
|
if err := system.EnsureRemoveAll(dir); err != nil {
|
2015-12-25 02:04:34 -05:00
|
|
|
return err
|
|
|
|
}
|
2018-01-14 18:42:25 -05:00
|
|
|
return d.subvolRescanQuota()
|
2013-11-21 15:45:48 -05:00
|
|
|
}
|
|
|
|
|
2015-07-22 16:51:35 -04:00
|
|
|
// Get the requested filesystem id.
|
2017-08-03 20:22:00 -04:00
|
|
|
func (d *Driver) Get(id, mountLabel string) (containerfs.ContainerFS, error) {
|
2015-07-22 16:51:35 -04:00
|
|
|
dir := d.subvolumesDirID(id)
|
2013-11-21 15:45:48 -05:00
|
|
|
st, err := os.Stat(dir)
|
|
|
|
if err != nil {
|
2017-08-03 20:22:00 -04:00
|
|
|
return nil, err
|
2013-11-21 15:45:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if !st.IsDir() {
|
2017-08-03 20:22:00 -04:00
|
|
|
return nil, fmt.Errorf("%s: not a directory", dir)
|
2013-11-21 15:45:48 -05:00
|
|
|
}
|
|
|
|
|
2017-05-24 21:00:02 -04:00
|
|
|
if quota, err := ioutil.ReadFile(d.quotasDirID(id)); err == nil {
|
|
|
|
if size, err := strconv.ParseUint(string(quota), 10, 64); err == nil && size >= d.options.minSpace {
|
|
|
|
if err := d.subvolEnableQuota(); err != nil {
|
2017-08-03 20:22:00 -04:00
|
|
|
return nil, err
|
2017-05-24 21:00:02 -04:00
|
|
|
}
|
|
|
|
if err := subvolLimitQgroup(dir, size); err != nil {
|
2017-08-03 20:22:00 -04:00
|
|
|
return nil, err
|
2017-05-24 21:00:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
return containerfs.NewLocalContainerFS(dir), nil
|
2013-11-21 15:45:48 -05:00
|
|
|
}
|
|
|
|
|
2015-07-22 16:51:35 -04:00
|
|
|
// Put is not implemented for BTRFS as there is no cleanup required for the id.
|
2015-01-09 17:14:52 -05:00
|
|
|
func (d *Driver) Put(id string) error {
|
2014-01-28 03:56:51 -05:00
|
|
|
// Get() creates no runtime resources (like e.g. mounts)
|
|
|
|
// so this doesn't need to do anything.
|
2015-01-09 17:14:52 -05:00
|
|
|
return nil
|
2013-11-21 15:45:48 -05:00
|
|
|
}
|
|
|
|
|
2015-07-22 16:51:35 -04:00
|
|
|
// Exists checks if the id exists in the filesystem.
|
2013-11-21 15:45:48 -05:00
|
|
|
func (d *Driver) Exists(id string) bool {
|
2015-07-22 16:51:35 -04:00
|
|
|
dir := d.subvolumesDirID(id)
|
2013-11-21 15:45:48 -05:00
|
|
|
_, err := os.Stat(dir)
|
|
|
|
return err == nil
|
|
|
|
}
|