Commit graph

63 commits

Author SHA1 Message Date
Michael Vogt
c5655c9006 osbuild: make the entire /etc/selinux avaialble for the buildroot
When moving to `bootc install to-filesystem` we need more information
for bootc from /etc/selinux than our current /etc/selinux/targeted/contexts
policy.

This commit makes all of /etc/selinux available which unblocks
the bootc install.
2024-03-22 11:24:35 +01:00
Michael Vogt
345516e867 osbuild: ensure /var/tmp is a real directory
This is a followup for https://github.com/osbuild/osbuild/pull/1649

Instead of symlinking /var/tmp to /tmp which may be on a tmpfs
this commit puts it on a real filesystem.

This should fix:
https://github.com/osbuild/bootc-image-builder/issues/285
2024-03-20 20:36:26 +01:00
Michael Vogt
87015318d3 osbuild: tweak "origin=" values, thanks to Simon! 2024-03-12 16:44:12 +01:00
Achilleas Koutsou
83dc625fc3 buildroot: add origin to log messages
Create an origin string of the form 'stages/<stagename>' from the argv
argument and add it to the log prints for messages coming from the
stage.

Signed-off-by: Achilleas Koutsou <achilleas@koutsou.net>
2024-03-12 16:44:12 +01:00
Michael Vogt
2f0ed8c755 osbuild: ensure a usable /var/tmp is available inside the buildroot
Colin asked for this in
https://github.com/osbuild/bootc-image-builder/issues/223 and
it's easy enough.
2024-03-11 13:09:28 +01:00
Ondřej Budai
226b50eba5 stages: add new org.osbuild.bootc.install-to-filesystem
Support the `boot install to-filesystem` capability to install a
bootc image to a filesystem.
2024-02-09 12:03:09 +01:00
Dusty Mabe
83a14886d3 add --break for requesting a debug shell
Similar to rd.break for dracut this allows a user to specify:

- --break or --break=*
    - to get a shell before each stage is run
- --break=stage.name
    - to get a shell each time the stage with that name is run
    - example: --break=org.osbuild.copy
- --break=stage.id
    - to get a shell each time the stage with that ID is run
    - get the ID for the stages for your manifest by running
      osbuild on the manifest with --inspect
    - example: --break=dc6e3a66fef3ebe7c815eb24d348215b9e5e2ed0cd808c15ebbe85fc73181a86

and get a bash shell where they can inspect the environment to debug
and develop OSBuild stages.
2024-01-10 08:39:27 -08:00
Simon de Vlieger
d60690ce46 tox: add tox
`tox` is a standard testing tool for Python projects, this allows you to
test locally with all your installed Python version with the following
command:

`tox -m test -p all`

To run the tests in parallel for all supported Python versions.

To run linters or type analysis:

```
tox -m lint -p all
tox -m type -p all
```

This commit *also* disables the `import-error` warning from `pylint`,
not all Python versions have the system-installed Python libraries
available and they can't be fetched from PyPI.

Some linters have been added and the general order linters run in has
been changed. This allows for quicker test failure when running
`tox -m lint`. As a consequence the `test_pylint` test has been removed
as it's role can now be fulfilled by `tox`.

Other assorted linter fixes due to newer versions:
- use a str.join method (`consider-using-join`)
- fix various (newer) mypy and pylint issues
- comments starting with `#` and no space due to `autopep8`

This also changes our CI to use the new `tox` setup and on top of that
pins the versions of linters used. This might move into separate
requirements.txt files later on to allow for easier updating of those
dependencies.
2023-08-01 15:01:13 +02:00
Christian Kellner
77e717f4f4 buildroot: explicitly bind mount runner
Instead of relying on the assumption that the specific runner will
be in `/run/osbuild/lib/runners/` we now bind-mount the runner at a
specific well known path and execute it from there.
2022-10-11 12:49:16 +02:00
Simon de Vlieger
ea6085fae6 osbuild: run isort on all files 2022-09-12 13:32:51 +02:00
Simon de Vlieger
3703328751 osbuild: explicit encodings for open()
Provides explicit encodings for all calls to `open()`, this is a newer
pylint warning but also just makes sense to do.
2022-09-09 15:33:29 +02:00
Simon de Vlieger
3fd864e5a9 osbuild: fix optional-types
Optional types were provided in places but were not always correct. Add
mypy checking and fix those that fail(ed).
2022-07-13 17:31:37 +02:00
Christian Kellner
4ac62abbc3 buildroot: ability to drop capabilities
Add a new member variable `caps` that if not `None` indicates the
capabilities to retain, i.e. all other capabilities not specified
will be dropped via `bubblewrap` (`--cap-drop`).
Add corresponding tests.
2022-04-27 23:05:11 +01:00
Christian Kellner
136e13eca1 buildroot: don't explicitly add CAP_MAC_ADMIN
This is a left-over from the time when `systemd-nspawn` was used,
which only retained a limited set of capabilities which did not
include `CAP_MAC_ADMIN`[1]. Bubblewrap, on the other hand, retains
all currently capabilities if the process is run as root[2].

[1] see e.g. src/nspawn/nspawn.c#L147 of commit c52950c
[2] see commit abc56644566a6095bb72a5bf70fcee7dd90e9447
2022-04-20 12:05:36 +01:00
Alexander Larsson
46a228df38 Add support for installing containers in images
This adds a stage called org.osbuild.skopeo that installs docker and
oci archive files into the container storage of the tree being
constructed.

The source can either be a file from another pipeline, for example one
created with the existing org.osbuild.oci-archive stage, or it can
be using the new org.osbuild.skopeo source and org.osbuild.containers
input, which will download an image from a registry and install that.

There is an optional option in the install stage that lets you
configure a custom storage location, which allows the use of the
additionalimagestores option in the container storage.conf
to use a read-only image stores (instead of /var/lib/container).

Note: skopeo fails to start if /etc/containers/policy.json is
not available, so we bind mount it from the build tree to the
buildroot if available.
2022-02-10 14:43:17 +01:00
Alexander Larsson
e516bf7898 BuildRoot: Support adding custom env vars
We will need this later to set SOURCE_DATE_EPOCH in the build.
2022-02-09 09:58:49 +01:00
Christian Kellner
c825c7e4fa buildroot: set container env variable
Set the container environment variable to indicate to programs
inside the build root that they are indeed running inside a
container (see also https://systemd.io/CONTAINER_INTERFACE/).
2021-12-09 13:14:27 +01:00
Christian Kellner
0c71289067 buildroot: isolate environment from the host
Create a well-defined environment with and use that for the build
root. It is not desirable to have the host's environment leak
into the container. Add a test to ensure that this works.
NB: This was probably an oversight when we switched from systemd-
nspawn to bubblewrap.
2021-12-09 13:14:27 +01:00
Christian Kellner
fdf3160f49 buildroot: small whitespace fix
Introduce a newline. David, we miss you.
2021-12-07 09:47:01 +00:00
Christian Kellner
c434d7bea2 buildroot: rename stage_timeout to timeout
The build root does not know anything about stages; the stage
concept is one level higher. Therefore rename `stage_timeout`
to `timeout` in the buildroot.
2021-12-07 09:47:01 +00:00
AaronH88
cd8f8681ad osbuild: added a configuable timeout for package installation
Also added new command line option for setting the timeout in milliseconds
2021-12-03 14:29:36 +00:00
Christian Kellner
36356342b0 buildroot: mask /proc/cmdline
Since we bind `/proc` inside the container, we leak certain information
that comes with it. One of this is the kernel command line. None of the
decisions done by software running inside the container should depend
on the kernel command line on the host, so overwrite the kernel command
line by creating a temporary directory and mapping it inside the build-
root. For now we default to a simple `root=/dev/osbuild` fake kernel
command line.
Add a simple check for it as well.
2021-11-30 12:01:13 +01:00
Christian Kellner
7846fa592b buildroot: create var inside a generic temp dir
Instead of having a temporary directory on the host for `/var` inside 
the container, create a generic temporary directory that can be used
for other things and create the `var` inside that.
2021-11-30 12:01:13 +01:00
Christian Kellner
ccb26806fc buildroot: make mounting /boot optional
Currently, we take to paths from the root file system supplied
to the `BuildRoot` class: `/boot` and `/usr`. The reason for
mounting `/boot` is that grub2 and shim install efi binaries
there and for certain images we want to copy the binaries from
the build root and not install the respective packages.
However, if we build to build root itself, we probably don't
want the mount the hosts' `/boot` since we don't want to copy
anything from there. This change should give us the ability to
do exactly that.
2021-10-30 00:16:03 +01:00
Christian Kellner
704d5d305a buildroot: mount /sys as read-only
This will prevent any modification of anything in `/sys`. It will
also prevent `udevadm tigger` to run, which needs /sys writeable.
This is a desired effect, since uevents are not delivered to the
contained environment, so `udevadm trigger` might hang.
2021-06-28 13:39:25 +01:00
Christian Kellner
acfc5f6d71 buildroot: set PYTHONUNBUFFERED env variable
This disables buffering for the standard output stream for python
executables spawn within the build root. This should help with
the ordering of text output in stages: when stdout is buffered,
debug messages via `print` will be end up in that buffer. When
executables are run in the stage, via `subprocess.run` their 
stdout has its own buffering, which will be flushed at the end 
of the run. If stdout was not manually flushed before invoking
the executable, the output of the tool will be emitted before
anything in the buffer. For example:
  print("stage")
  subprocess.run(["echo", "tool"])
Will lead have the following ordering:
  "tool"
  "stage"
To avoid this, without having to manually flush the stdout
buffer before every `subprocess.run`, disable buffering for
python binaries run inside the build root.
2021-06-09 18:37:47 +01:00
Christian Kellner
7f50d2b57f buildroot: don't specify encoding for popen
Since low-level primitives (os.read) are used to read from the stdout
pipe, manual text decoding was necessary there anyway. The `encoding`
argument meant that we could forgo the manual decoding for the call
to `communicate`. But this meant that text handling is not uniform.
Therefore, remove the `encoding` argument from the `Popen` call and
manual decode all the text.
2021-06-09 18:37:47 +01:00
Christian Kellner
6d52349370 buildroot: bind mount mke2fs config
There was a bug in mke2fs (fixed in versionv 1.45.7, with commit
6fa8edd0) where mkfs.ext4 would fail because the default config,
created on the fly, would contain a syntax error. The program
would abort with:
  Syntax error in mke2fs config file (<default>, line #22)
    Unknown code prof 17

To avoid this error, we try to bind mount the config from the build
root.
2021-02-19 14:42:32 +00:00
Christian Kellner
48418be8de buildroot: rename path argument to rundir
This matches the internal, "private", property and better
describes what the variable is for. Additionally, make it
into a proper keyword argument.
2021-02-11 23:49:18 +01:00
Christian Kellner
749f59cc9a buildroot: make var a positional argument
Commit d028ea5b16 introduced bug when introducing the `store`
argument to `Stage.run`, instead of passing `var=var`, i.e.
`var` is being passed as keyword argument, it is now being
passed as a positional one. Since the `path=/run/osbuild`
keyword argument comes before the `var=/var/tmp` argument,
`var` is now being passed as `path` instead of var.
Since `var` is always being passed in throughout the entire
codebase, make it a positional argument, and move it before
`path`.
Adapt the tests to pass `var` as positional argument.
2021-02-11 23:49:18 +01:00
Christian Kellner
d9168ee625 buildroot: continuously stream log data to monitor
All runners stopped calling `api.setup_stdio` (commit c40b414), and
thus all output of runners and also modules is now redirected to a
pipe (created via Popen and subprocess.PIPE for stdout).
Text was read from that pipe via `stdout.read(4096)`, which means
that it is now buffered in chunks of 4096, where it previously was
line buffered in the case that osbuild was run in the terminal and
--json was not specified. This is very annoying for anyone wanting
to follow osbuild's output in real-time.
Restore the previous behavior by using `os.read`, which should be
a small wrapper around read(3), which does not block until all the
requested data is available but returns early (short reads). This
means, new text will be forwarded as soon is it is available in the
pipe. Increase the read buffer to 32768 while at it, which is what
Popen is using in Python 3.9.
2020-10-28 14:28:07 +01:00
Christian Kellner
10579ee6f5 buildroot: return a new CompletedBuild with output
Create a new CompletedBuild object that wraps and is very similar
to the subprocess.CompletedProcess, i.e. it has a process member
but also has shortcuts for returncode. Additionally, the output
of the process is not only forwarded to the monitor, but also
captured and then handed to CompletedBuild, so its output member
will actually contain the full build output. To be compatible
with the previously returned CompletedProcess, `stderr`, `stdout`
members exist on CompletedBuild that also return `output`.
2020-08-31 15:06:36 +02:00
Christian Kellner
96a5499ed9 buildroot: log bubblewrap's output
In case that bubblewrap fails to, e.g. because it fails to execute
the runner, it will print an error message to stderr. Currently,
this output is not capture and thus not logged. To fix that, the
`BuildRoot.run` method now takes a monitor object and will stream
stdout/stderr to the log via the monitor.
2020-08-27 08:07:14 +02:00
Christian Kellner
41cf4bf2d3 buildroot: ensure /sys/fs/selinux is read-only
Make sure "/sys/fs/selinux" is read-only, otherwise libselinux and
tools will assume that SELinux is available and active and in turn
use /sys/fs/selinux to e.g. verify the file systems labels; this
will then prevent setting unknown labels via `setfiles`.
2020-08-12 16:52:27 +02:00
chloenayon
1e3c0aea1b osbuild: unified libdir handling
Change the default of libdir to /usr/lib/osbuild and
remove redundant logic. Additionally, change how the
python package is detected.

Instead of checking if libdir is None, check if
/usr/lib/osbuild is empty - i.e. if the user has specified
a different directory than the default.
2020-08-04 09:02:22 +02:00
Christian Kellner
c5925fd185 buildroot: unshare the network
Run the container in a new network namespace, to isolate the host's
network from that of the container. Stages, assemblers and the tools
they execute are not supposed to assume network access is available
and this isolation will make sure of that.
2020-07-29 02:16:20 +01:00
Christian Kellner
0203dc4ccd buildroot: ensure rundir and vardir exist
The `BuildRoot` wants to create temporary directories in two
locations, `rundir` (supplied as `path`) and `vardir`. Make
sure these directories exist before trying to create temporary
directories in them.
2020-07-27 12:50:38 +01:00
Christian Kellner
aebff47908 buildroot: remove api temporary directory
Now that no API provider is using the temporary directory to place
its sockets in it anymore, this directory can be removed.
2020-07-27 12:50:38 +01:00
Christian Kellner
21a60324bc buildroot: bind mount individual API endpoints
The current way API end points, i.e. sockets for API providers,
are provided to the sandbox is via a temporary directory that
is created by `BuildRoot` which later gets bind-mounted to a well
known path, i.e. /run/osbuild/api inside the sandbox. API providers
are expected to create their socket in that temporary directory.

Now that `BuildRoot` has a `regsiter_api` method and each API has
an `endpoint` property, the socket of each API provider, no matter
where it is located, will get bind-mounted individually inside
the sandbox at /run/osbuild/api using the `endpoint` identifier.

For backwards compatibility reasons the temporary api directory
will still be created by `BuildRoot`, but it is no longer bind
mounted inside the container. This paves the way to remove that
directory completely once all API providers are converted to not
use that directory anymore.
2020-07-27 12:50:38 +01:00
Christian Kellner
03c5cfb37e buildroot: ability to register api endpoints
Add a new `register_api` method that is meant to be used by clients
to register API end point providers, i.e. instances of `api.BaseAPI`.
When the context of the `BuildRoot` is enter, all providers are
activated, i.e. their context is entered. In case `regsiter_api` is
called with an already active context, the provider will immediately
be activated. In both cases their lifetime is thus bound to the
context of the `BuildRoot`. This also means that they are cleaned-up
with the `BuildRoot`, i.e. when its context is exited.
2020-07-27 12:50:38 +01:00
David Rheinsberg
d1b9304a56 buildroot: use bwrap to contain stages
This swaps the `systemd-nspawn` implementation for `bubblewrap` to
contain sub-processes. It also adjusts the `BuildRoot` implementation
to reduce the number of mounts required to keep locally.

This has the following advantages:

  * We know exactly how the build-root looks like. Only the bits and
    pieces we select will end up in the build-root. We can let RPM
    authors know what environment their post-install scripts need to
    run in, and we can reliably test this.

  * We no longer need any D-Bus access or access to other PID1
    facilities. Bubblewrap allows us to execute from any environment,
    including containers and sandboxes.

  * Bubblewrap setup is significantly faster than nspawn. This is a
    minor point though, since nspawn is still fast enough compared to
    the operations we perform in the container.

  * Bubblewrap does not require root.

At the same time, we have a bunch of downsides which might increase the
workload in the future:

  * We now control the build-root, which also means we have to make sure
    it works on all our supported architectures, all quirks are
    included, and all required resources are accessible from within the
    build-root.
    The good thing here is that we have lots of previous-art we can
    follow, and all the other ones just play whack-a-mole, so we can
    join that fun.

The `bubblewrap` project is used by podman and flatpak, it is packaged
for all major distributions, and looks like a stable dependency.
2020-07-21 14:20:32 +02:00
Christian Kellner
a419ee9038 buildroot: grant CAP_MAC_ADMIN for labeling
When applying labels inside the container that are unknown to the
host, the process needs to have the CAP_MAC_ADMIN capability in order
to do so, otherwise the kernel will prevent setting those unknown
labels. See the previous commit for more details.
2020-06-10 01:35:05 +02:00
David Rheinsberg
dfec7aca9d buildroot: convert unused format-string
Convert the `f""` into a `""`, since no format identifier is used. This
makes pylint happier!
2020-05-28 11:06:05 +02:00
David Rheinsberg
d4f40362ec buildroot: drop kwargs from buildroot.run()
Drop the `kwargs` forwarding from buildroot.run() to subprocess.run().
We do not use it other than for `stdin=subprocess.DEVNULL`. Set that
option directly instead.

Doing the kwargs forwarding mixes the argument namespaces and is very
hard to read. It is not clear from the call-site which argument goes to
buildroot.run() and which to subprocess.run().

Lastly, it requires us to manually fetch `check` just to make pylint
happy. Lets just drop this dance and make the API explicit.
2020-05-13 14:17:30 +02:00
David Rheinsberg
41a4afd3ba Revert "buildroot: explicitly invoke runners via python3"
This reverts commit 33844711cd.

There are systems were our runners have no standard python3 location
available. They will fix the environment before invoking any further
utilities. Therefore, we cannot rely on `python3 foo.py` to work in our
ad-hoc containers.

This simply reverts the behavior back to using the shebang.
2020-05-05 16:57:50 +02:00
David Rheinsberg
3c56a3b7ac buildroot: insert osbuild-library into PYTHONPATH
Prepare the PYTHONPATH of the build-root container to include the path
to the osbuild library. This way, we no longer need any symlinks or
bind-mounts for the individual modules.
2020-05-04 12:32:25 +02:00
David Rheinsberg
33844711cd buildroot: explicitly invoke runners via python3
Use the python-interpreter explicitly to invoke the runners. This works
around inconsistencies between scripts imported from the host, and the
interpreter taken from a build-root.
2020-05-04 12:32:25 +02:00
David Rheinsberg
975ef7ff54 buildroot: drop unneeded DeviceAllow=
With `--keep-unit` we now run with the privileges and resources of the
caller. We no longer require external services to extend our privileges.
This also means we no longer have to configure our unit sandbox
manually, but simply rely on kernel sandboxing to do the right thing.
2020-04-29 20:31:32 +02:00
Christian Kellner
11ffe72aff buildroot: micro cleanups for some strings
Remove an unnecessary format string modifier and unify the usage
of quotation marks.
2020-04-24 15:49:03 +02:00
David Rheinsberg
f12c57c1fd buildroot: reduce nspawn requirements further
This adds one more flags to `systemd-nspawn`:

    --keep-unit
        This prevents nspawn from creating its own scope unit and
        instead uses the scope of the caller. Since we want nspawn to
        run with the privileges of the caller, this is fitting for our
        use case.
        Furthermore, this makes nspawn work without a running system
        bus, since it no longer needs to talk to systemd pid1.

        (introduced with systemd-v209)

With this in place, osbuild can be run from within docker containers (or
other containers without systemd as pid1). This still requires some
extra setup, but this can all be done in the container manager.
2020-04-22 18:27:10 +02:00