From 9cc29a013a0a232002e80981bd66c7189d12dddf Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Mon, 21 Jul 2014 17:53:46 -0700 Subject: [PATCH] trim.sh: Work-in-progress container reduction script Dump everything except @world and it's runtime dependencies (and dump the Docker history too) to get a container down to a reasonable size. --- trim.sh | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100755 trim.sh diff --git a/trim.sh b/trim.sh new file mode 100755 index 0000000..1091a69 --- /dev/null +++ b/trim.sh @@ -0,0 +1,71 @@ +#!/bin/sh +# +# Copyright (C) 2013-2014 W. Trevor King +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, this +# list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +die() +{ + echo "$1" + exit 1 +} + +trim() +{ + IMAGE="$1" + NAMESPACE_REPOSITORY="${IMAGE%:*}" + TAG="${IMAGE#*:}" + if [ "${TAG}" = "${NAMESPACE_REPOSITORY}" ] + then + TAG="" + fi + REPOSITORY="${NAMESPACE_REPOSITORY#*/}" + CONTAINER="${REPOSITORY}-trimmed-container" + IMG="${NAMESPACE_REPOSITORY}-trimmed" + if [ -n "${TAG}" ] + then + IMG="${IMG}:${TAG}" + fi + #sys-apps/openrc (/sbin/rc) + #sys-apps/net-tools (/bin/hostname) + #sys-devel/gettext (/usr/bin/envsubst) + # unmerge GCC to break the GCC <-> libc dependency loop + docker run -t --name "${CONTAINER}" \ + -v "${PWD}/empty-system.py:/tmp/empty-system.py" \ + "${IMAGE}" /bin/bash -c " + /tmp/empty-system.py && + emerge --noreplace app-shells/bash sys-apps/net-tools sys-apps/openrc sys-devel/gettext && + emerge --unmerge sys-devel/gcc && + emerge -v --depclean && + rm -rf /usr/portage" || + die "failed to create ${CONTAINER}" + docker export "${CONTAINER}" | docker import - "${IMG}" || + die "failed to export/import ${IMG}" + docker rm "${CONTAINER}" || + die "failed to remove ${CONTAINER}" +} + +for IMAGE in "$@" +do + trim "${IMAGE}" +done -- 2.26.2