Rsync of sparse qemu image increases disk size

qemursyncsparse-filesxfs

I have a sparse raw qemu image that I want to transfer to another server. qemu-img info gives me:

image: sparse.img
file format: raw
virtual size: 50G
disk size: 16G

I transfer it with:

rsync -azhP --sparse origin:/path/to/img/sparse.img .

Now, on the destination server I have:

image: sparse.img
file format: raw
virtual size: 50G
disk size: 40G

However, after running virt-sparsify again on the copied image, I get this:

image: sparse.img
file format: raw
virtual size: 50G
disk size: 16G

Both servers are running CentOS 7.2 on a XFS filesystem. So what happened?

Update:

After some more research, I found several posts indicating that rsync doesn't handle sparse files well, and that it's better to use a different tool, such as tar, to transfer sparse files.

You could follow up the tar transfer with rsync --inplace, to make sure the file was transferred wtihout errors, as explained here.

Another solution that was proposed was creating an empty sparse file of the same size on the destination, and then using rsync --inplace to transfer the actual data.

I didn't write this as a solution, because it doesn't really explain why rsync --sparse is behaving this way.

Best Answer

I bet this is due to FS disparities between the source and the destination.

Let me elaborate with an example. Sparse files are files whose empty blocks (i.e. full of 0) are not allocated on the disk. The smaller the block size on the FS, the likelier such a block can be found. So, your issue may be due to a block size being bigger on the destination than on the source.

There may be other XFS parameters that I don't know.

See also this question on ServerFault

Related Question