#!/bin/bash # Copyright 1999-2003 Gentoo Technologies, Inc. # Distributed under the terms of the GNU General Public License v2 # $Header: $ # Ned Ludd CHROOTDIR=$1 BIN=$2 if [ "x${CHROOTDIR}" = "x" ]; then echo "Usage: mkchroot [path/bin]" exit fi umask 022 mkdir -p ${CHROOTDIR} cd ${CHROOTDIR} || exit 1 mkdir -p etc dev proc tmp lib usr root home usr/bin usr/sbin usr/lib bin sbin var var/log var/run > /dev/null [ -r "/etc/localtime" ] && cp /etc/localtime etc/ cd dev [ ! -e tty ] && mknod -m 666 tty c 5 0 [ ! -e null ] && mknod -m 666 null c 1 3 [ ! -e zero ] && mknod -m 666 zero c 1 5 [ ! -e urandom ] && mknod -m 644 urandom c 1 9 chown root:tty tty chown root:sys null zero if [ -e /proc/self/fd ] ; then ln -fs /proc/self/fd fd 2> /dev/null ln -fs fd/0 stdin ln -fs fd/1 stdout ln -fs fd/2 stderr fi cd .. # If were using a dynamically linked executable then # we must figure out what libraries will be needed in # our chrooted environment. cd .. [ "x${BIN}" == "x" ] && exit 0 if [ -z "${BIN}" -o -z "${CHROOTDIR}" -o ! -f "${BIN}" ]; then echo "You have to enter both binary and directory name" exit -1 fi LIBDIR=$(echo /lib /usr/lib; grep ^/ /etc/ld.so.conf) ldd "${BIN}" | awk -F" " '{print $1}'| while read LIB; do if [ $(echo "${LIB}"|cut -c1) != "/" ]; then # Looking for library in LIBDIRs LIB=$(find ${LIBDIR} \( -type f -o -type l \) -name "${LIB}"|head -n 1) if [ -z "${LIB}" ]; then echo "ERROR: Library ${LIB} not found" exit -1 fi fi SUBDIR=$(dirname "${LIB}"); if [ ! -f ${CHROOTDIR}${LIB} ] ; then echo "Copying ${LIB} -> ${CHROOTDIR}${LIB}" mkdir -p "${CHROOTDIR}$SUBDIR" || exit 1 cp "${LIB}" "${CHROOTDIR}${LIB}" || exit 1 strip "${CHROOTDIR}${LIB}" || exit 1 fi done if [ ! -x ${CHROOTDIR}/${BIN} ] ; then mkdir -p $(dirname ${CHROOTDIR}/${BIN}) cp ${BIN} ${CHROOTDIR}/${BIN} fi echo "Done..."