Rework the interface of finalize_host_scripts() to take a dictionary
We would create <chroot> in the calling function, then pass it to
finalize_host_scripts(), which would rename it during installation to "mkosi-chroot".
This is too much magic: when looking for 'mkosi-chroot' in the code, the name appears
in one place, and the definition in a completely different place. It's also hard to
extend with other commands in the future. Let's use the proper name from the
beginning and switch to a dictionary, so we can add other helpers in the future.
Backslashes were required in old python versions, but thankfully they are not
anymore. Even though we used the continuation backslashes, we already had
parenthesis everywhere, so let's just drop the backslashes, and also use a
trailing comma per the usual style.
(https://github.com/python/cpython/issues/56991 says that this happened in
Python 3.10, but it also works with Python 3.9.18 here. I guess the CI will
provide the definite confirmation.)
mkosi-chroot: just run shell if no arguments are specified
chroot(1) is documented to run "$SHELL -i" as the default command. Let's do
something similar and call "sh -i". When a user is using '--debug-shell' and one
of the scripts fails, the mkosi-chroot can be used to chroot into the image.
I think this is what users expect, based on the 'chroot' in the name.
(I don't think using $SHELL makes sense. It could either be set to 'sh', or to
something from the outside, which might not even be installed in the chroot. We
call 'sh' ourselves, so we know it must be there, so let's just call that
uncoditionally.)
Daan De Meyer [Sat, 28 Oct 2023 19:38:37 +0000 (21:38 +0200)]
Don't mount our own passwd when building images
It's much more important that we use the image's /etc/passwd instead
of our own. Both inside and outside the image so that UIDs/GIDs get
resolved correctly.
Daan De Meyer [Sat, 28 Oct 2023 15:49:14 +0000 (17:49 +0200)]
Extend README instructions
Let's make sure we have complete copy-paste shell commands for
all the different installation methods. Let's also avoid confusion
and always use python3 in the example commands.
Daan De Meyer [Sat, 28 Oct 2023 10:42:58 +0000 (12:42 +0200)]
Add BuildSources= match
One pattern I've started using a lot when I have to build multiple
projects from source in a build script is to check if a source tree
has been mounted at some location using BuildSources= and to only
build the project if that's the case.
The problem with this is that this only allows me to skip the build,
it doesn't allow me to skip installing the necessary build and runtime
packages for that particular project.
Let's add a BuildSources= match so that everything related to the
project can be skipped if the project is not configured to be mounted
at some location using BuildSources=.
Daan De Meyer [Sat, 28 Oct 2023 11:33:40 +0000 (13:33 +0200)]
Make sure the chroot scripts dir variable is not overwritten
We used `d` for both the chroot scripts dir and the host scripts dir,
which means the host scripts dir overwrites the chroot scripts dir
variable. This isn't a problem when there's only one script, but
becomes an issue when there's multiple scripts of the same type.
Daan De Meyer [Fri, 20 Oct 2023 19:21:18 +0000 (21:21 +0200)]
Stop using subdirectories of cache and build dirs automatically
In some cases, for example mkosi-initrd running as a kernel-install
script, we want to reuse the system package cache. Currently this is
impossible as we unconditionally create a subdirectory beneath the
provided cache directory. Let's stop doing that, as users can now
explicitly configure this behavior themselves by specifying the
cache directory or build directory as follows:
Additionally, make sure the default tools tree only reuses the same
cache as the preset it's used for when the distribution, release and
architecture are the same as the preset's.
Daan De Meyer [Fri, 27 Oct 2023 19:17:27 +0000 (21:17 +0200)]
Parse mkosi.local.conf before parsing any other files
Currently extra local configuration is a bit implicit, users have
to add a drop-in in mkosi.conf.d/ named 00-local.conf or so and add
that to the gitignore. This file will then unconditionally override
settings from mkosi.conf even if that's not intended.
Let's make local configuration a bit more explicit by parsing
mkosi.local.conf in a directory first before parsing anything else.
This makes local configuration easy to detect and grep for, and
makes sure that local configuration is by default overridden by
non-local configuration.
Daan De Meyer [Fri, 27 Oct 2023 12:22:09 +0000 (14:22 +0200)]
Make mkosi.repart/ append to the default value for RepartDirectories=
While for other list based settings that have a canonical path, we
generally want to append them all to the user provided values, for
RepartDirectories=, it probably makes more sense that if the user
specifies it explicitly, it overrides all the directories defined by
the project in mkosi.repart/ directories. Let's accomodate this by
having mkosi.repart/ modify the default value for RepartDirectories=
which is only used if the setting is not set explicitly.
Daan De Meyer [Fri, 27 Oct 2023 13:41:41 +0000 (15:41 +0200)]
Fail when CLI configuration is not applied to any image
Currently we silently ignore settings configured via the CLI that
are overridden by image configuration files. Let's instead fail with
a descriptive error message when this happens, indicating users that
they should use '@' in their configuration files to set the default
value for the setting if they want to allow overriding it from the
command line.
Daan De Meyer [Fri, 27 Oct 2023 09:19:27 +0000 (11:19 +0200)]
Don't use configured default value when empty string is assigned
Currently, if a setting is assigned the empty string on the CLI, any
default value configured in the config file is still used. Let's change
this and not use any configured default value when the empty string is
assigned so that default values configured in config files can be
overridden from the CLI.
Daan De Meyer [Thu, 26 Oct 2023 14:04:36 +0000 (16:04 +0200)]
action: Make logic for unprivileged KVM access more robust
- Copy static-nodes-permissions.conf to /etc before modifying so our
modifications don't get overwritten if systemd is updated.
- Add udev rules to set the permissions correctly as well
Daan De Meyer [Mon, 23 Oct 2023 20:33:32 +0000 (22:33 +0200)]
Fix missing build overlay
We also need a build overlay if there are prepare scripts but no
build packages, so let's replace need_build_packages() with a new
function need_build_overlay() that also checks for that.
Daan De Meyer [Sun, 22 Oct 2023 18:26:12 +0000 (20:26 +0200)]
Rename Presets to Images
Presets doesn't really have any meaning without searching up its
definition. By just using mkosi.images/, it's much clearer that the
directory contains image definitions compared to mkosi.presets/.
The old names are kept intact for backwards compatibility.
Daan De Meyer [Sun, 22 Oct 2023 13:53:22 +0000 (15:53 +0200)]
Add support for profiles
A profile is a set of configuration options that represents a known
variant of the given image. Its primary purpose is to allow grouping
known useful derivatives of the current image under a recognizable
identifier.
The difference with presets is that presets represent the individual
images that might need to be built to complete the final image, whereas
profiles change how the individual presets are built. Specifically, only
ever one profile can be selected, while it's perfectly valid to build
many presets.
We parse the specified profile after mkosi.conf but before
mkosi.conf.d to allow configuring the profile to build in mkosi.conf.
Daan De Meyer [Fri, 20 Oct 2023 13:41:52 +0000 (15:41 +0200)]
Drop verb and cmdline from summary
The summary is only printed when we use the summary verb which doesn't
take command line arguments, so both of these are self evident and will
always have the same value.
Daan De Meyer [Fri, 20 Oct 2023 09:03:02 +0000 (11:03 +0200)]
qemu: Be smarter when a kernel is passed in by the user
Depending on whether the user passes us a kernel that's a UKI, PE
binary or raw Linux image, we have to change our behavior. We now
do the following based on the kernel type we're about to boot:
- If we get passed in a UKI or PE kernel, we use UEFI firmware,
otherwise we use qemu direct kernel boot
- If the kernel is a UKI and SMBIOS is available, we don't use
-append, otherwise we do
- If we do direct kernel boot, we always synthesize a root= argument
if needed.
Daan De Meyer [Thu, 19 Oct 2023 11:57:11 +0000 (13:57 +0200)]
Add git script to set safe.directory=* whenever git is executed
Instead of requiring every git command to be executed as the user
invoking mkosi, let's add a git script that appends
"-c safe.directory=*" to disable the git safe directory check. This
also reworks the script machinery to support scripts both inside and
outside of the chroot since the git script needs to be available in both.
Daan De Meyer [Thu, 19 Oct 2023 10:27:43 +0000 (12:27 +0200)]
Don't store default initrd artifacts in output directory
We don't use these for anything and the initrd can already be
accessed using the split initrd, so let's not store the default
initrd artifacts in the output directory.
Daan De Meyer [Wed, 18 Oct 2023 10:00:53 +0000 (12:00 +0200)]
Beef up bundled images
Let's beef up the bundled images with all the packages from the tools
trees where applicable so that we can run easily run mkosi from these
images if needed for testing on different distributions.
Daan De Meyer [Wed, 18 Oct 2023 10:01:26 +0000 (12:01 +0200)]
Use with in spawn()
If we yield the Popen, we yield a context manager from a context
manager, which becomes hard to follow, since we'll only enter the
outer context manager, and not the inner Popen context manager. To
make things simpler, let's enter the Popen context manager in
spawn() itself.
Daan De Meyer [Wed, 18 Oct 2023 07:30:11 +0000 (09:30 +0200)]
Reword readme
Let's reduce the amount of detail we put in the readme and list the
installation methods in order of how easy-to-use they are. Let's also
only recommend using the distribution's package if it's sufficiently
recent.
Daan De Meyer [Tue, 17 Oct 2023 20:42:20 +0000 (22:42 +0200)]
Rework qemu device support checks
Let's implement an available() method on the QemuDeviceNode enum
and move the checks from load_config() to run_qemu() so they don't
impede showing the summary or other verbs.
Let's also prefer using the file descriptor as a check whether the
feature is available in run_qemu() instead of calling the available()
method, as by the time we get to run_qemu() the available() method
might return a different result.
Malte Poll [Tue, 17 Oct 2023 08:37:16 +0000 (10:37 +0200)]
make_tar: do not emit extended PAX headers for atime, ctime and mtime
The use of the --xattrs flag implies PAX headers (--format=pax).
In this mode, the tar will record atime, ctime and mtime as PAX headers (in addition to the usual USTAR header).
Removing the headers makes the output reproducible.
See also: https://www.gnu.org/software/tar/manual/html_node/PAX-keywords.html
Daan De Meyer [Fri, 13 Oct 2023 11:24:44 +0000 (13:24 +0200)]
Open qemu device nodes before unsharing user namespace
Where possible, we should open the qemu device nodes before we unshare
the user namespace as this might not be possible anymore after unsharing
the user namespace because we might lose access to the kvm group.
Currently this is only possible for /dev/vhost-vsock. I've opened
https://gitlab.com/qemu-project/qemu/-/issues/1936 to hopefully make
it work for /dev/kvm as well.
This is the default in the pacman.conf arch ships (https://gitlab.archlinux.org/archlinux/packaging/packages/pacman/-/blob/main/pacman.conf?ref_type=heads#L42)
and allows to build AUR packages in build scripts and installing them in
the final image.
Daan De Meyer [Thu, 12 Oct 2023 12:34:12 +0000 (14:34 +0200)]
Run qemu as the invoking user again
This commit also reworks InvokingUser to calculate all its members
on module import (when we haven't yet unshared the user namespace).
become_root() is also changed to modify the InvokingUser object
instead of returning the new uid, gid. Finally, we stop passing
around uid, gid everywhere and just use the InvokingUser object
directly as a singleton.
We also stop dropping privileges in mkosi itself. Instead, we prefer
running ssh, qemu and the embedded web server unprivileged. This
allows us to get rid of the logic to not unmount the last tools tree
as we will now always still have enough privileges to do so.
We also start passing file descriptors to swtpm and virtiofsd to avoid
race conditions where the socket hasn't been created yet before we
pass it to qemu or before we try to chown it.
Daan De Meyer [Fri, 13 Oct 2023 07:54:39 +0000 (09:54 +0200)]
Add WithRecommends=
While this can already be configured using dropins. The concept of
recommended packages seems widespread enough that we can provide an
option to enable/disable it via the configuration file.
Do not remount build root as ro in build chroot
The PR #1970 added an additional volatile overlay to the buildroot, which
currently can only be used from the host, i.e. without mkosi-chroot.
Once mkosi-chroot is run, the build overlay is readonly again.
Fixes https://github.com/systemd/mkosi/issues/1974.
Daan De Meyer [Wed, 11 Oct 2023 11:33:00 +0000 (13:33 +0200)]
Mount volatile overlay when running build scripts
When building multiple projects, it might be needed to make the
header files produced by an earlier build available to later builds.
Let's make this possible by not making the root directory read-only
but instead mounting a writable overlay on top of it so that all
changes made while running the build scripts are thrown away at the
end.
Daan De Meyer [Wed, 11 Oct 2023 09:27:30 +0000 (11:27 +0200)]
Run scripts with ".chroot" extension inside the image
Our current approach to running scripts inside the image is only
really applicable to shell scripts. Let's make it easier to run
scripts written in arbitrary languages inside the image by running
scripts with the ".chroot" extension inside the image.