Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
virtcontainers: move GetDevicePathAndFsType to utils_linux
Browse files Browse the repository at this point in the history
`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 <[email protected]>
  • Loading branch information
Julio Montes committed Mar 20, 2020
1 parent 2c7f27e commit 0a4e2ed
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 74 deletions.
2 changes: 1 addition & 1 deletion virtcontainers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
60 changes: 3 additions & 57 deletions virtcontainers/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand Down
16 changes: 0 additions & 16 deletions virtcontainers/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
56 changes: 56 additions & 0 deletions virtcontainers/utils/utils_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
package utils

import (
"bufio"
"crypto/rand"
"fmt"
"io"
"math/big"
"os"
"strings"
"syscall"
"unsafe"

Expand Down Expand Up @@ -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
}
}
}
16 changes: 16 additions & 0 deletions virtcontainers/utils/utils_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit 0a4e2ed

Please sign in to comment.