2017-03-26 16:47:18 -04:00
|
|
|
// +build linux freebsd solaris openbsd
|
2016-05-06 06:03:01 -04:00
|
|
|
|
|
|
|
// Package kernel provides helper function to get, parse and compare kernel
|
|
|
|
// versions for different platforms.
|
|
|
|
package kernel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-08-02 01:17:20 -04:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-05-06 06:03:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetKernelVersion gets the current kernel version.
|
|
|
|
func GetKernelVersion() (*VersionInfo, error) {
|
|
|
|
uts, err := uname()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
release := make([]byte, len(uts.Release))
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for _, c := range uts.Release {
|
|
|
|
release[i] = byte(c)
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the \x00 from the release for Atoi to parse correctly
|
|
|
|
release = release[:bytes.IndexByte(release, 0)]
|
|
|
|
|
|
|
|
return ParseRelease(string(release))
|
|
|
|
}
|
2016-08-02 01:17:20 -04:00
|
|
|
|
|
|
|
// CheckKernelVersion checks if current kernel is newer than (or equal to)
|
|
|
|
// the given version.
|
|
|
|
func CheckKernelVersion(k, major, minor int) bool {
|
|
|
|
if v, err := GetKernelVersion(); err != nil {
|
|
|
|
logrus.Warnf("error getting kernel version: %s", err)
|
|
|
|
} else {
|
|
|
|
if CompareKernelVersion(*v, VersionInfo{Kernel: k, Major: major, Minor: minor}) < 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|