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

Support systemd optional prefix '-' for devices. #18362

Merged
merged 1 commit into from
Apr 27, 2023
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
3 changes: 2 additions & 1 deletion docs/source/markdown/podman-systemd.unit.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ Adds a device node from the host into the container. The format of this is
`HOST-DEVICE[:CONTAINER-DEVICE][:PERMISSIONS]`, where `HOST-DEVICE` is the path of
the device node on the host, `CONTAINER-DEVICE` is the path of the device node in
the container, and `PERMISSIONS` is a list of permissions combining 'r' for read,
'w' for write, and 'm' for mknod(2).
'w' for write, and 'm' for mknod(2). The `-` prefix tells quadlet to add the device
only if it exists on the host.

This key can be listed multiple times.

Expand Down
9 changes: 9 additions & 0 deletions pkg/systemd/quadlet/quadlet.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package quadlet

import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -421,6 +423,13 @@ func ConvertContainer(container *parser.UnitFile, isUser bool) (*parser.UnitFile
// But allow overrides with AddCapability
devices := container.LookupAllStrv(ContainerGroup, KeyAddDevice)
for _, device := range devices {
if device[0] == '-' {
device = device[1:]
_, err := os.Stat(strings.Split(device, ":")[0])
if errors.Is(err, os.ErrNotExist) {
continue
}
}
podman.addf("--device=%s", device)
}

Expand Down
7 changes: 7 additions & 0 deletions test/e2e/quadlet/devices.container
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
## assert-podman-args --device=/dev/fuse
## assert-podman-args --device=/dev/loop0:r
## assert-podman-args --device=/dev/null:/dev/test
## !assert-podman-args --device=/dev/bogus:r
## !assert-podman-args --device=/dev/bogus
## !assert-podman-args --device=/dev/bogus1

[Container]
Image=localhost/imagename
AddDevice=/dev/fuse
AddDevice=/dev/loop0:r
AddDevice=-/dev/null:/dev/test
AddDevice=-/dev/bogus:r
AddDevice=-/dev/bogus1