mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-22 05:28:25 +00:00
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:
parent
2c7f27ec4f
commit
0a4e2edcf4
@ -1323,7 +1323,7 @@ func (c *Container) hotplugDrive() error {
|
|||||||
c.rootfsSuffix = ""
|
c.rootfsSuffix = ""
|
||||||
}
|
}
|
||||||
// If device mapper device, then fetch the full path of the device
|
// 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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -6,17 +6,16 @@
|
|||||||
package virtcontainers
|
package virtcontainers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
merr "github.com/hashicorp/go-multierror"
|
merr "github.com/hashicorp/go-multierror"
|
||||||
|
"github.com/kata-containers/runtime/virtcontainers/utils"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -187,59 +186,6 @@ func getDeviceForPath(path string) (device, error) {
|
|||||||
return dev, nil
|
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 blockFormatTemplate = "/sys/dev/block/%d:%d/dm"
|
||||||
|
|
||||||
var checkStorageDriver = isDeviceMapper
|
var checkStorageDriver = isDeviceMapper
|
||||||
@ -445,7 +391,7 @@ func IsEphemeralStorage(path string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, fsType, _ := GetDevicePathAndFsType(path); fsType == "tmpfs" {
|
if _, fsType, _ := utils.GetDevicePathAndFsType(path); fsType == "tmpfs" {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -460,7 +406,7 @@ func Isk8sHostEmptyDir(path string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, fsType, _ := GetDevicePathAndFsType(path); fsType != "tmpfs" {
|
if _, fsType, _ := utils.GetDevicePathAndFsType(path); fsType != "tmpfs" {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
@ -206,22 +206,6 @@ func TestGetDeviceForPathBindMount(t *testing.T) {
|
|||||||
assert.Equal(sourceDev, destDev)
|
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) {
|
func TestIsDeviceMapper(t *testing.T) {
|
||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
@ -6,10 +6,13 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"math/big"
|
"math/big"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
@ -85,3 +88,56 @@ func FindContextID() (*os.File, uint64, error) {
|
|||||||
vsockFd.Close()
|
vsockFd.Close()
|
||||||
return nil, 0, fmt.Errorf("Could not get a unique context ID for the vsock : %s", err)
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -33,3 +33,19 @@ func TestFindContextID(t *testing.T) {
|
|||||||
assert.Zero(cid)
|
assert.Zero(cid)
|
||||||
assert.Error(err)
|
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")
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user