#!/bin/bash

set -o nounset
set -o errexit
shopt -s nullglob
cd $(dirname "${0}")

if [ -f ppid ]; then
  kill -9 $(cat ppid) 2> /dev/null || true
  rm -f ppid
fi

# Wait for the kernel to release resources
for i in $(seq 20); do
  [ $(losetup -j fs | wc -l) -eq 0 ] && break
  sleep 0.1
done

#!/bin/bash

set -o nounset
set -o errexit
shopt -s nullglob
cd $(dirname "${0}")

# Wait for the kernel to release resources
while [ 1 ]; do
  if [ -f ppid ]; then
    kill -9 $(cat ppid) 2> /dev/null || true
    rm -f ppid
  fi

  [ $(losetup -j fs | wc -l) -eq 0 ] && exit 0
  sleep 0.1
done

exit 1

Move container pid check inside loop
warden: Move container pid check inside loop

There is a race between the start operation being in progress while
destroy is first executed, where the pid is not yet written when the
destroy script tries to read it. Checking for the pid file from inside
the loop body solves this issue.

Change-Id: I2f21cc0a87a731d24c901ce68111d12b1808f758
