2014-12-17 17:12:23 +08:00
package configs
2014-07-09 01:17:05 +08:00
2015-02-01 11:56:27 +08:00
import "fmt"
2015-01-27 20:54:19 +08:00
2015-02-01 11:56:27 +08:00
type Rlimit struct {
2015-02-12 08:45:23 +08:00
Type int ` json:"type" `
Hard uint64 ` json:"hard" `
Soft uint64 ` json:"soft" `
2015-01-27 20:54:19 +08:00
}
2015-02-01 11:56:27 +08:00
// IDMap represents UID/GID Mappings for User Namespaces.
type IDMap struct {
2015-02-12 08:45:23 +08:00
ContainerID int ` json:"container_id" `
HostID int ` json:"host_id" `
Size int ` json:"size" `
2014-11-25 06:39:32 +08:00
}
2014-07-09 01:17:05 +08:00
// Config defines configuration options for executing a process inside a contained environment.
type Config struct {
2015-02-01 11:56:27 +08:00
// NoPivotRoot will use MS_MOVE and a chroot to jail the process into the container's rootfs
// This is a common option when the container is running in ramdisk
2015-02-12 08:45:23 +08:00
NoPivotRoot bool ` json:"no_pivot_root" `
2015-02-01 11:56:27 +08:00
2015-02-07 10:50:11 +08:00
// ParentDeathSignal specifies the signal that is sent to the container's process in the case
// that the parent process dies.
2015-02-12 08:45:23 +08:00
ParentDeathSignal int ` json:"parent_death_signal" `
2015-02-07 10:50:11 +08:00
2015-02-01 11:56:27 +08:00
// PivotDir allows a custom directory inside the container's root filesystem to be used as pivot, when NoPivotRoot is not set.
// When a custom PivotDir not set, a temporary dir inside the root filesystem will be used. The pivot dir needs to be writeable.
// This is required when using read only root filesystems. In these cases, a read/writeable path can be (bind) mounted somewhere inside the root filesystem to act as pivot.
2015-02-12 08:45:23 +08:00
PivotDir string ` json:"pivot_dir" `
2015-02-01 11:56:27 +08:00
2015-02-04 09:44:58 +08:00
// Path to a directory containing the container's root filesystem.
2015-02-12 08:45:23 +08:00
Rootfs string ` json:"rootfs" `
2015-02-04 09:44:58 +08:00
// Readonlyfs will remount the container's rootfs as readonly where only externally mounted
// bind mounts are writtable.
2015-02-12 08:45:23 +08:00
Readonlyfs bool ` json:"readonlyfs" `
2015-02-01 11:56:27 +08:00
// Mounts specify additional source and destination paths that will be mounted inside the container's
// rootfs and mount namespace if specified
2015-02-12 08:45:23 +08:00
Mounts [ ] * Mount ` json:"mounts" `
2015-02-01 11:56:27 +08:00
// The device nodes that should be automatically created within the container upon container start. Note, make sure that the node is marked as allowed in the cgroup as well!
2015-02-12 08:45:23 +08:00
Devices [ ] * Device ` json:"devices" `
2015-02-01 11:56:27 +08:00
2015-02-12 08:45:23 +08:00
MountLabel string ` json:"mount_label" `
2014-07-09 01:17:05 +08:00
// Hostname optionally sets the container's hostname if provided
2015-02-12 08:45:23 +08:00
Hostname string ` json:"hostname" `
2014-07-09 01:17:05 +08:00
// Namespaces specifies the container's namespaces that it should setup when cloning the init process
// If a namespace is not provided that namespace is shared from the container's parent process
2015-02-12 08:45:23 +08:00
Namespaces Namespaces ` json:"namespaces" `
2014-07-09 01:17:05 +08:00
// Capabilities specify the capabilities to keep when executing the process inside the container
// All capbilities not specified will be dropped from the processes capability mask
2015-02-12 08:45:23 +08:00
Capabilities [ ] string ` json:"capabilities" `
2014-07-09 01:17:05 +08:00
// Networks specifies the container's network setup to be created
2015-02-12 08:45:23 +08:00
Networks [ ] * Network ` json:"networks" `
2014-07-09 01:17:05 +08:00
// Routes can be specified to create entries in the route table as the container is started
2015-02-12 08:45:23 +08:00
Routes [ ] * Route ` json:"routes" `
2014-07-09 01:17:05 +08:00
// Cgroups specifies specific cgroup settings for the various subsystems that the container is
// placed into to limit the resources the container has available
2015-02-12 08:45:23 +08:00
Cgroups * Cgroup ` json:"cgroups" `
2014-07-09 01:17:05 +08:00
// AppArmorProfile specifies the profile to apply to the process running in the container and is
// change at the time the process is execed
2015-02-12 08:45:23 +08:00
AppArmorProfile string ` json:"apparmor_profile" `
2014-07-09 01:17:05 +08:00
// ProcessLabel specifies the label to apply to the process running in the container. It is
// commonly used by selinux
2015-02-12 08:45:23 +08:00
ProcessLabel string ` json:"process_label" `
2014-07-09 01:17:05 +08:00
2014-11-27 02:16:53 +08:00
// Rlimits specifies the resource limits, such as max open files, to set in the container
// If Rlimits are not set, the container will inherit rlimits from the parent process
2015-02-12 08:45:23 +08:00
Rlimits [ ] Rlimit ` json:"rlimits" `
2015-01-27 20:54:19 +08:00
// AdditionalGroups specifies the gids that should be added to supplementary groups
// in addition to those that the user belongs to.
2015-02-12 08:45:23 +08:00
AdditionalGroups [ ] int ` json:"additional_groups" `
2015-02-01 11:56:27 +08:00
2015-01-27 20:54:19 +08:00
// UidMappings is an array of User ID mappings for User Namespaces
2015-02-12 08:45:23 +08:00
UidMappings [ ] IDMap ` json:"uid_mappings" `
2015-01-27 20:54:19 +08:00
// GidMappings is an array of Group ID mappings for User Namespaces
2015-02-12 08:45:23 +08:00
GidMappings [ ] IDMap ` json:"gid_mappings" `
2015-02-13 08:23:05 +08:00
// MaskPaths specifies paths within the container's rootfs to mask over with a bind
// mount pointing to /dev/null as to prevent reads of the file.
MaskPaths [ ] string ` json:"mask_paths" `
// ReadonlyPaths specifies paths within the container's rootfs to remount as read-only
// so that these files prevent any writes.
ReadonlyPaths [ ] string ` json:"readonly_paths" `
2014-07-09 01:17:05 +08:00
}
2015-02-01 11:56:27 +08:00
// Gets the root uid for the process on host which could be non-zero
// when user namespaces are enabled.
2015-02-25 08:06:53 +08:00
func ( c Config ) HostUID ( ) ( int , error ) {
2015-02-01 11:56:27 +08:00
if c . Namespaces . Contains ( NEWUSER ) {
if c . UidMappings == nil {
return - 1 , fmt . Errorf ( "User namespaces enabled, but no user mappings found." )
}
id , found := c . hostIDFromMapping ( 0 , c . UidMappings )
if ! found {
return - 1 , fmt . Errorf ( "User namespaces enabled, but no root user mapping found." )
}
return id , nil
}
// Return default root uid 0
return 0 , nil
2014-07-09 01:17:05 +08:00
}
2014-11-27 02:16:53 +08:00
2015-02-01 11:56:27 +08:00
// Gets the root uid for the process on host which could be non-zero
// when user namespaces are enabled.
2015-02-25 08:06:53 +08:00
func ( c Config ) HostGID ( ) ( int , error ) {
2015-02-01 11:56:27 +08:00
if c . Namespaces . Contains ( NEWUSER ) {
if c . GidMappings == nil {
return - 1 , fmt . Errorf ( "User namespaces enabled, but no gid mappings found." )
}
id , found := c . hostIDFromMapping ( 0 , c . GidMappings )
if ! found {
return - 1 , fmt . Errorf ( "User namespaces enabled, but no root user mapping found." )
}
return id , nil
}
// Return default root uid 0
return 0 , nil
2014-11-27 02:16:53 +08:00
}
2015-01-27 20:54:19 +08:00
2015-02-01 11:56:27 +08:00
// Utility function that gets a host ID for a container ID from user namespace map
// if that ID is present in the map.
2015-02-25 08:06:53 +08:00
func ( c Config ) hostIDFromMapping ( containerID int , uMap [ ] IDMap ) ( int , bool ) {
2015-02-01 11:56:27 +08:00
for _ , m := range uMap {
if ( containerID >= m . ContainerID ) && ( containerID <= ( m . ContainerID + m . Size - 1 ) ) {
hostID := m . HostID + ( containerID - m . ContainerID )
return hostID , true
}
}
return - 1 , false
2015-01-27 20:54:19 +08:00
}