diff --git a/cmd/ctr/commands/run/run_unix.go b/cmd/ctr/commands/run/run_unix.go index ac4e8e9cb..920edbfd6 100644 --- a/cmd/ctr/commands/run/run_unix.go +++ b/cmd/ctr/commands/run/run_unix.go @@ -205,24 +205,9 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli } } - runtimeOpts := &options.Options{} - if runcBinary := context.String("runc-binary"); runcBinary != "" { - if context.String("runtime") == "io.containerd.runc.v2" { - runtimeOpts.BinaryName = runcBinary - } else { - return nil, errors.New("specifying runc-binary is only supported for \"io.containerd.runc.v2\" runtime") - } - } - if context.Bool("runc-systemd-cgroup") { - if context.String("runtime") == "io.containerd.runc.v2" { - if context.String("cgroup") == "" { - // runc maps "machine.slice:foo:deadbeef" to "/machine.slice/foo-deadbeef.scope" - return nil, errors.New("option --runc-systemd-cgroup requires --cgroup to be set, e.g. \"machine.slice:foo:deadbeef\"") - } - runtimeOpts.SystemdCgroup = true - } else { - return nil, errors.New("specifying runc-systemd-cgroup is only supported for \"io.containerd.runc.v2\" runtime") - } + runtimeOpts, err := getRuntimeOptions(context) + if err != nil { + return nil, err } cOpts = append(cOpts, containerd.WithRuntime(context.String("runtime"), runtimeOpts)) @@ -237,6 +222,36 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli return client.NewContainer(ctx, id, cOpts...) } +func getRuncOptions(context *cli.Context) (*options.Options, error) { + runtimeOpts := &options.Options{} + if runcBinary := context.String("runc-binary"); runcBinary != "" { + runtimeOpts.BinaryName = runcBinary + } + if context.Bool("runc-systemd-cgroup") { + if context.String("cgroup") == "" { + // runc maps "machine.slice:foo:deadbeef" to "/machine.slice/foo-deadbeef.scope" + return nil, errors.New("option --runc-systemd-cgroup requires --cgroup to be set, e.g. \"machine.slice:foo:deadbeef\"") + } + runtimeOpts.SystemdCgroup = true + } + + return runtimeOpts, nil +} + +func getRuntimeOptions(context *cli.Context) (interface{}, error) { + // validate first + if (context.String("runc-binary") != "" || context.Bool("runc-systemd-cgroup")) && + context.String("runtime") != "io.containerd.runc.v2" { + return nil, errors.New("specifying runc-binary and runc-systemd-cgroup is only supported for \"io.containerd.runc.v2\" runtime") + } + + if context.String("runtime") == "io.containerd.runc.v2" { + return getRuncOptions(context) + } + + return nil, nil +} + func getNewTaskOpts(context *cli.Context) []containerd.NewTaskOpts { var ( tOpts []containerd.NewTaskOpts