Skip to content

Commit 4f3319b

Browse files
committed
libct: decouple libct/cg/devices
Commit b6967fa 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 <[email protected]>
1 parent 6a2813f commit 4f3319b

File tree

6 files changed

+37
-7
lines changed

6 files changed

+37
-7
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
### Changed
10+
11+
* libcontainer/cgroups users who want to manage cgroup devices need to explicitly
12+
import libcontainer/cgroups/devices. (#3452, #4248)
13+
914
## [1.2.0-rc.1] - 2024-04-03
1015

1116
> There's a frood who really knows where his towel is.

libcontainer/README.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ It allows you to manage the lifecycle of the container performing additional ope
88
after the container is created.
99

1010

11-
#### Container
11+
## Container
1212
A container is a self contained execution environment that shares the kernel of the
1313
host system and which is (optionally) isolated from other containers in the system.
1414

15-
#### Using libcontainer
15+
## Using libcontainer
16+
17+
### Container init
1618

1719
Because containers are spawned in a two step process you will need a binary that
1820
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
2729
[init.go](https://github.com/opencontainers/runc/blob/master/init.go)
2830
and [libcontainer/init_linux.go](https://github.com/opencontainers/runc/blob/master/libcontainer/init_linux.go).
2931

30-
Then to create a container you first have to create a configuration
32+
### Device management
33+
34+
If you want containers that have access to some devices, you need to import
35+
this package into your code:
36+
37+
```go
38+
import (
39+
_ "github.com/opencontainers/runc/libcontainer/cgroups/devices"
40+
)
41+
```
42+
43+
Without doing this, libcontainer cgroup manager won't be able to set up device
44+
access rules, and will fail if devices are specified in the container
45+
configuration.
46+
47+
### Container creation
48+
49+
To create a container you first have to create a configuration
3150
struct describing how the container is to be created. A sample would look similar to this:
3251

3352
```go
@@ -274,7 +293,7 @@ state, err := container.State()
274293
```
275294

276295

277-
#### Checkpoint & Restore
296+
## Checkpoint & Restore
278297

279298
libcontainer now integrates [CRIU](http://criu.org/) for checkpointing and restoring containers.
280299
This lets you save the state of a process running inside a container to disk, and then restore

libcontainer/cgroups/cgroups.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ var (
1212
ErrDevicesUnsupported = errors.New("cgroup manager is not configured to set device rules")
1313

1414
// DevicesSetV1 and DevicesSetV2 are functions to set devices for
15-
// cgroup v1 and v2, respectively. Unless libcontainer/cgroups/devices
15+
// cgroup v1 and v2, respectively. Unless
16+
// [github.com/opencontainers/runc/libcontainer/cgroups/devices]
1617
// package is imported, it is set to nil, so cgroup managers can't
1718
// manage devices.
1819
DevicesSetV1 func(path string, r *configs.Resources) error

libcontainer/cgroups/systemd/common.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ var (
3333
isRunningSystemdOnce sync.Once
3434
isRunningSystemd bool
3535

36+
// GenerateDeviceProps is a function to generate systemd device
37+
// properties, used by Set methods. Unless
38+
// [github.com/opencontainers/runc/libcontainer/cgroups/devices]
39+
// package is imported, it is set to nil, so cgroup managers can't
40+
// configure devices.
3641
GenerateDeviceProps func(r *configs.Resources, sdVer int) ([]systemdDbus.Property, error)
3742
)
3843

libcontainer/factory_linux.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99
securejoin "github.com/cyphar/filepath-securejoin"
1010
"golang.org/x/sys/unix"
1111

12-
//nolint:revive // Enable cgroup manager to manage devices
13-
_ "github.com/opencontainers/runc/libcontainer/cgroups/devices"
1412
"github.com/opencontainers/runc/libcontainer/cgroups/manager"
1513
"github.com/opencontainers/runc/libcontainer/configs"
1614
"github.com/opencontainers/runc/libcontainer/configs/validate"

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"strconv"
1111
"strings"
1212

13+
//nolint:revive // Enable cgroup manager to manage devices
14+
_ "github.com/opencontainers/runc/libcontainer/cgroups/devices"
1315
"github.com/opencontainers/runc/libcontainer/seccomp"
1416
"github.com/opencontainers/runtime-spec/specs-go"
1517

0 commit comments

Comments
 (0)