github.com/damirazo/docker@v1.9.0/pkg/parsers/operatingsystem/operatingsystem_linux.go (about)

     1  // Package operatingsystem provides helper function to get the operating system
     2  // name for different platforms.
     3  package operatingsystem
     4  
     5  import (
     6  	"bytes"
     7  	"errors"
     8  	"io/ioutil"
     9  )
    10  
    11  var (
    12  	// file to use to detect if the daemon is running in a container
    13  	proc1Cgroup = "/proc/1/cgroup"
    14  
    15  	// file to check to determine Operating System
    16  	etcOsRelease = "/etc/os-release"
    17  )
    18  
    19  // GetOperatingSystem gets the name of the current operating system.
    20  func GetOperatingSystem() (string, error) {
    21  	b, err := ioutil.ReadFile(etcOsRelease)
    22  	if err != nil {
    23  		return "", err
    24  	}
    25  	if i := bytes.Index(b, []byte("PRETTY_NAME")); i >= 0 {
    26  		b = b[i+13:]
    27  		return string(b[:bytes.IndexByte(b, '"')]), nil
    28  	}
    29  	return "", errors.New("PRETTY_NAME not found")
    30  }
    31  
    32  // IsContainerized returns true if we are running inside a container.
    33  func IsContainerized() (bool, error) {
    34  	b, err := ioutil.ReadFile(proc1Cgroup)
    35  	if err != nil {
    36  		return false, err
    37  	}
    38  	for _, line := range bytes.Split(b, []byte{'\n'}) {
    39  		if len(line) > 0 && !bytes.HasSuffix(line, []byte{'/'}) {
    40  			return true, nil
    41  		}
    42  	}
    43  	return false, nil
    44  }