Skip to content

Commit

Permalink
Merge pull request #5798 from nalind/mounts-implicit-workdir
Browse files Browse the repository at this point in the history
Handle RUN --mount with relative targets and no configured workdir
  • Loading branch information
openshift-merge-bot[bot] authored Nov 4, 2024
2 parents 1752337 + 701d6bb commit 0787ba6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
13 changes: 9 additions & 4 deletions internal/volumes/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ func GetVolumes(ctx *types.SystemContext, store storage.Store, volumes []string,
// getMounts takes user-provided input from the --mount flag and creates OCI
// spec mounts.
// buildah run --mount type=bind,src=/etc/resolv.conf,target=/etc/resolv.conf ...
// buildah run --mount type=cache,target=/var/cache ...
// buildah run --mount type=tmpfs,target=/dev/shm ...
//
// If this function succeeds, the caller must unlock the returned *lockfile.LockFile s if any (when??).
Expand Down Expand Up @@ -590,7 +591,7 @@ func getMounts(ctx *types.SystemContext, store storage.Store, mounts []string, c
}
finalMounts[mount.Destination] = mount
case TypeTmpfs:
mount, err := GetTmpfsMount(tokens)
mount, err := GetTmpfsMount(tokens, workDir)
if err != nil {
return nil, mountedImages, nil, err
}
Expand All @@ -608,7 +609,7 @@ func getMounts(ctx *types.SystemContext, store storage.Store, mounts []string, c
}

// GetTmpfsMount parses a single tmpfs mount entry from the --mount flag
func GetTmpfsMount(args []string) (specs.Mount, error) {
func GetTmpfsMount(args []string, workDir string) (specs.Mount, error) {
newMount := specs.Mount{
Type: TypeTmpfs,
Source: TypeTmpfs,
Expand Down Expand Up @@ -646,10 +647,14 @@ func GetTmpfsMount(args []string) (specs.Mount, error) {
if !hasArgValue {
return newMount, fmt.Errorf("%v: %w", argName, errBadOptionArg)
}
if err := parse.ValidateVolumeCtrDir(argValue); err != nil {
targetPath := argValue
if !path.IsAbs(targetPath) {
targetPath = filepath.Join(workDir, targetPath)
}
if err := parse.ValidateVolumeCtrDir(targetPath); err != nil {
return newMount, err
}
newMount.Destination = argValue
newMount.Destination = targetPath
setDest = true
default:
return newMount, fmt.Errorf("%v: %w", argName, errBadMntOption)
Expand Down
6 changes: 3 additions & 3 deletions run_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,7 @@ func (b *Builder) runSetupRunMounts(mountPoint string, mounts []string, sources
mountImages = append(mountImages, image)
}
case "tmpfs":
mountSpec, err = b.getTmpfsMount(tokens, idMaps)
mountSpec, err = b.getTmpfsMount(tokens, idMaps, sources.WorkDir)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -1665,9 +1665,9 @@ func (b *Builder) getBindMount(tokens []string, context *imageTypes.SystemContex
return &volumes[0], image, nil
}

func (b *Builder) getTmpfsMount(tokens []string, idMaps IDMaps) (*specs.Mount, error) {
func (b *Builder) getTmpfsMount(tokens []string, idMaps IDMaps, workDir string) (*specs.Mount, error) {
var optionMounts []specs.Mount
mount, err := volumes.GetTmpfsMount(tokens)
mount, err := volumes.GetTmpfsMount(tokens, workDir)
if err != nil {
return nil, err
}
Expand Down
7 changes: 7 additions & 0 deletions run_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,16 @@ func (b *Builder) Run(command []string, options RunOptions) error {
return err
}

workDir := b.WorkDir()
if options.WorkingDir != "" {
g.SetProcessCwd(options.WorkingDir)
workDir = options.WorkingDir
} else if b.WorkDir() != "" {
g.SetProcessCwd(b.WorkDir())
workDir = b.WorkDir()
}
if workDir == "" {
workDir = string(os.PathSeparator)
}
mountPoint, err := b.Mount(b.MountLabel)
if err != nil {
Expand Down Expand Up @@ -249,6 +255,7 @@ func (b *Builder) Run(command []string, options RunOptions) error {
}

runMountInfo := runMountInfo{
WorkDir: workDir,
ContextDir: options.ContextDir,
Secrets: options.Secrets,
SSHSources: options.SSHSources,
Expand Down
4 changes: 4 additions & 0 deletions run_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ func (b *Builder) Run(command []string, options RunOptions) error {
workDir = options.WorkingDir
} else if b.WorkDir() != "" {
g.SetProcessCwd(b.WorkDir())
workDir = b.WorkDir()
}
if workDir == "" {
workDir = string(os.PathSeparator)
}
setupSelinux(g, b.ProcessLabel, b.MountLabel)
mountPoint, err := b.Mount(b.MountLabel)
Expand Down
14 changes: 14 additions & 0 deletions tests/bud.bats
Original file line number Diff line number Diff line change
Expand Up @@ -7017,3 +7017,17 @@ EOF
run_buildah 1 build --security-opt label=disable --build-context testbuild=${TEST_SCRATCH_DIR}/cve20249675/ --no-cache ${TEST_SCRATCH_DIR}/cve20249675/
expect_output --substring "cat: can't open '/var/tmp/file.txt': No such file or directory"
}

@test "build-mounts-implicit-workdir" {
base=busybox
_prefetch $base
run_buildah inspect --format '{{.Docker.Config.WorkingDir}}' --type=image $base
expect_output "" "test base image needs to not have a default working directory defined in its configuration"
# check that the target for a bind mount can be specified as a relative path even when there's no WorkingDir defined for it to be relative to
echo FROM $base > ${TEST_SCRATCH_DIR}/Containerfile
echo RUN --mount=type=bind,src=Containerfile,target=Containerfile test -s Containerfile >> ${TEST_SCRATCH_DIR}/Containerfile
echo RUN --mount=type=cache,id=cash,target=cachesubdir truncate -s 1024 cachesubdir/cachefile >> ${TEST_SCRATCH_DIR}/Containerfile
echo RUN --mount=type=cache,id=cash,target=cachesubdir2 test -s cachesubdir2/cachefile >> ${TEST_SCRATCH_DIR}/Containerfile
echo RUN --mount=type=tmpfs,target=tmpfssubdir test '`stat -f -c %i .`' '!=' '`stat -f -c %i tmpfssubdir`' >> ${TEST_SCRATCH_DIR}/Containerfile
run_buildah build --security-opt label=disable ${TEST_SCRATCH_DIR}
}

1 comment on commit 0787ba6

@packit-as-a-service
Copy link

Choose a reason for hiding this comment

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

podman-next COPR build failed. @containers/packit-build please check.

Please sign in to comment.