Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I use windows server 2008r2 with role Hyper-V. The guest system is Ubuntu 12.04 LTC. It is situated on the dynamic virtual hard disk (2TB). The size of this vhd is 360 GB. But the real size is about 50 GB.

I want to reduce the size of this vhd using standard Hyper-V tools. But I can not do this, because the Ubuntu file system is not NTFS.

How can I reset the free space on the disk .vhd?

share|improve this question
This probably isn't a Unix/Linux question. The problem you are having is finding the tool on the Windows Hypervisor to resize a virtual hard drive. Sounds like a Windows question. – Tim Nov 6 '13 at 15:12
no, the question is: "How can I fill the free space with zeroes in Ubuntu 12.04?" I also do not have the internet on the machine with Linux. – AmShegar Nov 7 '13 at 6:25
After filling the free space with zeroes the file system will still not be NTFS so standard Hyper-V tools still will not work. Either you're leaving something out or it's a Windows question. – Doug O'Neal Nov 7 '13 at 20:30
If your hypervisor supports it (no idea if Hyper-V does), then fstrim may do what you want. With a lot less I/O, and far faster. – derobert Nov 13 '13 at 11:58
lwn.net/Articles/566769 has a patch to make fstrim work. Not sure if/when it went into the mainline kernel, but that's fairly recent. If it works for you (or you're willing to patch your kernel to make it work), that's a FAR better solution. – derobert Nov 13 '13 at 12:04
up vote 1 down vote accepted

How can I fill the free space with zeroes in Ubuntu 12.04 (ext4)?

It's rather simple:

dd if=/dev/zero of=/some/path/to/zerofile bs=128M count=NumOf128MBlocksToFillTheDiskWith

You'd better have some space left untouched with that commands, say 1 %. By default EXT…FSes have special amount of reserved space for root user (AFAIR, 5 %), so in case you'd run this command as ordinary user, you won't fill up the whole disk, and that's great, since another system software running as root may malfunction in that case. But even running this dd as ordinary user you'd better not use the rest available space (95 %), otherwise other programms like, say Firefox, wouldn't be happy to realize they can't write to disk anymore and you may end up with inconsistent data. So leave the space untouched. It'd be even better to run this in single mode, then you'd squeeze the maximum and won't hurt anything.

After that, needless to say, but just in case — simply remove /some/path/to/zerofile.

P. S. Thanks to @Weijian for sharing info regarding zerofree which does suit better for EXT2/EXT3/EXT4.

share|improve this answer

in Ubuntu, you can use zerofree(http://manpages.ubuntu.com/manpages/natty/man8/zerofree.8.html)

sudo aptitude install zerofree

dd is okay but may have some disadvantages according to the link above

share|improve this answer
Wow, thanks, it's always nice to find out there's better solution ;) – poige Nov 7 '13 at 16:23

Your Answer


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.