Skip to content

Commit 8195b03

Browse files
QiWang19rh-atomic-bot
authored andcommitted
Enable --device directory as src device
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]> Closes: #1937 Approved by: rhatdan
1 parent fa4eec7 commit 8195b03

File tree

7 files changed

+59
-18
lines changed

7 files changed

+59
-18
lines changed

cmd/buildah/bud.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func budCmd(c *cobra.Command, inputArgs []string, iopts budResults) error {
273273
if err != nil {
274274
return err
275275
}
276-
devices = append(devices, dev)
276+
devices = append(devices, dev...)
277277
}
278278

279279
options := imagebuildah.BuildOptions{

cmd/buildah/from.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ func fromCmd(c *cobra.Command, args []string, iopts fromReply) error {
218218
if err != nil {
219219
return err
220220
}
221-
devices = append(devices, dev)
221+
devices = append(devices, dev...)
222222
}
223223

224224
options := buildah.BuilderOptions{

docs/buildah-bud.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ value can be entered. The password is entered without echo.
176176

177177
**--device**=*device*
178178

179-
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)
179+
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)
180180

181181
**--disable-compression, -D**
182182
Don't compress filesystem layers when building the image unless it is required

docs/buildah-from.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ value can be entered. The password is entered without echo.
173173

174174
**--device**=*device*
175175

176-
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)
176+
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)
177177

178178
**--dns**=[]
179179

pkg/parse/parse_test.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,22 @@ func TestDeviceFromPath(t *testing.T) {
7474
// Path is valid
7575
dev, err := DeviceFromPath("/dev/null")
7676
assert.NoError(t, err)
77-
assert.Equal(t, dev.Major, int64(1))
78-
assert.Equal(t, dev.Minor, int64(3))
79-
assert.Equal(t, dev.Permissions, "rwm")
80-
assert.Equal(t, dev.Uid, uint32(0))
81-
assert.Equal(t, dev.Gid, uint32(0))
77+
assert.Equal(t, len(dev), 1)
78+
assert.Equal(t, dev[0].Major, int64(1))
79+
assert.Equal(t, dev[0].Minor, int64(3))
80+
assert.Equal(t, dev[0].Permissions, "rwm")
81+
assert.Equal(t, dev[0].Uid, uint32(0))
82+
assert.Equal(t, dev[0].Gid, uint32(0))
8283

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

87-
// Path exists but is not a device
88+
// Path is a directory of devices
8889
_, err = DeviceFromPath("/dev/pts")
90+
assert.NoError(t, err)
91+
92+
// path of directory has no device
93+
_, err = DeviceFromPath("/etc/passwd")
8994
assert.Error(t, err)
9095
}

pkg/parse/parse_unix.go

+31-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ package parse
44

55
import (
66
"fmt"
7+
"os"
8+
"path/filepath"
79

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

27-
func DeviceFromPath(device string) (configs.Device, error) {
29+
func DeviceFromPath(device string) ([]configs.Device, error) {
30+
var devs []configs.Device
2831
src, dst, permissions, err := Device(device)
2932
if err != nil {
30-
return configs.Device{}, err
33+
return nil, err
3134
}
3235
if unshare.IsRootless() {
33-
return configs.Device{}, errors.Errorf("Renaming device %s to %s is not a supported in rootless containers", src, dst)
36+
return nil, errors.Errorf("Renaming device %s to %s is not a supported in rootless containers", src, dst)
3437
}
35-
dev, err := devices.DeviceFromPath(src, permissions)
38+
srcInfo, err := os.Stat(src)
3639
if err != nil {
37-
return configs.Device{}, errors.Wrapf(err, "%s is not a valid device", src)
40+
return nil, errors.Wrapf(err, "error getting info of source device %s", src)
3841
}
39-
dev.Path = dst
40-
return *dev, nil
42+
43+
if !srcInfo.IsDir() {
44+
45+
dev, err := devices.DeviceFromPath(src, permissions)
46+
if err != nil {
47+
return nil, errors.Wrapf(err, "%s is not a valid device", src)
48+
}
49+
dev.Path = dst
50+
devs = append(devs, *dev)
51+
return devs, nil
52+
}
53+
54+
// If source device is a directory
55+
srcDevices, err := devices.GetDevices(src)
56+
if err != nil {
57+
return nil, errors.Wrapf(err, "error getting source devices from directory %s", src)
58+
}
59+
for _, d := range srcDevices {
60+
d.Path = filepath.Join(dst, filepath.Base(d.Path))
61+
d.Permissions = permissions
62+
devs = append(devs, *d)
63+
}
64+
return devs, nil
4165
}

tests/bud.bats

+13-1
Original file line numberDiff line numberDiff line change
@@ -1690,6 +1690,18 @@ load helpers
16901690
run_buildah bud --signature-policy ${TESTSDIR}/policy.json --squash ${TESTSDIR}/bud/layers-squash
16911691
}
16921692

1693+
@test "bud with additional directory of devices" {
1694+
target=alpine-image
1695+
mkdir ${TESTSDIR}/foo
1696+
mknod ${TESTSDIR}/foo/null c 1 3
1697+
run_buildah bud --signature-policy ${TESTSDIR}/policy.json --device ${TESTSDIR}/foo:/dev/fuse -t ${target} -f ${TESTSDIR}/bud/device/Dockerfile ${TESTSDIR}/bud/device
1698+
[ "${status}" -eq 0 ]
1699+
expect_output --substring "null"
1700+
1701+
buildah rmi ${target}
1702+
rm -rf ${TESTSDIR}/foo
1703+
}
1704+
16931705
@test "bud with additional device" {
16941706
target=alpine-image
16951707
run_buildah bud --signature-policy ${TESTSDIR}/policy.json --device /dev/fuse -t ${target} -f ${TESTSDIR}/bud/device/Dockerfile ${TESTSDIR}/bud/device
@@ -1793,7 +1805,7 @@ load helpers
17931805
echo "$output"
17941806
expect_output --substring "abc.txt"
17951807

1796-
rm ${TESTSDIR}/bud/use-args/abc.txt
1808+
rm ${TESTSDIR}/bud/use-args/abc.txt
17971809
buildah rm --all
17981810
buildah rmi --all --force
17991811
}

0 commit comments

Comments
 (0)