Skip to content

Commit a7278ca

Browse files
committed
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 <crosbymichael@gmail.com>
1 parent 8e8d01d commit a7278ca

File tree

8 files changed

+36
-40
lines changed

8 files changed

+36
-40
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ You can also run specific test cases by:
6565

6666
### Using:
6767

68-
To run a container, execute `runc start` in the bundle's root directory:
68+
To run a container with the id "test", execute `runc start` with the containers id as arg one
69+
in the bundle's root directory:
70+
6971
```bash
70-
runc start
72+
runc start test
7173
/ $ ps
7274
PID USER COMMAND
7375
1 daemon sh
@@ -98,7 +100,7 @@ tar -C rootfs -xf busybox.tar
98100
* Create `config.json` by using `runc spec`.
99101
* Execute `runc start` and you should be placed into a shell where you can run `ps`:
100102
```
101-
$ runc start
103+
$ runc start test
102104
/ # ps
103105
PID USER COMMAND
104106
1 root sh
@@ -120,7 +122,7 @@ After=network.target
120122
[Service]
121123
CPUQuota=200%
122124
MemoryLimit=1536M
123-
ExecStart=/usr/local/bin/runc start
125+
ExecStart=/usr/local/bin/runc start minecraft
124126
Restart=on-failure
125127
WorkingDirectory=/containers/minecraftbuild
126128

exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func getProcess(context *cli.Context, bundle string) (*specs.Process, error) {
128128
return nil, err
129129
}
130130
p := spec.Process
131-
p.Args = context.Args()
131+
p.Args = context.Args()[1:]
132132
// override the cwd, if passed
133133
if context.String("cwd") != "" {
134134
p.Cwd = context.String("cwd")

kill.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ var killCommand = cli.Command{
5858
fatal(err)
5959
}
6060

61-
sigstr := context.Args().First()
61+
sigstr := context.Args().Get(1)
6262
if sigstr == "" {
6363
sigstr = "SIGTERM"
6464
}

main.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ After creating config files for your root filesystem with runc, you can execute
2727
a container in your shell by running:
2828
2929
# cd /mycontainer
30-
# runc start [ -b bundle ]
30+
# runc start [ -b bundle ] <container-id>
3131
3232
If not specified, the default value for the 'bundle' is the current directory.
3333
'Bundle' is the directory where '` + specConfig + `' must be located.`
@@ -39,11 +39,6 @@ func main() {
3939
app.Usage = usage
4040
app.Version = fmt.Sprintf("%s\nspec version %s", version, specs.Version)
4141
app.Flags = []cli.Flag{
42-
cli.StringFlag{
43-
Name: "id",
44-
Value: getDefaultID(),
45-
Usage: "specify the ID to be used for the container",
46-
},
4742
cli.BoolFlag{
4843
Name: "debug",
4944
Usage: "enable debug output for logging",

main_unsupported.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ import (
77
"github.com/codegangsta/cli"
88
)
99

10-
func getDefaultID() string {
11-
return ""
12-
}
13-
1410
var (
1511
checkpointCommand cli.Command
1612
eventsCommand cli.Command

restore.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ var restoreCommand = cli.Command{
6666
},
6767
Action: func(context *cli.Context) {
6868
imagePath := context.String("image-path")
69+
id := context.Args().First()
70+
if id == "" {
71+
fatal(errEmptyID)
72+
}
6973
if imagePath == "" {
7074
imagePath = getDefaultImagePath(context)
7175
}
@@ -79,7 +83,7 @@ var restoreCommand = cli.Command{
7983
if err != nil {
8084
fatal(err)
8185
}
82-
config, err := createLibcontainerConfig(context.GlobalString("id"), spec)
86+
config, err := createLibcontainerConfig(id, spec)
8387
if err != nil {
8488
fatal(err)
8589
}
@@ -92,14 +96,17 @@ var restoreCommand = cli.Command{
9296
}
9397

9498
func restoreContainer(context *cli.Context, spec *specs.LinuxSpec, config *configs.Config, imagePath string) (code int, err error) {
95-
rootuid := 0
99+
var (
100+
rootuid = 0
101+
id = context.Args().First()
102+
)
96103
factory, err := loadFactory(context)
97104
if err != nil {
98105
return -1, err
99106
}
100-
container, err := factory.Load(context.GlobalString("id"))
107+
container, err := factory.Load(id)
101108
if err != nil {
102-
container, err = factory.Create(context.GlobalString("id"), config)
109+
container, err = factory.Create(id, config)
103110
if err != nil {
104111
return -1, err
105112
}
@@ -111,7 +118,7 @@ func restoreContainer(context *cli.Context, spec *specs.LinuxSpec, config *confi
111118
logrus.Error(err)
112119
}
113120
if status == libcontainer.Running {
114-
fatal(fmt.Errorf("Container with id %s already running", context.GlobalString("id")))
121+
fatal(fmt.Errorf("Container with id %s already running", id))
115122
}
116123

117124
setManageCgroupsMode(context, options)

start.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,17 @@ func init() {
9393
}
9494

9595
func startContainer(context *cli.Context, spec *specs.LinuxSpec) (int, error) {
96-
config, err := createLibcontainerConfig(context.GlobalString("id"), spec)
96+
id := context.Args().First()
97+
if id == "" {
98+
return -1, errEmptyID
99+
}
100+
config, err := createLibcontainerConfig(id, spec)
97101
if err != nil {
98102
return -1, err
99103
}
100104
if _, err := os.Stat(config.Rootfs); err != nil {
101105
if os.IsNotExist(err) {
102-
return -1, fmt.Errorf("Rootfs (%q) does not exist", config.Rootfs)
106+
return -1, fmt.Errorf("rootfs (%q) does not exist", config.Rootfs)
103107
}
104108
return -1, err
105109
}
@@ -111,7 +115,7 @@ func startContainer(context *cli.Context, spec *specs.LinuxSpec) (int, error) {
111115
if err != nil {
112116
return -1, err
113117
}
114-
container, err := factory.Create(context.GlobalString("id"), config)
118+
container, err := factory.Create(id, config)
115119
if err != nil {
116120
return -1, err
117121
}

utils.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package main
44

55
import (
6+
"errors"
67
"fmt"
78
"os"
89
"path/filepath"
@@ -17,6 +18,8 @@ import (
1718

1819
const wildcard = -1
1920

21+
var errEmptyID = errors.New("container id cannot be empty")
22+
2023
var allowedDevices = []*configs.Device{
2124
// allow mknod for any device
2225
{
@@ -160,15 +163,15 @@ func loadFactory(context *cli.Context) (libcontainer.Factory, error) {
160163
// getContainer returns the specified container instance by loading it from state
161164
// with the default factory.
162165
func getContainer(context *cli.Context) (libcontainer.Container, error) {
163-
factory, err := loadFactory(context)
164-
if err != nil {
165-
return nil, err
166+
id := context.Args().First()
167+
if id == "" {
168+
return nil, errEmptyID
166169
}
167-
container, err := factory.Load(context.GlobalString("id"))
170+
factory, err := loadFactory(context)
168171
if err != nil {
169172
return nil, err
170173
}
171-
return container, nil
174+
return factory.Load(id)
172175
}
173176

174177
// fatal prints the error's details if it is a libcontainer specific error type
@@ -182,17 +185,6 @@ func fatal(err error) {
182185
os.Exit(1)
183186
}
184187

185-
// getDefaultID returns a string to be used as the container id based on the
186-
// current working directory of the runc process. This function panics
187-
// if the cwd is unable to be found based on a system error.
188-
func getDefaultID() string {
189-
cwd, err := os.Getwd()
190-
if err != nil {
191-
panic(err)
192-
}
193-
return filepath.Base(cwd)
194-
}
195-
196188
func getDefaultImagePath(context *cli.Context) string {
197189
cwd, err := os.Getwd()
198190
if err != nil {

0 commit comments

Comments
 (0)