mountinfo: refactored

Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
This commit is contained in:
Akihiro Suda
2017-05-10 04:53:59 +00:00
parent d5707d3ac7
commit 8cd218237b
9 changed files with 71 additions and 67 deletions

View File

@@ -36,17 +36,17 @@ type snapshotter struct {
ms *storage.MetaStore
}
func getBtrfsDevice(root string, mounts []*mountinfo.Info) (string, error) {
func getBtrfsDevice(root string, mounts []mountinfo.Info) (string, error) {
device := ""
deviceMountpoint := ""
for _, info := range mounts {
if (info.Root == "/" || info.Root == "") && strings.HasPrefix(root, info.Mountpoint) {
if info.Fstype == "btrfs" && len(info.Mountpoint) > len(deviceMountpoint) {
if info.FSType == "btrfs" && len(info.Mountpoint) > len(deviceMountpoint) {
device = info.Source
deviceMountpoint = info.Mountpoint
}
if root == info.Mountpoint && info.Fstype != "btrfs" {
return "", fmt.Errorf("%s needs to be btrfs, but seems %s", root, info.Fstype)
if root == info.Mountpoint && info.FSType != "btrfs" {
return "", fmt.Errorf("%s needs to be btrfs, but seems %s", root, info.FSType)
}
}
}
@@ -62,7 +62,7 @@ func getBtrfsDevice(root string, mounts []*mountinfo.Info) (string, error) {
// a file in the provided root.
// root needs to be a mount point of btrfs.
func NewSnapshotter(root string) (snapshot.Snapshotter, error) {
mounts, err := mountinfo.GetMounts()
mounts, err := mountinfo.Self()
if err != nil {
return nil, err
}

View File

@@ -222,45 +222,45 @@ func TestGetBtrfsDevice(t *testing.T) {
expectedDevice string
expectedError string
root string
mounts []*mountinfo.Info
mounts []mountinfo.Info
}{
{
expectedDevice: "/dev/loop0",
root: "/var/lib/containerd/snapshot/btrfs",
mounts: []*mountinfo.Info{
{Root: "/", Mountpoint: "/", Fstype: "ext4", Source: "/dev/sda1"},
{Root: "/", Mountpoint: "/var/lib/containerd/snapshot/btrfs", Fstype: "btrfs", Source: "/dev/loop0"},
mounts: []mountinfo.Info{
{Root: "/", Mountpoint: "/", FSType: "ext4", Source: "/dev/sda1"},
{Root: "/", Mountpoint: "/var/lib/containerd/snapshot/btrfs", FSType: "btrfs", Source: "/dev/loop0"},
},
},
{
expectedError: "/var/lib/containerd/snapshot/btrfs is not mounted as btrfs",
root: "/var/lib/containerd/snapshot/btrfs",
mounts: []*mountinfo.Info{
{Root: "/", Mountpoint: "/", Fstype: "ext4", Source: "/dev/sda1"},
mounts: []mountinfo.Info{
{Root: "/", Mountpoint: "/", FSType: "ext4", Source: "/dev/sda1"},
},
},
{
expectedDevice: "/dev/sda1",
root: "/var/lib/containerd/snapshot/btrfs",
mounts: []*mountinfo.Info{
{Root: "/", Mountpoint: "/", Fstype: "btrfs", Source: "/dev/sda1"},
mounts: []mountinfo.Info{
{Root: "/", Mountpoint: "/", FSType: "btrfs", Source: "/dev/sda1"},
},
},
{
expectedDevice: "/dev/sda2",
root: "/var/lib/containerd/snapshot/btrfs",
mounts: []*mountinfo.Info{
{Root: "/", Mountpoint: "/", Fstype: "btrfs", Source: "/dev/sda1"},
{Root: "/", Mountpoint: "/var/lib/containerd/snapshot/btrfs", Fstype: "btrfs", Source: "/dev/sda2"},
mounts: []mountinfo.Info{
{Root: "/", Mountpoint: "/", FSType: "btrfs", Source: "/dev/sda1"},
{Root: "/", Mountpoint: "/var/lib/containerd/snapshot/btrfs", FSType: "btrfs", Source: "/dev/sda2"},
},
},
{
expectedDevice: "/dev/sda2",
root: "/var/lib/containerd/snapshot/btrfs",
mounts: []*mountinfo.Info{
{Root: "/", Mountpoint: "/var/lib/containerd/snapshot/btrfs", Fstype: "btrfs", Source: "/dev/sda2"},
{Root: "/", Mountpoint: "/var/lib/foooooooooooooooooooo/baaaaaaaaaaaaaaaaaaaar", Fstype: "btrfs", Source: "/dev/sda3"}, // mountpoint length longer than /var/lib/containerd/snapshot/btrfs
{Root: "/", Mountpoint: "/", Fstype: "btrfs", Source: "/dev/sda1"},
mounts: []mountinfo.Info{
{Root: "/", Mountpoint: "/var/lib/containerd/snapshot/btrfs", FSType: "btrfs", Source: "/dev/sda2"},
{Root: "/", Mountpoint: "/var/lib/foooooooooooooooooooo/baaaaaaaaaaaaaaaaaaaar", FSType: "btrfs", Source: "/dev/sda3"}, // mountpoint length longer than /var/lib/containerd/snapshot/btrfs
{Root: "/", Mountpoint: "/", FSType: "btrfs", Source: "/dev/sda1"},
},
},
}