Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: kargs module #388

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
14 changes: 14 additions & 0 deletions modules/kargs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# kargs

The kargs module injects kernel arguments into the image.

Kernel arguments can be used to define how kernel will interact with the hardware or software.

Instead of modifying & rebuilding the kernel, it is much easier to just input the kernel arguments & `bootc` will do its job.

You can see how `bootc` injects kernel arguments [here](https://containers.github.io/bootc/building/kernel-arguments.html).

For this reason, it is required to have `bootc` installed & used in the image.
By usage, it means that instead of `rpm-ostree update`, you need to use `bootc update` for kargs to get applied on next boot.

To see which kargs are currently applied to the system in run-time, you can issue `rpm-ostree kargs` command.
43 changes: 43 additions & 0 deletions modules/kargs/kargs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash

set -euo pipefail

if ! command -v bootc &> /dev/null; then
echo "ERROR: 'bootc' package is not installed, please install it, as it's necessary for injecting kargs."
exit 1
fi

KARGS_D="/usr/lib/bootc/kargs.d"
BLUEBUILD_TOML="${KARGS_D}/bluebuild-kargs.toml"

get_json_array KARGS 'try .["kargs"][]' "${1}"
formatted_kargs=$(printf '"%s", ' "${KARGS[@]}")
formatted_kargs=${formatted_kargs%, }

ARCH=$(echo "${1}" | jq -r 'try .["arch"]')
formatted_arch=$(echo "${ARCH}" | sed 's/[^, ]\+/"&"/g')

if [[ ${#KARGS[@]} -gt 0 ]]; then
# Make kargs.d directory in case it doesn't exist
mkdir -p "${KARGS_D}"
# If bluebuild-kargs.toml already exists from the previous module run, append a new suffixed toml file instead
if [[ -f "${BLUEBUILD_TOML}" ]]; then
counter=1
new_filename="${KARGS_D}/bluebuild-kargs-${counter}.toml"
while [[ -f "${new_filename}" ]]; do
counter=$((counter + 1))
new_filename="${KARGS_D}/bluebuild-kargs-${counter}.toml"
done
BLUEBUILD_TOML="${new_filename}"
fi
# Write kargs to toml file
echo "Writing following kernel arguments to kargs.d TOML file: ${formatted_kargs}"
echo "kargs = [${formatted_kargs}]" > "${BLUEBUILD_TOML}"
if [[ "${ARCH}" != "null" ]]; then
echo "Those kernel arguments are applied to the following specific OS architecture(s): ${formatted_arch}"
echo "match-architectures = [${formatted_arch}]" >> "${BLUEBUILD_TOML}"
fi
else
echo "ERROR: You did not include any kernel arguments to inject in the image."
exit 1
fi
21 changes: 21 additions & 0 deletions modules/kargs/kargs.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import "@typespec/json-schema";
using TypeSpec.JsonSchema;

@jsonSchema("/modules/kargs-latest.json")
model KargsModuleLatest {
...KargsModuleV1;
}

@jsonSchema("/modules/kargs-v1.json")
model KargsModuleV1 {
/** The kargs module injects kernel arguments into the image.
* https://blue-build.org/reference/modules/kargs/
*/
type: "kargs" | "kargs@v1" | "kargs@latest";

/** Defines on which OS architectures are kargs applied. Defaults to all architectures if ommited. */
`arch`?: string;

/** Kargs to inject in the image. */
`kargs`: Array<string>;
}
9 changes: 9 additions & 0 deletions modules/kargs/module.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: kargs
shortdesc: The kargs module injects kernel arguments into the image.
example: |
type: kargs
arch: x86_64, aarch64 # only injects kernel arguments to those specific OS architectures
kargs:
- console=ttyS0,114800n8
- mitigations=on
- systemd.unified_cgroup_hierarchy=0