Playing with sparse files

Joseph Hall joseph at
Thu Mar 10 12:09:08 MST 2011

I'm playing with some sparse files today, and I'm confused as to what
might be causing this situation. Here's the story, forgive me if it
gets a little long:

First, I create a sparse file:

# dd if=/dev/null of=sparse bs=100k seek=5120

Then I drop a filesystem on it and mount it:

# mkfs.ext3 sparse
# mkdir /mnt/sparse
# mount -o loop /mnt/sparse sparse

I check the space reported to be used by the file, and the space
actually used by the file:

# ls -lh sparse
-rw-r--r-- 1 root root 500M Mar 10 18:51 sparse
# du -hs sparse
25M    sparse

Then I drop a file full of zeros inside the file:

dd if=/dev/zero of=/mnt/sparse/test bs=4k count=5120

Check the actual space used:

# du -hs sparse
30M    sparse

Copy the sparse file sparsely:

# cp --sparse=always sparse sparse2

Check the size:

# du -hs sparse*
30M    sparse
1.5M    sparse2

Question 1: Why is my non-sparse file full of zeros, sitting inside
the filesystem inside the new sparse file, not taking up a lot of
Question 2: Why is the metadata not taking up a lot of space?

So I decided to try a different approach. I unmounted and wiped out
everything, then did everything above all from scratch, with one
exception: I made the new non-sparse file using /dev/urandom instead
of /dev/zero. When i finished sparsely copying the sparse file, I got
the following:

# du -hs sparse*
45M    sparse
22M    sparse2

Question 3: Same as question 2.

If it helps, this is all happening on top of an ext3 filesystem,
running on a GFS volume on a SAN. Because I love abstraction.

Any thoughts?


More information about the PLUG mailing list