2014-08-02 03:46:15 -04:00
|
|
|
// +build linux
|
2013-11-21 15:45:48 -05:00
|
|
|
|
|
|
|
package btrfs
|
|
|
|
|
|
|
|
/*
|
|
|
|
#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>
|
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"
|
|
|
|
"os"
|
|
|
|
"path"
|
2015-08-05 03:10:40 -04:00
|
|
|
"path/filepath"
|
2013-11-21 15:45:48 -05:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
2014-06-05 15:50:53 -04:00
|
|
|
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/daemon/graphdriver"
|
|
|
|
"github.com/docker/docker/pkg/mount"
|
2013-11-21 15:45:48 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
graphdriver.Register("btrfs", Init)
|
|
|
|
}
|
|
|
|
|
2015-07-22 16:51:35 -04:00
|
|
|
// Init returns a new BTRFS driver.
|
|
|
|
// An error is returned if BTRFS is not supported.
|
2014-06-05 04:34:20 -04:00
|
|
|
func Init(home string, options []string) (graphdriver.Driver, error) {
|
2013-11-21 15:45:48 -05:00
|
|
|
rootdir := path.Dir(home)
|
|
|
|
|
|
|
|
var buf syscall.Statfs_t
|
|
|
|
if err := syscall.Statfs(rootdir, &buf); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-02 21:26:41 -04:00
|
|
|
if graphdriver.FsMagic(buf.Type) != graphdriver.FsMagicBtrfs {
|
2014-05-29 09:50:52 -04:00
|
|
|
return nil, graphdriver.ErrPrerequisites
|
2013-11-21 15:45:48 -05:00
|
|
|
}
|
|
|
|
|
2014-06-05 15:50:53 -04:00
|
|
|
if err := os.MkdirAll(home, 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-10-30 17:04:56 -04:00
|
|
|
if err := mount.MakePrivate(home); err != nil {
|
2014-06-05 15:50:53 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-09-10 23:30:52 -04:00
|
|
|
driver := &Driver{
|
2013-11-21 15:45:48 -05:00
|
|
|
home: home,
|
2014-09-10 23:30:52 -04:00
|
|
|
}
|
|
|
|
|
2015-09-23 21:21:28 -04:00
|
|
|
return graphdriver.NewNaiveDiffDriver(driver), nil
|
2013-11-21 15:45:48 -05: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
|
2013-11-21 15:45:48 -05:00
|
|
|
home string
|
|
|
|
}
|
|
|
|
|
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 {
|
2014-06-05 15:50:53 -04:00
|
|
|
return mount.Unmount(d.home)
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_SUBVOL_CREATE,
|
|
|
|
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))
|
|
|
|
for i, c := range []byte(name) {
|
|
|
|
args.name[i] = C.char(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, getDirFd(destDir), C.BTRFS_IOC_SNAP_CREATE_V2,
|
|
|
|
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) {
|
|
|
|
var bufStat syscall.Stat_t
|
|
|
|
if err := syscall.Lstat(p, &bufStat); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// return true if it is a btrfs subvolume
|
|
|
|
return bufStat.Ino == C.BTRFS_FIRST_FREE_OBJECTID, nil
|
|
|
|
}
|
|
|
|
|
2015-08-05 03:10:40 -04:00
|
|
|
func subvolDelete(dirpath, name string) error {
|
|
|
|
dir, err := openDir(dirpath)
|
2013-11-21 15:45:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closeDir(dir)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
// we want to check children only so skip itself
|
|
|
|
// it will be removed after the filepath walk anyways
|
|
|
|
if f.IsDir() && p != path.Join(dirpath, name) {
|
|
|
|
sv, err := isSubvolume(p)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to test if %s is a btrfs subvolume: %v", p, err)
|
|
|
|
}
|
|
|
|
if sv {
|
|
|
|
if err := subvolDelete(p, f.Name()); err != nil {
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_SNAP_DESTROY,
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-07-22 16:51:35 -04:00
|
|
|
// Create the filesystem with given id.
|
2014-04-17 19:47:27 -04:00
|
|
|
func (d *Driver) Create(id string, parent string) error {
|
2013-11-21 15:45:48 -05:00
|
|
|
subvolumes := path.Join(d.home, "subvolumes")
|
|
|
|
if err := os.MkdirAll(subvolumes, 0700); err != nil {
|
|
|
|
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 {
|
2014-04-17 19:47:27 -04:00
|
|
|
parentDir, err := d.Get(parent, "")
|
2013-11-21 15:45:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := subvolSnapshot(parentDir, subvolumes, id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if err := subvolDelete(d.subvolumesDir(), id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.RemoveAll(dir)
|
|
|
|
}
|
|
|
|
|
2015-07-22 16:51:35 -04:00
|
|
|
// Get the requested filesystem id.
|
2014-04-17 19:47:27 -04:00
|
|
|
func (d *Driver) Get(id, mountLabel string) (string, 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 {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !st.IsDir() {
|
|
|
|
return "", fmt.Errorf("%s: not a directory", dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dir, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|