#!/bin/sh
# xcompile_fix_links_img.sh
# 2016-02-18
# by Gernot Walzl
# Many root filesystems for embedded linux devices contain absolute links.
# When such an image is mounted into the filesystem,
# the required libraries are not found by the linker during the build process.
# This scripts converts absolute links to relative links with valid targets.
# Another approach is to directly mount the root filesystem using sshfs.
# sshfs is able to convert absolute links on the fly.
# sshfs pi@raspberrypi:/ ~/raspberrypi -o transform_symlinks
ROOT="$1"
if [ -z "$ROOT" ]; then
echo "Usage: $0 /path/to/root-fs"
exit 1
fi
if [ ! -d "$ROOT/usr" ]; then
echo "Error: The given path does not point to"
echo "the root of a usual filesystem layout."
exit 1
fi
for LINK in $(find "$ROOT/usr" -type l); do
DST=$(readlink $LINK | grep '^/') # filter absolute links
if [ ! -z "$DST" ]; then
DIR=$(dirname $LINK)
NEW=$(realpath -s --relative-to=$DIR $ROOT$DST)
if [ -f "$DIR/$NEW" -o -h "$DIR/$NEW" -o -d "$DIR/$NEW" ]; then
rm -f "$LINK"
ln -s "$NEW" "$LINK"
echo "$LINK -> $NEW"
else
echo "Warning: Unable to fix $LINK"
fi
fi
done