From 0a4e2edcf4464d0c0ba6c501472687537de344bb Mon Sep 17 00:00:00 2001 From: Julio Montes Date: Fri, 6 Mar 2020 20:54:21 +0000 Subject: [PATCH] virtcontainers: move GetDevicePathAndFsType to utils_linux `GetDevicePathAndFsType` is a function to get the path and filesystem type of a mount point from `/proc/mounts`. Move `GetDevicePathAndFsType` to utils_linux since it's linux specific and that way it can be used in other subpackages. Signed-off-by: Julio Montes --- virtcontainers/container.go | 2 +- virtcontainers/mount.go | 60 ++---------------------- virtcontainers/mount_test.go | 16 ------- virtcontainers/utils/utils_linux.go | 56 ++++++++++++++++++++++ virtcontainers/utils/utils_linux_test.go | 16 +++++++ 5 files changed, 76 insertions(+), 74 deletions(-) diff --git a/virtcontainers/container.go b/virtcontainers/container.go index 1f5d2c4e5f..bfb32e9a42 100644 --- a/virtcontainers/container.go +++ b/virtcontainers/container.go @@ -1323,7 +1323,7 @@ func (c *Container) hotplugDrive() error { c.rootfsSuffix = "" } // If device mapper device, then fetch the full path of the device - devicePath, fsType, err = GetDevicePathAndFsType(dev.mountPoint) + devicePath, fsType, err = utils.GetDevicePathAndFsType(dev.mountPoint) if err != nil { return err } diff --git a/virtcontainers/mount.go b/virtcontainers/mount.go index fc06424c94..dd6177bef7 100644 --- a/virtcontainers/mount.go +++ b/virtcontainers/mount.go @@ -6,17 +6,16 @@ package virtcontainers import ( - "bufio" "context" "errors" "fmt" - "io" "os" "path/filepath" "strings" "syscall" merr "github.com/hashicorp/go-multierror" + "github.com/kata-containers/runtime/virtcontainers/utils" "github.com/sirupsen/logrus" ) @@ -187,59 +186,6 @@ func getDeviceForPath(path string) (device, error) { return dev, nil } -const ( - procMountsFile = "/proc/mounts" - - fieldsPerLine = 6 -) - -const ( - procDeviceIndex = iota - procPathIndex - procTypeIndex -) - -// GetDevicePathAndFsType gets the device for the mount point and the file system type -// of the mount. -func GetDevicePathAndFsType(mountPoint string) (devicePath, fsType string, err error) { - if mountPoint == "" { - err = fmt.Errorf("Mount point cannot be empty") - return - } - - var file *os.File - - file, err = os.Open(procMountsFile) - if err != nil { - return - } - - defer file.Close() - - reader := bufio.NewReader(file) - for { - var line string - - line, err = reader.ReadString('\n') - if err == io.EOF { - err = fmt.Errorf("Mount %s not found", mountPoint) - return - } - - fields := strings.Fields(line) - if len(fields) != fieldsPerLine { - err = fmt.Errorf("Incorrect no of fields (expected %d, got %d)) :%s", fieldsPerLine, len(fields), line) - return - } - - if mountPoint == fields[procPathIndex] { - devicePath = fields[procDeviceIndex] - fsType = fields[procTypeIndex] - return - } - } -} - var blockFormatTemplate = "/sys/dev/block/%d:%d/dm" var checkStorageDriver = isDeviceMapper @@ -445,7 +391,7 @@ func IsEphemeralStorage(path string) bool { return false } - if _, fsType, _ := GetDevicePathAndFsType(path); fsType == "tmpfs" { + if _, fsType, _ := utils.GetDevicePathAndFsType(path); fsType == "tmpfs" { return true } @@ -460,7 +406,7 @@ func Isk8sHostEmptyDir(path string) bool { return false } - if _, fsType, _ := GetDevicePathAndFsType(path); fsType != "tmpfs" { + if _, fsType, _ := utils.GetDevicePathAndFsType(path); fsType != "tmpfs" { return true } return false diff --git a/virtcontainers/mount_test.go b/virtcontainers/mount_test.go index fc394cc558..5a39133be7 100644 --- a/virtcontainers/mount_test.go +++ b/virtcontainers/mount_test.go @@ -206,22 +206,6 @@ func TestGetDeviceForPathBindMount(t *testing.T) { assert.Equal(sourceDev, destDev) } -func TestGetDevicePathAndFsTypeEmptyMount(t *testing.T) { - assert := assert.New(t) - _, _, err := GetDevicePathAndFsType("") - assert.Error(err) -} - -func TestGetDevicePathAndFsTypeSuccessful(t *testing.T) { - assert := assert.New(t) - - path, fstype, err := GetDevicePathAndFsType("/proc") - assert.NoError(err) - - assert.Equal(path, "proc") - assert.Equal(fstype, "proc") -} - func TestIsDeviceMapper(t *testing.T) { assert := assert.New(t) diff --git a/virtcontainers/utils/utils_linux.go b/virtcontainers/utils/utils_linux.go index 49d89dbd71..ad870d63ee 100644 --- a/virtcontainers/utils/utils_linux.go +++ b/virtcontainers/utils/utils_linux.go @@ -6,10 +6,13 @@ package utils import ( + "bufio" "crypto/rand" "fmt" + "io" "math/big" "os" + "strings" "syscall" "unsafe" @@ -85,3 +88,56 @@ func FindContextID() (*os.File, uint64, error) { vsockFd.Close() return nil, 0, fmt.Errorf("Could not get a unique context ID for the vsock : %s", err) } + +const ( + procMountsFile = "/proc/mounts" + + fieldsPerLine = 6 +) + +const ( + procDeviceIndex = iota + procPathIndex + procTypeIndex +) + +// GetDevicePathAndFsType gets the device for the mount point and the file system type +// of the mount. +func GetDevicePathAndFsType(mountPoint string) (devicePath, fsType string, err error) { + if mountPoint == "" { + err = fmt.Errorf("Mount point cannot be empty") + return + } + + var file *os.File + + file, err = os.Open(procMountsFile) + if err != nil { + return + } + + defer file.Close() + + reader := bufio.NewReader(file) + for { + var line string + + line, err = reader.ReadString('\n') + if err == io.EOF { + err = fmt.Errorf("Mount %s not found", mountPoint) + return + } + + fields := strings.Fields(line) + if len(fields) != fieldsPerLine { + err = fmt.Errorf("Incorrect no of fields (expected %d, got %d)) :%s", fieldsPerLine, len(fields), line) + return + } + + if mountPoint == fields[procPathIndex] { + devicePath = fields[procDeviceIndex] + fsType = fields[procTypeIndex] + return + } + } +} diff --git a/virtcontainers/utils/utils_linux_test.go b/virtcontainers/utils/utils_linux_test.go index 4901348ebc..4554fa935d 100644 --- a/virtcontainers/utils/utils_linux_test.go +++ b/virtcontainers/utils/utils_linux_test.go @@ -33,3 +33,19 @@ func TestFindContextID(t *testing.T) { assert.Zero(cid) assert.Error(err) } + +func TestGetDevicePathAndFsTypeEmptyMount(t *testing.T) { + assert := assert.New(t) + _, _, err := GetDevicePathAndFsType("") + assert.Error(err) +} + +func TestGetDevicePathAndFsTypeSuccessful(t *testing.T) { + assert := assert.New(t) + + path, fstype, err := GetDevicePathAndFsType("/proc") + assert.NoError(err) + + assert.Equal(path, "proc") + assert.Equal(fstype, "proc") +}