Skip to content
This repository has been archived by the owner on Feb 8, 2021. It is now read-only.

Commit

Permalink
setup the kata-container runing env for test
Browse files Browse the repository at this point in the history
Signed-off-by: fupan <[email protected]>
  • Loading branch information
lifupan committed Jul 3, 2018
1 parent 970024d commit 16bec09
Show file tree
Hide file tree
Showing 7 changed files with 533 additions and 0 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ install:
- ./autogen.sh && ./configure && make

script:
- ./scripts/kata-env-setup.sh
- cd ${TRAVIS_BUILD_DIR} && hack/test-cmd.sh
14 changes: 14 additions & 0 deletions scripts/install_agent.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
#
# Copyright (c) 2017-2018 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
#

set -e

cidir=$(dirname "$0")

source "${cidir}/lib.sh"

clone_build_and_install "github.com/kata-containers/agent"
136 changes: 136 additions & 0 deletions scripts/install_kata_image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#!/bin/bash
#
# Copyright (c) 2017-2018 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
#

set -o errexit
set -o nounset
set -o pipefail

cidir=$(dirname "$0")

source /etc/os-release
source "${cidir}/lib.sh"

AGENT_INIT=${AGENT_INIT:-no}
TEST_INITRD=${TEST_INITRD:-no}

TMP_DIR=
ROOTFS_DIR=

PACKAGED_IMAGE="kata-containers-image"
IMG_PATH="/usr/share/kata-containers"
IMG_NAME="kata-containers.img"

agent_path="${GOPATH}/src/github.com/kata-containers/agent"

IMG_MOUNT_DIR=
LOOP_DEVICE=

# Build Kata agent
bash -f "${cidir}/install_agent.sh"
agent_commit=$(git --work-tree="${agent_path}" --git-dir="${agent_path}/.git" log --format=%h -1 HEAD)

cleanup() {
[ -d "${ROOTFS_DIR}" ] && [[ "${ROOTFS_DIR}" = *"rootfs"* ]] && sudo rm -rf "${ROOTFS_DIR}"
[ -d "${TMP_DIR}" ] && rm -rf "${TMP_DIR}"
if [ -n "${IMG_MOUNT_DIR}" ] && mount | grep -q "${IMG_MOUNT_DIR}"; then
sudo umount "${IMG_MOUNT_DIR}"
fi
if [ -d "${IMG_MOUNT_DIR}" ]; then
rm -rf "${IMG_MOUNT_DIR}"
fi
if [ -n "${LOOP_DEVICE}" ]; then
sudo losetup -d "${LOOP_DEVICE}"
fi
}

trap cleanup EXIT

get_packaged_agent_version() {
version=$(ls "$IMG_PATH" | grep "$PACKAGED_IMAGE" | cut -d'_' -f4 | cut -d'.' -f1)
if [ -z "$version" ]; then
die "unknown agent version"
fi
echo "$version"
}

install_packaged_image() {
if [ "$ID" == "ubuntu" ]; then
sudo -E apt install -y "$PACKAGED_IMAGE"
elif [ "$ID" == "fedora" ]; then
sudo -E dnf install -y "$PACKAGED_IMAGE"
elif [ "$ID" == "centos" ]; then
sudo -E yum install -y "$PACKAGED_IMAGE"
else
die "Linux distribution not supported"
fi
}

update_agent() {
pushd "$agent_path"

LOOP_DEVICE=$(sudo losetup -f --show "${IMG_PATH}/${IMG_NAME}")
IMG_MOUNT_DIR=$(mktemp -d -t kata-image-mount.XXXXXXXXXX)
sudo partprobe "$LOOP_DEVICE"
sudo mount "${LOOP_DEVICE}p1" "$IMG_MOUNT_DIR"

echo "Old agent version:"
"${IMG_MOUNT_DIR}/usr/bin/kata-agent" --version

echo "Install new agent"
sudo -E PATH="$PATH" bash -c "make install DESTDIR=$IMG_MOUNT_DIR"
installed_version=$("${IMG_MOUNT_DIR}/usr/bin/kata-agent" --version)
echo "New agent version: $installed_version"

popd
installed_version=${installed_version##k*-}
[[ "${installed_version}" == *"${current_version}"* ]]
}

build_image() {
TMP_DIR=$(mktemp -d -t kata-image-install.XXXXXXXXXX)
readonly ROOTFS_DIR="${TMP_DIR}/rootfs"
export ROOTFS_DIR

image_type=$(get_version "assets.image.meta.image-type")
OSBUILDER_DISTRO="alpine"

osbuilder_repo="github.com/kata-containers/osbuilder"

# Clone os-builder repository
go get -d "${osbuilder_repo}" || true

(cd "${GOPATH}/src/${osbuilder_repo}/rootfs-builder" && \
sudo -E AGENT_INIT="${AGENT_INIT}" AGENT_VERSION="${agent_commit}" \
GOPATH="$GOPATH" USE_DOCKER=true ./rootfs.sh "${OSBUILDER_DISTRO}")

# Build the image
if [ x"${TEST_INITRD}" == x"yes" ]; then
pushd "${GOPATH}/src/${osbuilder_repo}/initrd-builder"
sudo -E AGENT_INIT="${AGENT_INIT}" USE_DOCKER=true ./initrd_builder.sh "$ROOTFS_DIR"
image_name="kata-containers-initrd.img"
else
pushd "${GOPATH}/src/${osbuilder_repo}/image-builder"
sudo -E AGENT_INIT="${AGENT_INIT}" USE_DOCKER=true ./image_builder.sh "$ROOTFS_DIR"
image_name="kata-containers.img"
fi

# Install the image
commit=$(git log --format=%h -1 HEAD)
date=$(date +%Y-%m-%d-%T.%N%z)
image="kata-containers-${date}-osbuilder-${commit}-agent-${agent_commit}"

sudo install -o root -g root -m 0640 -D ${image_name} "/usr/share/kata-containers/${image}"
(cd /usr/share/kata-containers && sudo ln -sf "$image" ${image_name})

popd
}

main() {
install_yq || build_image
}

main
122 changes: 122 additions & 0 deletions scripts/install_kata_kernel.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#!/bin/bash
#
# Copyright (c) 2017-2018 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
#

# Currently we will use this repository until this issue is solved
# See https://github.com/kata-containers/packaging/issues/1

set -e

cidir=$(dirname "$0")
source "${cidir}/lib.sh"
source "/etc/os-release"

repo_name="packaging"
repo_owner="kata-containers"
kata_kernel_dir="/usr/share/kata-containers"
kernel_arch="$(arch)"
get_kernel_url="https://cdn.kernel.org/pub/linux/kernel"
tmp_dir="$(mktemp -d)"
hypervisor="kvm"
packaged_kernel="kata-linux-container"

download_repo() {
pushd ${tmp_dir}
git clone --depth 1 https://github.com/${repo_owner}/${repo_name}
popd
}

get_current_kernel_version() {
kernel_version=$(get_version "assets.kernel.version")
echo "${kernel_version/v/}"
}

get_kata_config_version() {
pushd "${tmp_dir}/${repo_name}" >> /dev/null
kata_config_version=$(cat kernel/kata_config_version)
popd >> /dev/null
echo "${kata_config_version}"
}

get_packaged_kernel_version() {
if [ "$ID" == "ubuntu" ]; then
kernel_version=$(sudo apt-cache madison $packaged_kernel | awk '{print $3}' | cut -d'-' -f1)
elif [ "$ID" == "fedora" ]; then
kernel_version=$(sudo dnf --showduplicate list ${packaged_kernel}.${kernel_arch} | awk '/'$packaged_kernel'/ {print $2}' | cut -d'-' -f1)
elif [ "$ID" == "centos" ]; then
kernel_version=$(sudo yum --showduplicate list $packaged_kernel | awk '/'$packaged_kernel'/ {print $2}' | cut -d'-' -f1)
fi

if [ -z "$kernel_version" ]; then
die "unknown kernel version"
else
echo "${kernel_version}"
fi

}

# download the linux kernel, first argument is the kernel version
download_kernel() {
kernel_version=$1
pushd $tmp_dir
kernel_tar_file="linux-${kernel_version}.tar.xz"
kernel_url="${get_kernel_url}/v$(echo $kernel_version | cut -f1 -d.).x/${kernel_tar_file}"
curl -LOk ${kernel_url}
tar -xf ${kernel_tar_file}
popd
}

# build the linux kernel, first argument is the kernel version
build_and_install_kernel() {
kernel_version=$1
pushd ${tmp_dir}
kernel_config_file=$(realpath ${repo_name}/kernel/configs/[${kernel_arch}]*_kata_${hypervisor}_* | tail -1)
kernel_patches=$(realpath ${repo_name}/kernel/patches/*)
kernel_src_dir="linux-${kernel_version}"
pushd ${kernel_src_dir}
cp ${kernel_config_file} .config
for p in ${kernel_patches}; do patch -p1 < $p; done
make -s ARCH=${kernel_arch} oldconfig > /dev/null
if [ $CI == "true" ]; then
make ARCH=${kernel_arch} -j$(nproc)
else
make ARCH=${kernel_arch}
fi
sudo mkdir -p ${kata_kernel_dir}
sudo cp -a "$(realpath arch/${kernel_arch}/boot/bzImage)" "${kata_kernel_dir}/vmlinuz.container"
sudo cp -a "$(realpath vmlinux)" "${kata_kernel_dir}/vmlinux.container"
popd
popd
}

install_packaged_kernel(){
if [ "$ID" == "ubuntu" ]; then
sudo apt install -y "$packaged_kernel"
elif [ "$ID" == "fedora" ]; then
sudo dnf install -y "$packaged_kernel"
elif [ "$ID" == "centos" ]; then
sudo yum install -y "$packaged_kernel"
else
die "Unrecognized distro"
fi
}

cleanup() {
rm -rf "${tmp_dir}"
}

main() {
download_repo
kernel_version="$(get_current_kernel_version)"
kata_config_version="$(get_kata_config_version)"
current_kernel_version="${kernel_version}.${kata_config_version}"

download_kernel ${kernel_version}
build_and_install_kernel ${kernel_version}
cleanup
}

main
83 changes: 83 additions & 0 deletions scripts/install_qemu.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/bin/bash
#
# Copyright (c) 2018 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
#

set -e

cidir=$(dirname "$0")
source "${cidir}/lib.sh"
source /etc/os-release

CURRENT_QEMU_COMMIT=$(get_version "assets.hypervisor.qemu-lite.commit")
PACKAGED_QEMU="qemu-lite"
QEMU_ARCH=$(arch)

get_packaged_qemu_commit() {
if [ "$ID" == "ubuntu" ]; then
qemu_commit=$(sudo apt-cache madison $PACKAGED_QEMU \
| awk '{print $3}' | cut -d'-' -f1 | cut -d'.' -f4)
elif [ "$ID" == "fedora" ]; then
qemu_commit=$(sudo dnf --showduplicate list ${PACKAGED_QEMU}.${QEMU_ARCH} \
| awk '/'$PACKAGED_QEMU'/ {print $2}' | cut -d'-' -f1 | cut -d'.' -f4)
elif [ "$ID" == "centos" ]; then
qemu_commit=$(sudo yum --showduplicate list $PACKAGED_QEMU \
| awk '/'$PACKAGED_QEMU'/ {print $2}' | cut -d'-' -f1 | cut -d'.' -f4)
fi

if [ -z "$qemu_commit" ]; then
die "unknown qemu commit"
else
echo "${qemu_commit}"
fi
}

install_packaged_qemu() {
if [ "$ID" == "ubuntu" ]; then
sudo apt install -y "$PACKAGED_QEMU"
elif [ "$ID" == "fedora" ]; then
sudo dnf install -y "$PACKAGED_QEMU"
elif [ "$ID" == "centos" ]; then
sudo yum install -y "$PACKAGED_QEMU"
else
die "Unrecognized distro"
fi
}

build_and_install_qemu() {
QEMU_REPO=$(get_version "assets.hypervisor.qemu-lite.url")
# Remove 'https://' from the repo url to be able to clone the repo using 'go get'
QEMU_REPO=${QEMU_REPO/https:\/\//}
PACKAGING_REPO="github.com/kata-containers/packaging"
QEMU_CONFIG_SCRIPT="${GOPATH}/src/${PACKAGING_REPO}/scripts/configure-hypervisor.sh"

go get -d "${QEMU_REPO}" || true
go get -d "$PACKAGING_REPO" || true

pushd "${GOPATH}/src/${QEMU_REPO}"
git fetch
git checkout "$CURRENT_QEMU_COMMIT"
[ -d "capstone" ] || git clone https://github.com/qemu/capstone.git capstone
[ -d "ui/keycodemapdb" ] || git clone https://github.com/qemu/keycodemapdb.git ui/keycodemapdb

echo "Build Qemu"
"${QEMU_CONFIG_SCRIPT}" "qemu" | xargs ./configure
make -j $(nproc)

echo "Install Qemu"
sudo -E make install

# Add link from /usr/local/bin to /usr/bin
sudo ln -sf $(command -v qemu-system-${QEMU_ARCH}) "/usr/bin/qemu-lite-system-${QEMU_ARCH}"
popd
}

main() {
packaged_qemu_commit=$(get_packaged_qemu_commit)
short_current_qemu_commit=${CURRENT_QEMU_COMMIT:0:10}
build_and_install_qemu
}

main
11 changes: 11 additions & 0 deletions scripts/kata-env-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

set -e

cidir=$(dirname "$0")

source "${cidir}/lib.sh"

clone_repo "github.com/kata-containers/runtime"

${cidir}/install_kata_image.sh && ${cidir}/install_kata_kernel.sh && ${cidir}/install_qemu.sh
Loading

0 comments on commit 16bec09

Please sign in to comment.