Skip to content

Commit

Permalink
Enable --device directory as src device
Browse files Browse the repository at this point in the history
Enables --device accepte directory path as source device. Add the devices under the source directory to the destination directory.

complete card test criteria: https://jira.coreos.com/browse/RUN-497
related podman issue: containers/podman#2380

Signed-off-by: Qi Wang <[email protected]>
  • Loading branch information
QiWang19 committed Oct 23, 2019
1 parent 049fdf4 commit 4db2a49
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cmd/buildah/bud.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func budCmd(c *cobra.Command, inputArgs []string, iopts budResults) error {
if err != nil {
return err
}
devices = append(devices, dev)
devices = append(devices, dev...)
}

options := imagebuildah.BuildOptions{
Expand Down
2 changes: 1 addition & 1 deletion cmd/buildah/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func fromCmd(c *cobra.Command, args []string, iopts fromReply) error {
if err != nil {
return err
}
devices = append(devices, dev)
devices = append(devices, dev...)
}

options := buildah.BuilderOptions{
Expand Down
2 changes: 1 addition & 1 deletion docs/buildah-bud.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ value can be entered. The password is entered without echo.

**--device**=*device*

Add a host device to the container. The format is `<device-on-host>[:<device-on-container>][:<permissions>]` (e.g. --device=/dev/sdc:/dev/xvdc:rwm)
Add a host device or devices under a directory to the container. The format is `<device-on-host>[:<device-on-container>][:<permissions>]` (e.g. --device=/dev/sdc:/dev/xvdc:rwm)

**--disable-compression, -D**
Don't compress filesystem layers when building the image unless it is required
Expand Down
2 changes: 1 addition & 1 deletion docs/buildah-from.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ value can be entered. The password is entered without echo.

**--device**=*device*

Add a host device to the container. The format is `<device-on-host>[:<device-on-container>][:<permissions>]` (e.g. --device=/dev/sdc:/dev/xvdc:rwm)
Add a host device or devices under a directory to the container. The format is `<device-on-host>[:<device-on-container>][:<permissions>]` (e.g. --device=/dev/sdc:/dev/xvdc:rwm)

**--dns**=[]

Expand Down
17 changes: 11 additions & 6 deletions pkg/parse/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,22 @@ func TestDeviceFromPath(t *testing.T) {
// Path is valid
dev, err := DeviceFromPath("/dev/null")
assert.NoError(t, err)
assert.Equal(t, dev.Major, int64(1))
assert.Equal(t, dev.Minor, int64(3))
assert.Equal(t, dev.Permissions, "rwm")
assert.Equal(t, dev.Uid, uint32(0))
assert.Equal(t, dev.Gid, uint32(0))
assert.Equal(t, len(dev), 1)
assert.Equal(t, dev[0].Major, int64(1))
assert.Equal(t, dev[0].Minor, int64(3))
assert.Equal(t, dev[0].Permissions, "rwm")
assert.Equal(t, dev[0].Uid, uint32(0))
assert.Equal(t, dev[0].Gid, uint32(0))

// Path does not exists
_, err = DeviceFromPath("/dev/BOGUS")
assert.Error(t, err)

// Path exists but is not a device
// Path is a directory of devices
_, err = DeviceFromPath("/dev/pts")
assert.NoError(t, err)

// path of directory has no device
_, err = DeviceFromPath("/etc/passwd")
assert.Error(t, err)
}
38 changes: 31 additions & 7 deletions pkg/parse/parse_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package parse

import (
"fmt"
"os"
"path/filepath"

"github.com/containers/buildah/pkg/unshare"
"github.com/opencontainers/runc/libcontainer/configs"
Expand All @@ -24,18 +26,40 @@ func getDefaultProcessLimits() []string {
return defaultLimits
}

func DeviceFromPath(device string) (configs.Device, error) {
func DeviceFromPath(device string) ([]configs.Device, error) {
var devs []configs.Device
src, dst, permissions, err := Device(device)
if err != nil {
return configs.Device{}, err
return nil, err
}
if unshare.IsRootless() {
return configs.Device{}, errors.Errorf("Renaming device %s to %s is not a supported in rootless containers", src, dst)
return nil, errors.Errorf("Renaming device %s to %s is not a supported in rootless containers", src, dst)
}
dev, err := devices.DeviceFromPath(src, permissions)
srcInfo, err := os.Stat(src)
if err != nil {
return configs.Device{}, errors.Wrapf(err, "%s is not a valid device", src)
return nil, errors.Wrapf(err, "error getting info of source device %s", src)
}
dev.Path = dst
return *dev, nil

if !srcInfo.IsDir() {

dev, err := devices.DeviceFromPath(src, permissions)
if err != nil {
return nil, errors.Wrapf(err, "%s is not a valid device", src)
}
dev.Path = dst
devs = append(devs, *dev)
return devs, nil
}

// If source device is a directory
srcDevices, err := devices.GetDevices(src)
if err != nil {
return nil, errors.Wrapf(err, "error getting source devices from directory %s", src)
}
for _, d := range srcDevices {
d.Path = filepath.Join(dst, filepath.Base(d.Path))
d.Permissions = permissions
devs = append(devs, *d)
}
return devs, nil
}
14 changes: 13 additions & 1 deletion tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1690,6 +1690,18 @@ load helpers
run_buildah bud --signature-policy ${TESTSDIR}/policy.json --squash ${TESTSDIR}/bud/layers-squash
}

@test "bud with additional directory of devices" {
target=alpine-image
mkdir ${TESTSDIR}/foo
mknod ${TESTSDIR}/foo/null c 1 3
run_buildah bud --signature-policy ${TESTSDIR}/policy.json --device ${TESTSDIR}/foo:/dev/fuse -t ${target} -f ${TESTSDIR}/bud/device/Dockerfile ${TESTSDIR}/bud/device
[ "${status}" -eq 0 ]
expect_output --substring "null"

buildah rmi ${target}
rm -rf ${TESTSDIR}/foo
}

@test "bud with additional device" {
target=alpine-image
run_buildah bud --signature-policy ${TESTSDIR}/policy.json --device /dev/fuse -t ${target} -f ${TESTSDIR}/bud/device/Dockerfile ${TESTSDIR}/bud/device
Expand Down Expand Up @@ -1793,7 +1805,7 @@ load helpers
echo "$output"
expect_output --substring "abc.txt"

rm ${TESTSDIR}/bud/use-args/abc.txt
rm ${TESTSDIR}/bud/use-args/abc.txt
buildah rm --all
buildah rmi --all --force
}

0 comments on commit 4db2a49

Please sign in to comment.