github.com/dmaizel/tests@v0.0.0-20210728163746-cae6a2d9cee8/.ci/install_qemu.sh (about)

     1  #!/bin/bash
     2  #
     3  # Copyright (c) 2018 Intel Corporation
     4  #
     5  # SPDX-License-Identifier: Apache-2.0
     6  #
     7  
     8  set -o errexit
     9  set -o nounset
    10  set -o pipefail
    11  set -o errtrace
    12  
    13  cidir=$(dirname "$0")
    14  source "${cidir}/lib.sh"
    15  source "${cidir}/../lib/common.bash"
    16  source /etc/os-release || source /usr/lib/os-release
    17  
    18  PREFIX=${PREFIX:-/usr}
    19  KATA_DEV_MODE="${KATA_DEV_MODE:-}"
    20  
    21  CURRENT_QEMU_VERSION=$(get_version "assets.hypervisor.qemu.version")
    22  QEMU_REPO_URL=$(get_version "assets.hypervisor.qemu.url")
    23  # Remove 'https://' from the repo url to be able to git clone the repo
    24  QEMU_REPO=${QEMU_REPO_URL/https:\/\//}
    25  QEMU_ARCH=$(${cidir}/kata-arch.sh -d)
    26  PACKAGING_DIR="${kata_repo_dir}/tools/packaging"
    27  ARCH=$("${cidir}"/kata-arch.sh -d)
    28  QEMU_TAR="kata-static-qemu.tar.gz"
    29  qemu_latest_build_url="${jenkins_url}/job/kata-containers-2.0-qemu-$(uname -m)/${cached_artifacts_path}"
    30  
    31  # option "--shallow-submodules" was introduced in git v2.9.0
    32  GIT_SHADOW_VERSION="2.9.0"
    33  
    34  # We need to move the tar file to a specific location so we
    35  # can know where it is and then we can perform the build cache
    36  # operations
    37  update_cache() {
    38  	sudo mkdir -p "${KATA_TESTS_CACHEDIR}"
    39  	sudo mv ${QEMU_TAR} ${KATA_TESTS_CACHEDIR}
    40  }
    41  
    42  build_static_qemu() {
    43  	info "building static QEMU"
    44  	# only x86_64 is supported for building static QEMU
    45  	[ "$ARCH" != "x86_64" ] && return 1
    46  
    47  	(
    48  	cd "${PACKAGING_DIR}/static-build/qemu"
    49  	prefix="${KATA_QEMU_DESTDIR}" make
    50  
    51  	update_cache
    52  	)
    53  }
    54  
    55  uncompress_static_qemu() {
    56  	local qemu_tar_location="$1"
    57  	[ -n "$qemu_tar_location" ] || die "provide the location of the QEMU compressed file"
    58  	sudo tar -xf "${qemu_tar_location}" -C /
    59  	# verify installed binaries existance
    60  	ls /usr/libexec/kata-qemu/virtiofsd || return 1
    61  	ls /usr/bin/qemu-system-x86_64 || return 1
    62  }
    63  
    64  build_and_install_static_qemu() {
    65  	build_static_qemu
    66  	uncompress_static_qemu "${KATA_TESTS_CACHEDIR}/${QEMU_TAR}"
    67  }
    68  
    69  install_cached_qemu() {
    70  	info "Installing cached QEMU"
    71  	curl -fL --progress-bar "${qemu_latest_build_url}/${QEMU_TAR}" -o "${QEMU_TAR}" || return 1
    72  	curl -fsOL "${qemu_latest_build_url}/sha256sum-${QEMU_TAR}" || return 1
    73  
    74  	sha256sum -c "sha256sum-${QEMU_TAR}" || return 1
    75  	uncompress_static_qemu "${QEMU_TAR}"
    76  	update_cache
    77  }
    78  
    79  clone_qemu_repo() {
    80  	# check if git is capable of shadow cloning
    81          git_shadow_clone=$(check_git_version "${GIT_SHADOW_VERSION}")
    82  
    83  	if [ "$git_shadow_clone" == "true" ]; then
    84  		git clone --branch "${CURRENT_QEMU_VERSION}" --single-branch --depth 1 --shallow-submodules "${QEMU_REPO_URL}" "${GOPATH}/src/${QEMU_REPO}"
    85  	else
    86  		git clone --branch "${CURRENT_QEMU_VERSION}" --single-branch --depth 1 "${QEMU_REPO_URL}" "${GOPATH}/src/${QEMU_REPO}"
    87  	fi
    88  }
    89  
    90  build_and_install_qemu() {
    91  	if [ -n "$(command -v qemu-system-${QEMU_ARCH})" ] && [ -n "$KATA_DEV_MODE" ]; then
    92  		die "QEMU will not be installed"
    93  	fi
    94  
    95  	QEMU_CONFIG_SCRIPT="${PACKAGING_DIR}/scripts/configure-hypervisor.sh"
    96  
    97  	mkdir -p "${GOPATH}/src"
    98  
    99  	clone_kata_repo
   100  	clone_qemu_repo
   101  
   102  	pushd "${GOPATH}/src/${QEMU_REPO}"
   103  	git fetch
   104  	[ -n "$(ls -A capstone)" ] || git clone https://github.com/qemu/capstone.git capstone
   105  	[ -n "$(ls -A ui/keycodemapdb)" ] || git clone  https://github.com/qemu/keycodemapdb.git ui/keycodemapdb
   106  
   107  	# Apply required patches
   108  	QEMU_PATCHES_BRANCH=$(echo "${CURRENT_QEMU_VERSION}" | cut -d '.' -f1-2)
   109  	QEMU_PATCHES_BRANCH=${QEMU_PATCHES_BRANCH#"v"}
   110  	QEMU_PATCHES_PATH="${PACKAGING_DIR}/qemu/patches/${QEMU_PATCHES_BRANCH}.x"
   111  	QEMU_VERSION_PATCHES_PATH="${PACKAGING_DIR}/qemu/patches/tag_patches/${CURRENT_QEMU_VERSION}"
   112  	for patch in ${QEMU_PATCHES_PATH}/*.patch; do
   113  		echo "Applying patch: $patch"
   114  		git apply "$patch"
   115  	done
   116  
   117  	echo "INFO: Apply patches for qemu version ${CURRENT_QEMU_VERSION}"
   118  	patches=($(find "$QEMU_VERSION_PATCHES_PATH" -name '*.patch'|sort -t- -k1,1n))
   119  	echo "INFO: Found ${#patches[@]} patches"
   120  	for patch in ${patches[@]}; do
   121  		echo "Applying patch for version ${CURRENT_QEMU_VERSION}: $patch"
   122  		git apply "$patch"
   123  	done
   124  
   125  	echo "Build QEMU"
   126  	# Not all distros have the libpmem package
   127  	"${QEMU_CONFIG_SCRIPT}" "qemu" |
   128  		if [ "${NAME}" == "Ubuntu" ] && [ "$(echo "${VERSION_ID} < 18.04" | bc -q)" == "1" ]; then
   129  			sed -e 's/--enable-libpmem/--disable-libpmem/g'
   130  		else
   131  			cat
   132  		fi | xargs ./configure --prefix=${PREFIX}
   133  	make -j $(nproc)
   134  
   135  	echo "Install QEMU"
   136  	sudo -E make install
   137  	# qemu by default installs virtiofsd under libexec
   138  	sudo mkdir -p /usr/libexec/kata-qemu/
   139  	sudo ln -sf ${PREFIX}/libexec/qemu/virtiofsd /usr/libexec/kata-qemu/virtiofsd
   140  	ls -l /usr/libexec/kata-qemu/virtiofsd || return 1
   141  	popd
   142  }
   143  
   144  #Load specific configure file
   145  if [ -f "${cidir}/${QEMU_ARCH}/lib_install_qemu_${QEMU_ARCH}.sh" ]; then
   146  	source "${cidir}/${QEMU_ARCH}/lib_install_qemu_${QEMU_ARCH}.sh"
   147  fi
   148  
   149  main() {
   150  	case "$QEMU_ARCH" in
   151  		"x86_64")
   152  			# latest is "version sha256sum"
   153  			latest=$(curl -sfL "${qemu_latest_build_url}/latest") || latest="none"
   154  			cached_qemu_version=$(echo $latest | awk '{print $1}')
   155  			info "current QEMU version: $CURRENT_QEMU_VERSION"
   156  			info "cached QEMU version: $cached_qemu_version"
   157  
   158  			if [ -n "${FORCE_BUILD_QEMU:-}" ]; then
   159  				build_and_install_qemu
   160  			elif [ "$CURRENT_QEMU_VERSION" == "$cached_qemu_version" ]; then
   161  				# Let's check if the current sha256sum matches
   162  				# with the cached, otherwise build QEMU locally
   163  				current_sha256sum="$(calc_qemu_files_sha256sum)"
   164  				[ -n "$current_sha256sum" ] || \
   165  					die "Failed to calculate SHA-256 for QEMU"
   166  				cached_sha256sum="$(echo $latest | awk '{print $2}')"
   167  				if [ "$current_sha256sum" == "$cached_sha256sum" ]; then
   168  					install_cached_qemu
   169  				else
   170  					warn "Mismatch of cached ($cached_sha256sum) and expected ($current_sha256sum) versions"
   171  					build_and_install_static_qemu
   172  				fi
   173  			else
   174  				build_and_install_static_qemu
   175  			fi
   176  			;;
   177  		"aarch64"|"ppc64le"|"s390x")
   178  			build_and_install_qemu
   179  			;;
   180  		*)
   181  			die "Architecture $QEMU_ARCH not supported"
   182  			;;
   183  	esac
   184  }
   185  
   186  main