2017-03-17 23:09:06 +00:00
|
|
|
// +build linux
|
|
|
|
|
2017-02-24 23:50:59 +00:00
|
|
|
package linux
|
2017-02-13 18:23:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/grpclog"
|
|
|
|
|
2017-04-03 20:14:15 +00:00
|
|
|
"github.com/containerd/containerd/api/services/shim"
|
|
|
|
localShim "github.com/containerd/containerd/linux/shim"
|
|
|
|
"github.com/containerd/containerd/reaper"
|
|
|
|
"github.com/containerd/containerd/sys"
|
2017-02-13 18:23:28 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2017-03-10 23:28:21 +00:00
|
|
|
func newShim(path string, remote bool) (shim.ShimClient, error) {
|
|
|
|
if !remote {
|
|
|
|
return localShim.Client(path), nil
|
|
|
|
}
|
2017-02-13 18:23:28 +00:00
|
|
|
socket := filepath.Join(path, "shim.sock")
|
2017-03-29 15:09:08 +00:00
|
|
|
l, err := sys.CreateUnixSocket(socket)
|
2017-02-13 18:23:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cmd := exec.Command("containerd-shim")
|
|
|
|
cmd.Dir = path
|
|
|
|
f, err := l.(*net.UnixListener).File()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-21 16:51:40 +00:00
|
|
|
// close our side of the socket, do not close the listener as it will
|
|
|
|
// remove the socket from disk
|
|
|
|
defer f.Close()
|
2017-02-13 18:23:28 +00:00
|
|
|
cmd.ExtraFiles = append(cmd.ExtraFiles, f)
|
|
|
|
// make sure the shim can be re-parented to system init
|
|
|
|
// and is cloned in a new mount namespace because the overlay/filesystems
|
|
|
|
// will be mounted by the shim
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{
|
|
|
|
Cloneflags: syscall.CLONE_NEWNS,
|
|
|
|
Setpgid: true,
|
|
|
|
}
|
2017-03-10 00:07:35 +00:00
|
|
|
if err := reaper.Default.Start(cmd); err != nil {
|
2017-02-13 18:23:28 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to start shim")
|
|
|
|
}
|
|
|
|
return connectShim(socket)
|
|
|
|
}
|
|
|
|
|
2017-03-10 23:28:21 +00:00
|
|
|
func loadShim(path string, remote bool) (shim.ShimClient, error) {
|
|
|
|
if !remote {
|
|
|
|
return localShim.Client(path), nil
|
|
|
|
}
|
2017-02-13 18:23:28 +00:00
|
|
|
socket := filepath.Join(path, "shim.sock")
|
|
|
|
return connectShim(socket)
|
|
|
|
// TODO: failed to connect to the shim, check if it's alive
|
|
|
|
// - if it is kill it
|
|
|
|
// - in both case call runc killall and runc delete on the id
|
|
|
|
}
|
|
|
|
|
|
|
|
func connectShim(socket string) (shim.ShimClient, error) {
|
|
|
|
// reset the logger for grpc to log to dev/null so that it does not mess with our stdio
|
|
|
|
grpclog.SetLogger(log.New(ioutil.Discard, "", log.LstdFlags))
|
|
|
|
dialOpts := []grpc.DialOption{grpc.WithInsecure(), grpc.WithTimeout(100 * time.Second)}
|
|
|
|
dialOpts = append(dialOpts,
|
|
|
|
grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
|
|
|
|
return net.DialTimeout("unix", socket, timeout)
|
|
|
|
}),
|
|
|
|
grpc.WithBlock(),
|
|
|
|
grpc.WithTimeout(2*time.Second),
|
|
|
|
)
|
|
|
|
conn, err := grpc.Dial(fmt.Sprintf("unix://%s", socket), dialOpts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to connect to shim via \"%s\"", fmt.Sprintf("unix://%s", socket))
|
|
|
|
}
|
|
|
|
return shim.NewShimClient(conn), nil
|
|
|
|
}
|