image: update mkosi and use package directory feature

This commit is contained in:
Malte Poll 2024-02-16 13:10:46 +01:00
parent 5ef12895fa
commit 889677c795
5 changed files with 45 additions and 54 deletions

View File

@ -1,6 +1,5 @@
""" Bazel rule for building mkosi images. """ """ Bazel rule for building mkosi images. """
load("@aspect_bazel_lib//lib:paths.bzl", "relative_file")
load("@bazel_skylib//lib:paths.bzl", "paths") load("@bazel_skylib//lib:paths.bzl", "paths")
def _mkosi_image_impl(ctx): def _mkosi_image_impl(ctx):
@ -9,7 +8,6 @@ def _mkosi_image_impl(ctx):
outputs = [] outputs = []
tools = [] tools = []
workdir = ctx.file.mkosi_conf.dirname workdir = ctx.file.mkosi_conf.dirname
config_rel = lambda target: relative_file(target, ctx.file.mkosi_conf.path)
env = {} env = {}
args.add("-C", workdir) args.add("-C", workdir)
if ctx.attr.distribution: if ctx.attr.distribution:
@ -19,24 +17,24 @@ def _mkosi_image_impl(ctx):
if ctx.attr.output: if ctx.attr.output:
args.add("--output", ctx.attr.output) args.add("--output", ctx.attr.output)
args.add_all(ctx.attr.packages, before_each = "--package") args.add_all(ctx.attr.packages, before_each = "--package")
for package_file in ctx.files.package_files: for package_dir in ctx.files.package_directories:
args.add("--package", config_rel(package_file.path)) args.add("--package-directory", package_dir.path)
if len(ctx.files.local_mirror) > 0: 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: 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: 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: 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: 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: for initrd in ctx.files.initrds:
inputs.append(initrd) 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.mkosi_conf)
inputs.extend(ctx.files.srcs[:]) 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.base_trees[:])
inputs.extend(ctx.files.skeleton_trees[:]) inputs.extend(ctx.files.skeleton_trees[:])
inputs.extend(ctx.files.package_manager_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 + "/"): 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)) 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) outputs.append(out_dir)
args.add("--output-dir", config_rel(out_dir.path)) args.add("--output-dir", out_dir.path)
else: 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) args.add_all(ctx.attr.extra_args)
for key, value in ctx.attr.env.items(): for key, value in ctx.attr.env.items():
args.add("--environment", "{}={}".format(key, value)) args.add("--environment", "{}={}".format(key, value))
@ -126,7 +124,7 @@ mkosi_image = rule(
"out_dir": attr.string(), "out_dir": attr.string(),
"output": attr.string(), "output": attr.string(),
"outs": attr.output_list(), "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), "package_manager_trees": attr.label_list(allow_files = True),
"packages": attr.string_list(), "packages": attr.string_list(),
"seed": attr.string(), "seed": attr.string(),

View File

@ -4,7 +4,24 @@ shopt -s inherit_errexit
export PATH=/run/wrappers/bin:/run/current-system/sw/bin:/bin:/usr/bin:/usr/local/bin export PATH=/run/wrappers/bin:/run/current-system/sw/bin:/bin:/usr/bin:/usr/local/bin
VERSION_ARG="" 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 if [[ -n ${VERSION_FILE+x} ]]; then
VERSION_ARG="--environment=IMAGE_VERSION=$(cat "${VERSION_FILE}")" VERSION_ARG="--environment=IMAGE_VERSION=$(cat "${VERSION_FILE}")"
@ -14,14 +31,12 @@ fi
if [[ -n ${LOCAL_MIRROR+x} ]]; then if [[ -n ${LOCAL_MIRROR+x} ]]; then
LOCAL_MIRROR=$(realpath "${LOCAL_MIRROR}") LOCAL_MIRROR=$(realpath "${LOCAL_MIRROR}")
reposdir=$(mktemp -d) reposdir=$(mktemp -d)
cat > "${reposdir}/mkosi.repo" << EOF # putting an empty repo file under /etc/yum.repos.d/mkosi.repo
[local-mirror] # will make mkosi use only package directories
name=local-mirror # and not try to fetch packages from the network
baseurl=file://${LOCAL_MIRROR} touch "${reposdir}/mkosi.repo"
enabled=1
gpgcheck=0
EOF
args+=("--package-manager-tree=${reposdir}:/etc/yum.repos.d") args+=("--package-manager-tree=${reposdir}:/etc/yum.repos.d")
args+=("--package-directory" "${LOCAL_MIRROR}")
fi fi
exec @@MKOSI@@ "${args[@]}" build exec @@MKOSI@@ "${args[@]}" build

View File

@ -28,17 +28,11 @@
callPackage = pkgsUnstable.callPackage; callPackage = pkgsUnstable.callPackage;
mkosiDev = (pkgsUnstable.mkosi.overrideAttrs (oldAttrs: rec { 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; [ propagatedBuildInputs = oldAttrs.propagatedBuildInputs ++ (with pkgsUnstable; [
# package management # package management
dnf5 dnf5
rpm rpm
createrepo_c
# filesystem tools # filesystem tools
squashfsTools # mksquashfs squashfsTools # mksquashfs
@ -50,6 +44,10 @@
cpio # cpio cpio # cpio
zstd # zstd zstd # zstd
xz # xz xz # xz
# utils
gnused # sed
gnugrep # grep
]); ]);
})); }));

View File

@ -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("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory")
load("@rules_pkg//:pkg.bzl", "pkg_tar") load("@rules_pkg//:pkg.bzl", "pkg_tar")
load("//bazel/mkosi:mkosi_image.bzl", "mkosi_image") load("//bazel/mkosi:mkosi_image.bzl", "mkosi_image")
@ -27,25 +26,6 @@ copy_to_directory(
replace_prefixes = {"file": ""}, 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( mkosi_image(
name = "base_" + kernel_variant, name = "base_" + kernel_variant,
@ -71,11 +51,8 @@ copy_to_directory(
local_mirror = ["@mkosi_rpms//:repo"], local_mirror = ["@mkosi_rpms//:repo"],
mkosi_conf = "mkosi.conf", mkosi_conf = "mkosi.conf",
output = kernel_variant, output = kernel_variant,
package_files = [ package_directories = [
":kernel_" + kernel_variant, ":rpms_" + kernel_variant,
":kernel_core_" + kernel_variant,
":kernel_modules_" + kernel_variant,
":kernel_modules_core_" + kernel_variant,
], ],
tags = [ tags = [
"manual", "manual",

View File

@ -15,6 +15,9 @@ Packages=systemd
dbus dbus
udev udev
util-linux util-linux
kernel
kernel-core
kernel-modules
# nvme / disk / udev tools # nvme / disk / udev tools
Packages=nvme-cli Packages=nvme-cli