Commit Graph

88 Commits

Author SHA1 Message Date
Jan Kaluza a0a9ac6380 Switch common, storage and image to monorepo.
Signed-off-by: Jan Kaluza <jkaluza@redhat.com>
2025-08-29 15:04:28 +02:00
openshift-merge-bot[bot] f753f46ac4
Merge pull request #6307 from nalind/reap
Run: reap stray processes
2025-08-07 15:08:26 +00:00
Nalin Dahyabhai 87f60f60c6 Only suppress "noted" items when not squashing
When suppressing what we think are pulled-up directories at commit-time,
only do that when we're _not_ squashing the image, in which case we
really do need to output it into the one layer that our output image
will have.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-08-01 16:55:16 -04:00
Nalin Dahyabhai 27c40b3dbe Reap stray processes
run: after we've picked up the exit status of the "main" process that
we're running, reap anything that was reparented to us before returning.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-08-01 13:30:47 -04:00
Nalin Dahyabhai 7d302c38d1 Exclude pulled up parent directories at commit-time
When Ensure() tells us about the parent directories of a mountpoint
target that it created for us, add them to the list of items that we'll
exclude from the commit unless something else causes them to be
modified.

The "native" overlay diff method just walks the diff directory, so we
can get directories in the committed layer that look exactly like the
ones in the previous layer, depending on the storage driver.  And while
it's barely noticeable, we can still control for it.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-07-24 13:13:53 -04:00
Nalin Dahyabhai 95013b363f copier.Ensure(): also return parent directories
Have Ensure() also return the parent directories of items that it
created, along with information about them that can be used to filter
them out of the layer at commit-time.

This modifies the signature of Ensure(), but it was added in 1.41.0, and
shouldn't (yet) have any external users.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-07-24 13:13:53 -04:00
Nalin Dahyabhai df36bb835a commit: exclude parents of mount targets, too
When RUN requires us to create the target for a mountpoint, make note of
it and any parent directories that needed to be created, and filter them
out when generating a layer diff or --output data.

The exceptions will be directories that the conformance tests confirm
that BuildKit also leaves behind, though for compatibility with the
classic builder, we have to make that conditional.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-06-20 11:19:27 -04:00
Nalin Dahyabhai a3bea818b8 run: clean up parents of mount targets, too
When RUN requires us to create the target for a mountpoint, make note of
it and any parent directories that needed to be created, and clear them
all out if they look basically the same after the command finishes.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-06-20 11:19:27 -04:00
Nalin Dahyabhai fce45b77d0 Builder: drop the TempVolumes field
The TempVolumes field is used to track data that's specific to a Run()
call, and there can be more than one of those going at a time.  Merge
its data into the runMountArtifacts struct, which already keeps a list
of locations that we have to clean up in exactly the same way.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-06-20 11:19:27 -04:00
Nalin Dahyabhai 8178c3a5d3 runUsingRuntime: use named constants for runtime states
Use the named constants for the status values that runtimes can report
to us when we run them with the "state" command.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-05-29 14:17:43 -04:00
Kir Kolyshkin 608e4cc24f generateHostname: simplify
No functionality change, just code simplification.

Covered by existing test (e.g. "run should also override /etc/hostname"
in tests/run.bats).

NO NEW TESTS NEEDED

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-08 15:26:09 -07:00
Kir Kolyshkin e8dba98314 ci: add nolintlint, fix found issues
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-07 13:02:39 -07:00
Kir Kolyshkin e8a5d77b09 ci: switch to golangci-lint v2
Also, add a way to update golangci-lint locally, if an old version is
installed.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2025-04-07 13:01:56 -07:00
Nalin Dahyabhai 72e2bf4c69 Support the containers.conf container_name_as_hostname option
When containers.conf has the "container_name_as_hostname" option set,
use that value, with values that don't fit `[A-Za-z0-9][A-Za-z0-9.-]+`
stripped out.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-02-18 17:43:07 -05:00
Nalin Dahyabhai 3541a3ce2a Accept image names as sources for cache mounts
Allow the "from" option for cache mounts to refer to an image, which, if
we attempt to mount read-write, is mounted using an overlay to avoid
getting errors if the process we're running with the cache mount
attempts to write to it.

Add logic to clean up the bind mount in Builder.getBindMount() that's
triggered if Builder.runSetupVolumeMounts() returns an error.

When creating cache directories for cache mounts, process the
container's ID mappings.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-01-23 13:38:02 -05:00
Eng Zer Jun 59024c41ad
refactor: replace golang.org/x/exp with stdlib
These experimental packages are now available in the Go standard
library since Go 1.21:

	1. golang.org/x/exp/slices -> slices [1]
	2. golang.org/x/exp/maps -> maps [2]

[1]: https://go.dev/doc/go1.21#slices
[2]: https://go.dev/doc/go1.21#maps

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2025-01-24 00:27:55 +08:00
Nalin Dahyabhai e7f790296e relabel(): correct a misleading parameter name
The "recurse" parameter of the relabel() function signature is passed to
a function which calls it "shared".

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-01-20 13:24:35 -05:00
Nalin Dahyabhai 6e62bd0835 Fix TOCTOU error when bind and cache mounts use "src" values
Fix a time-of-check/time-of-use error when mounting type=bind and
type=cache directories that use a "src" flag.  A hostile writer could
use a concurrently-running stage or build to replace that "src" location
between the point when we had resolved possible symbolic links and when
runc/crun/whatever actually went to create the bind mount
(CVE-2024-11218).

Stop ignoring the "src" option for cache mounts when there's no "from"
option.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-01-17 09:59:55 -05:00
Nalin Dahyabhai 25a3b385af internal/volume.GetBindMount(): discard writes in bind mounts
When handling RUN --mount=type=bind, where the mount is read-write,
instead of a simple bind mount, create an overlay mount with an upper
directory that will be discarded after the overlay mount is unmounted.
This brings us in line with the expected behavior, wherein writes to
bind mounts should be discarded.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2025-01-17 09:59:55 -05:00
Nalin Dahyabhai 701d6bbe91 Handle RUN --mount with relative targets and no configured workdir
When the target location of a RUN --mount is specified as a relative
path, we normally try to convert it to an absolute path by combining it
with the currently-configured working directory.  If there is no such
value, though, the result is still not an absolute path.  Work around
this by using "/" when the configured working directory is "".

Set this field in the `runMountInfo` struct on FreeBSD, as we already
did on Linux.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-11-04 09:51:20 -05:00
Nalin Dahyabhai 6c85740bab Document more buildah build --secret options
Describe the "env" and "type" options in the buildah-build(1) man page.

When parsing the "--secret=" flag for the CLI, instead of ignoring an
option that we don't recognize, return an error.

Even though the set of meaningful "id" values for secrets is passed in
via the command line, don't directly use it to construct a file path.

Change the default mode for SSH agent sockets that we create from 0o620
to 0o600.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-10-17 12:02:29 -04:00
Paul Holzinger 5184bf1612
Add support for the new c/common pasta options
We always map 169.254.1.2 with pasta to the host now so ensure the
host.containers.internal entry is set correctly.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-09-06 17:05:52 +02:00
Nalin Dahyabhai 0ad0ffaad9 Fix some govet linter warnings
govet warned about some places where we were passing something other
than a literal string to a function that took format specifiers.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-08-15 16:09:24 -04:00
Nalin Dahyabhai 8ae99121c1 CI: enable the gofumpt linter
Turn on the gofumpt linter.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-08-15 13:17:44 -04:00
Kir Kolyshkin 24da18800e *: fix build tags
This change is generated by `go1.23rc2 fix ./...`.

Had to use go1.23rc2, since all released go versions have a bug
preventing it from working with `go 1.22.0` in go.mod (opened
https://github.com/golang/go/issues/68825,
https://github.com/golang/go/issues/68824 for awareness).

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2024-08-09 17:05:30 -07:00
Nalin Dahyabhai a7098c776a imagebuildah: make traditional volume handling not the default
Make the traditional handling of volumes (where they're "frozen" and can
only be modified by ADD or COPY, which requires that we cache their
contents and save/restore them before/after RUN instructions) an option
that is not enabled by default.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-07-15 09:38:28 -04:00
Doug Rabson 1480f147fa buildah: fix a nil pointer reference on FreeBSD
When setting up resolv.conf on Linux we need to reference the Linux
namespaces from spec.Linux. This is not needed for FreeBSD and this
change moves the dereference of spec.Linux to addResolvConfEntries and
makes it conditional.

Signed-off-by: Doug Rabson <dfr@rabson.org>
2024-06-09 12:00:05 +01:00
Nalin Dahyabhai a42019d614 Drop copyStringSlice() and copyStringStringMap()
Use slices.Clone() and maps.Clone() instead of our own non-generic
functions.  We have to be more careful in a couple of places where we
set items in maps which aren't unconditionally initialized.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-06-05 11:01:07 -04:00
Daniel J Walsh 864a40cbdc
Don't leak temp files on failures
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2024-05-16 14:11:37 -04:00
Giuseppe Scrivano 7053383792
buildah: use fileutils.(Le|E)xists
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2024-04-12 09:58:14 +02:00
Paul Holzinger 516f9e9a24
correctly configure /etc/hosts and resolv.conf when using network
Previously buildah may have created a incorrect hosts/resolv.conf file,
when netavark, slirp4netns or pasta are used we have to actually
consider their special setup and use the correct nameservers.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-03-18 14:38:01 +01:00
Paul Holzinger 9db5a16f7d
buildah: refactor resolv/hosts setup.
We should never configure the files before we setup the network as we
are missing a lot of information. This is part 1 of the refactor where
we split the functions between create/add entries part. See the
following commit to actualy see how me make use of this.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-03-18 14:37:32 +01:00
Daniel J Walsh 471460018b
Ignore errors if label.Relabel returns ENOSUP
This is a common mistake by users and is ignored in some places
but not everywhere. This change will help this to be ignored everwhere.

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2023-11-29 06:54:21 -05:00
Valentin Rothberg 02f781b354 container.conf: support attributed string slices
All `[]string`s in containers.conf have now been migrated to attributed
string slices which require some adjustments in Buildah and Podman.

[NO NEW TESTS NEEDED]

Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
2023-10-26 15:34:23 +02:00
Daniel J Walsh 03b0042243
Use cutil.StringInSlice rather then contains
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2023-10-24 19:44:00 -04:00
Benjamin Schubert 249e30c981
bug: Ensure the mount type is always BindMount by default
Previously, when mounting multiple mounts, if any mount had a `type`
specified, it would override the default type for subsequent mounts.

This meant, for example, that having a RUN step like:

```
RUN --mount=type=cache,target=/cache --mount=source=.,target=/src
```

would incorrectly mount the second source as a cache, instead of a
bind-mount.

This fix ensures the default is reset between each iteration of the
loop, ensuring we get the right mount type.

Signed-off-by: Benjamin Schubert <contact@benschubert.me>
2023-09-30 17:26:37 +01:00
Daniel J Walsh ccac7ca0bf
Run codespell on code
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2023-09-14 06:05:57 -04:00
Paul Holzinger 6e6827b270
Move most of internal/parse to internal/volumes
internal/parse does not need to depend on libimage.
This allows for a smaller podman remote client.

Based on Miloslav's work: https://github.com/containers/podman/pull/19718

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2023-09-12 14:29:31 +02:00
Lokesh Mandvekar c34c6d19a5
Merge branch 'main' into pkg-import 2023-06-27 13:18:00 -04:00
guoguangwu d22a5293be chore: pkg imported more than once
Signed-off-by: guoguangwu <guoguangwu@magic-shield.com>
2023-06-27 13:35:36 +08:00
Paul Holzinger 11ba328e2b
use slirp4netns package from c/common
This uses the same code as podman for slirp4netns, this means
- ipv6 is enabled by default
- slirp4netns options are read from contianers.conf
- slirp4netns options can now be set on the cli. This required some
small rework on where we parse the network string.

Lastly I updated the --network docs, to document the new slirp4netns
mode. That included fixing up buildah-from and buildah-run pages which
were incomplete in that regard. Now we show the same for all options.

Fixes #3968

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2023-06-23 13:57:30 +02:00
Paul Holzinger 14cd28c262
add hostname to /etc/hosts when running with host network
Some tools depend on the hostname being present in /etc/hosts. I would
argue they are broken but its not like we can do anything about that.

This adds the hostname with the local host ip when the host network is
used. For private networking we already add the hostname.

We also now correctly force host networking in chroot mode, it was
silently ignored before thus causing extra confusion here.

Fixes #4446

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2023-06-20 16:12:28 +02:00
Nalin Dahyabhai 68337bf9c6 Go back to setting SysProcAttr.Pdeathsig for child processes
Restore SysProcAttr.Pdeathsig values for child processes that we start,
so that they get a SIGKILL when we exit for whatever reason.  Lock the
calling goroutine to a native thread while that's happening, like we
should always have done.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2023-05-23 09:01:40 -04:00
Giuseppe Scrivano 0eb266a958
run: drop Pdeathsig
it is not safe to use prctl(PR_SET_PDEATHSIG) in a Go program since
the parent of the process is considered the *thread* that created the
child process, and the Go runtime can free a thread at any time.   We
would either need to lock the thread, or simpler just not use it.

[NO NEW TESTS NEEDED]

Closes: https://github.com/containers/podman/issues/17918

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2023-05-16 20:07:58 +02:00
Aditya R dcc7feb6fd
secret: accept required flag w/o value
`Buildkit` and `docker` accepts `required` flag as `true` in
`--mount=type=secret` so let do same for buildah.

Example usage

```Dockerfile
FROM docker.io/nginx:1.23.3

RUN --mount=type=secret,id=nginx-crt,dst=/nginx-repo.crt,required \
    --mount=type=secret,id=nginx-key,dst=/nginx-repo.key,required \
    set -x \
    && . /etc/os-release \
    && stat /nginx-repo.crt \
    && stat /nginx-repo.key
```

Closes: https://github.com/containers/podman/issues/18438

Signed-off-by: Aditya R <arajan@redhat.com>
2023-05-15 13:07:17 +05:30
Aditya R 2fed5fc912
run,mount: remove path only if they didnt pre-exist
It seems buildah was cleaning path after unmounting the content which
was added by `--mount`, however buildah should `remove` only if the path
it did not pre-exist otherwise it should just simply `unmount`.

Following behaviour aligns buildah to buildkit

Closes: https://github.com/containers/buildah/issues/4739

Signed-off-by: Aditya R <arajan@redhat.com>
2023-05-04 11:26:52 +05:30
Aditya R 9fced965ee
build, secret: allow realtive mountpoints wrt to work dir
When working with `--mount=type=secret` allow `target`/`dst` to accept relative paths w.r.t to the configured work dir.

See detailed use-case here: https://github.com/containers/buildah/issues/4491

Closes: https://github.com/containers/buildah/issues/4491

**Steps to reproduce the issue from #4491:**

1. Create Dockerfile and Makefile:

Dockerfile:
```
FROM docker.io/ubuntu:22.04

WORKDIR /somedir

RUN --mount=type=secret,id=secret-foo,dst=secret1.txt --mount=type=secret,id=secret-bar,dst=secret2.txt \
     printf "PWD=%s\n" "$(pwd)" && ls -la && ls -la / && stat secret1.txt && stat secret2.txt && \
     cp secret1.txt /root/secret-foo.txt && \
     cp secret2.txt /root/secret-bar.txt
```

Makefile:
```
DOCKER ?= docker

.PHONY: build-container

build-container:
	rm -rf build
	mkdir build
	echo "secret:foo" >build/secret1.txt
	echo "secret:bar" >build/secret2.txt
	buildah --no-cache --secret id=secret-foo,src=build/secret1.txt --secret id=secret-bar,src=build/secret2.txt -t defanator/example:tag1 .
	podman run --rm -t -i defanator/example:tag1 cat /root/secret-foo.txt
	podman run --rm -t -i defanator/example:tag1 cat /root/secret-bar.txt
	podman rmi defanator/example:tag1
```
```
make
```

Signed-off-by: Aditya R <arajan@redhat.com>
2023-01-09 23:34:55 +05:30
Daniel J Walsh f8eb73820a
Merge pull request #4452 from flouthoc/relative-mount
build, mount: allow realtive mountpoints wrt to work dir for `--mount=type=cache` and `--mount=type=bind`
2023-01-03 08:33:18 -05:00
Daniel J Walsh 60be7f2509
Add support for --group-add to buildah from
Allow containers running under buildah to use
--group-add keep-groups, so that they can inherit
access to the users groups.

Also allow users to add supplimental groups to the container.

Fixes: https://github.com/containers/buildah/issues/4476

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2022-12-22 11:53:00 -05:00
Paul Holzinger 67c2e4de59
buildah: wrap network setup errors
User may get confusing error messages were it is not clear that they are
related to the network setup, wrap the error to make it more clear that
some network setup failed.

[NO NEW TESTS NEEDED]

Fixes containers/podman#16809

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2022-12-15 17:29:48 +01:00