2021-08-23 09:14:53 -04:00
|
|
|
//go:build darwin
|
2016-05-06 06:03:01 -04:00
|
|
|
// +build darwin
|
|
|
|
|
|
|
|
// Package kernel provides helper function to get, parse and compare kernel
|
|
|
|
// versions for different platforms.
|
2018-02-05 16:05:59 -05:00
|
|
|
package kernel // import "github.com/docker/docker/pkg/parsers/kernel"
|
2016-05-06 06:03:01 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetKernelVersion gets the current kernel version.
|
|
|
|
func GetKernelVersion() (*VersionInfo, error) {
|
2020-07-14 08:03:10 -04:00
|
|
|
osName, err := getSPSoftwareDataType()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
release, err := getRelease(osName)
|
2016-05-06 06:03:01 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ParseRelease(release)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getRelease uses `system_profiler SPSoftwareDataType` to get OSX kernel version
|
2020-07-14 08:03:10 -04:00
|
|
|
func getRelease(osName string) (string, error) {
|
2016-05-06 06:03:01 -04:00
|
|
|
var release string
|
2020-07-14 08:03:10 -04:00
|
|
|
data := strings.Split(osName, "\n")
|
2016-05-06 06:03:01 -04:00
|
|
|
for _, line := range data {
|
2020-07-14 08:03:10 -04:00
|
|
|
if !strings.Contains(line, "Kernel Version") {
|
|
|
|
continue
|
2016-05-06 06:03:01 -04:00
|
|
|
}
|
2020-07-14 08:03:10 -04:00
|
|
|
// It has the format like ' Kernel Version: Darwin 14.5.0'
|
|
|
|
content := strings.SplitN(line, ":", 2)
|
|
|
|
if len(content) != 2 {
|
|
|
|
return "", fmt.Errorf("Kernel Version is invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
prettyNames := strings.SplitN(strings.TrimSpace(content[1]), " ", 2)
|
|
|
|
|
|
|
|
if len(prettyNames) != 2 {
|
|
|
|
return "", fmt.Errorf("Kernel Version needs to be 'Darwin x.x.x' ")
|
|
|
|
}
|
|
|
|
release = prettyNames[1]
|
2016-05-06 06:03:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return release, nil
|
|
|
|
}
|
2020-07-14 08:03:10 -04:00
|
|
|
|
|
|
|
func getSPSoftwareDataType() (string, error) {
|
|
|
|
cmd := exec.Command("system_profiler", "SPSoftwareDataType")
|
|
|
|
osName, err := cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(osName), nil
|
|
|
|
}
|