UPDATE:. I can't reproduce the problem anymore. The below code works fine in both cases. (I am not sure if I should remove the question then.).


我使用cgroupus GO包v2,我不能创建一个cgroup,其中一些参数设置为非默认值.我负责CentOS—9

以下GO代码运行良好:

package main
import (
    "fmt"
    cgroupsv2 "github.com/containerd/cgroups/v2"
)
func main() {
    res := cgroupsv2.Resources{}
    //quota := int64(200000)
    //period  := uint64(1000000)
    //max := cgroupsv2.NewCPUMax(&quota, &period)
    //cpu := cgroupsv2.CPU{Max: max}
    //res = cgroupsv2.Resources{CPU: &cpu}
    cgroupManager, err := cgroupsv2.NewManager("/sys/fs/cgroup/", "/mytestgroup", &res)
    if err != nil {
        fmt.Printf("Error creating cgroup: %v\n", err)
        return
    } else {
        fmt.Println("The group created successfully")
    }
    cgroupManager.Delete()
}

$ go build -o test
$ sudo ./test
The group created successfully

但是,如果我取消注释删除的行,我会得到一个权限错误.

$ go build -o test
$ sudo ./test
Error creating cgroup: open /sys/fs/cgroup/mytestgroup/cpu.max: permission denied

以下是SELinux的状态,如果这很重要的话

$ sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   permissive
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Memory protection checking:     actual (secure)
Max kernel policy version:      33

Thank you for your help.

推荐答案

我正在使用这个代码,它运行得很好

package main
import (
        "fmt"
        cgroupsv2 "github.com/containerd/cgroups/v3/cgroup2" // Old lib is not resolved
)
func main() {
        res := cgroupsv2.Resources{}
        quota := int64(200000)
        period  := uint64(1000000)
        max := cgroupsv2.NewCPUMax(&quota, &period)
        cpu := cgroupsv2.CPU{Max: max}
        res = cgroupsv2.Resources{CPU: &cpu}
        cgroupManager, err := cgroupsv2.NewManager("/sys/fs/cgroup/", "/mytestgroup", &res)
        if err != nil {
                fmt.Printf("Error creating cgroup: %v\n", err)
                return
        } else {
                fmt.Println("The group created successfully")
        }
        cgroupManager.Delete()
}

如果代码没有按预期执行,请验证/sys/fs/cgroup目录是否具有必要的读写(rw)权限.您可以通过执行以下命令来判断/sys/fs/cgroup文件系统的当前装载选项:

mount | grep /sys/fs/cgroup

如果/sys/fs/cgroup文件系统是以只读方式装载的,则可以使用以下命令以读写权限重新装载它:

sudo mount -o remount,rw /sys/fs/cgroup

然后再try 运行你的程序.

Linux相关问答推荐

如何确定Linux上的最大静态TLS(线程本地存储)块大小?

是否可以在Bash正则表达式中排除?

如何将netcdf文件列表中的特定变量添加到不同的文件中?

AWK+向AWK导出值未传递

Docker运行错误:exec/app/backend/server:没有这样的文件或目录

为什么库中不调用全局变量的构造函数?

Linux TTY 操作顺序

使用 PowerShell 删除重复行所需的时间比 WSL 长得多

ln命令报错target not a directory

在不编写任何代码的情况下,是否有一个命令可以检索当前 shell 的亲和力中的可用内核数?

用于判断进程是否正在运行并对结果采取行动的 Linux 脚本

关于 putenv() 和 setenv() 的问题

bash 中的sed命令

如何从任意 pthread_t 获取线程 ID?

如何让 GNU 屏幕读取 .bash_profile/.bash_rc 更改?

如果关键字触发然后执行命令,Shell 脚本来监视日志(log)文件?

用于提取 IP 地址的 Linux bash 脚本

bash中变量名后的2个逗号是什么意思?

在 shell 脚本的 for 循环中迭代行而不是单词

Bash 将 awk 的输出捕获到数组中