Skip to content

Commit

Permalink
enable specifying directory as device on container with --device
Browse files Browse the repository at this point in the history
Signed-off-by: Qi Wang <qiwan@redhat.com>
  • Loading branch information
QiWang19 committed Feb 25, 2019
1 parent 553ac80 commit c0227ac
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
24 changes: 19 additions & 5 deletions pkg/spec/config_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,33 @@ func devicesFromPath(g *generate.Generator, devicePath string) error {
return errors.Wrapf(err, "cannot stat %s", devicePath)
}
if st.IsDir() {
found := false
src := resolvedDevicePath
dest := src
var devmode string
if len(devs) > 1 {
if len(devs[1]) > 0 && devs[1][0] == '/' {
dest = devs[1]
} else {
devmode = devs[1]
}
}
if len(devs) > 2 {
return errors.Wrapf(unix.EINVAL, "not allowed to specify destination with a directory %s", devicePath)
if devmode != "" {
return errors.Errorf("invalid device specification %s", devicePath)
}
devmode = devs[2]
}
found := false

// mount the internal devices recursively
if err := filepath.Walk(resolvedDevicePath, func(dpath string, f os.FileInfo, e error) error {

if f.Mode()&os.ModeDevice == os.ModeDevice {
found = true
device := dpath

if len(devs) > 1 {
device = fmt.Sprintf("%s:%s", dpath, devs[1])
device = fmt.Sprintf("%s:%s", dpath, filepath.Join(dest, strings.TrimPrefix(dpath, src)))
if devmode != "" {
device = fmt.Sprintf("%s:%s", device, devmode)
}
if err := addDevice(g, device); err != nil {
return errors.Wrapf(err, "failed to add %s device", dpath)
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/run_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,12 @@ var _ = Describe("Podman run device", func() {
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Not(Equal(0)))
})

FIt("podman run device host device and container device parameter are directories", func() {
SystemExec("mkdir", []string{"/dev/foo"})
SystemExec("cp", []string{"/dev/null", "/dev/foo/null1"})
session := podmanTest.Podman([]string{"run", "-q", "--device", "/dev/foo:/dev/bar", ALPINE, "ls", "/dev/bar/null1"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
})
})

0 comments on commit c0227ac

Please sign in to comment.