Skip to content

Commit

Permalink
Merge pull request kubevirt#6236 from kubevirt-bot/cherry-pick-6190-t…
Browse files Browse the repository at this point in the history
…o-release-0.44

[release-0.44] Avoid setting ownership for /dev/kvm in emulation mode
  • Loading branch information
kubevirt-bot authored Aug 12, 2021
2 parents ea26d36 + 431c191 commit 01805c7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 26 deletions.
18 changes: 18 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package util

import (
"fmt"
"os"
"strings"

v1 "kubevirt.io/client-go/api/v1"
Expand Down Expand Up @@ -81,6 +82,23 @@ func NeedVirtioNetDevice(vmi *v1.VirtualMachineInstance, useEmulation bool) bool
return false
}

// UseSoftwareEmulationForDevice determines whether to fallback to software emulation for the given device.
// This happens when the given device doesn't exist, and software emulation is enabled.
func UseSoftwareEmulationForDevice(devicePath string, useEmulation bool) (bool, error) {
if !useEmulation {
return false, nil
}

_, err := os.Stat(devicePath)
if err == nil {
return false, nil
}
if os.IsNotExist(err) {
return true, nil
}
return false, err
}

func ResourceNameToEnvVar(prefix string, resourceName string) string {
varName := strings.ToUpper(resourceName)
varName = strings.Replace(varName, "/", "_", -1)
Expand Down
31 changes: 20 additions & 11 deletions pkg/virt-handler/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2414,13 +2414,9 @@ func (d *VirtualMachineController) vmUpdateHelperMigrationTarget(origVMI *v1.Vir

}

res, err := d.podIsolationDetector.Detect(vmi)
err = d.claimKVMDeviceOwnership(vmi)
if err != nil {
return err
}

if err := diskutils.DefaultOwnershipManager.SetFileOwnership(path.Join(res.MountRoot(), "dev", "kvm")); err != nil {
return err
return fmt.Errorf("failed to set up file ownership for /dev/kvm: %v", err)
}

if virtutil.IsNonRootVMI(vmi) {
Expand Down Expand Up @@ -2504,12 +2500,9 @@ func (d *VirtualMachineController) vmUpdateHelperDefault(origVMI *v1.VirtualMach

}

res, err := d.podIsolationDetector.Detect(vmi)
err = d.claimKVMDeviceOwnership(vmi)
if err != nil {
return err
}
if err := diskutils.DefaultOwnershipManager.SetFileOwnership(path.Join(res.MountRoot(), "dev", "kvm")); err != nil {
return err
return fmt.Errorf("failed to set up file ownership for /dev/kvm: %v", err)
}

if virtutil.IsNonRootVMI(vmi) {
Expand Down Expand Up @@ -2772,6 +2765,22 @@ func (d *VirtualMachineController) isHostModelMigratable(vmi *v1.VirtualMachineI
return nil
}

func (d *VirtualMachineController) claimKVMDeviceOwnership(vmi *v1.VirtualMachineInstance) error {
isolation, err := d.podIsolationDetector.Detect(vmi)
if err != nil {
return err
}

kvmPath := path.Join(isolation.MountRoot(), "dev", "kvm")

softwareEmulation, err := util.UseSoftwareEmulationForDevice(kvmPath, d.clusterConfig.IsUseEmulation())
if err != nil || softwareEmulation {
return err
}

return diskutils.DefaultOwnershipManager.SetFileOwnership(kvmPath)
}

func nodeHasHostModelLabel(node *k8sv1.Node) bool {
for key, _ := range node.Labels {
if strings.HasPrefix(key, v1.HostModelCPULabel) {
Expand Down
32 changes: 17 additions & 15 deletions pkg/virt-launcher/virtwrap/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1136,26 +1136,28 @@ func Convert_v1_VirtualMachineInstance_To_api_Domain(vmi *v1.VirtualMachineInsta
CPUs: cpuCount,
}

if _, err := os.Stat("/dev/kvm"); os.IsNotExist(err) {
if c.UseEmulation {
logger := log.DefaultLogger()
logger.Infof("Hardware emulation device '/dev/kvm' not present. Using software emulation.")
domain.Spec.Type = "qemu"
} else {
return fmt.Errorf("hardware emulation device '/dev/kvm' not present")
}
kvmPath := "/dev/kvm"
if softwareEmulation, err := util.UseSoftwareEmulationForDevice(kvmPath, c.UseEmulation); err != nil {
return err
} else if softwareEmulation {
logger := log.DefaultLogger()
logger.Infof("Hardware emulation device '%s' not present. Using software emulation.", kvmPath)
domain.Spec.Type = "qemu"
} else if _, err := os.Stat(kvmPath); os.IsNotExist(err) {
return fmt.Errorf("hardware emulation device '%s' not present", kvmPath)
} else if err != nil {
return err
}

virtioNetProhibited := false
if _, err := os.Stat("/dev/vhost-net"); os.IsNotExist(err) {
if c.UseEmulation {
logger := log.DefaultLogger()
logger.Infof("In-kernel virtio-net device emulation '/dev/vhost-net' not present. Falling back to QEMU userland emulation.")
} else {
virtioNetProhibited = true
}
vhostNetPath := "/dev/vhost-net"
if softwareEmulation, err := util.UseSoftwareEmulationForDevice(vhostNetPath, c.UseEmulation); err != nil {
return err
} else if softwareEmulation {
logger := log.DefaultLogger()
logger.Infof("In-kernel virtio-net device emulation '%s' not present. Falling back to QEMU userland emulation.", vhostNetPath)
} else if _, err := os.Stat(vhostNetPath); os.IsNotExist(err) {
virtioNetProhibited = true
} else if err != nil {
return err
}
Expand Down

0 comments on commit 01805c7

Please sign in to comment.