diff --git a/tools/packaging/static-build/cache_components.sh b/tools/packaging/static-build/cache_components.sh index 659551359..6bd295ec1 100755 --- a/tools/packaging/static-build/cache_components.sh +++ b/tools/packaging/static-build/cache_components.sh @@ -39,6 +39,28 @@ cache_clh_artifacts() { echo "${current_cloud_hypervisor_version}" > "latest" } +cache_kernel_artifacts() { + local current_kernel_version=$(get_from_kata_deps "assets.kernel.version" | cut -c2- ) + local gral_path="$(echo $script_dir | sed 's,/*[^/]\+/*$,,' | sed 's,/*[^/]\+/*$,,' | sed 's,/*[^/]\+/*$,,')" + local kernel_config_file="${gral_path}/tools/packaging/kernel/kata_config_version" + local kernel_config="$(cat ${kernel_config_file})" + echo "${current_kernel_version} ${kernel_config}" > "latest" + local kernel_path="${gral_path}/tools/packaging/kata-deploy/local-build/build/cc-kernel/destdir/opt/confidential-containers/share/kata-containers" + local vmlinux_binary_name="vmlinux-${current_kernel_version}-${kernel_config}" + ls ${kernel_path} + local vmlinux_file="${kernel_path}/${vmlinux_binary_name}" + if [ -f "${vmlinux_file}" ]; then + cp -a "${vmlinux_file}" . + create_cache_asset "${vmlinux_binary_name}" "${current_kernel_version}" + fi + local vmlinuz_binary_name="vmlinuz-${current_kernel_version}-${kernel_config}" + local vmlinuz_file="${kernel_path}/${vmlinuz_binary_name}" + if [ -f "${vmlinuz_file}" ]; then + cp -a "${vmlinuz_file}" . + create_cache_asset "${vmlinuz_binary_name}" "${current_kernel_version}" + fi +} + create_cache_asset() { local component_name="$1" local component_version="$2" @@ -55,6 +77,7 @@ Usage: $0 "[options]" Builds the cache of several kata components. Options: -c Cloud hypervisor cache + -k Kernel cache -q Qemu cache -h Shows help EOF @@ -64,13 +87,17 @@ EOF main() { local cloud_hypervisor_component="${cloud_hypervisor_component:-}" local qemu_component="${qemu_component:-}" + local kernel_component="${kernel_component:-}" local OPTIND - while getopts ":cqh:" opt + while getopts ":ckqh:" opt do case "$opt" in c) cloud_hypervisor_component="1" ;; + k) + kernel_component="1" + ;; q) qemu_component="1" ;; @@ -88,6 +115,7 @@ main() { shift $((OPTIND-1)) [[ -z "${cloud_hypervisor_component}" ]] && \ + [[ -z "${kernel_component}" ]] && \ [[ -z "${qemu_component}" ]] && \ help && die "Must choose at least one option" @@ -96,6 +124,7 @@ main() { echo "Artifacts:" [ "${cloud_hypervisor_component}" == "1" ] && cache_clh_artifacts + [ "${kernel_component}" == "1" ] && cache_kernel_artifacts [ "${qemu_component}" == "1" ] && cache_qemu_artifacts ls -la "${WORKSPACE}/artifacts/" diff --git a/tools/packaging/static-build/kernel/build.sh b/tools/packaging/static-build/kernel/build.sh index 8bd4dc2da..663a007c8 100755 --- a/tools/packaging/static-build/kernel/build.sh +++ b/tools/packaging/static-build/kernel/build.sh @@ -17,25 +17,82 @@ source "${script_dir}/../../scripts/lib.sh" DESTDIR=${DESTDIR:-${PWD}} PREFIX=${PREFIX:-/opt/kata} container_image="${KERNEL_CONTAINER_BUILDER:-${CC_BUILDER_REGISTRY}:kernel-$(get_last_modification ${repo_root_dir} ${script_dir})-$(uname -m)}" +kernel_latest_build_url="${jenkins_url}/job/kata-containers-2.0-kernel-cc-$(uname -m)/${cached_artifacts_path}" +current_kernel_version=${kernel_version:-$(get_from_kata_deps "assets.kernel.version")} +cached_path="$(echo ${script_dir} | sed 's,/*[^/]\+/*$,,' | sed 's,/*[^/]\+/*$,,' | sed 's,/*[^/]\+/*$,,' | sed 's,/*[^/]\+/*$,,')" +current_kernel_config_file="${cached_path}/tools/packaging/kernel/kata_config_version" +current_kernel_config="$(cat $current_kernel_config_file)" +kernel_version="$(echo ${current_kernel_version} | cut -c2- )" -sudo docker pull ${container_image} || \ - (sudo docker build -t "${container_image}" "${script_dir}" && \ - # No-op unless PUSH_TO_REGISTRY is exported as "yes" - push_to_registry "${container_image}") +build_from_source() { + sudo docker pull ${container_image} || \ + (sudo docker build -t "${container_image}" "${script_dir}" && \ + # No-op unless PUSH_TO_REGISTRY is exported as "yes" + push_to_registry "${container_image}") -sudo docker run --rm -i -v "${repo_root_dir}:${repo_root_dir}" \ - -w "${PWD}" \ - --env KATA_BUILD_CC="${KATA_BUILD_CC:-}" \ - "${container_image}" \ - bash -c "${kernel_builder} $* setup" + sudo docker run --rm -i -v "${repo_root_dir}:${repo_root_dir}" \ + -w "${PWD}" \ + --env KATA_BUILD_CC="${KATA_BUILD_CC:-}" \ + "${container_image}" \ + bash -c "${kernel_builder} $* setup" -sudo docker run --rm -i -v "${repo_root_dir}:${repo_root_dir}" \ - -w "${PWD}" \ - "${container_image}" \ - bash -c "${kernel_builder} $* build" + sudo docker run --rm -i -v "${repo_root_dir}:${repo_root_dir}" \ + -w "${PWD}" \ + "${container_image}" \ + bash -c "${kernel_builder} $* build" -sudo docker run --rm -i -v "${repo_root_dir}:${repo_root_dir}" \ - -w "${PWD}" \ - --env DESTDIR="${DESTDIR}" --env PREFIX="${PREFIX}" \ - "${container_image}" \ - bash -c "${kernel_builder} $* install" + sudo docker run --rm -i -v "${repo_root_dir}:${repo_root_dir}" \ + -w "${PWD}" \ + --env DESTDIR="${DESTDIR}" --env PREFIX="${PREFIX}" \ + "${container_image}" \ + bash -c "${kernel_builder} $* install" +} + +check_cached_kernel() { + local latest=$(curl -sfL "${kernel_latest_build_url}"/latest) || latest="none" + local cached_kernel_version="$(echo ${latest} | awk '{print $1}')" + info "Current kernel version: ${kernel_version}" + info "Cached kernel version: ${cached_kernel_version}" + if [ "${kernel_version}" == "${cached_kernel_version}" ] && [ "$(uname -m)" == "x86_64" ]; then + local cached_kernel_config="$(echo ${latest} | awk '{print $2}')" + info "Cached kernel config: ${cached_kernel_config}" + info "Current kernel config: ${current_kernel_config}" + if [ -z "${cached_kernel_config}" ]; then + build_from_source $* + else + install_cached_kernel $* + fi + else + build_from_source $* + fi +} + +install_cached_kernel() { + local kernel_directory="${cached_path}/tools/packaging/kata-deploy/local-build/build/cc-kernel/destdir/opt/confidential-containers/share/kata-containers" + local vmlinux_kernel_name="vmlinux-${cached_kernel_version}-${cached_kernel_config}" + local vmlinuz_kernel_name="vmlinuz-${cached_kernel_version}-${cached_kernel_config}" + mkdir -p "${kernel_directory}" + pushd "${kernel_directory}" + ls + local vmlinux_url="${kernel_latest_build_url}/${vmlinux_kernel_name}" + if curl --output /dev/null --silent --head --fail "${vmlinux_url}"; then + info "Installing vmlinux cached kernel" + curl -fL --progress-bar "${kernel_latest_build_url}/${vmlinux_kernel_name}" -o "${vmlinux_kernel_name}" || return 1 + sudo -E ln -sf "${kernel_directory}/${vmlinux_kernel_name}" "${kernel_directory}/vmlinux.container" + fi + + local vmlinuz_url="${kernel_latest_build_url}/${vmlinuz_kernel_name}" + if curl --output /dev/null --silent --head --fail "${vmlinuz_url}"; then + info "Installing vmlinuz cached kernel" + curl -fL --progress-bar "${kernel_latest_build_url}/${vmlinuz_kernel_name}" -o "${vmlinuz_kernel_name}" || return 1 + sudo -E ln -sf "${kernel_directory}/${vmlinuz_kernel_name}" "${kernel_directory}/vmlinuz.container" + fi + popd + +} + +main() { + check_cached_kernel $* +} + +main $*