summaryrefslogtreecommitdiff
path: root/build-chroot
diff options
context:
space:
mode:
authorPeter Wu <lekensteyn@gmail.com>2013-04-23 18:59:15 +0200
committerPeter Wu <lekensteyn@gmail.com>2013-04-23 18:59:15 +0200
commitaf38cb02a9ca95b20056bc92e73fe72a45d1f523 (patch)
tree41628cac3a02d2c4dcf4ff6ecbb1fb8c5378db20 /build-chroot
downloadscripts-af38cb02a9ca95b20056bc92e73fe72a45d1f523.tar.gz
Initial checkin.
Diffstat (limited to 'build-chroot')
-rwxr-xr-xbuild-chroot116
1 files changed, 116 insertions, 0 deletions
diff --git a/build-chroot b/build-chroot
new file mode 100755
index 0000000..cd608e6
--- /dev/null
+++ b/build-chroot
@@ -0,0 +1,116 @@
+#!/bin/bash
+VERSION=0.1
+find_libs=~/scripts/find-libs
+usage() {
+ cat <<HELP
+Copies binaries and their dependencies for chroot usage.
+
+Copyright (C) 2012 Peter Wu
+This is free software; see the source for copying conditions. There is NO
+warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+Written by Peter Wu <lekensteyn@gmail.com>
+
+Usage: $0 [OPTION] file...
+
+Options:
+ -n, --dry-run Do not copy files, just print the action that would be
+ performed.
+ -d DIR, --destdir DIR Destination directory for the files.
+ -c, --continue Continue even if a given file cannot be located.
+ -h, --help Print this help text, then exit.
+ --version Prints the version of this program, then exit.
+
+Environment:
+ DESTDIR If --destdir is not given, this environment variable is sought. If
+ this variable does not exist either, a temporary directory is
+ created which is printed on termination.
+HELP
+}
+
+opts=`getopt --name "$0" -o nd:ch \
+ --longoptions dry-run,destdir:,continue,help,version \
+ -- "$@"`
+if [ $? != 0 -o -z "$opts" ]; then
+ usage
+ exit 1
+fi
+eval set -- "$opts"
+
+DRY_RUN=false
+CONT_ON_ERR=false
+while :; do
+ case "$1" in
+ -n|--dry-run) DRY_RUN=true ;;
+ -d|--destdir) DESTDIR="$2" ; shift ;;
+ -c|--continue) CONT_ON_ERR=true ;;
+ -h|--help) usage; exit 0 ;;
+ --version) echo "$VERSION" ; exit 0 ;;
+ --) shift; break ;;
+ *) echo "Internal error!"; exit 1 ;;
+ esac
+ shift
+done
+
+if [ $# -eq 0 ]; then
+ echo "Need at least one binary or library to isolate."
+ echo "Try '$0 --help' for more information."
+ exit 1
+fi
+
+subjects=()
+for subject; do
+ realsubject="$subject"
+ #realsubject="$(readlink -e "$subject")"
+ [ -e "$realsubject" ] || realsubject="$(which "$subject" 2>/dev/null)" || true
+ if [ -n "$realsubject" ]; then
+ subjects[${#subjects[@]}]="$realsubject"
+ else
+ echo "Missing file: $subject"
+ fi
+done
+
+if ! [ $# = ${#subjects[@]} ]; then
+ echo "Some files could not be found"
+ echo "Expected $#, got ${#subjects[@]} valid args."
+ if ! $CONT_ON_ERR; then
+ echo "Aborting. To ignore this, pass the '--continue' option."
+ exit 255
+ fi
+fi
+
+info="$("$find_libs" "${subjects[@]}")"
+echo "$info"
+count=${info%% *}
+filelist="${info#* }"
+
+try_install() {
+ local file="$1"
+ # target without leading ../../etc
+ local target="$DESTDIR/$(sed 's,\(\.\./\)*,,' <<<"$file")"
+ if [ ! -e "$target" ]; then
+ if $DRY_RUN; then
+ echo "Installing $file"
+ else
+ install -v -D "$file" "$target"
+ fi
+ fi
+}
+if [ $count -gt 0 ]; then
+ tmpdir=
+ if [ -z "$DESTDIR" ]; then
+ tmpdir="$(mktemp -d)"
+ DESTDIR="$tmpdir"
+ fi
+ umask 002
+ while read file; do
+ try_install "$file"
+ done < "$filelist"
+ [ -z "$tmpdir" ] || rmdir --ignore-fail-on-non-empty "$tmpdir"
+else
+ echo "No files to copy"
+fi
+for subject in "${subjects[@]}"; do
+ try_install "$subject"
+done
+rm "$filelist"