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

Commit

Permalink
device: add GetHostPath() to generic device
Browse files Browse the repository at this point in the history
`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 <[email protected]>
  • Loading branch information
Julio Montes committed Apr 28, 2020
1 parent 3b98b25 commit 0d3b697
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions virtcontainers/device/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions virtcontainers/device/drivers/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions virtcontainers/device/drivers/generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package drivers
import (
"testing"

"github.com/kata-containers/runtime/virtcontainers/device/config"
"github.com/stretchr/testify/assert"
)

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

0 comments on commit 0d3b697

Please sign in to comment.