From 12bd4cffd03597e1f4513945891cf8d4b231e32c Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Wed, 30 Mar 2016 11:12:03 -0700 Subject: [PATCH] Add --no-pivot option for containers on ramdisk This adds a `--no-pivot` cli flag to runc so that a container's rootfs can be located ontop of ramdisk/tmpfs and not fail because you cannot pivot root. This should be a cli flag and not part of the spec because this is a detail of the host/runtime environment and not an attribute of a container. Signed-off-by: Michael Crosby --- libcontainer/specconv/spec_linux.go | 19 ++++++++++++++----- restore.go | 11 ++++++++++- start.go | 4 ++++ utils.go | 7 ++++++- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/libcontainer/specconv/spec_linux.go b/libcontainer/specconv/spec_linux.go index 5d7a2ad49..4ad0175c9 100644 --- a/libcontainer/specconv/spec_linux.go +++ b/libcontainer/specconv/spec_linux.go @@ -158,9 +158,16 @@ var allowedDevices = []*configs.Device{ }, } +type CreateOpts struct { + CgroupName string + UseSystemdCgroup bool + NoPivotRoot bool + Spec *specs.Spec +} + // CreateLibcontainerConfig creates a new libcontainer configuration from a // given specification and a cgroup name -func CreateLibcontainerConfig(cgroupName string, useSystemdCgroup bool, spec *specs.Spec) (*configs.Config, error) { +func CreateLibcontainerConfig(opts *CreateOpts) (*configs.Config, error) { // runc's cwd will always be the bundle path rcwd, err := os.Getwd() if err != nil { @@ -170,14 +177,16 @@ func CreateLibcontainerConfig(cgroupName string, useSystemdCgroup bool, spec *sp if err != nil { return nil, err } + spec := opts.Spec rootfsPath := spec.Root.Path if !filepath.IsAbs(rootfsPath) { rootfsPath = filepath.Join(cwd, rootfsPath) } config := &configs.Config{ - Rootfs: rootfsPath, - Readonlyfs: spec.Root.Readonly, - Hostname: spec.Hostname, + Rootfs: rootfsPath, + NoPivotRoot: opts.NoPivotRoot, + Readonlyfs: spec.Root.Readonly, + Hostname: spec.Hostname, Labels: []string{ "bundle=" + cwd, }, @@ -211,7 +220,7 @@ func CreateLibcontainerConfig(cgroupName string, useSystemdCgroup bool, spec *sp if err := setupUserNamespace(spec, config); err != nil { return nil, err } - c, err := createCgroupConfig(cgroupName, useSystemdCgroup, spec) + c, err := createCgroupConfig(opts.CgroupName, opts.UseSystemdCgroup, spec) if err != nil { return nil, err } diff --git a/restore.go b/restore.go index 48349356b..f015e6667 100644 --- a/restore.go +++ b/restore.go @@ -73,6 +73,10 @@ using the runc checkpoint command.`, Name: "no-subreaper", Usage: "disable the use of the subreaper used to reap reparented processes", }, + cli.BoolFlag{ + Name: "no-pivot", + Usage: "do not use pivot root to jail process inside rootfs. This should be used whenever the rootfs is on top of a ramdisk", + }, }, Action: func(context *cli.Context) { imagePath := context.String("image-path") @@ -93,7 +97,12 @@ using the runc checkpoint command.`, if err != nil { fatal(err) } - config, err := specconv.CreateLibcontainerConfig(id, context.GlobalBool("systemd-cgroup"), spec) + config, err := specconv.CreateLibcontainerConfig(&specconv.CreateOpts{ + CgroupName: id, + UseSystemdCgroup: context.GlobalBool("systemd-cgroup"), + NoPivotRoot: context.Bool("no-pivot"), + Spec: spec, + }) if err != nil { fatal(err) } diff --git a/start.go b/start.go index 277daa486..b0d50ecf0 100644 --- a/start.go +++ b/start.go @@ -53,6 +53,10 @@ command(s) that get executed on start, edit the args parameter of the spec. See Name: "no-subreaper", Usage: "disable the use of the subreaper used to reap reparented processes", }, + cli.BoolFlag{ + Name: "no-pivot", + Usage: "do not use pivot root to jail process inside rootfs. This should be used whenever the rootfs is on top of a ramdisk", + }, }, Action: func(context *cli.Context) { bundle := context.String("bundle") diff --git a/utils.go b/utils.go index 1f611eaec..e1bfe38b5 100644 --- a/utils.go +++ b/utils.go @@ -175,7 +175,12 @@ func createPidFile(path string, process *libcontainer.Process) error { } func createContainer(context *cli.Context, id string, spec *specs.Spec) (libcontainer.Container, error) { - config, err := specconv.CreateLibcontainerConfig(id, context.GlobalBool("systemd-cgroup"), spec) + config, err := specconv.CreateLibcontainerConfig(&specconv.CreateOpts{ + CgroupName: id, + UseSystemdCgroup: context.GlobalBool("systemd-cgroup"), + NoPivotRoot: context.Bool("no-pivot"), + Spec: spec, + }) if err != nil { return nil, err }