I wouldnt do this.
I wouldnt do it like this - there's no error checking, you dont test
outputs before moving on to next stage,`mount -a` is almost certainly
not what you want.
That said, it works for me - 300G image on a 1TB drive
What shell are you running? (I note you dont specify in the script)
Is `cd` at all relevant, or would *any* command do?
Would it work if you just waited for a few minutes?
My guess is that there's some issue between the *first* losetup and the
LOOPDEV variable, and on the second run you pick up the *first* - you
could test this with a long sleep between the losetup line and the
LOOPDEV line.