diff options
Diffstat (limited to 'librechroot')
-rwxr-xr-x | librechroot | 127 |
1 files changed, 95 insertions, 32 deletions
diff --git a/librechroot b/librechroot index d3e02f1..31516ac 100755 --- a/librechroot +++ b/librechroot @@ -3,6 +3,7 @@ # Enters a chroot # Copyright 2010 Nicolás Reynolds +# Copyright 2011 Joshua Haase # ---------- GNU General Public License 3 ---------- @@ -21,52 +22,114 @@ # You should have received a copy of the GNU General Public License # along with Parabola. If not, see <http://www.gnu.org/licenses/>. -source /etc/libretools.conf - function usage { - echo "Usage: $0 chrootname" - echo "Change to a chroot in $CHROOTDIR. Use it as root." - echo + + echo "Usage: $0 [options] [chrootname]" + echo "Use it as root." + echo '' echo "Default chroot name: $CHROOT" + echo "Default chrootdir: $CHROOTDIR" + echo '' + echo "OPTIONS:" + echo '' + echo " -c : clean the chroot using pacman" + echo " only 'base', 'base-devel' and 'sudo' on chroot" + echo " -d <chrootdir> : use <chrootdir> instead of default" + echo " -r : clean /repo on the chroot" + echo " -h : this message" + echo " -u : update the chroot" + echo "" + } -while getopts 'h' arg; do - case $arg in - h) usage; exit 0 ;; - esac +function clean_chroot { # Clean packages with pacman + + msg "Cleaning chroot: ${CHROOTDIR}/${CHROOTNAME}" + + cp "/etc/libretools.d/cleansystem" "${CHROOTDIR}/${CHROOTNAME}/cleansystem" + (cat <<EOF +#!/bin/bash +export LANG=C + +count='0' + +while [ "\$count" -lt "3" ]; do + + pkgs=(\$(comm -23 <(pacman -Qq | sort) <(sort /cleansystem))) + + if [ \${#pkgs[@]} -gt 0 ]; then + pacman --noconfirm -Rcs \${pkgs[@]} + else + echo "clean" + exit 0 + fi + + declare -i "count=\$count + 1" + done -[[ "$UID" != "0" ]] && { - error "This script must be run as root." - exit 1 -} +echo "Cleaning failed: please report error" -custom_config=${XDG_CONFIG_HOME}/libretools/libretools.conf -[[ -e ${custom_config} ]] && source ${custom_config} +EOF + ) > "${CHROOTDIR}/${CHROOTNAME}/clean" -# Enter the chroot copy by default -chrootname=${1:-$CHROOT} -CACHEDIR=${CACHEDIR:-/var/cache/pacman/pkg} + chmod +x "${CHROOTDIR}/${CHROOTNAME}/clean" + mkarchroot -r "/clean" "${CHROOTDIR}/${CHROOTNAME}" -[[ ! -d ${CHROOTDIR}/${chrootname} ]] && { - error "${CHROOTDIR}/${chrootname} is not a dir." - exit 1 + rm "${CHROOTDIR}/${CHROOTNAME}/clean" + rm "${CHROOTDIR}/${CHROOTNAME}/cleansystem" +} + +function clean_repo { + msg "Cleaning repo for chroot: ${CHROOTDIR}/${CHROOTNAME}" + if [ -d "${CHROOTDIR}/${CHROOTNAME}/repo" ]; then + rm -rf "${CHROOTDIR}/${CHROOTNAME}/repo" + fi } +source /etc/libretools.conf -mount -t proc proc ${CHROOTDIR}/${chrootname}/proc/ -mount -t sysfs sys ${CHROOTDIR}/${chrootname}/sys/ -mount -o bind /dev ${CHROOTDIR}/${chrootname}/dev/ +if [ -e "$XDG_CONFIG_HOME/libretools/libretools.conf" ]; then + source "$XDG_CONFIG_HOME/libretools/libretools.conf" +fi -# Share pacman cache -mount -o bind ${CACHEDIR} ${CHROOTDIR}/${chrootname}/var/cache/pacman/pkg +CLEANCHROOT='false' +UPDATE='false' +CLEANREPO='false' +CHROOTNAME="${CHROOT:-${SUDO_USER:-root}}" -cp -L /etc/resolv.conf ${CHROOTDIR}/${chrootname}/etc/resolv.conf +while getopts 'hrcud:' arg; do + case $arg in + h) usage; exit 0 ;; + c) CLEANCHROOT='true' ;; + u) UPDATE='true' ;; + r) CLEANREPO='true' ;; + d) CHROOTDIR="$(readlink -e $OPTARG)" ;; + esac +done -chroot ${CHROOTDIR}/${chrootname} /bin/bash +[[ "$UID" != "0" ]] && { + error "This script must be run as root." + exit 1 +} -umount ${CHROOTDIR}/${chrootname}/proc/ -umount ${CHROOTDIR}/${chrootname}/sys/ -umount ${CHROOTDIR}/${chrootname}/dev/ -umount ${CHROOTDIR}/${chrootname}/var/cache/pacman/pkg +shift $(($OPTIND - 1)) + +if [ $# -eq 1 ]; then + CHROOTNAME="$1" +fi + +if "$CLEANREPO"; then + clean_repo +fi + +if "$CLEANCHROOT"; then + clean_chroot +elif "$UPDATE"; then + msg "Updating chroot: ${CHROOTDIR}/${CHROOTNAME}" + mkarchroot -u "${CHROOTDIR}/${CHROOTNAME}" +else + msg "Entering chroot: ${CHROOTDIR}/${CHROOTNAME}" + mkarchroot -r "bash" "${CHROOTDIR}/${CHROOTNAME}" +fi exit 0 |