From 39ab77948a6de074ecee64c83dddbb05deaaeb40 Mon Sep 17 00:00:00 2001 From: Manuel Huber Date: Thu, 13 Jun 2024 22:48:34 +0000 Subject: [PATCH 1/2] rootfs: Install Rust only when necessary For docker-based builds only install Rust when necessary. Further, execute the detect Rust version check only when intending to install Rust. As of today, this is the case when we intend to build the agent during rootfs build. Signed-off-by: Manuel Huber --- tools/osbuilder/rootfs-builder/rootfs.sh | 6 ------ tools/osbuilder/scripts/lib.sh | 13 +++++++++++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/tools/osbuilder/rootfs-builder/rootfs.sh b/tools/osbuilder/rootfs-builder/rootfs.sh index e0765113d667..7d6446aae704 100755 --- a/tools/osbuilder/rootfs-builder/rootfs.sh +++ b/tools/osbuilder/rootfs-builder/rootfs.sh @@ -372,12 +372,6 @@ build_rootfs_distro() mkdir -p ${ROOTFS_DIR} fi - # need to detect rustc's version too? - detect_rust_version || - die "Could not detect the required rust version for AGENT_VERSION='${AGENT_VERSION:-main}'." - - echo "Required rust version: $RUST_VERSION" - if [ "${SELINUX}" == "yes" ]; then if [ "${AGENT_INIT}" == "yes" ]; then die "Guest SELinux with the agent init is not supported yet" diff --git a/tools/osbuilder/scripts/lib.sh b/tools/osbuilder/scripts/lib.sh index 615ff10a9508..56c4199f7cfd 100644 --- a/tools/osbuilder/scripts/lib.sh +++ b/tools/osbuilder/scripts/lib.sh @@ -224,14 +224,23 @@ generate_dockerfile() [ -n "${http_proxy:-}" ] && readonly set_proxy="RUN sed -i '$ a proxy="${http_proxy:-}"' /etc/dnf/dnf.conf /etc/yum.conf; true" - # Rust agent - readonly install_rust=" + # Only install Rust if agent needs to be built + local install_rust="" + + if [ ! -z "${AGENT_SOURCE_BIN}" ] ; then + if [ "$RUST_VERSION" == "null" ]; then + detect_rust_version || \ + die "Could not detect the required rust version for AGENT_VERSION='${AGENT_VERSION:-main}'." + fi + install_rust=" ENV http_proxy=${http_proxy:-} ENV https_proxy=${http_proxy:-} RUN curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSLf | \ sh -s -- -y --default-toolchain ${RUST_VERSION} -t ${rustarch}-unknown-linux-${LIBC} RUN . /root/.cargo/env; cargo install cargo-when " + fi + pushd "${dir}" sed \ From 9c9199e2f484e9fb6c4e7cecfaadb5abdee4bf91 Mon Sep 17 00:00:00 2001 From: Manuel Huber Date: Tue, 11 Jun 2024 20:51:26 +0000 Subject: [PATCH 2/2] build: allow rootfs builds w/o git or VERSION file deps We set the VERSION variable consistently across Makefiles to 'unknown' if the file is empty or not present. We also use git commands consistently for calculating the COMMIT, COMMIT_NO variables, not erroring out when building outside of a git repository. In create_summary_file we also account for a missing/empty VERSION file. This makes e.g. the UVM build process in an environment where we build outside of git with a minimal/reduced set of files smoother. Signed-off-by: Manuel Huber --- src/agent/Makefile | 2 +- src/runtime-rs/Makefile | 2 +- src/tools/kata-ctl/Makefile | 2 +- src/tools/log-parser/Makefile | 7 ++++--- tools/osbuilder/Makefile | 6 +++--- tools/osbuilder/scripts/lib.sh | 3 ++- 6 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/agent/Makefile b/src/agent/Makefile index 5b118beb9c9c..169c2ab421ba 100644 --- a/src/agent/Makefile +++ b/src/agent/Makefile @@ -15,7 +15,7 @@ PROJECT_COMPONENT = kata-agent TARGET = $(PROJECT_COMPONENT) VERSION_FILE := ./VERSION -VERSION := $(shell grep -v ^\# $(VERSION_FILE)) +VERSION := $(shell grep -v ^\# $(VERSION_FILE) 2>/dev/null || echo "unknown") COMMIT_NO := $(shell git rev-parse HEAD 2>/dev/null || true) COMMIT := $(if $(shell git status --porcelain --untracked-files=no 2>/dev/null || true),${COMMIT_NO}-dirty,${COMMIT_NO}) COMMIT_MSG = $(if $(COMMIT),$(COMMIT),unknown) diff --git a/src/runtime-rs/Makefile b/src/runtime-rs/Makefile index f492d1d33485..cdb72ebafea7 100644 --- a/src/runtime-rs/Makefile +++ b/src/runtime-rs/Makefile @@ -355,7 +355,7 @@ SOURCES := \ Cargo.toml VERSION_FILE := ./VERSION -VERSION := $(shell grep -v ^\# $(VERSION_FILE)) +VERSION := $(shell grep -v ^\# $(VERSION_FILE) 2>/dev/null || echo "unknown") COMMIT_NO := $(shell git rev-parse HEAD 2>/dev/null || true) COMMIT := $(if $(shell git status --porcelain --untracked-files=no 2>/dev/null || true),${COMMIT_NO}-dirty,${COMMIT_NO}) COMMIT_MSG = $(if $(COMMIT),$(COMMIT),unknown) diff --git a/src/tools/kata-ctl/Makefile b/src/tools/kata-ctl/Makefile index 546f0783adfd..c14d72a92a4a 100644 --- a/src/tools/kata-ctl/Makefile +++ b/src/tools/kata-ctl/Makefile @@ -13,7 +13,7 @@ TARGET = $(PROJECT_COMPONENT) INSTALL_PATH = $(HOME)/.cargo VERSION_FILE := ./VERSION -export VERSION := $(shell grep -v ^\# $(VERSION_FILE)) +export VERSION := $(shell grep -v ^\# $(VERSION_FILE) 2>/dev/null || echo "unknown") COMMIT_NO := $(shell git rev-parse HEAD 2>/dev/null || true) COMMIT_NO_SHORT := $(shell git rev-parse --short HEAD 2>/dev/null || true) export COMMIT := $(if $(shell git status --porcelain --untracked-files=no 2>/dev/null || true),${COMMIT_NO}-dirty,${COMMIT_NO}) diff --git a/src/tools/log-parser/Makefile b/src/tools/log-parser/Makefile index 0a680059b3a9..91b75652d0a5 100644 --- a/src/tools/log-parser/Makefile +++ b/src/tools/log-parser/Makefile @@ -7,9 +7,10 @@ TARGET = kata-log-parser SOURCES = $(shell find . 2>&1 | grep -E '.*\.go$$') -VERSION := ${shell cat ./VERSION} -COMMIT_NO := $(shell git rev-parse HEAD 2> /dev/null || true) -COMMIT := $(if $(shell git status --porcelain --untracked-files=no),"${COMMIT_NO}-dirty","${COMMIT_NO}") +VERSION_FILE := ./VERSION +VERSION := $(shell grep -v ^\# $(VERSION_FILE) 2>/dev/null || echo "unknown") +COMMIT_NO := $(shell git rev-parse HEAD 2>/dev/null || true) +COMMIT := $(if $(shell git status --porcelain --untracked-files=no 2>/dev/null || true),"${COMMIT_NO}-dirty","${COMMIT_NO}") BINDIR := $(GOPATH)/bin DESTTARGET := $(abspath $(BINDIR)/$(TARGET)) diff --git a/tools/osbuilder/Makefile b/tools/osbuilder/Makefile index 81e48907c9a0..f7924a7c3497 100644 --- a/tools/osbuilder/Makefile +++ b/tools/osbuilder/Makefile @@ -31,9 +31,9 @@ TARGET_IGVM_DEBUG_MSMT:= $(IGVM_BUILD_DEST)/igvm-debug-measurement.cose TARGET_IGVM_LOG := $(IGVM_BUILD_DEST)/igvm.log VERSION_FILE := ./VERSION -VERSION := $(shell grep -v ^\# $(VERSION_FILE)) -COMMIT_NO := $(shell git rev-parse HEAD 2> /dev/null || true) -COMMIT := $(if $(shell git status --porcelain --untracked-files=no),${COMMIT_NO}-dirty,${COMMIT_NO}) +VERSION := $(shell grep -v ^\# $(VERSION_FILE) 2>/dev/null || echo "unknown") +COMMIT_NO := $(shell git rev-parse HEAD 2>/dev/null || true) +COMMIT := $(if $(shell git status --porcelain --untracked-files=no 2>/dev/null || true),${COMMIT_NO}-dirty,${COMMIT_NO}) VERSION_COMMIT := $(if $(COMMIT),$(VERSION)-$(COMMIT),$(VERSION)) ifeq ($(filter $(BUILD_METHOD),$(BUILD_METHOD_LIST)),) diff --git a/tools/osbuilder/scripts/lib.sh b/tools/osbuilder/scripts/lib.sh index 56c4199f7cfd..5e3687595953 100644 --- a/tools/osbuilder/scripts/lib.sh +++ b/tools/osbuilder/scripts/lib.sh @@ -181,7 +181,8 @@ create_summary_file() [ "$AGENT_INIT" = yes ] && agent="${init}" local -r agentdir="${script_dir}/../../../" - local -r agent_version=$(cat ${agentdir}/VERSION) + local agent_version=$(cat ${agentdir}/VERSION 2> /dev/null) + [ -z "$agent_version" ] && agent_version="unknown" cat >"$file"<<-EOF ---