2015-07-25 08:35:07 +00:00
|
|
|
// Package operatingsystem provides helper function to get the operating system
|
|
|
|
// name for different platforms.
|
2014-07-24 15:42:38 +00:00
|
|
|
package operatingsystem
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-07-25 08:35:07 +00:00
|
|
|
// GetOperatingSystem gets the name of the current operating system.
|
2014-07-24 15:42:38 +00:00
|
|
|
func GetOperatingSystem() (string, error) {
|
|
|
|
b, err := ioutil.ReadFile(etcOsRelease)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if i := bytes.Index(b, []byte("PRETTY_NAME")); i >= 0 {
|
|
|
|
b = b[i+13:]
|
|
|
|
return string(b[:bytes.IndexByte(b, '"')]), nil
|
|
|
|
}
|
|
|
|
return "", errors.New("PRETTY_NAME not found")
|
|
|
|
}
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// IsContainerized returns true if we are running inside a container.
|
2014-07-24 15:42:38 +00: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-28 02:00:07 +00:00
|
|
|
if len(line) > 0 && !bytes.HasSuffix(line, []byte{'/'}) && !bytes.HasSuffix(line, []byte("init.scope")) {
|
2014-07-24 15:42:38 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|