From 3491ea5d799479c9ae0da2c2864084146d61a10a Mon Sep 17 00:00:00 2001 From: Jan Steffens Date: Sat, 12 Mar 2011 15:16:31 +0100 Subject: Syntax cleanup No functional change. --- mkarchroot | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'mkarchroot') diff --git a/mkarchroot b/mkarchroot index ac05a6b..254841e 100755 --- a/mkarchroot +++ b/mkarchroot @@ -26,7 +26,7 @@ usage() { echo ' -C Location of a pacman config file' echo ' -M Location of a makepkg config file' echo ' -n Do not copy config files into the chroot' - echo " -c Set pacman cache. Default: /var/cache/pacman/pkg" + echo ' -c Set pacman cache. Default: /var/cache/pacman/pkg' echo ' -h This message' exit $1 } @@ -68,7 +68,7 @@ shift 1 if [ -z "$cache_dir" ]; then cache_conf=${working_dir}/etc/pacman.conf [ ! -f $cache_conf ] && cache_conf=${pac_conf:-/etc/pacman.conf} - cache_dir=$((grep -m 1 '^CacheDir' $cache_conf || echo 'CacheDir = /var/cache/pacman/pkg') | sed 's/CacheDir\s*=\s*//') + cache_dir=$( (grep -m 1 '^CacheDir' $cache_conf || echo 'CacheDir = /var/cache/pacman/pkg') | sed 's/CacheDir\s*=\s*//') unset cache_conf fi @@ -82,8 +82,7 @@ if echo "${host_mirror}" | grep -q 'file://'; then host_mirror_path=$(echo "${host_mirror}" | sed -E 's#file://(/.*)/\$repo/os/\$arch#\1#g') fi -# {{{ functions - +# {{{ functions chroot_mount() { [ -e "${working_dir}/sys" ] || mkdir "${working_dir}/sys" mount -t sysfs sysfs "${working_dir}/sys" @@ -160,7 +159,7 @@ if [ "$RUN" != "" ]; then eval chroot "${working_dir}" ${RUN} # }}} - else +else # {{{ build chroot if [ -e "${working_dir}" -a "${FORCE}" = "n" ]; then echo "error: working dir '${working_dir}' already exists - try using -f" -- cgit v1.2.3-54-g00ecf 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 'mkarchroot') 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-54-g00ecf 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 'mkarchroot') 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-54-g00ecf