From a89fca251514dee26b4e6e077f4a0484a2d45a5f Mon Sep 17 00:00:00 2001
From: Liu Wenyuan <liuwenyuan9@huawei.com>
Date: Fri, 1 Sep 2023 20:44:30 +0800
Subject: [PATCH 4/5] kata-deploy: Add StratoVirt support to deploy process

Allow kata-deploy process to pull StratoVirt from release binaries, and
add them as a part of kata release.

Signed-off-by: Liu Wenyuan <liuwenyuan9@huawei.com>
---
 .../kata-cleanup/base/kata-cleanup.yaml       |  2 +-
 .../kata-deploy/base/kata-deploy.yaml         |  2 +-
 .../kata-deploy/local-build/Makefile          |  4 ++
 .../local-build/kata-deploy-binaries.sh       | 28 ++++++++++++++
 .../runtimeclasses/kata-runtimeClasses.yaml   | 13 +++++++
 .../runtimeclasses/kata-stratovirt.yaml       | 13 +++++++
 .../stratovirt/build-static-stratovirt.sh     | 37 +++++++++++++++++++
 versions.yaml                                 |  5 +++
 8 files changed, 102 insertions(+), 2 deletions(-)
 create mode 100644 tools/packaging/kata-deploy/runtimeclasses/kata-stratovirt.yaml
 create mode 100755 tools/packaging/static-build/stratovirt/build-static-stratovirt.sh

diff --git a/tools/packaging/kata-deploy/kata-cleanup/base/kata-cleanup.yaml b/tools/packaging/kata-deploy/kata-cleanup/base/kata-cleanup.yaml
index df1a2ff..4814b93 100644
--- a/tools/packaging/kata-deploy/kata-cleanup/base/kata-cleanup.yaml
+++ b/tools/packaging/kata-deploy/kata-cleanup/base/kata-cleanup.yaml
@@ -30,7 +30,7 @@ spec:
             - name: DEBUG
               value: "false"
             - name: SHIMS
-              value: "clh dragonball fc qemu-nvidia-gpu qemu-sev qemu-snp qemu-tdx qemu"
+              value: "clh dragonball fc qemu-nvidia-gpu qemu-sev qemu-snp qemu-tdx qemu stratovirt"
             - name: DEFAULT_SHIM
               value: "qemu"
             - name: CREATE_RUNTIMECLASSES
diff --git a/tools/packaging/kata-deploy/kata-deploy/base/kata-deploy.yaml b/tools/packaging/kata-deploy/kata-deploy/base/kata-deploy.yaml
index 383eec9..52e5987 100644
--- a/tools/packaging/kata-deploy/kata-deploy/base/kata-deploy.yaml
+++ b/tools/packaging/kata-deploy/kata-deploy/base/kata-deploy.yaml
@@ -32,7 +32,7 @@ spec:
             - name: DEBUG
               value: "false"
             - name: SHIMS
-              value: "clh dragonball fc qemu qemu-nvidia-gpu qemu-sev qemu-snp qemu-tdx"
+              value: "clh dragonball fc qemu qemu-nvidia-gpu qemu-sev qemu-snp qemu-tdx stratovirt"
             - name: DEFAULT_SHIM
               value: "qemu"
             - name: CREATE_RUNTIMECLASSES
diff --git a/tools/packaging/kata-deploy/local-build/Makefile b/tools/packaging/kata-deploy/local-build/Makefile
index d9e28a5..ef63f2f 100644
--- a/tools/packaging/kata-deploy/local-build/Makefile
+++ b/tools/packaging/kata-deploy/local-build/Makefile
@@ -35,6 +35,7 @@ all: serial-targets \
 	qemu-snp-experimental-tarball \
 	qemu-tarball \
 	qemu-tdx-experimental-tarball \
+	stratovirt-tarball \
 	shim-v2-tarball \
 	tdvf-tarball \
 	virtiofsd-tarball
@@ -112,6 +113,9 @@ qemu-tarball:
 qemu-tdx-experimental-tarball:
 	${MAKE} $@-build
 
+stratovirt-tarball:
+	${MAKE} $@-build
+
 rootfs-image-tarball:
 	${MAKE} $@-build
 
diff --git a/tools/packaging/kata-deploy/local-build/kata-deploy-binaries.sh b/tools/packaging/kata-deploy/local-build/kata-deploy-binaries.sh
index bb68d88..70e07b4 100755
--- a/tools/packaging/kata-deploy/local-build/kata-deploy-binaries.sh
+++ b/tools/packaging/kata-deploy/local-build/kata-deploy-binaries.sh
@@ -30,6 +30,7 @@ readonly kernel_builder="${static_build_dir}/kernel/build.sh"
 readonly ovmf_builder="${static_build_dir}/ovmf/build.sh"
 readonly qemu_builder="${static_build_dir}/qemu/build-static-qemu.sh"
 readonly qemu_experimental_builder="${static_build_dir}/qemu/build-static-qemu-experimental.sh"
+readonly stratovirt_builder="${static_build_dir}/stratovirt/build-static-stratovirt.sh"
 readonly shimv2_builder="${static_build_dir}/shim-v2/build.sh"
 readonly virtiofsd_builder="${static_build_dir}/virtiofsd/build.sh"
 readonly nydus_builder="${static_build_dir}/nydus/build.sh"
@@ -103,6 +104,7 @@ options:
 	qemu
 	qemu-snp-experimental
 	qemu-tdx-experimental
+	stratovirt
 	rootfs-image
 	rootfs-image-tdx
 	rootfs-initrd
@@ -511,6 +513,28 @@ install_clh_glibc() {
 	install_clh_helper "gnu" "${features}" "-glibc"
 }
 
+# Install static stratovirt asset
+install_stratovirt() {
+	local stratovirt_version=$(get_from_kata_deps "assets.hypervisor.stratovirt.version")
+
+	latest_artefact="${stratovirt_version}"
+	latest_builder_image=""
+
+	install_cached_tarball_component \
+		"stratovirt" \
+		"${latest_artefact}" \
+		"${latest_builder_image}" \
+		"${final_tarball_name}" \
+		"${final_tarball_path}" \
+		&& return 0
+
+	info "build static stratovirt"
+	"${stratovirt_builder}"
+	info "Install static stratovirt"
+	mkdir -p "${destdir}/opt/kata/bin/"
+	sudo install -D --owner root --group root --mode 0744 static-stratovirt/stratovirt "${destdir}/opt/kata/bin/stratovirt"
+}
+
 # Install static virtiofsd asset
 install_virtiofsd() {
 	latest_artefact="$(get_from_kata_deps "externals.virtiofsd.version")-$(get_from_kata_deps "externals.virtiofsd.toolchain")"
@@ -734,6 +758,7 @@ handle_build() {
 		install_qemu
 		install_qemu_snp_experimental
 		install_qemu_tdx_experimental
+		install_stratovirt
 		install_runk
 		install_shimv2
 		install_tdvf
@@ -781,6 +806,8 @@ handle_build() {
 
 	qemu-tdx-experimental) install_qemu_tdx_experimental ;;
 
+	stratovirt) install_stratovirt ;;
+
 	rootfs-image) install_image ;;
 
 	rootfs-image-tdx) install_image_tdx ;;
@@ -860,6 +887,7 @@ main() {
 		log-parser-rs
 		nydus
 		qemu
+		stratovirt
 		rootfs-image
 		rootfs-initrd
 		rootfs-initrd-mariner
diff --git a/tools/packaging/kata-deploy/runtimeclasses/kata-runtimeClasses.yaml b/tools/packaging/kata-deploy/runtimeclasses/kata-runtimeClasses.yaml
index e67cafe..ae3c8c8 100644
--- a/tools/packaging/kata-deploy/runtimeclasses/kata-runtimeClasses.yaml
+++ b/tools/packaging/kata-deploy/runtimeclasses/kata-runtimeClasses.yaml
@@ -102,3 +102,16 @@ overhead:
 scheduling:
   nodeSelector:
     katacontainers.io/kata-runtime: "true"
+---
+kind: RuntimeClass
+apiVersion: node.k8s.io/v1
+metadata:
+    name: kata-stratovirt
+handler: kata-stratovirt
+overhead:
+    podFixed:
+        memory: "130Mi"
+        cpu: "250m"
+scheduling:
+  nodeSelector:
+    katacontainers.io/kata-runtime: "true"
diff --git a/tools/packaging/kata-deploy/runtimeclasses/kata-stratovirt.yaml b/tools/packaging/kata-deploy/runtimeclasses/kata-stratovirt.yaml
new file mode 100644
index 0000000..ea27046
--- /dev/null
+++ b/tools/packaging/kata-deploy/runtimeclasses/kata-stratovirt.yaml
@@ -0,0 +1,13 @@
+---
+kind: RuntimeClass
+apiVersion: node.k8s.io/v1
+metadata:
+    name: kata-stratovirt
+handler: kata-stratovirt
+overhead:
+    podFixed:
+        memory: "130Mi"
+        cpu: "250m"
+scheduling:
+  nodeSelector:
+    katacontainers.io/kata-runtime: "true"
diff --git a/tools/packaging/static-build/stratovirt/build-static-stratovirt.sh b/tools/packaging/static-build/stratovirt/build-static-stratovirt.sh
new file mode 100755
index 0000000..2fe28aa
--- /dev/null
+++ b/tools/packaging/static-build/stratovirt/build-static-stratovirt.sh
@@ -0,0 +1,37 @@
+#!/usr/bin/env bash
+#
+# Copyright (c) 2023 Huawei Technologies Co.,Ltd.
+#
+# SPDX-License-Identifier: Apache-2.0
+
+set -o errexit
+set -o nounset
+set -o pipefail
+
+ARCH=$(uname -m)
+
+# Currently, StratoVirt only support x86_64 and aarch64.
+[ "${ARCH}" != "x86_64" ] && [ "${ARCH}" != "aarch64" ] && exit
+
+script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+source "${script_dir}/../../scripts/lib.sh"
+
+info "Get stratovirt information from runtime versions.yaml"
+stratovirt_url="${stratovirt_url:-}"
+[ -n "$stratovirt_url" ] || stratovirt_url=$(get_from_kata_deps "assets.hypervisor.stratovirt.url")
+[ -n "$stratovirt_url" ] || die "failed to get stratovirt url"
+
+stratovirt_version="${stratovirt_version:-}"
+[ -n "$stratovirt_version" ] || stratovirt_version=$(get_from_kata_deps "assets.hypervisor.stratovirt.version")
+[ -n "$stratovirt_version" ] || die "failed to get stratovirt version"
+
+pull_stratovirt_released_binary() {
+    file_name="stratovirt-static-${stratovirt_version##*v}-${ARCH}"
+    download_url="${stratovirt_url}/releases/download/${stratovirt_version}/${file_name}.tar.gz"
+
+    curl -L ${download_url} -o ${file_name}.tar.gz
+    mkdir -p static-stratovirt
+    tar zxvf ${file_name}.tar.gz -C static-stratovirt
+}
+
+pull_stratovirt_released_binary
diff --git a/versions.yaml b/versions.yaml
index 57e3c94..3b5f37a 100644
--- a/versions.yaml
+++ b/versions.yaml
@@ -115,6 +115,11 @@ assets:
       url: "https://github.com/AMDESE/qemu"
       tag: "3b6a2b6b7466f6dea53243900b7516c3f29027b7"
 
+    stratovirt:
+      description: "StratoVirt is an lightweight opensource VMM"
+      url: "https://github.com/openeuler-mirror/stratovirt"
+      version: "v2.3.0"
+
   image:
     description: |
       Root filesystem disk image used to boot the guest virtual
-- 
2.34.1

