Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions libcontainer/configs/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,12 @@ type Config struct {
// bind mounts are writtable.
Readonlyfs bool `json:"readonlyfs"`

// Privatefs will mount the container's rootfs as private where mount points from the parent will not propogate
Privatefs bool `json:"privatefs"`
// RootfsMountMode is the rootfs mount propagation mode.
// On linux it is one of the followings:
// "private": rootfs is mounted as MS_PRIVATE
// "shared": rootfs is mounted as MS_SHARED
// "slave": rootfs is mounted as MS_SLAVE
RootfsMountMode PropagationMode `json:"root_mount_mode"`

// Mounts specify additional source and destination paths that will be mounted inside the container's
// rootfs and mount namespace if specified
Expand Down
3 changes: 3 additions & 0 deletions libcontainer/configs/mountpropagation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package configs

type PropagationMode string
7 changes: 7 additions & 0 deletions libcontainer/configs/mountpropagation_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package configs

const (
PRIVATE PropagationMode = "private"
SLAVE PropagationMode = "slave"
SHARED PropagationMode = "shared"
)
32 changes: 27 additions & 5 deletions libcontainer/rootfs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,14 @@ func setupRootfs(config *configs.Config, console *linuxConsole) (err error) {
if err := syscall.Chdir(config.Rootfs); err != nil {
return newSystemError(err)
}
if config.NoPivotRoot {
err = msMoveRoot(config.Rootfs)
if config.RootfsMountMode == configs.SHARED {
err = changeRoot(config.Rootfs)
} else {
err = pivotRoot(config.Rootfs, config.PivotDir)
if config.NoPivotRoot {
err = msMoveRoot(config.Rootfs)
} else {
err = pivotRoot(config.Rootfs, config.PivotDir)
}
}
if err != nil {
return newSystemError(err)
Expand Down Expand Up @@ -393,13 +397,24 @@ func mknodDevice(dest string, node *configs.Device) error {

func prepareRoot(config *configs.Config) error {
flag := syscall.MS_SLAVE | syscall.MS_REC
if config.Privatefs {
if config.RootfsMountMode == configs.PRIVATE {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use a switch here and also handle configs.SLAVE?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

configs.SLAVE is the default. Does this properly handle configs.SHARED?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SHARED is handled below.

flag = syscall.MS_PRIVATE | syscall.MS_REC
}
if config.RootfsMountMode == configs.SHARED {
flag = syscall.MS_SHARED | syscall.MS_REC
}
if err := syscall.Mount("", "/", "", uintptr(flag), ""); err != nil {
return err
}
return syscall.Mount(config.Rootfs, config.Rootfs, "bind", syscall.MS_BIND|syscall.MS_REC, "")
if err := syscall.Mount(config.Rootfs, config.Rootfs, "bind", syscall.MS_BIND|syscall.MS_REC, ""); err != nil {
return err
}
if config.RootfsMountMode == configs.SHARED {
if err := syscall.Mount("", config.Rootfs, "none", uintptr(syscall.MS_PRIVATE), ""); err != nil {
return fmt.Errorf("failed to mount rootfs %s: %v", config.Rootfs, err)
}
}
return nil
}

func setReadonly() error {
Expand Down Expand Up @@ -456,6 +471,13 @@ func msMoveRoot(rootfs string) error {
return syscall.Chdir("/")
}

func changeRoot(rootfs string) error {
if err := syscall.Chroot(rootfs); err != nil {
return err
}
return syscall.Chdir("/")
}

// createIfNotExists creates a file or a directory only if it does not already exist.
func createIfNotExists(path string, isDir bool) error {
if _, err := os.Stat(path); err != nil {
Expand Down
12 changes: 11 additions & 1 deletion spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ var namespaceMapping = map[string]configs.NamespaceType{
"uts": configs.NEWUTS,
}

var mountPropagationMapping = map[string]configs.PropagationMode{
"private": configs.PRIVATE,
"slave": configs.SLAVE,
"shared": configs.SHARED,
}

// loadSpec loads the specification from the provided path.
// If the path is empty then the default path will be "config.json"
func loadSpec(path string) (*specs.LinuxSpec, error) {
Expand Down Expand Up @@ -190,7 +196,11 @@ func createLibcontainerConfig(spec *specs.LinuxSpec) (*configs.Config, error) {
Capabilities: spec.Linux.Capabilities,
Readonlyfs: spec.Root.Readonly,
Hostname: spec.Hostname,
Privatefs: true,
}
if p, exists := mountPropagationMapping[spec.Linux.RootfsPropagation]; exists {
config.RootfsMountMode = p
} else {
config.RootfsMountMode = mountPropagationMapping["slave"]
}
for _, ns := range spec.Linux.Namespaces {
t, exists := namespaceMapping[ns.Type]
Expand Down