diff --git a/pkg/pillar/cmd/domainmgr/domainmgr.go b/pkg/pillar/cmd/domainmgr/domainmgr.go index dff759a41fb..0bd3940d3a0 100644 --- a/pkg/pillar/cmd/domainmgr/domainmgr.go +++ b/pkg/pillar/cmd/domainmgr/domainmgr.go @@ -133,20 +133,9 @@ func (ctx *domainContext) publishAssignableAdapters() { ctx.pubAssignableAdapters.Publish("global", *ctx.assignableAdapters) } -var currentHypervisor hypervisor.Hypervisor -var currentHypervisorMutex sync.Mutex var logger *logrus.Logger var log *base.LogObject -// CurrentHypervisor returns the current hypervisor -func CurrentHypervisor() hypervisor.Hypervisor { - currentHypervisorMutex.Lock() - hv := currentHypervisor - currentHypervisorMutex.Unlock() - - return hv -} - func Run(ps *pubsub.PubSub, loggerArg *logrus.Logger, logArg *base.LogObject, arguments []string) int { logger = loggerArg log = logArg @@ -176,9 +165,6 @@ func Run(ps *pubsub.PubSub, loggerArg *logrus.Logger, logArg *base.LogObject, ar fmt.Printf("%s: %s\n", agentName, Version) return 0 } - currentHypervisorMutex.Lock() - currentHypervisor, err = hypervisor.GetHypervisor(*domainCtx.hypervisorPtr) - currentHypervisorMutex.Unlock() if err != nil { log.Fatal(err) } @@ -186,7 +172,7 @@ func Run(ps *pubsub.PubSub, loggerArg *logrus.Logger, logArg *base.LogObject, ar if err := pidfile.CheckAndCreatePidfile(log, agentName); err != nil { log.Fatal(err) } - log.Functionf("Starting %s with %s hypervisor backend", agentName, CurrentHypervisor().Name()) + log.Functionf("Starting %s with %s hypervisor backend", agentName, hypervisor.CurrentHypervisor().Name()) // Run a periodic timer so we always update StillRunning stillRunning := time.NewTicker(25 * time.Second) @@ -483,7 +469,7 @@ func Run(ps *pubsub.PubSub, loggerArg *logrus.Logger, logArg *base.LogObject, ar capabilitiesSent := false capabilitiesTicker := time.NewTicker(5 * time.Second) - if err := getAndPublishCapabilities(&domainCtx, CurrentHypervisor()); err != nil { + if err := getAndPublishCapabilities(&domainCtx, hypervisor.CurrentHypervisor()); err != nil { log.Warnf("getAndPublishCapabilities: %v", err) } else { capabilitiesSent = true @@ -491,7 +477,7 @@ func Run(ps *pubsub.PubSub, loggerArg *logrus.Logger, logArg *base.LogObject, ar } log.Functionf("Creating %s at %s", "metricsTimerTask", agentlog.GetMyStack()) - go metricsTimerTask(&domainCtx, CurrentHypervisor()) + go metricsTimerTask(&domainCtx, hypervisor.CurrentHypervisor()) // Before starting to process DomainConfig, domainmgr should (in this order): // 1. wait for NIM to publish DNS to learn which ports are used for management @@ -527,7 +513,7 @@ func Run(ps *pubsub.PubSub, loggerArg *logrus.Logger, logArg *base.LogObject, ar publishProcessesHandler(&domainCtx) case <-capabilitiesTicker.C: - if err := getAndPublishCapabilities(&domainCtx, CurrentHypervisor()); err != nil { + if err := getAndPublishCapabilities(&domainCtx, hypervisor.CurrentHypervisor()); err != nil { log.Warnf("getAndPublishCapabilities: %v", err) } else { capabilitiesSent = true @@ -553,7 +539,7 @@ func Run(ps *pubsub.PubSub, loggerArg *logrus.Logger, logArg *base.LogObject, ar var resources types.HostMemory for i := 0; true; i++ { delay := 10 - resources, err = CurrentHypervisor().GetHostCPUMem() + resources, err = hypervisor.CurrentHypervisor().GetHostCPUMem() if err == nil { break } @@ -941,7 +927,7 @@ func verifyStatus(ctx *domainContext, status *types.DomainStatus) { configActivate = true } - domainID, domainStatus, err := CurrentHypervisor().Task(status).Info(status.DomainName) + domainID, domainStatus, err := hypervisor.CurrentHypervisor().Task(status).Info(status.DomainName) if err != nil || domainStatus == types.HALTED { if status.Activated && configActivate { if err == nil { @@ -974,10 +960,10 @@ func verifyStatus(ctx *domainContext, status *types.DomainStatus) { } //cleanup app instance tasks - if err := CurrentHypervisor().Task(status).Delete(status.DomainName); err != nil { + if err := hypervisor.CurrentHypervisor().Task(status).Delete(status.DomainName); err != nil { log.Errorf("failed to delete domain: %s (%v)", status.DomainName, err) } - if err := CurrentHypervisor().Task(status).Cleanup(status.DomainName); err != nil { + if err := hypervisor.CurrentHypervisor().Task(status).Cleanup(status.DomainName); err != nil { log.Errorf("failed to cleanup domain: %s (%v)", status.DomainName, err) } } @@ -1115,7 +1101,7 @@ func maybeRetryBoot(ctx *domainContext, status *types.DomainStatus) { } defer file.Close() - if err := CurrentHypervisor().Task(status).Setup(*status, *config, ctx.assignableAdapters, nil, file); err != nil { + if err := hypervisor.CurrentHypervisor().Task(status).Setup(*status, *config, ctx.assignableAdapters, nil, file); err != nil { //it is retry, so omit error log.Errorf("Failed to create DomainStatus from %v: %s", config, err) @@ -1394,14 +1380,14 @@ func doAssignIoAdaptersToDomain(ctx *domainContext, config types.DomainConfig, } for i, long := range assignmentsPci { - err := CurrentHypervisor().PCIReserve(long) + err := hypervisor.CurrentHypervisor().PCIReserve(long) if err != nil { // Undo what we assigned for j, long := range assignmentsPci { if j >= i { break } - CurrentHypervisor().PCIRelease(long) + hypervisor.CurrentHypervisor().PCIRelease(long) } if publishAssignableAdapters { ctx.publishAssignableAdapters() @@ -1506,7 +1492,7 @@ func doActivate(ctx *domainContext, config types.DomainConfig, defer file.Close() globalConfig := agentlog.GetGlobalConfig(log, ctx.subGlobalConfig) - if err := CurrentHypervisor().Task(status).Setup(*status, config, ctx.assignableAdapters, globalConfig, file); err != nil { + if err := hypervisor.CurrentHypervisor().Task(status).Setup(*status, config, ctx.assignableAdapters, globalConfig, file); err != nil { log.Errorf("Failed to create DomainStatus from %v: %s", config, err) status.SetErrorNow(err.Error()) @@ -1565,17 +1551,17 @@ func doActivateTail(ctx *domainContext, status *types.DomainStatus, status.State = types.BOOTING publishDomainStatus(ctx, status) - err := CurrentHypervisor().Task(status).Start(status.DomainName) + err := hypervisor.CurrentHypervisor().Task(status).Start(status.DomainName) if err != nil { log.Errorf("domain start for %s: %s", status.DomainName, err) status.SetErrorNow(err.Error()) // Delete - if err := CurrentHypervisor().Task(status).Delete(status.DomainName); err != nil { + if err := hypervisor.CurrentHypervisor().Task(status).Delete(status.DomainName); err != nil { log.Errorf("failed to delete domain: %s (%v)", status.DomainName, err) } // Cleanup - if err := CurrentHypervisor().Task(status).Cleanup(status.DomainName); err != nil { + if err := hypervisor.CurrentHypervisor().Task(status).Cleanup(status.DomainName); err != nil { log.Errorf("failed to cleanup domain: %s (%v)", status.DomainName, err) } @@ -1589,7 +1575,7 @@ func doActivateTail(ctx *domainContext, status *types.DomainStatus, status.VifList = checkIfEmu(status.VifList) status.State = types.RUNNING - domainID, state, err := CurrentHypervisor().Task(status).Info(status.DomainName) + domainID, state, err := hypervisor.CurrentHypervisor().Task(status).Info(status.DomainName) if err != nil { // Immediate failure treat as above @@ -1600,11 +1586,11 @@ func doActivateTail(ctx *domainContext, status *types.DomainStatus, log.Errorf("doActivateTail(%v) failed for %s: %s", status.UUIDandVersion, status.DisplayName, err) // Delete - if err := CurrentHypervisor().Task(status).Delete(status.DomainName); err != nil { + if err := hypervisor.CurrentHypervisor().Task(status).Delete(status.DomainName); err != nil { log.Errorf("failed to delete domain: %s (%v)", status.DomainName, err) } // Cleanup - if err := CurrentHypervisor().Task(status).Cleanup(status.DomainName); err != nil { + if err := hypervisor.CurrentHypervisor().Task(status).Cleanup(status.DomainName); err != nil { log.Errorf("failed to cleanup domain: %s (%v)", status.DomainName, err) } return @@ -1649,7 +1635,7 @@ func doInactivate(ctx *domainContext, status *types.DomainStatus, impatient bool log.Functionf("doInactivate(%v) for %s domainId %d", status.UUIDandVersion, status.DisplayName, status.DomainId) - domainID, _, err := CurrentHypervisor().Task(status).Info(status.DomainName) + domainID, _, err := hypervisor.CurrentHypervisor().Task(status).Info(status.DomainName) if err == nil && domainID != status.DomainId { status.DomainId = domainID status.BootTime = time.Now() @@ -1723,7 +1709,7 @@ func doInactivate(ctx *domainContext, status *types.DomainStatus, impatient bool } if status.DomainId != 0 { - if err := CurrentHypervisor().Task(status).Delete(status.DomainName); err != nil { + if err := hypervisor.CurrentHypervisor().Task(status).Delete(status.DomainName); err != nil { log.Errorf("Failed to delete domain %s (%v)", status.DomainName, err) } else { log.Functionf("doInactivate(%v) for %s: Delete succeeded", @@ -1741,7 +1727,7 @@ func doInactivate(ctx *domainContext, status *types.DomainStatus, impatient bool } func doCleanup(ctx *domainContext, status *types.DomainStatus) { - if err := CurrentHypervisor().Task(status).Cleanup(status.DomainName); err != nil { + if err := hypervisor.CurrentHypervisor().Task(status).Cleanup(status.DomainName); err != nil { log.Errorf("failed to cleanup domain: %s (%v)", status.DomainName, err) } @@ -1845,7 +1831,7 @@ func releaseAdapters(ctx *domainContext, ioAdapterList []types.IoAdapter, checkIoBundleAll(ctx) } for _, long := range assignments { - err := CurrentHypervisor().PCIRelease(long) + err := hypervisor.CurrentHypervisor().PCIRelease(long) if err != nil && !ignoreErrors { status.SetErrorNow(err.Error()) } @@ -2196,7 +2182,7 @@ func waitForDomainGone(status types.DomainStatus, maxDelay time.Duration) bool { time.Sleep(delay) waited += delay } - _, state, err := CurrentHypervisor().Task(&status).Info(status.DomainName) + _, state, err := hypervisor.CurrentHypervisor().Task(&status).Info(status.DomainName) if err != nil { log.Errorf("waitForDomainGone(%v) for %s error %s state %s", status.UUIDandVersion, status.DisplayName, @@ -2284,7 +2270,7 @@ func DomainCreate(ctx *domainContext, status types.DomainStatus) (int, error) { log.Errorf("DomainCreate(%s) no DomainConfig", status.Key()) return 0, fmt.Errorf("DomainCreate(%s) no DomainConfig", status.Key()) } - domainID, err = CurrentHypervisor().Task(&status).Create(status.DomainName, filename, config) + domainID, err = hypervisor.CurrentHypervisor().Task(&status).Create(status.DomainName, filename, config) return domainID, err } @@ -2297,7 +2283,7 @@ func DomainShutdown(status types.DomainStatus, force bool) error { // Stop the domain log.Functionf("Stopping domain - %s", status.DomainName) - err = CurrentHypervisor().Task(&status).Stop(status.DomainName, force) + err = hypervisor.CurrentHypervisor().Task(&status).Stop(status.DomainName, force) return err } @@ -2755,7 +2741,7 @@ func handlePhysicalIOAdapterListImpl(ctxArg interface{}, key string, len(aa.IoBundleList)) // check for mismatched PCI-ids and assignment groups and mark as errors - aa.CheckBadAssignmentGroups(log, CurrentHypervisor().PCISameController) + aa.CheckBadAssignmentGroups(log, hypervisor.CurrentHypervisor().PCISameController) for i := range aa.IoBundleList { ib := &aa.IoBundleList[i] log.Functionf("handlePhysicalIOAdapterListImpl: new Adapter: %+v", @@ -2800,7 +2786,7 @@ func handlePhysicalIOAdapterListImpl(ctxArg interface{}, key string, aa.AddOrUpdateIoBundle(log, *ib) // check for mismatched PCI-ids and assignment groups and mark as errors - aa.CheckBadAssignmentGroups(log, CurrentHypervisor().PCISameController) + aa.CheckBadAssignmentGroups(log, hypervisor.CurrentHypervisor().PCISameController) // Lookup since it could have changed ib = aa.LookupIoBundlePhylabel(ib.Phylabel) updatePortAndPciBackIoBundle(ctx, ib) @@ -2901,7 +2887,7 @@ func updatePortAndPciBackIoBundle(ctx *domainContext, ib *types.IoBundle) (chang // expand list to include other PCI functions on the same PCI controller // since they need to be treated as part of the same bundle even if the // EVE controller doesn't know it - list = aa.ExpandControllers(log, list, CurrentHypervisor().PCISameController) + list = aa.ExpandControllers(log, list, hypervisor.CurrentHypervisor().PCISameController) for _, ib := range list { if types.IsPort(ctx.deviceNetworkStatus, ib.Ifname) { isPort = true @@ -2987,7 +2973,7 @@ func updatePortAndPciBackIoMember(ctx *domainContext, ib *types.IoBundle, isPort if ib.PciLong != "" { log.Functionf("updatePortAndPciBackIoMember: Removing %s (%s) from pciback", ib.Phylabel, ib.PciLong) - err = CurrentHypervisor().PCIRelease(ib.PciLong) + err = hypervisor.CurrentHypervisor().PCIRelease(ib.PciLong) if err != nil { err = fmt.Errorf("adapter %s (group %s, type %d) PCI ID %s; not released by hypervisor: %v", ib.Phylabel, ib.AssignmentGroup, ib.Type, @@ -3034,7 +3020,7 @@ func updatePortAndPciBackIoMember(ctx *domainContext, ib *types.IoBundle, isPort } else if ib.PciLong != "" && ib.UsbAddr == "" { log.Noticef("Assigning %s (%s) to pciback", ib.Phylabel, ib.PciLong) - err := CurrentHypervisor().PCIReserve(ib.PciLong) + err := hypervisor.CurrentHypervisor().PCIReserve(ib.PciLong) if err != nil { return changed, err } @@ -3275,7 +3261,7 @@ func handleIBDelete(ctx *domainContext, phylabel string) { log.Functionf("handleIBDelete: Assigning %s (%s) back", ib.Phylabel, ib.PciLong) if ib.PciLong != "" { - err := CurrentHypervisor().PCIRelease(ib.PciLong) + err := hypervisor.CurrentHypervisor().PCIRelease(ib.PciLong) if err != nil { log.Errorf("handleIBDelete(%d %s %s) PCIRelease %s failed %v", ib.Type, ib.Phylabel, ib.AssignmentGroup, ib.PciLong, err) diff --git a/pkg/pillar/cmd/usbmanager/usbmanager.go b/pkg/pillar/cmd/usbmanager/usbmanager.go index 7ff3b262414..6d1597860d8 100644 --- a/pkg/pillar/cmd/usbmanager/usbmanager.go +++ b/pkg/pillar/cmd/usbmanager/usbmanager.go @@ -10,7 +10,6 @@ import ( "github.com/lf-edge/eve/pkg/pillar/agentbase" "github.com/lf-edge/eve/pkg/pillar/base" - "github.com/lf-edge/eve/pkg/pillar/cmd/domainmgr" "github.com/lf-edge/eve/pkg/pillar/hypervisor" "github.com/lf-edge/eve/pkg/pillar/pubsub" "github.com/lf-edge/eve/pkg/pillar/utils" @@ -84,8 +83,7 @@ func Run(ps *pubsub.PubSub, loggerArg *logrus.Logger, logArg *base.LogObject, ar var subs []pubsub.Subscription - currentHypervisor := domainmgr.CurrentHypervisor() - fmt.Fprintf(os.Stderr, "BBBBB hv: %+v (%T)\n", currentHypervisor, currentHypervisor) + currentHypervisor := hypervisor.CurrentHypervisor() _, ok := currentHypervisor.(hypervisor.KvmContext) if ok { usbCtx.subscribe(ps) diff --git a/pkg/pillar/hypervisor/hypervisor.go b/pkg/pillar/hypervisor/hypervisor.go index d874dd0e05a..23def564856 100644 --- a/pkg/pillar/hypervisor/hypervisor.go +++ b/pkg/pillar/hypervisor/hypervisor.go @@ -4,14 +4,37 @@ package hypervisor import ( + "flag" "fmt" + "log" + "os" + "github.com/lf-edge/eve/pkg/pillar/types" "github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/mem" "github.com/sirupsen/logrus" - "os" ) +var currentHypervisor Hypervisor + +func init() { + var err error + + flagSet := flag.NewFlagSet("", flag.ExitOnError) + allHypervisors, enabledHypervisors := GetAvailableHypervisors() + hypervisorPtr := flagSet.String("h", enabledHypervisors[0], fmt.Sprintf("Current hypervisor %+q", allHypervisors)) + + currentHypervisor, err = GetHypervisor(*hypervisorPtr) + if err != nil { + log.Fatal(err) + } +} + +// CurrentHypervisor returns the current hypervisor +func CurrentHypervisor() Hypervisor { + return currentHypervisor +} + // Hypervisor provides methods for manipulating domains on the host type Hypervisor interface { Name() string