From f72297a639cda567107f83c1da4eafa2de604d9f Mon Sep 17 00:00:00 2001 From: Jan Steffens Date: Sun, 13 Mar 2011 17:57:28 +0100 Subject: Use readlink -e to canonicalize chrootdir --- makechrootpkg | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'makechrootpkg') diff --git a/makechrootpkg b/makechrootpkg index e3e2baf..726f828 100755 --- a/makechrootpkg +++ b/makechrootpkg @@ -67,8 +67,9 @@ while getopts 'hcudr:I:l:' arg; do esac done -#Get rid of trailing / in chrootdir -[ "$chrootdir" != "/" ] && chrootdir=$(echo $chrootdir | sed 's#/$##') +# Canonicalize chrootdir, getting rid of trailing / +chrootdir=$(readlink -e "$chrootdir") + copydir="$chrootdir/$COPY" # Pass all arguments after -- right to makepkg -- cgit v1.2.3-70-g09d2 From 2ff5c45d148ae137f0cdacf0677a53b06d272c6d Mon Sep 17 00:00:00 2001 From: Lukas Fleischer Date: Fri, 11 Mar 2011 23:15:53 +0100 Subject: makechrootpkg: Copy package logs with split packages (fixes FS#23239). Signed-off-by: Lukas Fleischer --- makechrootpkg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'makechrootpkg') diff --git a/makechrootpkg b/makechrootpkg index 726f828..9facac6 100755 --- a/makechrootpkg +++ b/makechrootpkg @@ -233,7 +233,7 @@ if mkarchroot -r "/chrootbuild" "$copydir"; then fi done - for l in "${copydir}"/build/{namcap,*-{build,package}}.log; do + for l in "${copydir}"/build/{namcap,*-{build,package,package_*}}.log; do [ -f "$l" ] && mv "$l" "${WORKDIR}" done else -- cgit v1.2.3-70-g09d2 From 36dc5d8792599c547889fcaa30052b7c09e7add0 Mon Sep 17 00:00:00 2001 From: Jan Steffens Date: Fri, 18 Mar 2011 08:49:37 +0100 Subject: Stop the PACKAGER= and MAKEFLAGS= from piling on Repeatedly reusing the same chroot kept adding lines to makepkg.conf. --- makechrootpkg | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'makechrootpkg') diff --git a/makechrootpkg b/makechrootpkg index 9facac6..a38a740 100755 --- a/makechrootpkg +++ b/makechrootpkg @@ -161,9 +161,15 @@ if ! grep 'SRCDEST="/srcdest"' "$copydir/etc/makepkg.conf" >/dev/null 2>&1; then echo 'SRCDEST="/srcdest"' >> "$copydir/etc/makepkg.conf" fi [ -z "${MAKEFLAGS}" ] && eval $(grep '^MAKEFLAGS=' /etc/makepkg.conf) -[ -n "${MAKEFLAGS}" ] && echo "MAKEFLAGS='${MAKEFLAGS}'" >> "$copydir/etc/makepkg.conf" +if [ -n "${MAKEFLAGS}" ]; then + sed -i '/^MAKEFLAGS=/d' "$copydir/etc/makepkg.conf" + echo "MAKEFLAGS='${MAKEFLAGS}'" >> "$copydir/etc/makepkg.conf" +fi [ -z "${PACKAGER}" ] && eval $(grep '^PACKAGER=' /etc/makepkg.conf) -[ -n "${PACKAGER}" ] && echo "PACKAGER='${PACKAGER}'" >> "$copydir/etc/makepkg.conf" +if [ -n "${PACKAGER}" ]; then + sed -i '/^PACKAGER=/d' "$copydir/etc/makepkg.conf" + echo "PACKAGER='${PACKAGER}'" >> "$copydir/etc/makepkg.conf" +fi # Set target CARCH as it might be used within the PKGBUILD to select correct sources eval $(grep '^CARCH=' "$copydir/etc/makepkg.conf") -- cgit v1.2.3-70-g09d2 From 174ff59dba8c24f544e354cd43f3b68aea91d265 Mon Sep 17 00:00:00 2001 From: Jan Steffens Date: Sun, 13 Mar 2011 19:06:27 +0100 Subject: Add flock-based locking to chroots This prevents accidents when chroots are shared between multiple users. --- archbuild | 13 +++++++++++++ makechrootpkg | 23 +++++++++++++++++++++++ mkarchroot | 16 ++++++++++++++++ 3 files changed, 52 insertions(+) (limited to 'makechrootpkg') diff --git a/archbuild b/archbuild index 7e8c456..9dd4888 100755 --- a/archbuild +++ b/archbuild @@ -36,6 +36,19 @@ fi if ${clean_first} || [ ! -d "${chroots}/${repo}-${arch}" ]; then echo "Creating chroot for [${repo}] (${arch})..." + + for copy in ${chroots}/${repo}-${arch}/*; do + [[ -d $copy ]] || continue + echo "Deleting chroot copy '$(basename "${copy}")'..." + + # Lock the copy + exec 9>${copy}.lock + flock 9 + + rm -rf ${copy} + done + exec 9>&- + rm -rf ${chroots}/${repo}-${arch} mkdir -p ${chroots}/${repo}-${arch} setarch ${arch} mkarchroot \ diff --git a/makechrootpkg b/makechrootpkg index a38a740..1f6f20a 100755 --- a/makechrootpkg +++ b/makechrootpkg @@ -105,11 +105,34 @@ if [ ! -d "$chrootdir/root" ]; then fi umask 0022 + +# Lock the chroot we want to use. We'll keep this lock until we exit. +# Note this is the same FD number as in mkarchroot +exec 9>"$copydir.lock" +if ! flock -n 9; then + echo -n "locking chroot copy '$copy'..." + flock 9 + echo "done" +fi + if [ ! -d "$copydir" -o "$clean_first" -eq "1" ]; then + # Get a read lock on the root chroot to make + # sure we don't clone a half-updated chroot + exec 8>"$chrootdir/root.lock" + + if ! flock -sn 8; then + echo -n "locking clean chroot..." + flock -s 8 + echo "done" + fi + echo -n 'creating clean working copy...' mkdir -p "$copydir" rsync -a --delete -q -W -x "$chrootdir/root/" "$copydir" echo 'done' + + # Drop the read lock again + exec 8>&- fi if [ -n "$install_pkg" ]; then diff --git a/mkarchroot b/mkarchroot index 254841e..5c6548e 100755 --- a/mkarchroot +++ b/mkarchroot @@ -141,6 +141,20 @@ chroot_umount () { umount "${working_dir}/${cache_dir}" [ -n "${host_mirror_path}" ] && umount "${working_dir}/${host_mirror_path}" } + +chroot_lock () { + # Only reopen the FD if it wasn't handed to us + if [ "$(readlink -f /dev/fd/9)" != "${working_dir}.lock" ]; then + exec 9>"${working_dir}.lock" + fi + + # Lock the chroot. Take note of the FD number. + if ! flock -n 9; then + echo -n "locking chroot..." + flock 9 + echo "done" + fi +} # }}} umask 0022 @@ -153,6 +167,7 @@ if [ "$RUN" != "" ]; then exit 1 fi + chroot_lock chroot_mount copy_hostconf @@ -169,6 +184,7 @@ else mkdir -p "${working_dir}/var/lib/pacman/sync" mkdir -p "${working_dir}/etc/" + chroot_lock chroot_mount pacargs="--noconfirm --root=${working_dir} --cachedir=${cache_dir}" -- cgit v1.2.3-70-g09d2 From 0af05a48abb1d35380f4f4259deb163eb3b7b174 Mon Sep 17 00:00:00 2001 From: Jan Steffens Date: Sun, 13 Mar 2011 15:19:20 +0100 Subject: Use Btrfs snapshots for chroot copies, when available This is much faster than using Rsync to clone. Rsync stays available when the chroots are not on a Btrfs. --- archbuild | 1 + makechrootpkg | 14 ++++++++++++-- mkarchroot | 4 ++++ 3 files changed, 17 insertions(+), 2 deletions(-) (limited to 'makechrootpkg') diff --git a/archbuild b/archbuild index 9dd4888..19b734b 100755 --- a/archbuild +++ b/archbuild @@ -45,6 +45,7 @@ if ${clean_first} || [ ! -d "${chroots}/${repo}-${arch}" ]; then exec 9>${copy}.lock flock 9 + { type -P btrfs && btrfs subvolume delete ${copy}; } &>/dev/null rm -rf ${copy} done exec 9>&- diff --git a/makechrootpkg b/makechrootpkg index 1f6f20a..5bf492f 100755 --- a/makechrootpkg +++ b/makechrootpkg @@ -127,8 +127,18 @@ if [ ! -d "$copydir" -o "$clean_first" -eq "1" ]; then fi echo -n 'creating clean working copy...' - mkdir -p "$copydir" - rsync -a --delete -q -W -x "$chrootdir/root/" "$copydir" + 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/root/" "$copydir" + fi echo 'done' # Drop the read lock again diff --git a/mkarchroot b/mkarchroot index 5c6548e..f385731 100755 --- a/mkarchroot +++ b/mkarchroot @@ -181,6 +181,10 @@ else exit 1 fi + if { type -P btrfs && btrfs subvolume create "${working_dir}"; } &>/dev/null; then + chmod 0755 "${working_dir}" + fi + mkdir -p "${working_dir}/var/lib/pacman/sync" mkdir -p "${working_dir}/etc/" -- cgit v1.2.3-70-g09d2 From 2fa8fdec478529cd8649d38f9bfe2e50a3e28469 Mon Sep 17 00:00:00 2001 From: Jan Steffens Date: Sun, 13 Mar 2011 19:07:04 +0100 Subject: Make default copydir user-dependent Eases usage when chroots are shared between multiple users. --- makechrootpkg | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'makechrootpkg') diff --git a/makechrootpkg b/makechrootpkg index 5bf492f..2a9f56b 100755 --- a/makechrootpkg +++ b/makechrootpkg @@ -12,7 +12,6 @@ FORCE='n' RUN='' MAKEPKG_ARGS='-s --noconfirm' REPACK='' -COPY='copy' WORKDIR=$PWD update_first='0' @@ -24,6 +23,10 @@ chrootdir='' APPNAME=$(basename "${0}") +default_copy=$USER +[[ -n $SUDO_USER ]] && default_copy=$SUDO_USER +[[ -z $default_copy || $default_copy = root ]] && default_copy=copy + usage() { echo "usage ${APPNAME} [options] -r [--] [makepkg args]" echo ' Run this script in a PKGBUILD dir to build a package inside a' @@ -50,7 +53,8 @@ usage() { echo '-r The chroot dir to use' echo '-I Install a package into the working copy of the chroot' echo '-l The directory to use as the working copy of the chroot' - echo ' Useful for maintain multiple copies Default: copy' + echo ' Useful for maintaining multiple copies.' + echo " Default: $default_copy" exit 1 } @@ -62,7 +66,7 @@ while getopts 'hcudr:I:l:' arg; do d) add_to_db=1 ;; r) chrootdir="$OPTARG" ;; I) install_pkg="$OPTARG" ;; - l) COPY="$OPTARG" ;; + l) copy="$OPTARG" ;; *) MAKEPKG_ARGS="$MAKEPKG_ARGS -$arg $OPTARG" ;; esac done @@ -70,7 +74,8 @@ done # Canonicalize chrootdir, getting rid of trailing / chrootdir=$(readlink -e "$chrootdir") -copydir="$chrootdir/$COPY" +[[ -z $copy ]] && copy=$default_copy +copydir="$chrootdir/$copy" # Pass all arguments after -- right to makepkg MAKEPKG_ARGS="$MAKEPKG_ARGS ${*:$OPTIND}" -- cgit v1.2.3-70-g09d2