diff --git a/src/runtime/virtcontainers/pkg/cgroups/utils.go b/src/runtime/virtcontainers/pkg/cgroups/utils.go new file mode 100644 index 0000000000..383bfb11dc --- /dev/null +++ b/src/runtime/virtcontainers/pkg/cgroups/utils.go @@ -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 +} diff --git a/src/runtime/virtcontainers/pkg/cgroups/utils_linux.go b/src/runtime/virtcontainers/pkg/cgroups/utils_linux.go index afc095bbac..6bb7bdec41 100644 --- a/src/runtime/virtcontainers/pkg/cgroups/utils_linux.go +++ b/src/runtime/virtcontainers/pkg/cgroups/utils_linux.go @@ -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 -}