Commit graph

844 commits

Author SHA1 Message Date
Jonathan Lebon
3c3be92016 devices/loopback: add read-only option
It's sometimes useful to set up a loop device for an already formatted
disk/filesystem image to derive new artifacts from it. In that case, we
want to make sure it's impossible to modify its contents in any way in
that process, both for our own purposes and for other stages operating
on it.

Notably, mounting some filesystems read-only still seem to touch the
disk (like XFS).
2024-09-18 08:54:34 +02:00
Michael Vogt
478fee2876 util: use Libc.memfd_create() when os.memfd_create() is missing
This provide compat for pyton versions below 3.8. This can be
removed (together with the previous commit) once we are at
python3.8+.
2024-09-17 19:27:03 +02:00
Michael Vogt
09e78c52d9 uktil: add libc.memfd_create() wrapper
This is required for python3.6 where there is no `os.memfd_create()`
yet. Can be removed once we move to python3.8+.
2024-09-17 19:27:03 +02:00
Michael Vogt
0abdfb9041 jsoncomm: transparently handle huge messages via fds
The existing jsoncomm is a work of beautiy. For very big arguments
however the used `SOCK_SEQPACKET` hits the limitations of the
kernel network buffer size (see also [0]). This lead to various
workarounds in #824,#1331,#1836 where parts of the request are
encoded as part of the json method call and parts are done via
a side-channel via fd-passing.

This commit changes the code so that the fd channel is automatically
and transparently created and the workarounds are removed. A test
is added that ensures that very big messages can be passed.

[0] https://github.com/osbuild/osbuild/pull/1833
2024-09-17 19:27:03 +02:00
schutzbot
6953ea89b4 Post release version bump
[skip ci]
2024-09-11 08:27:12 +00:00
Michael Vogt
f3188e841f osbuild: also print what export is availalble when one is not found
The current error message when an export is not found could be
improved by printing what exports are actually availalble to make
it easier for the user to e.g. spot typos.
2024-09-10 10:49:18 -07:00
Lukas Zapletal
88474fd4d9 util: run returns CompletedProcess 2024-09-10 19:10:06 +02:00
Michael Vogt
ea14713f3a osbuild add new -q, --quiet option
Current osbuild will always print some non output even
when run with `--monitor=JSONSeqMonitor` because of the
unconditional `print/sys.stdout.write()` in `main_cli.py`.

This commit adds a new `-q` option to silence this so that something
like osbuild-composer can run `osbuild -q --monitor=JSONSeqMonitor`
to get pure json-seq output during the build.

The use-case is to run `osbuild --monitor-fd` from e.g. bib and
osbuild-composer so that we get pure json from the monitor-fd
and anything that goes on std{out,err} can be logged as it is
most likely error output.
2024-09-09 18:07:54 -07:00
Achilleas Koutsou
1093b5eeb2 util/chroot: use subprocess.run() for all commands
For consistency, use subprocess.run() with check=True for the calls that
were previously using subprocess.check_call().

Update the affected tests to match.
2024-08-28 16:45:48 -07:00
Achilleas Koutsou
3dbf389ebf util/chroot: add run() method to context class
Rename the ChrootProcDevSys class to just Chroot and add a run() method.
Calls now can be made using:

  with Chroot(root) as chroot:
      chroot.run(command)
2024-08-28 16:45:48 -07:00
Achilleas Koutsou
149e3ead96 util/chroot: call unmount with check=False
If one of the chroot mounts fails to unmount, keep iterating so that we
don't stop and continue to unmount the rest.
Print an error message with the failed mounts, but don't fail the build.

Since failing to unmount doesn't fail the exiting of the context, and
the context itself doesn't know what will be running in the chroot,
do a lazy unmount.
2024-08-28 16:45:48 -07:00
schutzbot
d893e81004 Post release version bump
[skip ci]
2024-08-28 08:26:06 +00:00
Michael Vogt
77a61da760 osbuild: drop libdir from download() methods
The libdir is passed down for sources but it is never used in
any of our sources. As this is confusing and we want to eventually
support multiple libdirs remove this code.

It looks like the libdir for soruces was added a long time ago in 8423da3
but there is no indication if/how it is/was supposed to get used and
AFACT from going over the git history it was very used.

SourceService:dispatch() never sends "libdir" to the actual sources,
so it is not an even technically an API break.
2024-08-26 19:58:55 +02:00
Achilleas Koutsou
b496732a02 util/toml: disable unspecified-encoding check 2024-08-21 19:26:31 +02:00
Achilleas Koutsou
30fcf37c03 util/toml: add encoding and type hints
- Specify utf-8 encoding when opening files in text mode.
- Add type hints.
- Prefix all the top-level names with _.
2024-08-21 19:26:31 +02:00
Achilleas Koutsou
07a597481b util: move get_host_storage() to a separate module
Add a new util module called host which is used for functions that are
meant for interactions with the host.  These functions should not be
used in stages.

The containers.get_host_storage() function is renamed to
host.get_container_storage() for clarity, since it is no longer
namespaced under containers.
2024-08-21 19:26:31 +02:00
Achilleas Koutsou
bce908e4a2 util: replace toml imports with our util module 2024-08-21 19:26:31 +02:00
Achilleas Koutsou
123b23fb66 util/toml: support writing comment headers
The containers.storage.conf stage writes a header explaining what the
configuration is doing and its origin.  It also supports adding extra
comments via stage options, which we need to support.  Add support for
writing comments at the top of the file in the toml.dump_to_file()
function.
2024-08-21 19:26:31 +02:00
Achilleas Koutsou
94cdcecafb util: add new module for reading and writing toml
The toml module situation in Python is a bit of a mess.  Different
distro versions have different modules packaged or built-in, sometimes
with different capabilities (no writing).  Since we need to support
reading and writing toml files both on the host (osbuild internals,
sources, inputs) and in the build root (stages), let's centralise the
import decision making in an internal utility module that covers all
cases.

Two of the modules we might import (tomli and tomllib) don't support
writing, so we need to either import a separate module (tomli_w) or
raise an exception when dump() is called without a write-capable module.

The tomli and tomllib modules require files be opened in binary mode
(not text) while the others require text mode.  So we can't wrap the
toml.load() and toml.dump() functions directly; the caller doesn't know
which module it will be using.  Let's keep track of the mode based on
which import succeeded and have our functions open the files as needed.

The wrapper functions are named load_from_file() and dump_to_file() to
avoid confusion with the load() and dump() functions that take a file
object.

See also #1847
2024-08-21 19:26:31 +02:00
schutzbot
347c0dec4a Post release version bump
[skip ci]
2024-08-21 17:07:31 +00:00
Achilleas Koutsou
69625505cf util/chroot: rename context class to ChrootProcDevSys
Rename the context class to better describe what it's doing.  It doesn't
run anything in a chroot, but it prepares the tree for it.
2024-08-21 18:50:05 +02:00
Achilleas Koutsou
9edda1d163 osbuild/util: new module: chroot
New chroot utility module that sets up a tree with the necessary virtual
filesystems needed for running commands in the root tree in a similar
environment as they would run in the build root.

This is needed for some stages, but may also be used for all chroot
calls to unify the setup and teardown of the root environment.

The Chroot context class was previously part of the org.osbuild.dracut
stage, which was the first stage to need this setup.
2024-08-21 18:50:05 +02:00
schutzbot
761c03611b Post release version bump
[skip ci]
2024-08-14 08:26:53 +00:00
Michael Vogt
88c35ea306 osbuild: make inputs map() function use fd for reply as well
We recently hit the issue that `osbuild` crashed with:
```
Unable to decode response body "Traceback (most recent call last):
  File \"/usr/bin/osbuild\", line 33, in <module>
    sys.exit(load_entry_point('osbuild==124', 'console_scripts', 'osbuild')())
  File \"/usr/lib/python3.9/site-packages/osbuild/main_cli.py\", line 181, in osbuild_cli
    r = manifest.build(
  File \"/usr/lib/python3.9/site-packages/osbuild/pipeline.py\", line 477, in build
    res = pl.run(store, monitor, libdir, debug_break, stage_timeout)
  File \"/usr/lib/python3.9/site-packages/osbuild/pipeline.py\", line 376, in run
    results = self.build_stages(store,
  File \"/usr/lib/python3.9/site-packages/osbuild/pipeline.py\", line 348, in build_stages
    r = stage.run(tree,
  File \"/usr/lib/python3.9/site-packages/osbuild/pipeline.py\", line 213, in run
    data = ipmgr.map(ip, store)
  File \"/usr/lib/python3.9/site-packages/osbuild/inputs.py\", line 94, in map
    reply, _ = client.call_with_fds(\"map\", {}, fds)
  File \"/usr/lib/python3.9/site-packages/osbuild/host.py\", line 373, in call_with_fds
    kind, data = self.protocol.decode_message(ret)
  File \"/usr/lib/python3.9/site-packages/osbuild/host.py\", line 83, in decode_message
    raise ProtocolError(\"message empty\")
osbuild.host.ProtocolError: message empty
cannot run osbuild: exit status 1" into osbuild result: invalid character 'T' looking for beginning of value
...
input/packages (org.osbuild.files): Traceback (most recent call last):
input/packages (org.osbuild.files):   File "/usr/lib/osbuild/inputs/org.osbuild.files", line 226, in <module>
input/packages (org.osbuild.files):     main()
input/packages (org.osbuild.files):   File "/usr/lib/osbuild/inputs/org.osbuild.files", line 222, in main
input/packages (org.osbuild.files):     service.main()
input/packages (org.osbuild.files):   File "/usr/lib/python3.11/site-packages/osbuild/host.py", line 250, in main
input/packages (org.osbuild.files):     self.serve()
input/packages (org.osbuild.files):   File "/usr/lib/python3.11/site-packages/osbuild/host.py", line 284, in serve
input/packages (org.osbuild.files):     self.sock.send(reply, fds=reply_fds)
input/packages (org.osbuild.files):   File "/usr/lib/python3.11/site-packages/osbuild/util/jsoncomm.py", line 407, in send
input/packages (org.osbuild.files):     n = self._socket.sendmsg([serialized], cmsg, 0)
input/packages (org.osbuild.files):         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
input/packages (org.osbuild.files): OSError: [Errno 90] Message too long
```

The underlying issue is that the reply of the `map()` call is too
big for the buffer that `jsoncomm` uses. This problem existed before
for the args of map and was fixed by introducing a temporary file
in https://github.com/osbuild/osbuild/pull/1331 (and similarly
before in https://github.com/osbuild/osbuild/pull/824).

This commit writes the return values also into a file. This should
fix the crash above and make the function more symetrical as well.

Alternative/complementary version of
https://github.com/osbuild/osbuild/pull/1833

Closes: HMS-4537
2024-08-13 13:13:24 +02:00
Michael Vogt
29f926f305 jsoncom: gracefully report EMSGSIZE errors
When `jsoncomm` fails because the message is too big it currently
does not indicate just how big the message was. This commit adds
this information so that it's easier for us to determine what to
do about it.

We could also include a pointer to `/proc/sys/net/core/wmem_defaults`
but it seems we want to not require fiddling with that so let's
not do it for now.

See also https://github.com/osbuild/osbuild/pull/1838
2024-08-13 09:38:59 +02:00
Tomáš Hozza
1c4ec8faa3 solver/dnf5.py: fix depsolve MarkingErrors
A wrong exception type was returned for the same kind of issues,
compared to the DNF4 version. Specifically, the DNF4 version returned
`MarkingErrors`, while the DNF5 version returned `DepsolveError`, when
a non-existent package was specified in the depsplve request. Make the
behavior consistent and return `MarkingErrors` also from the DNF5
version.

Signed-off-by: Tomáš Hozza <thozza@redhat.com>
2024-08-08 09:58:41 +02:00
Tomáš Hozza
cdde20385d solver/dnf.py: fix depsolve MarkingErrors
The returned error reason didn't contain any details after the merge
with DNF4 version. The reason is that previously, the actual exception
returned by the DNF library was appended to the error reason. However,
now it is wrapped by a custom `MarkingErrors` exception, which didn't
have any details set. The wrapped exception in the `__cause__`
property was not taken into account. Revert to the original behavior
by reusing the wrapped exception message as the message for the
wrapper exception.

Extend the unit test to allow testing of depsolving failures.

Signed-off-by: Tomáš Hozza <thozza@redhat.com>
2024-08-08 09:58:41 +02:00
Michael Vogt
ae72480612 solver: include "solver" key in the dnfjson reply
This commit includes the used sovler in the dnfjson reply. This
is mostly information (e.g. in service logs) but also useful in
tests to ensure that the expected solver was really run.

Note that this needs https://github.com/osbuild/images/pull/723
first.
2024-08-06 21:34:04 +02:00
Brian C. Lane
f17ab5cbaf osbuild-depsolve-dnf: refactor into osbuild.solver module
This moves the dnf and dnf5 code into a new osbuild module called
solver. The dnf specific code is in dnf.py and dnf5 is in dnf5.py

At runtime the osbuild-depsolve-dnf script reads a config file from
/usr/lib/osbuild/solver.json and imports the selected solver. This
currently just contains a 'use_dnf5' bool but can be extended to support
other configuration options or depsolvers.

At build time a config file is selected from tools/solver-dnf.json or
tools/solver-dnf5.json and installed. Currently dnf5 is not installed,
it will be added when dnf5 5.2.1.0 becomes available in rawhide (Fedora
41).

The error messages have been normalized since the top level functions in
osbuild-depsolve-dnf do not know which version of dnf is being used.
2024-08-01 08:57:30 +02:00
Michael Vogt
7b16313ce2 main,monitor: fix total steps in progress reporting
The existing code to record progress was a bit too naive. Instead
of just counting the number os pipelines in a manifest to get the
total steps we need to look at the resolved pipelines.

with this fix `bib` will report the correct number of steps left
when doing e.g. a qcow2 image build. Right now the number of
steps is incorrect because the osbuild manifest contains pipelines
for qcow2,vdmk,raw,ami and all are currently considered steps
that need to be completed. With this commit this is fixed.
2024-07-31 23:00:33 +02:00
schutzbot
89b3221b2c Post release version bump
[skip ci]
2024-07-31 08:15:39 +00:00
Michael Vogt
e535877798 test: add new https_serve_directory() and test certs
This commit adds a new `https_serve_directory()` test helper
and some custom self-signed and worthless certs that are used
during testing. They are not dynamically generated to avoid the
extra compuation time during tests (but they could be).

Generated via:
```
$ openssl req -new -newkey rsa:2048  -nodes -x509  \
   -subj "/C=DE/ST=Berlin/L=Berlin/O=Org/CN=localhost"   \
   -keyout "key1.pem" -out "cert1.pem"
```

This will allow us to test `https` download URLs as well in e.g.
the curl source.
2024-07-29 16:51:48 +02:00
schutzbot
2b2fec85b2 Post release version bump
[skip ci]
2024-07-17 08:16:09 +00:00
Michael Vogt
0d3a153c78 sources: add new _fetch_all_new_curl() helper
When using a modern curl we can download download multiple urls
in parallel which avoids connection setup overhead and is generally
more efficient. Use when it's detected.

TODO: ensure both old and new curl are tested automatically via
the testsuite.
2024-07-04 11:53:40 +02:00
schutzbot
8507b32a87 Post release version bump
[skip ci]
2024-07-03 08:16:19 +00:00
schutzbot
477a164492 Post release version bump
[skip ci]
2024-06-19 08:15:53 +00:00
Florian Schüller
41f528eeb2 osbuild/monitor.py: improve naming of progress 2024-06-18 16:00:55 +02:00
schutzbot
49b86f0b5a Post release version bump
[skip ci]
2024-06-05 08:15:18 +00:00
schutzbot
6b8f2ad20b Post release version bump
[skip ci]
2024-05-22 08:15:46 +00:00
Michael Vogt
2a17756f45 Revert "runners: clean up temp files before exiting the runner"
This reverts commit bc04bfc366.

The `remove_tmpfiles()` helper is nice but it is also problematic
because it creates extra output after the command was run and
created output. E.g. a test failure on centos stream9 [0]
```
            r = root.run(["stat", "--format=%a", "/var/tmp"], monitor)
            assert r.returncode == 0
>           assert r.stdout.strip().split("\n")[-1] == "1777"
E           AssertionError: assert '/usr/lib/tmp... such process' == '1777'
E
E             - 1777
E             + /usr/lib/tmpfiles.d/rpcbind.conf:2: Failed to resolve user 'rpc': No such process
```
Here the output from "stat" is not the last output because the
rempve_tmpfiles runs `systemd-tmpfiles --clean --remove` which
produces some noisy output after stat was run.

This was found by @thozza (thanks!) and discussed in osbuild PR#1785.

There are various ways to fix this, the one is to use the
`--graceful` option of systemd-tmpfiles. However that only got added in
systemd v256 and centos-stream9 has v252 so that is sadly not an option.

Plus even when avaialble it will produce some informational output like
```
All rules containing unresolvable specifiers will be skipped.
```

Another way would be to sent the output from systemd-tmpfiles cleanup
to /dev/null. Not really great as we will not know about real problems
or warnings that we should care about.

None of the option above is good. So I started looking at the tmpfiles.d
rules and the cleanup and why we are doing it. It was added relatively
recently in https://github.com/osbuild/osbuild/pull/1458 and after
some medidiation not having it seems to do no harm (details below). The
tl;dr is that the buildroot is created inside bubblewrap and the
dirs that `--clean` and `--remove` touch are already tmpdirs created
just for the buildroot so the cleanup in the runner is redundant
(and because the cleanup is now run for each buidlroot.run() command
there *might* be unintended conequences but the current rules seem
to not have any).

In detail, the tmpfiles_cleanup() does two things:
1. `--clean`
It will remove files that are older then the given age
in tmpfiles.d. The tmpfiles in centos9 give me the following ages:
```
$ systemd-tmpfiles --cat-config|grep -E '[0-9]+d$'
d /var/lib/systemd/pstore 0755 root root 14d
d /var/lib/systemd/coredump 0755 root root 3d
q /tmp 1777 root root 10d
q /var/tmp 1777 root root 30d
D! /tmp/.X11-unix 1777 root root 10d
D! /tmp/.ICE-unix 1777 root root 10d
D! /tmp/.XIM-unix 1777 root root 10d
D! /tmp/.font-unix 1777 root root 10d
```
Given that we run our commands inside a bubblewrap environment and
give it a fresh /run, /tmp, /var [1] there really should be no long
lived things and even if there are they are cleaned up from the
buildroot itself

2. `--remove`
It will remove files marked for removal in tmpdfiles.d. Running
it on a centos9 env it yields for me:
```
$ systemd-tmpfiles --cat-config|grep -E '^[rRD]'
R /var/tmp/dnf*/locks/*
r /var/cache/dnf/download_lock.pid
r /var/cache/dnf/metadata_lock.pid
r /var/lib/dnf/rpmdb_lock.pid
r /var/log/log_lock.pid
r! /forcefsck
r! /fastboot
r! /forcequotacheck
D! /var/lib/containers/storage/tmp 0700 root root
D! /run/podman 0700 root root
D! /var/lib/cni/networks
R! /var/tmp/container_images*
D     /run/rpcbind 0700  rpc  rpc  -  -
D /run/sudo/ts 0700 root root
R! /tmp/systemd-private-*
R! /var/tmp/systemd-private-*
r! /var/lib/systemd/coredump/.#*
D! /tmp/.X11-unix 1777 root root 10d
D! /tmp/.ICE-unix 1777 root root 10d
D! /tmp/.XIM-unix 1777 root root 10d
D! /tmp/.font-unix 1777 root root 10d
r! /tmp/.X[0-9]*-lock
```
which is also covered by the bwrap cleanup.

[0] https://artifacts.dev.testing-farm.io/2d07b8f3-5f52-4e61-b1fa-5328a0ff1058/#artifacts-/plans/unit-tests
[1] https://github.com/osbuild/osbuild/blob/main/osbuild/buildroot.py#L218
2024-05-20 11:55:24 -07:00
schutzbot
77ce02c3d3 Post release version bump
[skip ci]
2024-05-08 08:13:38 +00:00
Michael Vogt
d50857e5aa stages: fix test values for "images" inputs
The "images" inputs in the tests were bad test values and only worked
"by accident" [0]. Thanks to Achilleas for discovering this!

This commit fixes this.

[0] https://github.com/osbuild/osbuild/pull/1752#discussion_r1580891435
2024-04-29 14:11:44 +02:00
Michael Vogt
a3f86a0736 testutil: fix make_container() cleanup
During the work on PR#1752 Florian discovered that make_containers()
is broken for nested containers like:
```
with make_container(tmp_path, {"file1": "file1 from base"}) as base_tag:
    with make_container(tmp_path, {"file1": "file1 from final layer"}, base_tag) as cont_tag:
```
It errors with:
```
Error: 5b947de461ee21b858dd5b4224e80442b2f65b6410189147f2445884d9e4e3d8: image not known
```
The reason is that we work with hashes for the image and then call
`podman image rm` which by default will also remove all dangling
references. Those are defined by not having a tag and not referenced
anymore. So the inner container cleanup also removes the outter.

There are many ways to fix this, I went with re-adding tags to the
test containers because it also makes it easy for the user to see if
we left any containers (accidently) around.
2024-04-25 21:59:40 +02:00
schutzbot
59bff6d742 Post release version bump
[skip ci]
2024-04-24 08:16:01 +00:00
Tomáš Hozza
3ae8f25f55 Testutil/importlib: don't write bytecode when importing modules
Cache files will split the extension, this means that all pyc cache
files looks like we get many clashing `org.osbuild.cpython-py311.pyc
files. Moreover, the cache bytecode invalidation is based on the
timestamp (which is the same after git checkout) and the file size
(which may be the same for two different files). This means that we
can't rely on the cache files.

This issue has been found after the previous commit made the
`org.osbuild.systemd` and `org.osbuild.selinux` stages to have exactly
the same size, which caused the interpreter to reuse the bytecode for
the selinux stage when running unit tests for the systemd stage. This
resulted in consistent and weird failures when the systemd stage
options were passed to the selinux stage code.

The credit for this fix goes to Michael Vogt, who found the cause and
fix. Also thanks to Simon de Vlieger for his help with debugging the
problem.

Co-authored-by: Michael Vogt <michael.vogt@gmail.com>
Signed-off-by: Tomáš Hozza <thozza@redhat.com>
2024-04-22 16:33:59 +02:00
Michael Vogt
2586a748fd testutil: skip tests for missing ThreadingHTTPServer in py36
Only py3.7+ has ThreadingHTTPServer and SimpleHTTPRequestHandler
that can take a directory argument. We could reimplement this
on py36 (easy for threading, harder for missing directory) but
instead this commit just skips tests that try to use a
ThreadingHTTPServer.

Remove once we no longer support py3.6.
2024-04-16 15:16:49 +02:00
Michael Vogt
659f1f06f2 meta: automatically allow devices as input in the stages schemas
With the new `bootc install to-filesystem` support many stages
will need a devices/mount setup to bind mount the deployment root
from the bootc deployment root of the generated image. To make
this globally available just allow "devices/mounts" for all stages
in the schema validation.

Note that `mounts` is already globally allowed so this just adds
devices (this was added in `7e776a076` with ostree as the use-case).
Nothing will change for the filesystem stages that already define
"devices" in a more specialized way.
2024-04-16 08:04:43 +02:00
schutzbot
130ca6ad64 Post release version bump
[skip ci]
2024-04-15 09:09:06 +00:00
Gianluca Zuccarelli
3a94b35a62 osbuild: fix newline issue with final output
Add a new line to after a successful build and before the final output
is printed to the terminal. Since the final build output and the
"manifest finished successfully" line were being printed to the same
line.
2024-04-12 13:41:04 +02:00
schutzbot
e32b25a1b5 Post release version bump
[skip ci]
2024-04-10 08:17:22 +00:00