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

Add preflight check for hardware-assisted virtualization #644

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions pkg/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ func (c *Console) doRun() error {
preflight.CPUCheck{},
preflight.MemoryCheck{},
preflight.VirtCheck{},
preflight.KVMHostCheck{},
}
for _, c := range checks {
msg, err := c.Run()
Expand Down
11 changes: 11 additions & 0 deletions pkg/preflight/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"errors"
"fmt"
"io/fs"
"os"
"os/exec"
"strconv"
Expand All @@ -23,6 +24,7 @@ var (
// So that we can fake this stuff up for unit tests
execCommand = exec.Command
procMemInfo = "/proc/meminfo"
devKvm = "/dev/kvm"
)

// The Run() method of a preflight.Check returns a string. If the string
Expand All @@ -36,6 +38,7 @@ type Check interface {
type CPUCheck struct{}
type MemoryCheck struct{}
type VirtCheck struct{}
type KVMHostCheck struct{}

func (c CPUCheck) Run() (msg string, err error) {
out, err := execCommand("/usr/bin/nproc", "--all").Output()
Expand Down Expand Up @@ -118,3 +121,11 @@ func (c VirtCheck) Run() (msg string, err error) {
msg = fmt.Sprintf("System is virtualized (%s) which is not supported.", virt)
return
}

func (c KVMHostCheck) Run() (msg string, err error) {
if _, err = os.Stat(devKvm); errors.Is(err, fs.ErrNotExist) {
msg = "Harvester requires hardware-assisted virtualization, but /dev/kvm does not exist."
err = nil
}
return
}
18 changes: 18 additions & 0 deletions pkg/preflight/checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,21 @@ func TestMemoryCheck(t *testing.T) {
assert.Equal(t, expectedOutput, msg)
}
}

func TestKVMHostCheck(t *testing.T) {
defaultDevKvm := devKvm
defer func() { devKvm = defaultDevKvm }()

expectedOutputs := map[string]string{
"./testdata/dev-kvm-does-not-exist": "Harvester requires hardware-assisted virtualization, but /dev/kvm does not exist.",
"./testdata/dev-kvm": "",
}

check := KVMHostCheck{}
for file, expectedOutput := range expectedOutputs {
devKvm = file
msg, err := check.Run()
assert.Nil(t, err)
assert.Equal(t, expectedOutput, msg)
}
}
Empty file added pkg/preflight/testdata/dev-kvm
Empty file.
Loading