summaryrefslogtreecommitdiff
path: root/libpod
diff options
context:
space:
mode:
Diffstat (limited to 'libpod')
-rw-r--r--libpod/container_api.go4
-rw-r--r--libpod/container_internal.go17
-rw-r--r--libpod/events.go17
-rw-r--r--libpod/events/events.go2
-rw-r--r--libpod/image/image.go67
-rw-r--r--libpod/networking_linux.go30
-rw-r--r--libpod/oci.go2
-rw-r--r--libpod/runtime.go156
-rw-r--r--libpod/runtime_volume_unsupported.go2
9 files changed, 199 insertions, 98 deletions
diff --git a/libpod/container_api.go b/libpod/container_api.go
index 3698a15ec..96435c2ff 100644
--- a/libpod/container_api.go
+++ b/libpod/container_api.go
@@ -14,7 +14,7 @@ import (
"github.com/containers/libpod/pkg/inspect"
"github.com/containers/libpod/pkg/lookup"
"github.com/containers/storage/pkg/stringid"
- "github.com/docker/docker/daemon/caps"
+ "github.com/docker/docker/oci/caps"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
@@ -89,7 +89,6 @@ func (c *Container) Start(ctx context.Context, recursive bool) (err error) {
}
// Start the container
- defer c.newContainerEvent(events.Start)
return c.start()
}
@@ -127,7 +126,6 @@ func (c *Container) StartAndAttach(ctx context.Context, streams *AttachStreams,
}
close(attachChan)
}()
- c.newContainerEvent(events.Start)
c.newContainerEvent(events.Attach)
return attachChan, nil
}
diff --git a/libpod/container_internal.go b/libpod/container_internal.go
index 330745314..872802016 100644
--- a/libpod/container_internal.go
+++ b/libpod/container_internal.go
@@ -211,6 +211,9 @@ func (c *Container) handleExitFile(exitFile string, fi os.FileInfo) error {
c.state.Exited = true
+ // Write an event for the container's death
+ c.newContainerExitedEvent(c.state.ExitCode)
+
return nil
}
@@ -334,11 +337,13 @@ func (c *Container) setupStorage(ctx context.Context) error {
}
// Set the default Entrypoint and Command
- if c.config.Entrypoint == nil {
- c.config.Entrypoint = containerInfo.Config.Config.Entrypoint
- }
- if c.config.Command == nil {
- c.config.Command = containerInfo.Config.Config.Cmd
+ if containerInfo.Config != nil {
+ if c.config.Entrypoint == nil {
+ c.config.Entrypoint = containerInfo.Config.Config.Entrypoint
+ }
+ if c.config.Command == nil {
+ c.config.Command = containerInfo.Config.Config.Cmd
+ }
}
artifacts := filepath.Join(c.config.StaticDir, artifactsDir)
@@ -948,6 +953,8 @@ func (c *Container) start() error {
c.state.State = ContainerStateRunning
+ defer c.newContainerEvent(events.Start)
+
return c.save()
}
diff --git a/libpod/events.go b/libpod/events.go
index 9806c117b..f09529a05 100644
--- a/libpod/events.go
+++ b/libpod/events.go
@@ -1,6 +1,8 @@
package libpod
import (
+ "os"
+
"github.com/containers/libpod/libpod/events"
"github.com/hpcloud/tail"
"github.com/pkg/errors"
@@ -19,6 +21,19 @@ func (c *Container) newContainerEvent(status events.Status) {
}
}
+// newContainerExitedEvent creates a new event for a container's death
+func (c *Container) newContainerExitedEvent(exitCode int32) {
+ e := events.NewEvent(events.Exited)
+ e.ID = c.ID()
+ e.Name = c.Name()
+ e.Image = c.config.RootfsImageName
+ e.Type = events.Container
+ e.ContainerExitCode = int(exitCode)
+ if err := e.Write(c.runtime.config.EventsLogFilePath); err != nil {
+ logrus.Errorf("unable to write event to %s", c.runtime.config.EventsLogFilePath)
+ }
+}
+
// newPodEvent creates a new event for a libpod pod
func (p *Pod) newPodEvent(status events.Status) {
e := events.NewEvent(status)
@@ -72,7 +87,7 @@ func (r *Runtime) Events(fromStart, stream bool, options []events.EventFilter, e
func (r *Runtime) getTail(fromStart, stream bool) (*tail.Tail, error) {
reopen := true
- seek := tail.SeekInfo{Offset: 0, Whence: 2}
+ seek := tail.SeekInfo{Offset: 0, Whence: os.SEEK_END}
if fromStart || !stream {
seek.Whence = 0
reopen = false
diff --git a/libpod/events/events.go b/libpod/events/events.go
index 186790500..48bbbb00e 100644
--- a/libpod/events/events.go
+++ b/libpod/events/events.go
@@ -60,6 +60,8 @@ const (
Create Status = "create"
// Exec ...
Exec Status = "exec"
+ // Exited indicates that a container's process died
+ Exited Status = "died"
// Export ...
Export Status = "export"
// History ...
diff --git a/libpod/image/image.go b/libpod/image/image.go
index 72f07dad1..c5939e055 100644
--- a/libpod/image/image.go
+++ b/libpod/image/image.go
@@ -1212,3 +1212,70 @@ func (i *Image) newImageEvent(status events.Status) {
logrus.Infof("unable to write event to %s", i.imageruntime.EventsLogFilePath)
}
}
+
+// LayerInfo keeps information of single layer
+type LayerInfo struct {
+ // Layer ID
+ ID string
+ // Parent ID of current layer.
+ ParentID string
+ // ChildID of current layer.
+ // there can be multiple children in case of fork
+ ChildID []string
+ // RepoTag will have image repo names, if layer is top layer of image
+ RepoTags []string
+ // Size stores Uncompressed size of layer.
+ Size int64
+}
+
+// GetLayersMapWithImageInfo returns map of image-layers, with associated information like RepoTags, parent and list of child layers.
+func GetLayersMapWithImageInfo(imageruntime *Runtime) (map[string]*LayerInfo, error) {
+
+ // Memory allocated to store map of layers with key LayerID.
+ // Map will build dependency chain with ParentID and ChildID(s)
+ layerInfoMap := make(map[string]*LayerInfo)
+
+ // scan all layers & fill size and parent id for each layer in layerInfoMap
+ layers, err := imageruntime.store.Layers()
+ if err != nil {
+ return nil, err
+ }
+ for _, layer := range layers {
+ _, ok := layerInfoMap[layer.ID]
+ if !ok {
+ layerInfoMap[layer.ID] = &LayerInfo{
+ ID: layer.ID,
+ Size: layer.UncompressedSize,
+ ParentID: layer.Parent,
+ }
+ } else {
+ return nil, fmt.Errorf("detected multiple layers with the same ID %q", layer.ID)
+ }
+ }
+
+ // scan all layers & add all childs for each layers to layerInfo
+ for _, layer := range layers {
+ _, ok := layerInfoMap[layer.ID]
+ if ok {
+ if layer.Parent != "" {
+ layerInfoMap[layer.Parent].ChildID = append(layerInfoMap[layer.Parent].ChildID, layer.ID)
+ }
+ } else {
+ return nil, fmt.Errorf("lookup error: layer-id %s, not found", layer.ID)
+ }
+ }
+
+ // Add the Repo Tags to Top layer of each image.
+ imgs, err := imageruntime.store.Images()
+ if err != nil {
+ return nil, err
+ }
+ for _, img := range imgs {
+ e, ok := layerInfoMap[img.TopLayer]
+ if !ok {
+ return nil, fmt.Errorf("top-layer for image %s not found local store", img.ID)
+ }
+ e.RepoTags = append(e.RepoTags, img.Names...)
+ }
+ return layerInfoMap, nil
+}
diff --git a/libpod/networking_linux.go b/libpod/networking_linux.go
index 80d7d8213..d8b0cffcb 100644
--- a/libpod/networking_linux.go
+++ b/libpod/networking_linux.go
@@ -134,6 +134,15 @@ type slirp4netnsCmd struct {
Args slirp4netnsCmdArg `json:"arguments"`
}
+func checkSlirpFlags(path string) (bool, bool, error) {
+ cmd := exec.Command(path, "--help")
+ out, err := cmd.CombinedOutput()
+ if err != nil {
+ return false, false, err
+ }
+ return strings.Contains(string(out), "--disable-host-loopback"), strings.Contains(string(out), "--mtu"), nil
+}
+
// Configure the network namespace for a rootless container
func (r *Runtime) setupRootlessNetNS(ctr *Container) (err error) {
defer ctr.rootlessSlirpSyncR.Close()
@@ -159,13 +168,24 @@ func (r *Runtime) setupRootlessNetNS(ctr *Container) (err error) {
havePortMapping := len(ctr.Config().PortMappings) > 0
apiSocket := filepath.Join(r.ociRuntime.tmpDir, fmt.Sprintf("%s.net", ctr.config.ID))
- var cmd *exec.Cmd
+
+ cmdArgs := []string{}
if havePortMapping {
- // if we need ports to be mapped from the host, create a API socket to use for communicating with slirp4netns.
- cmd = exec.Command(path, "-c", "-e", "3", "-r", "4", "--api-socket", apiSocket, fmt.Sprintf("%d", ctr.state.PID), "tap0")
- } else {
- cmd = exec.Command(path, "-c", "-e", "3", "-r", "4", fmt.Sprintf("%d", ctr.state.PID), "tap0")
+ cmdArgs = append(cmdArgs, "--api-socket", apiSocket, fmt.Sprintf("%d", ctr.state.PID))
}
+ dhp, mtu, err := checkSlirpFlags(path)
+ if err != nil {
+ return errors.Wrapf(err, "error checking slirp4netns binary %s", path)
+ }
+ if dhp {
+ cmdArgs = append(cmdArgs, "--disable-host-loopback")
+ }
+ if mtu {
+ cmdArgs = append(cmdArgs, "--mtu", "65520")
+ }
+ cmdArgs = append(cmdArgs, "-c", "-e", "3", "-r", "4", fmt.Sprintf("%d", ctr.state.PID), "tap0")
+
+ cmd := exec.Command(path, cmdArgs...)
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
diff --git a/libpod/oci.go b/libpod/oci.go
index c3b5f9af2..30360d289 100644
--- a/libpod/oci.go
+++ b/libpod/oci.go
@@ -477,7 +477,7 @@ func (r *OCIRuntime) updateContainerStatus(ctr *Container, useRunc bool) error {
// If not using runc, we don't need to do most of this.
if !useRunc {
// If the container's not running, nothing to do.
- if ctr.state.State != ContainerStateRunning {
+ if ctr.state.State != ContainerStateRunning && ctr.state.State != ContainerStatePaused {
return nil
}
diff --git a/libpod/runtime.go b/libpod/runtime.go
index fa208a2ca..9836b7aab 100644
--- a/libpod/runtime.go
+++ b/libpod/runtime.go
@@ -241,6 +241,12 @@ type runtimeConfiguredFrom struct {
libpodStaticDirSet bool
libpodTmpDirSet bool
volPathSet bool
+ conmonPath bool
+ conmonEnvVars bool
+ ociRuntimes bool
+ runtimePath bool
+ cniPluginDir bool
+ noPivotRoot bool
}
var (
@@ -324,6 +330,22 @@ func SetXdgRuntimeDir(val string) error {
// NewRuntime creates a new container runtime
// Options can be passed to override the default configuration for the runtime
func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) {
+ return newRuntimeFromConfig("", options...)
+}
+
+// NewRuntimeFromConfig creates a new container runtime using the given
+// configuration file for its default configuration. Passed RuntimeOption
+// functions can be used to mutate this configuration further.
+// An error will be returned if the configuration file at the given path does
+// not exist or cannot be loaded
+func NewRuntimeFromConfig(userConfigPath string, options ...RuntimeOption) (runtime *Runtime, err error) {
+ if userConfigPath == "" {
+ return nil, errors.New("invalid configuration file specified")
+ }
+ return newRuntimeFromConfig(userConfigPath, options...)
+}
+
+func newRuntimeFromConfig(userConfigPath string, options ...RuntimeOption) (runtime *Runtime, err error) {
runtime = new(Runtime)
runtime.config = new(RuntimeConfig)
runtime.configuredFrom = new(runtimeConfiguredFrom)
@@ -358,11 +380,6 @@ func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) {
rootlessConfigPath = filepath.Join(home, ".config/containers/libpod.conf")
- configPath = rootlessConfigPath
- if _, err := os.Stat(configPath); err != nil {
- foundConfig = false
- }
-
runtimeDir, err := util.GetRootlessRuntimeDir()
if err != nil {
return nil, err
@@ -374,6 +391,20 @@ func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) {
return nil, errors.Wrapf(err, "cannot set XDG_RUNTIME_DIR")
}
+ }
+
+ if userConfigPath != "" {
+ configPath = userConfigPath
+ if _, err := os.Stat(configPath); err != nil {
+ // If the user specified a config file, we must fail immediately
+ // when it doesn't exist
+ return nil, errors.Wrapf(err, "cannot stat %s", configPath)
+ }
+ } else if rootless.IsRootless() {
+ configPath = rootlessConfigPath
+ if _, err := os.Stat(configPath); err != nil {
+ foundConfig = false
+ }
} else if _, err := os.Stat(OverrideConfigPath); err == nil {
// Use the override configuration path
configPath = OverrideConfigPath
@@ -409,6 +440,24 @@ func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) {
if tmpConfig.VolumePath != "" {
runtime.configuredFrom.volPathSet = true
}
+ if tmpConfig.ConmonPath != nil {
+ runtime.configuredFrom.conmonPath = true
+ }
+ if tmpConfig.ConmonEnvVars != nil {
+ runtime.configuredFrom.conmonEnvVars = true
+ }
+ if tmpConfig.OCIRuntimes != nil {
+ runtime.configuredFrom.ociRuntimes = true
+ }
+ if tmpConfig.RuntimePath != nil {
+ runtime.configuredFrom.runtimePath = true
+ }
+ if tmpConfig.CNIPluginDir != nil {
+ runtime.configuredFrom.cniPluginDir = true
+ }
+ if tmpConfig.NoPivotRoot {
+ runtime.configuredFrom.noPivotRoot = true
+ }
if _, err := toml.Decode(string(contents), runtime.config); err != nil {
return nil, errors.Wrapf(err, "error decoding configuration file %s", configPath)
@@ -428,12 +477,24 @@ func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) {
}
// Cherry pick the settings we want from the global configuration
- runtime.config.ConmonPath = tmpConfig.ConmonPath
- runtime.config.ConmonEnvVars = tmpConfig.ConmonEnvVars
- runtime.config.OCIRuntimes = tmpConfig.OCIRuntimes
- runtime.config.RuntimePath = tmpConfig.RuntimePath
- runtime.config.CNIPluginDir = tmpConfig.CNIPluginDir
- runtime.config.NoPivotRoot = tmpConfig.NoPivotRoot
+ if !runtime.configuredFrom.conmonPath {
+ runtime.config.ConmonPath = tmpConfig.ConmonPath
+ }
+ if !runtime.configuredFrom.conmonEnvVars {
+ runtime.config.ConmonEnvVars = tmpConfig.ConmonEnvVars
+ }
+ if !runtime.configuredFrom.ociRuntimes {
+ runtime.config.OCIRuntimes = tmpConfig.OCIRuntimes
+ }
+ if !runtime.configuredFrom.runtimePath {
+ runtime.config.RuntimePath = tmpConfig.RuntimePath
+ }
+ if !runtime.configuredFrom.cniPluginDir {
+ runtime.config.CNIPluginDir = tmpConfig.CNIPluginDir
+ }
+ if !runtime.configuredFrom.noPivotRoot {
+ runtime.config.NoPivotRoot = tmpConfig.NoPivotRoot
+ }
break
}
}
@@ -465,80 +526,9 @@ func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) {
return runtime, nil
}
-// NewRuntimeFromConfig creates a new container runtime using the given
-// configuration file for its default configuration. Passed RuntimeOption
-// functions can be used to mutate this configuration further.
-// An error will be returned if the configuration file at the given path does
-// not exist or cannot be loaded
-func NewRuntimeFromConfig(configPath string, options ...RuntimeOption) (runtime *Runtime, err error) {
- runtime = new(Runtime)
- runtime.config = new(RuntimeConfig)
- runtime.configuredFrom = new(runtimeConfiguredFrom)
-
- // Set three fields not in the TOML config
- runtime.config.StateType = defaultRuntimeConfig.StateType
- runtime.config.OCIRuntime = defaultRuntimeConfig.OCIRuntime
-
- storageConf, err := util.GetDefaultStoreOptions()
- if err != nil {
- return nil, errors.Wrapf(err, "error retrieving storage config")
- }
- runtime.config.StorageConfig = storageConf
- runtime.config.StaticDir = filepath.Join(storageConf.GraphRoot, "libpod")
- runtime.config.VolumePath = filepath.Join(storageConf.GraphRoot, "volumes")
-
- tmpDir, err := getDefaultTmpDir()
- if err != nil {
- return nil, err
- }
- runtime.config.TmpDir = tmpDir
- if rootless.IsRootless() {
- runtimeDir, err := util.GetRootlessRuntimeDir()
- if err != nil {
- return nil, err
- }
- // containers/image uses XDG_RUNTIME_DIR to locate the auth file.
- // So make sure the env variable is set.
- if err := SetXdgRuntimeDir(runtimeDir); err != nil {
- return nil, errors.Wrapf(err, "cannot set XDG_RUNTIME_DIR")
- }
- }
-
- // Check to see if the given configuration file exists
- if _, err := os.Stat(configPath); err != nil {
- return nil, errors.Wrapf(err, "error checking existence of configuration file %s", configPath)
- }
-
- // Read contents of the config file
- contents, err := ioutil.ReadFile(configPath)
- if err != nil {
- return nil, errors.Wrapf(err, "error reading configuration file %s", configPath)
- }
-
- // Decode configuration file
- if _, err := toml.Decode(string(contents), runtime.config); err != nil {
- return nil, errors.Wrapf(err, "error decoding configuration from file %s", configPath)
- }
-
- // Overwrite the config with user-given configuration options
- for _, opt := range options {
- if err := opt(runtime); err != nil {
- return nil, errors.Wrapf(err, "error configuring runtime")
- }
- }
-
- if err := makeRuntime(runtime); err != nil {
- return nil, err
- }
-
- return runtime, nil
-}
-
// Make a new runtime based on the given configuration
// Sets up containers/storage, state store, OCI runtime
func makeRuntime(runtime *Runtime) (err error) {
- runtime.config.EventsLogFilePath = filepath.Join(runtime.config.TmpDir, "events", "events.log")
-
// Backward compatibility for `runtime_path`
if runtime.config.RuntimePath != nil {
// Don't print twice in rootless mode.
@@ -697,6 +687,8 @@ func makeRuntime(runtime *Runtime) (err error) {
runtime.config.VolumePath = dbConfig.VolumePath
}
+ runtime.config.EventsLogFilePath = filepath.Join(runtime.config.TmpDir, "events", "events.log")
+
logrus.Debugf("Using graph driver %s", runtime.config.StorageConfig.GraphDriverName)
logrus.Debugf("Using graph root %s", runtime.config.StorageConfig.GraphRoot)
logrus.Debugf("Using run root %s", runtime.config.StorageConfig.RunRoot)
diff --git a/libpod/runtime_volume_unsupported.go b/libpod/runtime_volume_unsupported.go
index d87459759..5fe487114 100644
--- a/libpod/runtime_volume_unsupported.go
+++ b/libpod/runtime_volume_unsupported.go
@@ -6,7 +6,7 @@ import (
"context"
)
-func (r *Runtime) removeVolume(ctx context.Context, v *Volume, force, prune bool) error {
+func (r *Runtime) removeVolume(ctx context.Context, v *Volume, force bool) error {
return ErrNotImplemented
}