github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/pkg/parsers/kernel/kernel_unix.go (about)

     1  // +build linux freebsd openbsd
     2  
     3  // Package kernel provides helper function to get, parse and compare kernel
     4  // versions for different platforms.
     5  package kernel // import "github.com/demonoid81/moby/pkg/parsers/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  	// Remove the \x00 from the release for Atoi to parse correctly
    21  	return ParseRelease(string(uts.Release[:bytes.IndexByte(uts.Release[:], 0)]))
    22  }
    23  
    24  // CheckKernelVersion checks if current kernel is newer than (or equal to)
    25  // the given version.
    26  func CheckKernelVersion(k, major, minor int) bool {
    27  	if v, err := GetKernelVersion(); err != nil {
    28  		logrus.Warnf("error getting kernel version: %s", err)
    29  	} else {
    30  		if CompareKernelVersion(*v, VersionInfo{Kernel: k, Major: major, Minor: minor}) < 0 {
    31  			return false
    32  		}
    33  	}
    34  	return true
    35  }