How to use zerofree on a whole disk?

7,578

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
Share:
7,578

Related videos on Youtube

Oleksandr
Author by

Oleksandr

Updated on September 18, 2022

Comments

  • Oleksandr
    Oleksandr almost 2 years

    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.

    • Admin
      Admin over 5 years
      zerofree will expand thin provisioning disks, you will end up worse if using thin provisioning. Also not advised using them frequently with SSD disks.
    • Admin
      Admin over 5 years
      Thank you for the tip! Do you know an alternative solution to use then?
    • Admin
      Admin over 5 years
      Are you using thin provisioning? I might write an answer around that then.
    • Admin
      Admin over 5 years
      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.
    • Admin
      Admin over 5 years
      Thank you both for your tips! I found an alternative solution which works well for my case. I've posted the answer.