Skip to content

Commit

Permalink
preflight: Import existing VM from system libvirt connection
Browse files Browse the repository at this point in the history
This includes:

* Deleting the `crc` VM from system libvirt.
* Ensuring the disk image has the current user as owner.
  • Loading branch information
zeenix authored and cfergeau committed Dec 20, 2019
1 parent 0863cb2 commit 84229ed
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/crc/cmd/config/config_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
)
46 changes: 46 additions & 0 deletions pkg/crc/preflight/preflight_checks_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 7 additions & 0 deletions pkg/crc/preflight/preflight_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 84229ed

Please sign in to comment.