Skip to content

Commit baded62

Browse files
authored
Merge pull request #73 from elezar/add-host-path
Add HostPath to DeviceNode to allow for use cases where device nodes are not at root
2 parents 3dfdac4 + dc8524e commit baded62

File tree

7 files changed

+37
-22
lines changed

7 files changed

+37
-22
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ Remember to restart containerd for any configuration changes to take effect.
7171
$ mkdir /etc/cdi
7272
$ cat > /etc/cdi/vendor.json <<EOF
7373
{
74-
"cdiVersion": "0.4.0",
74+
"cdiVersion": "0.5.0",
7575
"kind": "vendor.com/device",
7676
"devices": [
7777
{
7878
"name": "myDevice",
7979
"containerEdits": {
8080
"deviceNodes": [
81-
{"path": "/dev/card1", "type": "c", "major": 25, "minor": 25, "fileMode": 384, "permissions": "rw", "uid": 1000, "gid": 1000},
81+
{"hostPath": "/vendor/dev/card1": "path": "/dev/card1", "type": "c", "major": 25, "minor": 25, "fileMode": 384, "permissions": "rw", "uid": 1000, "gid": 1000},
8282
{"path": "/dev/card-render1", "type": "c", "major": 25, "minor": 25, "fileMode": 384, "permissions": "rwm", "uid": 1000, "gid": 1000}
8383
]
8484
}

SPEC.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
## Version
1010

11-
This is CDI **spec** version **0.4.0**.
11+
This is CDI **spec** version **0.5.0**.
1212

1313
### Update policy
1414

@@ -25,6 +25,7 @@ Released versions of the spec are available as Git tags.
2525
| -----| -----------------| -------|
2626
| v0.3.0 | | Initial tagged release of Spec |
2727
| v0.4.0 | | Added `type` field to Mount specification |
28+
| v0.5.0 | | Add `HostPath` to `DeviceNodes` |
2829

2930
*Note*: The initial release of a **spec** with version `v0.x.0` will be tagged as
3031
`v0.x.0` with subsequent changes to the API applicable to this version tagged as `v0.x.y`.
@@ -79,7 +80,7 @@ The key words "must", "must not", "required", "shall", "shall not", "should", "s
7980

8081
```
8182
{
82-
"cdiVersion": "0.3.0",
83+
"cdiVersion": "0.5.0",
8384
"kind": "<name>",
8485
8586
"devices": [
@@ -103,6 +104,7 @@ The key words "must", "must not", "required", "shall", "shall not", "should", "s
103104
"deviceNodes": [ (optional)
104105
{
105106
"path": "<path>",
107+
"hostPath": "<hostPath>" (optional),
106108
"type": "<type>" (optional),
107109
"major": <int32> (optional),
108110
"minor": <int32> (optional),
@@ -181,6 +183,7 @@ The `containerEdits` field has the following definition:
181183
* `env` (array of strings in the format of "VARNAME=VARVALUE", OPTIONAL) describes the environment variables that should be set. These values are appended to the container environment array.
182184
* `deviceNodes` (array of objects, OPTIONAL) describes the device nodes that should be mounted:
183185
* `path` (string, REQUIRED) path of the device within the container.
186+
* `hostPath` (string, OPTIONAL) path of the device node on the host. If not specified the value for `path` is used.
184187
* `type` (string, OPTIONAL) Device type: block, char, etc.
185188
* `major` (int64, OPTIONAL) Device major number.
186189
* `minor` (int64, OPTIONAL) Device minor number.

pkg/cdi/container-edits.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,13 @@ func (e *ContainerEdits) Apply(spec *oci.Spec) error {
8585
}
8686

8787
for _, d := range e.DeviceNodes {
88-
dev := d.ToOCI()
89-
if err := fillMissingInfo(&dev); err != nil {
88+
dn := DeviceNode{d}
89+
90+
err := dn.fillMissingInfo()
91+
if err != nil {
9092
return err
9193
}
92-
94+
dev := d.ToOCI()
9395
if dev.UID == nil && spec.Process != nil {
9496
if uid := spec.Process.User.UID; uid > 0 {
9597
dev.UID = &uid
@@ -288,26 +290,31 @@ func ensureOCIHooks(spec *oci.Spec) {
288290
}
289291

290292
// fillMissingInfo fills in missing mandatory attributes from the host device.
291-
func fillMissingInfo(dev *oci.LinuxDevice) error {
292-
if dev.Type != "" && (dev.Major != 0 || dev.Type == "p") {
293+
func (d *DeviceNode) fillMissingInfo() error {
294+
if d.HostPath == "" {
295+
d.HostPath = d.Path
296+
}
297+
298+
if d.Type != "" && (d.Major != 0 || d.Type == "p") {
293299
return nil
294300
}
295-
hostDev, err := runc.DeviceFromPath(dev.Path, "rwm")
301+
302+
hostDev, err := runc.DeviceFromPath(d.HostPath, "rwm")
296303
if err != nil {
297-
return errors.Wrapf(err, "failed to stat CDI host device %q", dev.Path)
304+
return errors.Wrapf(err, "failed to stat CDI host device %q", d.HostPath)
298305
}
299306

300-
if dev.Type == "" {
301-
dev.Type = string(hostDev.Type)
307+
if d.Type == "" {
308+
d.Type = string(hostDev.Type)
302309
} else {
303-
if dev.Type != string(hostDev.Type) {
304-
return errors.Errorf("CDI device %q, host type mismatch (%s, %s)",
305-
dev.Path, dev.Type, string(hostDev.Type))
310+
if d.Type != string(hostDev.Type) {
311+
return errors.Errorf("CDI device (%q, %q), host type mismatch (%s, %s)",
312+
d.Path, d.HostPath, d.Type, string(hostDev.Type))
306313
}
307314
}
308-
if dev.Major == 0 && dev.Type != "p" {
309-
dev.Major = hostDev.Major
310-
dev.Minor = hostDev.Minor
315+
if d.Major == 0 && d.Type != "p" {
316+
d.Major = hostDev.Major
317+
d.Minor = hostDev.Minor
311318
}
312319

313320
return nil

pkg/cdi/spec.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var (
3636
"0.2.0": {},
3737
"0.3.0": {},
3838
"0.4.0": {},
39+
"0.5.0": {},
3940
}
4041

4142
// Externally set CDI Spec validation function.

schema/defs.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
"path": {
3030
"$ref": "#/definitions/FilePath"
3131
},
32+
"hostPath": {
33+
"$ref": "#/definitions/FilePath"
34+
},
3235
"permissions": {
3336
"type": "string"
3437
},

schema/testdata/good/spec-example.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
2-
"cdiVersion": "0.4.0",
2+
"cdiVersion": "0.5.0",
33
"kind": "vendor.com/device",
44
"devices": [
55
{
66
"name": "myDevice",
77
"containerEdits": {
88
"deviceNodes": [
9-
{"path": "/dev/card1"},
9+
{"hostPath": "/vendorroot/dev/card1", "path": "/dev/card1"},
1010
{"path": "/dev/card2"}
1111
]
1212
}

specs-go/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package specs
33
import "os"
44

55
// CurrentVersion is the current version of the Spec.
6-
const CurrentVersion = "0.4.0"
6+
const CurrentVersion = "0.5.0"
77

88
// Spec is the base configuration for CDI
99
type Spec struct {
@@ -31,6 +31,7 @@ type ContainerEdits struct {
3131
// DeviceNode represents a device node that needs to be added to the OCI spec.
3232
type DeviceNode struct {
3333
Path string `json:"path"`
34+
HostPath string `json:"hostPath,omitempty"`
3435
Type string `json:"type,omitempty"`
3536
Major int64 `json:"major,omitempty"`
3637
Minor int64 `json:"minor,omitempty"`

0 commit comments

Comments
 (0)