diff --git a/cmd/crc/cmd/config/config_linux.go b/cmd/crc/cmd/config/config_linux.go index e814e35d04..157360c227 100644 --- a/cmd/crc/cmd/config/config_linux.go +++ b/cmd/crc/cmd/config/config_linux.go @@ -38,4 +38,6 @@ var ( SkipCheckNetworkManagerInstalled = cfg.AddSetting("skip-check-network-manager-installed", nil, []cfg.ValidationFnType{cfg.ValidateBool}, []cfg.SetFn{cfg.SuccessfullyApplied}) WarnCheckNetworkManagerRunning = cfg.AddSetting("warn-check-network-manager-running", nil, []cfg.ValidationFnType{cfg.ValidateBool}, []cfg.SetFn{cfg.SuccessfullyApplied}) SkipCheckNetworkManagerRunning = cfg.AddSetting("skip-check-network-manager-running", nil, []cfg.ValidationFnType{cfg.ValidateBool}, []cfg.SetFn{cfg.SuccessfullyApplied}) + WarnCheckSystemLibvirtVM = cfg.AddSetting("warn-check-system-libvirt-vm", nil, []cfg.ValidationFnType{cfg.ValidateBool}, []cfg.SetFn{cfg.SuccessfullyApplied}) + SkipCheckSystemLibvirtVM = cfg.AddSetting("skip-check-system-libvirt-vm", nil, []cfg.ValidationFnType{cfg.ValidateBool}, []cfg.SetFn{cfg.SuccessfullyApplied}) ) diff --git a/pkg/crc/preflight/preflight_checks_linux.go b/pkg/crc/preflight/preflight_checks_linux.go index fd273dc30f..99ebceb000 100644 --- a/pkg/crc/preflight/preflight_checks_linux.go +++ b/pkg/crc/preflight/preflight_checks_linux.go @@ -547,3 +547,49 @@ func CheckNetworkManagerIsRunning() error { func fixNetworkManagerIsRunning() error { return fmt.Errorf("NetworkManager is required. Please make sure it is installed and running manually") } + +func checkSystemLibvirtVM() error { + logging.Debug("Checking for existing VM on libvirt's system connection") + stdOut, stdErr, err := crcos.RunWithDefaultLocale("virsh", "--connect", "qemu:///system", "list", "--all") + if err != nil { + return fmt.Errorf("%+v: %s", err, stdErr) + } + outputSlice := strings.Split(stdOut, "\n") + for _, stdOut = range outputSlice { + stdOut = strings.TrimSpace(stdOut) + if strings.HasPrefix(stdOut, "crc") { + logging.Debug("Found existing VM on libvirt system connection") + return errors.New("Existing VM on system libvirt connection") + } + } + return nil +} + +func fixSystemLibvirtVM() error { + logging.Debug("Deleting existing VM from libvirt's system connection") + _, stdErr, err := crcos.RunWithDefaultLocale("virsh", "--connect", "qemu:///system", "delete", "crc") + if err != nil { + // VM is probably not running, that's OK. + logging.Debug("Failed to stop `crc` VM: %+v: %s", err, stdErr) + } + _, stdErr, err = crcos.RunWithDefaultLocale("virsh", "--connect", "qemu:///system", "undefine", "crc") + if err != nil { + return fmt.Errorf("%+v: %s", err, stdErr) + } + + // Ensure disk is owned by current user + + // FIXME: First check if owner isn't already correctly set, even though if this function is called, it likely means + // it's not. + currentUser, err := user.Current() + if err != nil { + return fmt.Errorf("Error fetching current user info: %+v: %s", err, stdErr) + } + diskPath := filepath.Join(constants.MachineBaseDir, "machines", constants.DefaultName, constants.DefaultName) + err = crcos.ChownAsRoot(currentUser, diskPath) + if err != nil { + return err + } + + return nil +} diff --git a/pkg/crc/preflight/preflight_linux.go b/pkg/crc/preflight/preflight_linux.go index 7b844e4c9c..9900022c13 100644 --- a/pkg/crc/preflight/preflight_linux.go +++ b/pkg/crc/preflight/preflight_linux.go @@ -115,6 +115,13 @@ var libvirtPreflightChecks = [...]PreflightCheck{ fixDescription: "Writing dnsmasq config for crc", fix: fixCrcDnsmasqConfigFile, }, + { + configKeySuffix: "check-system-libvirt-vm", + checkDescription: "Checking for existing `crc` VM on system libvirt connection", + check: checkSystemLibvirtVM, + fixDescription: "Importing existing `crc` VM", + fix: fixSystemLibvirtVM, + }, } func getPreflightChecks() []PreflightCheck {