diff --git a/config.json b/config.json index 1c5e925..ad8d469 100644 --- a/config.json +++ b/config.json @@ -20,6 +20,12 @@ "Value": "0" } ], + "Interface": { + "Socket": "dvd.sock", + "Types": [ + "docker.volumedriver/1.0" + ] + }, "Linux": { "Capabilities": [ "CAP_BPF", diff --git a/main.go b/main.go index 411b48b..e2abb2d 100644 --- a/main.go +++ b/main.go @@ -8,21 +8,28 @@ import ( "context" "device-volume-driver/internal/cgroup" "fmt" + "log" + "os" + "os/user" + "path" + "path/filepath" + "strconv" + "strings" + "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/client" + "github.com/docker/go-plugins-helpers/volume" _ "github.com/opencontainers/runtime-spec/specs-go" "golang.org/x/sys/unix" - "log" - "os" - "path" - "path/filepath" - "strings" ) const pluginId = "dvd" const rootPath = "/host" +type fakeDriver struct { +} + func Ptr[T any](v T) *T { return &v } @@ -62,6 +69,14 @@ func getDeviceInfo(devicePath string) (string, int64, int64, error) { func listenForMounts() { ctx := context.Background() + d := &fakeDriver{} + h := volume.NewHandler(d) + u, _ := user.Lookup("root") + gid, _ := strconv.Atoi(u.Gid) + if err := h.ServeUnix("/run/docker/plugins/dvd.sock", gid); err != nil { + log.Fatal(err) + } + cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation()) if err != nil { @@ -178,3 +193,36 @@ func applyDeviceRules(api cgroup.Interface, mountPath string, cgroupPath string, return nil } + +func (d *fakeDriver) Capabilities() *volume.CapabilitiesResponse { + return &volume.CapabilitiesResponse{Capabilities: volume.Capability{Scope: "local"}} +} + +func (d *fakeDriver) Create(r *volume.CreateRequest) error { + return fmt.Error("This driver device-mapping-manager cannot create volumes.") +} + +func (d *fakeDriver) List() (*volume.ListResponse, error) { + var vols []*volume.Volume + return &volume.ListResponse{Volumes: vols}, nil +} + +func (d *fakeDriver) Get(r *volume.GetRequest) (*volume.GetResponse, error) { + return &volume.GetResponse{}, fmt.Errorf("volume %s does not exist", r.Name) +} + +func (d *fakeDriver) Remove(r *volume.RemoveRequest) error { + return fmt.Errorf("volume %s does not exist", r.Name) +} + +func (d *fakeDriver) Path(r *volume.PathRequest) (*volume.PathResponse, error) { + return &volume.PathResponse{}, fmt.Errorf("volume %s does not exist", r.Name) +} + +func (d *fakeDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error) { + return &volume.MountResponse{}, fmt.Errorf("volume %s does not exist", r.Name) +} + +func (d *fakeDriver) Unmount(r *volume.UnmountRequest) error { + return fmt.Errorf("volume %s does not exist", r.Name) +}