From e90891059b035fa3dc7aa0887e4db1779cc6be65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabiano=20Fid=C3=AAncio?= Date: Thu, 16 Mar 2023 12:59:40 +0100 Subject: [PATCH] tools: Add support for caching QEMU artefacts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Let's add support for caching QEMU artefacts that are generated using the kata-deploy local-build scripts. Right now those are not used, but we'll switch to using them very soon as part of upcoming changes of how we build the components we test in our CI. Fixes: #6480 Signed-off-by: Fabiano FidĂȘncio Signed-off-by: Gabriela Cervantes --- tools/packaging/scripts/lib.sh | 41 +++++++++++++++++++ .../static-build/cache_components_main.sh | 17 +++++++- .../static-build/qemu/build-base-qemu.sh | 2 +- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/tools/packaging/scripts/lib.sh b/tools/packaging/scripts/lib.sh index deece4ede9..6f006c1f9e 100644 --- a/tools/packaging/scripts/lib.sh +++ b/tools/packaging/scripts/lib.sh @@ -134,3 +134,44 @@ get_kernel_image_name() { kernel_script_dir="${repo_root_dir}/tools/packaging/static-build/kernel" echo "${BUILDER_REGISTRY}:kernel-$(get_last_modification ${kernel_script_dir})-$(uname -m)" } + +sha256sum_from_files() { + local files_in=${@:-} + local files="" + local shasum="" + + # Process the input files: + # - discard the files/directories that don't exist. + # - find the files if it is a directory + for f in $files_in; do + if [ -d "$f" ]; then + files+=" $(find $f -type f)" + elif [ -f "$f" ]; then + files+=" $f" + fi + done + # Return in case there is none input files. + [ -n "$files" ] || return 0 + + # Alphabetically sorting the files. + files="$(echo $files | tr ' ' '\n' | LC_ALL=C sort -u)" + # Concate the files and calculate a hash. + shasum="$(cat $files | sha256sum -b)" || true + if [ -n "$shasum" ];then + # Return only the SHA field. + echo $(awk '{ print $1 }' <<< $shasum) + fi +} + +calc_qemu_files_sha256sum() { + local files="${repo_root_dir}/tools/packaging/qemu \ + ${repo_root_dir}/tools/packaging/static-build/qemu.blacklist \ + ${repo_root_dir}/tools/packaging/static-build/scripts" + + sha256sum_from_files "$files" +} + +get_qemu_image_name() { + qemu_script_dir="${repo_root_dir}/tools/packaging/static-build/qemu" + echo "${BUILDER_REGISTRY}:qemu-$(get_last_modification ${qemu_script_dir})-$(uname -m)" +} diff --git a/tools/packaging/static-build/cache_components_main.sh b/tools/packaging/static-build/cache_components_main.sh index 84ca73a0be..0136d1db05 100755 --- a/tools/packaging/static-build/cache_components_main.sh +++ b/tools/packaging/static-build/cache_components_main.sh @@ -40,6 +40,14 @@ cache_nydus_artifacts() { create_cache_asset "${nydus_tarball_name}" "${current_nydus_version}" "" } +cache_qemu_artifacts() { + local qemu_tarball_name="kata-static-qemu.tar.xz" + local current_qemu_version=$(get_from_kata_deps "assets.hypervisor.qemu.version") + local qemu_sha=$(calc_qemu_files_sha256sum) + local current_qemu_image="$(get_qemu_image_name)" + create_cache_asset "${qemu_tarball_name}" "${current_qemu_version}-${qemu_sha}" "${current_qemu_image}" +} + create_cache_asset() { local component_name="${1}" local component_version="${2}" @@ -67,6 +75,7 @@ Usage: $0 "[options]" * Export KERNEL_FLAVOUR="kernel|kernek-experimental|kernel-arm-experimental|kernel-dragonball-experimental" for a specific build The default KERNEL_FLAVOUR value is "kernel" -n Nydus cache + -q QEMU cache -h Shows help EOF )" @@ -77,8 +86,9 @@ main() { local firecracker_component="${firecracker_component:-}" local kernel_component="${kernel_component:-}" local nydus_component="${nydus_component:-}" + local qemu_component="${qemu_component:-}" local OPTIND - while getopts ":cFknh:" opt + while getopts ":cFknqh:" opt do case "$opt" in c) @@ -93,6 +103,9 @@ main() { n) nydus_component="1" ;; + q) + qemu_component="1" + ;; h) help exit 0; @@ -110,6 +123,7 @@ main() { [[ -z "${firecracker_component}" ]] && \ [[ -z "${kernel_component}" ]] && \ [[ -z "${nydus_component}" ]] && \ + [[ -z "${qemu_component}" ]] && \ help && die "Must choose at least one option" mkdir -p "${WORKSPACE}/artifacts" @@ -120,6 +134,7 @@ main() { [ "${firecracker_component}" == "1" ] && cache_firecracker_artifacts [ "${kernel_component}" == "1" ] && cache_kernel_artifacts [ "${nydus_component}" == "1" ] && cache_nydus_artifacts + [ "${qemu_component}" == "1" ] && cache_qemu_artifacts ls -la "${WORKSPACE}/artifacts/" popd diff --git a/tools/packaging/static-build/qemu/build-base-qemu.sh b/tools/packaging/static-build/qemu/build-base-qemu.sh index 55ab71d35e..9767e5d548 100755 --- a/tools/packaging/static-build/qemu/build-base-qemu.sh +++ b/tools/packaging/static-build/qemu/build-base-qemu.sh @@ -38,7 +38,7 @@ CACHE_TIMEOUT=$(date +"%Y-%m-%d") [ -n "${build_suffix}" ] && HYPERVISOR_NAME="kata-qemu-${build_suffix}" || HYPERVISOR_NAME="kata-qemu" [ -n "${build_suffix}" ] && PKGVERSION="kata-static-${build_suffix}" || PKGVERSION="kata-static" -container_image="${QEMU_CONTAINER_BUILDER:-${BUILDER_REGISTRY}:qemu-$(get_last_modification ${script_dir})-$(uname -m)}" +container_image="${QEMU_CONTAINER_BUILDER:-$(get_qemu_image_name)}" sudo docker pull ${container_image} || (sudo "${container_engine}" build \ --build-arg CACHE_TIMEOUT="${CACHE_TIMEOUT}" \