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 <james.o.hunt@intel.com>
This commit is contained in:
James O. D. Hunt 2018-04-19 16:40:44 +01:00
parent 93b632c328
commit f90f65247e
7 changed files with 91 additions and 4 deletions

View File

@ -14,12 +14,13 @@ bash "${cidir}/static-checks.sh"
source /etc/os-release source /etc/os-release
if [ "$ID" == fedora ];then if [ "$ID" == fedora ];then
sudo -E dnf -y install automake bats sudo -E dnf -y install automake bats yamllint
elif [ "$ID" == ubuntu ];then elif [ "$ID" == ubuntu ];then
#bats isn't available for Ubuntu trusty, need for travis #bats isn't available for Ubuntu trusty, need for travis
sudo add-apt-repository -y ppa:duggan/bats sudo add-apt-repository -y ppa:duggan/bats
sudo apt-get -qq update 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 else
echo "Linux distribution not supported" echo "Linux distribution not supported"
fi fi

View File

@ -10,10 +10,16 @@ DISTRO_ROOTFS := "$(PWD)/$(DISTRO)_rootfs"
IMG_SIZE=500 IMG_SIZE=500
AGENT_INIT ?= no 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 all: rootfs image initrd
rootfs: rootfs:
@echo Creating rootfs based on "$(DISTRO)" @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 image: rootfs image-only

2
VERSION Normal file
View File

@ -0,0 +1,2 @@
# This is the version of osbuilder.
0.0.1

View File

@ -104,6 +104,7 @@ if [ -n "${USE_DOCKER}" ] ; then
--env AGENT_INIT=${AGENT_INIT} \ --env AGENT_INIT=${AGENT_INIT} \
-v /dev:/dev \ -v /dev:/dev \
-v "${script_dir}":"/osbuilder" \ -v "${script_dir}":"/osbuilder" \
-v "${script_dir}/../scripts":"/scripts" \
-v "${ROOTFS}":"/rootfs" \ -v "${ROOTFS}":"/rootfs" \
-v "${IMAGE_DIR}":"/image" \ -v "${IMAGE_DIR}":"/image" \
${image_name} \ ${image_name} \

View File

@ -49,6 +49,7 @@ $(get_distros)
Options: Options:
-a : agent version DEFAULT: ${AGENT_VERSION} ENV: AGENT_VERSION -a : agent version DEFAULT: ${AGENT_VERSION} ENV: AGENT_VERSION
-h : Show this help message -h : Show this help message
-o : specify version of osbuilder
-r : rootfs directory DEFAULT: ${ROOTFS_DIR} ENV: ROOTFS_DIR -r : rootfs directory DEFAULT: ${ROOTFS_DIR} ENV: ROOTFS_DIR
ENV VARIABLES: ENV VARIABLES:
@ -144,11 +145,14 @@ copy_kernel_modules()
OK "Kernel modules copied" OK "Kernel modules copied"
} }
while getopts c:hr: opt OSBUILDER_VERSION="unknown"
while getopts c:ho:r: opt
do do
case $opt in case $opt in
a) AGENT_VERSION="${OPTARG}" ;; a) AGENT_VERSION="${OPTARG}" ;;
h) usage ;; h) usage ;;
o) OSBUILDER_VERSION="${OPTARG}" ;;
r) ROOTFS_DIR="${OPTARG}" ;; r) ROOTFS_DIR="${OPTARG}" ;;
esac esac
done 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" [ -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" distro="$1"
[ -n "${distro}" ] || usage 1 [ -n "${distro}" ] || usage 1
@ -214,6 +220,7 @@ if [ -n "${USE_DOCKER}" ] ; then
--env GOPATH="${GOPATH}" \ --env GOPATH="${GOPATH}" \
--env KERNEL_MODULES_DIR="${KERNEL_MODULES_DIR}" \ --env KERNEL_MODULES_DIR="${KERNEL_MODULES_DIR}" \
--env EXTRA_PKGS="${EXTRA_PKGS}" \ --env EXTRA_PKGS="${EXTRA_PKGS}" \
--env OSBUILDER_VERSION="${OSBUILDER_VERSION}" \
-v "${script_dir}":"/osbuilder" \ -v "${script_dir}":"/osbuilder" \
-v "${ROOTFS_DIR}":"/rootfs" \ -v "${ROOTFS_DIR}":"/rootfs" \
-v "${script_dir}/../scripts":"/scripts" \ -v "${script_dir}/../scripts":"/scripts" \
@ -251,3 +258,6 @@ OK "Agent installed"
info "Check init is installed" info "Check init is installed"
[ -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_DIR}"
OK "init is installed" OK "init is installed"
info "Creating summary file"
create_summary_file "${ROOTFS_DIR}"

View File

@ -126,3 +126,65 @@ build_rootfs()
[ -n "${ROOTFS_DIR}" ] && rm -r "${ROOTFS_DIR}${CACHE_DIR}" [ -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"
}

View File

@ -27,7 +27,12 @@ teardown(){
function build_rootfs() function build_rootfs()
{ {
local file="/var/lib/osbuilder/osbuilder.yaml"
local full="${tmp_rootfs}${file}"
sudo -E ${rootfs_sh} -r "${tmp_rootfs}" "${distro}" sudo -E ${rootfs_sh} -r "${tmp_rootfs}" "${distro}"
yamllint "${full}"
} }
function build_image() function build_image()