5

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.

5
  • zerofree will expand thin provisioning disks, you will end up worse if using thin provisioning. Also not advised using them frequently with SSD disks. Commented Feb 9, 2019 at 9:51
  • Thank you for the tip! Do you know an alternative solution to use then?
    – Oleksandr
    Commented Feb 9, 2019 at 9:56
  • Are you using thin provisioning? I might write an answer around that then. Commented Feb 9, 2019 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. Commented Feb 9, 2019 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.
    – Oleksandr
    Commented Feb 9, 2019 at 11:10

4 Answers 4

6

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
1
  • This creates a file that fills up the entire disk, which may not be a viable solution for virtual disks
    – endolith
    Commented Oct 18, 2022 at 20:50
0

You most likely will need to mount the filesystem as read-only before running Zerofree. The Zerofree man page explains how to do this.

filesystem has to be unmounted or mounted read-only for zerofree to work. It will exit with an error message if the filesystem is mounted writable. To remount the root file-system readonly, you can first switch to single user runlevel (telinit 1) then use mount -o remount,ro filesystem.

1
  • That remount can also fail if the filesystem is in use
    – golimar
    Commented Oct 20, 2022 at 5:07
0

Seems original question let an VDI/QCOW2/RAW file access, no needs of "telinit 1" if host is linux ;)

This is part of my "on the scratch" bash backup scheme with zerofree processes on a RAW file.

Script is OK under Lubuntu 22.04 LTS , i'm on my VM to write these stuffs here ;)

  pathe=/media/sebastian/VM
  nname=lubuntu.raw
  if [ -f /media/$USER/$hd/$nname ]; then
   sudo apt install zerofree
  fi 
  res1=`sudo losetup -fP --show $pathe/$nname`
  echo DEBUG: Now RAW file is on $res1 .
  losetup -l
  ls -l ${res1}*
  res2=${res1#/dev/*}
  sudo zerofree -v /dev/${res2}
  for i in {0..5}
  do
   sudo zerofree -v /dev/${res2}p${i}
  done
0

Just use the Live CD from this Open Source Project, on Linux Hosts / Guests ( this Live CD has the zerofree utility built in; also read the instructions on the site ) :

https://sourceforge.net/projects/live-cd-with-zerofree-utility/

This will also help in reducing the size of an exported virtual machine appliance as well ( i.e. .ova file ).

As others mentioned, after zerofree is run, use the compact option, to reduce the vdi file size.

P.S. A 32-Bit Version ( i686 architecture ) of this Live CD is available at: https://sourceforge.net/projects/live-cd-with-zerofree-32-bit/

For very OLD Architectures, Please try this Live CD : https://sourceforge.net/projects/live-cd-with-zerofree-x86/

You must log in to answer this question.

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