Skip to content

Commit d6c54b3

Browse files
committed
source/kernel: Add kvm availability
This commit adds a feature to check kvm availability. Signed-off-by: Seunguk Shin <[email protected]> Reviewed-by: Nick Connolly <[email protected]>
1 parent 3496b27 commit d6c54b3

File tree

7 files changed

+58
-0
lines changed

7 files changed

+58
-0
lines changed

deployment/base/nfd-crds/cr-sample.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ spec:
4747
- feature: kernel.selinux
4848
matchExpressions:
4949
enabled: {op: IsFalse}
50+
- feature: kernel.kvm
51+
matchExpressions:
52+
enabled: {op: IsFalse}
5053
- feature: kernel.version
5154
matchExpressions:
5255
major: {op: In, value: ["5"]}

deployment/components/worker-config/nfd-worker.conf.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@
116116
# - feature: kernel.selinux
117117
# matchExpressions:
118118
# enabled: {op: IsFalse}
119+
# - feature: kernel.kvm
120+
# matchExpressions:
121+
# enabled: {op: IsFalse}
119122
# - feature: kernel.version
120123
# matchExpressions:
121124
# major: {op: In, value: ["5"]}

deployment/helm/node-feature-discovery/values.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,9 @@ worker:
464464
# - feature: kernel.selinux
465465
# matchExpressions:
466466
# enabled: {op: IsFalse}
467+
# - feature: kernel.kvm
468+
# matchExpressions:
469+
# enabled: {op: IsFalse}
467470
# - feature: kernel.version
468471
# matchExpressions:
469472
# major: {op: In, value: ["5"]}

docs/usage/customization-guide.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,8 @@ The following features are available for matching:
962962
| | | **`mod-name`** | | Kernel module `<mod-name>` is loaded |
963963
| **`kernel.selinux`** | attribute | | | Kernel SELinux related features |
964964
| | | **`enabled`** | bool | `true` if SELinux has been enabled and is in enforcing mode, otherwise `false` |
965+
| **`kernel.kvm`** | attribute | | | Kernel KVM related features |
966+
| | | **`enabled`** | bool | `true` if KVM has been enabled, otherwise `false` |
965967
| **`kernel.version`** | attribute | | | Kernel version information |
966968
| | | **`full`** | string | Full kernel version (e.g. ‘4.5.6-7-g123abcde') |
967969
| | | **`major`** | int | First component of the kernel version (e.g. ‘4') |

source/kernel/kernel.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const (
3636
SelinuxFeature = "selinux"
3737
VersionFeature = "version"
3838
EnabledModuleFeature = "enabledmodule"
39+
KvmFeature = "kvm"
3940
)
4041

4142
// Configuration file options
@@ -159,6 +160,13 @@ func (s *kernelSource) Discover() error {
159160
s.features.Attributes[SelinuxFeature].Elements["enabled"] = strconv.FormatBool(selinux)
160161
}
161162

163+
if kvm, err := KvmEnabled(); err != nil {
164+
klog.ErrorS(err, "failed to detect kvm status")
165+
} else {
166+
s.features.Attributes[KvmFeature] = nfdv1alpha1.NewAttributeFeatures(nil)
167+
s.features.Attributes[KvmFeature].Elements["enabled"] = strconv.FormatBool(kvm)
168+
}
169+
162170
klog.V(3).InfoS("discovered features", "featureSource", s.Name(), "features", utils.DelayedDumper(s.features))
163171

164172
return nil

source/kernel/kvm.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright 2017-2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package kernel
18+
19+
import (
20+
"os"
21+
22+
"k8s.io/klog/v2"
23+
24+
"sigs.k8s.io/node-feature-discovery/pkg/utils/hostpath"
25+
)
26+
27+
// KvmEnabled detects if kvm has been enabled in the kernel
28+
func KvmEnabled() (bool, error) {
29+
_, err := os.Stat(hostpath.SysfsDir.Path("devices/virtual/misc/kvm"))
30+
if err != nil {
31+
if os.IsNotExist(err) {
32+
klog.V(1).InfoS("kvm not available on the system")
33+
return false, nil
34+
}
35+
return false, err
36+
}
37+
return true, nil
38+
}

test/e2e/e2e-test-config.example.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ defaultFeatures:
5252
- "feature.node.kubernetes.io/kernel-config.NO_HZ_IDLE"
5353
- "feature.node.kubernetes.io/kernel-config.PREEMPT"
5454
- "feature.node.kubernetes.io/kernel-selinux.enabled"
55+
- "feature.node.kubernetes.io/kernel-kvm.enabled"
5556
- "feature.node.kubernetes.io/kernel-version.full"
5657
- "feature.node.kubernetes.io/kernel-version.major"
5758
- "feature.node.kubernetes.io/kernel-version.minor"

0 commit comments

Comments
 (0)