2014-08-02 07:46:15 +00:00
|
|
|
// +build linux
|
2013-11-21 20:45:48 +00:00
|
|
|
|
|
|
|
package btrfs
|
|
|
|
|
|
|
|
/*
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <dirent.h>
|
2014-02-02 04:40:51 +00:00
|
|
|
#include <btrfs/ioctl.h>
|
2013-11-21 20:45:48 +00:00
|
|
|
*/
|
|
|
|
import "C"
|
2014-02-02 04:40:51 +00:00
|
|
|
|
2013-11-21 20:45:48 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
2014-06-05 19:50:53 +00:00
|
|
|
|
2014-07-24 22:19:50 +00:00
|
|
|
"github.com/docker/docker/daemon/graphdriver"
|
|
|
|
"github.com/docker/docker/pkg/mount"
|
2013-11-21 20:45:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
graphdriver.Register("btrfs", Init)
|
|
|
|
}
|
|
|
|
|
2014-06-05 08:34:20 +00:00
|
|
|
func Init(home string, options []string) (graphdriver.Driver, error) {
|
2013-11-21 20:45:48 +00:00
|
|
|
rootdir := path.Dir(home)
|
|
|
|
|
|
|
|
var buf syscall.Statfs_t
|
|
|
|
if err := syscall.Statfs(rootdir, &buf); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-03 01:26:41 +00:00
|
|
|
if graphdriver.FsMagic(buf.Type) != graphdriver.FsMagicBtrfs {
|
2014-05-29 13:50:52 +00:00
|
|
|
return nil, graphdriver.ErrPrerequisites
|
2013-11-21 20:45:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 19:50:53 +00:00
|
|
|
if err := os.MkdirAll(home, 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := graphdriver.MakePrivate(home); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-09-11 03:30:52 +00:00
|
|
|
driver := &Driver{
|
2013-11-21 20:45:48 +00:00
|
|
|
home: home,
|
2014-09-11 03:30:52 +00:00
|
|
|
}
|
|
|
|
|
2014-09-16 19:13:50 +00:00
|
|
|
return graphdriver.NaiveDiffDriver(driver), nil
|
2013-11-21 20:45:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Driver struct {
|
|
|
|
home string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) String() string {
|
|
|
|
return "btrfs"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) Status() [][2]string {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) Cleanup() error {
|
2014-06-05 19:50:53 +00:00
|
|
|
return mount.Unmount(d.home)
|
2013-11-21 20:45:48 +00: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 23:47:27 +00:00
|
|
|
func subvolCreate(path, name string) error {
|
2013-11-21 20:45:48 +00: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 06:52:52 +00:00
|
|
|
return fmt.Errorf("Failed to create btrfs subvolume: %v", errno.Error())
|
2013-11-21 20:45:48 +00: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 06:52:52 +00:00
|
|
|
return fmt.Errorf("Failed to create btrfs snapshot: %v", errno.Error())
|
2013-11-21 20:45:48 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func subvolDelete(path, name string) error {
|
|
|
|
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_SNAP_DESTROY,
|
|
|
|
uintptr(unsafe.Pointer(&args)))
|
|
|
|
if errno != 0 {
|
2014-02-02 06:52:52 +00:00
|
|
|
return fmt.Errorf("Failed to destroy btrfs snapshot: %v", errno.Error())
|
2013-11-21 20:45:48 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) subvolumesDir() string {
|
|
|
|
return path.Join(d.home, "subvolumes")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) subvolumesDirId(id string) string {
|
|
|
|
return path.Join(d.subvolumesDir(), id)
|
|
|
|
}
|
|
|
|
|
2014-04-17 23:47:27 +00:00
|
|
|
func (d *Driver) Create(id string, parent string) error {
|
2013-11-21 20:45:48 +00:00
|
|
|
subvolumes := path.Join(d.home, "subvolumes")
|
|
|
|
if err := os.MkdirAll(subvolumes, 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if parent == "" {
|
2014-04-17 23:47:27 +00:00
|
|
|
if err := subvolCreate(subvolumes, id); err != nil {
|
2013-11-21 20:45:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2014-04-17 23:47:27 +00:00
|
|
|
parentDir, err := d.Get(parent, "")
|
2013-11-21 20:45:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := subvolSnapshot(parentDir, subvolumes, id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) Remove(id string) error {
|
|
|
|
dir := d.subvolumesDirId(id)
|
|
|
|
if _, err := os.Stat(dir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := subvolDelete(d.subvolumesDir(), id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.RemoveAll(dir)
|
|
|
|
}
|
|
|
|
|
2014-04-17 23:47:27 +00:00
|
|
|
func (d *Driver) Get(id, mountLabel string) (string, error) {
|
2013-11-21 20:45:48 +00:00
|
|
|
dir := d.subvolumesDirId(id)
|
|
|
|
st, err := os.Stat(dir)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !st.IsDir() {
|
|
|
|
return "", fmt.Errorf("%s: not a directory", dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dir, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) Put(id string) {
|
2014-01-28 08:56:51 +00:00
|
|
|
// Get() creates no runtime resources (like e.g. mounts)
|
|
|
|
// so this doesn't need to do anything.
|
2013-11-21 20:45:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) Exists(id string) bool {
|
|
|
|
dir := d.subvolumesDirId(id)
|
|
|
|
_, err := os.Stat(dir)
|
|
|
|
return err == nil
|
|
|
|
}
|