client, ctr: allow specifying unmanaged rootfs dir
e.g. ctr run -t --rm --rootfs /tmp/busybox-rootfs foo /bin/sh (--rm removes the container but does not remove rootfs dir, of course) Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
This commit is contained in:
parent
1a054c67b1
commit
752d253f40
@ -47,7 +47,7 @@ func withMounts(context *cli.Context) containerd.SpecOpts {
|
|||||||
var runCommand = cli.Command{
|
var runCommand = cli.Command{
|
||||||
Name: "run",
|
Name: "run",
|
||||||
Usage: "run a container",
|
Usage: "run a container",
|
||||||
ArgsUsage: "IMAGE CONTAINER [COMMAND] [ARG...]",
|
ArgsUsage: "Image|RootFS ID [COMMAND] [ARG...]",
|
||||||
Flags: append([]cli.Flag{
|
Flags: append([]cli.Flag{
|
||||||
cli.BoolFlag{
|
cli.BoolFlag{
|
||||||
Name: "tty,t",
|
Name: "tty,t",
|
||||||
|
@ -18,6 +18,13 @@ import (
|
|||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
runCommand.Flags = append(runCommand.Flags, cli.BoolFlag{
|
||||||
|
Name: "rootfs",
|
||||||
|
Usage: "Use custom rootfs that is not managed by containerd snapshotter.",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func handleConsoleResize(ctx gocontext.Context, task resizer, con console.Console) error {
|
func handleConsoleResize(ctx gocontext.Context, task resizer, con console.Console) error {
|
||||||
// do an initial resize of the console
|
// do an initial resize of the console
|
||||||
size, err := con.Size()
|
size, err := con.Size()
|
||||||
@ -54,38 +61,50 @@ func setHostNetworking() containerd.SpecOpts {
|
|||||||
|
|
||||||
func newContainer(ctx gocontext.Context, client *containerd.Client, context *cli.Context) (containerd.Container, error) {
|
func newContainer(ctx gocontext.Context, client *containerd.Client, context *cli.Context) (containerd.Container, error) {
|
||||||
var (
|
var (
|
||||||
err error
|
|
||||||
checkpointIndex digest.Digest
|
|
||||||
|
|
||||||
ref = context.Args().First()
|
ref = context.Args().First()
|
||||||
id = context.Args().Get(1)
|
id = context.Args().Get(1)
|
||||||
args = context.Args()[2:]
|
args = context.Args()[2:]
|
||||||
tty = context.Bool("tty")
|
|
||||||
labelStrings = context.StringSlice("label")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
labels := labelArgs(labelStrings)
|
|
||||||
|
|
||||||
if raw := context.String("checkpoint"); raw != "" {
|
if raw := context.String("checkpoint"); raw != "" {
|
||||||
if checkpointIndex, err = digest.Parse(raw); err != nil {
|
checkpointIndex, err := digest.Parse(raw)
|
||||||
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if checkpointIndex != "" {
|
||||||
|
return client.NewContainer(ctx, id, containerd.WithCheckpoint(v1.Descriptor{
|
||||||
|
Digest: checkpointIndex,
|
||||||
|
}, id))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
opts []containerd.SpecOpts
|
||||||
|
cOpts []containerd.NewContainerOpts
|
||||||
|
)
|
||||||
|
cOpts = append(cOpts, containerd.WithContainerLabels(labelArgs(context.StringSlice("label"))))
|
||||||
|
if context.Bool("rootfs") {
|
||||||
|
opts = append(opts, containerd.WithRootFSPath(ref, context.Bool("readonly")))
|
||||||
|
} else {
|
||||||
image, err := client.GetImage(ctx, ref)
|
image, err := client.GetImage(ctx, ref)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
opts = append(opts, containerd.WithImageConfig(ctx, image))
|
||||||
if checkpointIndex == "" {
|
cOpts = append(cOpts, containerd.WithImage(image))
|
||||||
opts := []containerd.SpecOpts{
|
if context.Bool("readonly") {
|
||||||
containerd.WithImageConfig(ctx, image),
|
cOpts = append(cOpts, containerd.WithNewSnapshotView(id, image))
|
||||||
withEnv(context),
|
} else {
|
||||||
withMounts(context),
|
cOpts = append(cOpts, containerd.WithNewSnapshot(id, image))
|
||||||
}
|
}
|
||||||
|
cOpts = append(cOpts, containerd.WithSnapshotter(context.String("snapshotter")))
|
||||||
|
}
|
||||||
|
|
||||||
|
opts = append(opts, withEnv(context), withMounts(context))
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
opts = append(opts, containerd.WithProcessArgs(args...))
|
opts = append(opts, containerd.WithProcessArgs(args...))
|
||||||
}
|
}
|
||||||
if tty {
|
if context.Bool("tty") {
|
||||||
opts = append(opts, withTTY())
|
opts = append(opts, withTTY())
|
||||||
}
|
}
|
||||||
if context.Bool("net-host") {
|
if context.Bool("net-host") {
|
||||||
@ -95,25 +114,8 @@ func newContainer(ctx gocontext.Context, client *containerd.Client, context *cli
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var rootfs containerd.NewContainerOpts
|
cOpts = append([]containerd.NewContainerOpts{containerd.WithSpec(spec)}, cOpts...)
|
||||||
if context.Bool("readonly") {
|
return client.NewContainer(ctx, id, cOpts...)
|
||||||
rootfs = containerd.WithNewSnapshotView(id, image)
|
|
||||||
} else {
|
|
||||||
rootfs = containerd.WithNewSnapshot(id, image)
|
|
||||||
}
|
|
||||||
|
|
||||||
return client.NewContainer(ctx, id,
|
|
||||||
containerd.WithSpec(spec),
|
|
||||||
containerd.WithImage(image),
|
|
||||||
containerd.WithContainerLabels(labels),
|
|
||||||
containerd.WithSnapshotter(context.String("snapshotter")),
|
|
||||||
rootfs,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return client.NewContainer(ctx, id, containerd.WithCheckpoint(v1.Descriptor{
|
|
||||||
Digest: checkpointIndex,
|
|
||||||
}, id))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTask(ctx gocontext.Context, container containerd.Container, checkpoint digest.Digest, tty bool) (containerd.Task, error) {
|
func newTask(ctx gocontext.Context, container containerd.Container, checkpoint digest.Digest, tty bool) (containerd.Task, error) {
|
||||||
|
24
spec_unix.go
24
spec_unix.go
@ -30,6 +30,12 @@ const (
|
|||||||
defaultRootfsPath = "rootfs"
|
defaultRootfsPath = "rootfs"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
defaultEnv = []string{
|
||||||
|
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
func defaltCaps() []string {
|
func defaltCaps() []string {
|
||||||
return []string{
|
return []string{
|
||||||
"CAP_CHOWN",
|
"CAP_CHOWN",
|
||||||
@ -76,6 +82,7 @@ func createDefaultSpec() (*specs.Spec, error) {
|
|||||||
Path: defaultRootfsPath,
|
Path: defaultRootfsPath,
|
||||||
},
|
},
|
||||||
Process: &specs.Process{
|
Process: &specs.Process{
|
||||||
|
Env: defaultEnv,
|
||||||
Cwd: "/",
|
Cwd: "/",
|
||||||
NoNewPrivileges: true,
|
NoNewPrivileges: true,
|
||||||
User: specs.User{
|
User: specs.User{
|
||||||
@ -220,10 +227,7 @@ func WithImageConfig(ctx context.Context, i Image) SpecOpts {
|
|||||||
default:
|
default:
|
||||||
return fmt.Errorf("unknown image config media type %s", ic.MediaType)
|
return fmt.Errorf("unknown image config media type %s", ic.MediaType)
|
||||||
}
|
}
|
||||||
env := []string{
|
s.Process.Env = append(s.Process.Env, config.Env...)
|
||||||
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
|
|
||||||
}
|
|
||||||
s.Process.Env = append(env, config.Env...)
|
|
||||||
var (
|
var (
|
||||||
uid, gid uint32
|
uid, gid uint32
|
||||||
)
|
)
|
||||||
@ -262,6 +266,18 @@ func WithImageConfig(ctx context.Context, i Image) SpecOpts {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithRootFSPath specifies unmanaged rootfs path.
|
||||||
|
func WithRootFSPath(path string, readonly bool) SpecOpts {
|
||||||
|
return func(s *specs.Spec) error {
|
||||||
|
s.Root = &specs.Root{
|
||||||
|
Path: path,
|
||||||
|
Readonly: readonly,
|
||||||
|
}
|
||||||
|
// Entrypoint is not set here (it's up to caller)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// WithSpec sets the provided spec for a new container
|
// WithSpec sets the provided spec for a new container
|
||||||
func WithSpec(spec *specs.Spec) NewContainerOpts {
|
func WithSpec(spec *specs.Spec) NewContainerOpts {
|
||||||
return func(ctx context.Context, client *Client, c *containers.Container) error {
|
return func(ctx context.Context, client *Client, c *containers.Container) error {
|
||||||
|
Loading…
Reference in New Issue
Block a user