From a7278cad98558ae78cff73172bf707d3dbf77e12 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Mon, 8 Feb 2016 14:25:03 -0800 Subject: [PATCH] Require containerd id as arg 1 Closes #532 This requires the container id to always be passed to all runc commands as arg one on the cli. This was the result of the last OCI meeting and how operations work with the spec. Signed-off-by: Michael Crosby --- README.md | 10 ++++++---- exec.go | 2 +- kill.go | 2 +- main.go | 7 +------ main_unsupported.go | 4 ---- restore.go | 17 ++++++++++++----- start.go | 10 +++++++--- utils.go | 24 ++++++++---------------- 8 files changed, 36 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index fbc76f99a20..1b5343d1783 100644 --- a/README.md +++ b/README.md @@ -65,9 +65,11 @@ You can also run specific test cases by: ### Using: -To run a container, execute `runc start` in the bundle's root directory: +To run a container with the id "test", execute `runc start` with the containers id as arg one +in the bundle's root directory: + ```bash -runc start +runc start test / $ ps PID USER COMMAND 1 daemon sh @@ -98,7 +100,7 @@ tar -C rootfs -xf busybox.tar * Create `config.json` by using `runc spec`. * Execute `runc start` and you should be placed into a shell where you can run `ps`: ``` -$ runc start +$ runc start test / # ps PID USER COMMAND 1 root sh @@ -120,7 +122,7 @@ After=network.target [Service] CPUQuota=200% MemoryLimit=1536M -ExecStart=/usr/local/bin/runc start +ExecStart=/usr/local/bin/runc start minecraft Restart=on-failure WorkingDirectory=/containers/minecraftbuild diff --git a/exec.go b/exec.go index 094a9791eb0..fdfc08e3872 100644 --- a/exec.go +++ b/exec.go @@ -128,7 +128,7 @@ func getProcess(context *cli.Context, bundle string) (*specs.Process, error) { return nil, err } p := spec.Process - p.Args = context.Args() + p.Args = context.Args()[1:] // override the cwd, if passed if context.String("cwd") != "" { p.Cwd = context.String("cwd") diff --git a/kill.go b/kill.go index 0cf680c7781..4040b596679 100644 --- a/kill.go +++ b/kill.go @@ -58,7 +58,7 @@ var killCommand = cli.Command{ fatal(err) } - sigstr := context.Args().First() + sigstr := context.Args().Get(1) if sigstr == "" { sigstr = "SIGTERM" } diff --git a/main.go b/main.go index 349dc68ba8a..5bac4471af0 100644 --- a/main.go +++ b/main.go @@ -27,7 +27,7 @@ After creating config files for your root filesystem with runc, you can execute a container in your shell by running: # cd /mycontainer - # runc start [ -b bundle ] + # runc start [ -b bundle ] If not specified, the default value for the 'bundle' is the current directory. 'Bundle' is the directory where '` + specConfig + `' must be located.` @@ -39,11 +39,6 @@ func main() { app.Usage = usage app.Version = fmt.Sprintf("%s\nspec version %s", version, specs.Version) app.Flags = []cli.Flag{ - cli.StringFlag{ - Name: "id", - Value: getDefaultID(), - Usage: "specify the ID to be used for the container", - }, cli.BoolFlag{ Name: "debug", Usage: "enable debug output for logging", diff --git a/main_unsupported.go b/main_unsupported.go index 00031e473cf..610012044d1 100644 --- a/main_unsupported.go +++ b/main_unsupported.go @@ -7,10 +7,6 @@ import ( "github.com/codegangsta/cli" ) -func getDefaultID() string { - return "" -} - var ( checkpointCommand cli.Command eventsCommand cli.Command diff --git a/restore.go b/restore.go index 8fa899b7bfa..a0dae2d2faf 100644 --- a/restore.go +++ b/restore.go @@ -66,6 +66,10 @@ var restoreCommand = cli.Command{ }, Action: func(context *cli.Context) { imagePath := context.String("image-path") + id := context.Args().First() + if id == "" { + fatal(errEmptyID) + } if imagePath == "" { imagePath = getDefaultImagePath(context) } @@ -79,7 +83,7 @@ var restoreCommand = cli.Command{ if err != nil { fatal(err) } - config, err := createLibcontainerConfig(context.GlobalString("id"), spec) + config, err := createLibcontainerConfig(id, spec) if err != nil { fatal(err) } @@ -92,14 +96,17 @@ var restoreCommand = cli.Command{ } func restoreContainer(context *cli.Context, spec *specs.LinuxSpec, config *configs.Config, imagePath string) (code int, err error) { - rootuid := 0 + var ( + rootuid = 0 + id = context.Args().First() + ) factory, err := loadFactory(context) if err != nil { return -1, err } - container, err := factory.Load(context.GlobalString("id")) + container, err := factory.Load(id) if err != nil { - container, err = factory.Create(context.GlobalString("id"), config) + container, err = factory.Create(id, config) if err != nil { return -1, err } @@ -111,7 +118,7 @@ func restoreContainer(context *cli.Context, spec *specs.LinuxSpec, config *confi logrus.Error(err) } if status == libcontainer.Running { - fatal(fmt.Errorf("Container with id %s already running", context.GlobalString("id"))) + fatal(fmt.Errorf("Container with id %s already running", id)) } setManageCgroupsMode(context, options) diff --git a/start.go b/start.go index 55a65c1d56d..47ea9241204 100644 --- a/start.go +++ b/start.go @@ -93,13 +93,17 @@ func init() { } func startContainer(context *cli.Context, spec *specs.LinuxSpec) (int, error) { - config, err := createLibcontainerConfig(context.GlobalString("id"), spec) + id := context.Args().First() + if id == "" { + return -1, errEmptyID + } + config, err := createLibcontainerConfig(id, spec) if err != nil { return -1, err } if _, err := os.Stat(config.Rootfs); err != nil { if os.IsNotExist(err) { - return -1, fmt.Errorf("Rootfs (%q) does not exist", config.Rootfs) + return -1, fmt.Errorf("rootfs (%q) does not exist", config.Rootfs) } return -1, err } @@ -111,7 +115,7 @@ func startContainer(context *cli.Context, spec *specs.LinuxSpec) (int, error) { if err != nil { return -1, err } - container, err := factory.Create(context.GlobalString("id"), config) + container, err := factory.Create(id, config) if err != nil { return -1, err } diff --git a/utils.go b/utils.go index eda732ab24f..6614b9ca005 100644 --- a/utils.go +++ b/utils.go @@ -3,6 +3,7 @@ package main import ( + "errors" "fmt" "os" "path/filepath" @@ -17,6 +18,8 @@ import ( const wildcard = -1 +var errEmptyID = errors.New("container id cannot be empty") + var allowedDevices = []*configs.Device{ // allow mknod for any device { @@ -160,15 +163,15 @@ func loadFactory(context *cli.Context) (libcontainer.Factory, error) { // getContainer returns the specified container instance by loading it from state // with the default factory. func getContainer(context *cli.Context) (libcontainer.Container, error) { - factory, err := loadFactory(context) - if err != nil { - return nil, err + id := context.Args().First() + if id == "" { + return nil, errEmptyID } - container, err := factory.Load(context.GlobalString("id")) + factory, err := loadFactory(context) if err != nil { return nil, err } - return container, nil + return factory.Load(id) } // fatal prints the error's details if it is a libcontainer specific error type @@ -182,17 +185,6 @@ func fatal(err error) { os.Exit(1) } -// getDefaultID returns a string to be used as the container id based on the -// current working directory of the runc process. This function panics -// if the cwd is unable to be found based on a system error. -func getDefaultID() string { - cwd, err := os.Getwd() - if err != nil { - panic(err) - } - return filepath.Base(cwd) -} - func getDefaultImagePath(context *cli.Context) string { cwd, err := os.Getwd() if err != nil {