Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable --device directory as src device #1937

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
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should add another test for a file

_, err = DeviceFromPath("/etc/passwd") 	
    assert.Error(t, err)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test added


// 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
}
17 changes: 16 additions & 1 deletion tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1690,6 +1690,21 @@ load helpers
run_buildah bud --signature-policy ${TESTSDIR}/policy.json --squash ${TESTSDIR}/bud/layers-squash
}

@test "bud with additional directory of devices" {
if test "$BUILDAH_ISOLATION" = "chroot" -o "$BUILDAH_ISOLATION" = "rootless" ; then
skip "BUILDAH_ISOLATION = $BUILDAH_ISOLATION"
fi
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 +1808,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
}
Expand Down