|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | + |
| 3 | +// Copyright (C) 2022 The Go Authors. All rights reserved. |
| 4 | +// Copyright (C) 2025 SUSE LLC. All rights reserved. |
| 5 | +// Use of this source code is governed by a BSD-style |
| 6 | +// license that can be found in the LICENSE.BSD file. |
| 7 | + |
| 8 | +// The parsing logic is very loosely based on the Go stdlib's |
| 9 | +// src/internal/syscall/unix/kernel_version_linux.go but with an API that looks |
| 10 | +// a bit like runc's libcontainer/system/kernelversion. |
| 11 | + |
| 12 | +// Package kernelversion provides a simple mechanism for checking whether the |
| 13 | +// running kernel is at least as new as some baseline kernel version. This is |
| 14 | +// often useful when checking for features that would be too complicated to |
| 15 | +// test support for (or in cases where we know that some kernel features in |
| 16 | +// backport-heavy kernels are broken and need to be avoided). |
| 17 | +package kernelversion |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "strconv" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "golang.org/x/sys/unix" |
| 27 | + |
| 28 | + "github.com/moby/sys/kernelversion/internal/gocompat" |
| 29 | +) |
| 30 | + |
| 31 | +// KernelVersion is a numeric representation of the key numerical elements of a |
| 32 | +// kernel version (for instance, "4.1.2-default-1" would be represented as |
| 33 | +// KernelVersion{4, 1, 2}). |
| 34 | +type KernelVersion []uint64 |
| 35 | + |
| 36 | +func (kver KernelVersion) String() string { |
| 37 | + var str strings.Builder |
| 38 | + for idx, elem := range kver { |
| 39 | + if idx != 0 { |
| 40 | + _, _ = str.WriteRune('.') |
| 41 | + } |
| 42 | + _, _ = str.WriteString(strconv.FormatUint(elem, 10)) |
| 43 | + } |
| 44 | + return str.String() |
| 45 | +} |
| 46 | + |
| 47 | +var errInvalidKernelVersion = errors.New("invalid kernel version") |
| 48 | + |
| 49 | +// parseKernelVersion parses a string and creates a KernelVersion based on it. |
| 50 | +func parseKernelVersion(kverStr string) (KernelVersion, error) { |
| 51 | + kver := make(KernelVersion, 1, 3) |
| 52 | + for idx, ch := range kverStr { |
| 53 | + if '0' <= ch && ch <= '9' { |
| 54 | + v := &kver[len(kver)-1] |
| 55 | + *v = (*v * 10) + uint64(ch-'0') |
| 56 | + } else { |
| 57 | + if idx == 0 || kverStr[idx-1] < '0' || '9' < kverStr[idx-1] { |
| 58 | + // "." must be preceded by a digit while in version section |
| 59 | + return nil, fmt.Errorf("%w %q: kernel version has dot(s) followed by non-digit in version section", errInvalidKernelVersion, kverStr) |
| 60 | + } |
| 61 | + if ch != '.' { |
| 62 | + break |
| 63 | + } |
| 64 | + kver = append(kver, 0) |
| 65 | + } |
| 66 | + } |
| 67 | + if len(kver) < 2 { |
| 68 | + return nil, fmt.Errorf("%w %q: kernel versions must contain at least two components", errInvalidKernelVersion, kverStr) |
| 69 | + } |
| 70 | + return kver, nil |
| 71 | +} |
| 72 | + |
| 73 | +// getKernelVersion gets the current kernel version. |
| 74 | +var getKernelVersion = gocompat.SyncOnceValues(func() (KernelVersion, error) { |
| 75 | + var uts unix.Utsname |
| 76 | + if err := unix.Uname(&uts); err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + // Remove the \x00 from the release. |
| 80 | + release := uts.Release[:] |
| 81 | + return parseKernelVersion(string(release[:bytes.IndexByte(release, 0)])) |
| 82 | +}) |
| 83 | + |
| 84 | +// GreaterEqualThan returns true if the the host kernel version is greater than |
| 85 | +// or equal to the provided [KernelVersion]. When doing this comparison, any |
| 86 | +// non-numerical suffixes of the host kernel version are ignored. |
| 87 | +// |
| 88 | +// If the number of components provided is not equal to the number of numerical |
| 89 | +// components of the host kernel version, any missing components are treated as |
| 90 | +// 0. This means that GreaterEqualThan(KernelVersion{4}) will be treated the |
| 91 | +// same as GreaterEqualThan(KernelVersion{4, 0, 0, ..., 0, 0}), and that if the |
| 92 | +// host kernel version is "4" then GreaterEqualThan(KernelVersion{4, 1}) will |
| 93 | +// return false (because the host version will be treated as "4.0"). |
| 94 | +func GreaterEqualThan(wantKver KernelVersion) (bool, error) { |
| 95 | + hostKver, err := getKernelVersion() |
| 96 | + if err != nil { |
| 97 | + return false, err |
| 98 | + } |
| 99 | + |
| 100 | + // Pad out the kernel version lengths to match one another. |
| 101 | + cmpLen := gocompat.Max2(len(hostKver), len(wantKver)) |
| 102 | + hostKver = append(hostKver, make(KernelVersion, cmpLen-len(hostKver))...) |
| 103 | + wantKver = append(wantKver, make(KernelVersion, cmpLen-len(wantKver))...) |
| 104 | + |
| 105 | + for i := 0; i < cmpLen; i++ { |
| 106 | + switch gocompat.CmpCompare(hostKver[i], wantKver[i]) { |
| 107 | + case -1: |
| 108 | + // host < want |
| 109 | + return false, nil |
| 110 | + case +1: |
| 111 | + // host > want |
| 112 | + return true, nil |
| 113 | + case 0: |
| 114 | + continue |
| 115 | + } |
| 116 | + } |
| 117 | + // equal version values |
| 118 | + return true, nil |
| 119 | +} |
0 commit comments