[lug] Convertion of ext4 Sparse Files

Aaron D. Johnson adj at fnord.greeley.co.us
Fri Jan 5 16:16:56 MST 2018


stimits at comcast.net writes:
> Unfortunately this does not work (currently testing on Fedora 27),
> at least not for this file which was originally a dd of a GPT
> partition and then converted to sparse at a later time. Output and
> input files are both the sparse file size.
>

> It may be a matter of dd or other tools not detecting this as sparse
> for whatever reason, but I keep running into options for creating
> sparse files and not t he reverse, e.g., dd has "conv=sparse", but
> not the reverse, so I can't force it to do a sparse to non-sparse
> conversion.

GNU coreutils is being entirely too smart for you.  I didn't know it
did this, but here's what happens on one of my machines:

    $ truncate -s 1G sparse-file    # make 1Gibyte sparse file
    $ cp sparse-file this-should-be-a-sparse-file
    $ cp --sparse=3Dnever sparse-file not-sparse-file    # copy to non-spa=
rse file =

    $ ls -ls *sparse-file
    1048580 -rw-r--r-- 1 adj adj 1073741824 Jan  5 15:57 not-sparse-file
          0 -rw-r--r-- 1 adj adj 1073741824 Jan  5 15:56 sparse-file
	  0 -rw-r--r-- 1 adj adj 1073741824 Jan  5 16:07 this-should-be-a-sparse-=
file
    $

A sparse file isn't a special type of file (at least on UNIX-y
filesystems -- I think it might be special on NTFS.)  Being sparse
just means that not all of the data blocks have been written to and
the filesystem nicely doesn't allocate on-disk space for those
unwritten blocks.  Reads from offsets that haven't been written to
will return all zeroes.  Writing zeros on your own will allocate
blocks.  (At least on ext[234] filesystems; ZFS knows better.)  The
"--sparse=3Dnever" option on the cp above tells it "write all the zeroes
out, don't just seek to the next non-zero block"

Hope this helps.

- Aaron


More information about the LUG mailing list