The error cgroups: cgroup mountpoint does not exist indicates that the cgroup filesystem is not mounted at the expected location (/sys/fs/cgroup) on your system. This is a common issue when working with cgroups, especially in environments where cgroups are not properly set up, such as minimal container runtimes, certain Linux distributions, or systems with cgroup v2 (unified hierarchy) instead of cgroup v1 (legacy hierarchy).
Below, I'll explain the issue, provide steps to diagnose and fix it, and suggest modifications to your code to handle both cgroup v1 and cgroup v2.
Understanding the Issue
Cgroup Mountpoint (/sys/fs/cgroup):
The /sys/fs/cgroup directory is the standard mountpoint for the cgroup filesystem in Linux.
In cgroup v1 (legacy), this directory contains subdirectories for each controller (e.g., cpu, memory, pids, etc.).
In cgroup v2 (unified), /sys/fs/cgroup is a single hierarchy, and controllers are managed differently.
Your Code:
Your code uses the github.com/opencontainers/runc/libcontainer/cgroups package, which is designed for cgroup v1 by default.
If your system uses cgroup v2 (common in modern Linux distributions like Ubuntu 21.04+, Fedora 31+, etc.), the /sys/fs/cgroup directory might not have the expected structure, or the cgroup v1 controllers might not be mounted.
Error Cause:
The error occurs because the cgroup v1 controllers are not mounted at /sys/fs/cgroup, or the system is using cgroup v2, which is not compatible with the cgroup1 package you're using.
Diagnosing the Issue
To determine the root cause, follow these steps:
Check if /sys/fs/cgroup Exists:
bash
ls /sys/fs/cgroup
If the directory does not exist, the cgroup filesystem is not mounted.
If it exists, check its contents. For cgroup v1, you should see subdirectories like cpu, memory, pids, etc. For cgroup v2, you might see a single hierarchy with files like cgroup.controllers.
:
Run the following command to check if your system uses cgroup v1 or v2:
Check Cgroup Version
bash
stat -fc %T /sys/fs/cgroup/
If the output is cgroup2fs, your system uses cgroup v2.
If the output is tmpfs, your system uses cgroup v1.
Check Mounts:
Run the following command to see how cgroups are mounted:
bash
mount | grep cgroup
For cgroup v1, you should see multiple mounts under /sys/fs/cgroup (e.g., /sys/fs/cgroup/cpu, /sys/fs/cgroup/memory, etc.).
For cgroup v2, you should see a single mount of type cgroup2 at /sys/fs/cgroup.
Fixing the Issue
Option 1: Ensure Cgroup v1 is Enabled (if Compatible)
If your system supports cgroup v1 and you want to use it (e.g., for compatibility with your code), you can enable cgroup v1 by rebooting your system with the appropriate kernel parameters.
Edit GRUB Configuration:
Open the GRUB configuration file:
bash
sudo nano /etc/default/grub
Add Kernel Parameters:
Modify the GRUB_CMDLINE_LINUX_DEFAULT line to include the following parameters:
systemd.unified_cgroup_hierarchy=0 forces the system to use cgroup v1.
Update GRUB and Reboot:
bash
sudo update-grub
sudo reboot
Verify:
After rebooting, verify that cgroup v1 is in use:
bash
mount | grep cgroup
You should see multiple cgroup v1 mounts under /sys/fs/cgroup.
Option 2: Use Cgroup v2 (Modern Approach)
If your system uses cgroup v2 (recommended for modern systems), you need to modify your code to use a library that supports cgroup v2, such as github.com/containerd/cgroups/v2. The cgroup1 package you're using is designed for cgroup v1 and won't work with cgroup v2.
Here's how you can modify your code to support cgroup v2:
Install the Cgroup v2 Library:
Add the containerd/cgroups library to your project:
bash
go get github.com/containerd/cgroups/v2
Update Your Code:
Replace the cgroup1 package with the cgroups/v2 package and update the logic to work with cgroup v2. Below is an example of how to modify your code:
go
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/containerd/cgroups/v2"
"github.com/opencontainers/runtime-spec/specs-go"
)
// CgroupsSandbox represents a sandbox with cgroup isolation
type CgroupsSandbox struct {
workDir string
cgroup *cgroups.Manager
}
// NewCgroupsSandbox creates a new sandbox with cgroup isolation
func NewCgroupsSandbox() (*CgroupsSandbox, error) {
// Create temporary work directory
workDir, err := os.MkdirTemp("", "cgsb_*")
if err != nil {
return nil, fmt.Errorf("failed to create work directory: %w", err)
}
// Check if cgroup v2 is supported
if !cgroups.Mode() == cgroups.Unified {
os.RemoveAll(workDir)
return nil, fmt.Errorf("cgroup v2 is not supported on this system")
}
// Define cgroup resources (example settings)
resources := &specs.LinuxResources{
CPU: &specs.LinuxCPU{
Quota: int64Ptr(100000), // 100ms CPU quota
Period: uint64Ptr(1000000), // 1s CPU period
},
Memory: &specs.LinuxMemory{
Limit: int64Ptr(100 * 1024 * 1024), // 100MB memory limit
},
}
// Create a new cgroup with a unique name
cgroupName := fmt.Sprintf("sandbox_%d", os.Getpid())
cgroupPath := filepath.Join(cgroups.V2, cgroupName)
// Create the cgroup manager
manager, err := cgroups.NewManager(cgroups.V2, cgroupPath, resources)
if err != nil {
os.RemoveAll(workDir)
return nil, fmt.Errorf("failed to create cgroup: %w", err)
}
return &CgroupsSandbox{
workDir: workDir,
cgroup: manager,
}, nil
}
// Cleanup removes the work directory and cgroup
func (s *CgroupsSandbox) Cleanup() error {
if err := s.cgroup.Delete(); err != nil {
return fmt.Errorf("failed to delete cgroup: %w", err)
}
if err := os.RemoveAll(s.workDir); err != nil {
return fmt.Errorf("failed to remove work directory: %w", err)
}
return nil
}
// Helper function to create int64 pointers
func int64Ptr(i int64) *int64 {
return &i
}
// Helper function to create uint64 pointers
func uint64Ptr(i uint64) *uint64 {
return &i
}
func main() {
sandbox, err := NewCgroupsSandbox()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
defer sandbox.Cleanup()
fmt.Println("Cgroup sandbox created successfully!")
}
Key Changes:
The code now uses github.com/containerd/cgroups/v2 instead of cgroup1.
It checks if cgroup v2 is supported using cgroups.Mode().
It creates a cgroup manager using cgroups.NewManager with the appropriate path and resources.
The Cleanup method ensures the cgroup is deleted when the sandbox is destroyed.
Option 3: Support Both Cgroup v1 and v2
If you need to support both cgroup v1 and v2 (e.g., for portability across different systems), you can write code that detects the cgroup version and uses the appropriate library. Here's an example:
go
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/containerd/cgroups"
"github.com/containerd/cgroups/v2"
cgroup1 "github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runtime-spec/specs-go"
)
// CgroupsSandbox represents a sandbox with cgroup isolation
type CgroupsSandbox struct {
workDir string
cgroupV1 cgroup1.Cgroup
cgroupV2 *cgroups.Manager
isCgroupV2 bool
}
// NewCgroupsSandbox creates a new sandbox with cgroup isolation
func NewCgroupsSandbox() (*CgroupsSandbox, error) {
// Create temporary work directory
workDir, err := os.MkdirTemp("", "cgsb_*")
if err != nil {
return nil, fmt.Errorf("failed to create work directory: %w", err)
}
// Define cgroup resources (example settings)
resources := &specs.LinuxResources{
CPU: &specs.LinuxCPU{
Quota: int64Ptr(100000), // 100ms CPU quota
Period: uint64Ptr(1000000), // 1s CPU period
},
Memory: &specs.LinuxMemory{
Limit: int64Ptr(100 * 1024 * 1024), // 100MB memory limit
},
}
// Create a unique cgroup name
cgroupName := fmt.Sprintf("sandbox_%d", os.Getpid())
// Check cgroup mode
switch cgroups.Mode() {
case cgroups.Unified: // Cgroup v2
cgroupPath := filepath.Join(cgroups.V2, cgroupName)
manager, err := cgroups.NewManager(cgroups.V2, cgroupPath, resources)
if err != nil {
os.RemoveAll(workDir)
return nil, fmt.Errorf("failed to create cgroup v2: %w", err)
}
return &CgroupsSandbox{
workDir: workDir,
cgroupV2: manager,
isCgroupV2: true,
}, nil
case cgroups.Legacy, cgroups.Hybrid: // Cgroup v1
control, err := cgroup1.New(cgroup1.StaticPath("/"+cgroupName), resources)
if err != nil {
os.RemoveAll(workDir)
return nil, fmt.Errorf("failed to create cgroup v1: %w", err)
}
return &CgroupsSandbox{
workDir: workDir,
cgroupV1: control,
isCgroupV2: false,
}, nil
default:
os.RemoveAll(workDir)
return nil, fmt.Errorf("unsupported cgroup mode")
}
}
// Cleanup removes the work directory and cgroup
func (s *CgroupsSandbox) Cleanup() error {
if s.isCgroupV2 {
if err := s.cgroupV2.Delete(); err != nil {
return fmt.Errorf("failed to delete cgroup v2: %w", err)
}
} else {
if err := s.cgroupV1.Delete(); err != nil {
return fmt.Errorf("failed to delete cgroup v1: %w", err)
}
}
if err := os.RemoveAll(s.workDir); err != nil {
return fmt.Errorf("failed to remove work directory: %w", err)
}
return nil
}
// Helper function to create int64 pointers
func int64Ptr(i int64) *int64 {
return &i
}
// Helper function to create uint64 pointers
func uint64Ptr(i uint64) *uint64 {
return &i
}
func main() {
sandbox, err := NewCgroupsSandbox()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
defer sandbox.Cleanup()
fmt.Println("Cgroup sandbox created successfully!")
}
Additional Notes
Permissions:
Creating and managing cgroups typically requires root privileges. Ensure your program runs with sufficient permissions (e.g., using sudo).
If running in a container, ensure the container has the necessary capabilities (e.g., CAP_SYS_ADMIN) and cgroup namespace access.
Testing:
Test your code on systems with both cgroup v1 and v2 to ensure compatibility.
Use tools like cgcreate, cgexec, and cgdelete (from the cgroup-tools package) to experiment with cgroups manually.
Dependencies:
Ensure you have the correct versions of the libraries (containerd/cgroups for v2, opencontainers/runc/libcontainer/cgroups for v1).
Modern Systems:
Cgroup v2 is the future of cgroups in Linux. If possible, design your application to use cgroup v2, as cgroup v1 is deprecated in many modern distributions.
Conclusion
The error you're encountering is due to a mismatch between the cgroup version your system uses and the library you're using. You can fix it by either enabling cgroup v1 on your system or updating your code to support cgroup v2 (or both). The provided code examples should help you implement a robust solution that works across different environments.