37
pkg/os/os.go
37
pkg/os/os.go
@@ -17,11 +17,13 @@ limitations under the License.
|
||||
package os
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
containerdmount "github.com/containerd/containerd/mount"
|
||||
"github.com/containerd/fifo"
|
||||
"github.com/docker/docker/pkg/mount"
|
||||
"golang.org/x/net/context"
|
||||
@@ -41,6 +43,8 @@ type OS interface {
|
||||
Mount(source string, target string, fstype string, flags uintptr, data string) error
|
||||
Unmount(target string, flags int) error
|
||||
GetMounts() ([]*mount.Info, error)
|
||||
LookupMount(path string) (containerdmount.Info, error)
|
||||
DeviceUUID(device string) (string, error)
|
||||
}
|
||||
|
||||
// RealOS is used to dispatch the real system level operations.
|
||||
@@ -120,3 +124,36 @@ func (RealOS) Unmount(target string, flags int) error {
|
||||
func (RealOS) GetMounts() ([]*mount.Info, error) {
|
||||
return mount.GetMounts()
|
||||
}
|
||||
|
||||
// LookupMount gets mount info of a given path.
|
||||
func (RealOS) LookupMount(path string) (containerdmount.Info, error) {
|
||||
return containerdmount.Lookup(path)
|
||||
}
|
||||
|
||||
// DeviceUUID gets device uuid of a device. The passed in device should be
|
||||
// an absolute path of the device.
|
||||
func (RealOS) DeviceUUID(device string) (string, error) {
|
||||
const uuidDir = "/dev/disk/by-uuid"
|
||||
if _, err := os.Stat(uuidDir); err != nil {
|
||||
return "", err
|
||||
}
|
||||
files, err := ioutil.ReadDir(uuidDir)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, file := range files {
|
||||
path := filepath.Join(uuidDir, file.Name())
|
||||
target, err := os.Readlink(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
dev, err := filepath.Abs(filepath.Join(uuidDir, target))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if dev == device {
|
||||
return file.Name(), nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("device not found")
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
containerdmount "github.com/containerd/containerd/mount"
|
||||
"github.com/docker/docker/pkg/mount"
|
||||
"golang.org/x/net/context"
|
||||
|
||||
@@ -50,6 +51,8 @@ type FakeOS struct {
|
||||
MountFn func(source string, target string, fstype string, flags uintptr, data string) error
|
||||
UnmountFn func(target string, flags int) error
|
||||
GetMountsFn func() ([]*mount.Info, error)
|
||||
LookupMountFn func(path string) (containerdmount.Info, error)
|
||||
DeviceUUIDFn func(device string) (string, error)
|
||||
calls []CalledDetail
|
||||
errors map[string]error
|
||||
}
|
||||
@@ -240,3 +243,29 @@ func (f *FakeOS) GetMounts() ([]*mount.Info, error) {
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// LookupMount is a fake call that invokes LookupMountFn or just return nil.
|
||||
func (f *FakeOS) LookupMount(path string) (containerdmount.Info, error) {
|
||||
f.appendCalls("LookupMount", path)
|
||||
if err := f.getError("LookupMount"); err != nil {
|
||||
return containerdmount.Info{}, err
|
||||
}
|
||||
|
||||
if f.LookupMountFn != nil {
|
||||
return f.LookupMountFn(path)
|
||||
}
|
||||
return containerdmount.Info{}, nil
|
||||
}
|
||||
|
||||
// DeviceUUID is a fake call that invodes DeviceUUIDFn or just return nil.
|
||||
func (f *FakeOS) DeviceUUID(device string) (string, error) {
|
||||
f.appendCalls("DeviceUUID", device)
|
||||
if err := f.getError("DeviceUUID"); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if f.DeviceUUIDFn != nil {
|
||||
return f.DeviceUUIDFn(device)
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user