From e9bc885c355babf7851de31db8e1920dde752993 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 7 Nov 2012 00:17:08 -0500 Subject: organize the files --- src/chroot-tools/librechroot | 108 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100755 src/chroot-tools/librechroot (limited to 'src/chroot-tools/librechroot') diff --git a/src/chroot-tools/librechroot b/src/chroot-tools/librechroot new file mode 100755 index 0000000..c8e02b0 --- /dev/null +++ b/src/chroot-tools/librechroot @@ -0,0 +1,108 @@ +#!/bin/bash +# LibreChRoot +# Enters a chroot + +# Copyright 2010 Nicolás Reynolds +# Copyright 2011 Joshua Haase + +# ---------- GNU General Public License 3 ---------- + +# This file is part of Parabola. + +# Parabola is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# Parabola is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with Parabola. If not, see . + +function usage { + + 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 : use instead of default" + echo " -r : clean /repo on the chroot" + echo " -h : this message" + echo " -u : update the chroot" + echo "" + +} + +function clean_chroot { # Clean packages with pacman + cp -a "$(dirname $0)/chcleanup" "${CHROOTDIR}/${CHROOTNAME}/clean" + + mkarchroot -r "cd /build; /clean" "${CHROOTDIR}/${CHROOTNAME}" +} + +function clean_repo { + msg "Cleaning repo for chroot: ${CHROOTDIR}/${CHROOTNAME}" + if [ -d "${CHROOTDIR}/${CHROOTNAME}/repo" ]; then + find "${CHROOTDIR}/${CHROOTNAME}/repo/" -mindepth 1 -delete + else + mkdir -p "${CHROOTDIR}/${CHROOTNAME}/repo" + fi + bsdtar -czf "${CHROOTDIR}/${CHROOTNAME}/repo/repo.db.tar.gz" -T /dev/null + ln -s "repo.db.tar.gz" "${CHROOTDIR}/${CHROOTNAME}/repo/repo.db" +} +source /etc/libretools.conf + +if [ -e "$XDG_CONFIG_HOME/libretools/libretools.conf" ]; then + source "$XDG_CONFIG_HOME/libretools/libretools.conf" +fi + +CLEANCHROOT='false' +UPDATE='false' +CLEANREPO='false' +CHROOTNAME="${CHROOT:-${SUDO_USER:-root}}" + +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 + +[[ "$UID" != "0" ]] && { + error "This script must be run as root." + exit 1 +} + +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 -- cgit v1.2.3-2-g168b From 16e41597e98333f6383a3cb9aa6e9371f64522bf Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Mon, 12 Nov 2012 17:32:40 -0500 Subject: chroot-tools: clean up, make play nice with new devtools * buildenv: delete; this is done by `mkarchroot` * librechroot: - adopt `archbuild`'s concept of CHROOT and CHROOTCOPY - add `-l` option to set the CHROOTCOPY * libremakepkg: - adopt `archbuild`'s concept of CHROOT and CHROOTCOPY - add `-l` option to set the CHROOTCOPY - pass options to `makechrootpkg` and `makepkg` the way `-h` always said it did * libremkchroot: - adopt `archbuild`'s concept of CHROOT and CHROOTCOPY - remove `-c` option to set the pacman cache - remove `-f` option to force overwrite --- src/chroot-tools/librechroot | 160 +++++++++++++++++++++++-------------------- 1 file changed, 87 insertions(+), 73 deletions(-) (limited to 'src/chroot-tools/librechroot') diff --git a/src/chroot-tools/librechroot b/src/chroot-tools/librechroot index c8e02b0..d53448a 100755 --- a/src/chroot-tools/librechroot +++ b/src/chroot-tools/librechroot @@ -4,6 +4,7 @@ # Copyright 2010 Nicolás Reynolds # Copyright 2011 Joshua Haase +# Copyright 2012 Luke Shumaker # ---------- GNU General Public License 3 ---------- @@ -22,87 +23,100 @@ # You should have received a copy of the GNU General Public License # along with Parabola. If not, see . -function usage { - - 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 : use instead of default" - echo " -r : clean /repo on the chroot" - echo " -h : this message" - echo " -u : update the chroot" - echo "" +. /etc/libretools.conf -} +cmd=${0##*/} -function clean_chroot { # Clean packages with pacman - cp -a "$(dirname $0)/chcleanup" "${CHROOTDIR}/${CHROOTNAME}/clean" +clean_chroot() { # Clean packages with pacman + msg "Cleaning chroot with pacman: ${chroot_path}" - mkarchroot -r "cd /build; /clean" "${CHROOTDIR}/${CHROOTNAME}" -} + cp -a "$(dirname $0)/chcleanup" "${chroot_path}/clean" + mkarchroot -r "cd /build; /clean" "${chroot_path}" -function clean_repo { - msg "Cleaning repo for chroot: ${CHROOTDIR}/${CHROOTNAME}" - if [ -d "${CHROOTDIR}/${CHROOTNAME}/repo" ]; then - find "${CHROOTDIR}/${CHROOTNAME}/repo/" -mindepth 1 -delete - else - mkdir -p "${CHROOTDIR}/${CHROOTNAME}/repo" - fi - bsdtar -czf "${CHROOTDIR}/${CHROOTNAME}/repo/repo.db.tar.gz" -T /dev/null - ln -s "repo.db.tar.gz" "${CHROOTDIR}/${CHROOTNAME}/repo/repo.db" + #mkarchroot "${chroot_path}" base base-devel sudo "${CHROOTEXTRAPKG[@]}" } -source /etc/libretools.conf - -if [ -e "$XDG_CONFIG_HOME/libretools/libretools.conf" ]; then - source "$XDG_CONFIG_HOME/libretools/libretools.conf" -fi - -CLEANCHROOT='false' -UPDATE='false' -CLEANREPO='false' -CHROOTNAME="${CHROOT:-${SUDO_USER:-root}}" - -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 - -[[ "$UID" != "0" ]] && { - error "This script must be run as root." - exit 1 + +clean_repo() { + msg "Cleaning repo for chroot: ${chroot_path}" + + if [ -d "${chroot_path}/repo" ]; then + find "${chroot_path}/repo/" -mindepth 1 -delete + else + mkdir -p "${chroot_path}/repo" + fi + bsdtar -czf "${chroot_path}/repo/repo.db.tar.gz" -T /dev/null + ln -s "repo.db.tar.gz" "${chroot_path}/repo/repo.db" } -shift $(($OPTIND - 1)) +update() { + msg "Updating chroot: ${chroot_path}" + mkarchroot -u "${chroot_path}" +} -if [ $# -eq 1 ]; then - CHROOTNAME="$1" -fi +enter() { + msg "Entering chroot: ${chroot_path}" + mkarchroot -r "bash" "${chroot_path}" +} -if "$CLEANREPO"; then - clean_repo -fi +usage() { + echo "Usage: $cmd [OPTIONS] [CHROOT]" + echo 'Interacts with a chroot.' + echo '' + echo "The default CHROOT is \`${CHROOT}'." + echo '' + echo 'Options:' + echo ' Settings:' + echo " -d Use this dir instead of \`${CHROOTDIR}'" + echo ' -l Use this as the chroot copy instead of basing it' + echo ' on the username' + echo ' Modes:' + echo ' -h Show this message' + echo ' -c Clean the chroot using pacman' + echo ' -r Clean /repo in the chroot' + echo ' -u Update the chroot' +} -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 +main() { + # The logic for setting CHROOTCOPY is mirred from makechrootpkg + CHROOTCOPY=$USER + [[ -n $SUDO_USER ]] && CHROOTCOPY=$SUDO_USER + [[ -z $CHROOTCOPY || $CHROOTCOPY = root ]] && CHROOTCOPY=copy + + local mode=enter + while getopts 'hrcud:l:' arg; do + case $arg in + d) CHROOTDIR=$OPTARG;; + l) CHROOTCOPY=$OPTARG;; + + c) mode=clean_chroot;; + r) mode=clean_repo;; + u) mode=update;; + + h) usage; exit 0;; + *) usage; exit 1;; + esac + done + shift $(($OPTIND - 1)) + case $# in + 0) :;; + 1) CHROOT="$1";; + *) usage; exit 1;; + esac + + # not local + chroot_path="${CHROOTDIR}/${CHROOT}/${CHROOTCOPY}" + + if (( EUID )); then + error "This script must be run as root." + exit 1 + fi + + case "$mode" in + clean_chroot) clean_chroot; exit $?;; + clean_repo) clean_repo; exit $?;; + update) update; exit $?;; + enter) enter; exit $?;; + esac +} -exit 0 +main "$@" -- cgit v1.2.3-2-g168b From e5c5ec7c6bf8d0460b870b1dacc5c928326fb81b Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Mon, 12 Nov 2012 18:56:53 -0500 Subject: librechroot: learn how to sync copies with root --- src/chroot-tools/librechroot | 119 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 101 insertions(+), 18 deletions(-) (limited to 'src/chroot-tools/librechroot') diff --git a/src/chroot-tools/librechroot b/src/chroot-tools/librechroot index d53448a..1c9700c 100755 --- a/src/chroot-tools/librechroot +++ b/src/chroot-tools/librechroot @@ -27,35 +27,107 @@ cmd=${0##*/} -clean_chroot() { # Clean packages with pacman - msg "Cleaning chroot with pacman: ${chroot_path}" +lock_open_write() { + local fd=$1 + local path=$2 + local msg=$3 + + # Only reopen the FD if it wasn't handed to us + if [[ $(readlink -f /dev/fd/$fd) != "${path}.lock" ]]; then + exec $fd>"${path}.lock" + fi + + if ! flock -n $fd; then + stat_busy "$msg" + flock $fd + stat_done + fi +} + +lock_open_read() { + local fd=$1 + local path=$2 + local msg=$3 + + # Only reopen the FD if it wasn't handed to us + if [[ $(readlink -f /dev/fd/$fd) != "${path}.lock" ]]; then + exec $fd>"${path}.lock" + fi - cp -a "$(dirname $0)/chcleanup" "${chroot_path}/clean" - mkarchroot -r "cd /build; /clean" "${chroot_path}" + if ! flock -sn $fd; then + stat_busy "$msg" + flock -s $fd + stat_done + fi +} - #mkarchroot "${chroot_path}" base base-devel sudo "${CHROOTEXTRAPKG[@]}" +lock_close() { + local fd=$1 + exec $fd>&- +} + +clean_pacman() { + msg "Cleaning chroot with pacman: ${copydir}" + + cp -a "$(dirname $0)/chcleanup" "${copydir}/clean" + mkarchroot -r "cd /build; /clean" "${copydir}" + + #mkarchroot "${copydir}" base base-devel sudo "${CHROOTEXTRAPKG[@]}" } clean_repo() { - msg "Cleaning repo for chroot: ${chroot_path}" + msg "Cleaning repo for chroot: ${copydir}" + + if [ -d "${copydir}/repo" ]; then + find "${copydir}/repo/" -mindepth 1 -delete + else + mkdir -p "${copydir}/repo" + fi + bsdtar -czf "${copydir}/repo/repo.db.tar.gz" -T /dev/null + ln -s "repo.db.tar.gz" "${copydir}/repo/repo.db" +} + +## +# This function is almost verbatim from makechrootpkg +## +sync() { + if [[ $CHROOTCOPY = root ]]; then + error "Cannot sync the root copy with itself" + exit 1 + fi + + # Note that '9' is the same FD number as in mkarchroot + lock_open_write 9 "$copydir" "Locking chroot copy '$copy'" + lock_open_read 8 "$rootdir" "Locking clean chroot" - if [ -d "${chroot_path}/repo" ]; then - find "${chroot_path}/repo/" -mindepth 1 -delete + stat_busy 'Creating clean working copy' + local use_rsync=false + if type -P btrfs >/dev/null; then + [[ -d $copydir ]] && btrfs subvolume delete "$copydir" &>/dev/null + btrfs subvolume snapshot "$chrootdir/root" "$copydir" &>/dev/null || + use_rsync=true else - mkdir -p "${chroot_path}/repo" + use_rsync=true fi - bsdtar -czf "${chroot_path}/repo/repo.db.tar.gz" -T /dev/null - ln -s "repo.db.tar.gz" "${chroot_path}/repo/repo.db" + + if $use_rsync; then + mkdir -p "$copydir" + rsync -a --delete -q -W -x "$CHROOTDIR/$CHROOT/root/" "$copydir" + fi + stat_done + + lock_close 8 + lock_close 9 } update() { - msg "Updating chroot: ${chroot_path}" - mkarchroot -u "${chroot_path}" + msg "Updating chroot: ${copydir}" + mkarchroot -u "${copydir}" } enter() { - msg "Entering chroot: ${chroot_path}" - mkarchroot -r "bash" "${chroot_path}" + msg "Entering chroot: ${copydir}" + mkarchroot -r "bash" "${copydir}" } usage() { @@ -72,6 +144,7 @@ usage() { echo ' Modes:' echo ' -h Show this message' echo ' -c Clean the chroot using pacman' + echo " -C Clean the chroot by syncing it with 'root' copy" echo ' -r Clean /repo in the chroot' echo ' -u Update the chroot' } @@ -88,7 +161,8 @@ main() { d) CHROOTDIR=$OPTARG;; l) CHROOTCOPY=$OPTARG;; - c) mode=clean_chroot;; + c) mode=clean_pacman;; + C) mode=sync;; r) mode=clean_repo;; u) mode=update;; @@ -104,15 +178,24 @@ main() { esac # not local - chroot_path="${CHROOTDIR}/${CHROOT}/${CHROOTCOPY}" + rootdir="${CHROOTDIR}/${CHROOT}/root" + copydir="${CHROOTDIR}/${CHROOT}/${CHROOTCOPY}" if (( EUID )); then error "This script must be run as root." exit 1 fi + if [[ ! -d $rootdir ]]; then + libremkchroot -d "$CHROOTDIR" "$CHROOT" + fi + + if [[ ! -d $copydir ]] && [[ $mode != sync ]]; then + sync + fi + case "$mode" in - clean_chroot) clean_chroot; exit $?;; + clean_pacman) clean_pacman; exit $?;; clean_repo) clean_repo; exit $?;; update) update; exit $?;; enter) enter; exit $?;; -- cgit v1.2.3-2-g168b From ec88851b92ead25bfdd1e29046c95e7dd4508f3f Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Mon, 12 Nov 2012 21:45:39 -0500 Subject: I'm embarrassed; I commited non-working file lock code --- src/chroot-tools/librechroot | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/chroot-tools/librechroot') diff --git a/src/chroot-tools/librechroot b/src/chroot-tools/librechroot index 1c9700c..2c952cc 100755 --- a/src/chroot-tools/librechroot +++ b/src/chroot-tools/librechroot @@ -34,7 +34,7 @@ lock_open_write() { # Only reopen the FD if it wasn't handed to us if [[ $(readlink -f /dev/fd/$fd) != "${path}.lock" ]]; then - exec $fd>"${path}.lock" + eval "exec $fd>${path}.lock" fi if ! flock -n $fd; then @@ -51,7 +51,7 @@ lock_open_read() { # Only reopen the FD if it wasn't handed to us if [[ $(readlink -f /dev/fd/$fd) != "${path}.lock" ]]; then - exec $fd>"${path}.lock" + eval "exec $fd>${path}.lock" fi if ! flock -sn $fd; then @@ -63,7 +63,7 @@ lock_open_read() { lock_close() { local fd=$1 - exec $fd>&- + eval "exec $fd>&-" } clean_pacman() { -- cgit v1.2.3-2-g168b From 80125fc674b087b42791d2bd48a74bfa7edb2fe0 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Mon, 12 Nov 2012 23:36:28 -0500 Subject: librechroot: fix a bug in clean_pacman --- src/chroot-tools/librechroot | 1 + 1 file changed, 1 insertion(+) (limited to 'src/chroot-tools/librechroot') diff --git a/src/chroot-tools/librechroot b/src/chroot-tools/librechroot index 2c952cc..07360d7 100755 --- a/src/chroot-tools/librechroot +++ b/src/chroot-tools/librechroot @@ -70,6 +70,7 @@ clean_pacman() { msg "Cleaning chroot with pacman: ${copydir}" cp -a "$(dirname $0)/chcleanup" "${copydir}/clean" + mkdir -p "$copydir/build" mkarchroot -r "cd /build; /clean" "${copydir}" #mkarchroot "${copydir}" base base-devel sudo "${CHROOTEXTRAPKG[@]}" -- cgit v1.2.3-2-g168b From e17b270bbfa29160beb34266533f5b2e2120041f Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Mon, 12 Nov 2012 23:37:29 -0500 Subject: librechroot: keep a lock on the CHROOTCOPY the whole time --- src/chroot-tools/librechroot | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/chroot-tools/librechroot') diff --git a/src/chroot-tools/librechroot b/src/chroot-tools/librechroot index 07360d7..a5c5728 100755 --- a/src/chroot-tools/librechroot +++ b/src/chroot-tools/librechroot @@ -97,8 +97,6 @@ sync() { exit 1 fi - # Note that '9' is the same FD number as in mkarchroot - lock_open_write 9 "$copydir" "Locking chroot copy '$copy'" lock_open_read 8 "$rootdir" "Locking clean chroot" stat_busy 'Creating clean working copy' @@ -118,7 +116,6 @@ sync() { stat_done lock_close 8 - lock_close 9 } update() { @@ -187,6 +184,10 @@ main() { exit 1 fi + # Keep this lock as long as we are running + # Note that '9' is the same FD number as in mkarchroot + lock_open_write 9 "$copydir" "Locking chroot copy '$copy'" + if [[ ! -d $rootdir ]]; then libremkchroot -d "$CHROOTDIR" "$CHROOT" fi -- cgit v1.2.3-2-g168b From fd1e5a426713715d9c0e3fabbfe0d8a20b629bad Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 28 Nov 2012 15:18:44 -0500 Subject: chroot-tools: disable overriding CHROOTDIR at the command line --- src/chroot-tools/librechroot | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/chroot-tools/librechroot') diff --git a/src/chroot-tools/librechroot b/src/chroot-tools/librechroot index a5c5728..48e6cdc 100755 --- a/src/chroot-tools/librechroot +++ b/src/chroot-tools/librechroot @@ -136,7 +136,6 @@ usage() { echo '' echo 'Options:' echo ' Settings:' - echo " -d Use this dir instead of \`${CHROOTDIR}'" echo ' -l Use this as the chroot copy instead of basing it' echo ' on the username' echo ' Modes:' @@ -156,7 +155,6 @@ main() { local mode=enter while getopts 'hrcud:l:' arg; do case $arg in - d) CHROOTDIR=$OPTARG;; l) CHROOTCOPY=$OPTARG;; c) mode=clean_pacman;; -- cgit v1.2.3-2-g168b From 00d67a5051e5d4163acffa80b7ef85f81f7e548f Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 28 Nov 2012 15:20:59 -0500 Subject: take (some) advantage of chroottools, fix some compatability things mkarchroot/archroot's option parsing is a little less flexible (but a lot more understandable) than it was before. --- src/chroot-tools/librechroot | 51 +++++--------------------------------------- 1 file changed, 5 insertions(+), 46 deletions(-) (limited to 'src/chroot-tools/librechroot') diff --git a/src/chroot-tools/librechroot b/src/chroot-tools/librechroot index 48e6cdc..8830c39 100755 --- a/src/chroot-tools/librechroot +++ b/src/chroot-tools/librechroot @@ -27,53 +27,12 @@ cmd=${0##*/} -lock_open_write() { - local fd=$1 - local path=$2 - local msg=$3 - - # Only reopen the FD if it wasn't handed to us - if [[ $(readlink -f /dev/fd/$fd) != "${path}.lock" ]]; then - eval "exec $fd>${path}.lock" - fi - - if ! flock -n $fd; then - stat_busy "$msg" - flock $fd - stat_done - fi -} - -lock_open_read() { - local fd=$1 - local path=$2 - local msg=$3 - - # Only reopen the FD if it wasn't handed to us - if [[ $(readlink -f /dev/fd/$fd) != "${path}.lock" ]]; then - eval "exec $fd>${path}.lock" - fi - - if ! flock -sn $fd; then - stat_busy "$msg" - flock -s $fd - stat_done - fi -} - -lock_close() { - local fd=$1 - eval "exec $fd>&-" -} - clean_pacman() { msg "Cleaning chroot with pacman: ${copydir}" cp -a "$(dirname $0)/chcleanup" "${copydir}/clean" mkdir -p "$copydir/build" - mkarchroot -r "cd /build; /clean" "${copydir}" - - #mkarchroot "${copydir}" base base-devel sudo "${CHROOTEXTRAPKG[@]}" + archroot "${copydir}" -r "cd /build; /clean" } clean_repo() { @@ -120,12 +79,12 @@ sync() { update() { msg "Updating chroot: ${copydir}" - mkarchroot -u "${copydir}" + archroot "${copydir}" -u } enter() { msg "Entering chroot: ${copydir}" - mkarchroot -r "bash" "${copydir}" + archroot "${copydir}" -r "bash" } usage() { @@ -183,11 +142,11 @@ main() { fi # Keep this lock as long as we are running - # Note that '9' is the same FD number as in mkarchroot + # Note that '9' is the same FD number as in (mk)archroot lock_open_write 9 "$copydir" "Locking chroot copy '$copy'" if [[ ! -d $rootdir ]]; then - libremkchroot -d "$CHROOTDIR" "$CHROOT" + libremkchroot "$CHROOT" fi if [[ ! -d $copydir ]] && [[ $mode != sync ]]; then -- cgit v1.2.3-2-g168b From 2a86ab6a3575724412f32fdbd794b0fd2738eb91 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 28 Nov 2012 19:17:54 -0500 Subject: reorganize, add a few features to librechroot --- src/chroot-tools/librechroot | 130 +++++++++++++++++++++++-------------------- 1 file changed, 69 insertions(+), 61 deletions(-) (limited to 'src/chroot-tools/librechroot') diff --git a/src/chroot-tools/librechroot b/src/chroot-tools/librechroot index 8830c39..c3c7374 100755 --- a/src/chroot-tools/librechroot +++ b/src/chroot-tools/librechroot @@ -1,52 +1,27 @@ -#!/bin/bash -# LibreChRoot -# Enters a chroot +#!/bin/bash -euE +# librechroot # Copyright 2010 Nicolás Reynolds # Copyright 2011 Joshua Haase # Copyright 2012 Luke Shumaker - -# ---------- GNU General Public License 3 ---------- - +# # This file is part of Parabola. - +# # Parabola is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. - +# # Parabola is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. - +# # You should have received a copy of the GNU General Public License # along with Parabola. If not, see . . /etc/libretools.conf -cmd=${0##*/} - -clean_pacman() { - msg "Cleaning chroot with pacman: ${copydir}" - - cp -a "$(dirname $0)/chcleanup" "${copydir}/clean" - mkdir -p "$copydir/build" - archroot "${copydir}" -r "cd /build; /clean" -} - -clean_repo() { - msg "Cleaning repo for chroot: ${copydir}" - - if [ -d "${copydir}/repo" ]; then - find "${copydir}/repo/" -mindepth 1 -delete - else - mkdir -p "${copydir}/repo" - fi - bsdtar -czf "${copydir}/repo/repo.db.tar.gz" -T /dev/null - ln -s "repo.db.tar.gz" "${copydir}/repo/repo.db" -} - ## # This function is almost verbatim from makechrootpkg ## @@ -62,8 +37,7 @@ sync() { local use_rsync=false if type -P btrfs >/dev/null; then [[ -d $copydir ]] && btrfs subvolume delete "$copydir" &>/dev/null - btrfs subvolume snapshot "$chrootdir/root" "$copydir" &>/dev/null || - use_rsync=true + btrfs subvolume snapshot "$chrootdir/root" "$copydir" &>/dev/null || use_rsync=true else use_rsync=true fi @@ -77,48 +51,49 @@ sync() { lock_close 8 } -update() { - msg "Updating chroot: ${copydir}" - archroot "${copydir}" -u -} - -enter() { - msg "Entering chroot: ${copydir}" - archroot "${copydir}" -r "bash" -} - usage() { - echo "Usage: $cmd [OPTIONS] [CHROOT]" + echo "Usage: $cmd [OPTIONS] [CHROOT] " echo 'Interacts with a chroot.' echo '' echo "The default CHROOT is \`${CHROOT}'." echo '' echo 'Options:' echo ' Settings:' - echo ' -l Use this as the chroot copy instead of basing it' + echo ' -l Use this as the chroot copy instead of basing it' echo ' on the username' - echo ' Modes:' - echo ' -h Show this message' - echo ' -c Clean the chroot using pacman' - echo " -C Clean the chroot by syncing it with 'root' copy" - echo ' -r Clean /repo in the chroot' + echo ' -N Disable networking in the chroot' + echo '' + echo ' Modes: (the last mode given will be used)' + echo ' -C Clean /repo in the chroot' + echo ' -c Clean the packages installed in the chroot' + echo ' -I Install the package FILE into the chroot' + echo ' -i Install the package PKG from repos into the chroot' + echo ' -n No-op, just make sure that the chroot exists' + echo ' -r Run CMD in the chroot' + echo " -s Sync the copy with the 'root' copy" echo ' -u Update the chroot' + echo ' -h Print this message' } main() { - # The logic for setting CHROOTCOPY is mirred from makechrootpkg - CHROOTCOPY=$USER - [[ -n $SUDO_USER ]] && CHROOTCOPY=$SUDO_USER - [[ -z $CHROOTCOPY || $CHROOTCOPY = root ]] && CHROOTCOPY=copy + CHROOTCOPY=$LIBREUSER + [[ $CHROOTCOPY != root ]] || CHROOTCOPY=copy local mode=enter - while getopts 'hrcud:l:' arg; do + local archroot_args=() + local ARG='' + while getopts 'l:NCcI:i:nrsuh' arg; do case $arg in l) CHROOTCOPY=$OPTARG;; + N) archroot_args+=(-N);; + C) mode=clean_repo c) mode=clean_pacman;; - C) mode=sync;; - r) mode=clean_repo;; + I) mode=install_file; ARG=$OPTARG;; + I) mode=install_pkg; ARG=$OPTARG;; + n) mode=noop;; + r) mode=run; ARG=$OPTARG;; + s) mode=sync;; u) mode=update;; h) usage; exit 0;; @@ -154,10 +129,43 @@ main() { fi case "$mode" in - clean_pacman) clean_pacman; exit $?;; - clean_repo) clean_repo; exit $?;; - update) update; exit $?;; - enter) enter; exit $?;; + clean_repo) + msg "Cleaning local pacman repository" + rm -rf "${copydir}/repo" + mkdir "${copydir}/repo" + bsdtar -czf "${copydir}/repo/repo.db.tar.gz" -T /dev/null + ln -s "repo.db.tar.gz" "${copydir}/repo/repo.db" + ;; + clean_pacman) + msg "Intelligently cleaning packages" + cp -a "$(dirname $0)/chcleanup" "${copydir}/clean" + mkdir -p "$copydir/build" + archroot "${archroot_args[@]}" "${copydir}" -r "cd /build; /clean" + ;; + install_file) + msg "Installing package file: $ARG" + cp "$ARG" "$copydir/${ARG##*/}" + archroot "${archroot_args[@]}" "$copydir" -r "pacman -U /${ARG##*/} --noconfirm" + rm "$copydir/${ARG##*/}" + ;; + install_pkg) + msg "Installing package(s): $ARG" + archroot "${archroot_args[@]}" "$copydir" -i $ARG + ;; + noop) :;; + run) + msg "Running command" + archroot "${archroot_args[@]}" "${copydir}" -r "$ARG" + ;; + sync) sync;; + update) + msg "Updating chroot" + archroot "${archroot_args[@]}" "${copydir}" -u + ;; + enter) + msg "Entering chroot" + archroot "${archroot_args[@]}" "${copydir}" -r bash + ;; esac } -- cgit v1.2.3-2-g168b From ba312fb72ec0843297978796a20c6ffc1fe3ef6e Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 28 Nov 2012 20:14:44 -0500 Subject: fix a typo --- src/chroot-tools/librechroot | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/chroot-tools/librechroot') diff --git a/src/chroot-tools/librechroot b/src/chroot-tools/librechroot index c3c7374..e39b105 100755 --- a/src/chroot-tools/librechroot +++ b/src/chroot-tools/librechroot @@ -87,7 +87,7 @@ main() { l) CHROOTCOPY=$OPTARG;; N) archroot_args+=(-N);; - C) mode=clean_repo + C) mode=clean_repo;; c) mode=clean_pacman;; I) mode=install_file; ARG=$OPTARG;; I) mode=install_pkg; ARG=$OPTARG;; -- cgit v1.2.3-2-g168b From 06f10b87d16d4c83016ff3ef21217494f921d93b Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 28 Nov 2012 22:10:26 -0500 Subject: many fixes --- src/chroot-tools/librechroot | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/chroot-tools/librechroot') diff --git a/src/chroot-tools/librechroot b/src/chroot-tools/librechroot index e39b105..d43ea2f 100755 --- a/src/chroot-tools/librechroot +++ b/src/chroot-tools/librechroot @@ -51,6 +51,7 @@ sync() { lock_close 8 } +cmd=${0##*/} usage() { echo "Usage: $cmd [OPTIONS] [CHROOT] " echo 'Interacts with a chroot.' @@ -80,7 +81,7 @@ main() { [[ $CHROOTCOPY != root ]] || CHROOTCOPY=copy local mode=enter - local archroot_args=() + local archroot_args=(-f) local ARG='' while getopts 'l:NCcI:i:nrsuh' arg; do case $arg in @@ -90,7 +91,7 @@ main() { C) mode=clean_repo;; c) mode=clean_pacman;; I) mode=install_file; ARG=$OPTARG;; - I) mode=install_pkg; ARG=$OPTARG;; + i) mode=install_pkg; ARG=$OPTARG;; n) mode=noop;; r) mode=run; ARG=$OPTARG;; s) mode=sync;; @@ -118,7 +119,7 @@ main() { # Keep this lock as long as we are running # Note that '9' is the same FD number as in (mk)archroot - lock_open_write 9 "$copydir" "Locking chroot copy '$copy'" + lock_open_write 9 "$copydir" "Locking chroot copy '$CHROOTCOPY'" if [[ ! -d $rootdir ]]; then libremkchroot "$CHROOT" @@ -139,8 +140,11 @@ main() { clean_pacman) msg "Intelligently cleaning packages" cp -a "$(dirname $0)/chcleanup" "${copydir}/clean" - mkdir -p "$copydir/build" - archroot "${archroot_args[@]}" "${copydir}" -r "cd /build; /clean" + echo '#!/bin/bash' > "${copydir}/cleanstrap" + echo 'mkdir /build' >> "${copydir}/cleanstrap" + echo 'cd /build; /clean' >> "${copydir}/cleanstrap" + chmod 755 "${copydir}/cleanstrap" + archroot "${archroot_args[@]}" "${copydir}" -r /cleanstrap ;; install_file) msg "Installing package file: $ARG" -- cgit v1.2.3-2-g168b From d602c1d5d462100313a2c6c47434c652444ba7fd Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 30 Nov 2012 16:07:48 -0500 Subject: move librechroot's sync into it's own file for licensing reasons --- src/chroot-tools/librechroot | 33 +++++---------------------------- 1 file changed, 5 insertions(+), 28 deletions(-) (limited to 'src/chroot-tools/librechroot') diff --git a/src/chroot-tools/librechroot b/src/chroot-tools/librechroot index d43ea2f..8e57004 100755 --- a/src/chroot-tools/librechroot +++ b/src/chroot-tools/librechroot @@ -22,34 +22,11 @@ . /etc/libretools.conf -## -# This function is almost verbatim from makechrootpkg -## -sync() { - if [[ $CHROOTCOPY = root ]]; then - error "Cannot sync the root copy with itself" - exit 1 - fi - - lock_open_read 8 "$rootdir" "Locking clean chroot" - - stat_busy 'Creating clean working copy' - local use_rsync=false - if type -P btrfs >/dev/null; then - [[ -d $copydir ]] && btrfs subvolume delete "$copydir" &>/dev/null - btrfs subvolume snapshot "$chrootdir/root" "$copydir" &>/dev/null || use_rsync=true - else - use_rsync=true - fi - - if $use_rsync; then - mkdir -p "$copydir" - rsync -a --delete -q -W -x "$CHROOTDIR/$CHROOT/root/" "$copydir" - fi - stat_done - - lock_close 8 -} +# This file (librechroot) is GPLv3+, but I would like to use some code +# modified from devtools' "makechrootpkg", which is GPLv2. +. "$(dirname "$0")/librechroot.gpl2" +# This gives us the functions: +# - sync cmd=${0##*/} usage() { -- cgit v1.2.3-2-g168b From 762e198c42dcd5b3da5c3a9dbe1006c7f85ec643 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 30 Nov 2012 16:10:48 -0500 Subject: [librechroot] have -n set the CHROOT; rename the old -n to -m https://labs.parabola.nu/issues/252 --- src/chroot-tools/librechroot | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'src/chroot-tools/librechroot') diff --git a/src/chroot-tools/librechroot b/src/chroot-tools/librechroot index 8e57004..e93c57e 100755 --- a/src/chroot-tools/librechroot +++ b/src/chroot-tools/librechroot @@ -30,13 +30,14 @@ cmd=${0##*/} usage() { - echo "Usage: $cmd [OPTIONS] [CHROOT] " + echo "Usage: $cmd [OPTIONS] " echo 'Interacts with a chroot.' echo '' echo "The default CHROOT is \`${CHROOT}'." echo '' echo 'Options:' echo ' Settings:' + echo " -n Use this chroot instead of \`$CHROOT'" echo ' -l Use this as the chroot copy instead of basing it' echo ' on the username' echo ' -N Disable networking in the chroot' @@ -46,7 +47,7 @@ usage() { echo ' -c Clean the packages installed in the chroot' echo ' -I Install the package FILE into the chroot' echo ' -i Install the package PKG from repos into the chroot' - echo ' -n No-op, just make sure that the chroot exists' + echo ' -m Make sure the chroot exists; do nothing else' echo ' -r Run CMD in the chroot' echo " -s Sync the copy with the 'root' copy" echo ' -u Update the chroot' @@ -60,8 +61,9 @@ main() { local mode=enter local archroot_args=(-f) local ARG='' - while getopts 'l:NCcI:i:nrsuh' arg; do + while getopts 'n:l:NCcI:i:mrsuh' arg; do case $arg in + n) CHROOT=$OPTARG;; l) CHROOTCOPY=$OPTARG;; N) archroot_args+=(-N);; @@ -69,7 +71,7 @@ main() { c) mode=clean_pacman;; I) mode=install_file; ARG=$OPTARG;; i) mode=install_pkg; ARG=$OPTARG;; - n) mode=noop;; + m) mode=noop;; r) mode=run; ARG=$OPTARG;; s) mode=sync;; u) mode=update;; @@ -79,16 +81,17 @@ main() { esac done shift $(($OPTIND - 1)) - case $# in - 0) :;; - 1) CHROOT="$1";; - *) usage; exit 1;; - esac + if [[ $# > 0 ]]; then + usage + exit 1 + fi # not local rootdir="${CHROOTDIR}/${CHROOT}/root" copydir="${CHROOTDIR}/${CHROOT}/${CHROOTCOPY}" + ######################################################################## + if (( EUID )); then error "This script must be run as root." exit 1 @@ -106,6 +109,8 @@ main() { sync fi + ######################################################################## + case "$mode" in clean_repo) msg "Cleaning local pacman repository" -- cgit v1.2.3-2-g168b From 63467291c9890288e983abae3c72e112aa028c66 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 30 Nov 2012 18:27:24 -0500 Subject: librechroot: use PATH to find chcleanup --- src/chroot-tools/librechroot | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/chroot-tools/librechroot') diff --git a/src/chroot-tools/librechroot b/src/chroot-tools/librechroot index e93c57e..6594f15 100755 --- a/src/chroot-tools/librechroot +++ b/src/chroot-tools/librechroot @@ -121,7 +121,7 @@ main() { ;; clean_pacman) msg "Intelligently cleaning packages" - cp -a "$(dirname $0)/chcleanup" "${copydir}/clean" + cp -a "$(which chcleanup)" "${copydir}/clean" echo '#!/bin/bash' > "${copydir}/cleanstrap" echo 'mkdir /build' >> "${copydir}/cleanstrap" echo 'cd /build; /clean' >> "${copydir}/cleanstrap" -- cgit v1.2.3-2-g168b