2017-08-11 16:42:25 +00:00
|
|
|
package libpod
|
|
|
|
|
|
|
|
import (
|
2017-10-10 20:33:20 +00:00
|
|
|
"os"
|
2017-08-28 21:33:02 +00:00
|
|
|
"sync"
|
|
|
|
|
2017-10-24 22:40:50 +00:00
|
|
|
is "github.com/containers/image/storage"
|
2017-08-28 21:33:02 +00:00
|
|
|
"github.com/containers/image/types"
|
2017-08-15 15:25:35 +00:00
|
|
|
"github.com/containers/storage"
|
2017-08-28 21:33:02 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-10-27 13:50:09 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-08-28 21:33:02 +00:00
|
|
|
"github.com/ulule/deepcopier"
|
2017-08-11 16:42:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// A RuntimeOption is a functional option which alters the Runtime created by
|
|
|
|
// NewRuntime
|
|
|
|
type RuntimeOption func(*Runtime) error
|
|
|
|
|
|
|
|
// Runtime is the core libpod runtime
|
|
|
|
type Runtime struct {
|
2017-10-10 20:33:20 +00:00
|
|
|
config *RuntimeConfig
|
|
|
|
state State
|
|
|
|
store storage.Store
|
|
|
|
storageService *storageService
|
|
|
|
imageContext *types.SystemContext
|
|
|
|
ociRuntime *OCIRuntime
|
|
|
|
valid bool
|
|
|
|
lock sync.RWMutex
|
2017-08-28 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RuntimeConfig contains configuration options used to set up the runtime
|
|
|
|
type RuntimeConfig struct {
|
|
|
|
StorageConfig storage.StoreOptions
|
|
|
|
ImageDefaultTransport string
|
|
|
|
InsecureRegistries []string
|
|
|
|
Registries []string
|
|
|
|
SignaturePolicyPath string
|
|
|
|
RuntimePath string
|
|
|
|
ConmonPath string
|
|
|
|
ConmonEnvVars []string
|
|
|
|
CgroupManager string
|
2017-10-23 15:36:10 +00:00
|
|
|
StaticDir string
|
|
|
|
TmpDir string
|
2017-08-28 21:33:02 +00:00
|
|
|
SelinuxEnabled bool
|
|
|
|
PidsLimit int64
|
2017-10-10 20:33:20 +00:00
|
|
|
MaxLogSize int64
|
|
|
|
NoPivotRoot bool
|
2017-08-11 16:42:25 +00:00
|
|
|
}
|
|
|
|
|
2017-08-28 21:33:02 +00:00
|
|
|
var (
|
|
|
|
defaultRuntimeConfig = RuntimeConfig{
|
|
|
|
// Leave this empty so containers/storage will use its defaults
|
|
|
|
StorageConfig: storage.StoreOptions{},
|
|
|
|
ImageDefaultTransport: "docker://",
|
|
|
|
RuntimePath: "/usr/bin/runc",
|
|
|
|
ConmonPath: "/usr/local/libexec/crio/conmon",
|
|
|
|
ConmonEnvVars: []string{
|
|
|
|
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
|
|
|
|
},
|
|
|
|
CgroupManager: "cgroupfs",
|
2017-10-23 15:36:10 +00:00
|
|
|
StaticDir: "/var/lib/libpod",
|
|
|
|
TmpDir: "/var/run/libpod",
|
2017-08-28 21:33:02 +00:00
|
|
|
SelinuxEnabled: false,
|
|
|
|
PidsLimit: 1024,
|
2017-10-10 20:33:20 +00:00
|
|
|
MaxLogSize: -1,
|
|
|
|
NoPivotRoot: false,
|
2017-08-28 21:33:02 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-08-11 16:42:25 +00:00
|
|
|
// NewRuntime creates a new container runtime
|
2017-08-28 21:33:02 +00:00
|
|
|
// Options can be passed to override the default configuration for the runtime
|
2017-10-27 13:50:09 +00:00
|
|
|
func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) {
|
|
|
|
runtime = new(Runtime)
|
2017-08-28 21:33:02 +00:00
|
|
|
runtime.config = new(RuntimeConfig)
|
|
|
|
|
|
|
|
// Copy the default configuration
|
|
|
|
deepcopier.Copy(defaultRuntimeConfig).To(runtime.config)
|
|
|
|
|
|
|
|
// Overwrite it with user-given configuration options
|
|
|
|
for _, opt := range options {
|
|
|
|
if err := opt(runtime); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error configuring runtime")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up containers/storage
|
|
|
|
store, err := storage.GetStore(runtime.config.StorageConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
runtime.store = store
|
2017-10-24 22:40:50 +00:00
|
|
|
is.Transport.SetStore(store)
|
2017-10-27 13:50:09 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
// Don't forcibly shut down
|
|
|
|
// We could be opening a store in use by another libpod
|
|
|
|
_, err2 := runtime.store.Shutdown(false)
|
|
|
|
if err2 != nil {
|
|
|
|
logrus.Errorf("Error removing store for partially-created runtime: %s", err2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2017-08-28 21:33:02 +00:00
|
|
|
|
2017-10-27 13:50:09 +00:00
|
|
|
// Set up a storage service for creating container root filesystems from
|
|
|
|
// images
|
2017-10-10 20:33:20 +00:00
|
|
|
storageService, err := getStorageService(runtime.store)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
runtime.storageService = storageService
|
|
|
|
|
2017-08-28 21:33:02 +00:00
|
|
|
// Set up containers/image
|
|
|
|
runtime.imageContext = &types.SystemContext{
|
|
|
|
SignaturePolicyPath: runtime.config.SignaturePolicyPath,
|
|
|
|
}
|
|
|
|
|
2017-09-14 14:51:07 +00:00
|
|
|
// Set up the state
|
|
|
|
state, err := NewInMemoryState()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
runtime.state = state
|
|
|
|
|
2017-10-10 20:33:20 +00:00
|
|
|
// Make an OCI runtime to perform container operations
|
|
|
|
ociRuntime, err := newOCIRuntime("runc", runtime.config.RuntimePath,
|
|
|
|
runtime.config.ConmonPath, runtime.config.ConmonEnvVars,
|
2017-10-23 15:36:10 +00:00
|
|
|
runtime.config.CgroupManager, runtime.config.TmpDir,
|
2017-10-10 20:33:20 +00:00
|
|
|
runtime.config.MaxLogSize, runtime.config.NoPivotRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
runtime.ociRuntime = ociRuntime
|
|
|
|
|
2017-10-23 15:36:10 +00:00
|
|
|
// Make the static files directory if it does not exist
|
|
|
|
if err := os.MkdirAll(runtime.config.StaticDir, 0755); err != nil {
|
2017-10-10 20:33:20 +00:00
|
|
|
// The directory is allowed to exist
|
|
|
|
if !os.IsExist(err) {
|
2017-10-23 15:36:10 +00:00
|
|
|
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)
|
2017-10-10 20:33:20 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-28 21:33:02 +00:00
|
|
|
|
|
|
|
// Mark the runtime as valid - ready to be used, cannot be modified
|
|
|
|
// further
|
|
|
|
runtime.valid = true
|
|
|
|
|
|
|
|
return runtime, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetConfig returns a copy of the configuration used by the runtime
|
|
|
|
func (r *Runtime) GetConfig() *RuntimeConfig {
|
|
|
|
r.lock.RLock()
|
|
|
|
defer r.lock.RUnlock()
|
|
|
|
|
|
|
|
if !r.valid {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
config := new(RuntimeConfig)
|
|
|
|
|
|
|
|
// Copy so the caller won't be able to modify the actual config
|
|
|
|
deepcopier.Copy(r.config).To(config)
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown shuts down the runtime and associated containers and storage
|
|
|
|
// If force is true, containers and mounted storage will be shut down before
|
|
|
|
// cleaning up; if force is false, an error will be returned if there are
|
|
|
|
// still containers running or mounted
|
|
|
|
func (r *Runtime) Shutdown(force bool) error {
|
|
|
|
r.lock.Lock()
|
|
|
|
defer r.lock.Unlock()
|
|
|
|
|
|
|
|
if !r.valid {
|
2017-09-11 15:27:40 +00:00
|
|
|
return ErrRuntimeStopped
|
2017-08-28 21:33:02 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 14:29:46 +00:00
|
|
|
r.valid = false
|
|
|
|
|
2017-08-28 21:33:02 +00:00
|
|
|
_, err := r.store.Shutdown(force)
|
|
|
|
return err
|
2017-08-11 16:42:25 +00:00
|
|
|
}
|