From 397dce609e48b4de56d4839d6aadc98110524239 Mon Sep 17 00:00:00 2001 From: Pramodh Pallapothu Date: Mon, 29 Apr 2024 10:23:13 -0700 Subject: [PATCH] Refactor code in hypervisor.go Signed-off-by: Pramodh Pallapothu --- pkg/pillar/hypervisor/hypervisor.go | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/pkg/pillar/hypervisor/hypervisor.go b/pkg/pillar/hypervisor/hypervisor.go index 527662dbfc..ee9aa11776 100644 --- a/pkg/pillar/hypervisor/hypervisor.go +++ b/pkg/pillar/hypervisor/hypervisor.go @@ -11,6 +11,7 @@ import ( "github.com/lf-edge/eve/pkg/pillar/base" "github.com/lf-edge/eve/pkg/pillar/types" + fileutils "github.com/lf-edge/eve/pkg/pillar/utils/file" uuid "github.com/satori/go.uuid" "github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/mem" @@ -38,17 +39,17 @@ type Hypervisor interface { type hypervisorDesc struct { constructor func() Hypervisor - dom0handle string + enabled func() bool hvTypeFileContent string } var knownHypervisors = map[string]hypervisorDesc{ - XenHypervisorName: {constructor: newXen, dom0handle: "/proc/xen", hvTypeFileContent: "xen"}, - KVMHypervisorName: {constructor: newKvm, dom0handle: "/dev/kvm", hvTypeFileContent: "kvm"}, - KubevirtHypervisorName: {constructor: newKubevirt, dom0handle: "/dev/kvm", hvTypeFileContent: "kubevirt"}, - ACRNHypervisorName: {constructor: newAcrn, dom0handle: "/dev/acrn", hvTypeFileContent: "acrn"}, - ContainerdHypervisorName: {constructor: newContainerd, dom0handle: "/run/containerd/containerd.sock"}, - NullHypervisorName: {constructor: newNull, dom0handle: "/"}, + XenHypervisorName: {constructor: newXen, enabled: func() bool { return fileutils.FileExists(nil, "/proc/xen") }, hvTypeFileContent: "xen"}, + KVMHypervisorName: {constructor: newKvm, enabled: func() bool { return fileutils.FileExists(nil, "/dev/kvm") && !base.IsHVTypeKube() }, hvTypeFileContent: "kvm"}, + KubevirtHypervisorName: {constructor: newKubevirt, enabled: func() bool { return fileutils.FileExists(nil, "/dev/kvm") && base.IsHVTypeKube() }, hvTypeFileContent: "kubevirt"}, + ACRNHypervisorName: {constructor: newAcrn, enabled: func() bool { return fileutils.FileExists(nil, "/dev/acrn") }, hvTypeFileContent: "acrn"}, + ContainerdHypervisorName: {constructor: newContainerd, enabled: func() bool { return fileutils.FileExists(nil, "/run/containerd/containerd.sock") }}, + NullHypervisorName: {constructor: newNull, enabled: func() bool { return fileutils.DirExists(nil, "/") }}, } // this is a priority order to pick a default hypervisor if multiple are available (more to less likely) @@ -96,18 +97,9 @@ func BootTimeHypervisor() Hypervisor { // the advice of this function and always ask for the enabled one. func GetAvailableHypervisors() (all []string, enabled []string) { all = hypervisorPriority - isHVTypeKube := base.IsHVTypeKube() for _, v := range all { - if _, err := os.Stat(knownHypervisors[v].dom0handle); err == nil { - // Both Kubevirt and KVM use same dom0handle. - // Lets differentiate by eve_flavor - if isHVTypeKube && strings.Compare(v, KVMHypervisorName) == 0 { - continue // kubevirt image don't set kvm - } else if !isHVTypeKube && strings.Compare(v, KubevirtHypervisorName) == 0 { - continue // kvm image don't set kubevirt - } else { - enabled = append(enabled, v) - } + if knownHypervisors[v].enabled() { + enabled = append(enabled, v) } } return