2015-05-08 21:59:38 +00:00
|
|
|
// +build !windows
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// Package kernel provides helper function to get, parse and compare kernel
|
|
|
|
// versions for different platforms.
|
2014-07-29 00:23:38 +00:00
|
|
|
package kernel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// VersionInfo holds information about the kernel.
|
|
|
|
type VersionInfo struct {
|
|
|
|
Kernel int // Version of the kernel (e.g. 4.1.2-generic -> 4)
|
|
|
|
Major int // Major part of the kernel version (e.g. 4.1.2-generic -> 1)
|
|
|
|
Minor int // Minor part of the kernel version (e.g. 4.1.2-generic -> 2)
|
|
|
|
Flavor string // Flavor of the kernel version (e.g. 4.1.2-generic -> generic)
|
2014-07-29 00:23:38 +00:00
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
func (k *VersionInfo) String() string {
|
2014-07-29 00:23:38 +00:00
|
|
|
return fmt.Sprintf("%d.%d.%d%s", k.Kernel, k.Major, k.Minor, k.Flavor)
|
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// CompareKernelVersion compares two kernel.VersionInfo structs.
|
2014-07-29 00:23:38 +00:00
|
|
|
// Returns -1 if a < b, 0 if a == b, 1 it a > b
|
2015-07-25 08:35:07 +00:00
|
|
|
func CompareKernelVersion(a, b VersionInfo) int {
|
2014-07-29 00:23:38 +00:00
|
|
|
if a.Kernel < b.Kernel {
|
|
|
|
return -1
|
|
|
|
} else if a.Kernel > b.Kernel {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.Major < b.Major {
|
|
|
|
return -1
|
|
|
|
} else if a.Major > b.Major {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.Minor < b.Minor {
|
|
|
|
return -1
|
|
|
|
} else if a.Minor > b.Minor {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// GetKernelVersion gets the current kernel version.
|
|
|
|
func GetKernelVersion() (*VersionInfo, error) {
|
2014-07-29 00:23:38 +00:00
|
|
|
var (
|
|
|
|
err 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))
|
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// ParseRelease parses a string and creates a VersionInfo based on it.
|
|
|
|
func ParseRelease(release string) (*VersionInfo, error) {
|
2014-07-29 00:23:38 +00:00
|
|
|
var (
|
|
|
|
kernel, major, minor, parsed int
|
|
|
|
flavor, partial string
|
|
|
|
)
|
|
|
|
|
|
|
|
// Ignore error from Sscanf to allow an empty flavor. Instead, just
|
|
|
|
// make sure we got all the version numbers.
|
|
|
|
parsed, _ = fmt.Sscanf(release, "%d.%d%s", &kernel, &major, &partial)
|
|
|
|
if parsed < 2 {
|
|
|
|
return nil, errors.New("Can't parse kernel version " + release)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sometimes we have 3.12.25-gentoo, but sometimes we just have 3.12-1-amd64
|
|
|
|
parsed, _ = fmt.Sscanf(partial, ".%d%s", &minor, &flavor)
|
|
|
|
if parsed < 1 {
|
|
|
|
flavor = partial
|
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
return &VersionInfo{
|
2014-07-29 00:23:38 +00:00
|
|
|
Kernel: kernel,
|
|
|
|
Major: major,
|
|
|
|
Minor: minor,
|
|
|
|
Flavor: flavor,
|
|
|
|
}, nil
|
|
|
|
}
|