My VirtualBox filesystem looks like:

# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda2       29799396 5467616  22795012  20% /
devtmpfs         1929980       0   1929980   0% /dev
tmpfs            1940308      12   1940296   1% /dev/shm
tmpfs            1940308    8712   1931596   1% /run
tmpfs            1940308       0   1940308   0% /sys/fs/cgroup
/dev/sdb        31441920 1124928  30316992   4% /srv/node/d1
/dev/sdc        31441920   49612  31392308   1% /srv/node/d2
/dev/sdd        31441920   34252  31407668   1% /srv/node/d3
/dev/sda1         999320  253564    676944  28% /boot
tmpfs             388064       0    388064   0% /run/user/0

Disks /dev/sdb, /dev/sdc, /dev/sdd are VDI data disks. I removed some data from them (not everything) and would like to use zerofree to compress them afterwards. Looks like I can't use zerofree on those disks. Here is an execution:

# zerofree -v /dev/sdb
zerofree: failed to open filesystem /dev/sdb

Is it possible to use zerofree on such disks? If not, is there any alternative solution? I need to keep the existing data on those disks, but use zerofree (or anything else) to fill removed data with zeros.

  • zerofree will expand thin provisioning disks, you will end up worse if using thin provisioning. Also not advised using them frequently with SSD disks. – Rui F Ribeiro Feb 9 at 9:51
  • Thank you for the tip! Do you know an alternative solution to use then? – Alexandr Feb 9 at 9:56
  • Are you using thin provisioning? I might write an answer around that then. – Rui F Ribeiro Feb 9 at 9:57
  • If virtualbox supports discard and your filesystems do too, then you can just run fstrim. Much more convenient than zerofree but requires support both within the VM and by the virtualization/host. – frostschutz Feb 9 at 10:57
  • Thank you both for your tips! I found an alternative solution which works well for my case. I've posted the answer. – Alexandr Feb 9 at 11:10

I didn't find the answer on how to use zerofree on such disks but I found an alternative solution which works well.

  1. Mount your disk somewhere (in my case 3 disks are mounted to locations: /srv/node/d1, /srv/node/d2, /srv/node/d3).
  2. Enter the directory where your disk is mounted (cd /srv/node/d1).
  3. Perform the command: dd if=/dev/zero of=zerofillfile bs=1M
  4. Remove the a created file: rm -f zerofillfile
  5. Perform the above operations for all disks.

P.S. not related to this question, but for virtual box disk compaction, use the command after performing the above commands:

VBoxManage modifyhd --compact /path/to/my/disks/disk1.vdi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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