| 1 | |
| 2 | |
| 3 | |
| 4 | package qemu |
| 5 | |
| 6 | import ( |
| 7 | "errors" |
| 8 | "io/fs" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "strings" |
| 12 | |
| 13 | "github.com/coreos/go-semver/semver" |
| 14 | "github.com/sirupsen/logrus" |
| 15 | |
| 16 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 17 | ) |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | func nestedVirtualizationEnabled(arch, accel, cpu string, _ *semver.Version) (bool, error) { |
| 26 | if arch != limatype.X8664 { |
| 27 | logrus.Warnf("field `nestedVirtualization` is not supported for architecture %#q, ignoring", arch) |
| 28 | return false, nil |
| 29 | } |
| 30 | if accel != "kvm" { |
| 31 | logrus.Warnf("field `nestedVirtualization` is not supported with accelerator %#q for architecture %#q, ignoring", accel, arch) |
| 32 | return false, nil |
| 33 | } |
| 34 | for _, module := range []string{"kvm_intel", "kvm_amd"} { |
| 35 | b, err := os.ReadFile(filepath.Join("/sys/module", module, "parameters", "nested")) |
| 36 | if err != nil { |
| 37 | if errors.Is(err, fs.ErrNotExist) { |
| 38 | continue |
| 39 | } |
| 40 | logrus.WithError(err).Warn("Failed to check whether nested virtualization is enabled in the host KVM module") |
| 41 | return false, nil |
| 42 | } |
| 43 | enabled, err := parseKVMNestedParam(string(b)) |
| 44 | if err != nil { |
| 45 | return false, err |
| 46 | } |
| 47 | if !enabled { |
| 48 | return false, errors.New("nested virtualization is disabled in the host KVM module (`nested` parameter of kvm_intel / kvm_amd)") |
| 49 | } |
| 50 | if !strings.HasPrefix(cpu, "host") { |
| 51 | logrus.Warnf("nested virtualization on x86_64 requires CPU type `host`, got %#q", cpu) |
| 52 | } |
| 53 | return true, nil |
| 54 | } |
| 55 | return false, errors.New("neither kvm_intel nor kvm_amd is loaded") |
| 56 | } |