mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-08-12 13:22:28 +00:00
virtcontainers: cgroups: Move non Linux routine to utils.go
Have an OS agnostic file for sharing routines. Signed-off-by: Samuel Ortiz <s.ortiz@apple.com>
This commit is contained in:
parent
d49d0b6f39
commit
ad10e201e1
59
src/runtime/virtcontainers/pkg/cgroups/utils.go
Normal file
59
src/runtime/virtcontainers/pkg/cgroups/utils.go
Normal file
@ -0,0 +1,59 @@
|
||||
// Copyright (c) 2020 Intel Corporation
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
|
||||
package cgroups
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/opencontainers/runc/libcontainer/devices"
|
||||
"github.com/opencontainers/runtime-spec/specs-go"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func DeviceToCgroupDeviceRule(device string) (*devices.Rule, error) {
|
||||
var st unix.Stat_t
|
||||
deviceRule := devices.Rule{
|
||||
Allow: true,
|
||||
Permissions: "rwm",
|
||||
}
|
||||
|
||||
if err := unix.Stat(device, &st); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
devType := st.Mode & unix.S_IFMT
|
||||
|
||||
switch devType {
|
||||
case unix.S_IFCHR:
|
||||
deviceRule.Type = 'c'
|
||||
case unix.S_IFBLK:
|
||||
deviceRule.Type = 'b'
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported device type: %v", devType)
|
||||
}
|
||||
|
||||
major := int64(unix.Major(st.Rdev))
|
||||
minor := int64(unix.Minor(st.Rdev))
|
||||
deviceRule.Major = major
|
||||
deviceRule.Minor = minor
|
||||
|
||||
return &deviceRule, nil
|
||||
}
|
||||
|
||||
func DeviceToLinuxDevice(device string) (specs.LinuxDeviceCgroup, error) {
|
||||
dev, err := DeviceToCgroupDeviceRule(device)
|
||||
if err != nil {
|
||||
return specs.LinuxDeviceCgroup{}, err
|
||||
}
|
||||
|
||||
return specs.LinuxDeviceCgroup{
|
||||
Allow: dev.Allow,
|
||||
Type: string(dev.Type),
|
||||
Major: &dev.Major,
|
||||
Minor: &dev.Minor,
|
||||
Access: string(dev.Permissions),
|
||||
}, nil
|
||||
}
|
@ -15,9 +15,6 @@ import (
|
||||
systemdDbus "github.com/coreos/go-systemd/v22/dbus"
|
||||
"github.com/godbus/dbus/v5"
|
||||
"github.com/opencontainers/runc/libcontainer/cgroups/systemd"
|
||||
"github.com/opencontainers/runc/libcontainer/devices"
|
||||
"github.com/opencontainers/runtime-spec/specs-go"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// validCgroupPath returns a valid cgroup path.
|
||||
@ -129,48 +126,3 @@ func getSliceAndUnit(cgroupPath string) (string, string, error) {
|
||||
|
||||
return "", "", fmt.Errorf("Path: %s is not valid systemd's cgroups path", cgroupPath)
|
||||
}
|
||||
|
||||
func DeviceToCgroupDeviceRule(device string) (*devices.Rule, error) {
|
||||
var st unix.Stat_t
|
||||
deviceRule := devices.Rule{
|
||||
Allow: true,
|
||||
Permissions: "rwm",
|
||||
}
|
||||
|
||||
if err := unix.Stat(device, &st); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
devType := st.Mode & unix.S_IFMT
|
||||
|
||||
switch devType {
|
||||
case unix.S_IFCHR:
|
||||
deviceRule.Type = 'c'
|
||||
case unix.S_IFBLK:
|
||||
deviceRule.Type = 'b'
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported device type: %v", devType)
|
||||
}
|
||||
|
||||
major := int64(unix.Major(st.Rdev))
|
||||
minor := int64(unix.Minor(st.Rdev))
|
||||
deviceRule.Major = major
|
||||
deviceRule.Minor = minor
|
||||
|
||||
return &deviceRule, nil
|
||||
}
|
||||
|
||||
func DeviceToLinuxDevice(device string) (specs.LinuxDeviceCgroup, error) {
|
||||
dev, err := DeviceToCgroupDeviceRule(device)
|
||||
if err != nil {
|
||||
return specs.LinuxDeviceCgroup{}, err
|
||||
}
|
||||
|
||||
return specs.LinuxDeviceCgroup{
|
||||
Allow: dev.Allow,
|
||||
Type: string(dev.Type),
|
||||
Major: &dev.Major,
|
||||
Minor: &dev.Minor,
|
||||
Access: string(dev.Permissions),
|
||||
}, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user