Revision 31f11819 common.sh.in

b/common.sh.in
165 165
}
166 166

  
167 167
cleanup() {
168
  # if something fails here, it souldn't call cleanup again...
169
  trap - EXIT
170

  
168 171
  if [ ${#CLEANUP[*]} -gt 0 ]; then
169 172
    LAST_ELEMENT=$((${#CLEANUP[*]}-1))
170 173
    REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
171 174
    for i in $REVERSE_INDEXES; do
172
      ${CLEANUP[$i]}
175
        # If something fails here, it's better to retry it for a few times
176
        # before we give up with an error. This is needed for kpartx when
177
        # dealing with ntfs partitions mounted through fuse. umount is not
178
        # synchronous and may return while the partition is still busy. A
179
        # premature attempt to delete partition mappings through kpartx on a
180
        # device that hosts previously mounted ntfs partition may fail with an 
181
        # `device-mapper: remove ioctl failed: Device or resource busy'
182
        # error. A sensible workaround for this is to wait for a while and then
183
        # try again.
184
        local cmd=${CLEANUP[$i]}
185
        $cmd || for interval in 0.25 0.5 1 2 4; do
186
            echo "Command $cmd failed!"
187
            echo "I'll wait for $interval secs and will retry..."
188
            sleep $interval
189
            $cmd && break
190
        done
191
        test $? -eq 1 && { echo "Giving Up..."; exit 1; }
173 192
    done
174 193
  fi
194
  echo "Clean UP executed"
175 195
}
176 196

  
177 197
trap cleanup EXIT

Also available in: Unified diff