Commit Graph

204 Commits

Author SHA1 Message Date
Nalin Dahyabhai 5198af7d7c Add support for passing CDI specs to --device
Add support for using CDI to resolve `--device` devices for RUN
instructions during `buildah build`, `buildah from`, and `buildah run`,
as `podman run` does.

This generally requires that we stop resolving device specifications
(arguments passed to --device) earlier and deferring that until it's
time to run a process, because CDI wants to pick over those values,
modify a runtime spec to set up the ones that it knows about, and then
hand back the list of values that it doesn't know about.

We don't want to do a dry run of this during CLI processing because that
would create a window where the underlying hardware state could change,
and that could produce some hard-to-diagnose errors.

Being able to test this is going to require that we add the `--device`
flag to `buildah run` (`--security-opt` affects how we build the
container's layer, so it has to be done at `buildah from`).

The default configured devices list is pulled in by CLI flag processing
during `buildah from` and `buildah build`, so it doesn't also need to be
explicitly passed to `buildah run` or the internal Run() method.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-03-29 16:57:10 -04: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
Paul Holzinger 8c1fee7e75
rename the hostFile var to reflect the value better
I got confused a bit there so make it clearer to readers that both are
different.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2024-03-18 14:37:32 +01:00
Nalin Dahyabhai 4f0b619dd2 Use golang.org/x/exp/slices.Contains
... instead of github.com/containers/common/pkg/util.StringInSlice,
per linters.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2024-02-07 11:50:41 -05:00
Daniel J Walsh 79fb3c2135
Make buildah match podman for handling of ulimits
Podman currently sets the ulimits of nofile and nproc
to max in rootless mode, if the user does not override.

Buildah on the other hand just passes in the current defaults.

Podman build should match podman run, and this will fix that problem.

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

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2024-01-31 15:28:51 -05: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
Daniel J Walsh 8df9b760b3
Stop using DefaultNetworkSysctl and use containers.conf only
Also mark uses of DefaultCapabilities as Deprecated.

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

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2023-11-13 11:46:04 -05:00
Daniel J Walsh 3b96b1cafb
Use mask definitions from containers/common
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2023-10-27 13:10:18 -04: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
Daniel J Walsh 48a9c43718
Add --no-hostname option to buildah containers
Fixes: https://github.com/containers/buildah/issues/5093

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2023-10-24 19:43:56 -04:00
Aditya R 9c99d7ca88
run: use internal.GetTempDir with os.MkdirTemp
Projects which are using buildah as a library and set `TMPDIR` manually
can stumble upon a use-case where `TMPDIR` was set to a relative path.

Such as `export TMPDIR=.` in such case buildah will try to create a
temporary root using `Mkdirtemp` leading to a point where bundle is not
generated correctly since path was relative.

Following use case can be resolved by making sure that buildah always
converts relative path to absolute path and `GetTempDir` does it well.

Example reproducer with podman

```Dockerfile
FROM alpine
RUN echo hello
```

```console
export TMPDIR=.
podman build --no-cache -t test .
```

Expected failure
```console
STEP 1/2: FROM alpine
STEP 2/2: RUN echo hello
error running container: checking permissions on "buildah2341274198": stat buildah2341274198: no such file or directory
ERRO[0000] did not get container create message from subprocess: EOF
Error: building at STEP "RUN echo hello": while running runtime: exit status 1
```

Closes: RHEL-2598

Signed-off-by: Aditya R <arajan@redhat.com>
2023-10-11 21:49:18 +05:30
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
Daniel J Walsh 3454d5b291
Support passing of ULimits as -1 to mean max
Docker allows the passing of -1 to indicate the maximum limit
allowed for the current process.

Fixes: https://github.com/containers/podman/issues/19319

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2023-08-26 06:51:09 -04:00
Paul Holzinger a8f350fc51
rootless: use default_rootless_network_cmd config
Use the `default_rootless_network_cmd` containers.conf options to know
which rootless network program to use as default. This setting is
important so distros and user can actually set a different default if
they wish.

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2023-06-28 15:20:39 +02:00
Lokesh Mandvekar c34c6d19a5
Merge branch 'main' into pkg-import 2023-06-27 13:18:00 -04:00
Paul Holzinger b3edcaa062
vendor: update c/{common,image,storage} to latest
includes two test fixes

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2023-06-27 10:43:34 +02: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 74b885b9e2
buildah: add pasta(1) support
Like podman allow buildah and therefore podman build to use the network
mode pasta. The pasta integration is very simple and we do not even
need a teardown handler for that as pasta will exit on its own when the
netns path is removed.

However right now this is broken, pasta will fail to open
/proc/$pid/ns/net. I send a patch[1] to fix this upstream in pasta.
I assume this will land quickly so I like to get this in now just so we
have this included in podman v4.6. Thus the test is skipped for now.

[1] https://archives.passt.top/passt-dev/20230623082531.25947-2-pholzing@redhat.com/

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2023-06-23 13:58:34 +02: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
Giuseppe Scrivano 8e84bb7097
run_linux: Update heuristic for mounting /sys
change the heuristic to mount a fresh sysfs every time a new network
namespace is created.  This modification ensures the creation of the
sysfs when the network namespace is created, which is better than
sharing the one from the host.

Closes: https://bugzilla.redhat.com/show_bug.cgi?id=2164524

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
2023-03-23 23:21:39 +01:00
Nalin Dahyabhai 0b7a7f80bb docs/tutorials/04: add defaults for Run()
In the tutorial, switch to calling `DefaultStoreOptionsAutoDetectUID()`
instead of `DefaultStoreOptions()`, which should figure things out
better.

In the tutorial, add an example of using Run(), where for API backward
compatibility reasons, we can't tell the difference between "grant no
capabilties by default" and "grant the default set of capabilities by
default".

The default isolation can be set automatically, so start doing that at
run-time, but have the tutorial look it up anyway because the tutorial
on the web will be newer than our current release for at least a while.

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2023-02-21 08:59:43 -05:00
Aditya R d9578d32cd
build, mount: allow realtive mountpoints wrt to work dir
When working with `--mount=type=bind` and `--mount=type=cache` allow
`target` to accept relative paths w.r.t to the configured work dir.

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

Signed-off-by: Aditya R <arajan@redhat.com>
2022-12-15 16:30:34 +05:30
Chris Evich 46eea31588
Replace io/ioutil calls with os calls
In golang 1.19, `io/ioutil` is fully deprecated preventing Buildah from
compiling.  Replace all calls with equivalent calls from the `os`
package.

Signed-off-by: Chris Evich <cevich@redhat.com>
2022-12-06 14:29:32 -05:00
Miloslav Trmač a1698cde60 Update c/storage after https://github.com/containers/storage/pull/1436
... and update to remove the now-deprecated Locker interface.

[NO NEW TESTS NEEDED]

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2022-12-01 15:39:25 +01:00
Daniel J Walsh d4c661a774
Add no-new-privileges flag
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2022-11-04 11:43:29 -04:00
Miloslav Trmač 8356687157 Ensure the cache volume locks are unlocked on all paths
... and use a more traditional error handling model,
where responsibility for the cleanup passes to the caller
_only_ if the called function succeeds.

To reinforce that, hard-code nil returns on error paths
instead of returning the locks.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2022-10-20 00:14:18 +02:00
Miloslav Trmač 60382209e3 Simplify the interface of GetCacheMount and getCacheMount
It can return at most one lock, so don't return an array.

Should not change behavior right now, but it will simplify
cleanup.

[NO NEW TESTS NEEDED]

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2022-10-19 23:23:34 +02:00
Miloslav Trmač cfa10d16c9 Maintain cache mount locks as lock objects instead of paths
They exist in memory anyway, so this is more efficient:
we avoid the need to manually touch the filesystem again,
the associated costs - and the error paths go away.

[NO NEW TESTS NEEDED]

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2022-10-19 23:23:33 +02:00
Daniel J Walsh 9ae09bf72f
Set sysctls from containers.conf
Fixes: https://github.com/containers/buildah/issues/4291

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2022-09-28 05:50:45 -04:00
Daniel J Walsh 8d5d763213
Fix stutters
Podman adds an Error: to every error message. So starting an error
message with "error" ends up being reported to the user as

Error: error ...

This patch removes the stutter.

Also ioutil.ReadFile errors report the Path, so wrapping the err message
with the path causes a stutter.

Signed-off-by: Daniel J Walsh dwalsh@redhat.com

[NO NEW TESTS NEEDED]

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2022-09-19 07:11:44 -04:00
Daniel J Walsh fc3ae53b70
buildah bud --network=none should have no network
Fixes: https://github.com/containers/buildah/issues/4227

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
2022-09-17 06:13:36 -04:00
Nalin Dahyabhai bb149ea686 Use errors.Is() instead of os.Is{Not,}Exist
If errors for which os.IsExist() or os.IsNotExist() would have returned
true have been wrapped using fmt.Errorf()'s "%w" verb, os.IsExist() and
os.IsNotExist(), not having been retrofitted to use errors.Is(), will
return false.

Use errors.Is() to check if an error is an os.ErrExist or os.ErrNotExist
error instead of calling os.IsExist() or os.IsNotExist().

Signed-off-by: Nalin Dahyabhai <nalin@redhat.com>
2022-07-26 15:36:58 -04:00
Doug Rabson cdac8e1ba9 Move cleanupTempVolumes and cleanupRunMounts to run_common.go
Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-07-12 09:14:22 +01:00
Doug Rabson 57a73847e4 Move get{Bind,Tmpfs,Secret,SSH}Mount to run_common.go
Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-07-12 09:14:22 +01:00
Doug Rabson 0832ea2f35 Move runSetupRunMounts to run_common.go
Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-07-12 09:14:22 +01:00
Doug Rabson 9c808c76ac Move cleanableDestinationListFromMounts to run_common.go
Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-07-12 09:14:22 +01:00
Doug Rabson cf079d017a Move setupMounts and runSetupBuiltinVolumes to run_common.go
Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-07-12 09:14:22 +01:00
Doug Rabson 95a18737a6 Move runAcceptTerminal to run_common.go
Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-07-12 09:14:21 +01:00
Doug Rabson 1ab567151a Move stdio copying utilities to run_common.go
Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-07-12 09:14:21 +01:00
Doug Rabson ba5ff64d1c Move runUsingRuntime and runCollectOutput to run_common.go
Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-07-12 09:14:21 +01:00
Doug Rabson b290c86172 Move fileCloser, waitForSync and contains to run_common.go
Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-07-12 09:14:21 +01:00
Doug Rabson bff1c193a3 Move checkAndOverrideIsolationOptions to run_common.go
Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-07-12 09:14:21 +01:00
Doug Rabson 75ae804029 Move DefaultNamespaceOptions to run_common.go
Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-07-12 09:14:21 +01:00
Doug Rabson acdf4777aa Move getNetworkInterface to run_common.go
Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-07-12 09:14:21 +01:00
Doug Rabson 8e36f5fc9c Move configureEnvironment to run_common.go
Signed-off-by: Doug Rabson <dfr@rabson.org>
2022-07-12 09:14:21 +01:00