Skip to content
This repository has been archived by the owner on Jan 22, 2024. It is now read-only.

Add support for cross-device volumes #48

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion tools/src/nvidia-docker-plugin/plugin_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"log"
"net/http"
"nvidia"
"path"
"regexp"
)
Expand Down Expand Up @@ -53,7 +54,7 @@ func (p *pluginVolume) create(resp http.ResponseWriter, req *http.Request) {
ok, err := v.Exists()
assert(err)
if !ok {
assert(v.Create())
assert(v.Create(nvidia.LinkStrategy{}))
}
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion tools/src/nvidia-docker/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func CreateLocalVolumes() error {
docker.RemoveVolume(n)
return err
}
if err := v.CreateAt(path); err != nil {
if err := v.CreateAt(path, nvidia.LinkOrCopyStrategy{}); err != nil {
docker.RemoveVolume(n)
return err
}
Expand Down
Binary file added tools/src/nvidia/.volumes.go.swp
Binary file not shown.
59 changes: 55 additions & 4 deletions tools/src/nvidia/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,57 @@ type Volume struct {

type VolumeMap map[string]*Volume

type FileCloneStrategy interface {
Clone(src, dst string) error
}

type LinkStrategy struct {}

func (s LinkStrategy) Clone(src, dst string) error {
return os.Link(src, dst)
}

type LinkOrCopyStrategy struct {}

func (s LinkOrCopyStrategy) Clone(src, dst string) error {
// Prefer hard link, fallback to copy
err := os.Link(src, dst)
if err != nil {
err = Copy(src, dst)
}
return err
}

func Copy(src, dst string) error {
s, err := os.Open(src)
if err != nil {
return err
}
defer s.Close()

fi, err := s.Stat()
if err != nil {
return err
}

d, err := os.Create(dst)
if err != nil {
return err
}

if _, err := io.Copy(d, s); err != nil {
d.Close()
return err
}

if err := d.Chmod(fi.Mode()); err != nil {
d.Close()
return err
}

return d.Close()
}

var Volumes = []VolumeInfo{
{
"nvidia_driver",
Expand Down Expand Up @@ -139,13 +190,13 @@ func blacklisted(file string, obj *elf.File) (bool, error) {
return false, nil
}

func (v *Volume) CreateAt(path string) error {
func (v *Volume) CreateAt(path string, s FileCloneStrategy) error {
v.Path = path
v.Version = ""
return v.Create()
return v.Create(s)
}

func (v *Volume) Create() (err error) {
func (v *Volume) Create(s FileCloneStrategy) (err error) {
if err = os.MkdirAll(v.Path, 0755); err != nil {
return
}
Expand Down Expand Up @@ -176,7 +227,7 @@ func (v *Volume) Create() (err error) {
}

l := path.Join(dir, path.Base(f))
if err := os.Link(f, l); err != nil {
if err := s.Clone(f, l); err != nil {
return err
}
soname, err := obj.DynString(elf.DT_SONAME)
Expand Down