Skip to content

Fix loading of kvm/kvm_intel/kvm_amd modules#1454

Merged
praveenkumar merged 4 commits intocrc-org:masterfrom
praveenkumar:issue_1449
Aug 17, 2020
Merged

Fix loading of kvm/kvm_intel/kvm_amd modules#1454
praveenkumar merged 4 commits intocrc-org:masterfrom
praveenkumar:issue_1449

Conversation

@praveenkumar
Copy link
Member

Fixes: Issue #1449

Solution/Idea

Enable the kvm_intel and kvm_amd kernel module according to processor.

Testing

With current master following will fail to start the crc

$ sudo modprobe -r kvm_intel
$ $ lsmod | grep kvm
$ crc setup
DEBU Trying to load kvm module                    
DEBU exit status 1 : modprobe: ERROR: could not insert 'kvm': Operation not permitted 
FATA Failed to load kvm module

If you manually execute the sudo modprobe kvm then the start will fail

$ sudo modprobe kvm
$ $ lsmod | grep kvm
kvm                   815104  0
irqbypass              16384  1 kvm
$ ./crc setup
$ crc start
...
DEBU Checking if /dev/kvm exists                  
DEBU kvm kernel module is not loaded              
FATA kvm kernel module is not loaded  

@openshift-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: praveenkumar

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

}
re := regexp.MustCompile(`flags.*:.*`)

flags := re.FindString(string(out))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guillaumerose this is what I also suggested to @cfergeau but since our usecase is not that much to include this complete library as dependency so I went without it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we merge #1396, we will have this library included 👼

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guillaumerose Ah, may be we will merge it soon :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Praveen mentioned it, but it's indeed a bit overkill for what we are doing. No strong feeling either way if it's already an indirect dependency.

func fixKvmEnabled() error {
logging.Debug("Trying to load kvm module")
stdOut, stdErr, err := crcos.RunWithPrivilege("Load kvm kernel module","modprobe", "kvm")
stdOut, stdErr, err := crcos.RunWithPrivilege("Load kvm kernel module", "modprobe", "kvm")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually you could remove loading of kvm (contrary to what the commit log is saying), kvm_intel/kvm_amd will trigger the load of the kvm module if needed.

}

re = regexp.MustCompile(`(vmx|svm)`)
re := regexp.MustCompile(`(vmx|svm)`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could almost use strings.Contains() to be consistent with the fixup method.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cfergeau I then need to use 2 different strings.Contains since it doesn't allow the regex :(

return fmt.Errorf("Failed to load kvm amd module: %s %v: %s", stdOut, err, stdErr)
}
default:
logging.Debug("Not able to detect processor details")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unable

}
re := regexp.MustCompile(`flags.*:.*`)

flags := re.FindString(string(out))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Praveen mentioned it, but it's indeed a bit overkill for what we are doing. No strong feeling either way if it's already an indirect dependency.

if err != nil {
logging.Debugf("%v : %s", err, buf.String())
return fmt.Errorf("Failed to load kvm module")
return fmt.Errorf("Failed to load kvm module: %s %v: %s", stdOut, err, stdErr)
Copy link
Contributor

@cfergeau cfergeau Aug 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fwiw, what would be nice would be to define an RunError type

type RunError struct {
    stdOut string
    stdErr string
    err Error
}

and make use of return fmt.Errorf("Failed to load kvm module: %w", runError)
(see https://blog.golang.org/go1.13-errors )
This way we keep a relatively structured error, in case higher layers don't want to show stdOut/stdErr on errors, or on the contrary, want to show it, but split on multiple lines, .. This probably also means the stdErr return value can be removed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cfergeau I think then this should go as part of runCmd where we can wrap err and stderr as part of struct.

// #nosec G204
cmd = exec.Command("virsh", "--connect", "qemu:///system", "net-undefine", libvirt.DefaultNetwork)
_ = cmd.Run()
_,_,_ = crcos.RunWithDefaultLocale("virsh", "--connect", "qemu:///system", "net-undefine", libvirt.DefaultNetwork)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make the spacing consistent with the previous call?

func fixKvmEnabled() error {
logging.Debug("Trying to load kvm module")
stdOut, stdErr, err := crcos.RunWithPrivilege("Load kvm kernel module","modprobe", "kvm")
stdOut, stdErr, err := crcos.RunWithPrivilege("Load kvm kernel module", "modprobe", "kvm")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this to the initial commit, since this is when the missing space was introduced.

_, _, _ = crcos.RunWithDefaultLocale("virsh", "--connect", "qemu:///system", "net-destroy", libvirt.DefaultNetwork)
// #nosec G204
_,_,_ = crcos.RunWithDefaultLocale("virsh", "--connect", "qemu:///system", "net-undefine", libvirt.DefaultNetwork)
_, _, _ = crcos.RunWithDefaultLocale("virsh", "--connect", "qemu:///system", "net-undefine", libvirt.DefaultNetwork)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this belongs to the previous commit where I asked to make the spacing consistent 👍

@cfergeau cfergeau changed the title Issue 1449 Fix loading of kvm/kvm_intel/kvm_amd modules Aug 13, 2020
we should use our own implementation of command execution when possible
it provide us stdout, stderr and err instead using exec.Command where
we need to create buffer for stderr and stdout.
For intel we need to run `modprobe kvm_intel`
to get the file `/dev/kvm` and for amd `modprobe kvm_amd`, it will
also enable the `kvm` module.
With centos-8 there is no `kvm` package so remove it
@praveenkumar praveenkumar merged commit 2f8acc0 into crc-org:master Aug 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants