Skip to content

Commit

Permalink
Added: binary_replace.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Yakubov authored and Alexey Yakubov committed Jan 10, 2025
1 parent 10e9e77 commit 1bc02a0
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
11 changes: 11 additions & 0 deletions images/sds-local-volume-scheduler-extender/werf.inc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,19 @@ image: {{ $.ImageName }}-binaries-artifact
from: {{ $.Root.BASE_ALT_P11 }}
final: false

git:
- add: /tools/dev_images/additional_tools/binary_replace.sh
to: /binary_replace.sh
stageDependencies:
install:
- '**/*'

shell:
install:
- apt-get update
- apt-get -y install glibc-utils mount nfs-utils
- rm -rf /var/lib/apt/lists/* /var/cache/apt/* && mkdir -p /var/lib/apt/lists/partial /var/cache/apt/archives/partial
- chmod +x /binary_replace.sh
- /binary_replace.sh -i "{{ $csiBinaries }}" -o /relocate

---
Expand Down
129 changes: 129 additions & 0 deletions tools/dev_images/additional_tools/binary_replace.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#!/bin/bash

# Copyright 2023 Flant JSC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -Eeuo pipefail
shopt -s failglob

FILE_TEMPLATE_BINS=""
TEMPLATE_BINS=""
RDIR=""

tools=("ldd" "readlink" "awk" "dirname" "ls" "cat")
for tool in "${tools[@]}"; do
if ! command -v "$tool" >/dev/null 2>&1; then
echo "$tool is not installed."
exit 1
fi
done

function Help() {
# Display Help
echo "Copy binaries and their libraries to a folder"
echo "Only one input parameter allowed (-f or -i) !!!"
echo
echo "Syntax: scriptTemplate [-h|f|i|o]"
echo "options:"
echo "f Files with paths to binaries; Support mask like /sbin/m*"
echo "i Paths to binaries separated by space; Support mask like /sbin/m*; Example: /bin/chmod /bin/mount /sbin/m*"
echo ' List of binaries should be in double quotes, -i "/bin/chmod /bin/mount" '
echo "o Output directory (Default value: '/relocate')"
echo "h Print this help"
echo
echo
}

while getopts ":h:i:f:o:" option; do
case $option in
h) # display Help
Help
exit;;
f)
FILE_TEMPLATE_BINS=$OPTARG
;;
i)
TEMPLATE_BINS=$OPTARG
;;
o)
RDIR=$OPTARG
;;
\?)
echo "Error: Invalid option"
exit;;
esac
done

if [[ -z $RDIR ]];then
RDIR="/relocate"
fi
mkdir -p "${RDIR}"

function relocate() {
local binary=$1
relocate_item ${binary}

for lib in $(ldd ${binary} 2>/dev/null | awk '{if ($2=="=>") print $3; else print $1}'); do
# don't try to relocate linux-vdso.so lib due to this lib is virtual
if [[ "${lib}" =~ "linux-vdso" ]]; then
continue
fi
relocate_item ${lib}
done
}

function relocate_item() {
local file=$1
local new_place="${RDIR}$(dirname ${file})"

mkdir -p ${new_place}
cp -a ${file} ${new_place}

# if symlink, copy original file too
local orig_file="$(readlink -f ${file})"
if [[ "${file}" != "${orig_file}" ]]; then
cp -a ${orig_file} ${new_place}
fi
}

function get_binary_path () {
local bin
BINARY_LIST=()

for bin in "$@"; do
if [[ ! -f $bin ]] || [ "${bin}" == "${RDIR}" ]; then
echo "Not found $bin"
exit 1
fi
BINARY_LIST+=$(ls -la $bin 2>/dev/null | awk '{print $9}')" "
done

if [[ -z $BINARY_LIST ]]; then echo "No binaryes for replace"; exit 1; fi;
}

# if get file with binaryes (-f)
if [[ -n $FILE_TEMPLATE_BINS ]] && [[ -f $FILE_TEMPLATE_BINS ]] && [[ -z $TEMPLATE_BINS ]]; then
BIN_TEMPLATE=$(cat $FILE_TEMPLATE_BINS)
get_binary_path ${BIN_TEMPLATE}
# Or get paths to bin via raw input (-i)
elif [[ -n $TEMPLATE_BINS ]] && [[ -z $FILE_TEMPLATE_BINS ]]; then
get_binary_path ${TEMPLATE_BINS}
else
Help
exit
fi

for binary in ${BINARY_LIST[@]}; do
relocate ${binary}
done

0 comments on commit 1bc02a0

Please sign in to comment.