Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
pkg/cgroups: add methods to add and remove device from the cgroup
Browse files Browse the repository at this point in the history
add `AddDevice` and `RemoveDevice` to cgroup manager to allow adding and
removing devices from the device cgroup

Signed-off-by: Julio Montes <[email protected]>
  • Loading branch information
Julio Montes committed Apr 28, 2020
1 parent b345855 commit 3fceece
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions virtcontainers/pkg/cgroups/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 3fceece

Please sign in to comment.