Revision 171aea62 common.sh.in

b/common.sh.in
376 376
}
377 377

  
378 378
cleanup() {
379
  # if something fails here, it souldn't call cleanup again...
380
  trap - EXIT
381

  
379 382
  if [ ${#CLEANUP[*]} -gt 0 ]; then
380 383
    LAST_ELEMENT=$((${#CLEANUP[*]}-1))
381 384
    REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
382 385
    for i in $REVERSE_INDEXES; do
383
      ${CLEANUP[$i]}
386
        # If something fails here, it's better to retry it for a few times
387
        # before we give up with an error. This is needed for kpartx when
388
        # dealing with ntfs partitions mounted through fuse. umount is not
389
        # synchronous and may return while the partition is still busy. A
390
        # premature attempt to delete partition mappings through kpartx on a
391
        # device that hosts previously mounted ntfs partition may fail with an 
392
        # `device-mapper: remove ioctl failed: Device or resource busy'
393
        # error. A sensible workaround for this is to wait for a while and then
394
        # try again.
395
        local cmd=${CLEANUP[$i]}
396
        $cmd || for interval in 0.25 0.5 1 2 4; do
397
            echo "Command $cmd failed!"
398
            echo "I'll wait for $interval secs and will retry..."
399
            sleep $interval
400
            $cmd && break
401
        done
402
        test $? -eq 1 && { echo "Giving Up..."; exit 1; }
384 403
    done
385 404
  fi
405
  echo "Clean UP executed"
386 406
}
387 407

  
388 408
trap cleanup EXIT

Also available in: Unified diff