Skip to content

Commit

Permalink
Move getKernelVersion to utils package
Browse files Browse the repository at this point in the history
  • Loading branch information
creack committed May 16, 2013
1 parent 10e19e4 commit f3bab52
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 9 deletions.
2 changes: 1 addition & 1 deletion runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions utils.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -51,7 +47,3 @@ func CompareConfig(a, b *Config) bool {

return true
}

func GetKernelVersion() (*utils.KernelVersionInfo, error) {
return getKernelVersion()
}
10 changes: 10 additions & 0 deletions utils/uname_darwin.go
Original file line number Diff line number Diff line change
@@ -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")
}
15 changes: 15 additions & 0 deletions utils/uname_linux.go
Original file line number Diff line number Diff line change
@@ -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
}
62 changes: 62 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -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
}

0 comments on commit f3bab52

Please sign in to comment.