libct: decouple libct/cg/devices

Commit b6967fa84c moved the functionality of managing cgroup devices
into a separate package, and decoupled libcontainer/cgroups from it.

Yet, some software (e.g. cadvisor) may need to use libcontainer package,
which imports libcontainer/cgroups/devices, thus making it impossible to
use libcontainer without bringing in cgroup/devices dependency.

In fact, we only need to manage devices in runc binary, so move the
import to main.go.

The need to import libct/cg/dev in order to manage devices is already
documented in libcontainer/cgroups, but let's
 - update that documentation;
 - add a similar note to libcontainer/cgroups/systemd;
 - add a note to libct README.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin
2024-04-17 14:16:35 -07:00
parent 6a2813f16a
commit 4f3319b56d
6 changed files with 37 additions and 7 deletions
+5
View File
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
### Changed
* libcontainer/cgroups users who want to manage cgroup devices need to explicitly
import libcontainer/cgroups/devices. (#3452, #4248)
## [1.2.0-rc.1] - 2024-04-03 ## [1.2.0-rc.1] - 2024-04-03
> There's a frood who really knows where his towel is. > There's a frood who really knows where his towel is.
+23 -4
View File
@@ -8,11 +8,13 @@ It allows you to manage the lifecycle of the container performing additional ope
after the container is created. after the container is created.
#### Container ## Container
A container is a self contained execution environment that shares the kernel of the A container is a self contained execution environment that shares the kernel of the
host system and which is (optionally) isolated from other containers in the system. host system and which is (optionally) isolated from other containers in the system.
#### Using libcontainer ## Using libcontainer
### Container init
Because containers are spawned in a two step process you will need a binary that Because containers are spawned in a two step process you will need a binary that
will be executed as the init process for the container. In libcontainer, we use will be executed as the init process for the container. In libcontainer, we use
@@ -27,7 +29,24 @@ For details on how runc implements such "init", see
[init.go](https://github.com/opencontainers/runc/blob/master/init.go) [init.go](https://github.com/opencontainers/runc/blob/master/init.go)
and [libcontainer/init_linux.go](https://github.com/opencontainers/runc/blob/master/libcontainer/init_linux.go). and [libcontainer/init_linux.go](https://github.com/opencontainers/runc/blob/master/libcontainer/init_linux.go).
Then to create a container you first have to create a configuration ### Device management
If you want containers that have access to some devices, you need to import
this package into your code:
```go
import (
_ "github.com/opencontainers/runc/libcontainer/cgroups/devices"
)
```
Without doing this, libcontainer cgroup manager won't be able to set up device
access rules, and will fail if devices are specified in the container
configuration.
### Container creation
To create a container you first have to create a configuration
struct describing how the container is to be created. A sample would look similar to this: struct describing how the container is to be created. A sample would look similar to this:
```go ```go
@@ -274,7 +293,7 @@ state, err := container.State()
``` ```
#### Checkpoint & Restore ## Checkpoint & Restore
libcontainer now integrates [CRIU](http://criu.org/) for checkpointing and restoring containers. libcontainer now integrates [CRIU](http://criu.org/) for checkpointing and restoring containers.
This lets you save the state of a process running inside a container to disk, and then restore This lets you save the state of a process running inside a container to disk, and then restore
+2 -1
View File
@@ -12,7 +12,8 @@ var (
ErrDevicesUnsupported = errors.New("cgroup manager is not configured to set device rules") ErrDevicesUnsupported = errors.New("cgroup manager is not configured to set device rules")
// DevicesSetV1 and DevicesSetV2 are functions to set devices for // DevicesSetV1 and DevicesSetV2 are functions to set devices for
// cgroup v1 and v2, respectively. Unless libcontainer/cgroups/devices // cgroup v1 and v2, respectively. Unless
// [github.com/opencontainers/runc/libcontainer/cgroups/devices]
// package is imported, it is set to nil, so cgroup managers can't // package is imported, it is set to nil, so cgroup managers can't
// manage devices. // manage devices.
DevicesSetV1 func(path string, r *configs.Resources) error DevicesSetV1 func(path string, r *configs.Resources) error
+5
View File
@@ -33,6 +33,11 @@ var (
isRunningSystemdOnce sync.Once isRunningSystemdOnce sync.Once
isRunningSystemd bool isRunningSystemd bool
// GenerateDeviceProps is a function to generate systemd device
// properties, used by Set methods. Unless
// [github.com/opencontainers/runc/libcontainer/cgroups/devices]
// package is imported, it is set to nil, so cgroup managers can't
// configure devices.
GenerateDeviceProps func(r *configs.Resources, sdVer int) ([]systemdDbus.Property, error) GenerateDeviceProps func(r *configs.Resources, sdVer int) ([]systemdDbus.Property, error)
) )
-2
View File
@@ -9,8 +9,6 @@ import (
securejoin "github.com/cyphar/filepath-securejoin" securejoin "github.com/cyphar/filepath-securejoin"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
//nolint:revive // Enable cgroup manager to manage devices
_ "github.com/opencontainers/runc/libcontainer/cgroups/devices"
"github.com/opencontainers/runc/libcontainer/cgroups/manager" "github.com/opencontainers/runc/libcontainer/cgroups/manager"
"github.com/opencontainers/runc/libcontainer/configs" "github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/configs/validate" "github.com/opencontainers/runc/libcontainer/configs/validate"
+2
View File
@@ -10,6 +10,8 @@ import (
"strconv" "strconv"
"strings" "strings"
//nolint:revive // Enable cgroup manager to manage devices
_ "github.com/opencontainers/runc/libcontainer/cgroups/devices"
"github.com/opencontainers/runc/libcontainer/seccomp" "github.com/opencontainers/runc/libcontainer/seccomp"
"github.com/opencontainers/runtime-spec/specs-go" "github.com/opencontainers/runtime-spec/specs-go"