Fix loading of kvm/kvm_intel/kvm_amd modules#1454
Fix loading of kvm/kvm_intel/kvm_amd modules#1454praveenkumar merged 4 commits intocrc-org:masterfrom praveenkumar:issue_1449
Conversation
|
[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 DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
| } | ||
| re := regexp.MustCompile(`flags.*:.*`) | ||
|
|
||
| flags := re.FindString(string(out)) |
There was a problem hiding this comment.
Maybe use this library https://godoc.org/github.com/shirou/gopsutil/cpu#InfoStat ?
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
if we merge #1396, we will have this library included 👼
There was a problem hiding this comment.
@guillaumerose Ah, may be we will merge it soon :)
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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)`) |
There was a problem hiding this comment.
This could almost use strings.Contains() to be consistent with the fixup method.
There was a problem hiding this comment.
@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") |
| } | ||
| re := regexp.MustCompile(`flags.*:.*`) | ||
|
|
||
| flags := re.FindString(string(out)) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@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) |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Ah, this belongs to the previous commit where I asked to make the spacing consistent 👍
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
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
If you manually execute the
sudo modprobe kvmthen the start will fail