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 <julio.montes@intel.com>
This commit is contained in:
Julio Montes 2020-03-06 20:54:21 +00:00
parent 2c7f27ec4f
commit 0a4e2edcf4
5 changed files with 76 additions and 74 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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)

View File

@ -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
}
}
}

View File

@ -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")
}