github.com/cdoern/storage@v1.12.13/pkg/parsers/kernel/kernel_unix.go (about)

     1  // +build linux freebsd solaris openbsd
     2  
     3  // Package kernel provides helper function to get, parse and compare kernel
     4  // versions for different platforms.
     5  package kernel
     6  
     7  import (
     8  	"bytes"
     9  
    10  	"github.com/sirupsen/logrus"
    11  )
    12  
    13  // GetKernelVersion gets the current kernel version.
    14  func GetKernelVersion() (*VersionInfo, error) {
    15  	uts, err := uname()
    16  	if err != nil {
    17  		return nil, err
    18  	}
    19  
    20  	release := make([]byte, len(uts.Release))
    21  
    22  	i := 0
    23  	for _, c := range uts.Release {
    24  		release[i] = byte(c)
    25  		i++
    26  	}
    27  
    28  	// Remove the \x00 from the release for Atoi to parse correctly
    29  	release = release[:bytes.IndexByte(release, 0)]
    30  
    31  	return ParseRelease(string(release))
    32  }
    33  
    34  // CheckKernelVersion checks if current kernel is newer than (or equal to)
    35  // the given version.
    36  func CheckKernelVersion(k, major, minor int) bool {
    37  	if v, err := GetKernelVersion(); err != nil {
    38  		logrus.Warnf("error getting kernel version: %s", err)
    39  	} else {
    40  		if CompareKernelVersion(*v, VersionInfo{Kernel: k, Major: major, Minor: minor}) < 0 {
    41  			return false
    42  		}
    43  	}
    44  	return true
    45  }