Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libct: decouple libct/cg/devices #4248

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [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

> There's a frood who really knows where his towel is.
Expand Down
27 changes: 23 additions & 4 deletions libcontainer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ It allows you to manage the lifecycle of the container performing additional ope
after the container is created.


#### Container
## Container
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.

#### Using libcontainer
## Using libcontainer

### Container init

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
Expand All @@ -27,7 +29,24 @@ For details on how runc implements such "init", see
[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).

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:

```go
Expand Down Expand Up @@ -274,7 +293,7 @@ state, err := container.State()
```


#### Checkpoint & Restore
## Checkpoint & Restore

libcontainer now integrates [CRIU](https://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
Expand Down
3 changes: 2 additions & 1 deletion libcontainer/cgroups/cgroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ var (
ErrDevicesUnsupported = errors.New("cgroup manager is not configured to set device rules")

// 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
// manage devices.
DevicesSetV1 func(path string, r *configs.Resources) error
Expand Down
5 changes: 5 additions & 0 deletions libcontainer/cgroups/systemd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ var (
isRunningSystemdOnce sync.Once
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)
)

Expand Down
2 changes: 0 additions & 2 deletions libcontainer/factory_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
securejoin "github.com/cyphar/filepath-securejoin"
"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/configs"
"github.com/opencontainers/runc/libcontainer/configs/validate"
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strconv"
"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/runtime-spec/specs-go"

Expand Down
Loading