tools: Fix shellcheck issues in initrd_builder.sh

Fix shellcheck warnings and notes identified by running
shellcheck --severity=style.

Signed-off-by: Fabiano Fidêncio <ffidencio@nvidia.com>
This commit is contained in:
Fabiano Fidêncio
2026-04-21 18:38:02 +02:00
parent ea6c77bd5e
commit a5a5f38630

View File

@@ -4,22 +4,25 @@
#
# SPDX-License-Identifier: Apache-2.0
[ -z "${DEBUG}" ] || set -x
[[ -z "${DEBUG}" ]] || set -x
set -o errexit
# set -o nounset
set -o pipefail
script_name="${0##*/}"
script_dir="$(dirname $(readlink -f $0))"
script_dir="$(dirname "$(readlink -f "$0")")"
lib_file="${script_dir}/../scripts/lib.sh"
source "$lib_file"
# shellcheck source=../scripts/lib.sh
# shellcheck disable=SC1091
source "${lib_file}"
INITRD_IMAGE="${INITRD_IMAGE:-kata-containers-initrd.img}"
AGENT_BIN=${AGENT_BIN:-kata-agent}
AGENT_INIT=${AGENT_INIT:-no}
# shellcheck disable=SC2120
usage()
{
error="${1:-0}"
@@ -44,41 +47,43 @@ exit "${error}"
while getopts "ho:" opt
do
case "$opt" in
case "${opt}" in
h) usage ;;
o) INITRD_IMAGE="${OPTARG}" ;;
*) usage 1 ;;
esac
done
shift $(( $OPTIND - 1 ))
shift $(( OPTIND - 1 ))
ROOTFS="$1"
[ -n "${ROOTFS}" ] || usage
[ -d "${ROOTFS}" ] || die "${ROOTFS} is not a directory"
# shellcheck disable=SC2119
[[ -n "${ROOTFS}" ]] || usage
[[ -d "${ROOTFS}" ]] || die "${ROOTFS} is not a directory"
ROOTFS=$(readlink -f ${ROOTFS})
IMAGE_DIR=$(dirname ${INITRD_IMAGE})
IMAGE_DIR=$(readlink -f ${IMAGE_DIR})
IMAGE_NAME=$(basename ${INITRD_IMAGE})
ROOTFS=$(readlink -f "${ROOTFS}")
IMAGE_DIR=$(dirname "${INITRD_IMAGE}")
IMAGE_DIR=$(readlink -f "${IMAGE_DIR}")
IMAGE_NAME=$(basename "${INITRD_IMAGE}")
# The kata rootfs image expects init to be installed
init="${ROOTFS}/sbin/init"
[ -x "${init}" ] || [ -L ${init} ] || die "/sbin/init is not installed in ${ROOTFS}"
[[ -x "${init}" ]] || [[ -L "${init}" ]] || die "/sbin/init is not installed in ${ROOTFS}"
OK "init is installed"
[ "${AGENT_INIT}" == "yes" ] || [ -x "${ROOTFS}/usr/bin/${AGENT_BIN}" ] || \
[[ "${AGENT_INIT}" == "yes" ]] || [[ -x "${ROOTFS}/usr/bin/${AGENT_BIN}" ]] || \
die "/usr/bin/${AGENT_BIN} is not installed in ${ROOTFS}
use AGENT_BIN env variable to change the expected agent binary name"
OK "Agent is installed"
# initramfs expects /init, create symlink only if ${ROOTFS}/init does not exist
# Init may be provided by other packages, e.g. systemd or GPU initrd/rootfs
if [ ! -x "${ROOTFS}/init" ] && [ ! -L "${ROOTFS}/init" ]; then
if [[ ! -x "${ROOTFS}/init" ]] && [[ ! -L "${ROOTFS}/init" ]]; then
# ATTN: In some instances, /init is not following two or more levels of symlinks
# i.e. (/init to /sbin/init to /lib/systemd/systemd)
# Setting /init directly to /lib/systemd/systemd when AGENT_INIT is disabled
if [ "${AGENT_INIT}" = "yes" ]; then
if [[ "${AGENT_INIT}" = "yes" ]]; then
sudo ln -sf /sbin/init "${ROOTFS}/init"
else
sudo ln -sf /lib/systemd/systemd "${ROOTFS}/init"