github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/container/lxc/lxcutils/utils.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package lxcutils
     5  
     6  import (
     7  	"bufio"
     8  	"os"
     9  	"strings"
    10  
    11  	"github.com/juju/errors"
    12  )
    13  
    14  var initProcessCgroupFile = "/proc/1/cgroup"
    15  
    16  // RunningInsideLXC reports whether or not we are running inside an
    17  // LXC container.
    18  func RunningInsideLXC() (bool, error) {
    19  	file, err := os.Open(initProcessCgroupFile)
    20  	if err != nil {
    21  		return false, errors.Trace(err)
    22  	}
    23  	defer file.Close()
    24  
    25  	scanner := bufio.NewScanner(file)
    26  	for scanner.Scan() {
    27  		line := scanner.Text()
    28  		fields := strings.Split(line, ":")
    29  		if len(fields) != 3 {
    30  			return false, errors.Errorf("malformed cgroup file")
    31  		}
    32  		if fields[2] != "/" {
    33  			// When running in a container the anchor point will be
    34  			// something other than "/".
    35  			return true, nil
    36  		}
    37  	}
    38  	if err := scanner.Err(); err != nil {
    39  		return false, errors.Annotate(err, "failed to read cgroup file")
    40  	}
    41  	return false, nil
    42  }