From f3bab52df4c6b54218ed36ac354ffc3573d31f3b Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Wed, 15 May 2013 17:40:47 -0700 Subject: [PATCH] Move getKernelVersion to utils package --- runtime.go | 2 +- utils.go | 8 ------ utils/uname_darwin.go | 10 +++++++ utils/uname_linux.go | 15 +++++++++++ utils/utils.go | 62 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 utils/uname_darwin.go create mode 100644 utils/uname_linux.go diff --git a/runtime.go b/runtime.go index ad48cb6c75..3c190e934e 100644 --- a/runtime.go +++ b/runtime.go @@ -251,7 +251,7 @@ func NewRuntime(autoRestart bool) (*Runtime, error) { return nil, err } - if k, err := GetKernelVersion(); err != nil { + if k, err := utils.GetKernelVersion(); err != nil { log.Printf("WARNING: %s\n", err) } else { runtime.kernelVersion = k diff --git a/utils.go b/utils.go index 980dfd4c72..d67f50e526 100644 --- a/utils.go +++ b/utils.go @@ -1,9 +1,5 @@ package docker -import ( - "github.com/dotcloud/docker/utils" -) - // Compare two Config struct. Do not compare the "Image" nor "Hostname" fields // If OpenStdin is set, then it differs func CompareConfig(a, b *Config) bool { @@ -51,7 +47,3 @@ func CompareConfig(a, b *Config) bool { return true } - -func GetKernelVersion() (*utils.KernelVersionInfo, error) { - return getKernelVersion() -} diff --git a/utils/uname_darwin.go b/utils/uname_darwin.go new file mode 100644 index 0000000000..d799554bbd --- /dev/null +++ b/utils/uname_darwin.go @@ -0,0 +1,10 @@ +package utils + +import ( + "errors" + "syscall" +) + +func uname() (*syscall.Utsname, error) { + return nil, errors.New("Kernel version detection is not available on darwin") +} diff --git a/utils/uname_linux.go b/utils/uname_linux.go new file mode 100644 index 0000000000..675a89b001 --- /dev/null +++ b/utils/uname_linux.go @@ -0,0 +1,15 @@ +package utils + +import ( + "syscall" +) + +// FIXME: Move this to utils package +func uname() (*syscall.Utsname, error) { + uts := &syscall.Utsname{} + + if err := syscall.Uname(uts); err != nil { + return nil, err + } + return uts, nil +} diff --git a/utils/utils.go b/utils/utils.go index 5e9543be8a..88d0c87f5c 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -14,6 +14,7 @@ import ( "os/exec" "path/filepath" "runtime" + "strconv" "strings" "sync" "time" @@ -468,3 +469,64 @@ func FindCgroupMountpoint(cgroupType string) (string, error) { return "", fmt.Errorf("cgroup mountpoint not found for %s", cgroupType) } + +func GetKernelVersion() (*KernelVersionInfo, error) { + var ( + flavor string + kernel, major, minor int + 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)] + + tmp := strings.SplitN(string(release), "-", 2) + tmp2 := strings.SplitN(tmp[0], ".", 3) + + if len(tmp2) > 0 { + kernel, err = strconv.Atoi(tmp2[0]) + if err != nil { + return nil, err + } + } + + if len(tmp2) > 1 { + major, err = strconv.Atoi(tmp2[1]) + if err != nil { + return nil, err + } + } + + if len(tmp2) > 2 { + minor, err = strconv.Atoi(tmp2[2]) + if err != nil { + return nil, err + } + } + + if len(tmp) == 2 { + flavor = tmp[1] + } else { + flavor = "" + } + + return &KernelVersionInfo{ + Kernel: kernel, + Major: major, + Minor: minor, + Flavor: flavor, + }, nil +}