Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preflight: Ensure crc network is accessible from session libvirt #709

Closed
wants to merge 6 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
preflight: Import existing VM from system libvirt connection
This includes:

* Deleting the `crc` VM from system libvirt.
* Ensuring the disk image has the current user as owner.
zeenix committed Jan 16, 2020
commit ae0ba7d35dbeccc86867ba7c27e8159f814d487f
2 changes: 2 additions & 0 deletions cmd/crc/cmd/config/config_linux.go
Original file line number Diff line number Diff line change
@@ -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})
)
47 changes: 47 additions & 0 deletions pkg/crc/preflight/preflight_checks_linux.go
Original file line number Diff line number Diff line change
@@ -547,3 +547,50 @@ 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)
}
regex := regexp.MustCompile(`- +crc`)

outputSlice := strings.Split(stdOut, "\n")
for _, stdOut = range outputSlice {
if regex.Match([]byte(stdOut)) {
return errors.New("Existing VM on system libvirt connection")
}
}
logging.Debug("No existing existing VM on libvirt system 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
@@ -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 {