From 1df5f4094bf31edba6f2a2f07bfdada54340c1e4 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Fri, 5 Apr 2013 18:00:10 -0700 Subject: [PATCH 01/10] docker run -v PATH: bind a new data volume to a container --- commands.go | 20 ++++++++++++++++++++ container.go | 5 +++++ runtime.go | 6 ++++++ 3 files changed, 31 insertions(+) diff --git a/commands.go b/commands.go index 4be282bce2..cb7b57ff00 100644 --- a/commands.go +++ b/commands.go @@ -10,6 +10,7 @@ import ( "log" "net/http" "net/url" + "path/filepath" "runtime" "strconv" "strings" @@ -913,6 +914,25 @@ func (opts AttachOpts) Get(val string) bool { return false } +// PathOpts stores a unique set of absolute paths +type PathOpts map[string]struct{} + +func NewPathOpts() PathOpts { + return make(PathOpts) +} + +func (opts PathOpts) String() string { + return fmt.Sprintf("%v", map[string]struct{}(opts)) +} + +func (opts PathOpts) Set(val string) error { + if !filepath.IsAbs(val) { + return fmt.Errorf("%s is not an absolute path", val) + } + opts[filepath.Clean(val)] = struct{}{} + return nil +} + func (srv *Server) CmdTag(stdin io.ReadCloser, stdout io.Writer, args ...string) error { cmd := rcli.Subcmd(stdout, "tag", "[OPTIONS] IMAGE REPOSITORY [TAG]", "Tag an image into a repository") force := cmd.Bool("f", false, "Force") diff --git a/container.go b/container.go index bac0951da4..109c8b158b 100644 --- a/container.go +++ b/container.go @@ -66,6 +66,7 @@ type Config struct { Cmd []string Dns []string Image string // Name of the image as it was passed by the operator (eg. could be symbolic) + Volumes map[string]struct{} } func ParseRun(args []string, stdout io.Writer, capabilities *Capabilities) (*Config, error) { @@ -97,6 +98,9 @@ func ParseRun(args []string, stdout io.Writer, capabilities *Capabilities) (*Con var flDns ListOpts cmd.Var(&flDns, "dns", "Set custom dns servers") + flVolumes := NewPathOpts() + cmd.Var(flVolumes, "v", "Attach a data volume") + if err := cmd.Parse(args); err != nil { return nil, err } @@ -136,6 +140,7 @@ func ParseRun(args []string, stdout io.Writer, capabilities *Capabilities) (*Con Cmd: runCmd, Dns: flDns, Image: image, + Volumes: flVolumes, } if *flMemory > 0 && !capabilities.SwapLimit { diff --git a/runtime.go b/runtime.go index 6e03226b36..16c98117fd 100644 --- a/runtime.go +++ b/runtime.go @@ -32,6 +32,7 @@ type Runtime struct { capabilities *Capabilities kernelVersion *KernelVersionInfo autoRestart bool + volumes *Graph } var sysInitPath string @@ -405,6 +406,10 @@ func NewRuntimeFromDirectory(root string, autoRestart bool) (*Runtime, error) { if err != nil { return nil, err } + volumes, err := NewGraph(path.Join(root, "volumes")) + if err != nil { + return nil, err + } repositories, err := NewTagStore(path.Join(root, "repositories"), g) if err != nil { return nil, fmt.Errorf("Couldn't create Tag store: %s", err) @@ -432,6 +437,7 @@ func NewRuntimeFromDirectory(root string, autoRestart bool) (*Runtime, error) { idIndex: NewTruncIndex(), capabilities: &Capabilities{}, autoRestart: autoRestart, + volumes: volumes, } if err := runtime.restore(); err != nil { From 35d704c8a006263d1539994f02c7bfbe97202528 Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Tue, 9 Apr 2013 18:08:03 -0700 Subject: [PATCH 02/10] Change the volumes type to map[string]string to store both source and destination --- commands.go | 6 +++--- container.go | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/commands.go b/commands.go index cb7b57ff00..5f5f8d36f9 100644 --- a/commands.go +++ b/commands.go @@ -915,21 +915,21 @@ func (opts AttachOpts) Get(val string) bool { } // PathOpts stores a unique set of absolute paths -type PathOpts map[string]struct{} +type PathOpts map[string]string func NewPathOpts() PathOpts { return make(PathOpts) } func (opts PathOpts) String() string { - return fmt.Sprintf("%v", map[string]struct{}(opts)) + return fmt.Sprintf("%v", map[string]string(opts)) } func (opts PathOpts) Set(val string) error { if !filepath.IsAbs(val) { return fmt.Errorf("%s is not an absolute path", val) } - opts[filepath.Clean(val)] = struct{}{} + opts[filepath.Clean(val)] = "" return nil } diff --git a/container.go b/container.go index 109c8b158b..aaa8723662 100644 --- a/container.go +++ b/container.go @@ -66,7 +66,7 @@ type Config struct { Cmd []string Dns []string Image string // Name of the image as it was passed by the operator (eg. could be symbolic) - Volumes map[string]struct{} + Volumes map[string]string } func ParseRun(args []string, stdout io.Writer, capabilities *Capabilities) (*Config, error) { @@ -461,6 +461,7 @@ func (container *Container) Start() error { // Init the lock container.waitLock = make(chan struct{}) + container.ToDisk() go container.monitor() return nil From 8d9aaee60bf03397e51e8c84232a73fcb3baf36c Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Tue, 9 Apr 2013 18:19:55 -0700 Subject: [PATCH 03/10] Handle data volumes mount points --- container.go | 17 +++++++++++++++++ lxc_template.go | 6 +++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/container.go b/container.go index aaa8723662..4475fdac40 100644 --- a/container.go +++ b/container.go @@ -400,9 +400,26 @@ func (container *Container) Start() error { container.Config.MemorySwap = -1 } + // Create the requested volumes volumes + for volPath := range container.Config.Volumes { + if c, err := container.runtime.volumes.Create(nil, container, ""); err != nil { + return err + } else { + if err := os.MkdirAll(path.Join(container.RootfsPath(), volPath), 0755); err != nil { + return nil + } + root, err := c.root() + if err != nil { + return err + } + container.Config.Volumes[volPath] = root + } + } + if err := container.generateLXCConfig(); err != nil { return err } + params := []string{ "-n", container.Id, "-f", container.lxcConfigPath(), diff --git a/lxc_template.go b/lxc_template.go index 5ac62f52af..fd6461bb13 100644 --- a/lxc_template.go +++ b/lxc_template.go @@ -79,7 +79,11 @@ lxc.mount.entry = {{.SysInitPath}} {{$ROOTFS}}/sbin/init none bind,ro 0 0 # In order to get a working DNS environment, mount bind (ro) the host's /etc/resolv.conf into the container lxc.mount.entry = {{.ResolvConfPath}} {{$ROOTFS}}/etc/resolv.conf none bind,ro 0 0 - +{{if .Config.Volumes}} +{{range $T0, $T1 := .Config.Volumes}} +lxc.mount.entry = {{$T1}}/layer {{$ROOTFS}}/{{$T0}} none bind,rw 0 0 +{{end}} +{{end}} # drop linux capabilities (apply mainly to the user root in the container) lxc.cap.drop = audit_control audit_write mac_admin mac_override mknod setfcap setpcap sys_admin sys_boot sys_module sys_nice sys_pacct sys_rawio sys_resource sys_time sys_tty_config From faf8daa7c68f2511dedfad15e1fd05f9eff077a2 Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Wed, 10 Apr 2013 16:09:34 -0700 Subject: [PATCH 04/10] Switch back config to map[string]struct{} --- commands.go | 6 +++--- container.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/commands.go b/commands.go index 5f5f8d36f9..cb7b57ff00 100644 --- a/commands.go +++ b/commands.go @@ -915,21 +915,21 @@ func (opts AttachOpts) Get(val string) bool { } // PathOpts stores a unique set of absolute paths -type PathOpts map[string]string +type PathOpts map[string]struct{} func NewPathOpts() PathOpts { return make(PathOpts) } func (opts PathOpts) String() string { - return fmt.Sprintf("%v", map[string]string(opts)) + return fmt.Sprintf("%v", map[string]struct{}(opts)) } func (opts PathOpts) Set(val string) error { if !filepath.IsAbs(val) { return fmt.Errorf("%s is not an absolute path", val) } - opts[filepath.Clean(val)] = "" + opts[filepath.Clean(val)] = struct{}{} return nil } diff --git a/container.go b/container.go index 4475fdac40..2edcd6776b 100644 --- a/container.go +++ b/container.go @@ -66,7 +66,7 @@ type Config struct { Cmd []string Dns []string Image string // Name of the image as it was passed by the operator (eg. could be symbolic) - Volumes map[string]string + Volumes map[string]struct{} } func ParseRun(args []string, stdout io.Writer, capabilities *Capabilities) (*Config, error) { From 6fb495bf6ff545753518e9b9ed39c2676230c277 Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Wed, 10 Apr 2013 16:10:53 -0700 Subject: [PATCH 05/10] Move the id of volumes to Container (instead of Container.Config) --- container.go | 24 +++++++++++++++++++----- lxc_template.go | 6 +++--- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/container.go b/container.go index 2edcd6776b..5de931dc43 100644 --- a/container.go +++ b/container.go @@ -48,6 +48,7 @@ type Container struct { runtime *Runtime waitLock chan struct{} + Volumes map[string]string } type Config struct { @@ -399,6 +400,7 @@ func (container *Container) Start() error { log.Printf("WARNING: Your kernel does not support swap limit capabilities. Limitation discarded.\n") container.Config.MemorySwap = -1 } + container.Volumes = make(map[string]string) // Create the requested volumes volumes for volPath := range container.Config.Volumes { @@ -408,11 +410,7 @@ func (container *Container) Start() error { if err := os.MkdirAll(path.Join(container.RootfsPath(), volPath), 0755); err != nil { return nil } - root, err := c.root() - if err != nil { - return err - } - container.Config.Volumes[volPath] = root + container.Volumes[volPath] = c.Id } } @@ -810,6 +808,22 @@ func (container *Container) RootfsPath() string { return path.Join(container.root, "rootfs") } +func (container *Container) GetVolumes() (map[string]string, error) { + ret := make(map[string]string) + for volPath, id := range container.Volumes { + volume, err := container.runtime.volumes.Get(id) + if err != nil { + return nil, err + } + root, err := volume.root() + if err != nil { + return nil, err + } + ret[volPath] = path.Join(root, "layer") + } + return ret, nil +} + func (container *Container) rwPath() string { return path.Join(container.root, "rw") } diff --git a/lxc_template.go b/lxc_template.go index fd6461bb13..e2be3f21cd 100644 --- a/lxc_template.go +++ b/lxc_template.go @@ -79,9 +79,9 @@ lxc.mount.entry = {{.SysInitPath}} {{$ROOTFS}}/sbin/init none bind,ro 0 0 # In order to get a working DNS environment, mount bind (ro) the host's /etc/resolv.conf into the container lxc.mount.entry = {{.ResolvConfPath}} {{$ROOTFS}}/etc/resolv.conf none bind,ro 0 0 -{{if .Config.Volumes}} -{{range $T0, $T1 := .Config.Volumes}} -lxc.mount.entry = {{$T1}}/layer {{$ROOTFS}}/{{$T0}} none bind,rw 0 0 +{{if .Volumes}} +{{range $virtualPath, $realPath := .GetVolumes}} +lxc.mount.entry = {{$realPath}} {{$ROOTFS}}/{{$virtualPath}} none bind,rw 0 0 {{end}} {{end}} From 4099a31304863edf5b4d1b594b41c7ce1a33c5e1 Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Wed, 10 Apr 2013 16:23:30 -0700 Subject: [PATCH 06/10] Implement the -volumes-from in order to mount volumes from an other container --- container.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/container.go b/container.go index 5de931dc43..7bd5a791ac 100644 --- a/container.go +++ b/container.go @@ -68,6 +68,7 @@ type Config struct { Dns []string Image string // Name of the image as it was passed by the operator (eg. could be symbolic) Volumes map[string]struct{} + VolumesFrom string } func ParseRun(args []string, stdout io.Writer, capabilities *Capabilities) (*Config, error) { @@ -102,6 +103,8 @@ func ParseRun(args []string, stdout io.Writer, capabilities *Capabilities) (*Con flVolumes := NewPathOpts() cmd.Var(flVolumes, "v", "Attach a data volume") + flVolumesFrom := cmd.String("volumes-from", "", "Mount volumes from the specified container") + if err := cmd.Parse(args); err != nil { return nil, err } @@ -142,6 +145,7 @@ func ParseRun(args []string, stdout io.Writer, capabilities *Capabilities) (*Con Dns: flDns, Image: image, Volumes: flVolumes, + VolumesFrom: *flVolumesFrom, } if *flMemory > 0 && !capabilities.SwapLimit { @@ -414,6 +418,22 @@ func (container *Container) Start() error { } } + if container.Config.VolumesFrom != "" { + c := container.runtime.Get(container.Config.VolumesFrom) + if c == nil { + return fmt.Errorf("Container %s not found. Impossible to mount its volumes") + } + for volPath, id := range c.Volumes { + if _, exists := container.Volumes[volPath]; exists { + return fmt.Errorf("The requested volume %s overlap one of the volume of the container %s", volPath, c.Id) + } + if err := os.MkdirAll(path.Join(container.RootfsPath(), volPath), 0755); err != nil { + return nil + } + container.Volumes[volPath] = id + } + } + if err := container.generateLXCConfig(); err != nil { return err } From 3edd14b8c2eb13f45834f5d9306a579b256f9348 Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Fri, 12 Apr 2013 09:23:57 -0700 Subject: [PATCH 07/10] Implement the data volume removal --- commands.go | 28 +++++++++++++++++++++++++++- container.go | 2 +- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/commands.go b/commands.go index cb7b57ff00..732fdcaaaa 100644 --- a/commands.go +++ b/commands.go @@ -401,7 +401,8 @@ func (srv *Server) CmdHistory(stdin io.ReadCloser, stdout io.Writer, args ...str } func (srv *Server) CmdRm(stdin io.ReadCloser, stdout io.Writer, args ...string) error { - cmd := rcli.Subcmd(stdout, "rm", "CONTAINER [CONTAINER...]", "Remove a container") + cmd := rcli.Subcmd(stdout, "rm", "[OPTIONS] CONTAINER [CONTAINER...]", "Remove a container") + v := cmd.Bool("v", false, "Remove the volumes associated to the container") if err := cmd.Parse(args); err != nil { return nil } @@ -409,15 +410,40 @@ func (srv *Server) CmdRm(stdin io.ReadCloser, stdout io.Writer, args ...string) cmd.Usage() return nil } + volumes := make(map[string]struct{}) for _, name := range cmd.Args() { container := srv.runtime.Get(name) if container == nil { return fmt.Errorf("No such container: %s", name) } + // Store all the deleted containers volumes + for _, volumeId := range container.Volumes { + volumes[volumeId] = struct{}{} + } if err := srv.runtime.Destroy(container); err != nil { fmt.Fprintln(stdout, "Error destroying container "+name+": "+err.Error()) } } + if *v { + // Retrieve all volumes from all remaining containers + usedVolumes := make(map[string]*Container) + for _, container := range srv.runtime.List() { + for _, containerVolumeId := range container.Volumes { + usedVolumes[containerVolumeId] = container + } + } + + for volumeId := range volumes { + // If the requested volu + if c, exists := usedVolumes[volumeId]; exists { + fmt.Fprintf(stdout, "The volume %s is used by the container %s. Impossible to remove it. Skipping.\n", volumeId, c.Id) + continue + } + if err := srv.runtime.volumes.Delete(volumeId); err != nil { + return err + } + } + } return nil } diff --git a/container.go b/container.go index 7bd5a791ac..92bcc2506d 100644 --- a/container.go +++ b/container.go @@ -421,7 +421,7 @@ func (container *Container) Start() error { if container.Config.VolumesFrom != "" { c := container.runtime.Get(container.Config.VolumesFrom) if c == nil { - return fmt.Errorf("Container %s not found. Impossible to mount its volumes") + return fmt.Errorf("Container %s not found. Impossible to mount its volumes", container.Id) } for volPath, id := range c.Volumes { if _, exists := container.Volumes[volPath]; exists { From b0459adc271b20cde11da6a74cc5a71bab23f2ac Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Mon, 22 Apr 2013 18:00:10 -0700 Subject: [PATCH 08/10] Comply to the new graph.Create() prototype --- container.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/container.go b/container.go index 92bcc2506d..98e62c8849 100644 --- a/container.go +++ b/container.go @@ -408,7 +408,7 @@ func (container *Container) Start() error { // Create the requested volumes volumes for volPath := range container.Config.Volumes { - if c, err := container.runtime.volumes.Create(nil, container, ""); err != nil { + if c, err := container.runtime.volumes.Create(nil, container, "", ""); err != nil { return err } else { if err := os.MkdirAll(path.Join(container.RootfsPath(), volPath), 0755); err != nil { From 897cc573f051f1d88be0fde05946e66d0c86c43e Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Thu, 2 May 2013 09:23:29 -0700 Subject: [PATCH 09/10] Fix the graph.Create prototype --- container.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/container.go b/container.go index 98e62c8849..dc57a31135 100644 --- a/container.go +++ b/container.go @@ -408,7 +408,7 @@ func (container *Container) Start() error { // Create the requested volumes volumes for volPath := range container.Config.Volumes { - if c, err := container.runtime.volumes.Create(nil, container, "", ""); err != nil { + if c, err := container.runtime.volumes.Create(nil, container, "", "", nil); err != nil { return err } else { if err := os.MkdirAll(path.Join(container.RootfsPath(), volPath), 0755); err != nil { From 21b9dcd518871088086943f95bf39f068b30691f Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Thu, 2 May 2013 09:26:29 -0700 Subject: [PATCH 10/10] Update docs for Command Run --- docs/sources/commandline/command/run.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/sources/commandline/command/run.rst b/docs/sources/commandline/command/run.rst index c2096b3bd9..7d394c0d9d 100644 --- a/docs/sources/commandline/command/run.rst +++ b/docs/sources/commandline/command/run.rst @@ -17,3 +17,5 @@ -p=[]: Map a network port to the container -t=false: Allocate a pseudo-tty -u="": Username or UID + -d=[]: Set custom dns servers for the container + -v=[]: Creates a new volumes and mount it at the specified path. A container ID can be passed instead of a path in order to mount all volumes from the given container.