From f3e89d38a9d69cc44833736f89a97f96adfed478 Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Thu, 19 Apr 2018 15:55:34 +0100 Subject: [PATCH 01/14] refactor: Simplify enabling debug Simplify the logic to enable debug and also enable debug as early as possible. Signed-off-by: James O. D. Hunt --- image-builder/image_builder.sh | 6 ++---- initrd-builder/initrd_builder.sh | 6 ++---- rootfs-builder/rootfs.sh | 6 ++---- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/image-builder/image_builder.sh b/image-builder/image_builder.sh index c9561754a5..99d0b27eee 100755 --- a/image-builder/image_builder.sh +++ b/image-builder/image_builder.sh @@ -6,13 +6,11 @@ set -e +[ -n "$DEBUG" ] && set -x + script_name="${0##*/}" script_dir="$(dirname $(readlink -f $0))" -if [ -n "$DEBUG" ] ; then - set -x -fi - SCRIPT_NAME="${0##*/}" IMAGE="${IMAGE:-kata-containers.img}" AGENT_BIN=${AGENT_BIN:-kata-agent} diff --git a/initrd-builder/initrd_builder.sh b/initrd-builder/initrd_builder.sh index ab8383dfcb..eef7c79378 100755 --- a/initrd-builder/initrd_builder.sh +++ b/initrd-builder/initrd_builder.sh @@ -6,13 +6,11 @@ set -e +[ -n "$DEBUG" ] && set -x + script_name="${0##*/}" script_dir="$(dirname $(readlink -f $0))" -if [ -n "$DEBUG" ] ; then - set -x -fi - SCRIPT_NAME="${0##*/}" INITRD_IMAGE="${INITRD_IMAGE:-kata-containers-initrd.img}" AGENT_BIN=${AGENT_BIN:-kata-agent} diff --git a/rootfs-builder/rootfs.sh b/rootfs-builder/rootfs.sh index 292bb89251..031142abd0 100755 --- a/rootfs-builder/rootfs.sh +++ b/rootfs-builder/rootfs.sh @@ -6,6 +6,8 @@ set -e +[ -n "$DEBUG" ] && set -x + script_name="${0##*/}" script_dir="$(dirname $(readlink -f $0))" AGENT_VERSION=${AGENT_VERSION:-master} @@ -26,10 +28,6 @@ typeset -r CONFIG_SH="config.sh" # Name of the extra file that could implement build_rootfs typeset -r LIB_SH="rootfs_lib.sh" -if [ -n "$DEBUG" ] ; then - set -x -fi - #$1: Error code if want to exit differnt to 0 usage(){ error="${1:-0}" From a18753b2fff32499803c01249d59b98112476c83 Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Thu, 19 Apr 2018 16:00:37 +0100 Subject: [PATCH 02/14] refactor: Remove duplicate variable Some of the scripts had two variables to refer to the program name. Signed-off-by: James O. D. Hunt --- image-builder/image_builder.sh | 3 +-- initrd-builder/initrd_builder.sh | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/image-builder/image_builder.sh b/image-builder/image_builder.sh index 99d0b27eee..61015a3283 100755 --- a/image-builder/image_builder.sh +++ b/image-builder/image_builder.sh @@ -11,7 +11,6 @@ set -e script_name="${0##*/}" script_dir="$(dirname $(readlink -f $0))" -SCRIPT_NAME="${0##*/}" IMAGE="${IMAGE:-kata-containers.img}" AGENT_BIN=${AGENT_BIN:-kata-agent} AGENT_INIT=${AGENT_INIT:-no} @@ -45,7 +44,7 @@ usage() { error="${1:-0}" cat < +Usage: ${script_name} [options] This script will create a Kata Containers image file of an adequate size based on the directory. The size of the image can be also be specified manually diff --git a/initrd-builder/initrd_builder.sh b/initrd-builder/initrd_builder.sh index eef7c79378..23c8085639 100755 --- a/initrd-builder/initrd_builder.sh +++ b/initrd-builder/initrd_builder.sh @@ -11,7 +11,6 @@ set -e script_name="${0##*/}" script_dir="$(dirname $(readlink -f $0))" -SCRIPT_NAME="${0##*/}" INITRD_IMAGE="${INITRD_IMAGE:-kata-containers-initrd.img}" AGENT_BIN=${AGENT_BIN:-kata-agent} AGENT_INIT=${AGENT_INIT:-no} @@ -39,7 +38,7 @@ usage() { error="${1:-0}" cat < +Usage: ${script_name} [options] This script creates a Kata Containers initrd image file based on the directory. From 019a80f30434ac7e12ba712c2cfaf62798586131 Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Thu, 19 Apr 2018 16:05:00 +0100 Subject: [PATCH 03/14] refactor: Move more functions to script library Moved the display functions to the script library to avoid duplication. Signed-off-by: James O. D. Hunt --- image-builder/image_builder.sh | 28 +++------------------------- initrd-builder/initrd_builder.sh | 22 +++------------------- rootfs-builder/rootfs.sh | 26 +++----------------------- scripts/lib.sh | 28 +++++++++++++++++++++++++++- 4 files changed, 36 insertions(+), 68 deletions(-) diff --git a/image-builder/image_builder.sh b/image-builder/image_builder.sh index 61015a3283..c5d0ceb2f4 100755 --- a/image-builder/image_builder.sh +++ b/image-builder/image_builder.sh @@ -11,35 +11,13 @@ set -e script_name="${0##*/}" script_dir="$(dirname $(readlink -f $0))" +lib_file="${script_dir}/../scripts/lib.sh" +source "$lib_file" + IMAGE="${IMAGE:-kata-containers.img}" AGENT_BIN=${AGENT_BIN:-kata-agent} AGENT_INIT=${AGENT_INIT:-no} -die() -{ - local msg="$*" - echo "ERROR: ${msg}" >&2 - exit 1 -} - -OK() -{ - local msg="$*" - echo "[OK] ${msg}" >&2 -} - -info() -{ - local msg="$*" - echo "INFO: ${msg}" -} - -warning() -{ - local msg="$*" - echo "WARNING: ${msg}" -} - usage() { error="${1:-0}" diff --git a/initrd-builder/initrd_builder.sh b/initrd-builder/initrd_builder.sh index 23c8085639..3f42b812e9 100755 --- a/initrd-builder/initrd_builder.sh +++ b/initrd-builder/initrd_builder.sh @@ -11,29 +11,13 @@ set -e script_name="${0##*/}" script_dir="$(dirname $(readlink -f $0))" +lib_file="${script_dir}/../scripts/lib.sh" +source "$lib_file" + INITRD_IMAGE="${INITRD_IMAGE:-kata-containers-initrd.img}" AGENT_BIN=${AGENT_BIN:-kata-agent} AGENT_INIT=${AGENT_INIT:-no} -die() -{ - local msg="$*" - echo "ERROR: ${msg}" >&2 - exit 1 -} - -OK() -{ - local msg="$*" - echo "[OK] ${msg}" >&2 -} - -info() -{ - local msg="$*" - echo "INFO: ${msg}" -} - usage() { error="${1:-0}" diff --git a/rootfs-builder/rootfs.sh b/rootfs-builder/rootfs.sh index 031142abd0..badf1ebc5d 100755 --- a/rootfs-builder/rootfs.sh +++ b/rootfs-builder/rootfs.sh @@ -16,6 +16,9 @@ AGENT_BIN=${AGENT_BIN:-kata-agent} AGENT_INIT=${AGENT_INIT:-no} KERNEL_MODULES_DIR=${KERNEL_MODULES_DIR:-""} +lib_file="${script_dir}/../scripts/lib.sh" +source "$lib_file" + # Default architecture ARCH=${ARCH:-"x86_64"} @@ -60,25 +63,6 @@ EOT exit "${error}" } -die() -{ - msg="$*" - echo "ERROR: ${msg}" >&2 - exit 1 -} - -info() -{ - msg="$*" - echo "INFO: ${msg}" >&2 -} - -OK() -{ - msg="$*" - echo "INFO: [OK] ${msg}" >&2 -} - get_distros() { cdirs=$(find "${script_dir}" -maxdepth 1 -type d) find ${cdirs} -maxdepth 1 -name "${CONFIG_SH}" -printf '%H\n' | while read dir; do @@ -174,10 +158,6 @@ distro_config_dir="${script_dir}/${distro}" rootfs_config="${distro_config_dir}/${CONFIG_SH}" source "${rootfs_config}" -lib_file="${script_dir}/../scripts/lib.sh" -info "Source $lib_file" -[ -e "$lib_file" ] && source "$lib_file" || true - [ -d "${distro_config_dir}" ] || die "Not found configuration directory ${distro_config_dir}" if [ -z "$ROOTFS_DIR" ]; then diff --git a/scripts/lib.sh b/scripts/lib.sh index 6e02c6f00c..02aadfce41 100644 --- a/scripts/lib.sh +++ b/scripts/lib.sh @@ -6,7 +6,33 @@ set -e -check_program(){ +die() +{ + local msg="$*" + echo "ERROR: ${msg}" >&2 + exit 1 +} + +OK() +{ + local msg="$*" + echo "[OK] ${msg}" >&2 +} + +info() +{ + local msg="$*" + echo "INFO: ${msg}" +} + +warning() +{ + local msg="$*" + echo "WARNING: ${msg}" +} + +check_program() +{ type "$1" >/dev/null 2>&1 } From 48b1ddabed7bb9f7542eefd2a468dd37b91d9b58 Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Thu, 19 Apr 2018 16:19:42 +0100 Subject: [PATCH 04/14] rootfs: Fix comments Improve a few comments and fix a typo. Signed-off-by: James O. D. Hunt --- rootfs-builder/rootfs.sh | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/rootfs-builder/rootfs.sh b/rootfs-builder/rootfs.sh index badf1ebc5d..7d5f7c6d24 100755 --- a/rootfs-builder/rootfs.sh +++ b/rootfs-builder/rootfs.sh @@ -22,16 +22,17 @@ source "$lib_file" # Default architecture ARCH=${ARCH:-"x86_64"} -#Load default vesions for golang and other componets +# Load default versions for golang and other componets source "${script_dir}/versions.txt" -# config file +# distro-specific config file typeset -r CONFIG_SH="config.sh" -# Name of the extra file that could implement build_rootfs +# Name of an optional distro-specific file which, if it exists, must implement the +# build_rootfs() function. typeset -r LIB_SH="rootfs_lib.sh" -#$1: Error code if want to exit differnt to 0 +#$1: Error code if want to exit different to 0 usage(){ error="${1:-0}" cat < Date: Thu, 19 Apr 2018 16:20:51 +0100 Subject: [PATCH 05/14] rootfs: Reformat functions For consistency with the rest of the code, put the opening brace on the line below the function name. Signed-off-by: James O. D. Hunt --- rootfs-builder/rootfs.sh | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/rootfs-builder/rootfs.sh b/rootfs-builder/rootfs.sh index 7d5f7c6d24..9a7ecff9d0 100755 --- a/rootfs-builder/rootfs.sh +++ b/rootfs-builder/rootfs.sh @@ -33,7 +33,8 @@ typeset -r CONFIG_SH="config.sh" typeset -r LIB_SH="rootfs_lib.sh" #$1: Error code if want to exit different to 0 -usage(){ +usage() +{ error="${1:-0}" cat < Date: Thu, 19 Apr 2018 16:23:03 +0100 Subject: [PATCH 06/14] rootfs: Check function parameters Add some checks to ensure function parameters are set. This fixes a bug `copy_kernel_modules()` where a test would never fail due to missing dollars. Signed-off-by: James O. D. Hunt --- rootfs-builder/rootfs.sh | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/rootfs-builder/rootfs.sh b/rootfs-builder/rootfs.sh index 9a7ecff9d0..d838009b71 100755 --- a/rootfs-builder/rootfs.sh +++ b/rootfs-builder/rootfs.sh @@ -119,6 +119,10 @@ setup_agent_init() { agent_bin="$1" init_bin="$2" + + [ -z "$agent_bin" ] && die "need agent binary path" + [ -z "$init_bin" ] && die "need init bin path" + info "Install $agent_bin as init process" mv -f "${agent_bin}" ${init_bin} OK "Agent is installed as init process" @@ -126,10 +130,11 @@ setup_agent_init() copy_kernel_modules() { - local module_dir=$1 - local rootfs_dir=$2 + local module_dir="$1" + local rootfs_dir="$2" - [ -z "module_dir" -o -z "rootfs_dir" ] && die "module dir and rootfs dir must be specified" + [ -z "$module_dir" ] && die "need module directory" + [ -z "$rootfs_dir" ] && die "need rootfs directory" info "Copy kernel modules from ${KERNEL_MODULES_DIR}" mkdir -p ${rootfs_dir}/lib/modules/ From b8f1a688340c7b1cfe5a1cb1bbe6a792dc97acf8 Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Thu, 19 Apr 2018 16:24:38 +0100 Subject: [PATCH 07/14] rootfs: Simplify code Use a variable in `copy_kernel_modules()` to avoid the duplicated string. Signed-off-by: James O. D. Hunt --- rootfs-builder/rootfs.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/rootfs-builder/rootfs.sh b/rootfs-builder/rootfs.sh index d838009b71..517859d649 100755 --- a/rootfs-builder/rootfs.sh +++ b/rootfs-builder/rootfs.sh @@ -136,9 +136,11 @@ copy_kernel_modules() [ -z "$module_dir" ] && die "need module directory" [ -z "$rootfs_dir" ] && die "need rootfs directory" + local destdir="${rootfs_dir}/lib/modules" + info "Copy kernel modules from ${KERNEL_MODULES_DIR}" - mkdir -p ${rootfs_dir}/lib/modules/ - cp -a ${KERNEL_MODULES_DIR} ${rootfs_dir}/lib/modules/ + mkdir -p "${destdir}" + cp -a "${KERNEL_MODULES_DIR}" "${dest_dir}/" OK "Kernel modules copied" } From a2a65621a19e30f1967fc9befebf5b97a788c9c5 Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Thu, 19 Apr 2018 16:26:43 +0100 Subject: [PATCH 08/14] rootfs: Simplify code Use more variables to avoid duplication and make the code cleaner. Signed-off-by: James O. D. Hunt --- rootfs-builder/rootfs.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/rootfs-builder/rootfs.sh b/rootfs-builder/rootfs.sh index 517859d649..f457427c54 100755 --- a/rootfs-builder/rootfs.sh +++ b/rootfs-builder/rootfs.sh @@ -240,10 +240,13 @@ make clean make INIT=${AGENT_INIT} make install DESTDIR="${ROOTFS_DIR}" INIT=${AGENT_INIT} popd -[ -x "${ROOTFS_DIR}/usr/bin/${AGENT_BIN}" ] || die "/usr/bin/${AGENT_BIN} is not installed in ${ROOTFS_DIR}" + +AGENT_DIR="${ROOTFS_DIR}/usr/bin" +AGENT_DEST="${AGENT_DIR}/${AGENT_BIN}" +[ -x "${AGENT_DEST}" ] || die "${AGENT_DEST} is not installed in ${ROOTFS_DIR}" OK "Agent installed" -[ "${AGENT_INIT}" == "yes" ] && setup_agent_init "${ROOTFS_DIR}/usr/bin/${AGENT_BIN}" "${init}" +[ "${AGENT_INIT}" == "yes" ] && setup_agent_init "${AGENT_DEST}" "${init}" info "Check init is installed" [ -x "${init}" ] || [ -L "${init}" ] || die "/sbin/init is not installed in ${ROOTFS_DIR}" From 5b6ced536b948d345114c23afd74777ade7bf49e Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Thu, 19 Apr 2018 16:30:11 +0100 Subject: [PATCH 09/14] rootfs/clearlinux: Resolve version If the Clear Linux `OS_VERSION` is specified as `latest`, resolve to an actual release number. Signed-off-by: James O. D. Hunt --- rootfs-builder/clearlinux/config.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/rootfs-builder/clearlinux/config.sh b/rootfs-builder/clearlinux/config.sh index ae65459184..e9c3a9fe58 100644 --- a/rootfs-builder/clearlinux/config.sh +++ b/rootfs-builder/clearlinux/config.sh @@ -4,12 +4,16 @@ # SPDX-License-Identifier: Apache-2.0 OS_NAME="Clear" +REPO_NAME="clear" OS_VERSION=${OS_VERSION:-latest} -BASE_URL="https://download.clearlinux.org/current/${ARCH}/os/" +clr_url="https://download.clearlinux.org" -REPO_NAME="clear" +# resolve version +[ "${OS_VERSION}" = "latest" ] && OS_VERSION=$(curl -sL "${clr_url}/latest") + +BASE_URL="${clr_url}/releases/${OS_VERSION}/${REPO_NAME}/${ARCH}/os/" PACKAGES="iptables-bin libudev0-shim" From f17b5c29f3cfe5b665b3d91cfd0c185d94d21fb0 Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Thu, 19 Apr 2018 16:33:44 +0100 Subject: [PATCH 10/14] scripts/lib: Fix whitespace Make whitespace consistent. Signed-off-by: James O. D. Hunt --- scripts/lib.sh | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/scripts/lib.sh b/scripts/lib.sh index 02aadfce41..7405cb68c8 100644 --- a/scripts/lib.sh +++ b/scripts/lib.sh @@ -62,7 +62,7 @@ reposdir=/root/mash retries=5 EOF if [ "$BASE_URL" != "" ]; then - cat >> "${DNF_CONF}" << EOF + cat >> "${DNF_CONF}" << EOF [base] name=${OS_NAME}-${OS_VERSION} ${REPO_NAME} @@ -71,7 +71,7 @@ baseurl=${BASE_URL} enabled=1 EOF elif [ "$MIRROR_LIST" != "" ]; then - cat >> "${DNF_CONF}" << EOF + cat >> "${DNF_CONF}" << EOF [base] name=${OS_NAME}-${OS_VERSION} ${REPO_NAME} @@ -81,13 +81,12 @@ EOF fi if [ "$GPG_KEY_FILE" != "" ]; then - cat >> "${DNF_CONF}" << EOF + cat >> "${DNF_CONF}" << EOF gpgcheck=1 gpgkey=file://${CONFIG_DIR}/${GPG_KEY_FILE} EOF fi - } build_rootfs() From ddb71e8ef5d7dd990b749f8c631961ba2a0b6e19 Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Fri, 20 Apr 2018 11:14:21 +0100 Subject: [PATCH 11/14] initrd: Remove mention of USE_DOCKER Removed `USE_DOCKER` from the initrd builder usage statement as that builder does not use Docker. Signed-off-by: James O. D. Hunt --- initrd-builder/initrd_builder.sh | 3 --- 1 file changed, 3 deletions(-) diff --git a/initrd-builder/initrd_builder.sh b/initrd-builder/initrd_builder.sh index 3f42b812e9..56e3dacfe4 100755 --- a/initrd-builder/initrd_builder.sh +++ b/initrd-builder/initrd_builder.sh @@ -36,9 +36,6 @@ Extra environment variables: DEFAULT: kata-agent AGENT_INIT: use kata agent as init process DEFAULT: no - USE_DOCKER: If set, the image builds in a Docker Container. Setting - this variable requires Docker. - DEFAULT: not set EOT exit "${error}" } From b14d117a8923c3207738355d76fc71761201ff29 Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Fri, 20 Apr 2018 11:15:45 +0100 Subject: [PATCH 12/14] image-builder: Fix incorrect error message Fixed an error message which was referring to an incorrect rootfs variable name. Signed-off-by: James O. D. Hunt --- image-builder/image_builder.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/image-builder/image_builder.sh b/image-builder/image_builder.sh index c5d0ceb2f4..d484d40ccc 100755 --- a/image-builder/image_builder.sh +++ b/image-builder/image_builder.sh @@ -113,7 +113,7 @@ if [ -n "${USE_DOCKER}" ] ; then fi # The kata rootfs image expect init and kata-agent to be installed init="${ROOTFS}/sbin/init" -[ -x "${init}" ] || [ -L ${init} ] || die "/sbin/init is not installed in ${ROOTFS_DIR}" +[ -x "${init}" ] || [ -L ${init} ] || die "/sbin/init is not installed in ${ROOTFS}" OK "init is installed" [ "${AGENT_INIT}" == "yes" ] || [ -x "${ROOTFS}/usr/bin/${AGENT_BIN}" ] || \ die "/usr/bin/${AGENT_BIN} is not installed in ${ROOTFS} From 93b632c3289377f448fe050571673e932aaef952 Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Thu, 19 Apr 2018 16:36:53 +0100 Subject: [PATCH 13/14] lib: Check rootfs parameter Add a check on the rootfs parameter in `build_rootfs()`. Signed-off-by: James O. D. Hunt --- scripts/lib.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/lib.sh b/scripts/lib.sh index 7405cb68c8..ffc007e817 100644 --- a/scripts/lib.sh +++ b/scripts/lib.sh @@ -94,6 +94,8 @@ build_rootfs() # Mandatory local ROOTFS_DIR="$1" + [ -z "$ROOTFS_DIR" ] && die "need rootfs" + # In case of support EXTRA packages, use it to allow # users add more packages to the base rootfs local EXTRA_PKGS=${EXTRA_PKGS:-""} From f90f65247eb1599b036040d8979d0e13727d2bbc Mon Sep 17 00:00:00 2001 From: "James O. D. Hunt" Date: Thu, 19 Apr 2018 16:40:44 +0100 Subject: [PATCH 14/14] rootfs: Create a summary file inside the image Create a YAML metadata file inside the rootfs image containing information about the environment: ``` /var/lib/osbuilder/osbuilder.yaml ``` Example contents: ``` --- osbuilder: url: "https://github.com/kata-containers/osbuilder" version: "unknown" rootfs-creation-time: "2018-04-19T16:19:30.254610305+0000Z" description: "osbuilder rootfs" file-format-version: "0.0.1" architecture: "x86_64" base-distro: name: "Centos" version: "7" packages: - "iptables" - "systemd" agent: url: "https://github.com/kata-containers/agent" name: "kata-agent" version: "0.0.1-2ec0b9593845b9a5e0eab5a85b20d74c35a2ca52-dirty" agent-is-init-daemon: "no" ``` This change adds a new `-o` option to `rootfs.sh` for specifying the version of osbuilder to the rootfs builder. Fixes #35. Signed-off-by: James O. D. Hunt --- .ci/setup.sh | 5 +-- Makefile | 8 ++++- VERSION | 2 ++ image-builder/image_builder.sh | 1 + rootfs-builder/rootfs.sh | 12 ++++++- scripts/lib.sh | 62 ++++++++++++++++++++++++++++++++++ tests/image_creation.bats | 5 +++ 7 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 VERSION diff --git a/.ci/setup.sh b/.ci/setup.sh index 1bab32bf7c..c5f966886a 100755 --- a/.ci/setup.sh +++ b/.ci/setup.sh @@ -14,12 +14,13 @@ bash "${cidir}/static-checks.sh" source /etc/os-release if [ "$ID" == fedora ];then - sudo -E dnf -y install automake bats + sudo -E dnf -y install automake bats yamllint elif [ "$ID" == ubuntu ];then #bats isn't available for Ubuntu trusty, need for travis sudo add-apt-repository -y ppa:duggan/bats sudo apt-get -qq update - sudo apt-get install -y -qq automake bats qemu-utils + sudo apt-get install -y -qq automake bats qemu-utils python-pip + sudo pip install yamllint else echo "Linux distribution not supported" fi diff --git a/Makefile b/Makefile index ccc2ea53e0..4053ebe97d 100644 --- a/Makefile +++ b/Makefile @@ -10,10 +10,16 @@ DISTRO_ROOTFS := "$(PWD)/$(DISTRO)_rootfs" IMG_SIZE=500 AGENT_INIT ?= no +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_COMMIT := $(if $(COMMIT),$(VERSION)-$(COMMIT),$(VERSION)) + all: rootfs image initrd rootfs: @echo Creating rootfs based on "$(DISTRO)" - "$(MK_DIR)/rootfs-builder/rootfs.sh" -r "$(DISTRO_ROOTFS)" "$(DISTRO)" + "$(MK_DIR)/rootfs-builder/rootfs.sh" -o $(VERSION_COMMIT) -r "$(DISTRO_ROOTFS)" "$(DISTRO)" image: rootfs image-only diff --git a/VERSION b/VERSION new file mode 100644 index 0000000000..5bae440ccb --- /dev/null +++ b/VERSION @@ -0,0 +1,2 @@ +# This is the version of osbuilder. +0.0.1 diff --git a/image-builder/image_builder.sh b/image-builder/image_builder.sh index d484d40ccc..c843d35ebf 100755 --- a/image-builder/image_builder.sh +++ b/image-builder/image_builder.sh @@ -104,6 +104,7 @@ if [ -n "${USE_DOCKER}" ] ; then --env AGENT_INIT=${AGENT_INIT} \ -v /dev:/dev \ -v "${script_dir}":"/osbuilder" \ + -v "${script_dir}/../scripts":"/scripts" \ -v "${ROOTFS}":"/rootfs" \ -v "${IMAGE_DIR}":"/image" \ ${image_name} \ diff --git a/rootfs-builder/rootfs.sh b/rootfs-builder/rootfs.sh index f457427c54..f29d220db6 100755 --- a/rootfs-builder/rootfs.sh +++ b/rootfs-builder/rootfs.sh @@ -49,6 +49,7 @@ $(get_distros) Options: -a : agent version DEFAULT: ${AGENT_VERSION} ENV: AGENT_VERSION -h : Show this help message +-o : specify version of osbuilder -r : rootfs directory DEFAULT: ${ROOTFS_DIR} ENV: ROOTFS_DIR ENV VARIABLES: @@ -144,11 +145,14 @@ copy_kernel_modules() OK "Kernel modules copied" } -while getopts c:hr: opt +OSBUILDER_VERSION="unknown" + +while getopts c:ho:r: opt do case $opt in a) AGENT_VERSION="${OPTARG}" ;; h) usage ;; + o) OSBUILDER_VERSION="${OPTARG}" ;; r) ROOTFS_DIR="${OPTARG}" ;; esac done @@ -161,6 +165,8 @@ shift $(($OPTIND - 1)) [ -n "${KERNEL_MODULES_DIR}" ] && [ ! -d "${KERNEL_MODULES_DIR}" ] && die "KERNEL_MODULES_DIR defined but is not an existing directory" +[ -z "${OSBUILDER_VERSION}" ] && die "need osbuilder version" + distro="$1" [ -n "${distro}" ] || usage 1 @@ -214,6 +220,7 @@ if [ -n "${USE_DOCKER}" ] ; then --env GOPATH="${GOPATH}" \ --env KERNEL_MODULES_DIR="${KERNEL_MODULES_DIR}" \ --env EXTRA_PKGS="${EXTRA_PKGS}" \ + --env OSBUILDER_VERSION="${OSBUILDER_VERSION}" \ -v "${script_dir}":"/osbuilder" \ -v "${ROOTFS_DIR}":"/rootfs" \ -v "${script_dir}/../scripts":"/scripts" \ @@ -251,3 +258,6 @@ OK "Agent installed" info "Check init is installed" [ -x "${init}" ] || [ -L "${init}" ] || die "/sbin/init is not installed in ${ROOTFS_DIR}" OK "init is installed" + +info "Creating summary file" +create_summary_file "${ROOTFS_DIR}" diff --git a/scripts/lib.sh b/scripts/lib.sh index ffc007e817..f323002054 100644 --- a/scripts/lib.sh +++ b/scripts/lib.sh @@ -126,3 +126,65 @@ build_rootfs() [ -n "${ROOTFS_DIR}" ] && rm -r "${ROOTFS_DIR}${CACHE_DIR}" } + +# Create a YAML metadata file inside the rootfs. +# +# This provides useful information about the rootfs than can be interrogated +# once the rootfs has been converted into a image/initrd. +create_summary_file() +{ + local -r rootfs_dir="$1" + + [ -z "$rootfs_dir" ] && die "need rootfs" + + local -r file_dir="/var/lib/osbuilder" + local -r dir="${rootfs_dir}${file_dir}" + + local -r filename="osbuilder.yaml" + local file="${dir}/${filename}" + + local -r now=$(date '+%Y-%m-%dT%T.%N%zZ') + + # sanitise package list + PACKAGES=$(echo "$PACKAGES"|tr ' ' '\n'|sort -u|tr '\n' ' ') + + local -r packages=$(for pkg in ${PACKAGES}; do echo " - \"${pkg}\""; done) + + mkdir -p "$dir" + + # Semantic version of the summary file format. + # + # XXX: Increment every time the format of the summary file changes! + local -r format_version="0.0.1" + + local -r osbuilder_url="https://github.com/kata-containers/osbuilder" + + local agent="${AGENT_DEST}" + [ "$AGENT_INIT" = yes ] && agent="${init}" + + local -r agent_version=$("$agent" --version|awk '{print $NF}') + + cat >"$file"<<-EOT + --- + osbuilder: + url: "${osbuilder_url}" + version: "${OSBUILDER_VERSION}" + rootfs-creation-time: "${now}" + description: "osbuilder rootfs" + file-format-version: "${format_version}" + architecture: "${ARCH}" + base-distro: + name: "${OS_NAME}" + version: "${OS_VERSION}" + packages: +${packages} + agent: + url: "https://${GO_AGENT_PKG}" + name: "${AGENT_BIN}" + version: "${agent_version}" + agent-is-init-daemon: "${AGENT_INIT}" +EOT + + local rootfs_file="${file_dir}/$(basename "${file}")" + info "Created summary file '${rootfs_file}' inside rootfs" +} diff --git a/tests/image_creation.bats b/tests/image_creation.bats index 5ff97b8b56..724f098b94 100644 --- a/tests/image_creation.bats +++ b/tests/image_creation.bats @@ -27,7 +27,12 @@ teardown(){ function build_rootfs() { + local file="/var/lib/osbuilder/osbuilder.yaml" + local full="${tmp_rootfs}${file}" + sudo -E ${rootfs_sh} -r "${tmp_rootfs}" "${distro}" + + yamllint "${full}" } function build_image()