The reason that you get an EOF is the block size on the device.
Block devices like hard disks treat the device as a series of fixed-size blocks, usually some multiple of 4096 bytes. Any attempt to read or write from the device normally uses the kernel methods read() and write(), which deal in a whole block of data at a time.
After running your example, I get this:
Code:
$ sudo blockdev --getsize /dev/loop0
40960
which shows a 40kiB block size.
i.e. the kernel thinks that the file is made up of 40k chunks. Unfortunately, it isn't, because you ran "echo 1 >> a", which adds an extra *two* bytes to the file.
Since the file size isn't a multiple of 4096 bytes, you can use it as a block device, but the kernel can't read a whole block from the end of the file, hence the error.
There are two options to losetup that let you get around this: add "-o 2" to offset the position of the image being read within the file, or use "--sizelimit 2097152" to limit the size to 2MiB.