mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-17 08:53:26 +00:00
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:
parent
045c7ae9a3
commit
44ed777c0f
@ -319,3 +319,41 @@ func (m *Manager) Destroy() error {
|
|||||||
defer m.Unlock()
|
defer m.Unlock()
|
||||||
return m.mgr.Destroy()
|
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)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user