2015-06-30 07:49:13 +08:00
|
|
|
// +build linux
|
|
|
|
|
2015-06-22 10:31:12 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-06-25 15:19:44 +08:00
|
|
|
"os"
|
2015-06-30 07:49:13 +08:00
|
|
|
"runtime"
|
2015-06-22 10:31:12 +08:00
|
|
|
|
|
|
|
"github.com/codegangsta/cli"
|
2016-02-10 02:33:27 +08:00
|
|
|
"github.com/coreos/go-systemd/activation"
|
2015-06-22 10:31:12 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
2015-07-03 00:55:24 +08:00
|
|
|
"github.com/opencontainers/specs"
|
2015-06-22 10:31:12 +08:00
|
|
|
)
|
|
|
|
|
2015-08-29 23:50:39 +08:00
|
|
|
// default action is to start a container
|
2015-08-18 09:30:17 +08:00
|
|
|
var startCommand = cli.Command{
|
|
|
|
Name: "start",
|
|
|
|
Usage: "create and run a container",
|
2016-02-11 01:30:06 +08:00
|
|
|
ArgsUsage: `<container-id>
|
|
|
|
|
|
|
|
Where "<container-id>" is your name for the instance of the container that you
|
|
|
|
are starting. The name you provide for the container instance must be unique on
|
|
|
|
your host.`,
|
|
|
|
Description: `The start command creates an instance of a container for a bundle. The bundle
|
|
|
|
is a directory with a specification file and a root filesystem.`,
|
2015-09-02 00:32:29 +08:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
2015-10-28 03:23:44 +08:00
|
|
|
Name: "bundle, b",
|
|
|
|
Value: "",
|
2016-02-11 01:30:06 +08:00
|
|
|
Usage: `path to the root of the bundle directory, defaults to the current directory`,
|
2015-09-02 00:32:29 +08:00
|
|
|
},
|
2016-01-08 06:34:11 +08:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "console",
|
|
|
|
Value: "",
|
|
|
|
Usage: "specify the pty slave path for use with the container",
|
|
|
|
},
|
2016-01-12 08:57:18 +08:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "detach,d",
|
|
|
|
Usage: "detach from the container's process",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "pid-file",
|
|
|
|
Value: "",
|
|
|
|
Usage: "specify the file to write the process id to",
|
|
|
|
},
|
2015-09-02 00:32:29 +08:00
|
|
|
},
|
2015-08-18 09:30:17 +08:00
|
|
|
Action: func(context *cli.Context) {
|
2015-10-28 03:23:44 +08:00
|
|
|
bundle := context.String("bundle")
|
|
|
|
if bundle != "" {
|
|
|
|
if err := os.Chdir(bundle); err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2016-02-06 02:46:12 +08:00
|
|
|
spec, err := loadSpec(specConfig)
|
2015-08-27 23:03:43 +08:00
|
|
|
if err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
2015-08-18 09:30:17 +08:00
|
|
|
|
|
|
|
notifySocket := os.Getenv("NOTIFY_SOCKET")
|
|
|
|
if notifySocket != "" {
|
2016-02-06 02:46:12 +08:00
|
|
|
setupSdNotify(spec, notifySocket)
|
2015-08-18 09:30:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if os.Geteuid() != 0 {
|
2016-03-09 10:05:50 +08:00
|
|
|
fatalf("runc should be run as root")
|
2015-08-18 09:30:17 +08:00
|
|
|
}
|
2016-02-10 02:33:27 +08:00
|
|
|
|
2016-02-06 02:46:12 +08:00
|
|
|
status, err := startContainer(context, spec)
|
2015-08-18 09:30:17 +08:00
|
|
|
if err != nil {
|
2016-03-09 10:05:50 +08:00
|
|
|
fatalf("Container start failed: %v", err)
|
2015-08-18 09:30:17 +08:00
|
|
|
}
|
|
|
|
// exit with the container's exit status so any external supervisor is
|
|
|
|
// notified of the exit with the correct exit status.
|
|
|
|
os.Exit(status)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-03-09 10:05:50 +08:00
|
|
|
var initCommand = cli.Command{
|
|
|
|
Name: "init",
|
|
|
|
Usage: `init is used to initialize the containers namespaces and launch the users process.
|
|
|
|
This command should not be called outside of runc.
|
|
|
|
`,
|
|
|
|
Action: func(context *cli.Context) {
|
2015-06-30 07:49:13 +08:00
|
|
|
runtime.GOMAXPROCS(1)
|
|
|
|
runtime.LockOSThread()
|
|
|
|
factory, _ := libcontainer.New("")
|
|
|
|
if err := factory.StartInitialization(); err != nil {
|
|
|
|
fatal(err)
|
|
|
|
}
|
2016-02-26 06:30:49 +08:00
|
|
|
panic("libcontainer: container init failed to exec")
|
2016-03-09 10:05:50 +08:00
|
|
|
},
|
2015-06-30 07:49:13 +08:00
|
|
|
}
|
|
|
|
|
2016-02-06 02:46:12 +08:00
|
|
|
func startContainer(context *cli.Context, spec *specs.LinuxSpec) (int, error) {
|
2016-02-09 06:25:03 +08:00
|
|
|
id := context.Args().First()
|
|
|
|
if id == "" {
|
|
|
|
return -1, errEmptyID
|
|
|
|
}
|
2016-02-05 09:58:46 +08:00
|
|
|
container, err := createContainer(context, id, spec)
|
2015-06-22 10:31:12 +08:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2016-02-05 09:58:46 +08:00
|
|
|
|
2015-06-22 10:31:12 +08:00
|
|
|
// ensure that the container is always removed if we were the process
|
|
|
|
// that created it.
|
2016-01-12 08:57:18 +08:00
|
|
|
detach := context.Bool("detach")
|
|
|
|
if !detach {
|
|
|
|
defer destroy(container)
|
|
|
|
}
|
2016-02-05 09:58:46 +08:00
|
|
|
|
2015-08-27 23:34:46 +08:00
|
|
|
// Support on-demand socket activation by passing file descriptors into the container init process.
|
2016-02-05 09:58:46 +08:00
|
|
|
listenFDs := []*os.File{}
|
2015-08-27 23:34:46 +08:00
|
|
|
if os.Getenv("LISTEN_FDS") != "" {
|
2016-02-05 09:58:46 +08:00
|
|
|
listenFDs = activation.Files(false)
|
2015-06-22 10:31:12 +08:00
|
|
|
}
|
2016-02-05 09:58:46 +08:00
|
|
|
|
2016-02-23 07:34:41 +08:00
|
|
|
status, err := runProcess(container, &spec.Process, listenFDs, context.String("console"), context.String("pid-file"), detach)
|
|
|
|
if err != nil {
|
|
|
|
destroy(container)
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return status, nil
|
2015-06-22 10:31:12 +08:00
|
|
|
}
|