Browse code

Rename Capabilities in sysinfo and move it to its own subpackage

Docker-DCO-1.1-Signed-off-by: Guillaume J. Charmes <guillaume.charmes@docker.com> (github: creack)

Docker-DCO-1.1-Signed-off-by: Guillaume J. Charmes <guillaume.charmes@docker.com> (github: crosbymichael)

Guillaume J. Charmes authored on 2014/01/16 07:36:13
Showing 7 changed files
... ...
@@ -657,16 +657,16 @@ func postContainersCreate(srv *Server, version float64, w http.ResponseWriter, r
657 657
 	for scanner.Scan() {
658 658
 		out.Warnings = append(out.Warnings, scanner.Text())
659 659
 	}
660
-	if job.GetenvInt("Memory") > 0 && !srv.runtime.capabilities.MemoryLimit {
660
+	if job.GetenvInt("Memory") > 0 && !srv.runtime.sysInfo.MemoryLimit {
661 661
 		log.Println("WARNING: Your kernel does not support memory limit capabilities. Limitation discarded.")
662 662
 		out.Warnings = append(out.Warnings, "Your kernel does not support memory limit capabilities. Limitation discarded.")
663 663
 	}
664
-	if job.GetenvInt("Memory") > 0 && !srv.runtime.capabilities.SwapLimit {
664
+	if job.GetenvInt("Memory") > 0 && !srv.runtime.sysInfo.SwapLimit {
665 665
 		log.Println("WARNING: Your kernel does not support swap limit capabilities. Limitation discarded.")
666 666
 		out.Warnings = append(out.Warnings, "Your kernel does not support memory swap capabilities. Limitation discarded.")
667 667
 	}
668 668
 
669
-	if !job.GetenvBool("NetworkDisabled") && srv.runtime.capabilities.IPv4ForwardingDisabled {
669
+	if !job.GetenvBool("NetworkDisabled") && srv.runtime.sysInfo.IPv4ForwardingDisabled {
670 670
 		log.Println("Warning: IPv4 forwarding is disabled.")
671 671
 		out.Warnings = append(out.Warnings, "IPv4 forwarding is disabled.")
672 672
 	}
... ...
@@ -12,6 +12,7 @@ import (
12 12
 	"github.com/dotcloud/docker/auth"
13 13
 	"github.com/dotcloud/docker/engine"
14 14
 	flag "github.com/dotcloud/docker/pkg/mflag"
15
+	"github.com/dotcloud/docker/pkg/sysinfo"
15 16
 	"github.com/dotcloud/docker/pkg/term"
16 17
 	"github.com/dotcloud/docker/registry"
17 18
 	"github.com/dotcloud/docker/utils"
... ...
@@ -1745,14 +1746,14 @@ func (cli *DockerCli) CmdTag(args ...string) error {
1745 1745
 }
1746 1746
 
1747 1747
 //FIXME Only used in tests
1748
-func ParseRun(args []string, capabilities *Capabilities) (*Config, *HostConfig, *flag.FlagSet, error) {
1748
+func ParseRun(args []string, sysInfo *sysinfo.SysInfo) (*Config, *HostConfig, *flag.FlagSet, error) {
1749 1749
 	cmd := flag.NewFlagSet("run", flag.ContinueOnError)
1750 1750
 	cmd.SetOutput(ioutil.Discard)
1751 1751
 	cmd.Usage = nil
1752
-	return parseRun(cmd, args, capabilities)
1752
+	return parseRun(cmd, args, sysInfo)
1753 1753
 }
1754 1754
 
1755
-func parseRun(cmd *flag.FlagSet, args []string, capabilities *Capabilities) (*Config, *HostConfig, *flag.FlagSet, error) {
1755
+func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Config, *HostConfig, *flag.FlagSet, error) {
1756 1756
 	var (
1757 1757
 		// FIXME: use utils.ListOpts for attach and volumes?
1758 1758
 		flAttach  = NewListOpts(ValidateAttach)
... ...
@@ -1802,7 +1803,7 @@ func parseRun(cmd *flag.FlagSet, args []string, capabilities *Capabilities) (*Co
1802 1802
 	}
1803 1803
 
1804 1804
 	// Check if the kernel supports memory limit cgroup.
1805
-	if capabilities != nil && *flMemoryString != "" && !capabilities.MemoryLimit {
1805
+	if sysInfo != nil && *flMemoryString != "" && !sysInfo.MemoryLimit {
1806 1806
 		*flMemoryString = ""
1807 1807
 	}
1808 1808
 
... ...
@@ -1934,7 +1935,7 @@ func parseRun(cmd *flag.FlagSet, args []string, capabilities *Capabilities) (*Co
1934 1934
 		PublishAllPorts: *flPublishAll,
1935 1935
 	}
1936 1936
 
1937
-	if capabilities != nil && flMemory > 0 && !capabilities.SwapLimit {
1937
+	if sysInfo != nil && flMemory > 0 && !sysInfo.SwapLimit {
1938 1938
 		//fmt.Fprintf(stdout, "WARNING: Your kernel does not support swap limit capabilities. Limitation discarded.\n")
1939 1939
 		config.MemorySwap = -1
1940 1940
 	}
... ...
@@ -527,16 +527,16 @@ func (container *Container) Start() (err error) {
527 527
 	}
528 528
 
529 529
 	// Make sure the config is compatible with the current kernel
530
-	if container.Config.Memory > 0 && !container.runtime.capabilities.MemoryLimit {
530
+	if container.Config.Memory > 0 && !container.runtime.sysInfo.MemoryLimit {
531 531
 		log.Printf("WARNING: Your kernel does not support memory limit capabilities. Limitation discarded.\n")
532 532
 		container.Config.Memory = 0
533 533
 	}
534
-	if container.Config.Memory > 0 && !container.runtime.capabilities.SwapLimit {
534
+	if container.Config.Memory > 0 && !container.runtime.sysInfo.SwapLimit {
535 535
 		log.Printf("WARNING: Your kernel does not support swap limit capabilities. Limitation discarded.\n")
536 536
 		container.Config.MemorySwap = -1
537 537
 	}
538 538
 
539
-	if container.runtime.capabilities.IPv4ForwardingDisabled {
539
+	if container.runtime.sysInfo.IPv4ForwardingDisabled {
540 540
 		log.Printf("WARNING: IPv4 forwarding is disabled. Networking will not work")
541 541
 	}
542 542
 
... ...
@@ -1,6 +1,7 @@
1 1
 package docker
2 2
 
3 3
 import (
4
+	"github.com/dotcloud/docker/pkg/sysinfo"
4 5
 	"strings"
5 6
 	"text/template"
6 7
 )
... ...
@@ -31,7 +32,7 @@ lxc.console = none
31 31
 lxc.tty = 1
32 32
 
33 33
 {{if (getHostConfig .).Privileged}}
34
-lxc.cgroup.devices.allow = a 
34
+lxc.cgroup.devices.allow = a
35 35
 {{else}}
36 36
 # no implicit access to devices
37 37
 lxc.cgroup.devices.deny = a
... ...
@@ -82,7 +83,7 @@ lxc.mount.entry = devpts {{escapeFstabSpaces $ROOTFS}}/dev/pts devpts newinstanc
82 82
 lxc.mount.entry = shm {{escapeFstabSpaces $ROOTFS}}/dev/shm tmpfs size=65536k,nosuid,nodev,noexec 0 0
83 83
 
84 84
 {{if (getHostConfig .).Privileged}}
85
-{{if (getCapabilities .).AppArmor}}
85
+{{if (getSysInfo .).AppArmor}}
86 86
 lxc.aa_profile = unconfined
87 87
 {{else}}
88 88
 #lxc.aa_profile = unconfined
... ...
@@ -129,8 +130,8 @@ func getHostConfig(container *Container) *HostConfig {
129 129
 	return container.hostConfig
130 130
 }
131 131
 
132
-func getCapabilities(container *Container) *Capabilities {
133
-	return container.runtime.capabilities
132
+func getSysInfo(container *Container) *sysinfo.SysInfo {
133
+	return container.runtime.sysInfo
134 134
 }
135 135
 
136 136
 func init() {
... ...
@@ -138,7 +139,7 @@ func init() {
138 138
 	funcMap := template.FuncMap{
139 139
 		"getMemorySwap":     getMemorySwap,
140 140
 		"getHostConfig":     getHostConfig,
141
-		"getCapabilities":   getCapabilities,
141
+		"getSysInfo":        getSysInfo,
142 142
 		"escapeFstabSpaces": escapeFstabSpaces,
143 143
 	}
144 144
 	LxcTemplateCompiled, err = template.New("lxc").Funcs(funcMap).Parse(LxcTemplate)
145 145
new file mode 100644
... ...
@@ -0,0 +1,55 @@
0
+package sysinfo
1
+
2
+import (
3
+	"github.com/dotcloud/docker/cgroups"
4
+	"github.com/dotcloud/docker/utils"
5
+	"io/ioutil"
6
+	"log"
7
+	"os"
8
+	"path"
9
+)
10
+
11
+type SysInfo struct {
12
+	MemoryLimit            bool
13
+	SwapLimit              bool
14
+	IPv4ForwardingDisabled bool
15
+	AppArmor               bool
16
+}
17
+
18
+func New(quiet bool) *SysInfo {
19
+	sysInfo := &SysInfo{}
20
+	if cgroupMemoryMountpoint, err := cgroups.FindCgroupMountpoint("memory"); err != nil {
21
+		if !quiet {
22
+			log.Printf("WARNING: %s\n", err)
23
+		}
24
+	} else {
25
+		_, err1 := ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memory.limit_in_bytes"))
26
+		_, err2 := ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memory.soft_limit_in_bytes"))
27
+		sysInfo.MemoryLimit = err1 == nil && err2 == nil
28
+		if !sysInfo.MemoryLimit && !quiet {
29
+			log.Printf("WARNING: Your kernel does not support cgroup memory limit.")
30
+		}
31
+
32
+		_, err = ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memory.memsw.limit_in_bytes"))
33
+		sysInfo.SwapLimit = err == nil
34
+		if !sysInfo.SwapLimit && !quiet {
35
+			log.Printf("WARNING: Your kernel does not support cgroup swap limit.")
36
+		}
37
+	}
38
+
39
+	content, err3 := ioutil.ReadFile("/proc/sys/net/ipv4/ip_forward")
40
+	sysInfo.IPv4ForwardingDisabled = err3 != nil || len(content) == 0 || content[0] != '1'
41
+	if sysInfo.IPv4ForwardingDisabled && !quiet {
42
+		log.Printf("WARNING: IPv4 forwarding is disabled.")
43
+	}
44
+
45
+	// Check if AppArmor seems to be enabled on this system.
46
+	if _, err := os.Stat("/sys/kernel/security/apparmor"); os.IsNotExist(err) {
47
+		utils.Debugf("/sys/kernel/security/apparmor not found; assuming AppArmor is not enabled.")
48
+		sysInfo.AppArmor = false
49
+	} else {
50
+		utils.Debugf("/sys/kernel/security/apparmor found; assuming AppArmor is enabled.")
51
+		sysInfo.AppArmor = true
52
+	}
53
+	return sysInfo
54
+}
... ...
@@ -4,7 +4,6 @@ import (
4 4
 	"container/list"
5 5
 	"fmt"
6 6
 	"github.com/dotcloud/docker/archive"
7
-	"github.com/dotcloud/docker/cgroups"
8 7
 	"github.com/dotcloud/docker/execdriver"
9 8
 	"github.com/dotcloud/docker/execdriver/chroot"
10 9
 	"github.com/dotcloud/docker/execdriver/lxc"
... ...
@@ -13,10 +12,10 @@ import (
13 13
 	_ "github.com/dotcloud/docker/graphdriver/devmapper"
14 14
 	_ "github.com/dotcloud/docker/graphdriver/vfs"
15 15
 	"github.com/dotcloud/docker/pkg/graphdb"
16
+	"github.com/dotcloud/docker/pkg/sysinfo"
16 17
 	"github.com/dotcloud/docker/utils"
17 18
 	"io"
18 19
 	"io/ioutil"
19
-	"log"
20 20
 	"os"
21 21
 	"path"
22 22
 	"regexp"
... ...
@@ -37,13 +36,6 @@ var (
37 37
 	validContainerNamePattern = regexp.MustCompile(`^/?` + validContainerNameChars + `+$`)
38 38
 )
39 39
 
40
-type Capabilities struct {
41
-	MemoryLimit            bool
42
-	SwapLimit              bool
43
-	IPv4ForwardingDisabled bool
44
-	AppArmor               bool
45
-}
46
-
47 40
 type Runtime struct {
48 41
 	repository     string
49 42
 	sysInitPath    string
... ...
@@ -52,7 +44,7 @@ type Runtime struct {
52 52
 	graph          *Graph
53 53
 	repositories   *TagStore
54 54
 	idIndex        *utils.TruncIndex
55
-	capabilities   *Capabilities
55
+	sysInfo        *sysinfo.SysInfo
56 56
 	volumes        *Graph
57 57
 	srv            *Server
58 58
 	config         *DaemonConfig
... ...
@@ -332,44 +324,6 @@ func (runtime *Runtime) restore() error {
332 332
 	return nil
333 333
 }
334 334
 
335
-func NewRuntimeCapabilities(quiet bool) *Capabilities {
336
-	capabilities := &Capabilities{}
337
-	if cgroupMemoryMountpoint, err := cgroups.FindCgroupMountpoint("memory"); err != nil {
338
-		if !quiet {
339
-			log.Printf("WARNING: %s\n", err)
340
-		}
341
-	} else {
342
-		_, err1 := ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memory.limit_in_bytes"))
343
-		_, err2 := ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memory.soft_limit_in_bytes"))
344
-		capabilities.MemoryLimit = err1 == nil && err2 == nil
345
-		if !capabilities.MemoryLimit && !quiet {
346
-			log.Printf("WARNING: Your kernel does not support cgroup memory limit.")
347
-		}
348
-
349
-		_, err = ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memory.memsw.limit_in_bytes"))
350
-		capabilities.SwapLimit = err == nil
351
-		if !capabilities.SwapLimit && !quiet {
352
-			log.Printf("WARNING: Your kernel does not support cgroup swap limit.")
353
-		}
354
-	}
355
-
356
-	content, err3 := ioutil.ReadFile("/proc/sys/net/ipv4/ip_forward")
357
-	capabilities.IPv4ForwardingDisabled = err3 != nil || len(content) == 0 || content[0] != '1'
358
-	if capabilities.IPv4ForwardingDisabled && !quiet {
359
-		log.Printf("WARNING: IPv4 forwarding is disabled.")
360
-	}
361
-
362
-	// Check if AppArmor seems to be enabled on this system.
363
-	if _, err := os.Stat("/sys/kernel/security/apparmor"); os.IsNotExist(err) {
364
-		utils.Debugf("/sys/kernel/security/apparmor not found; assuming AppArmor is not enabled.")
365
-		capabilities.AppArmor = false
366
-	} else {
367
-		utils.Debugf("/sys/kernel/security/apparmor found; assuming AppArmor is enabled.")
368
-		capabilities.AppArmor = true
369
-	}
370
-	return capabilities
371
-}
372
-
373 335
 // Create creates a new container from the given configuration with a given name.
374 336
 func (runtime *Runtime) Create(config *Config, name string) (*Container, []string, error) {
375 337
 	// Lookup image
... ...
@@ -732,7 +686,7 @@ func NewRuntimeFromDirectory(config *DaemonConfig) (*Runtime, error) {
732 732
 		sysInitPath = localCopy
733 733
 	}
734 734
 
735
-	capabilities := NewRuntimeCapabilities(false)
735
+	sysInfo := sysinfo.New(false)
736 736
 
737 737
 	/*
738 738
 		temporarilly disabled.
... ...
@@ -740,14 +694,14 @@ func NewRuntimeFromDirectory(config *DaemonConfig) (*Runtime, error) {
740 740
 	if false {
741 741
 		var ed execdriver.Driver
742 742
 		if driver := os.Getenv("EXEC_DRIVER"); driver == "lxc" {
743
-			ed, err = lxc.NewDriver(config.Root, capabilities.AppArmor)
743
+			ed, err = lxc.NewDriver(config.Root, sysInfo.AppArmor)
744 744
 		} else {
745 745
 			ed, err = chroot.NewDriver()
746 746
 		}
747 747
 		if ed != nil {
748 748
 		}
749 749
 	}
750
-	ed, err := lxc.NewDriver(config.Root, capabilities.AppArmor)
750
+	ed, err := lxc.NewDriver(config.Root, sysInfo.AppArmor)
751 751
 	if err != nil {
752 752
 		return nil, err
753 753
 	}
... ...
@@ -759,7 +713,7 @@ func NewRuntimeFromDirectory(config *DaemonConfig) (*Runtime, error) {
759 759
 		graph:          g,
760 760
 		repositories:   repositories,
761 761
 		idIndex:        utils.NewTruncIndex(),
762
-		capabilities:   capabilities,
762
+		sysInfo:        sysInfo,
763 763
 		volumes:        volumes,
764 764
 		config:         config,
765 765
 		containerGraph: graph,
... ...
@@ -516,7 +516,7 @@ func (srv *Server) ImageInsert(job *engine.Job) engine.Status {
516 516
 	}
517 517
 	defer file.Body.Close()
518 518
 
519
-	config, _, _, err := ParseRun([]string{img.ID, "echo", "insert", url, path}, srv.runtime.capabilities)
519
+	config, _, _, err := ParseRun([]string{img.ID, "echo", "insert", url, path}, srv.runtime.sysInfo)
520 520
 	if err != nil {
521 521
 		job.Error(err)
522 522
 		return engine.StatusErr
... ...
@@ -678,9 +678,9 @@ func (srv *Server) DockerInfo(job *engine.Job) engine.Status {
678 678
 	v.SetInt("Images", imgcount)
679 679
 	v.Set("Driver", srv.runtime.driver.String())
680 680
 	v.SetJson("DriverStatus", srv.runtime.driver.Status())
681
-	v.SetBool("MemoryLimit", srv.runtime.capabilities.MemoryLimit)
682
-	v.SetBool("SwapLimit", srv.runtime.capabilities.SwapLimit)
683
-	v.SetBool("IPv4Forwarding", !srv.runtime.capabilities.IPv4ForwardingDisabled)
681
+	v.SetBool("MemoryLimit", srv.runtime.sysInfo.MemoryLimit)
682
+	v.SetBool("SwapLimit", srv.runtime.sysInfo.SwapLimit)
683
+	v.SetBool("IPv4Forwarding", !srv.runtime.sysInfo.IPv4ForwardingDisabled)
684 684
 	v.SetBool("Debug", os.Getenv("DEBUG") != "")
685 685
 	v.SetInt("NFd", utils.GetTotalUsedFds())
686 686
 	v.SetInt("NGoroutines", runtime.NumGoroutine())
... ...
@@ -1470,10 +1470,10 @@ func (srv *Server) ContainerCreate(job *engine.Job) engine.Status {
1470 1470
 		job.Errorf("Minimum memory limit allowed is 512k")
1471 1471
 		return engine.StatusErr
1472 1472
 	}
1473
-	if config.Memory > 0 && !srv.runtime.capabilities.MemoryLimit {
1473
+	if config.Memory > 0 && !srv.runtime.sysInfo.MemoryLimit {
1474 1474
 		config.Memory = 0
1475 1475
 	}
1476
-	if config.Memory > 0 && !srv.runtime.capabilities.SwapLimit {
1476
+	if config.Memory > 0 && !srv.runtime.sysInfo.SwapLimit {
1477 1477
 		config.MemorySwap = -1
1478 1478
 	}
1479 1479
 	container, buildWarnings, err := srv.runtime.Create(&config, name)