Avoid setting ownership for /dev/kvm in emulation mode
@@ -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) {
@@ -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) {
@@ -2772,6 +2765,28 @@ 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")
+ hasKVM, err := diskutils.FileExists(kvmPath)
+ if err != nil {
+ return err
+ }
+
+ // This is the logic applied by virt-launcher (see virtwrap/converter/converter.go)
+ // to determine whether to start the VM via KVM or QEMU.
+ needsKVM := hasKVM || !d.clusterConfig.IsUseEmulation()
+ if !needsKVM {
+ return nil
+ }
+
+ return diskutils.DefaultOwnershipManager.SetFileOwnership(kvmPath)
+}
+
func nodeHasHostModelLabel(node *k8sv1.Node) bool {
for key, _ := range node.Labels {
if strings.HasPrefix(key, v1.HostModelCPULabel) {
