mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-09-29 04:25:40 +00:00
For example, under debian buster/sid. Those information should be provide with best effort instead of error out. Set name and version to "<<unknown>>" if they are not defined. Fixes: #1177 Signed-off-by: Yang Bo <bo@hyper.sh>
134 lines
2.8 KiB
Go
134 lines
2.8 KiB
Go
// Copyright (c) 2017 Intel Corporation
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/kata-containers/runtime/pkg/katautils"
|
|
)
|
|
|
|
const (
|
|
unknown = "<<unknown>>"
|
|
)
|
|
|
|
// variables to allow tests to modify the values
|
|
var (
|
|
procVersion = "/proc/version"
|
|
osRelease = "/etc/os-release"
|
|
|
|
// Clear Linux has a different path (for stateless support)
|
|
osReleaseClr = "/usr/lib/os-release"
|
|
)
|
|
|
|
func getKernelVersion() (string, error) {
|
|
contents, err := katautils.GetFileContents(procVersion)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
fields := strings.Fields(contents)
|
|
|
|
if len(fields) < 3 {
|
|
return "", fmt.Errorf("unexpected contents in %v", procVersion)
|
|
}
|
|
|
|
version := fields[2]
|
|
|
|
return version, nil
|
|
}
|
|
|
|
// getDistroDetails returns the distributions name and version string.
|
|
// If it is not possible to determine both values an error is
|
|
// returned.
|
|
func getDistroDetails() (name, version string, err error) {
|
|
files := []string{osRelease, osReleaseClr}
|
|
name = ""
|
|
version = ""
|
|
|
|
for _, file := range files {
|
|
contents, err := katautils.GetFileContents(file)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
continue
|
|
}
|
|
|
|
return "", "", err
|
|
}
|
|
|
|
lines := strings.Split(contents, "\n")
|
|
|
|
for _, line := range lines {
|
|
if strings.HasPrefix(line, "NAME=") && name == "" {
|
|
fields := strings.Split(line, "=")
|
|
name = strings.Trim(fields[1], `"`)
|
|
} else if strings.HasPrefix(line, "VERSION_ID=") && version == "" {
|
|
fields := strings.Split(line, "=")
|
|
version = strings.Trim(fields[1], `"`)
|
|
}
|
|
}
|
|
|
|
if name != "" && version != "" {
|
|
return name, version, nil
|
|
}
|
|
}
|
|
|
|
if name == "" {
|
|
name = unknown
|
|
}
|
|
|
|
if version == "" {
|
|
version = unknown
|
|
}
|
|
|
|
return name, version, nil
|
|
}
|
|
|
|
// genericGetCPUDetails returns the vendor and model of the CPU.
|
|
// If it is not possible to determine both values an error is
|
|
// returned.
|
|
func genericGetCPUDetails() (vendor, model string, err error) {
|
|
cpuinfo, err := getCPUInfo(procCPUInfo)
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
|
|
lines := strings.Split(cpuinfo, "\n")
|
|
|
|
for _, line := range lines {
|
|
if archCPUVendorField != "" {
|
|
if strings.HasPrefix(line, archCPUVendorField) {
|
|
fields := strings.Split(line, ":")
|
|
if len(fields) > 1 {
|
|
vendor = strings.TrimSpace(fields[1])
|
|
}
|
|
}
|
|
}
|
|
|
|
if archCPUModelField != "" {
|
|
if strings.HasPrefix(line, archCPUModelField) {
|
|
fields := strings.Split(line, ":")
|
|
if len(fields) > 1 {
|
|
model = strings.TrimSpace(fields[1])
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if archCPUVendorField != "" && vendor == "" {
|
|
return "", "", fmt.Errorf("cannot find vendor field in file %v", procCPUInfo)
|
|
}
|
|
|
|
// model name is optional
|
|
if archCPUModelField != "" && model == "" {
|
|
return "", "", fmt.Errorf("cannot find model field in file %v", procCPUInfo)
|
|
}
|
|
|
|
return vendor, model, nil
|
|
}
|