2015-07-25 04:35:07 -04:00
|
|
|
// Package operatingsystem provides helper function to get the operating system
|
|
|
|
// name for different platforms.
|
2018-02-05 16:05:59 -05:00
|
|
|
package operatingsystem // import "github.com/docker/docker/pkg/parsers/operatingsystem"
|
2014-07-24 11:42:38 -04:00
|
|
|
|
|
|
|
import (
|
2015-10-29 04:16:21 -04:00
|
|
|
"bufio"
|
2014-07-24 11:42:38 -04:00
|
|
|
"bytes"
|
2015-10-29 04:16:21 -04:00
|
|
|
"fmt"
|
2014-07-24 11:42:38 -04:00
|
|
|
"io/ioutil"
|
2015-10-29 04:16:21 -04:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mattn/go-shellwords"
|
2014-07-24 11:42:38 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// file to use to detect if the daemon is running in a container
|
|
|
|
proc1Cgroup = "/proc/1/cgroup"
|
|
|
|
|
|
|
|
// file to check to determine Operating System
|
|
|
|
etcOsRelease = "/etc/os-release"
|
2015-11-16 19:19:14 -05:00
|
|
|
|
|
|
|
// used by stateless systems like Clear Linux
|
|
|
|
altOsRelease = "/usr/lib/os-release"
|
2014-07-24 11:42:38 -04:00
|
|
|
)
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// GetOperatingSystem gets the name of the current operating system.
|
2014-07-24 11:42:38 -04:00
|
|
|
func GetOperatingSystem() (string, error) {
|
2019-05-30 12:51:41 -04:00
|
|
|
if prettyName, err := getValueFromOsRelease("PRETTY_NAME"); err != nil {
|
|
|
|
return "", err
|
|
|
|
} else if prettyName != "" {
|
|
|
|
return prettyName, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not set, defaults to PRETTY_NAME="Linux"
|
|
|
|
// c.f. http://www.freedesktop.org/software/systemd/man/os-release.html
|
|
|
|
return "Linux", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOperatingSystemVersion gets the version of the current operating system, as a string.
|
|
|
|
func GetOperatingSystemVersion() (string, error) {
|
|
|
|
return getValueFromOsRelease("VERSION_ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
// parses the os-release file and returns the value associated with `key`
|
|
|
|
func getValueFromOsRelease(key string) (string, error) {
|
2015-10-29 04:16:21 -04:00
|
|
|
osReleaseFile, err := os.Open(etcOsRelease)
|
2014-07-24 11:42:38 -04:00
|
|
|
if err != nil {
|
2015-11-16 19:19:14 -05:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return "", fmt.Errorf("Error opening %s: %v", etcOsRelease, err)
|
|
|
|
}
|
|
|
|
osReleaseFile, err = os.Open(altOsRelease)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error opening %s: %v", altOsRelease, err)
|
|
|
|
}
|
2014-07-24 11:42:38 -04:00
|
|
|
}
|
2015-10-29 04:16:21 -04:00
|
|
|
defer osReleaseFile.Close()
|
|
|
|
|
2019-05-30 12:51:41 -04:00
|
|
|
var value string
|
|
|
|
keyWithTrailingEqual := key + "="
|
2015-10-29 04:16:21 -04:00
|
|
|
scanner := bufio.NewScanner(osReleaseFile)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
2019-05-30 12:51:41 -04:00
|
|
|
if strings.HasPrefix(line, keyWithTrailingEqual) {
|
2015-10-29 04:16:21 -04:00
|
|
|
data := strings.SplitN(line, "=", 2)
|
2019-05-30 12:51:41 -04:00
|
|
|
values, err := shellwords.Parse(data[1])
|
2015-10-29 04:16:21 -04:00
|
|
|
if err != nil {
|
2019-05-30 12:51:41 -04:00
|
|
|
return "", fmt.Errorf("%s is invalid: %s", key, err.Error())
|
2015-10-29 04:16:21 -04:00
|
|
|
}
|
2019-05-30 12:51:41 -04:00
|
|
|
if len(values) != 1 {
|
|
|
|
return "", fmt.Errorf("%s needs to be enclosed by quotes if they have spaces: %s", key, data[1])
|
2015-10-29 04:16:21 -04:00
|
|
|
}
|
2019-05-30 12:51:41 -04:00
|
|
|
value = values[0]
|
2015-10-29 04:16:21 -04:00
|
|
|
}
|
|
|
|
}
|
2019-05-30 12:51:41 -04:00
|
|
|
|
|
|
|
return value, nil
|
2014-07-24 11:42:38 -04:00
|
|
|
}
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// IsContainerized returns true if we are running inside a container.
|
2014-07-24 11:42:38 -04:00
|
|
|
func IsContainerized() (bool, error) {
|
|
|
|
b, err := ioutil.ReadFile(proc1Cgroup)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
for _, line := range bytes.Split(b, []byte{'\n'}) {
|
2015-10-27 22:00:07 -04:00
|
|
|
if len(line) > 0 && !bytes.HasSuffix(line, []byte{'/'}) && !bytes.HasSuffix(line, []byte("init.scope")) {
|
2014-07-24 11:42:38 -04:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|