From 796f168c5c16e0980f0a52a88ff1c07b8028c131 Mon Sep 17 00:00:00 2001 From: Igor Malinovskiy Date: Wed, 16 Dec 2015 15:03:52 +0200 Subject: [PATCH] Add tox job for building LXD image Add additional tox job 'buildimage-lxd' which produces lxd metadata package and rootfs with installs nfs-ganesha, unfs3 and cifs. Co-Authored-By: Alexey Ovchinnikov Change-Id: Ia5a4701a828aeef93840f9c0a0e1d0bc0f4a5a2e --- bin/manila-image-create | 74 ++++++++++++++++++- data/lxd/meta/metadata.yaml | 56 ++++++++++++++ data/lxd/meta/templates/hostname.tpl | 1 + data/lxd/meta/templates/hosts.tpl | 10 +++ data/lxd/meta/templates/upstart-override.tpl | 1 + .../post-install.d/99-configure-network | 15 ++++ .../install.d/50-manila-ganesha-nfs | 35 +++++++++ .../manila-ganesha-nfs/install.d/nfs-ganesha | 71 ++++++++++++++++++ .../manila-ganesha-nfs/package-installs.yaml | 9 +++ .../environment.d/10-ubuntu-distro-name.bash | 1 + .../manila-ubuntu-core/package-installs.yaml | 1 - .../50-set-bash-as-default-shell | 1 + .../00-install-required-packages | 5 ++ .../manila-unfs3/install.d/50-manila-unfs3 | 31 ++++++++ elements/manila-unfs3/install.d/unfs3 | 43 +++++++++++ tools/add_to_rootfs_tar | 21 ++++++ tools/gate/build-images | 2 + tox.ini | 3 + 18 files changed, 375 insertions(+), 5 deletions(-) create mode 100644 data/lxd/meta/metadata.yaml create mode 100644 data/lxd/meta/templates/hostname.tpl create mode 100644 data/lxd/meta/templates/hosts.tpl create mode 100644 data/lxd/meta/templates/upstart-override.tpl create mode 100755 elements/manila-container/post-install.d/99-configure-network create mode 100755 elements/manila-ganesha-nfs/install.d/50-manila-ganesha-nfs create mode 100644 elements/manila-ganesha-nfs/install.d/nfs-ganesha create mode 100644 elements/manila-ganesha-nfs/package-installs.yaml delete mode 100755 elements/manila-ubuntu-core/package-installs.yaml create mode 100755 elements/manila-unfs3/install.d/50-manila-unfs3 create mode 100644 elements/manila-unfs3/install.d/unfs3 create mode 100755 tools/add_to_rootfs_tar diff --git a/bin/manila-image-create b/bin/manila-image-create index cdaea48..27f4ec6 100644 --- a/bin/manila-image-create +++ b/bin/manila-image-create @@ -13,6 +13,20 @@ else fi export ELEMENTS_PATH=$_PREFIX/elements +# Detect which image should be created +# generic - qcow image for VM +# lxd - tar for LXD +MANILA_IMAGE_TYPE=${1:-"generic"} + +if [ "$MANILA_IMAGE_TYPE" != "generic" ] && [ "$MANILA_IMAGE_TYPE" != "lxd" ]; then + echo -e "Image type $MANILA_IMAGE_TYPE is not supported." + exit 2 +fi + +echo +echo "Build image: $MANILA_IMAGE_TYPE" +echo + # Collect configuration # -------------------- # Development options: @@ -30,7 +44,8 @@ MANILA_USER_AUTHORIZED_KEYS="None" MANILA_IMG_ARCH=${MANILA_IMG_ARCH:-"i386"} MANILA_IMG_OS=${MANILA_IMG_OS:-"manila-ubuntu-core"} MANILA_IMG_OS_VER=${MANILA_IMG_OS_VER:-"trusty"} -MANILA_IMG_NAME=${MANILA_IMG_NAME:-"manila-service-image.qcow2"} +MANILA_IMG_NAME=${MANILA_IMG_NAME:-"manila-service-image"} +MANILA_LXD_METADATA_PACKAGE_NAME=${MANILA_LXD_METADATA_PACKAGE_NAME:-"manila-lxd-meta"} # Manila features MANILA_ENABLE_NFS_SUPPORT=${MANILA_ENABLE_NFS_SUPPORT:-"yes"} @@ -41,7 +56,15 @@ MANILA_ENABLE_ZFS_SUPPORT=${MANILA_ENABLE_ZFS_SUPPORT:-"yes"} # Verify configuration # -------------------- -REQUIRED_ELEMENTS="manila-ssh vm $MANILA_IMG_OS dhcp-all-interfaces cleanup-kernel-initrd" +if [ "$MANILA_IMAGE_TYPE" = "lxd" ]; then + REQUIRED_ELEMENTS="manila-container $MANILA_IMG_OS" + IMAGE_FORMAT="tar" + MANILA_ENABLE_ZFS_SUPPORT="no" + export INSTALL_KERNEL=false +else + REQUIRED_ELEMENTS="manila-ssh vm $MANILA_IMG_OS dhcp-all-interfaces cleanup-kernel-initrd" + IMAGE_FORMAT="qcow" +fi OPTIONAL_ELEMENTS= OPTIONAL_DIB_ARGS= @@ -50,7 +73,11 @@ if [ "$MANILA_ENABLE_CIFS_SUPPORT" != "yes" ] && [ "$MANILA_ENABLE_NFS_SUPPORT" fi if [ "$MANILA_ENABLE_NFS_SUPPORT" = "yes" ]; then - OPTIONAL_ELEMENTS="$OPTIONAL_ELEMENTS manila-nfs" + if [ "$MANILA_IMAGE_TYPE" = "lxd" ]; then + OPTIONAL_ELEMENTS="$OPTIONAL_ELEMENTS manila-ganesha-nfs manila-unfs3" + else + OPTIONAL_ELEMENTS="$OPTIONAL_ELEMENTS manila-nfs" + fi fi if [ "$MANILA_ENABLE_CIFS_SUPPORT" = "yes" ]; then @@ -184,5 +211,44 @@ export DIB_MANILA_USER_AUTHORIZED_KEYS=$MANILA_USER_AUTHORIZED_KEYS # Build image # ----------- -disk-image-create -a $MANILA_IMG_ARCH $OPTIONAL_DIB_ARGS -o $MANILA_IMG_NAME\ +disk-image-create \ + -t $IMAGE_FORMAT \ + -a $MANILA_IMG_ARCH \ + $OPTIONAL_DIB_ARGS \ + -o $MANILA_IMG_NAME \ $OPTIONAL_ELEMENTS $REQUIRED_ELEMENTS + +if [ "$MANILA_IMAGE_TYPE" = "lxd" ]; then + LXD_IMAGE_DIR=$_PREFIX/lxd-image.d + mkdir $LXD_IMAGE_DIR >/dev/null 2>&1 || rm -fR $LXD_IMAGE_DIR/* + mkdir $LXD_IMAGE_DIR/rootfs >/dev/null 2>&1 || rm -fR $LXD_IMAGE_DIR/rootfs/* + + # Create LXD metadata package + cp -R $_PREFIX/data/lxd/meta/* $LXD_IMAGE_DIR + + # Update creation timestamp in metafile + sed -i "s/%CREATION_DATE%/$(date +%s)/g" $LXD_IMAGE_DIR/metadata.yaml + + # Create package + echo "Creating LXD metadata package..." + tar -cf $_PREFIX/$MANILA_LXD_METADATA_PACKAGE_NAME.tar \ + -C $LXD_IMAGE_DIR templates metadata.yaml + echo "Done. Metadata could be found here: $_PREFIX/$MANILA_LXD_METADATA_PACKAGE_NAME.tar" + + # Create sys and proc directories required by LXD + mkdir -p $LXD_IMAGE_DIR/tmp/sys >/dev/null 2>&1 || echo "sys folder already exists in rootfs." + mkdir -p $LXD_IMAGE_DIR/tmp/proc >/dev/null 2>&1 || echo "proc folder already exists in rootfs." + + sudo $_PREFIX/tools/add_to_rootfs_tar $_PREFIX/$MANILA_IMG_NAME.tar \ + $LXD_IMAGE_DIR/tmp/sys /./sys/ + + sudo $_PREFIX/tools/add_to_rootfs_tar $_PREFIX/$MANILA_IMG_NAME.tar \ + $LXD_IMAGE_DIR/tmp/proc /./proc/ + + rm -fR $LXD_IMAGE_DIR/tmp + + # Compress rootfs + echo "Compressing rootfs package..." + sudo xz -f $_PREFIX/$MANILA_IMG_NAME.tar + echo "Done. rootfs could be found here: $_PREFIX/$MANILA_IMG_NAME.tar.xz" +fi diff --git a/data/lxd/meta/metadata.yaml b/data/lxd/meta/metadata.yaml new file mode 100644 index 0000000..81bf45a --- /dev/null +++ b/data/lxd/meta/metadata.yaml @@ -0,0 +1,56 @@ +{ + "architecture": "i686", + "creation_date": %CREATION_DATE%, + "properties": { + "architecture": "i686", + "description": "Openstack Manila LXD image (ubuntu trusty i686).", + "name": "manila-lxd-ubuntu-trusty-i686", + "os": "ubuntu", + "release": "trusty", + "variant": "default" + }, + "templates": { + "/etc/hostname": { + "template": "hostname.tpl", + "when": [ + "create" + ] + }, + "/etc/hosts": { + "template": "hosts.tpl", + "when": [ + "create" + ] + }, + "/etc/init/console.override": { + "template": "upstart-override.tpl", + "when": [ + "create" + ] + }, + "/etc/init/tty1.override": { + "template": "upstart-override.tpl", + "when": [ + "create" + ] + }, + "/etc/init/tty2.override": { + "template": "upstart-override.tpl", + "when": [ + "create" + ] + }, + "/etc/init/tty3.override": { + "template": "upstart-override.tpl", + "when": [ + "create" + ] + }, + "/etc/init/tty4.override": { + "template": "upstart-override.tpl", + "when": [ + "create" + ] + } + } +} diff --git a/data/lxd/meta/templates/hostname.tpl b/data/lxd/meta/templates/hostname.tpl new file mode 100644 index 0000000..69a84f1 --- /dev/null +++ b/data/lxd/meta/templates/hostname.tpl @@ -0,0 +1 @@ +{{ container.name }} diff --git a/data/lxd/meta/templates/hosts.tpl b/data/lxd/meta/templates/hosts.tpl new file mode 100644 index 0000000..6e6651f --- /dev/null +++ b/data/lxd/meta/templates/hosts.tpl @@ -0,0 +1,10 @@ +127.0.0.1 localhost +127.0.0.1 localhost.localdomain +127.0.1.1 {{ container.name }} + +# The following lines are desirable for IPv6 capable hosts +::1 ip6-localhost ip6-loopback +fe00::0 ip6-localnet +ff00::0 ip6-mcastprefix +ff02::1 ip6-allnodes +ff02::2 ip6-allrouters diff --git a/data/lxd/meta/templates/upstart-override.tpl b/data/lxd/meta/templates/upstart-override.tpl new file mode 100644 index 0000000..aabfa58 --- /dev/null +++ b/data/lxd/meta/templates/upstart-override.tpl @@ -0,0 +1 @@ +manual \ No newline at end of file diff --git a/elements/manila-container/post-install.d/99-configure-network b/elements/manila-container/post-install.d/99-configure-network new file mode 100755 index 0000000..0bd14bc --- /dev/null +++ b/elements/manila-container/post-install.d/99-configure-network @@ -0,0 +1,15 @@ +#!/bin/bash + +if [ ${DIB_DEBUG_TRACE:-0} -gt 0 ]; then + set -x +fi +set -eu +set -o pipefail + +echo "" > /etc/network/interfaces +echo "auto lo" >> /etc/network/interfaces +echo "iface lo inet loopback" >> /etc/network/interfaces + +echo "" >> /etc/network/interfaces +echo "auto eth0" >> /etc/network/interfaces +echo "iface eth0 inet dhcp" >> /etc/network/interfaces diff --git a/elements/manila-ganesha-nfs/install.d/50-manila-ganesha-nfs b/elements/manila-ganesha-nfs/install.d/50-manila-ganesha-nfs new file mode 100755 index 0000000..5d97a8c --- /dev/null +++ b/elements/manila-ganesha-nfs/install.d/50-manila-ganesha-nfs @@ -0,0 +1,35 @@ +#!/bin/bash + +if [ ${DIB_DEBUG_TRACE:-0} -gt 0 ]; then + set -x +fi +set -eu +set -o pipefail + +SCRIPTDIR=$(dirname $0) + +# Install required packages +install-packages git-core cmake build-essential libkrb5-dev bison flex + +# Build nfs-ganesha +cd /usr/src +# TODO(u_glide): Move branch to config option through "environment.d" +git config --global http.sslVerify false +git clone --depth 5 --recursive -b "V2.2-stable" \ + https://github.com/nfs-ganesha/nfs-ganesha.git +cd nfs-ganesha +mkdir build && cd build +cmake ../src && make + +# Install nfs-ganesha +sudo make install + +# (aovchinnikov): This will be used later on when all peculiarities of +# running ganesha inside a container are sorted out. +#sudo cp ../src/scripts/ganeshactl/org.ganesha.nfsd.conf /etc/dbus-1/system.d/ +#install -D -g root -o root -m 0755 \ +# ${SCRIPTDIR}/nfs-ganesha /etc/init.d/nfs-ganesha +#sudo update-rc.d nfs-ganesha defaults + +# Remove sources +cd /usr/src && rm -fR nfs-ganesha diff --git a/elements/manila-ganesha-nfs/install.d/nfs-ganesha b/elements/manila-ganesha-nfs/install.d/nfs-ganesha new file mode 100644 index 0000000..e5f2a71 --- /dev/null +++ b/elements/manila-ganesha-nfs/install.d/nfs-ganesha @@ -0,0 +1,71 @@ +#!/bin/bash + +# nfs-ganesha init script for ubuntu + + +# source function library +. /lib/lsb/init-functions + +PATHPROG=/usr/bin/ganesha.nfsd + +LOGFILE=/var/log/ganesha.log +CONFFILE=/etc/ganesha/ganesha.conf + +prog=ganesha.nfsd +PID_FILE=${PID_FILE:=/var/run/${prog}.pid} +LOCK_FILE=${LOCK_FILE:=/var/lock/subsys/${prog}} + +[ -f /etc/sysconfig/ganesha ] && . /etc/sysconfig/ganesha + +OPTIONS="-L $LOGFILE -f $CONFFILE -N NIV_EVENT" +RETVAL=0 + + +start() { + log_daemon_msg "Starting $prog" + + start-stop-daemon \ + --start --oknodo --pidfile "$PID_FILE" --exec $PATHPROG -- $OPTIONS + + status=$? + [ $status = 0 ] + log_end_msg $status + echo +} + +stop() { + log_daemon_msg "Stopping $prog" + start-stop-daemon \ + --stop --quiet --retry 5 --oknodo --pidfile $PID_FILE + status=$? + log_end_msg $status +} + +case "$1" in + start) + start + ;; + stop) + stop + ;; + restart) + stop + sleep 3 + start + ;; + status) + if pidof ganesha.nfsd >/dev/null + then + echo "ganesha.nfsd running" + exit 0 + else + echo "ganesha.nfsd not running" + exit 3 + fi + ;; + *) + echo $"Usage: $0 {start|stop|restart|reload|try-restart|status}" + RETVAL=1 +esac + +exit $RETVAL diff --git a/elements/manila-ganesha-nfs/package-installs.yaml b/elements/manila-ganesha-nfs/package-installs.yaml new file mode 100644 index 0000000..d108109 --- /dev/null +++ b/elements/manila-ganesha-nfs/package-installs.yaml @@ -0,0 +1,9 @@ +cmake: + phase: post-install.d + uninstall: True +build-essential: + phase: post-install.d + uninstall: True +git-core: + phase: post-install.d + uninstall: True diff --git a/elements/manila-ubuntu-core/environment.d/10-ubuntu-distro-name.bash b/elements/manila-ubuntu-core/environment.d/10-ubuntu-distro-name.bash index 2860427..7964fa7 100755 --- a/elements/manila-ubuntu-core/environment.d/10-ubuntu-distro-name.bash +++ b/elements/manila-ubuntu-core/environment.d/10-ubuntu-distro-name.bash @@ -1,2 +1,3 @@ export DISTRO_NAME=ubuntu export DIB_RELEASE=${DIB_RELEASE:-trusty} +export INSTALL_KERNEL=${INSTALL_KERNEL:-true} diff --git a/elements/manila-ubuntu-core/package-installs.yaml b/elements/manila-ubuntu-core/package-installs.yaml deleted file mode 100755 index 83f64fc..0000000 --- a/elements/manila-ubuntu-core/package-installs.yaml +++ /dev/null @@ -1 +0,0 @@ -linux-image-generic: diff --git a/elements/manila-ubuntu-core/post-install.d/50-set-bash-as-default-shell b/elements/manila-ubuntu-core/post-install.d/50-set-bash-as-default-shell index a907120..6301fd2 100755 --- a/elements/manila-ubuntu-core/post-install.d/50-set-bash-as-default-shell +++ b/elements/manila-ubuntu-core/post-install.d/50-set-bash-as-default-shell @@ -7,4 +7,5 @@ set -eu set -o pipefail # Set 'bash' as default shell for 'manila' user. +id -u manila &>/dev/null || adduser manila --gecos Foo,Foo,Foo,Foo --disabled-password --quiet sudo chsh -s /bin/bash manila diff --git a/elements/manila-ubuntu-core/pre-install.d/00-install-required-packages b/elements/manila-ubuntu-core/pre-install.d/00-install-required-packages index a616826..3c8cbce 100755 --- a/elements/manila-ubuntu-core/pre-install.d/00-install-required-packages +++ b/elements/manila-ubuntu-core/pre-install.d/00-install-required-packages @@ -10,6 +10,11 @@ sudo sed -i -E "s/#\s(deb.+universe)$/\1/g" /etc/apt/sources.list apt-get update apt-get upgrade -y + +if $INSTALL_KERNEL ; then + apt-get install linux-image-generic -y +fi + apt-get install \ python \ sudo \ diff --git a/elements/manila-unfs3/install.d/50-manila-unfs3 b/elements/manila-unfs3/install.d/50-manila-unfs3 new file mode 100755 index 0000000..07524f7 --- /dev/null +++ b/elements/manila-unfs3/install.d/50-manila-unfs3 @@ -0,0 +1,31 @@ +#!/bin/bash + +if [ ${DIB_DEBUG_TRACE:-0} -gt 0 ]; then + set -x +fi +set -eu +set -o pipefail + +SCRIPTDIR=$(dirname $0) + +# Install required packages +install-packages wget + +# Build unfs3 +cd /usr/src +mkdir unfs3 && cd unfs3 +wget --no-check-certificate https://sourceforge.net/projects/unfs3/files/unfs3/0.9.22/unfs3-0.9.22.tar.gz +tar -xzf unfs3-0.9.22.tar.gz && cd unfs3-0.9.22 +./configure +make +make install + +# Prepare /etc/exports +echo "# unfs3 exports should be defined here" >> /etc/exports + +install -D -g root -o root -m 0755 \ + ${SCRIPTDIR}/unfs3 /etc/init.d/unfs3 +sudo update-rc.d unfs3 defaults + +# Remove sources +cd /usr/src && rm -fR unfs3 diff --git a/elements/manila-unfs3/install.d/unfs3 b/elements/manila-unfs3/install.d/unfs3 new file mode 100644 index 0000000..09a02b7 --- /dev/null +++ b/elements/manila-unfs3/install.d/unfs3 @@ -0,0 +1,43 @@ +#!/bin/sh + +SCRIPT=/usr/local/sbin/unfsd +RUNAS=root + +PIDFILE=/var/run/unfs3.pid +LOGFILE=/var/log/unfs3.log + +start() { + if [ -f /var/run/$PIDNAME ] && kill -0 $(cat /var/run/$PIDNAME); then + echo 'Service already running' >&2 + return 1 + fi + echo 'Starting service...' >&2 + local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!" + su -c "$CMD" $RUNAS > "$PIDFILE" + echo 'Service started' >&2 +} + +stop() { + if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then + echo 'Service not running' >&2 + return 1 + fi + echo 'Stopping service...' >&2 + kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE" + echo 'Service stopped' >&2 +} + +case "$1" in + start) + start + ;; + stop) + stop + ;; + restart) + stop + start + ;; + *) + echo "Usage: $0 {start|stop|restart}" +esac diff --git a/tools/add_to_rootfs_tar b/tools/add_to_rootfs_tar new file mode 100755 index 0000000..e5c822d --- /dev/null +++ b/tools/add_to_rootfs_tar @@ -0,0 +1,21 @@ +#!/usr/bin/env python + +from __future__ import print_function +import tarfile +import sys + + +def main(tar_path, folder_path, target_name): + try: + tar = tarfile.open(tar_path, mode="a") + tar.add(folder_path, arcname=target_name) + tar.close() + except Exception as e: + print("Error: ", e) + +if __name__ == "__main__": + if len(sys.argv) != 4: + print("Usage: " + "add_to_rootfs_tar ") + + main(sys.argv[1], sys.argv[2], sys.argv[3]) \ No newline at end of file diff --git a/tools/gate/build-images b/tools/gate/build-images index c8327c9..a916b4d 100755 --- a/tools/gate/build-images +++ b/tools/gate/build-images @@ -4,4 +4,6 @@ IMAGE=$1 if [ "$IMAGE" = "generic" ]; then tox -v -e buildimage +elif [ "$IMAGE" = "lxd" ]; then + tox -v -e buildimage-lxd fi diff --git a/tox.ini b/tox.ini index 70e2edb..d3bef25 100644 --- a/tox.ini +++ b/tox.ini @@ -32,6 +32,9 @@ commands = manila-image-create deps = -r{toxinidir}/requirements.txt +[testenv:buildimage-lxd] +commands = manila-image-create lxd + [flake8] show-source = true builtins = _