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"
|
2016-04-13 04:35:51 +08:00
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
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
|
2016-03-19 02:54:06 +08:00
|
|
|
is a directory with a specification file named "` + specConfig + `" and a root
|
|
|
|
filesystem.
|
|
|
|
|
|
|
|
The specification file includes an args parameter. The args parameter is used
|
|
|
|
to specify command(s) that get run when the container is started. To change the
|
|
|
|
command(s) that get executed on start, edit the args parameter of the spec. See
|
|
|
|
"runc spec --help" for more explanation.`,
|
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",
|
|
|
|
},
|
2016-03-15 06:46:08 +08:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "no-subreaper",
|
|
|
|
Usage: "disable the use of the subreaper used to reap reparented processes",
|
|
|
|
},
|
2016-03-31 02:12:03 +08:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "no-pivot",
|
|
|
|
Usage: "do not use pivot root to jail process inside rootfs. This should be used whenever the rootfs is on top of a ramdisk",
|
|
|
|
},
|
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-10 09:48:12 +08:00
|
|
|
fatal(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-26 02:15:09 +08:00
|
|
|
func init() {
|
|
|
|
if len(os.Args) > 1 && os.Args[1] == "init" {
|
|
|
|
runtime.GOMAXPROCS(1)
|
|
|
|
runtime.LockOSThread()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-09 10:05:50 +08:00
|
|
|
var initCommand = cli.Command{
|
2016-03-18 14:02:42 +08:00
|
|
|
Name: "init",
|
|
|
|
Usage: `initialize the namespaces and launch the process (do not call it outside of runc)`,
|
2016-03-09 10:05:50 +08:00
|
|
|
Action: func(context *cli.Context) {
|
2015-06-30 07:49:13 +08:00
|
|
|
factory, _ := libcontainer.New("")
|
|
|
|
if err := factory.StartInitialization(); err != nil {
|
2016-03-15 02:19:16 +08:00
|
|
|
// as the error is sent back to the parent there is no need to log
|
|
|
|
// or write it to stderr because the parent process will handle this
|
|
|
|
os.Exit(1)
|
2015-06-30 07:49:13 +08:00
|
|
|
}
|
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-03-11 06:18:39 +08:00
|
|
|
func startContainer(context *cli.Context, spec *specs.Spec) (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-01-12 08:57:18 +08:00
|
|
|
detach := context.Bool("detach")
|
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-03-15 06:46:08 +08:00
|
|
|
r := &runner{
|
|
|
|
enableSubreaper: !context.Bool("no-subreaper"),
|
|
|
|
shouldDestroy: true,
|
|
|
|
container: container,
|
|
|
|
listenFDs: listenFDs,
|
|
|
|
console: context.String("console"),
|
|
|
|
detach: detach,
|
|
|
|
pidFile: context.String("pid-file"),
|
2016-02-23 07:34:41 +08:00
|
|
|
}
|
2016-03-15 06:46:08 +08:00
|
|
|
return r.run(&spec.Process)
|
2015-06-22 10:31:12 +08:00
|
|
|
}
|