From 4198d07804f17b99f811a1a3e8ec18ad24172f16 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 17 Jul 2016 18:14:48 +0200 Subject: Adding host system based overlay mounts. Signed-off-by: Daniel Baumann --- lib/container/create | 29 ++++++++++++++++++++-- lib/container/start | 25 +++++++++++++++++++ lib/container/stop | 23 ++++++++++++++++++ share/config/container.conf.in | 1 + share/doc/examples/cairon-backports.cfg | 1 + share/man/container-create.1.txt | 3 +++ share/scripts/debconf | 43 +++++++++++++++++++++++++++++++++ share/scripts/debconf.d/0003-debconf | 7 ++++++ 8 files changed, 130 insertions(+), 2 deletions(-) diff --git a/lib/container/create b/lib/container/create index 8080ffa..d1ef901 100755 --- a/lib/container/create +++ b/lib/container/create @@ -26,7 +26,7 @@ SCRIPTS="/usr/share/container-tools/scripts" Parameters () { - LONG_OPTIONS="name:,cnt.auto:,bind:,capability:,drop-capability:script:," + LONG_OPTIONS="name:,cnt.auto:,cnt.overlay:,bind:,capability:,drop-capability:script:," OPTIONS="n:,b:,c:,d:,s:," PARAMETERS="$(getopt --longoptions ${LONG_OPTIONS} --name=${COMMAND} --options ${OPTIONS} --shell sh -- ${@})" @@ -52,6 +52,11 @@ Parameters () shift 2 ;; + --cnt.overlay) + CNT_OVERLAY="${2}" + shift 2 + ;; + -b|--bind) BIND="${2}" shift 2 @@ -87,7 +92,7 @@ Parameters () Usage () { - echo "Usage: container ${COMMAND} -n|--name NAME [--cnt.auto=true|false|FQDN] [-b|--bind DIRECTORY:DIRECTORY[:OPTIONS]] [-c|--capability CAPABILITY[,CAPABILITY]] [-d|--drop-capability DROP_CAPABILITY[,DROP_CAPABILITY]] [-s|--script SCRIPT] [-- SCRIPT_OPTIONS]" >&2 + echo "Usage: container ${COMMAND} -n|--name NAME [--cnt.auto=true|false|FQDN] [--cnt.overlay=DIRECTORY_LOWER:DIRECTORY_UPPER:DIRECTORY_WORK:DIRECTORY_MERGED] [-b|--bind DIRECTORY:DIRECTORY[:OPTIONS]] [-c|--capability CAPABILITY[,CAPABILITY]] [-d|--drop-capability DROP_CAPABILITY[,DROP_CAPABILITY]] [-s|--script SCRIPT] [-- SCRIPT_OPTIONS]" >&2 exit 1 } @@ -149,11 +154,31 @@ then done fi +# Creating overlay mounts +if [ -n "${CNT_OVERLAY}" ] +then + CNT_OVERLAYS="$(echo ${CNT_OVERLAY} | sed -e 's|;| |g')" + + for CNT_OVERLAY in ${CNT_OVERLAYS} + do + DIRECTORY_LOWER="$(echo ${CNT_OVERLAY} | awk -F: '{ print $1 }')" + DIRECTORY_UPPER="$(echo ${CNT_OVERLAY} | awk -F: '{ print $2 }')" + DIRECTORY_WORK="$(echo ${CNT_OVERLAY} | awk -F: '{ print $3 }')" + DIRECTORY_MERGED="$(echo ${CNT_OVERLAY} | awk -F: '{ print $4 }')" + + for DIRECTORY in "${DIRECTORY_LOWER}" "${DIRECTORY_UPPER}" "${DIRECTORY_WORK}" "${DIRECTORY_MERGED}" + do + mkdir -p "${DIRECTORY}" + done + done +fi + # config mkdir -p "${CONFIG}" sed -e "s|@CNT_AUTO@|${CNT_AUTO}|g" \ -e "s|@CNT_NETWORK_BRIDGE@|${CNT_NETWORK_BRIDGE}|g" \ + -e "s|@CNT_OVERLAY@|${CNT_OVERLAY}|g" \ -e "s|@NAME@|${NAME}|g" \ -e "s|@BIND@|${BIND}|g" \ -e "s|@BOOT@|yes|g" \ diff --git a/lib/container/start b/lib/container/start index a4dc033..d5bbbf4 100755 --- a/lib/container/start +++ b/lib/container/start @@ -139,6 +139,31 @@ esac # config if [ -e "${CONFIG}/${NAME}.conf" ] then + CNT_OVERLAY="$(awk -F= '/^cnt.overlay=/ { print $2 }' ${CONFIG}/${NAME}.conf)" + + if [ -n "${CNT_OVERLAY}" ] + then + CNT_OVERLAYS="$(echo ${CNT_OVERLAY} | sed -e 's|;| |g')" + + for CNT_OVERLAY in ${CNT_OVERLAYS} + do + DIRECTORY_LOWER="$(echo ${CNT_OVERLAY} | awk -F: '{ print $1 }')" + DIRECTORY_UPPER="$(echo ${CNT_OVERLAY} | awk -F: '{ print $2 }')" + DIRECTORY_WORK="$(echo ${CNT_OVERLAY} | awk -F: '{ print $3 }')" + DIRECTORY_MERGED="$(echo ${CNT_OVERLAY} | awk -F: '{ print $4 }')" + + for DIRECTORY in "${DIRECTORY_LOWER}" "${DIRECTORY_UPPER}" "${DIRECTORY_WORK}" "${DIRECTORY_MERGED}" + do + mkdir -p "${DIRECTORY}" + done + + if ! findmnt -n -o SOURCE "${DIRECTORY_MERGED}" | grep -qs '^cnt.overlay-' + then + mount cnt.overlay-${NAME} -t overlay -olowerdir="${DIRECTORY_LOWER}",upperdir="${DIRECTORY_UPPER}",workdir="${DIRECTORY_WORK}",default_permissions "${DIRECTORY_MERGED}" + fi + done + fi + BIND="$(awk -F= '/^bind=/ { print $2 }' ${CONFIG}/${NAME}.conf)" if [ -n "${BIND}" ] diff --git a/lib/container/stop b/lib/container/stop index dc78f05..ec24d51 100755 --- a/lib/container/stop +++ b/lib/container/stop @@ -94,6 +94,29 @@ STATE="$(machinectl show ${NAME} 2>&1 | awk -F= '/^State=/ { print $2 }')" case "${CLEAN}" in true) + # Removing overlay mounts + CNT_OVERLAY="$(awk -F= '/^cnt.overlay=/ { print $2 }' ${CONFIG}/${NAME}.conf)" + + if [ -n "${CNT_OVERLAY}" ] + then + CNT_OVERLAYS="$(echo ${CNT_OVERLAY} | sed -e 's|;| |g')" + + for CNT_OVERLAY in ${CNT_OVERLAYS} + do + DIRECTORY_LOWER="$(echo ${CNT_OVERLAY} | awk -F: '{ print $1 }')" + DIRECTORY_UPPER="$(echo ${CNT_OVERLAY} | awk -F: '{ print $2 }')" + DIRECTORY_WORK="$(echo ${CNT_OVERLAY} | awk -F: '{ print $3 }')" + DIRECTORY_MERGED="$(echo ${CNT_OVERLAY} | awk -F: '{ print $4 }')" + + umount -f "${DIRECTORY_MERGED}" + + for DIRECTORY in "${DIRECTORY_LOWER}" "${DIRECTORY_UPPER}" "${DIRECTORY_WORK}" "${DIRECTORY_MERGED}" + do + rmdir --ignore-fail-on-non-empty --parents ${DIRECTORY} > /dev/null 2>&1 || true + done + done + fi + # Removing bind mounts BIND="$(awk -F= '/^bind=/ { print $2 }' ${CONFIG}/${NAME}.conf)" diff --git a/share/config/container.conf.in b/share/config/container.conf.in index dd52adb..b0211af 100644 --- a/share/config/container.conf.in +++ b/share/config/container.conf.in @@ -3,6 +3,7 @@ [start] cnt.auto=@CNT_AUTO@ cnt.network-bridge=@CNT_NETWORK_BRIDGE@ +cnt.overlay=@CNT_OVERLAY@ bind=@BIND@ boot=@BOOT@ capability=@CAPABILITY@ diff --git a/share/doc/examples/cairon-backports.cfg b/share/doc/examples/cairon-backports.cfg index eed9fd6..8243bbc 100644 --- a/share/doc/examples/cairon-backports.cfg +++ b/share/doc/examples/cairon-backports.cfg @@ -64,4 +64,5 @@ container-tools cnt-debconf/nameserver/options string timeout:1 attempts:1 #container-tools cnt-debconf/container-command string #container-tools cnt-debconf/host-command string container-tools cnt-debconf/auto string FQDN +#container-tools cnt-debconf/overlay string #container-tools cnt-debconf/bind string diff --git a/share/man/container-create.1.txt b/share/man/container-create.1.txt index b7a4f01..4fe592f 100644 --- a/share/man/container-create.1.txt +++ b/share/man/container-create.1.txt @@ -56,6 +56,9 @@ The following container-create options are available: *-b, --bind='DIRECTORY:DIRECTORY[:OPTIONS][;DIRECTORY:DIRECTORY[:OPTIONS]]'*:: Specify container bind mounts, see systemd-nspawn(1) --bind option. +*--cnt-overlay='DIRECTORY_LOWER:DIRECTORY_UPPER:DIRECTORY_WORK:DIRECTORY_MERGED[;DIRECTORY_UPPER:DIRECTORY_LOWER:DIRECTORY_WORK:DIRECTORY_MERGED]'*:: + Specify container overlay mounts, see Documentation/filesystems/overlayfs.txt. + SCRIPTS ------- diff --git a/share/scripts/debconf b/share/scripts/debconf index aa1c798..4c89e6f 100755 --- a/share/scripts/debconf +++ b/share/scripts/debconf @@ -909,6 +909,7 @@ Commands () sed -i -e "s|^cnt.auto=.*|cnt.auto=${CNT_AUTO}|" "${CONFIG}/${NAME}.conf" sed -i -e "s|^cnt.network-bridge=.*|cnt.network-bridge=${HOST_INTERFACE_NAME}:${NETWORK0_BRIDGE:-br0}|g" "${CONFIG}/${NAME}.conf" + sed -i -e "s|^cnt.overlay=.*|cnt.overlay=${CNT_OVERLAY}|g" "${CONFIG}/${NAME}.conf" sed -i -e "s|^bind=.*|bind=${BIND}|" "${CONFIG}/${NAME}.conf" sed -i -e "s|^network-veth-extra=.*|network-veth-extra=${HOST_INTERFACE_NAME}:eth0|g" "${CONFIG}/${NAME}.conf" @@ -1010,12 +1011,54 @@ then done fi +# Mounting overlay mounts +if [ -n "${CNT_OVERLAY}" ] +then + CNT_OVERLAYS="$(echo ${CNT_OVERLAY} | sed -e 's|;| |g')" + + for CNT_OVERLAY in ${CNT_OVERLAYS} + do + DIRECTORY_LOWER="$(echo ${CNT_OVERLAY} | awk -F: '{ print $1 }')" + DIRECTORY_UPPER="$(echo ${CNT_OVERLAY} | awk -F: '{ print $2 }')" + DIRECTORY_WORK="$(echo ${CNT_OVERLAY} | awk -F: '{ print $3 }')" + DIRECTORY_MERGED="$(echo ${CNT_OVERLAY} | awk -F: '{ print $4 }')" + + for DIRECTORY in "${DIRECTORY_LOWER}" "${DIRECTORY_UPPER}" "${DIRECTORY_WORK}" "${DIRECTORY_MERGED}" + do + mkdir -p "${DIRECTORY}" + done + + mount -t overlay overlay-${NAME} -olowerdir="${DIRECTORY_LOWER}",upperdir="${DIRECTORY_UPPER}",workdir="${DIRECTORY_WORK}" "${DIRECTORY_MERGED}" + done +fi + Configure_system "${MACHINES}/${NAME}" Configure_network "${MACHINES}/${NAME}" Cleanup_system "${MACHINES}/${NAME}" Commands "${MACHINES}/${NAME}" +# Unmounting overlay mounts +if [ -n "${CNT_OVERLAY}" ] +then + CNT_OVERLAYS="$(echo ${CNT_OVERLAY} | sed -e 's|;| |g')" + + for CNT_OVERLAY in ${CNT_OVERLAYS} + do + DIRECTORY_LOWER="$(echo ${CNT_OVERLAY} | awk -F: '{ print $1 }')" + DIRECTORY_UPPER="$(echo ${CNT_OVERLAY} | awk -F: '{ print $2 }')" + DIRECTORY_WORK="$(echo ${CNT_OVERLAY} | awk -F: '{ print $3 }')" + DIRECTORY_MERGED="$(echo ${CNT_OVERLAY} | awk -F: '{ print $4 }')" + + umount -f "${DIRECTORY_MERGED}" + + for DIRECTORY in "${DIRECTORY_LOWER}" "${DIRECTORY_UPPER}" "${DIRECTORY_WORK}" "${DIRECTORY_MERGED}" + do + rmdir --ignore-fail-on-non-empty --parents ${DIRECTORY} > /dev/null 2>&1 || true + done + done +fi + # Unmounting bind mounts if [ -n "${BIND}" ] then diff --git a/share/scripts/debconf.d/0003-debconf b/share/scripts/debconf.d/0003-debconf index 15a6a15..548fb07 100755 --- a/share/scripts/debconf.d/0003-debconf +++ b/share/scripts/debconf.d/0003-debconf @@ -1063,6 +1063,13 @@ Internal_options () fi echo "BIND=\"${BIND}\"" >> "${DEBCONF_TMPDIR}/debconf.default" + + if db_get cnt-debconf/overlay + then + CNT_OVERLAY="${RET}" # string (w/ empty) + fi + + echo "CNT_OVERLAY=\"${CNT_OVERLAY}\"" >> "${DEBCONF_TMPDIR}/debconf.default" } Distribution -- cgit v1.2.3