Skip to content

Commit

Permalink
rootfs: Create a summary file inside the image
Browse files Browse the repository at this point in the history
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 kata-containers#35.

Signed-off-by: James O. D. Hunt <[email protected]>
  • Loading branch information
jodh-intel committed Apr 20, 2018
1 parent 93b632c commit f90f652
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 4 deletions.
5 changes: 3 additions & 2 deletions .ci/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This is the version of osbuilder.
0.0.1
1 change: 1 addition & 0 deletions image-builder/image_builder.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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} \
Expand Down
12 changes: 11 additions & 1 deletion rootfs-builder/rootfs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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" \
Expand Down Expand Up @@ -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}"
62 changes: 62 additions & 0 deletions scripts/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
5 changes: 5 additions & 0 deletions tests/image_creation.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit f90f652

Please sign in to comment.