particle-os-modules/modules/kargs/kargs.sh
fiftydinar 734656f7cc
feat: kargs module (#388)
* feat: `kargs` module

* fix: ARCH conditions

* docs: Explain arch in module.yml

* docs: Refine kargs documentation

Co-authored-by: xyny <60004820+xynydev@users.noreply.github.com>

* chore: Add `kargs` to `modules.json`

* docs: Add info about discovering possible kargs

* docs: Use master branch link for kargs

* docs: Some grammar style fix

* docs: Add note about `bootc switch` for kargs to get applied

* chore: slight grammar change

* chore: small grammar change

---------

Co-authored-by: xyny <60004820+xynydev@users.noreply.github.com>
2025-04-27 08:45:30 +02:00

43 lines
1.5 KiB
Bash

#!/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