Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How to clear unused space with zeros ? (ext3,ext4)

I'm looking for something smarter than

cat /dev/zero > /mnt/X/big_zero ; sync; rm /mnt/X/big_zero

Like FSArchiver is looking for "used space" and ignores unused, but opposite site.

Purpose: I'd like to compress partition images, so filling unused space with zeros is highly recommended.

Btw. For btrfs : Clear unused space with zeros (btrfs)

share|improve this question
2  
Check this out: superuser.com/questions/19326/… –  Mat Jul 29 '12 at 10:18
    
Two different kind of answer are possible. What are you trying to achieve? Either 1) security, by forbidding someone to read those data, or 2) optimizing compression of the whole partition or SSD performance? –  Totor Jan 5 at 2:57

4 Answers 4

up vote 17 down vote accepted

Such an utility is zerofree.

From its description:

 Zerofree finds the unallocated, non-zeroed blocks in an ext2 or ext3
 file-system and fills them with zeroes. This is useful if the device
 on which this file-system resides is a disk image. In this case,
 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 requires the file-system to be unmounted or mounted
 read-only.
 .
 The usual way to achieve the same result (zeroing the unused
 blocks) is to run "dd" do create a file full of zeroes that takes up
 the entire free space on the drive, and then delete this file. This
 has many disadvantages, which zerofree alleviates:
  * it is slow;
  * it makes the disk image (temporarily) grow to its maximal extent;
  * it (temporarily) uses all free space on the disk, so other
    concurrent write actions may fail.
 .
 Zerofree has been written to be run from GNU/Linux systems installed
 as guest OSes inside a virtual machine. If this is not your case, you
 almost certainly don't need this package.

UPDATE #1

The description of the .deb package contains the following paragraph now which would imply this will work fine with ext4 too.

Description: zero free blocks from ext2, ext3 and ext4 file-systems Zerofree finds the unallocated blocks with non-zero value content in an ext2, ext3 or ext4 file-system and fills them with zeroes...

share|improve this answer
    
Is it official page of the tool intgat.tigress.co.uk/rmy/uml/index.html ? Do you think it's safe to use with ext4 ? –  Grzegorz Wierzowiecki Jul 29 '12 at 14:08
2  
@GrzegorzWierzowiecki: yes, that is the page, but for debian and friends it is already in the repos. I used on a ext4 partition on a virtual disk to successively shrink the disk file image, and had no problem. –  enzotib Jul 29 '12 at 14:12

sfill from secure-delete can do this and several other related jobs.

e.g.

sfill -l -l -z /mnt/X

UPDATE #1

There is a source tree that appears to be used by the ArchLinux project on github that contains the source for sfill which is a tool included in the package Secure-Delete.

Also a copy of sfill's man page is here:

share|improve this answer
    
that URL is obsolete. no idea where its home page is now (or even if it still has one), but it's packaged for debian and ubuntu. probably other distros too. if you need source code, that can be found in the debian archives if you can't find it anywhere else. –  cas Jul 29 '12 at 12:04

If you have e2fsprogs 1.42.9, then you can use e2image to create the partition image without the free space in the first place, so you can skip the zeroing step.

share|improve this answer

You can use sfill. It's a better solution for thin volumes.

share|improve this answer
    
If you want to comment on cas answer, wait until you have enough reputation to do so. –  Anthon Apr 2 at 15:01
2  
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. –  Anthon Apr 2 at 15:01
    
I think the answer is referring to manpages.ubuntu.com/manpages/lucid/man1/sfill.1.html ... which is at least an attempt at answering. ("online" in this case meaning "with the filesystem mounted", not "on the web"). –  derobert Apr 2 at 17:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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