diff --git a/README.md b/README.md index e7b30eed..b913aeaa 100644 --- a/README.md +++ b/README.md @@ -29,11 +29,9 @@ sudo make install ### Using: -To run a container that you received just execute `runc` with the JSON format as the argument or have a -`config.json` file in the current working directory. - +To run a container, execute `runc start` in the bundle's root directory: ```bash -runc +runc start / $ ps PID USER COMMAND 1 daemon sh @@ -41,6 +39,18 @@ PID USER COMMAND / $ ``` +Or you can specify the path to a JSON configuration file: +```bash +runc start config.json +/ $ ps +PID USER COMMAND +1 daemon sh +5 daemon sh +/ $ +``` +Note: the use of the `start` command is required when specifying a +configuration file. + ### OCF Container JSON Format: Below is a sample `config.json` configuration file. It assumes that @@ -210,9 +220,9 @@ tar -C rootfs -xf busybox.tar ``` * Create a file called `config.json` using the example from above. You can also generate a spec using `runc spec`, redirecting the output into `config.json` -* Execute `runc` and you should be placed into a shell where you can run `ps`: +* Execute `runc start` and you should be placed into a shell where you can run `ps`: ``` -$ runc +$ runc start / # ps PID USER COMMAND 1 root sh diff --git a/main.go b/main.go index 1118fa52..69b08c76 100644 --- a/main.go +++ b/main.go @@ -24,7 +24,11 @@ After creating a spec for your root filesystem with runc, you can execute a container in your shell by running: cd /mycontainer - runc [ spec-file ] + runc start + +or + cd /mycontainer + runc start [ spec-file ] If not specified, the default value for the 'spec-file' is 'config.json'. ` ) @@ -56,6 +60,7 @@ func main() { }, } app.Commands = []cli.Command{ + startCommand, checkpointCommand, eventsCommand, restoreCommand, @@ -69,7 +74,8 @@ func main() { return nil } - app.Action = runAction + // Default to 'start' is no command is specified + app.Action = startCommand.Action if err := app.Run(os.Args); err != nil { logrus.Fatal(err) diff --git a/run.go b/start.go similarity index 74% rename from run.go rename to start.go index 14dd8744..49669a07 100644 --- a/run.go +++ b/start.go @@ -13,6 +13,33 @@ import ( "github.com/opencontainers/specs" ) +var startCommand = cli.Command{ + Name: "start", + Usage: "create and run a container", + Action: func(context *cli.Context) { + spec, err := loadSpec(context.Args().First()) + + notifySocket := os.Getenv("NOTIFY_SOCKET") + if notifySocket != "" { + setupSdNotify(spec, notifySocket) + } + + if err != nil { + fatal(err) + } + if os.Geteuid() != 0 { + logrus.Fatal("runc should be run as root") + } + status, err := startContainer(context, spec) + if err != nil { + logrus.Fatalf("Container start failed: %v", err) + } + // exit with the container's exit status so any external supervisor is + // notified of the exit with the correct exit status. + os.Exit(status) + }, +} + func init() { if len(os.Args) > 1 && os.Args[1] == "init" { runtime.GOMAXPROCS(1) @@ -25,7 +52,7 @@ func init() { } } -func execContainer(context *cli.Context, spec *specs.LinuxSpec) (int, error) { +func startContainer(context *cli.Context, spec *specs.LinuxSpec) (int, error) { config, err := createLibcontainerConfig(context.GlobalString("id"), spec) if err != nil { return -1, err @@ -65,28 +92,6 @@ func execContainer(context *cli.Context, spec *specs.LinuxSpec) (int, error) { } // default action is to execute a container -func runAction(context *cli.Context) { - spec, err := loadSpec(context.Args().First()) - - notifySocket := os.Getenv("NOTIFY_SOCKET") - if notifySocket != "" { - setupSdNotify(spec, notifySocket) - } - - if err != nil { - fatal(err) - } - if os.Geteuid() != 0 { - logrus.Fatal("runc should be run as root") - } - status, err := execContainer(context, spec) - if err != nil { - logrus.Fatalf("Container start failed: %v", err) - } - // exit with the container's exit status so any external supervisor is - // notified of the exit with the correct exit status. - os.Exit(status) -} // If systemd is supporting sd_notify protocol, this function will add support // for sd_notify protocol from within the container.