Skip to content

Commit 037252b

Browse files
tonistiigicrazy-max
authored andcommitted
contrib: add setup definition for virtio-venus device
Signed-off-by: Tonis Tiigi <[email protected]>
1 parent cdcb1f1 commit 037252b

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

cmd/buildkitd/devices_venus.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//go:build venus
2+
// +build venus
3+
4+
package main
5+
6+
import (
7+
_ "github.com/moby/buildkit/contrib/cdisetup/venus"
8+
)

contrib/cdisetup/venus/venus.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package venus
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"os"
7+
"strings"
8+
9+
"github.com/moby/buildkit/solver/llbsolver/cdidevices"
10+
"github.com/pkg/errors"
11+
"golang.org/x/sys/unix"
12+
)
13+
14+
const (
15+
cdiKind = "docker.com/gpu"
16+
)
17+
18+
func init() {
19+
cdidevices.Register(cdiKind, &setup{})
20+
}
21+
22+
type setup struct {
23+
}
24+
25+
var _ cdidevices.Setup = &setup{}
26+
27+
func (s *setup) Validate() error {
28+
kVersion, err := getKernelVersion()
29+
if err != nil {
30+
return errors.Wrap(err, "failed to get kernel version")
31+
}
32+
if !strings.Contains(kVersion, "linuxkit") {
33+
return errors.Errorf("%s currently requires a linuxkit kernel", cdiKind)
34+
}
35+
36+
_, err = os.Stat("/dev/dri")
37+
if err != nil {
38+
if os.IsNotExist(err) {
39+
return errors.Errorf("no DRI device found, make you use Docker VMM Hypervisor")
40+
}
41+
return errors.Wrap(err, "failed to check DRI device")
42+
}
43+
44+
for _, dev := range []string{"renderD128", "card0"} {
45+
if _, err := os.Stat("/dev/dri/" + dev); err != nil {
46+
return errors.Wrapf(err, "failed to check DRI device %s", dev)
47+
}
48+
}
49+
return nil
50+
}
51+
52+
func (s *setup) Run(ctx context.Context) error {
53+
if err := s.Validate(); err != nil {
54+
return err
55+
}
56+
57+
const dt = `cdiVersion: "0.6.0"
58+
kind: "docker.com/gpu"
59+
annotations:
60+
cdi.device.name: "Virtio-GPU Venus (Docker Desktop)"
61+
devices:
62+
- name: venus
63+
containerEdits:
64+
deviceNodes:
65+
- path: /dev/dri/card0
66+
- path: /dev/dri/renderD128
67+
`
68+
69+
if err := os.MkdirAll("/etc/cdi", 0700); err != nil {
70+
return errors.Wrap(err, "failed to create /etc/cdi")
71+
}
72+
73+
if err := os.WriteFile("/etc/cdi/venus.yaml", []byte(dt), 0600); err != nil {
74+
return errors.Wrap(err, "failed to write /etc/cdi/venus.yaml")
75+
}
76+
77+
return nil
78+
}
79+
80+
func getKernelVersion() (string, error) {
81+
var uts unix.Utsname
82+
if err := unix.Uname(&uts); err != nil {
83+
return "", err
84+
}
85+
return string(uts.Release[:bytes.IndexByte(uts.Release[:], 0)]), nil
86+
}

0 commit comments

Comments
 (0)