0

For my virtual machine host, I use Windows Server 2008r2 with role Hyper-V, while the guest VM is Ubuntu 12.04 LTS. It is located on a dynamic virtual hard disk (2TB). The size of this VM's VHD is 360 GB. But the real size shown within Ubuntu is about 50 GB.

It's apparently not possible to reduce the size of this VHD using standard Hyper-V tools, because the file system used by the Ubuntu VM is not NTFS.

Is there another way of doing it on Ubuntu that would result in resetting the free space on the VHD outside?

6
  • 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
    Commented Nov 6, 2013 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
    Commented Nov 7, 2013 at 6:25
  • 1
    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
    Commented Nov 13, 2013 at 11:58
  • 1
    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
    Commented Nov 13, 2013 at 12:04
  • 1
    Actually, since you're on Ubuntu: bugs.launchpad.net/ubuntu/+source/linux/+bug/1223499 it's fixed in Saucy. You could probably backport that patch to LTS, or maybe if you ask nicely Ubuntu will do it...
    – derobert
    Commented Nov 13, 2013 at 12:08

2 Answers 2

4

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

3
  • Wow, thanks, it's always nice to find out there's better solution ;)
    – poige
    Commented Nov 7, 2013 at 16:23
  • For zerofree to work (at least on Ubuntu 12 LTS), the filesystem has to be unmounted or mounted read-only, which means it's not really panacea. Commented Oct 5, 2016 at 7:46
  • For the record, zerofree only works on "ext2, ext3 and ext4 file-systems", so the requirement is probably not strictly "Ubuntu only" but it will not, for example, work for CentOS/RHEL if you're using the default XFS filesystem there.
    – pzkpfw
    Commented Apr 12, 2021 at 7:33
3

UPDATE from 2019 (since someone upvoted and I noted it again)

Meanwhile the part of my answer that elaborates on dd use is correct by itself, proper answer to the question how to let hypervisor know that there's some not-in-fact-occupied disk space inside its guest block storage is to leverage TRIM/Discard commands.


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.

1
  • currently I'd say you need running fstrim
    – poige
    Commented Oct 6, 2016 at 8:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .