2017-01-13 23:31:21 +00:00
|
|
|
package btrfs
|
2016-12-13 05:53:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2016-12-30 00:59:18 +00:00
|
|
|
"os/exec"
|
2016-12-13 05:53:48 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/containerd"
|
2017-02-03 22:20:05 +00:00
|
|
|
"github.com/docker/containerd/snapshot"
|
2017-02-01 02:04:13 +00:00
|
|
|
"github.com/docker/containerd/testutil"
|
2016-12-30 00:59:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
mib = 1024 * 1024
|
2016-12-13 05:53:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBtrfs(t *testing.T) {
|
2017-01-25 00:10:48 +00:00
|
|
|
testutil.RequiresRoot(t)
|
2017-02-08 01:21:19 +00:00
|
|
|
snapshot.SnapshotterSuite(t, "Btrfs", func(root string) (snapshot.Snapshotter, func(), error) {
|
2017-02-03 22:20:05 +00:00
|
|
|
device := setupBtrfsLoopbackDevice(t, root)
|
2017-02-08 01:21:19 +00:00
|
|
|
snapshotter, err := NewSnapshotter(device.deviceName, root)
|
2017-02-03 22:20:05 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-02-08 01:21:19 +00:00
|
|
|
return snapshotter, func() {
|
2017-02-03 22:20:05 +00:00
|
|
|
device.remove(t)
|
|
|
|
}, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBtrfsMounts(t *testing.T) {
|
|
|
|
testutil.RequiresRoot(t)
|
|
|
|
|
|
|
|
// create temporary directory for mount point
|
|
|
|
mountPoint, err := ioutil.TempDir("", "containerd-btrfs-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("could not create mount point for btrfs test", err)
|
|
|
|
}
|
|
|
|
t.Log("temporary mount point created", mountPoint)
|
|
|
|
|
|
|
|
device := setupBtrfsLoopbackDevice(t, mountPoint)
|
|
|
|
defer device.remove(t)
|
|
|
|
|
2016-12-30 00:59:18 +00:00
|
|
|
root, err := ioutil.TempDir(device.mountPoint, "TestBtrfsPrepare-")
|
2016-12-13 05:53:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(root)
|
|
|
|
|
2017-01-17 21:26:56 +00:00
|
|
|
target := filepath.Join(root, "test")
|
2017-02-08 01:21:19 +00:00
|
|
|
b, err := NewSnapshotter(device.deviceName, root)
|
2016-12-13 05:53:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-01-17 08:42:27 +00:00
|
|
|
mounts, err := b.Prepare(target, "")
|
2016-12-13 05:53:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Log(mounts)
|
|
|
|
|
|
|
|
for _, mount := range mounts {
|
|
|
|
if mount.Type != "btrfs" {
|
2016-12-20 22:08:42 +00:00
|
|
|
t.Fatalf("wrong mount type: %v != btrfs", mount.Type)
|
2016-12-13 05:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// assumes the first, maybe incorrect in the future
|
|
|
|
if !strings.HasPrefix(mount.Options[0], "subvolid=") {
|
2016-12-20 22:08:42 +00:00
|
|
|
t.Fatalf("no subvolid option in %v", mount.Options)
|
2016-12-13 05:53:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-17 21:26:56 +00:00
|
|
|
if err := os.MkdirAll(target, 0755); err != nil {
|
2016-12-13 05:53:48 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-01-17 21:26:56 +00:00
|
|
|
if err := containerd.MountAll(mounts, target); err != nil {
|
2016-12-13 05:53:48 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-01-17 21:26:56 +00:00
|
|
|
defer testutil.Unmount(t, target)
|
2016-12-13 05:53:48 +00:00
|
|
|
|
|
|
|
// write in some data
|
2017-01-17 21:26:56 +00:00
|
|
|
if err := ioutil.WriteFile(filepath.Join(target, "foo"), []byte("content"), 0777); err != nil {
|
2016-12-13 05:53:48 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(stevvooe): We don't really make this with the driver, but that
|
|
|
|
// might prove annoying in practice.
|
|
|
|
if err := os.MkdirAll(filepath.Join(root, "snapshots"), 0755); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-01-17 08:42:27 +00:00
|
|
|
if err := b.Commit(filepath.Join(root, "snapshots/committed"), filepath.Join(root, "test")); err != nil {
|
2016-12-13 05:53:48 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-01-17 21:26:56 +00:00
|
|
|
target = filepath.Join(root, "test2")
|
2017-01-17 08:42:27 +00:00
|
|
|
mounts, err = b.Prepare(target, filepath.Join(root, "snapshots/committed"))
|
2016-12-13 05:53:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-01-17 21:26:56 +00:00
|
|
|
if err := os.MkdirAll(target, 0755); err != nil {
|
2016-12-13 05:53:48 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-01-17 21:26:56 +00:00
|
|
|
if err := containerd.MountAll(mounts, target); err != nil {
|
2016-12-13 05:53:48 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-01-17 21:26:56 +00:00
|
|
|
defer testutil.Unmount(t, target)
|
2016-12-13 05:53:48 +00:00
|
|
|
|
|
|
|
// TODO(stevvooe): Verify contents of "foo"
|
2017-01-17 21:26:56 +00:00
|
|
|
if err := ioutil.WriteFile(filepath.Join(target, "bar"), []byte("content"), 0777); err != nil {
|
2016-12-13 05:53:48 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-01-17 08:42:27 +00:00
|
|
|
if err := b.Commit(filepath.Join(root, "snapshots/committed2"), target); err != nil {
|
2016-12-13 05:53:48 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-12-30 00:59:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type testDevice struct {
|
|
|
|
mountPoint string
|
|
|
|
fileName string
|
|
|
|
deviceName string
|
|
|
|
}
|
|
|
|
|
|
|
|
// setupBtrfsLoopbackDevice creates a file, mounts it as a loopback device, and
|
|
|
|
// formats it as btrfs. The device should be cleaned up by calling
|
|
|
|
// removeBtrfsLoopbackDevice.
|
2017-02-03 22:20:05 +00:00
|
|
|
func setupBtrfsLoopbackDevice(t *testing.T, mountPoint string) *testDevice {
|
2016-12-30 00:59:18 +00:00
|
|
|
|
|
|
|
// create temporary file for the disk image
|
|
|
|
file, err := ioutil.TempFile("", "containerd-btrfs-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not create temporary file for btrfs test", err)
|
|
|
|
}
|
|
|
|
t.Log("Temporary file created", file.Name())
|
|
|
|
|
|
|
|
// initialize file with 100 MiB
|
2017-02-18 00:53:11 +00:00
|
|
|
if err := file.Truncate(100 << 20); err != nil {
|
|
|
|
t.Fatal(err)
|
2016-12-30 00:59:18 +00:00
|
|
|
}
|
|
|
|
file.Close()
|
|
|
|
|
|
|
|
// create device
|
|
|
|
losetup := exec.Command("losetup", "--find", "--show", file.Name())
|
2017-02-18 00:53:11 +00:00
|
|
|
p, err := losetup.Output()
|
2016-12-30 00:59:18 +00:00
|
|
|
if err != nil {
|
2017-02-18 00:53:11 +00:00
|
|
|
t.Fatal(err)
|
2016-12-30 00:59:18 +00:00
|
|
|
}
|
2017-02-18 00:53:11 +00:00
|
|
|
|
|
|
|
deviceName := strings.TrimSpace(string(p))
|
2016-12-30 00:59:18 +00:00
|
|
|
t.Log("Created loop device", deviceName)
|
|
|
|
|
|
|
|
// format
|
|
|
|
t.Log("Creating btrfs filesystem")
|
|
|
|
mkfs := exec.Command("mkfs.btrfs", deviceName)
|
|
|
|
err = mkfs.Run()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not run mkfs.btrfs", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// mount
|
|
|
|
t.Logf("Mounting %s at %s", deviceName, mountPoint)
|
|
|
|
mount := exec.Command("mount", deviceName, mountPoint)
|
|
|
|
err = mount.Run()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not mount", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &testDevice{
|
|
|
|
mountPoint: mountPoint,
|
|
|
|
fileName: file.Name(),
|
|
|
|
deviceName: deviceName,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-03 22:20:05 +00:00
|
|
|
// remove cleans up the test device, unmounting the loopback and disk image
|
|
|
|
// file.
|
|
|
|
func (device *testDevice) remove(t *testing.T) {
|
2016-12-30 00:59:18 +00:00
|
|
|
// unmount
|
2017-01-13 23:31:21 +00:00
|
|
|
testutil.Unmount(t, device.mountPoint)
|
2016-12-30 00:59:18 +00:00
|
|
|
|
|
|
|
// detach device
|
|
|
|
t.Log("Removing loop device")
|
|
|
|
losetup := exec.Command("losetup", "--detach", device.deviceName)
|
|
|
|
err := losetup.Run()
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Could not remove loop device", device.deviceName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove file
|
|
|
|
t.Log("Removing temporary file")
|
|
|
|
err = os.Remove(device.fileName)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove mount point
|
|
|
|
t.Log("Removing temporary mount point")
|
|
|
|
err = os.RemoveAll(device.mountPoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|