When running kas / bitbake inside the container, the printed paths point
into the container instead of the host. While this is technically
correct, it makes debugging harder as the user manually has to map these
back to the ones on the host (e.g. on build errors).
We now add the option --rewrite. When enabled, the output of the scripts
inside the container is captured and all mapped paths are rewritten. We
do not globally enable it, as capturing the output interfers with TTYs
(like kas shell, kas menu, oe menuconfig).
Signed-off-by: Felix Moessbauer <
felix.mo...@siemens.com>
---
kas-container | 65 +++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 63 insertions(+), 2 deletions(-)
diff --git a/kas-container b/kas-container
index a9311e462..cb33ed321 100755
--- a/kas-container
+++ b/kas-container
@@ -85,6 +85,8 @@ usage()
"container.\n"
printf "%b" "--git-credential-store\tFile path to the git credential " \
"store\n"
+ printf "%b" "--rewrite\t\tRewrite container paths to host paths in output\n" \
+ "\t\t\t(may break interactive TTYs inside the container).\n"
printf "%b" "--no-proxy-from-env\tDo not inherit proxy settings from " \
"environment.\n"
printf "%b" "--repo-ro\t\tMount current repository read-only\n" \
@@ -256,8 +258,10 @@ forward_dir()
[ -z "$_varval" ] && return
FW_DIR_REL=$(realpath -q --relative-base="${KAS_WORK_DIR}" "$_varval")
if [ "${FW_DIR_REL}" = "$_varval" ]; then
+ register_path_rewrite "$_varval" "$2"
KAS_RUNTIME_ARGS="${KAS_RUNTIME_ARGS} -v ${FW_DIR_REL}:$2:$3 -e $1=$2"
else
+ register_path_rewrite "$_varval" "/work/${FW_DIR_REL}"
KAS_RUNTIME_ARGS="${KAS_RUNTIME_ARGS} -e $1=/work/${FW_DIR_REL}"
fi
}
@@ -284,14 +288,60 @@ enable_docker_rootless()
}
KAS_GIT_OVERLAY_FILE=""
+KAS_OUTPUT_FILTER_DIR=""
+KAS_OUTPUT_FILTER_PIDS=""
+KAS_OUTPUT_FILTER_RULES=""
kas_container_cleanup()
{
+ if [ -n "${KAS_OUTPUT_FILTER_PIDS}" ]; then
+ # shellcheck disable=SC2086
+ kill ${KAS_OUTPUT_FILTER_PIDS} 2>/dev/null || true
+ fi
+ if [ -n "${KAS_OUTPUT_FILTER_DIR}" ]; then
+ rm -rf "${KAS_OUTPUT_FILTER_DIR}"
+ fi
if [ -f "${KAS_GIT_OVERLAY_FILE}" ]; then
trace rm -f "${KAS_GIT_OVERLAY_FILE}"
fi
}
trap kas_container_cleanup EXIT INT TERM
+register_path_rewrite()
+{
+ _esc_from=$(printf '%s\n' "$2" | sed 's/[\\&|]/\\&/g')
+ _esc_to=$(printf '%s\n' "$1" | sed 's/[\\&|]/\\&/g')
+ KAS_OUTPUT_FILTER_RULES="${KAS_OUTPUT_FILTER_RULES}
+s|${_esc_from}|${_esc_to}|g"
+}
+
+run_with_host_path_rewrite()
+{
+ KAS_OUTPUT_FILTER_DIR=$(mktemp -d)
+ _RULES="${KAS_OUTPUT_FILTER_DIR}/rewrite.sed"
+ _STDOUT_FIFO="${KAS_OUTPUT_FILTER_DIR}/stdout"
+ _STDERR_FIFO="${KAS_OUTPUT_FILTER_DIR}/stderr"
+
+ printf '%s\n' "${KAS_OUTPUT_FILTER_RULES}" | sed '/^$/d' > "${_RULES}"
+ mkfifo "${_STDOUT_FIFO}" "${_STDERR_FIFO}"
+
+ sed -u -f "${_RULES}" < "${_STDOUT_FIFO}" &
+ _STDOUT_PID=$!
+ sed -u -f "${_RULES}" < "${_STDERR_FIFO}" >&2 &
+ _STDERR_PID=$!
+ KAS_OUTPUT_FILTER_PIDS="${_STDOUT_PID} ${_STDERR_PID}"
+
+ _RET=0
+ # shellcheck disable=SC2086
+ trace ${KAS_CONTAINER_COMMAND} run "$@" \
+ > "${_STDOUT_FIFO}" 2> "${_STDERR_FIFO}" || _RET=$?
+
+ wait "${_STDOUT_PID}" || true
+ wait "${_STDERR_PID}" || true
+ KAS_OUTPUT_FILTER_PIDS=""
+
+ return "${_RET}"
+}
+
set_container_image_var()
{
# if the image is explicitly set, use that
@@ -410,6 +460,10 @@ while [ $# -gt 0 ]; do
KAS_GIT_CREDENTIAL_STORE="$2"
shift 2
;;
+ --rewrite)
+ KAS_REWRITE=1
+ shift 1
+ ;;
--no-proxy-from-env)
KAS_NO_PROXY_FROM_ENV=1
shift 1
@@ -622,6 +676,8 @@ forward_dir DL_DIR "/downloads" "rw"
forward_dir KAS_REPO_REF_DIR "/repo-ref" "rw"
forward_dir SSTATE_DIR "/sstate" "rw"
forward_dir KAS_BUILDTOOLS_DIR "/buildtools" "rw"
+register_path_rewrite "${KAS_WORK_DIR}" "/work"
+register_path_rewrite "${KAS_REPO_DIR}" "/repo"
if git_com_dir=$(git -C "${KAS_REPO_DIR}" rev-parse --git-common-dir 2>/dev/null) \
&& [ "$git_com_dir" != "$(git -C "${KAS_REPO_DIR}" rev-parse --git-dir)" ]; then
@@ -770,5 +826,10 @@ while [ $KAS_EXTRA_BITBAKE_ARGS -gt 0 ]; do
KAS_EXTRA_BITBAKE_ARGS=$((KAS_EXTRA_BITBAKE_ARGS - 1))
done
-# shellcheck disable=SC2086
-trace ${KAS_CONTAINER_COMMAND} run "$@"
+if [ -n "${KAS_REWRITE}" ] && ! [ -t 1 ]; then
+ # shellcheck disable=SC2086
+ run_with_host_path_rewrite "$@"
+else
+ # shellcheck disable=SC2086
+ trace ${KAS_CONTAINER_COMMAND} run "$@"
+fi
--
2.53.0