Merge pull request #1513 from Pennyzct/vsock

support-vsock: load vhost_vsock module if it isn't built-in
This commit is contained in:
James O. D. Hunt 2019-05-14 11:08:48 +01:00 committed by GitHub
commit 576b8a510c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 77 additions and 49 deletions

View File

@ -35,6 +35,9 @@ type kernelModule struct {
// maps parameter names to values // maps parameter names to values
parameters map[string]string parameters map[string]string
// if it is definitely required
required bool
} }
type vmContainerCapableDetails struct { type vmContainerCapableDetails struct {
@ -124,8 +127,11 @@ func haveKernelModule(module string) bool {
// Now, check if the module is unloaded, but available. // Now, check if the module is unloaded, but available.
// And modprobe it if so. // And modprobe it if so.
cmd := exec.Command(modProbeCmd, module) cmd := exec.Command(modProbeCmd, module)
err := cmd.Run() if output, err := cmd.CombinedOutput(); err != nil {
return err == nil kataLog.WithField("module", module).WithError(err).Warnf("modprobe insert module failed: %s", string(output))
return false
}
return true
} }
// checkCPU checks all required CPU attributes modules and returns a count of // checkCPU checks all required CPU attributes modules and returns a count of
@ -200,7 +206,9 @@ func checkKernelModules(modules map[string]kernelModule, handler kernelParamHand
if !haveKernelModule(module) { if !haveKernelModule(module) {
kataLog.WithFields(fields).Error("kernel property not found") kataLog.WithFields(fields).Error("kernel property not found")
if details.required {
count++ count++
}
continue continue
} }

View File

@ -23,6 +23,7 @@ const (
msgKernelVM = "Kernel-based Virtual Machine" msgKernelVM = "Kernel-based Virtual Machine"
msgKernelVirtio = "Host kernel accelerator for virtio" msgKernelVirtio = "Host kernel accelerator for virtio"
msgKernelVirtioNet = "Host kernel accelerator for virtio network" msgKernelVirtioNet = "Host kernel accelerator for virtio network"
msgKernelVirtioVhostVsock = "Host Support for Linux VM Sockets"
) )
// CPU types // CPU types
@ -76,16 +77,24 @@ func setCPUtype() error {
archRequiredKernelModules = map[string]kernelModule{ archRequiredKernelModules = map[string]kernelModule{
"kvm": { "kvm": {
desc: msgKernelVM, desc: msgKernelVM,
required: true,
}, },
"kvm_intel": { "kvm_intel": {
desc: "Intel KVM", desc: "Intel KVM",
parameters: kvmIntelParams, parameters: kvmIntelParams,
required: true,
}, },
"vhost": { "vhost": {
desc: msgKernelVirtio, desc: msgKernelVirtio,
required: true,
}, },
"vhost_net": { "vhost_net": {
desc: msgKernelVirtioNet, desc: msgKernelVirtioNet,
required: true,
},
"vhost_vsock": {
desc: msgKernelVirtioVhostVsock,
required: false,
}, },
} }
} else if cpuType == cpuTypeAMD { } else if cpuType == cpuTypeAMD {
@ -100,15 +109,23 @@ func setCPUtype() error {
archRequiredKernelModules = map[string]kernelModule{ archRequiredKernelModules = map[string]kernelModule{
"kvm": { "kvm": {
desc: msgKernelVM, desc: msgKernelVM,
required: true,
}, },
"kvm_amd": { "kvm_amd": {
desc: "AMD KVM", desc: "AMD KVM",
required: true,
}, },
"vhost": { "vhost": {
desc: msgKernelVirtio, desc: msgKernelVirtio,
required: true,
}, },
"vhost_net": { "vhost_net": {
desc: msgKernelVirtioNet, desc: msgKernelVirtioNet,
required: true,
},
"vhost_vsock": {
desc: msgKernelVirtioVhostVsock,
required: false,
}, },
} }
} }

View File

@ -171,6 +171,7 @@ func TestCheckCheckKernelModulesNoNesting(t *testing.T) {
"nested": "Y", "nested": "Y",
"unrestricted_guest": "Y", "unrestricted_guest": "Y",
}, },
required: true,
}, },
} }
@ -255,6 +256,7 @@ func TestCheckCheckKernelModulesNoUnrestrictedGuest(t *testing.T) {
"nested": "Y", "nested": "Y",
"unrestricted_guest": "Y", "unrestricted_guest": "Y",
}, },
required: true,
}, },
} }

View File

@ -31,12 +31,19 @@ var archRequiredCPUAttribs = map[string]string{}
var archRequiredKernelModules = map[string]kernelModule{ var archRequiredKernelModules = map[string]kernelModule{
"kvm": { "kvm": {
desc: "Kernel-based Virtual Machine", desc: "Kernel-based Virtual Machine",
required: true,
}, },
"vhost": { "vhost": {
desc: "Host kernel accelerator for virtio", desc: "Host kernel accelerator for virtio",
required: true,
}, },
"vhost_net": { "vhost_net": {
desc: "Host kernel accelerator for virtio network", desc: "Host kernel accelerator for virtio network",
required: true,
},
"vhost_vsock": {
desc: "Host Support for Linux VM Sockets",
required: false,
}, },
} }

View File

@ -43,9 +43,15 @@ var archRequiredCPUAttribs = map[string]string{}
var archRequiredKernelModules = map[string]kernelModule{ var archRequiredKernelModules = map[string]kernelModule{
"kvm": { "kvm": {
desc: "Kernel-based Virtual Machine", desc: "Kernel-based Virtual Machine",
required: true,
}, },
"kvm_hv": { "kvm_hv": {
desc: "Kernel-based Virtual Machine hardware virtualization", desc: "Kernel-based Virtual Machine hardware virtualization",
required: true,
},
"vhost_vsock": {
desc: "Host Support for Linux VM Sockets",
required: false,
}, },
} }

View File

@ -34,6 +34,11 @@ var archRequiredCPUAttribs = map[string]string{}
var archRequiredKernelModules = map[string]kernelModule{ var archRequiredKernelModules = map[string]kernelModule{
"kvm": { "kvm": {
desc: "Kernel-based Virtual Machine", desc: "Kernel-based Virtual Machine",
required: true,
},
"vhost_vsock": {
desc: "Host Support for Linux VM Sockets",
required: false,
}, },
} }

View File

@ -502,6 +502,7 @@ func TestCheckCheckKernelModules(t *testing.T) {
"foo": { "foo": {
desc: "desc", desc: "desc",
parameters: map[string]string{}, parameters: map[string]string{},
required: true,
}, },
"bar": { "bar": {
desc: "desc", desc: "desc",
@ -511,6 +512,7 @@ func TestCheckCheckKernelModules(t *testing.T) {
"param3": "a", "param3": "a",
"param4": ".", "param4": ".",
}, },
required: true,
}, },
} }
@ -570,6 +572,7 @@ func TestCheckCheckKernelModulesUnreadableFile(t *testing.T) {
parameters: map[string]string{ parameters: map[string]string{
"param1": "wibble", "param1": "wibble",
}, },
required: true,
}, },
} }
@ -617,6 +620,7 @@ func TestCheckCheckKernelModulesInvalidFileContents(t *testing.T) {
parameters: map[string]string{ parameters: map[string]string{
"param1": "wibble", "param1": "wibble",
}, },
required: true,
}, },
} }
@ -713,6 +717,7 @@ func TestCheckKernelParamHandler(t *testing.T) {
"foo": { "foo": {
desc: "desc", desc: "desc",
parameters: map[string]string{}, parameters: map[string]string{},
required: true,
}, },
"bar": { "bar": {
desc: "desc", desc: "desc",
@ -720,6 +725,7 @@ func TestCheckKernelParamHandler(t *testing.T) {
"param1": "hello", "param1": "hello",
"param2": "world", "param2": "world",
}, },
required: true,
}, },
} }
@ -731,6 +737,7 @@ func TestCheckKernelParamHandler(t *testing.T) {
parameters: map[string]string{ parameters: map[string]string{
"param1": "moo", "param1": "moo",
}, },
required: true,
}, },
} }
@ -740,6 +747,7 @@ func TestCheckKernelParamHandler(t *testing.T) {
parameters: map[string]string{ parameters: map[string]string{
"param1": "bar", "param1": "bar",
}, },
required: true,
}, },
} }

View File

@ -719,13 +719,10 @@ func TestMinimalRuntimeConfigWithVsock(t *testing.T) {
[agent.kata] [agent.kata]
` `
orgVHostVSockDevicePath := utils.VHostVSockDevicePath orgVHostVSockDevicePath := utils.VHostVSockDevicePath
orgVSockDevicePath := utils.VSockDevicePath
defer func() { defer func() {
utils.VHostVSockDevicePath = orgVHostVSockDevicePath utils.VHostVSockDevicePath = orgVHostVSockDevicePath
utils.VSockDevicePath = orgVSockDevicePath
}() }()
utils.VHostVSockDevicePath = "/dev/null" utils.VHostVSockDevicePath = "/dev/null"
utils.VSockDevicePath = "/dev/null"
configPath := path.Join(dir, "runtime.toml") configPath := path.Join(dir, "runtime.toml")
err = createConfig(configPath, runtimeMinimalConfig) err = createConfig(configPath, runtimeMinimalConfig)
@ -765,13 +762,10 @@ func TestNewQemuHypervisorConfig(t *testing.T) {
disableBlock := true disableBlock := true
enableIOThreads := true enableIOThreads := true
hotplugVFIOOnRootBus := true hotplugVFIOOnRootBus := true
orgVSockDevicePath := utils.VSockDevicePath
orgVHostVSockDevicePath := utils.VHostVSockDevicePath orgVHostVSockDevicePath := utils.VHostVSockDevicePath
defer func() { defer func() {
utils.VSockDevicePath = orgVSockDevicePath
utils.VHostVSockDevicePath = orgVHostVSockDevicePath utils.VHostVSockDevicePath = orgVHostVSockDevicePath
}() }()
utils.VSockDevicePath = "/dev/abc/xyz"
utils.VHostVSockDevicePath = "/dev/abc/xyz" utils.VHostVSockDevicePath = "/dev/abc/xyz"
hypervisor := hypervisor{ hypervisor := hypervisor{
@ -808,7 +802,6 @@ func TestNewQemuHypervisorConfig(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
utils.VSockDevicePath = "/dev/null"
utils.VHostVSockDevicePath = "/dev/null" utils.VHostVSockDevicePath = "/dev/null"
// all paths exist now // all paths exist now

View File

@ -29,9 +29,6 @@ const MibToBytesShift = 20
// 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 // VHostVSockDevicePath path to vhost-vsock device
var VHostVSockDevicePath = "/dev/vhost-vsock" var VHostVSockDevicePath = "/dev/vhost-vsock"
@ -234,10 +231,6 @@ func BuildSocketPath(elements ...string) (string, error) {
// SupportsVsocks returns true if vsocks are supported, otherwise false // SupportsVsocks returns true if vsocks are supported, otherwise false
func SupportsVsocks() bool { func SupportsVsocks() bool {
if _, err := os.Stat(VSockDevicePath); err != nil {
return false
}
if _, err := os.Stat(VHostVSockDevicePath); err != nil { if _, err := os.Stat(VHostVSockDevicePath); err != nil {
return false return false
} }

View File

@ -298,25 +298,14 @@ func TestBuildSocketPath(t *testing.T) {
func TestSupportsVsocks(t *testing.T) { func TestSupportsVsocks(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
orgVSockDevicePath := VSockDevicePath
orgVHostVSockDevicePath := VHostVSockDevicePath orgVHostVSockDevicePath := VHostVSockDevicePath
defer func() { defer func() {
VSockDevicePath = orgVSockDevicePath
VHostVSockDevicePath = orgVHostVSockDevicePath VHostVSockDevicePath = orgVHostVSockDevicePath
}() }()
VSockDevicePath = "/abc/xyz/123"
VHostVSockDevicePath = "/abc/xyz/123" VHostVSockDevicePath = "/abc/xyz/123"
assert.False(SupportsVsocks()) 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") vHostVSockFile, err := ioutil.TempFile("", "vhost-vsock")
assert.NoError(err) assert.NoError(err)
defer os.Remove(vHostVSockFile.Name()) defer os.Remove(vHostVSockFile.Name())