virtcontainers/utils: Implement function to check vsocks support

Implement function to check if the system has support for vsocks.
This function looks for vsock and vhost-vsock devices returning
true if those exist, otherwise false.

Signed-off-by: Jose Carlos Venegas Munoz <jose.carlos.venegas.munoz@intel.com>
Signed-off-by: Julio Montes <julio.montes@intel.com>
This commit is contained in:
Julio Montes 2018-07-19 08:44:55 -05:00
parent 8ae28888e0
commit 2339ac3f93
2 changed files with 50 additions and 0 deletions

View File

@ -23,6 +23,12 @@ const fileMode0755 = os.FileMode(0755)
// See unix(7). // See unix(7).
const MaxSocketPathLen = 107 const MaxSocketPathLen = 107
// VSockDevicePath path to vsock device
var VSockDevicePath = "/dev/vsock"
// VHostVSockDevicePath path to vhost-vsock device
var VHostVSockDevicePath = "/dev/vhost-vsock"
// FileCopy copys files from srcPath to dstPath // FileCopy copys files from srcPath to dstPath
func FileCopy(srcPath, dstPath string) error { func FileCopy(srcPath, dstPath string) error {
if srcPath == "" { if srcPath == "" {
@ -200,3 +206,16 @@ func BuildSocketPath(elements ...string) (string, error) {
return result, nil return result, nil
} }
// SupportsVsocks returns true if vsocks are supported, otherwise false
func SupportsVsocks() bool {
if _, err := os.Stat(VSockDevicePath); err != nil {
return false
}
if _, err := os.Stat(VHostVSockDevicePath); err != nil {
return false
}
return true
}

View File

@ -294,3 +294,34 @@ func TestBuildSocketPath(t *testing.T) {
assert.Equal(d.expected, result) assert.Equal(d.expected, result)
} }
} }
func TestSupportsVsocks(t *testing.T) {
assert := assert.New(t)
orgVSockDevicePath := VSockDevicePath
orgVHostVSockDevicePath := VHostVSockDevicePath
defer func() {
VSockDevicePath = orgVSockDevicePath
VHostVSockDevicePath = orgVHostVSockDevicePath
}()
VSockDevicePath = "/abc/xyz/123"
VHostVSockDevicePath = "/abc/xyz/123"
assert.False(SupportsVsocks())
vSockDeviceFile, err := ioutil.TempFile("", "vsock")
assert.NoError(err)
defer os.Remove(vSockDeviceFile.Name())
defer vSockDeviceFile.Close()
VSockDevicePath = vSockDeviceFile.Name()
assert.False(SupportsVsocks())
vHostVSockFile, err := ioutil.TempFile("", "vhost-vsock")
assert.NoError(err)
defer os.Remove(vHostVSockFile.Name())
defer vHostVSockFile.Close()
VHostVSockDevicePath = vHostVSockFile.Name()
assert.True(SupportsVsocks())
}