mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-13 18:43:34 +00:00
util: move kernel version functions to pkg/util/kernel
Signed-off-by: Niels de Vos <ndevos@ibm.com>
This commit is contained in:
committed by
mergify[bot]
parent
542ed3de63
commit
15da101b1b
132
pkg/util/kernel/version.go
Normal file
132
pkg/util/kernel/version.go
Normal file
@ -0,0 +1,132 @@
|
||||
/*
|
||||
Copyright 2025 The Ceph-CSI Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package kernel
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
|
||||
"github.com/ceph/ceph-csi/internal/util/log"
|
||||
)
|
||||
|
||||
// GetKernelVersion returns the version of the running Unix (like) system from the
|
||||
// 'utsname' structs 'release' component.
|
||||
func GetKernelVersion() (string, error) {
|
||||
utsname := unix.Utsname{}
|
||||
if err := unix.Uname(&utsname); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return strings.TrimRight(string(utsname.Release[:]), "\x00"), nil
|
||||
}
|
||||
|
||||
// KernelVersion holds kernel related information.
|
||||
type KernelVersion struct {
|
||||
Version int
|
||||
PatchLevel int
|
||||
SubLevel int
|
||||
ExtraVersion int // prefix of the part after the first "-"
|
||||
Distribution string // component of full extraversion
|
||||
Backport bool // backport have a fixed version/patchlevel/sublevel
|
||||
}
|
||||
|
||||
// parseKernelRelease parses a kernel release version string into:
|
||||
// version, patch version, sub version and extra version.
|
||||
func parseKernelRelease(release string) (int, int, int, int, error) {
|
||||
version := 0
|
||||
patchlevel := 0
|
||||
minVersions := 2
|
||||
|
||||
extra := ""
|
||||
n, err := fmt.Sscanf(release, "%d.%d%s", &version, &patchlevel, &extra)
|
||||
if n < minVersions && err != nil {
|
||||
return 0, 0, 0, 0, fmt.Errorf("failed to parse version and patchlevel from %s: %w", release, err)
|
||||
}
|
||||
|
||||
sublevel := 0
|
||||
extraversion := 0
|
||||
if n > minVersions {
|
||||
n, err = fmt.Sscanf(extra, ".%d%s", &sublevel, &extra)
|
||||
if err != nil && n == 0 && extra != "" && extra[0] != '-' && extra[0] == '.' {
|
||||
return 0, 0, 0, 0, fmt.Errorf("failed to parse subversion from %s: %w", release, err)
|
||||
}
|
||||
|
||||
extra = strings.TrimPrefix(extra, "-")
|
||||
// ignore errors, 1st component of extraversion does not need to be an int
|
||||
_, err = fmt.Sscanf(extra, "%d", &extraversion)
|
||||
if err != nil {
|
||||
// "go lint" wants err to be checked...
|
||||
extraversion = 0
|
||||
}
|
||||
}
|
||||
|
||||
return version, patchlevel, sublevel, extraversion, nil
|
||||
}
|
||||
|
||||
// CheckKernelSupport checks the running kernel and comparing it to known
|
||||
// versions that have support for required features . Distributors of
|
||||
// enterprise Linux have backport quota support to previous versions. This
|
||||
// function checks if the running kernel is one of the versions that have the
|
||||
// feature/fixes backport.
|
||||
//
|
||||
// `uname -r` (or Uname().Utsname.Release has a format like 1.2.3-rc.vendor
|
||||
// This can be slit up in the following components: - version (1) - patchlevel
|
||||
// (2) - sublevel (3) - optional, defaults to 0 - extraversion (rc) - optional,
|
||||
// matching integers only - distribution (.vendor) - optional, match against
|
||||
// whole `uname -r` string
|
||||
//
|
||||
// For matching multiple versions, the kernelSupport type contains a backport
|
||||
// bool, which will cause matching
|
||||
// version+patchlevel+sublevel+(>=extraversion)+(~distribution)
|
||||
//
|
||||
// In case the backport bool is false, a simple check for higher versions than
|
||||
// version+patchlevel+sublevel is done.
|
||||
func CheckKernelSupport(release string, supportedVersions []KernelVersion) bool {
|
||||
version, patchlevel, sublevel, extraversion, err := parseKernelRelease(release)
|
||||
if err != nil {
|
||||
log.ErrorLogMsg("%v", err)
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// compare running kernel against known versions
|
||||
for _, kernel := range supportedVersions {
|
||||
if !kernel.Backport {
|
||||
// deal with the default case(s), find >= match for version, patchlevel, sublevel
|
||||
if version > kernel.Version || (version == kernel.Version && patchlevel > kernel.PatchLevel) ||
|
||||
(version == kernel.Version && patchlevel == kernel.PatchLevel && sublevel >= kernel.SubLevel) {
|
||||
return true
|
||||
}
|
||||
} else {
|
||||
// specific backport, match distribution initially
|
||||
if !strings.Contains(release, kernel.Distribution) {
|
||||
continue
|
||||
}
|
||||
|
||||
// strict match version, patchlevel, sublevel, and >= match extraversion
|
||||
if version == kernel.Version && patchlevel == kernel.PatchLevel &&
|
||||
sublevel == kernel.SubLevel && extraversion >= kernel.ExtraVersion {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
log.WarningLogMsg("kernel %s does not support required features", release)
|
||||
|
||||
return false
|
||||
}
|
144
pkg/util/kernel/version_test.go
Normal file
144
pkg/util/kernel/version_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
/*
|
||||
Copyright 2025 The Ceph-CSI Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package kernel
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetKernelVersion(t *testing.T) {
|
||||
t.Parallel()
|
||||
version, err := GetKernelVersion()
|
||||
if err != nil {
|
||||
t.Errorf("failed to get kernel version: %s", err)
|
||||
}
|
||||
if version == "" {
|
||||
t.Error("version is empty, this is unexpected?!")
|
||||
}
|
||||
if strings.HasSuffix(version, "\x00") {
|
||||
t.Error("version ends with \\x00 byte(s)")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseKernelRelease(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
badReleases := []string{"x", "5", "5.", "5.4.", "5.x-2-oops", "4.1.x-7-oh", "5.12.x"}
|
||||
for _, release := range badReleases {
|
||||
_, _, _, _, err := parseKernelRelease(release)
|
||||
if err == nil {
|
||||
t.Errorf("release %q must not be parsed successfully", release)
|
||||
}
|
||||
}
|
||||
|
||||
goodReleases := []string{
|
||||
"5.12", "5.12xlinux", "5.1-2-yam", "3.1-5-x", "5.12.14", "5.12.14xlinux",
|
||||
"5.12.14-xlinux", "5.12.14-99-x", "3.3x-3",
|
||||
}
|
||||
goodVersions := [][]int{
|
||||
{5, 12, 0, 0},
|
||||
{5, 12, 0, 0},
|
||||
{5, 1, 0, 2},
|
||||
{3, 1, 0, 5},
|
||||
{5, 12, 14, 0},
|
||||
{5, 12, 14, 0},
|
||||
{5, 12, 14, 0},
|
||||
{5, 12, 14, 99},
|
||||
{3, 3, 0, 0},
|
||||
}
|
||||
for i, release := range goodReleases {
|
||||
version, patchlevel, sublevel, extraversion, err := parseKernelRelease(release)
|
||||
if err != nil {
|
||||
t.Errorf("parsing error for release %q: %s", release, err)
|
||||
}
|
||||
good := goodVersions[i]
|
||||
if version != good[0] || patchlevel != good[1] || sublevel != good[2] || extraversion != good[3] {
|
||||
t.Errorf("release %q parsed incorrectly: expected (%d.%d.%d-%d), actual (%d.%d.%d-%d)",
|
||||
release, good[0], good[1], good[2], good[3],
|
||||
version, patchlevel, sublevel, extraversion)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckKernelSupport(t *testing.T) {
|
||||
t.Parallel()
|
||||
supportsQuota := []string{
|
||||
"4.17.0",
|
||||
"5.0.0",
|
||||
"4.17.0-rc1",
|
||||
"4.18.0-80.el8",
|
||||
"3.10.0-1062.el7.x86_64", // 1st backport
|
||||
"3.10.0-1062.4.1.el7.x86_64", // updated backport
|
||||
}
|
||||
|
||||
noQuota := []string{
|
||||
"2.6.32-754.15.3.el6.x86_64", // too old
|
||||
"3.10.0-123.el7.x86_64", // too old for backport
|
||||
"3.10.0-1062.4.1.el8.x86_64", // nonexisting RHEL-8 kernel
|
||||
"3.11.0-123.el7.x86_64", // nonexisting RHEL-7 kernel
|
||||
}
|
||||
|
||||
quotaSupport := []KernelVersion{
|
||||
{4, 17, 0, 0, "", false}, // standard 4.17+ versions
|
||||
{3, 10, 0, 1062, ".el7", true}, // RHEL-7.7
|
||||
}
|
||||
for _, kernel := range supportsQuota {
|
||||
ok := CheckKernelSupport(kernel, quotaSupport)
|
||||
if !ok {
|
||||
t.Errorf("support expected for %s", kernel)
|
||||
}
|
||||
}
|
||||
|
||||
for _, kernel := range noQuota {
|
||||
ok := CheckKernelSupport(kernel, quotaSupport)
|
||||
if ok {
|
||||
t.Errorf("no support expected for %s", kernel)
|
||||
}
|
||||
}
|
||||
|
||||
supportsDeepFlatten := []string{
|
||||
"5.1.0", // 5.1+ supports deep-flatten
|
||||
"5.3.0",
|
||||
"4.18.0-193.9.1.el8_2.x86_64", // RHEL 8.2 kernel
|
||||
}
|
||||
|
||||
noDeepFlatten := []string{
|
||||
"4.18.0", // too old
|
||||
"3.10.0-123.el7.x86_64", // too old for backport
|
||||
"3.10.0-1062.4.1.el8.x86_64", // nonexisting RHEL-8 kernel
|
||||
"3.11.0-123.el7.x86_64", // nonexisting RHEL-7 kernel
|
||||
}
|
||||
|
||||
deepFlattenSupport := []KernelVersion{
|
||||
{5, 1, 0, 0, "", false}, // standard 5.1+ versions
|
||||
{4, 18, 0, 193, ".el8", true}, // RHEL 8.2 backport
|
||||
}
|
||||
for _, kernel := range supportsDeepFlatten {
|
||||
ok := CheckKernelSupport(kernel, deepFlattenSupport)
|
||||
if !ok {
|
||||
t.Errorf("support expected for %s", kernel)
|
||||
}
|
||||
}
|
||||
|
||||
for _, kernel := range noDeepFlatten {
|
||||
ok := CheckKernelSupport(kernel, deepFlattenSupport)
|
||||
if ok {
|
||||
t.Errorf("no support expected for %s", kernel)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user