Basic --cap-add and --cap-drop support for native

Docker-DCO-1.1-Signed-off-by: Victor Vieux <vieux@docker.com> (github: vieux)
This commit is contained in:
Victor Vieux 2014-07-10 18:41:11 +00:00
parent 319f551614
commit 94e6dc9781
6 changed files with 42 additions and 0 deletions

View file

@ -254,6 +254,8 @@ func populateCommand(c *Container, env []string) error {
Resources: resources,
AllowedDevices: allowedDevices,
AutoCreatedDevices: autoCreatedDevices,
CapAdd: c.hostConfig.CapAdd,
CapDrop: c.hostConfig.CapDrop,
}
c.command.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
c.command.Env = env

View file

@ -140,6 +140,8 @@ type Command struct {
Mounts []Mount `json:"mounts"`
AllowedDevices []*devices.Device `json:"allowed_devices"`
AutoCreatedDevices []*devices.Device `json:"autocreated_devices"`
CapAdd []string `json:"cap_add"`
CapDrop []string `json:"cap_drop"`
Terminal Terminal `json:"-"` // standard or tty terminal
Console string `json:"-"` // dev/console path

View file

@ -14,6 +14,7 @@ import (
"github.com/dotcloud/docker/daemon/execdriver"
"github.com/dotcloud/docker/daemon/execdriver/native/configuration"
"github.com/dotcloud/docker/daemon/execdriver/native/template"
"github.com/dotcloud/docker/utils"
)
// createContainer populates and configures the container type with the
@ -42,6 +43,8 @@ func (d *driver) createContainer(c *execdriver.Command) (*libcontainer.Config, e
if err := d.setPrivileged(container); err != nil {
return nil, err
}
} else {
d.setCapabilities(container, c)
}
if err := d.setupCgroups(container, c); err != nil {
@ -136,6 +139,23 @@ func (d *driver) setPrivileged(container *libcontainer.Config) (err error) {
return nil
}
func (d *driver) setCapabilities(container *libcontainer.Config, c *execdriver.Command) {
var caps []string
for _, cap := range container.Capabilities {
if !utils.StringsContains(c.CapDrop, cap) {
caps = append(caps, cap)
}
}
for _, cap := range c.CapAdd {
if !utils.StringsContains(caps, cap) {
caps = append(caps, cap)
}
}
container.Capabilities = caps
}
func (d *driver) setupCgroups(container *libcontainer.Config, c *execdriver.Command) error {
if c.Resources != nil {
container.Cgroups.CpuShares = c.Resources.CpuShares

View file

@ -38,6 +38,8 @@ type HostConfig struct {
VolumesFrom []string
Devices []DeviceMapping
NetworkMode NetworkMode
CapAdd []string
CapDrop []string
}
func ContainerHostConfigFromJob(job *engine.Job) *HostConfig {

View file

@ -50,6 +50,8 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
flVolumesFrom opts.ListOpts
flLxcOpts opts.ListOpts
flEnvFile opts.ListOpts
flCapAdd opts.ListOpts
flCapDrop opts.ListOpts
flAutoRemove = cmd.Bool([]string{"#rm", "-rm"}, false, "Automatically remove the container when it exits (incompatible with -d)")
flDetach = cmd.Bool([]string{"d", "-detach"}, false, "Detached mode: run container in the background and print new container ID")
@ -86,6 +88,9 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
cmd.Var(&flVolumesFrom, []string{"#volumes-from", "-volumes-from"}, "Mount volumes from the specified container(s)")
cmd.Var(&flLxcOpts, []string{"#lxc-conf", "-lxc-conf"}, "(lxc exec-driver only) Add custom lxc options --lxc-conf=\"lxc.cgroup.cpuset.cpus = 0,1\"")
cmd.Var(&flCapAdd, []string{"-cap-add"}, "Add Linux capability(ies)")
cmd.Var(&flCapDrop, []string{"-cap-drop"}, "Drop Linux capability(ies)")
if err := cmd.Parse(args); err != nil {
return nil, nil, cmd, err
}
@ -258,6 +263,8 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf
VolumesFrom: flVolumesFrom.GetAll(),
NetworkMode: netMode,
Devices: deviceMappings,
CapAdd: flCapAdd.GetAll(),
CapDrop: flCapDrop.GetAll(),
}
if sysInfo != nil && flMemory > 0 && !sysInfo.SwapLimit {

View file

@ -907,3 +907,12 @@ func ValidateContextDirectory(srcPath string) error {
})
return finalError
}
func StringsContains(slice []string, s string) bool {
for _, ss := range slice {
if s == ss {
return true
}
}
return false
}