2015-05-14 06:42:16 +08:00
|
|
|
// +build linux
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
package fs
|
|
|
|
|
2015-02-25 17:20:01 +08:00
|
|
|
import (
|
2015-06-22 10:29:59 +08:00
|
|
|
"github.com/opencontainers/runc/libcontainer/cgroups"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
2015-02-25 17:20:01 +08:00
|
|
|
)
|
2014-05-28 08:01:08 +08:00
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
type DevicesGroup struct {
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
|
|
|
|
2015-10-16 06:19:23 +08:00
|
|
|
func (s *DevicesGroup) Name() string {
|
|
|
|
return "devices"
|
|
|
|
}
|
|
|
|
|
2015-11-05 18:41:08 +08:00
|
|
|
func (s *DevicesGroup) Apply(d *cgroupData) error {
|
2015-12-19 23:45:33 +08:00
|
|
|
dir, err := d.join("devices")
|
2014-05-15 06:21:44 +08:00
|
|
|
if err != nil {
|
2015-04-22 10:18:22 +08:00
|
|
|
// We will return error even it's `not found` error, devices
|
|
|
|
// cgroup is hard requirement for container's security.
|
2015-03-24 03:14:03 +08:00
|
|
|
return err
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|
2015-12-19 23:45:33 +08:00
|
|
|
|
|
|
|
if err := s.Set(dir, d.config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-05-15 06:21:44 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-25 17:20:01 +08:00
|
|
|
func (s *DevicesGroup) Set(path string, cgroup *configs.Cgroup) error {
|
2015-12-15 08:26:29 +08:00
|
|
|
if !cgroup.Resources.AllowAllDevices {
|
2015-02-25 17:20:01 +08:00
|
|
|
if err := writeFile(path, "devices.deny", "a"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-15 08:26:29 +08:00
|
|
|
for _, dev := range cgroup.Resources.AllowedDevices {
|
2015-02-25 17:20:01 +08:00
|
|
|
if err := writeFile(path, "devices.allow", dev.CgroupString()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-03-31 16:36:00 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := writeFile(path, "devices.allow", "a"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-15 08:26:29 +08:00
|
|
|
for _, dev := range cgroup.Resources.DeniedDevices {
|
2015-03-31 16:36:00 +08:00
|
|
|
if err := writeFile(path, "devices.deny", dev.CgroupString()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-02-25 17:20:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-05 18:41:08 +08:00
|
|
|
func (s *DevicesGroup) Remove(d *cgroupData) error {
|
2014-05-15 06:21:44 +08:00
|
|
|
return removePath(d.path("devices"))
|
|
|
|
}
|
|
|
|
|
2014-06-20 21:13:56 +08:00
|
|
|
func (s *DevicesGroup) GetStats(path string, stats *cgroups.Stats) error {
|
2014-05-28 08:01:08 +08:00
|
|
|
return nil
|
2014-05-15 06:21:44 +08:00
|
|
|
}
|