device: add GetHostPath() to generic device

`GetHostPath()` method returns the device path in the host, this way the
runtime can get the device information for updating the sandbox's device
cgroup.

Signed-off-by: Julio Montes <julio.montes@intel.com>
Signed-off-by: Peng Tao <bergwolf@hyper.sh>
This commit is contained in:
Julio Montes
2020-05-26 00:26:33 -07:00
committed by Peng Tao
parent 83f116b483
commit 387d3d34dc
3 changed files with 24 additions and 0 deletions

View File

@@ -56,6 +56,9 @@ type Device interface {
// GetMajorMinor returns major and minor numbers
GetMajorMinor() (int64, int64)
// GetHostPath return the device path in the host
GetHostPath() string
// GetDeviceInfo returns device specific data used for hotplugging by hypervisor
// Caller could cast the return value to device specific struct
// e.g. Block device returns *config.BlockDrive,

View File

@@ -68,6 +68,14 @@ func (device *GenericDevice) GetMajorMinor() (int64, int64) {
return device.DeviceInfo.Major, device.DeviceInfo.Minor
}
// GetHostPath return the device path in the host
func (device *GenericDevice) GetHostPath() string {
if device.DeviceInfo != nil {
return device.DeviceInfo.HostPath
}
return ""
}
// Reference adds one reference to device
func (device *GenericDevice) Reference() uint {
if device.RefCount != intMax {

View File

@@ -8,6 +8,7 @@ package drivers
import (
"testing"
"github.com/kata-containers/runtime/virtcontainers/device/config"
"github.com/stretchr/testify/assert"
)
@@ -42,3 +43,15 @@ func TestBumpAttachCount(t *testing.T) {
}
}
}
func TestGetHostPath(t *testing.T) {
assert := assert.New(t)
dev := &GenericDevice{}
assert.Empty(dev.GetHostPath())
expectedHostPath := "/dev/null"
dev.DeviceInfo = &config.DeviceInfo{
HostPath: expectedHostPath,
}
assert.Equal(expectedHostPath, dev.GetHostPath())
}