1

What I am doing

I am studying the filesystem image sparse tool: zerofree.

I imagine the steps to create the scenario should be that:

  1. create a file system image.
  2. use some way to fill it.
  3. clear it, but the actual space usage is not changed, which means unallocated or unused space exist on this fs.
  4. running zerofree, its actual space usage decrease.

But after running the zerofree to fs, the actual space usage is not shrinking.

Is there something I misunderstand? How can I do it correctly?

Any suggestion is helpful!

Record of what I tried

# root❯❯❯ dd if=/dev/zero of=fs.image bs=1M seek=1024 count=0
0+0 records in
0+0 records out
0 bytes copied, 4.8167e-05 s, 0.0 kB/s
# root❯❯❯ du -h fs.image
0 fs.image
# root❯❯❯ mkdir /mnt/test
# root❯❯❯ mkfs.ext3 fs.image
mke2fs 1.45.6 (20-Mar-2020)
Discarding device blocks: done
Creating filesystem with 262144 4k blocks and 65536 inodes
Filesystem UUID: d65a10e7-4620-433b-a135-386d65acf414
Superblock backups stored on blocks:
  32768, 98304, 163840, 229376

Allocating group tables: done Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done

root❯❯❯ du -h fs.image

33M fs.image

root❯❯❯ mount fs.image /mnt/test/

root❯❯❯ yes abcdefghijklmnopqrstuvwxyz0123456789 > /mnt/test/largefile

yes: standard output: No space left on device

root❯❯❯ du -h fs.image

1005M fs.image

root❯❯❯ df -h

Filesystem Size Used Avail Use% Mounted on /dev/loop0 976M 976M 0 100% /mnt/test

root❯❯❯ rm /mnt/test/largefile

rm: remove regular file '/mnt/test/largefile'? y

root@oe ~/../project-zerofree ❯❯❯ du -h fs.image

1007M fs.image

root❯❯❯ df -h

Filesystem Size Used Avail Use% Mounted on /dev/loop0 976M 1.3M 924M 1% /mnt/test

root❯❯❯ umount /mnt/test

root❯❯❯ du -h fs.image

1007M fs.image

root❯❯❯ zerofree -v fs.image

249255/249500/262144

root❯❯❯ du -h fs.image

1007M fs.image

2 Answers2

2

Thanks for mokubai's answer that help me figure out the right way to test zerofree.

1. Make a file system image

# root❯❯❯ dd if=/dev/zero of=test.img bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 0.49236 s, 2.2 GB/s
# root❯❯❯ mkfs.ext4 test.img
mke2fs 1.45.6 (20-Mar-2020)
Discarding device blocks: done
Creating filesystem with 262144 4k blocks and 65536 inodes
Filesystem UUID: 4423921e-cb7c-4850-b29f-45625ce8e86f
Superblock backups stored on blocks:
  32768, 98304, 163840, 229376

Allocating group tables: done Writing inode tables: done Creating journal (8192 blocks): done Writing superblocks and filesystem accounting information: done

root❯❯❯ du -h test.img

33M test.img

2. Fill it

# root❯❯❯ mount test.img /mnt/test/
# root❯❯❯ yes abcdefghijklmnopqrstuvwxyz0123456789 > /mnt/test/largefile
yes: standard output: No space left on device
# root❯❯❯ du -h test.img
990M    test.img

3. Create unused space

# root❯❯ rm -f /mnt/test/largefile
# root❯❯❯ umount /mnt/test
# root❯❯❯ du -h test.img
990M    test.img

4. Copy with sparse option

# root❯❯❯ cp --sparse=always test.img sparsed.img
# root❯❯❯ du -h *
959M    sparsed.img
990M    test.img

There is a little effect.

5. Running zerofree

# root❯❯❯ zerofree -v test.img
245055/249189/262144
# root❯❯❯ cp --sparse=always test.img zerofreed_and_sparsed.img
# root❯❯❯ du -h *
959M    sparsed.img
990M    test.img
1.1M    zerofreed_and_sparsed.img

Obvious effect!

0

Zerofree does nothing more or less than writing zeroes to free areas of an image. On its own that is not going to do anything to the size of an image on disk.

From the zerofree man page

zerofree finds the unallocated, blocks with non-zero value content in an ext2, ext3 or ext4 filesystem (e.g. /dev/hda1) and fills them with zeroes (or another octet of your choice).

And

depending on the type of disk image, a secondary utility may be able to reduce the size of the disk image after zerofree has been run.

zerofree alone is not a command to convert images to being sparse, at best it can facilitate the conversion of such images by effectively erasing zones that contain no real data but it cannot do it on its own.

One use of it is to erase an area of disk prior to compacting within VM tools in a similar way to the sdelete tool on Windows

Mokubai
  • 95,412