From 94e7b1da4f8b469a2cf5f21ff4cdf98e11fa96a0 Mon Sep 17 00:00:00 2001 From: Peng Tao Date: Tue, 30 Jan 2018 17:05:56 +0800 Subject: [PATCH] rootfs: allow to use agent as init process Add AGENT_INIT env to make it configurable. Signed-off-by: Peng Tao --- Makefile | 5 ++++- image-builder/image_builder.sh | 6 ++++-- rootfs-builder/rootfs.sh | 31 ++++++++++++++++++++++++------- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 7637ea7799..f042d5258d 100644 --- a/Makefile +++ b/Makefile @@ -8,9 +8,12 @@ MK_DIR :=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) DISTRO ?= centos DISTRO_ROOTFS := "$(PWD)/$(DISTRO)_rootfs" IMG_SIZE=500 +AGENT_INIT ?= no -image: +rootfs: @echo Creating rootfs based on "$(DISTRO)" "$(MK_DIR)/rootfs-builder/rootfs.sh" -r "$(DISTRO_ROOTFS)" "$(DISTRO)" + +image: rootfs @echo Creating image based on "$(DISTRO_ROOTFS)" AGENT_BIN="$(AGENT_BIN)" "$(MK_DIR)/image-builder/image_builder.sh" -s "$(IMG_SIZE)" "$(DISTRO_ROOTFS)" diff --git a/image-builder/image_builder.sh b/image-builder/image_builder.sh index 12db51a010..60982650aa 100755 --- a/image-builder/image_builder.sh +++ b/image-builder/image_builder.sh @@ -16,6 +16,7 @@ fi SCRIPT_NAME="${0##*/}" IMAGE="${IMAGE:-kata-containers.img}" AGENT_BIN=${AGENT_BIN:-kata-agent} +AGENT_INIT=${AGENT_INIT:-no} die() { @@ -50,7 +51,8 @@ Options: -s Image size in MB (default $IMG_SIZE) ENV: IMG_SIZE Extra environment variables: - AGENT_BIN: use it to change the expected agent binary name" + AGENT_BIN: use it to change the expected agent binary name + AGENT_INIT: use kata agent as init process USE_DOCKER: If set will build image in a Docker Container (requries docker) DEFAULT: not set EOT @@ -107,7 +109,7 @@ fi init="${ROOTFS}/sbin/init" [ -x "${init}" ] || [ -L ${init} ] || die "/sbin/init is not installed in ${ROOTFS_DIR}" OK "init is installed" -[ -x "${ROOTFS}/bin/${AGENT_BIN}" ] || \ +[ "${AGENT_INIT}" == "yes" ] || [ -x "${ROOTFS}/bin/${AGENT_BIN}" ] || \ die "/bin/${AGENT_BIN} is not installed in ${ROOTFS} use AGENT_BIN env variable to change the expected agent binary name" OK "Agent installed" diff --git a/rootfs-builder/rootfs.sh b/rootfs-builder/rootfs.sh index 0175d72940..0490c069bb 100755 --- a/rootfs-builder/rootfs.sh +++ b/rootfs-builder/rootfs.sh @@ -12,6 +12,8 @@ ROOTFS_DIR=${ROOTFS_DIR:-${PWD}/rootfs} AGENT_VERSION=${AGENT_VERSION:-master} GO_AGENT_PKG=${GO_AGENT_PKG:-github.com/kata-containers/agent} AGENT_BIN=${AGENT_BIN:-kata-agent} +AGENT_INIT=${AGENT_INIT:-no} + #Load default vesions for golang and other componets source "${script_dir}/versions.txt" @@ -46,6 +48,8 @@ GO_AGENT_PKG: Change the golang package url to get the agent source code AGENT_BIN : Name of the agent binary (needed to check if agent is installed) USE_DOCKER: If set will build rootfs in a Docker Container (requries docker) DEFAULT: not set +AGENT_INIT : Use $(AGENT_BIN) as init process. + DEFAULT: no EOT exit "${error}" } @@ -103,6 +107,14 @@ ENV PATH=\$PATH:\$GOROOT/bin:\$GOPATH/bin popd } +setup_agent_init() { + agent_bin="$1" + init_bin="$2" + info "Install $agent_bin as init process" + mv -f "${agent_bin}" ${init_bin} + OK "Agent is installed as init process" +} + while getopts c:hr: opt do @@ -117,7 +129,10 @@ shift $(($OPTIND - 1)) [ -z "$GOPATH" ] && die "GOPATH not set" +[ "$AGENT_INIT" == "yes" -o "$AGENT_INIT" == "no" ] || die "AGENT_INIT($AGENT_INIT) is invalid (must be yes or no)" + distro="$1" +init="${ROOTFS_DIR}/sbin/init" [ -n "${distro}" ] || usage 1 distro_config_dir="${script_dir}/${distro}" @@ -151,6 +166,7 @@ if [ -n "${USE_DOCKER}" ] ; then --env ROOTFS_DIR="/rootfs" \ --env GO_AGENT_PKG="${GO_AGENT_PKG}" \ --env AGENT_BIN="${AGENT_BIN}" \ + --env AGENT_INIT="${AGENT_INIT}" \ --env GOPATH="${GOPATH}" \ -v "${script_dir}":"/osbuilder" \ -v "${ROOTFS_DIR}":"/rootfs" \ @@ -164,19 +180,20 @@ fi mkdir -p ${ROOTFS_DIR} build_rootfs ${ROOTFS_DIR} -info "Check init is installed" -init="${ROOTFS_DIR}/sbin/init" -[ -x "${init}" ] || [ -L ${init} ] || die "/sbin/init is not installed in ${ROOTFS_DIR}" -OK "init is installed" - info "Pull Agent source code" go get -d "${GO_AGENT_PKG}" || true OK "Pull Agent source code" info "Build agent" pushd "${GOPATH}/src/${GO_AGENT_PKG}" -make INIT=no -make install DESTDIR="${ROOTFS_DIR}" INIT=no +make INIT=${AGENT_INIT} +make install DESTDIR="${ROOTFS_DIR}" INIT=${AGENT_INIT} popd [ -x "${ROOTFS_DIR}/bin/${AGENT_BIN}" ] || die "/bin/${AGENT_BIN} is not installed in ${ROOTFS_DIR}" OK "Agent installed" + +[ "${AGENT_INIT}" == "yes" ] && setup_agent_init "${ROOTFS_DIR}/bin/${AGENT_BIN}" "${init}" + +info "Check init is installed" +[ -x "${init}" ] || [ -L ${init} ] || die "/sbin/init is not installed in ${ROOTFS_DIR}" +OK "init is installed"