pkg/cgroups: add methods to add and remove device from the cgroup

add `AddDevice` and `RemoveDevice` to cgroup manager to allow adding
and removing devices from the device cgroup

Signed-off-by: Julio Montes <julio.montes@intel.com>
Signed-off-by: Peng Tao <bergwolf@hyper.sh>
This commit is contained in:
Julio Montes 2020-05-26 00:28:28 -07:00 committed by Peng Tao
parent 045c7ae9a3
commit 44ed777c0f

View File

@ -319,3 +319,41 @@ func (m *Manager) Destroy() error {
defer m.Unlock()
return m.mgr.Destroy()
}
// AddDevice adds a device to the device cgroup
func (m *Manager) AddDevice(device string) error {
cgroups, err := m.GetCgroups()
if err != nil {
return err
}
ld, err := DeviceToCgroupDevice(device)
if err != nil {
return err
}
m.Lock()
cgroups.Devices = append(cgroups.Devices, ld)
m.Unlock()
return m.Apply()
}
// RemoceDevice removed a device from the device cgroup
func (m *Manager) RemoveDevice(device string) error {
cgroups, err := m.GetCgroups()
if err != nil {
return err
}
m.Lock()
for i, d := range cgroups.Devices {
if d.Path == device {
cgroups.Devices = append(cgroups.Devices[:i], cgroups.Devices[i+1:]...)
m.Unlock()
return m.Apply()
}
}
m.Unlock()
return fmt.Errorf("device %v not found in the cgroup", device)
}