Jelajahi Sumber

Allow to disable memory limit at compilation time

Guillaume J. Charmes 12 tahun lalu
induk
melakukan
1967c8342a
5 mengubah file dengan 32 tambahan dan 3 penghapusan
  1. 4 1
      Makefile
  2. 7 1
      commands.go
  3. 11 0
      container.go
  4. 8 1
      docker/docker.go
  5. 2 0
      runtime_test.go

+ 4 - 1
Makefile

@@ -13,7 +13,10 @@ endif
 GIT_COMMIT = $(shell git rev-parse --short HEAD)
 GIT_STATUS = $(shell test -n "`git status --porcelain`" && echo "+CHANGES")
 
-BUILD_OPTIONS = -ldflags "-X main.GIT_COMMIT $(GIT_COMMIT)$(GIT_STATUS)"
+NO_MEMORY_LIMIT ?= 0
+export NO_MEMORY_LIMIT
+
+BUILD_OPTIONS = -ldflags "-X main.GIT_COMMIT $(GIT_COMMIT)$(GIT_STATUS) -X main.NO_MEMORY_LIMIT $(NO_MEMORY_LIMIT)"
 
 SRC_DIR := $(GOPATH)/src
 

+ 7 - 1
commands.go

@@ -20,7 +20,10 @@ import (
 
 const VERSION = "0.1.4"
 
-var GIT_COMMIT string
+var (
+	GIT_COMMIT      string
+	NO_MEMORY_LIMIT bool
+)
 
 func (srv *Server) Name() string {
 	return "docker"
@@ -183,6 +186,9 @@ func (srv *Server) CmdWait(stdin io.ReadCloser, stdout io.Writer, args ...string
 func (srv *Server) CmdVersion(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
 	fmt.Fprintf(stdout, "Version:%s\n", VERSION)
 	fmt.Fprintf(stdout, "Git Commit:%s\n", GIT_COMMIT)
+	if NO_MEMORY_LIMIT {
+		fmt.Fprintf(stdout, "Memory limit disabled\n")
+	}
 	return nil
 }
 

+ 11 - 0
container.go

@@ -79,6 +79,11 @@ func ParseRun(args []string, stdout io.Writer) (*Config, error) {
 	flTty := cmd.Bool("t", false, "Allocate a pseudo-tty")
 	flMemory := cmd.Int64("m", 0, "Memory limit (in bytes)")
 
+	if *flMemory > 0 && NO_MEMORY_LIMIT {
+		fmt.Fprintf(stdout, "WARNING: This version of docker has been compiled without memory limit support. Discarding -m.")
+		*flMemory = 0
+	}
+
 	var flPorts ListOpts
 	cmd.Var(&flPorts, "p", "Expose a container's port to the host (use 'docker port' to see the actual mapping)")
 
@@ -355,6 +360,12 @@ func (container *Container) Start() error {
 	if err := container.allocateNetwork(); err != nil {
 		return err
 	}
+
+	if container.Config.Memory > 0 && NO_MEMORY_LIMIT {
+		log.Printf("WARNING: This version of docker has been compiled without memory limit support. Discarding the limit.")
+		container.Config.Memory = 0
+	}
+
 	if err := container.generateLXCConfig(); err != nil {
 		return err
 	}

+ 8 - 1
docker/docker.go

@@ -13,7 +13,10 @@ import (
 	"syscall"
 )
 
-var GIT_COMMIT string
+var (
+	GIT_COMMIT      string
+	NO_MEMORY_LIMIT string
+)
 
 func main() {
 	if docker.SelfPath() == "/sbin/init" {
@@ -36,11 +39,15 @@ func main() {
 		os.Setenv("DEBUG", "1")
 	}
 	docker.GIT_COMMIT = GIT_COMMIT
+	docker.NO_MEMORY_LIMIT = NO_MEMORY_LIMIT == "1"
 	if *flDaemon {
 		if flag.NArg() != 0 {
 			flag.Usage()
 			return
 		}
+		if NO_MEMORY_LIMIT == "1" {
+			log.Printf("WARNING: This version of docker has been compiled without memory limit support.")
+		}
 		if err := daemon(*pidfile); err != nil {
 			log.Fatal(err)
 		}

+ 2 - 0
runtime_test.go

@@ -48,6 +48,8 @@ func layerArchive(tarfile string) (io.Reader, error) {
 }
 
 func init() {
+	NO_MEMORY_LIMIT = os.Getenv("NO_MEMORY_LIMIT") == "1"
+
 	// Hack to run sys init during unit testing
 	if SelfPath() == "/sbin/init" {
 		SysInit()