Skip to content

Commit f7f0c3e

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 cfd7cc3 commit f7f0c3e

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
@@ -29,6 +29,7 @@ require (
2929
github.com/hashicorp/errwrap v1.1.0 // indirect
3030
github.com/kr/pretty v0.3.1 // indirect
3131
github.com/moby/sys/capability v0.4.0 // indirect
32+
github.com/opencontainers/cgroups v0.0.4 // indirect
3233
github.com/opencontainers/runtime-tools v0.9.1-0.20251114084447-edf4cb3d2116 // indirect
3334
github.com/pmezard/go-difflib v1.0.0 // indirect
3435
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
@@ -34,6 +34,8 @@ github.com/moby/sys/reexec v0.1.0 h1:RrBi8e0EBTLEgfruBOFcxtElzRGTEUkeIFaVXgU7wok
3434
github.com/moby/sys/reexec v0.1.0/go.mod h1:EqjBg8F3X7iZe5pU6nRZnYCMUTXoxsjiIfHup5wYIN8=
3535
github.com/moby/sys/symlink v0.3.0 h1:GZX89mEZ9u53f97npBy4Rc3vJKj7JBDj/PN2I22GrNU=
3636
github.com/moby/sys/symlink v0.3.0/go.mod h1:3eNdhduHmYPcgsJtZXW1W4XUJdZGBIkttZ8xKqPUJq0=
37+
github.com/opencontainers/cgroups v0.0.4 h1:XVj8P/IHVms/j+7eh8ggdkTLAxjz84ZzuFyGoE28DR4=
38+
github.com/opencontainers/cgroups v0.0.4/go.mod h1:s8lktyhlGUqM7OSRL5P7eAW6Wb+kWPNvt4qvVfzA5vs=
3739
github.com/opencontainers/runc v1.3.3 h1:qlmBbbhu+yY0QM7jqfuat7M1H3/iXjju3VkP9lkFQr4=
3840
github.com/opencontainers/runc v1.3.3/go.mod h1:D7rL72gfWxVs9cJ2/AayxB0Hlvn9g0gaF1R7uunumSI=
3941
github.com/opencontainers/runtime-spec v1.3.0 h1:YZupQUdctfhpZy3TM39nN9Ika5CBWT5diQ8ibYCRkxg=

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)