discard flag gone away, trim disabled

This is for discussing general topics about how to use VirtualBox.
Post Reply
DoubleHP
Posts: 13
Joined: 25. Nov 2016, 15:00

discard flag gone away, trim disabled

Post by DoubleHP »

Hello. Debian host and guest. VB v5.

Less than one year ago, I have enabled --discard for all guest storages, last week, I have seen in logs that one guest fails on it's weekly "fstrim /".

1h ago, in guest:

Code: Select all

# fstrim -a -v
Then I ran

Code: Select all

# systemctl status virtualbox-leon-05.service
# VBoxManage storageattach Leon-05 --storagectl "SATA" --port 0 --discard on --nonrotational on
# systemctl start virtualbox-leon-05.service
and now, in the guest:

Code: Select all

# hdparm -I /dev/sda | grep TRIM
           *    Data Set Management TRIM supported (limit unknown)
Other guests still have discard on.

1: for which reason could one guest loose DISCARD, and not other guests ?

2: is there a way to check the status of this flag perdiocally ? Goes the guest need to be off for this check, or can I perform this check live (from the host) ?

This feature is critial on long term. It helps me keeping my guest images small, and allows over-allocation of host storage (12 guests can have a 160G disk each on a 256G host; weekly trim allows to shrink VDI files when possible. Overall, my host usually stays around 80% free; but all guests can use huge space when needed). In the end, keeping my host always with more than 50% free spaces increases the SSD lifetime. For example, after running fstrim on this guest, my host gained 4G free space. I encourage all VB users to study this feature; it's really VERY usefull.
mpack
Site Moderator
Posts: 39134
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: discard flag gone away, trim disabled

Post by mpack »

You've made some assertions and asked us to speculate. I'm going to speculate that at least one assertion is incorrect.

AFAIK, options do not delete themselves from .vbox files.
DoubleHP
Posts: 13
Joined: 25. Nov 2016, 15:00

Re: discard flag gone away, trim disabled

Post by DoubleHP »

Answer for -2- :

Code: Select all

# VBoxManage storageattach Leon-09 --storagectl "SATA" --port 0 --discard off --nonrotational on
# for i in */*.vbox ; do cat $i | grep -q -e 'discard="true"' || echo "Missing DISCARD in $i" ; cat $i | grep -q -e 'nonrotational="true"' || echo "Missing NONROT in $i" ; done
Missing DISCARD in Leon-09/Leon-09.vbox
# VBoxManage storageattach Leon-09 --storagectl "SATA" --port 0 --discard on --nonrotational on
# for i in */*.vbox ; do cat $i | grep -q -e 'discard="true"' || echo "Missing DISCARD in $i" ; cat $i | grep -q -e 'nonrotational="true"' || echo "Missing NONROT in $i" ; done
So, here is my new cron script:

Code: Select all

# cat /etc/cron.daily/discard-check
#!/bin/bash

help=0

cd "/root/VirtualBox VMs"
for i in */*.vbox
do
        cat $i | grep -q -e 'discard="true"' || { echo "Missing DISCARD in $i" ; help=1 ; }
        cat $i | grep -q -e 'nonrotational="true"' || { echo "Missing NONROT in $i" ; help=1 ; }
done

[ $help -gt 0 ] && {
        echo
        echo "At least one guest seems to lack Discard or Nonrotationnal flags."
        echo
        echo "To set those flags, run a command like this on host:
VBoxManage storageattach Leon-03 --storagectl \"SATA\" --port 0 --discard on --nonrotational on
VBoxManage storageattach Leon-03 --storagectl \"SATA\" --port 1 --discard on --nonrotational on

To check flags are set:
run $0
or
cat \"/root/VirtualBox VMs/GUEST/GUEST.vbox\"

From inside the guest:
hdparm -I /dev/sda | grep TRIM
fstrim -v -a

OR

To fix all 12 guests in one shot:
for i in \$(seq 3 9 ) ; do VBoxManage storageattach Leon-0\$i --storagectl "SATA" --port 0 --discard on --nonrotational on ; done
for i in 0 1 2 ; do VBoxManage storageattach Leon-1\$i --storagectl "SATA" --port 0 --discard on --nonrotational on ; done

"

}
true
This code is working for me, for my server; it assumes only one disk per guest. The SEQ part could be optimised.

Still waiting an explanation for -1-.
Post Reply