2016-10-04 17:45:09 +00:00
|
|
|
|
package shim
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2016-10-05 22:07:20 +00:00
|
|
|
|
"errors"
|
2016-10-04 17:45:09 +00:00
|
|
|
|
"fmt"
|
2016-10-06 18:39:12 +00:00
|
|
|
|
"io/ioutil"
|
2016-10-04 17:45:09 +00:00
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"sync"
|
|
|
|
|
"syscall"
|
|
|
|
|
"time"
|
|
|
|
|
|
2016-10-06 18:46:03 +00:00
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
|
"github.com/docker/containerd/monitor"
|
2016-10-04 17:45:09 +00:00
|
|
|
|
"github.com/docker/containerd/oci"
|
|
|
|
|
"github.com/docker/containerkit"
|
2016-10-05 22:07:20 +00:00
|
|
|
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
2016-10-04 17:45:09 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
├── libcontainerd
|
|
|
|
|
│ ├── containerd
|
|
|
|
|
│ │ └── ff2e86955c2be43f0e3c300fbd3786599301bd8efcaa5a386587f132e73af242
|
|
|
|
|
│ │ ├── init
|
|
|
|
|
│ │ │ ├── control
|
|
|
|
|
│ │ │ ├── exit
|
|
|
|
|
│ │ │ ├── log.json
|
|
|
|
|
│ │ │ ├── pid
|
|
|
|
|
│ │ │ ├── process.json
|
|
|
|
|
│ │ │ ├── shim-log.json
|
|
|
|
|
│ │ │ └── starttime
|
|
|
|
|
│ │ └── state.json
|
|
|
|
|
*/
|
|
|
|
|
|
2016-10-05 22:07:20 +00:00
|
|
|
|
var (
|
|
|
|
|
ErrNotFifo = errors.New("shim: IO is not a valid fifo on disk")
|
|
|
|
|
errInitProcessNotExist = errors.New("shim: init process does not exist")
|
|
|
|
|
)
|
2016-10-04 17:45:09 +00:00
|
|
|
|
|
2016-10-05 22:07:20 +00:00
|
|
|
|
type Opts struct {
|
|
|
|
|
Name string
|
|
|
|
|
RuntimeName string
|
|
|
|
|
RuntimeArgs []string
|
2016-10-06 18:39:12 +00:00
|
|
|
|
RuntimeRoot string
|
2016-10-05 22:07:20 +00:00
|
|
|
|
NoPivotRoot bool
|
|
|
|
|
Root string
|
|
|
|
|
Timeout time.Duration
|
2016-10-04 17:45:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func New(opts Opts) (*Shim, error) {
|
2016-10-06 18:39:12 +00:00
|
|
|
|
if err := os.MkdirAll(filepath.Dir(opts.Root), 0711); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2016-10-05 22:07:20 +00:00
|
|
|
|
if err := os.Mkdir(opts.Root, 0711); err != nil {
|
2016-10-04 17:45:09 +00:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
r, err := oci.New(oci.Opts{
|
2016-10-06 18:39:12 +00:00
|
|
|
|
Root: opts.RuntimeRoot,
|
2016-10-05 22:07:20 +00:00
|
|
|
|
Name: opts.RuntimeName,
|
|
|
|
|
Args: opts.RuntimeArgs,
|
2016-10-04 17:45:09 +00:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2016-10-06 18:46:03 +00:00
|
|
|
|
m, err := monitor.New()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2016-10-05 22:07:20 +00:00
|
|
|
|
s := &Shim{
|
|
|
|
|
root: opts.Root,
|
|
|
|
|
name: opts.Name,
|
|
|
|
|
timeout: opts.Timeout,
|
|
|
|
|
runtime: r,
|
|
|
|
|
processes: make(map[string]*process),
|
2016-10-06 18:46:03 +00:00
|
|
|
|
m: m,
|
2016-10-05 22:07:20 +00:00
|
|
|
|
}
|
2016-10-06 18:46:03 +00:00
|
|
|
|
go s.startMonitor()
|
2016-10-05 22:07:20 +00:00
|
|
|
|
f, err := os.Create(filepath.Join(opts.Root, "state.json"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
err = json.NewEncoder(f).Encode(s)
|
|
|
|
|
f.Close()
|
|
|
|
|
return s, err
|
2016-10-04 17:45:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load will load an existing shim with all its information restored from the
|
|
|
|
|
// provided path
|
2016-10-05 22:07:20 +00:00
|
|
|
|
func Load(root string) (*Shim, error) {
|
|
|
|
|
f, err := os.Open(filepath.Join(root, "state.json"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
var s Shim
|
|
|
|
|
err = json.NewDecoder(f).Decode(&s)
|
|
|
|
|
f.Close()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2016-10-06 18:39:12 +00:00
|
|
|
|
dirs, err := ioutil.ReadDir(root)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
for _, d := range dirs {
|
|
|
|
|
if !d.IsDir() {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
name := d.Name()
|
|
|
|
|
if f, err = os.Open(filepath.Join(root, name, "process.json")); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
var p process
|
|
|
|
|
err = json.NewDecoder(f).Decode(&p)
|
|
|
|
|
f.Close()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
s.processes[name] = &p
|
|
|
|
|
}
|
2016-10-05 22:07:20 +00:00
|
|
|
|
return &s, nil
|
2016-10-04 17:45:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Shim is a container runtime that adds a shim process as the container's parent
|
|
|
|
|
// to hold open stdio and other resources so that higher level daemons can exit and
|
|
|
|
|
// load running containers for handling upgrades and/or crashes
|
|
|
|
|
//
|
|
|
|
|
// The shim uses an OCI compliant runtime as its executor
|
|
|
|
|
type Shim struct {
|
|
|
|
|
// root holds runtime state information for the containers
|
|
|
|
|
// launched by the runtime
|
2016-10-05 22:07:20 +00:00
|
|
|
|
root string
|
|
|
|
|
name string
|
|
|
|
|
timeout time.Duration
|
|
|
|
|
noPivotRoot bool
|
|
|
|
|
runtime *oci.OCIRuntime
|
|
|
|
|
pmu sync.Mutex
|
|
|
|
|
processes map[string]*process
|
|
|
|
|
bundle string
|
|
|
|
|
checkpoint string
|
2016-10-06 18:46:03 +00:00
|
|
|
|
m *monitor.Monitor
|
2016-10-05 22:07:20 +00:00
|
|
|
|
}
|
2016-10-04 17:45:09 +00:00
|
|
|
|
|
2016-10-05 22:07:20 +00:00
|
|
|
|
type state struct {
|
|
|
|
|
// Bundle is the path to the container's bundle
|
|
|
|
|
Bundle string `json:"bundle"`
|
|
|
|
|
// OCI runtime binary name
|
|
|
|
|
Runtime string `json:"runtime"`
|
|
|
|
|
// OCI runtime args
|
|
|
|
|
RuntimeArgs []string `json:"runtimeArgs"`
|
|
|
|
|
// Shim binary name
|
|
|
|
|
Name string `json:"shim"`
|
|
|
|
|
/// NoPivotRoot option
|
|
|
|
|
NoPivotRoot bool `json:"noPivotRoot"`
|
|
|
|
|
// Timeout for container start
|
|
|
|
|
Timeout time.Duration `json:"timeout"`
|
2016-10-04 17:45:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-05 22:07:20 +00:00
|
|
|
|
func (s *Shim) MarshalJSON() ([]byte, error) {
|
|
|
|
|
st := state{
|
|
|
|
|
Name: s.name,
|
|
|
|
|
Bundle: s.bundle,
|
|
|
|
|
Runtime: s.runtime.Name(),
|
|
|
|
|
RuntimeArgs: s.runtime.Args(),
|
|
|
|
|
NoPivotRoot: s.noPivotRoot,
|
|
|
|
|
Timeout: s.timeout,
|
2016-10-04 17:45:09 +00:00
|
|
|
|
}
|
2016-10-05 22:07:20 +00:00
|
|
|
|
return json.Marshal(st)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Shim) UnmarshalJSON(b []byte) error {
|
|
|
|
|
var st state
|
|
|
|
|
if err := json.Unmarshal(b, &st); err != nil {
|
|
|
|
|
return err
|
2016-10-04 17:45:09 +00:00
|
|
|
|
}
|
2016-10-05 22:07:20 +00:00
|
|
|
|
s.name = st.Name
|
|
|
|
|
s.bundle = st.Bundle
|
|
|
|
|
s.timeout = st.Timeout
|
|
|
|
|
s.noPivotRoot = st.NoPivotRoot
|
|
|
|
|
r, err := oci.New(oci.Opts{
|
|
|
|
|
Name: st.Runtime,
|
|
|
|
|
Args: st.RuntimeArgs,
|
2016-10-04 17:45:09 +00:00
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
2016-10-05 22:07:20 +00:00
|
|
|
|
return err
|
2016-10-04 17:45:09 +00:00
|
|
|
|
}
|
2016-10-05 22:07:20 +00:00
|
|
|
|
s.runtime = r
|
2016-10-06 18:39:12 +00:00
|
|
|
|
s.processes = make(map[string]*process)
|
2016-10-05 22:07:20 +00:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Shim) Create(c *containerkit.Container) (containerkit.ProcessDelegate, error) {
|
2016-10-06 18:39:12 +00:00
|
|
|
|
s.bundle = c.Path()
|
2016-10-05 22:07:20 +00:00
|
|
|
|
var (
|
|
|
|
|
root = filepath.Join(s.root, "init")
|
|
|
|
|
cmd = s.command(c.ID(), c.Path(), s.runtime.Name())
|
|
|
|
|
)
|
2016-10-04 17:45:09 +00:00
|
|
|
|
// exec the shim inside the state directory setup with the process
|
|
|
|
|
// information for what is being run
|
2016-10-05 22:07:20 +00:00
|
|
|
|
cmd.Dir = root
|
2016-10-04 17:45:09 +00:00
|
|
|
|
// make sure the shim is in a new process group
|
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{
|
|
|
|
|
Setpgid: true,
|
|
|
|
|
}
|
2016-10-05 22:07:20 +00:00
|
|
|
|
p, err := s.startCommand(c, cmd)
|
2016-10-04 17:45:09 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2016-10-06 18:46:03 +00:00
|
|
|
|
if err := s.m.Add(p); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2016-10-04 17:45:09 +00:00
|
|
|
|
s.pmu.Lock()
|
2016-10-05 22:07:20 +00:00
|
|
|
|
s.processes["init"] = p
|
2016-10-04 17:45:09 +00:00
|
|
|
|
s.pmu.Unlock()
|
2016-10-06 18:39:12 +00:00
|
|
|
|
|
|
|
|
|
f, err := os.Create(filepath.Join(s.root, "state.json"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
err = json.NewEncoder(f).Encode(s)
|
|
|
|
|
f.Close()
|
2016-10-04 17:45:09 +00:00
|
|
|
|
// ~TODO: oom and stats stuff here
|
2016-10-06 18:39:12 +00:00
|
|
|
|
return p, err
|
2016-10-04 17:45:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Shim) Start(c *containerkit.Container) error {
|
|
|
|
|
p, err := s.getContainerInit(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
var (
|
|
|
|
|
errC = make(chan error, 1)
|
|
|
|
|
cmd = s.runtime.Command("start", c.ID())
|
|
|
|
|
)
|
|
|
|
|
go func() {
|
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
|
if err != nil {
|
|
|
|
|
errC <- fmt.Errorf("%s: %q", err, out)
|
|
|
|
|
}
|
|
|
|
|
errC <- nil
|
|
|
|
|
}()
|
|
|
|
|
select {
|
|
|
|
|
case err := <-errC:
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
case <-p.done:
|
|
|
|
|
if !p.success {
|
|
|
|
|
if cmd.Process != nil {
|
|
|
|
|
cmd.Process.Kill()
|
|
|
|
|
}
|
|
|
|
|
cmd.Wait()
|
|
|
|
|
return ErrShimExited
|
|
|
|
|
}
|
|
|
|
|
err := <-errC
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-06 18:39:12 +00:00
|
|
|
|
func (s *Shim) Delete(c *containerkit.Container) error {
|
|
|
|
|
if err := s.runtime.Delete(c); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return os.RemoveAll(s.root)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var errnotimpl = errors.New("NOT IMPL RIGHT NOW, CHILL")
|
|
|
|
|
|
|
|
|
|
func (s *Shim) Exec(c *containerkit.Container, p *containerkit.Process) (containerkit.ProcessDelegate, error) {
|
|
|
|
|
return nil, errnotimpl
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Shim) Load(id string) (containerkit.ProcessDelegate, error) {
|
|
|
|
|
return nil, errnotimpl
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-04 17:45:09 +00:00
|
|
|
|
func (s *Shim) getContainerInit(c *containerkit.Container) (*process, error) {
|
|
|
|
|
s.pmu.Lock()
|
2016-10-05 22:07:20 +00:00
|
|
|
|
p, ok := s.processes["init"]
|
2016-10-04 17:45:09 +00:00
|
|
|
|
s.pmu.Unlock()
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, errInitProcessNotExist
|
|
|
|
|
}
|
|
|
|
|
return p, nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-05 22:07:20 +00:00
|
|
|
|
func (s *Shim) startCommand(c *containerkit.Container, cmd *exec.Cmd) (*process, error) {
|
|
|
|
|
p, err := newProcess(filepath.Join(s.root, "init"), s.noPivotRoot, s.checkpoint, c, cmd)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2016-10-04 17:45:09 +00:00
|
|
|
|
}
|
|
|
|
|
if err := cmd.Start(); err != nil {
|
2016-10-05 22:07:20 +00:00
|
|
|
|
close(p.done)
|
2016-10-04 17:45:09 +00:00
|
|
|
|
if checkShimNotFound(err) {
|
2016-10-05 22:07:20 +00:00
|
|
|
|
return nil, fmt.Errorf("%s not install on system", s.name)
|
2016-10-04 17:45:09 +00:00
|
|
|
|
}
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
// make sure it does not die before we get the container's pid
|
|
|
|
|
defer func() {
|
|
|
|
|
go p.checkExited()
|
|
|
|
|
}()
|
2016-10-05 22:07:20 +00:00
|
|
|
|
if err := p.waitForCreate(s.timeout); err != nil {
|
2016-10-04 17:45:09 +00:00
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return p, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Shim) command(args ...string) *exec.Cmd {
|
|
|
|
|
return exec.Command(s.name, args...)
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-06 18:46:03 +00:00
|
|
|
|
func (s *Shim) startMonitor() {
|
|
|
|
|
for m := range s.m.Events() {
|
|
|
|
|
p := m.(*process)
|
|
|
|
|
close(p.done)
|
|
|
|
|
if err := s.m.Remove(p); err != nil {
|
|
|
|
|
logrus.Error(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-04 17:45:09 +00:00
|
|
|
|
// checkShimNotFound checks the error returned from a exec call to see if the binary
|
|
|
|
|
// that was called exists on the system and returns true if the shim binary does not exist
|
|
|
|
|
func checkShimNotFound(err error) bool {
|
|
|
|
|
if exitError, ok := err.(*exec.Error); ok {
|
|
|
|
|
e := exitError.Err
|
|
|
|
|
return e == exec.ErrNotFound || e == os.ErrNotExist
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
2016-10-05 22:07:20 +00:00
|
|
|
|
|
|
|
|
|
// getFifoPath returns the path to the fifo on disk as long as the provided
|
|
|
|
|
// interface is an *os.File and has a valid path on the Name() method call
|
|
|
|
|
func getFifoPath(v interface{}) (string, error) {
|
|
|
|
|
f, ok := v.(*os.File)
|
|
|
|
|
if !ok {
|
|
|
|
|
return "", ErrNotFifo
|
|
|
|
|
}
|
|
|
|
|
p := f.Name()
|
|
|
|
|
if p == "" {
|
|
|
|
|
return "", ErrNotFifo
|
|
|
|
|
}
|
|
|
|
|
return p, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getRootIDs(s *specs.Spec) (int, int, error) {
|
|
|
|
|
if s == nil {
|
|
|
|
|
return 0, 0, nil
|
|
|
|
|
}
|
|
|
|
|
var hasUserns bool
|
|
|
|
|
for _, ns := range s.Linux.Namespaces {
|
|
|
|
|
if ns.Type == specs.UserNamespace {
|
|
|
|
|
hasUserns = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !hasUserns {
|
|
|
|
|
return 0, 0, nil
|
|
|
|
|
}
|
|
|
|
|
uid := hostIDFromMap(0, s.Linux.UIDMappings)
|
|
|
|
|
gid := hostIDFromMap(0, s.Linux.GIDMappings)
|
|
|
|
|
return uid, gid, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func hostIDFromMap(id uint32, mp []specs.IDMapping) int {
|
|
|
|
|
for _, m := range mp {
|
|
|
|
|
if (id >= m.ContainerID) && (id <= (m.ContainerID + m.Size - 1)) {
|
|
|
|
|
return int(m.HostID + (id - m.ContainerID))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|