Skip to content

Commit 85adc3f

Browse files
committed
Extract device information from host path if possible
This change extracts the required information from the host device node if available. Signed-off-by: Evan Lezar <[email protected]>
1 parent 614e469 commit 85adc3f

File tree

9 files changed

+555
-7
lines changed

9 files changed

+555
-7
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ require (
2727
github.com/google/uuid v1.6.0 // indirect
2828
github.com/hashicorp/errwrap v1.1.0 // indirect
2929
github.com/kr/pretty v0.3.1 // indirect
30+
github.com/opencontainers/cgroups v0.0.1 // indirect
3031
github.com/opencontainers/runtime-tools v0.9.1-0.20221107090550-2e043c6bd626 // indirect
3132
github.com/pmezard/go-difflib v1.0.0 // indirect
3233
github.com/rogpeppe/go-internal v1.11.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ github.com/moby/sys/reexec v0.1.0/go.mod h1:EqjBg8F3X7iZe5pU6nRZnYCMUTXoxsjiIfHu
3737
github.com/moby/sys/symlink v0.3.0 h1:GZX89mEZ9u53f97npBy4Rc3vJKj7JBDj/PN2I22GrNU=
3838
github.com/moby/sys/symlink v0.3.0/go.mod h1:3eNdhduHmYPcgsJtZXW1W4XUJdZGBIkttZ8xKqPUJq0=
3939
github.com/mrunalp/fileutils v0.5.0/go.mod h1:M1WthSahJixYnrXQl/DFQuteStB1weuxD2QJNHXfbSQ=
40+
github.com/opencontainers/cgroups v0.0.1 h1:MXjMkkFpKv6kpuirUa4USFBas573sSAY082B4CiHEVA=
41+
github.com/opencontainers/cgroups v0.0.1/go.mod h1:s8lktyhlGUqM7OSRL5P7eAW6Wb+kWPNvt4qvVfzA5vs=
4042
github.com/opencontainers/runc v1.3.0 h1:cvP7xbEvD0QQAs0nZKLzkVog2OPZhI/V2w3WmTmUSXI=
4143
github.com/opencontainers/runc v1.3.0/go.mod h1:9wbWt42gV+KRxKRVVugNP6D5+PQciRbenB4fLVsqGPs=
4244
github.com/opencontainers/runtime-spec v1.0.3-0.20220825212826-86290f6a00fb/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=

internal/edits/device.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"tags.cncf.io/container-device-interface/pkg/cdi"
2121
"tags.cncf.io/container-device-interface/specs-go"
2222

23+
"github.com/opencontainers/runc/libcontainer/devices"
24+
2325
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
2426
)
2527

@@ -43,19 +45,37 @@ func (d device) toEdits() (*cdi.ContainerEdits, error) {
4345
// toSpec converts a discovered Device to a CDI Spec Device. Note
4446
// that missing info is filled in when edits are applied by querying the Device node.
4547
func (d device) toSpec() (*specs.DeviceNode, error) {
48+
s := d.fromPathOrDefault()
4649
// The HostPath field was added in the v0.5.0 CDI specification.
4750
// The cdi package uses strict unmarshalling when loading specs from file causing failures for
4851
// unexpected fields.
4952
// Since the behaviour for HostPath == "" and HostPath == Path are equivalent, we clear HostPath
5053
// if it is equal to Path to ensure compatibility with the widest range of specs.
51-
hostPath := d.HostPath
52-
if hostPath == d.Path {
53-
hostPath = ""
54+
if s.HostPath == d.Path {
55+
s.HostPath = ""
5456
}
55-
s := specs.DeviceNode{
56-
HostPath: hostPath,
57-
Path: d.Path,
57+
58+
return s, nil
59+
}
60+
61+
// fromPathOrDefault attempts to return the returns the information about the
62+
// CDI device from the specified host path.
63+
// If this fails a minimal device is returned so that this information can be
64+
// queried by the container runtime such as containerd.
65+
func (d device) fromPathOrDefault() *specs.DeviceNode {
66+
dn, err := devices.DeviceFromPath(d.HostPath, "rwm")
67+
if err != nil {
68+
return &specs.DeviceNode{
69+
HostPath: d.HostPath,
70+
Path: d.Path,
71+
}
5872
}
5973

60-
return &s, nil
74+
return &specs.DeviceNode{
75+
HostPath: d.HostPath,
76+
Path: d.Path,
77+
Major: dn.Major,
78+
Minor: dn.Minor,
79+
FileMode: &dn.FileMode,
80+
}
6181
}

vendor/github.com/opencontainers/cgroups/LICENSE

Lines changed: 201 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)