cmdlib: Move cache2.qcow2 creation to be locked
Recent changes to parallelize builds have turned up a whole
lot of race conditions.   This is one; we only need to create
`cache2.qcow2` for the main `cosa build ostree`.  While here
I also added locking on general principle.
Partially fixes
@@ -143,14 +143,6 @@ prepare_build() {
preflight
if ! [ -d builds ]; then
fatal "No $(pwd)/builds found; did you run coreos-assembler init?"
- elif ! has_privileges; then
- # "cache2" has an explicit label so we can find it in qemu easily
- if [ ! -f cache/cache2.qcow2 ]; then
- qemu-img create -f qcow2 cache/cache2.qcow2 10G
- LIBGUESTFS_BACKEND=direct virt-format --filesystem=xfs --label=cosa-cache -a cache/cache2.qcow2
- fi
- # And remove the old one
- rm -vf cache/cache.qcow2
fi
workdir="$(pwd)"
@@ -393,11 +385,16 @@ runcompose() {
sudo -E "$@"
sudo chown -R -h "${USER}":"${USER}" "${tmprepo}"
else
- compose_qemu_args=()
- if [ -f "${workdir}/cache/cache2.qcow2" ]; then
+ # "cache2" has an explicit label so we can find it in qemu easily
+ if [ ! -f "${workdir}"/cache/cache2.qcow2 ]; then
+ qemu-img create -f qcow2 cache2.qcow2.tmp 10G
+ LIBGUESTFS_BACKEND=direct virt-format --filesystem=xfs --label=cosa-cache -a cache2.qcow2.tmp &&
+ mv -T cache2.qcow2.tmp "${workdir}"/cache/cache2.qcow2
+ fi
+ # And remove the old one
+ rm -vf "${workdir}"/cache/cache.qcow2
compose_qemu_args+=("-drive" "if=none,id=cache,discard=unmap,file=${workdir}/cache/cache2.qcow2" \
"-device" "virtio-blk,drive=cache")
- fi
runvm "${compose_qemu_args[@]}" -- "$@"
fi
}
