lib/common.sh: Introduce locking helper functions
Reduces code duplication. With makechrootpkg not calling mkarchroot anymore, the lock handover protocol is unneeded. arch-nspawn does not do any locking, so add protection to archbuild.
This commit is contained in:
parent
0e98bd8c48
commit
a7a05deb37
|
@ -51,12 +51,7 @@ if ${clean_first} || [[ ! -d "${chroots}/${repo}-${arch}" ]]; then
|
||||||
[[ -d $copy ]] || continue
|
[[ -d $copy ]] || continue
|
||||||
msg2 "Deleting chroot copy '$(basename "${copy}")'..."
|
msg2 "Deleting chroot copy '$(basename "${copy}")'..."
|
||||||
|
|
||||||
exec 9>"$copydir.lock"
|
lock 9 "$copydir.lock" "Locking chroot copy '$copy'"
|
||||||
if ! flock -n 9; then
|
|
||||||
stat_busy "Locking chroot copy '$copy'"
|
|
||||||
flock 9
|
|
||||||
stat_done
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "$(stat -f -c %T "${copy}")" == btrfs ]]; then
|
if [[ "$(stat -f -c %T "${copy}")" == btrfs ]]; then
|
||||||
{ type -P btrfs && btrfs subvolume delete "${copy}"; } &>/dev/null
|
{ type -P btrfs && btrfs subvolume delete "${copy}"; } &>/dev/null
|
||||||
|
@ -73,6 +68,7 @@ if ${clean_first} || [[ ! -d "${chroots}/${repo}-${arch}" ]]; then
|
||||||
"${chroots}/${repo}-${arch}/root" \
|
"${chroots}/${repo}-${arch}/root" \
|
||||||
"${base_packages[@]}" || abort
|
"${base_packages[@]}" || abort
|
||||||
else
|
else
|
||||||
|
lock 9 "${chroots}/${repo}-${arch}/root.lock" "Locking clean chroot"
|
||||||
setarch ${arch} arch-nspawn \
|
setarch ${arch} arch-nspawn \
|
||||||
-C "@pkgdatadir@/pacman-${repo}.conf" \
|
-C "@pkgdatadir@/pacman-${repo}.conf" \
|
||||||
-M "@pkgdatadir@/makepkg-${arch}.conf" \
|
-M "@pkgdatadir@/makepkg-${arch}.conf" \
|
||||||
|
|
|
@ -130,3 +130,27 @@ get_full_version() {
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
##
|
||||||
|
# usage : lock( $fd, $file, $message )
|
||||||
|
##
|
||||||
|
lock() {
|
||||||
|
eval "exec $1>"'"$2"'
|
||||||
|
if ! flock -n $1; then
|
||||||
|
stat_busy "$3"
|
||||||
|
flock $1
|
||||||
|
stat_done
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
##
|
||||||
|
# usage : slock( $fd, $file, $message )
|
||||||
|
##
|
||||||
|
slock() {
|
||||||
|
eval "exec $1>"'"$2"'
|
||||||
|
if ! flock -sn $1; then
|
||||||
|
stat_busy "$3"
|
||||||
|
flock -s $1
|
||||||
|
stat_done
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
|
@ -120,24 +120,12 @@ umask 0022
|
||||||
chroottype=$(stat -f -c %T "$chrootdir")
|
chroottype=$(stat -f -c %T "$chrootdir")
|
||||||
|
|
||||||
# Lock the chroot we want to use. We'll keep this lock until we exit.
|
# 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
|
lock 9 "$copydir.lock" "Locking chroot copy [$copy]"
|
||||||
exec 9>"$copydir.lock"
|
|
||||||
if ! flock -n 9; then
|
|
||||||
stat_busy "Locking chroot copy [$copy]"
|
|
||||||
flock 9
|
|
||||||
stat_done
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ ! -d $copydir ]] || $clean_first; then
|
if [[ ! -d $copydir ]] || $clean_first; then
|
||||||
# Get a read lock on the root chroot to make
|
# Get a read lock on the root chroot to make
|
||||||
# sure we don't clone a half-updated chroot
|
# sure we don't clone a half-updated chroot
|
||||||
exec 8>"$chrootdir/root.lock"
|
slock 8 "$chrootdir/root.lock" "Locking clean chroot"
|
||||||
|
|
||||||
if ! flock -sn 8; then
|
|
||||||
stat_busy "Locking clean chroot"
|
|
||||||
flock -s 8
|
|
||||||
stat_done
|
|
||||||
fi
|
|
||||||
|
|
||||||
stat_busy "Creating clean working copy [$copy]"
|
stat_busy "Creating clean working copy [$copy]"
|
||||||
if [[ "$chroottype" == btrfs ]]; then
|
if [[ "$chroottype" == btrfs ]]; then
|
||||||
|
|
|
@ -49,29 +49,13 @@ else
|
||||||
cache_dirs=(${cache_dir})
|
cache_dirs=(${cache_dir})
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# {{{ functions
|
|
||||||
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
|
|
||||||
stat_busy "Locking chroot"
|
|
||||||
flock 9
|
|
||||||
stat_done
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
# }}}
|
|
||||||
|
|
||||||
umask 0022
|
umask 0022
|
||||||
|
|
||||||
[[ -e $working_dir ]] && die "Working directory '$working_dir' already exists"
|
[[ -e $working_dir ]] && die "Working directory '$working_dir' already exists"
|
||||||
|
|
||||||
mkdir -p "$working_dir"
|
mkdir -p "$working_dir"
|
||||||
|
|
||||||
chroot_lock
|
lock 9 "${working_dir}.lock" "Locking chroot"
|
||||||
|
|
||||||
if [[ $(stat -f -c %T "$working_dir") == btrfs ]]; then
|
if [[ $(stat -f -c %T "$working_dir") == btrfs ]]; then
|
||||||
rmdir "$working_dir"
|
rmdir "$working_dir"
|
||||||
|
|
Loading…
Reference in New Issue