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()