From e304acb2679edf29fd9876776d4b6132f277d50b Mon Sep 17 00:00:00 2001 From: wanyaoqi Date: Mon, 2 Dec 2019 20:42:51 +0800 Subject: [PATCH] add bundle libraries script for build host image --- build/docker/Dockerfile.host | 11 ++ build/docker/Dockerfile.host-deployer | 11 ++ scripts/bundle-libraries.sh | 195 ++++++++++++++++++++++++++ scripts/docker_push.sh | 18 ++- 4 files changed, 231 insertions(+), 4 deletions(-) create mode 100644 build/docker/Dockerfile.host create mode 100644 build/docker/Dockerfile.host-deployer create mode 100755 scripts/bundle-libraries.sh diff --git a/build/docker/Dockerfile.host b/build/docker/Dockerfile.host new file mode 100644 index 0000000000..f907180085 --- /dev/null +++ b/build/docker/Dockerfile.host @@ -0,0 +1,11 @@ +FROM frolvlad/alpine-glibc:glibc-2.28 + +MAINTAINER "Yaoqi Wan wanyaoqi@yunionyun.com" + +ENV TZ Asia/Shanghai + +RUN mkdir -p /opt/yunion/bin + +ADD ./_output/bin/host /opt/yunion/bin/host +ADD ./_output/bin/.host.bin /opt/yunion/bin/.host.bin +ADD ./_output/bin/bundles/host /opt/yunion/bin/bundles/host diff --git a/build/docker/Dockerfile.host-deployer b/build/docker/Dockerfile.host-deployer new file mode 100644 index 0000000000..7fb4127a07 --- /dev/null +++ b/build/docker/Dockerfile.host-deployer @@ -0,0 +1,11 @@ +FROM frolvlad/alpine-glibc:glibc-2.28 + +MAINTAINER "Yaoqi Wan wanyaoqi@yunionyun.com" + +ENV TZ Asia/Shanghai + +RUN mkdir -p /opt/yunion/bin + +ADD ./_output/bin/host-deployer /opt/yunion/bin/host-deployer +ADD ./_output/bin/.host-deployer.bin /opt/yunion/bin/.host-deployer.bin +ADD ./_output/bin/bundles/host-deployer /opt/yunion/bin/bundles/host-deployer diff --git a/scripts/bundle-libraries.sh b/scripts/bundle-libraries.sh new file mode 100755 index 0000000000..e076c474c4 --- /dev/null +++ b/scripts/bundle-libraries.sh @@ -0,0 +1,195 @@ +#!/usr/bin/env bash +# +# Script to install host system binaries along with required libraries. +# +# Copyright (C) 2012-2017 Jo-Philipp Wich +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +DIR="$1"; shift + +_cp() { + cp ${VERBOSE:+-v} -L "$1" "$2" || { + echo "cp($1 $2) failed" >&2 + exit 1 + } +} + +_mv() { + mv ${VERBOSE:+-v} "$1" "$2" || { + echo "mv($1 $2) failed" >&2 + exit 1 + } +} + +_md() { + mkdir ${VERBOSE:+-v} -p "$1" || { + echo "mkdir($1) failed" >&2 + exit 2 + } +} + +_ln() { + ln ${VERBOSE:+-v} -sf "$1" "$2" || { + echo "ln($1 $2) failed" >&2 + exit 3 + } +} + +_relpath() { + local base="$(readlink -f "$1")" + local dest="$(readlink -f "$2")" + local up + + [ -d "$base" ] || base="${base%/*}" + [ -d "$dest" ] || dest="${dest%/*}" + + while true; do + case "$base" + in "$dest"/*) + echo "$up/${base#$dest/}" + break + ;; + *) + dest="${dest%/*}" + up="${up:+$up/}.." + ;; + esac + done +} + +_runas_so() { + cat <<-EOT | ${CC:-gcc} -x c -fPIC -shared -o "$1" - + #include + #include + #include + + int mangle_arg0(int argc, char **argv, char **env) { + char *arg0 = getenv("RUNAS_ARG0"); + + if (arg0) { + argv[0] = arg0; + unsetenv("RUNAS_ARG0"); + } + + return 0; + } + + #ifdef __APPLE__ + __attribute__((section("__DATA,__mod_init_func"))) + #else + __attribute__((section(".init_array"))) + #endif + static void *mangle_arg0_constructor = &mangle_arg0; + EOT + + [ -x "$1" ] || { + echo "compiling preload library failed" >&2 + exit 5 + } +} + +_patch_ldso() { + _cp "$1" "$1.patched" + sed -i -e 's,/\(usr\|lib\|etc\)/,/###/,g' "$1.patched" + + if "$1.patched" 2>&1 | grep -q -- --library-path; then + _mv "$1.patched" "$1" + else + echo "binary patched ${1##*/} not executable, using original" >&2 + rm -f "$1.patched" + fi +} + +_patch_glibc() { + _cp "$1" "$1.patched" + sed -i -e 's,/usr/\(\(lib\|share\)/locale\),/###/\1,g' "$1.patched" + + if "$1.patched" 2>&1 | grep -q -- GNU; then + _mv "$1.patched" "$1" + else + echo "binary patched ${1##*/} not executable, using original" >&2 + rm -f "$1.patched" + fi +} + +for LDD in ${PATH//://ldd }/ldd; do + "$LDD" --version >/dev/null 2>/dev/null && break + LDD="" +done + +[ -n "$LDD" -a -x "$LDD" ] || LDD= + +for BIN in "$@"; do + [ -n "$BIN" -a -n "$DIR" ] || { + echo "Usage: $0 ..." >&2 + exit 1 + } + + [ ! -d "$DIR/lib" ] && { + _md "$DIR/lib" + _md "$DIR/usr" + _ln "../lib" "$DIR/usr/lib" + } + + [ ! -x "$DIR/lib/runas.so" ] && { + _runas_so "$DIR/lib/runas.so" + } + + LDSO="" + + [ -n "$LDD" ] && [ -x "$BIN" ] && file "$BIN" | grep -sqE "ELF.*(executable|interpreter|uses shared libs)" && { + for token in $("$LDD" "$BIN" 2>/dev/null); do + case "$token" in */*.so*) + dest="$DIR/lib/${token##*/}" + ddir="${dest%/*}" + + case "$token" in + */ld-*.so*) LDSO="${token##*/}" ;; + esac + + [ -f "$token" -a ! -f "$dest" ] && { + _md "$ddir" + _cp "$token" "$dest" + case "$token" in + */ld-*.so*) _patch_ldso "$dest" ;; + */libc.so.6) _patch_glibc "$dest" ;; + esac + } + ;; esac + done + } + + # is a dynamically linked executable + if [ -n "$LDSO" ]; then + echo "Bundling ${BIN##*/}" + + RUNDIR="$(readlink -f "$BIN")"; RUNDIR="${RUNDIR%/*}" + RUN="${LDSO#ld-}"; RUN="run-${RUN%%.so*}.sh" + REL="$(_relpath "$DIR/lib" "$BIN")" + + _mv "$BIN" "$RUNDIR/.${BIN##*/}.bin" + + cat <<-EOF > "$BIN" + #! /usr/bin/env sh + dir="\$(dirname "\$0")" + export RUNAS_ARG0="\$0" + export LD_PRELOAD="\$dir/${REL:+$REL/}runas.so" + exec "\$dir/${REL:+$REL/}$LDSO" --library-path "\$dir/${REL:+$REL/}" "\$dir/.${BIN##*/}.bin" "\$@" + EOF + + chmod ${VERBOSE:+-v} 0755 "$BIN" + fi +done diff --git a/scripts/docker_push.sh b/scripts/docker_push.sh index 5651c8657b..116a6163c9 100755 --- a/scripts/docker_push.sh +++ b/scripts/docker_push.sh @@ -37,6 +37,15 @@ build_bin() { make cmd/$1 } +build_bundle_libraries() { + for component in 'host' 'host-deployer'; do + if [ $1 == $component ]; then + $CUR_DIR/bundle-libraries.sh _output/bin/bundles/$1 _output/bin/$1 + break + fi + done +} + build_image() { local tag=$1 local file=$2 @@ -52,9 +61,10 @@ push_image() { COMPONENTS=$@ cd $SRC_DIR -for compent in $COMPONENTS; do - build_bin $compent - img_name="$REGISTRY/$compent:$TAG" - build_image $img_name $DOCKER_DIR/Dockerfile.$compent $SRC_DIR +for component in $COMPONENTS; do + build_bin $component + build_bundle_libraries $component + img_name="$REGISTRY/$component:$TAG" + build_image $img_name $DOCKER_DIR/Dockerfile.$component $SRC_DIR push_image "$img_name" done