I've been working with qemu raw images and I just had a few questions about using tar with them.

From what I've read, bsdtar with kernel >= 3.1 is able to handle the sparse image files much quicker than gnu tar can because it can take advantage of the seek_hole functionality in the kernel. I tested it out and it is significantly quicker than tar.

My question is this... my image file (full size) is 260G. Since it isn't full and is sparse it only actually takes up 38G. When I do a tar -cvSf test.img.tar test.img it takes a long time (~10 minutes) but I end up with a file that's 20G. If I untar, it goes back up to 38G. When I do a bsdtar -cvf test.img.tar test.img it goes much quicker (~2.5 minutes), but the filesize is 38G intead of the 20G that gnu tar gave me.

What's the difference? Why is the filesize smaller with tar? I would expect the behavior to be like what bsdtar did because I thought tar -S only forced tar to treat the file as a sparse file and not expand it so I don't get why its smaller.

Thanks in advance!


From the GNU tar manual (info):

8.1.2 Archiving Sparse Files

Files in the file system occasionally have "holes". A "hole" in a file is a section of the file's contents which was never written. The contents of a hole reads as all zeros. On many operating systems, actual disk storage is not allocated for holes, but they are counted in the length of the file. If you archive such a file, 'tar' could create an archive longer than the original. To have 'tar' attempt to recognize the holes in a file, use '--sparse' ('-S'). When you use this option, then, for any file using less disk space than would be expected from its length, 'tar' searches the file for consecutive stretches of zeros. It then records in the archive for the file where the consecutive stretches of zeros are, and only archives the "real contents" of the file. On extraction (using '--sparse' is not needed on extraction) any such files have holes created wherever the continuous stretches of zeros were found. Thus, if you use '--sparse', 'tar' archives won't take more space than the original.

'-S' '--sparse' This option instructs 'tar' to test each file for sparseness before attempting to archive it. If the file is found to be sparse it is treated specially, thus allowing to decrease the amount of space used by its image in the archive.

This option is meaningful only when creating or updating archives. It has no effect on extraction.

Consider using '--sparse' when performing file system backups, to avoid archiving the expanded forms of files stored sparsely in the system.

Even if your system has no sparse files currently, some may be created in the future. If you use '--sparse' while making file system backups as a matter of course, you can be assured the archive will never take more space on the media than the files take on disk (otherwise, archiving a disk filled with sparse files might take hundreds of tapes). *Note Incremental Dumps::.

However, be aware that '--sparse' option presents a serious drawback. Namely, in order to determine if the file is sparse 'tar' has to read it before trying to archive it, so in total the file is read twice. So, always bear in mind that the time needed to process all files with this option is roughly twice the time needed to archive them without it.

When using 'POSIX' archive format, GNU 'tar' is able to store sparse files using in three distinct ways, called "sparse formats". A sparse format is identified by its "number", consisting, as usual of two decimal numbers, delimited by a dot. By default, format '1.0' is used. If, for some reason, you wish to use an earlier format, you can select it using '--sparse-version' option.


Select the format to store sparse files in. Valid VERSION values are: '0.0', '0.1' and '1.0'. *Note Sparse Formats::, for a detailed description of each format.

Using '--sparse-format' option implies '--sparse'.

(emphasis added)

Ie, it's slower because it reads the file(s) twice; the first time to analyze the file contents, second time to actually archive them.
This approach to detecting sparseness probably also explains why the archive ends up even smaller; quite possibly there are significant sequences of zeroes that are not actually stored sparsely.

  • Ah, that would make sense. I understood the difference in speed but it didn't occur to me that gnu tar is probably finding more sequences of zeros since it analyzes the whole thing. Thanks! – user165222 Aug 12 '15 at 0:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.