mirror of
https://github.com/aljazceru/kata-containers.git
synced 2026-01-25 09:14:29 +01:00
runtime: Move the resourcecontrol package one layer up
And try to reduce the number of virtcontainers packages, step by step. Signed-off-by: Samuel Ortiz <s.ortiz@apple.com>
This commit is contained in:
committed by
Samuel Ortiz
parent
823faee83a
commit
9fd4e5514f
59
src/runtime/pkg/resourcecontrol/utils.go
Normal file
59
src/runtime/pkg/resourcecontrol/utils.go
Normal file
@@ -0,0 +1,59 @@
|
||||
// Copyright (c) 2020 Intel Corporation
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
|
||||
package resourcecontrol
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user