From 3262565d617b2fd17420ad004c3b92383a6b9000 Mon Sep 17 00:00:00 2001 From: Matthew Heon Date: Mon, 23 Oct 2017 11:36:10 -0400 Subject: [PATCH] Add support for setting conmon sockets directory in libpod Signed-off-by: Matthew Heon --- libpod/oci.go | 26 ++++++++++++++++++++++++-- libpod/options.go | 23 +++++++++++++++++++---- libpod/runtime.go | 25 ++++++++++++++++++------- 3 files changed, 61 insertions(+), 13 deletions(-) diff --git a/libpod/oci.go b/libpod/oci.go index 3700e6f5..182f01ab 100644 --- a/libpod/oci.go +++ b/libpod/oci.go @@ -41,7 +41,9 @@ type OCIRuntime struct { conmonPath string conmonEnv []string cgroupManager string + tmpDir string exitsDir string + socketsDir string logSizeMax int64 noPivot bool } @@ -53,21 +55,40 @@ type syncInfo struct { } // Make a new OCI runtime with provided options -func newOCIRuntime(name string, path string, conmonPath string, conmonEnv []string, cgroupManager string, exitsDir string, logSizeMax int64, noPivotRoot bool) (*OCIRuntime, error) { +func newOCIRuntime(name string, path string, conmonPath string, conmonEnv []string, cgroupManager string, tmpDir string, logSizeMax int64, noPivotRoot bool) (*OCIRuntime, error) { runtime := new(OCIRuntime) runtime.name = name runtime.path = path runtime.conmonPath = conmonPath runtime.conmonEnv = conmonEnv runtime.cgroupManager = cgroupManager - runtime.exitsDir = exitsDir + runtime.tmpDir = tmpDir runtime.logSizeMax = logSizeMax runtime.noPivot = noPivotRoot + runtime.exitsDir = filepath.Join(runtime.tmpDir, "exits") + runtime.socketsDir = filepath.Join(runtime.tmpDir, "socket") + if cgroupManager != CgroupfsCgroupsManager && cgroupManager != SystemdCgroupsManager { return nil, errors.Wrapf(ErrInvalidArg, "invalid cgroup manager specified: %s", cgroupManager) } + // Create the exit files and attach sockets directories + if err := os.MkdirAll(runtime.exitsDir, 0750); err != nil { + // The directory is allowed to exist + if !os.IsExist(err) { + return nil, errors.Wrapf(err, "error creating OCI runtime exit files directory %s", + runtime.exitsDir) + } + } + if err := os.MkdirAll(runtime.socketsDir, 0750); err != nil { + // The directory is allowed to exist + if !os.IsExist(err) { + return nil, errors.Wrapf(err, "error creating OCI runtime attach sockets directory %s", + runtime.socketsDir) + } + } + return runtime, nil } @@ -117,6 +138,7 @@ func (r *OCIRuntime) createContainer(ctr *Container, cgroupParent string) error // The default also likely shouldn't be this args = append(args, "-l", filepath.Join(ctr.config.StaticDir, "ctr.log")) args = append(args, "--exit-dir", r.exitsDir) + args = append(args, "--socket-dir-path", r.socketsDir) if ctr.config.Spec.Process.Terminal { args = append(args, "-t") } else if ctr.config.Stdin { diff --git a/libpod/options.go b/libpod/options.go index 9b709ce7..a5305d7a 100644 --- a/libpod/options.go +++ b/libpod/options.go @@ -150,15 +150,30 @@ func WithCgroupManager(manager string) RuntimeOption { } } -// WithExitsDir sets the directory that container exit files (containing exit -// codes) will be created by conmon -func WithExitsDir(dir string) RuntimeOption { +// WithStaticDir sets the directory that static runtime files which persist +// across reboots will be stored +func WithStaticDir(dir string) RuntimeOption { return func(rt *Runtime) error { if rt.valid { return ErrRuntimeFinalized } - rt.config.ExitsDir = dir + rt.config.StaticDir = dir + + return nil + } +} + +// WithTmpDir sets the directory that temporary runtime files which are not +// expected to survive across reboots will be stored +// This should be located on a tmpfs mount (/tmp or /var/run for example) +func WithTmpDir(dir string) RuntimeOption { + return func(rt *Runtime) error { + if rt.valid { + return ErrRuntimeFinalized + } + + rt.config.TmpDir = dir return nil } diff --git a/libpod/runtime.go b/libpod/runtime.go index 94266a8a..48c710bc 100644 --- a/libpod/runtime.go +++ b/libpod/runtime.go @@ -38,7 +38,8 @@ type RuntimeConfig struct { ConmonPath string ConmonEnvVars []string CgroupManager string - ExitsDir string + StaticDir string + TmpDir string SelinuxEnabled bool PidsLimit int64 MaxLogSize int64 @@ -56,7 +57,8 @@ var ( "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", }, CgroupManager: "cgroupfs", - ExitsDir: "/var/run/libpod/exits", + StaticDir: "/var/lib/libpod", + TmpDir: "/var/run/libpod", SelinuxEnabled: false, PidsLimit: 1024, MaxLogSize: -1, @@ -111,19 +113,28 @@ func NewRuntime(options ...RuntimeOption) (*Runtime, error) { // Make an OCI runtime to perform container operations ociRuntime, err := newOCIRuntime("runc", runtime.config.RuntimePath, runtime.config.ConmonPath, runtime.config.ConmonEnvVars, - runtime.config.CgroupManager, runtime.config.ExitsDir, + runtime.config.CgroupManager, runtime.config.TmpDir, runtime.config.MaxLogSize, runtime.config.NoPivotRoot) if err != nil { return nil, err } runtime.ociRuntime = ociRuntime - // Make the directory that will hold container exit files - if err := os.MkdirAll(runtime.config.ExitsDir, 0755); err != nil { + // Make the static files directory if it does not exist + if err := os.MkdirAll(runtime.config.StaticDir, 0755); err != nil { // The directory is allowed to exist if !os.IsExist(err) { - return nil, errors.Wrapf(err, "error creating container exit files directory %s", - runtime.config.ExitsDir) + return nil, errors.Wrapf(err, "error creating runtime static files directory %s", + runtime.config.StaticDir) + } + } + + // Make the per-boot files directory if it does not exist + if err := os.MkdirAll(runtime.config.TmpDir, 0755); err != nil { + // The directory is allowed to exist + if !os.IsExist(err) { + return nil, errors.Wrapf(err, "error creating runtime temporary files directory %s", + runtime.config.TmpDir) } }