From 49779b674af09b46c165c8dfe2e76054336b0595 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Sat, 14 Nov 2015 23:03:02 +0100 Subject: [PATCH] Add pkg/parsers/architecture and pkg/platform Signed-off-by: Vincent Demeester --- daemon/info.go | 5 +- docs/reference/api/docker_remote_api_v1.21.md | 2 +- docs/reference/commandline/info.md | 2 +- man/docker-info.1.md | 2 +- pkg/platform/architecture_freebsd.go | 15 ++++++ pkg/platform/architecture_linux.go | 28 ++++++++++ pkg/platform/architecture_windows.go | 52 +++++++++++++++++++ pkg/platform/platform.go | 23 ++++++++ 8 files changed, 124 insertions(+), 5 deletions(-) create mode 100644 pkg/platform/architecture_freebsd.go create mode 100644 pkg/platform/architecture_linux.go create mode 100644 pkg/platform/architecture_windows.go create mode 100644 pkg/platform/platform.go diff --git a/daemon/info.go b/daemon/info.go index 8f9f3afa97..9243675463 100644 --- a/daemon/info.go +++ b/daemon/info.go @@ -11,6 +11,7 @@ import ( "github.com/docker/docker/pkg/fileutils" "github.com/docker/docker/pkg/parsers/kernel" "github.com/docker/docker/pkg/parsers/operatingsystem" + "github.com/docker/docker/pkg/platform" "github.com/docker/docker/pkg/sysinfo" "github.com/docker/docker/pkg/system" "github.com/docker/docker/registry" @@ -75,8 +76,8 @@ func (daemon *Daemon) SystemInfo() (*types.Info, error) { KernelVersion: kernelVersion, OperatingSystem: operatingSystem, IndexServerAddress: registry.IndexServer, - OSType: runtime.GOOS, - Architecture: runtime.GOARCH, + OSType: platform.OSType, + Architecture: platform.Architecture, RegistryConfig: daemon.RegistryService.Config, InitSha1: dockerversion.InitSHA1, InitPath: initPath, diff --git a/docs/reference/api/docker_remote_api_v1.21.md b/docs/reference/api/docker_remote_api_v1.21.md index a53a6fcb80..c57edc9e71 100644 --- a/docs/reference/api/docker_remote_api_v1.21.md +++ b/docs/reference/api/docker_remote_api_v1.21.md @@ -1884,7 +1884,7 @@ Display system-wide information Content-Type: application/json { - "Architecture": "amd64", + "Architecture": "x86_64", "Containers": 11, "CpuCfsPeriod": true, "CpuCfsQuota": true, diff --git a/docs/reference/commandline/info.md b/docs/reference/commandline/info.md index e23d9d6780..c1f208418b 100644 --- a/docs/reference/commandline/info.md +++ b/docs/reference/commandline/info.md @@ -32,7 +32,7 @@ For example: Logging Driver: json-file Kernel Version: 3.19.0-22-generic OSType: linux - Architecture: amd64 + Architecture: x86_64 Operating System: Ubuntu 15.04 CPUs: 24 Total Memory: 62.86 GiB diff --git a/man/docker-info.1.md b/man/docker-info.1.md index 208d3423a3..4e451a3b3d 100644 --- a/man/docker-info.1.md +++ b/man/docker-info.1.md @@ -42,7 +42,7 @@ Here is a sample output: Kernel Version: 3.13.0-24-generic Operating System: Ubuntu 14.04 LTS OSType: linux - Architecture: amd64 + Architecture: x86_64 CPUs: 1 Total Memory: 2 GiB diff --git a/pkg/platform/architecture_freebsd.go b/pkg/platform/architecture_freebsd.go new file mode 100644 index 0000000000..e8bf398fa5 --- /dev/null +++ b/pkg/platform/architecture_freebsd.go @@ -0,0 +1,15 @@ +package platform + +import ( + "os/exec" +) + +// GetRuntimeArchitecture get the name of the current architecture (x86, x86_64, …) +func GetRuntimeArchitecture() (string, error) { + cmd := exec.Command("uname", "-m") + machine, err := cmd.Output() + if err != nil { + return "", err + } + return string(machine), nil +} diff --git a/pkg/platform/architecture_linux.go b/pkg/platform/architecture_linux.go new file mode 100644 index 0000000000..e732534f33 --- /dev/null +++ b/pkg/platform/architecture_linux.go @@ -0,0 +1,28 @@ +// Package platform provides helper function to get the runtime architecture +// for different platforms. +package platform + +import ( + "syscall" +) + +// GetRuntimeArchitecture get the name of the current architecture (x86, x86_64, …) +func GetRuntimeArchitecture() (string, error) { + utsname := &syscall.Utsname{} + if err := syscall.Uname(utsname); err != nil { + return "", err + } + return charsToString(utsname.Machine), nil +} + +func charsToString(ca [65]int8) string { + s := make([]byte, len(ca)) + var lens int + for ; lens < len(ca); lens++ { + if ca[lens] == 0 { + break + } + s[lens] = uint8(ca[lens]) + } + return string(s[0:lens]) +} diff --git a/pkg/platform/architecture_windows.go b/pkg/platform/architecture_windows.go new file mode 100644 index 0000000000..19717c9349 --- /dev/null +++ b/pkg/platform/architecture_windows.go @@ -0,0 +1,52 @@ +package platform + +import ( + "fmt" + "syscall" + "unsafe" +) + +var ( + modkernel32 = syscall.NewLazyDLL("kernel32.dll") + procGetSystemInfo = modkernel32.NewProc("GetSystemInfo") +) + +// see http://msdn.microsoft.com/en-us/library/windows/desktop/ms724958(v=vs.85).aspx +type systeminfo struct { + wProcessorArchitecture uint16 + wReserved uint16 + dwPageSize uint32 + lpMinimumApplicationAddress uintptr + lpMaximumApplicationAddress uintptr + dwActiveProcessorMask uintptr + dwNumberOfProcessors uint32 + dwProcessorType uint32 + dwAllocationGranularity uint32 + wProcessorLevel uint16 + wProcessorRevision uint16 +} + +// Constants +const ( + ProcessorArchitecture64 = 9 // PROCESSOR_ARCHITECTURE_AMD64 + ProcessorArchitectureIA64 = 6 // PROCESSOR_ARCHITECTURE_IA64 + ProcessorArchitecture32 = 0 // PROCESSOR_ARCHITECTURE_INTEL + ProcessorArchitectureArm = 5 // PROCESSOR_ARCHITECTURE_ARM +) + +var sysinfo systeminfo + +// GetRuntimeArchitecture get the name of the current architecture (x86, x86_64, …) +func GetRuntimeArchitecture() (string, error) { + syscall.Syscall(procGetSystemInfo.Addr(), 1, uintptr(unsafe.Pointer(&sysinfo)), 0, 0) + switch sysinfo.wProcessorArchitecture { + case ProcessorArchitecture64, ProcessorArchitectureIA64: + return "x86_64", nil + case ProcessorArchitecture32: + return "i686", nil + case ProcessorArchitectureArm: + return "arm", nil + default: + return "", fmt.Errorf("Unknown processor architecture") + } +} diff --git a/pkg/platform/platform.go b/pkg/platform/platform.go new file mode 100644 index 0000000000..d84c4f59de --- /dev/null +++ b/pkg/platform/platform.go @@ -0,0 +1,23 @@ +package platform + +import ( + "runtime" + + "github.com/Sirupsen/logrus" +) + +var ( + // Architecture holds the runtime architecture of the process. + Architecture string + // OSType holds the runtime operating system type (Linux, …) of the process. + OSType string +) + +func init() { + var err error + Architecture, err = GetRuntimeArchitecture() + if err != nil { + logrus.Errorf("Could no read system architecture info: %v", err) + } + OSType = runtime.GOOS +}