From 37e34bcfb0e99f1c5884f0fb8e148e05dcbcf70e Mon Sep 17 00:00:00 2001 From: Malte Poll <1780588+malt3@users.noreply.github.com> Date: Fri, 16 Feb 2024 13:10:46 +0100 Subject: [PATCH] image: update mkosi and use package directory feature --- bazel/mkosi/mkosi_image.bzl | 26 ++++++++++++-------------- bazel/mkosi/mkosi_wrapper.sh.in | 31 +++++++++++++++++++++++-------- flake.nix | 12 +++++------- image/base/BUILD.bazel | 27 ++------------------------- image/base/mkosi.conf | 3 +++ 5 files changed, 45 insertions(+), 54 deletions(-) diff --git a/bazel/mkosi/mkosi_image.bzl b/bazel/mkosi/mkosi_image.bzl index 585ebeed7c7..dbb3c9a8b8e 100644 --- a/bazel/mkosi/mkosi_image.bzl +++ b/bazel/mkosi/mkosi_image.bzl @@ -1,6 +1,5 @@ """ Bazel rule for building mkosi images. """ -load("@aspect_bazel_lib//lib:paths.bzl", "relative_file") load("@bazel_skylib//lib:paths.bzl", "paths") def _mkosi_image_impl(ctx): @@ -9,7 +8,6 @@ def _mkosi_image_impl(ctx): outputs = [] tools = [] workdir = ctx.file.mkosi_conf.dirname - config_rel = lambda target: relative_file(target, ctx.file.mkosi_conf.path) env = {} args.add("-C", workdir) if ctx.attr.distribution: @@ -19,24 +17,24 @@ def _mkosi_image_impl(ctx): if ctx.attr.output: args.add("--output", ctx.attr.output) args.add_all(ctx.attr.packages, before_each = "--package") - for package_file in ctx.files.package_files: - args.add("--package", config_rel(package_file.path)) + for package_dir in ctx.files.package_directories: + args.add("--package-directory", package_dir.path) if len(ctx.files.local_mirror) > 0: - env["LOCAL_MIRROR"] = config_rel(ctx.files.local_mirror[0].dirname) + env["LOCAL_MIRROR"] = ctx.files.local_mirror[0].dirname for tree in ctx.files.base_trees: - args.add("--base-tree", config_rel(tree.path)) + args.add("--base-tree", tree.path) for tree in ctx.files.skeleton_trees: - args.add("--skeleton-tree", config_rel(tree.path)) + args.add("--skeleton-tree", tree.path) for tree in ctx.files.package_manager_trees: - args.add("--package-manager_tree", config_rel(tree.path)) + args.add("--package-manager-tree", tree.path) for tree in ctx.files.extra_trees: - args.add("--extra-tree", config_rel(tree.path)) + args.add("--extra-tree", tree.path) for initrd in ctx.files.initrds: inputs.append(initrd) - args.add("--initrd", config_rel(initrd.path)) + args.add("--initrd", initrd.path) inputs.extend(ctx.files.mkosi_conf) inputs.extend(ctx.files.srcs[:]) - inputs.extend(ctx.files.package_files[:]) + inputs.extend(ctx.files.package_directories[:]) inputs.extend(ctx.files.base_trees[:]) inputs.extend(ctx.files.skeleton_trees[:]) inputs.extend(ctx.files.package_manager_trees[:]) @@ -59,9 +57,9 @@ def _mkosi_image_impl(ctx): if output.is_directory and out_dir.path.startswith(output.path + "/"): fail("output directory {} is nested within output directory {}; outputs cannot be nested within each other!".format(out_dir.path, output.path)) outputs.append(out_dir) - args.add("--output-dir", config_rel(out_dir.path)) + args.add("--output-dir", out_dir.path) else: - args.add("--output-dir", config_rel(paths.join(ctx.bin_dir.path, ctx.label.package))) + args.add("--output-dir", paths.join(ctx.bin_dir.path, ctx.label.package)) args.add_all(ctx.attr.extra_args) for key, value in ctx.attr.env.items(): args.add("--environment", "{}={}".format(key, value)) @@ -126,7 +124,7 @@ mkosi_image = rule( "out_dir": attr.string(), "output": attr.string(), "outs": attr.output_list(), - "package_files": attr.label_list(allow_files = True), + "package_directories": attr.label_list(allow_files = True), "package_manager_trees": attr.label_list(allow_files = True), "packages": attr.string_list(), "seed": attr.string(), diff --git a/bazel/mkosi/mkosi_wrapper.sh.in b/bazel/mkosi/mkosi_wrapper.sh.in index c8e541a0b3b..bd1eb07aeaa 100644 --- a/bazel/mkosi/mkosi_wrapper.sh.in +++ b/bazel/mkosi/mkosi_wrapper.sh.in @@ -4,7 +4,24 @@ shopt -s inherit_errexit export PATH=/run/wrappers/bin:/run/current-system/sw/bin:/bin:/usr/bin:/usr/local/bin VERSION_ARG="" -args=("$@") +args=() + +while [[ $# -gt 0 ]]; do + key="$1" + case $key in + --*-tree|--initrd|--package-directory|--output-dir) + # absolutize any file paths + shift # past the key and to the value + value="$1" + args+=("${key}" "$(realpath "${value}")") + shift # past the value + ;; + *) + args+=("$1") + shift + ;; + esac +done if [[ -n ${VERSION_FILE+x} ]]; then VERSION_ARG="--environment=IMAGE_VERSION=$(cat "${VERSION_FILE}")" @@ -14,14 +31,12 @@ fi if [[ -n ${LOCAL_MIRROR+x} ]]; then LOCAL_MIRROR=$(realpath "${LOCAL_MIRROR}") reposdir=$(mktemp -d) - cat > "${reposdir}/mkosi.repo" << EOF -[local-mirror] -name=local-mirror -baseurl=file://${LOCAL_MIRROR} -enabled=1 -gpgcheck=0 -EOF + # putting an empty repo file under /etc/yum.repos.d/mkosi.repo + # will make mkosi use only package directories + # and not try to fetch packages from the network + touch "${reposdir}/mkosi.repo" args+=("--package-manager-tree=${reposdir}:/etc/yum.repos.d") + args+=("--package-directory" "${LOCAL_MIRROR}") fi exec @@MKOSI@@ "${args[@]}" build diff --git a/flake.nix b/flake.nix index 9dede33e4d8..3f1d265db65 100644 --- a/flake.nix +++ b/flake.nix @@ -26,17 +26,11 @@ callPackage = pkgsUnstable.callPackage; mkosiDev = (pkgsUnstable.mkosi.overrideAttrs (oldAttrs: rec { - # TODO(malt3): remove patch once merged and released upstream (systemd/mkosi#2163) - src = pkgsUnstable.fetchFromGitHub { - owner = "systemd"; - repo = "mkosi"; - rev = "abf22cdc6ccb13f2cd84679ede77231455ec6813"; - hash = "sha256-njtYWSXSLMcn6AtGfAeL/ncZQ6g+Vgpe7EaKLkzAOl4="; - }; propagatedBuildInputs = oldAttrs.propagatedBuildInputs ++ (with pkgsUnstable; [ # package management dnf5 rpm + createrepo_c # filesystem tools squashfsTools # mksquashfs @@ -48,6 +42,10 @@ cpio # cpio zstd # zstd xz # xz + + # utils + gnused # sed + gnugrep # grep ]); })); diff --git a/image/base/BUILD.bazel b/image/base/BUILD.bazel index 7dc5215e08b..9028b83761f 100644 --- a/image/base/BUILD.bazel +++ b/image/base/BUILD.bazel @@ -1,4 +1,3 @@ -load("@aspect_bazel_lib//lib:copy_file.bzl", "copy_file") load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory") load("@rules_pkg//:pkg.bzl", "pkg_tar") load("//bazel/mkosi:mkosi_image.bzl", "mkosi_image") @@ -27,25 +26,6 @@ copy_to_directory( replace_prefixes = {"file": ""}, ) -[ - copy_file( - name = name, - src = "@" + name + "//file", - out = name + ".rpm", - allow_symlink = True, - ) - for name in [ - "kernel_lts", - "kernel_core_lts", - "kernel_modules_lts", - "kernel_modules_core_lts", - "kernel_mainline", - "kernel_core_mainline", - "kernel_modules_mainline", - "kernel_modules_core_mainline", - ] -] - [ mkosi_image( name = "base_" + kernel_variant, @@ -71,11 +51,8 @@ copy_to_directory( local_mirror = ["@mkosi_rpms//:repo"], mkosi_conf = "mkosi.conf", output = kernel_variant, - package_files = [ - ":kernel_" + kernel_variant, - ":kernel_core_" + kernel_variant, - ":kernel_modules_" + kernel_variant, - ":kernel_modules_core_" + kernel_variant, + package_directories = [ + ":rpms_" + kernel_variant, ], tags = [ "manual", diff --git a/image/base/mkosi.conf b/image/base/mkosi.conf index 4ee2f73fcf7..736665422f0 100644 --- a/image/base/mkosi.conf +++ b/image/base/mkosi.conf @@ -15,6 +15,9 @@ Packages=systemd dbus udev util-linux + kernel + kernel-core + kernel-modules # nvme / disk / udev tools Packages=nvme-cli