Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Copy a command and its dependencies to the specified chroot filesystem
#!/bin/sh
##
# Copy a command and its dependencies to the specified chroot filesystem
#
# Example:
# cpchroot /var/chroot bash cp ls mkdir mv
#
# Copyright (c) 2016 Timothy Boronczyk
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# (1) Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# (2) 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.
#
# (3)The name of the author may not be used to
# endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''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 AUTHOR 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.
usage()(
echo "Usage: $(basename "$0") chroot cmd [...]"
exit 1
)
deplibs()(
shared=$(objdump -p "$1" | awk '/NEEDED/ { print $2 }')
for s in $shared; do
dep=$(find / ! -path "$2*" -name "$s" -executable -print -quit)
echo "$dep"
deplibs "$dep" "$2"
done
)
main()(
dir="$1"
shift
while [ $# -gt 0 ]; do
cmd=$(which "$1")
cp "$cmd" "$dir/$cmd"
for dep in $(deplibs "$cmd" "$dir" | sort -u); do
if [ ! -f "$dir/$dep" ]; then
cp "$dep" "$dir/$dep"
fi
done
shift
done
)
[ $# -lt 2 ] && usage
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment