Trim does not work on VirtualBox 5.2.8

Discussions about using Linux guests in VirtualBox.
Post Reply
Löscha
Posts: 3
Joined: 10. Apr 2018, 11:57

Trim does not work on VirtualBox 5.2.8

Post by Löscha »

I have this in the .VBOX file:

Code: Select all

<AttachedDevice discard="true" nonrotational="true" type="HardDisk" hotpluggable="false" port="0" device="0">
    <Image uuid="{460a432f-g142-5a8b-df77-e12a3b0695cc}"/>
</AttachedDevice>
The guest OS is Ubuntu 16.04 x64, file system = ext4.

When i try to execute

Code: Select all

fstrim -v /
I'll get this error:

Code: Select all

fstrim: /: the discard operation is not supported
The harddisk is physically a ssd drive
ssd-drive.png
ssd-drive.png (45.42 KiB) Viewed 6780 times

Code: Select all

hdparm -I /dev/sda

/dev/sda:

ATA device, with non-removable media
        Model Number:       VBOX HARDDISK                           
        Serial Number:      VB4634c117-973718fa 
        Firmware Revision:  1.0     
Standards:
        Used: unknown (minor revision code 0x0028) 
        Supported: 8 7 6 5 
        Likely used: 8
Configuration:
        Logical         max     current
        cylinders       16383   16383
        heads           16      16
        sectors/track   63      63
        --
        CHS current addressable sectors:   16514064
        LBA    user addressable sectors:   83886080
        LBA48  user addressable sectors:   83886080
        Logical/Physical Sector size:           512 bytes
        device size with M = 1024*1024:       40960 MBytes
        device size with M = 1000*1000:       42949 MBytes (42 GB)
        cache/buffer size  = 256 KBytes (type=DualPortCache)
        Nominal Media Rotation Rate: Solid State Device
Capabilities:
        LBA, IORDY(cannot be disabled)
        Queue depth: 32
        Standby timer values: spec'd by Vendor, no device specific minimum
        R/W multiple sector transfer: Max = 128 Current = 128
        DMA: mdma0 mdma1 mdma2 udma0 udma1 udma2 udma3 udma4 udma5 *udma6 
             Cycle time: min=120ns recommended=120ns
        PIO: pio0 pio1 pio2 pio3 pio4 
             Cycle time: no flow control=120ns  IORDY flow control=120ns
Commands/features:
        Enabled Supported:
           *    Power Management feature set
           *    Write cache
           *    Look-ahead
           *    48-bit Address feature set
           *    Mandatory FLUSH_CACHE
           *    FLUSH_CACHE_EXT
           *    Gen2 signaling speed (3.0Gb/s)
           *    Native Command Queueing (NCQ)
Checksum: correct
Why does the discard operation not work?

Thank you!
Last edited by socratis on 6. May 2018, 07:23, edited 1 time in total.
Reason: Replaced attachment with actual text. Be more "green".
socratis
Site Moderator
Posts: 27330
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: Trim does not work on VirtualBox 5.2.8

Post by socratis »

Löscha wrote:Why does the discard operation not work?
What exactly are you expecting the TRIM operation to do in a virtual hard drive, which is pretty much a single file on your host, which is the one that actually does all the logistics regarding the SSD drive?

Are you expecting a TRIM command in LibreOffice for example? Not really...

Moving to "Linux Guests" from "Windows Hosts", since the host is not important here. I could have moved it to the "Using VirtualBox" section since this is a more generic question, covering more than a specific host or guest family. We'll see how the discussion goes...


PS. I replaced your screenshot with the /dev/sda properties with the actual text. Much smaller, more readable, searchable...
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
Löscha
Posts: 3
Joined: 10. Apr 2018, 11:57

Re: Trim does not work on VirtualBox 5.2.8

Post by Löscha »

socratis wrote:
Löscha wrote:Why does the discard operation not work?
What exactly are you expecting the TRIM operation to do in a virtual hard drive, which is pretty much a single file on your host, which is the one that actually does all the logistics regarding the SSD drive?
The file size of the virtual hard disk is about 520 GB, but

Code: Select all

df -h
reports only 100 GB, so i would like to shrink the file size of the virtual hard drive, according to that blog article for example: https://erikwramner.wordpress.com/2017/ ... with-trim/.

Many other websites are reporting that VirtualBox has TRIM support to shrink the disk size of the virtual hard drive: https://www.google.com/search?q=fstrim+ ... virtualbox but i didn't get it running.

Why?

Thank you!
socratis
Site Moderator
Posts: 27330
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: Trim does not work on VirtualBox 5.2.8

Post by socratis »

I don't necessarily trust/believe/support whatever you might be reading on the internet, that's a vast place. You could ask them... On the other hand, I do trust the manual. It's more of an ... authority on the issue, any VirtualBox issue. ;)
Löscha wrote:Why?
Most probably because you didn't read the manual carefully, or you read half of it, or not at all? Or the blog for that matter? Ch. 8.18 VBoxManage storageattach, at the "--discard" detail. I didn't see you mentioning anywhere in your description that you mounted your filesystem with the "-o discard" mount flag[1]. Did you mount your filesystem like that? Is your filesystem 'ext4'? A step that was mentioned in the blog that you linked to, but most probably you skipped because you don't use LVM.

If the 'trim' command was supported and the filesystem was mounted properly, you'd see the following in the output of "hdparm -I /dev/sda":

Code: Select all

Commands/features:
        Enabled Supported:
           ...
           *    Data Set Management TRIM supported (limit 8 blocks)
 
NOTE: The "8 blocks" part may be different.

One of the sites that I do trust reading however on the webs, is the Arch wiki. These guys are my go-to reference for anything/everything Linux related. So, take a good reading at their Solid State Drive page. They have plenty of details to help you understand the situation and the potential resolution.


[1]: Disclaimer: I haven't tried it, I'm "trimming" my VDIs the old-fashioned way; boot with 'systemrescuecd' then clean them with 'zerofree' and then with 'VBoxManage modifymedium --compact'.
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
howaboutsynergy
Posts: 1
Joined: 2. May 2019, 11:56

Re: Trim does not work on VirtualBox 5.2.8

Post by howaboutsynergy »

I have just stumbled upon this myself and the reason I didn't see TRIM in hdparm(just like OP), ie. like this:

Code: Select all

	   *	Data Set Management TRIM supported (limit unknown)
, is because discard="true" was not present in my VM's *.vbox file for that Virtualbox Harddisk. And thus fstrim / will report trim is not supported:

Code: Select all

fstrim: /: the discard operation is not supported
The .vbox file got overwritten from memory when VirtualBox UI exited and the overwrite might not have been instant(ie. a few seconds passed after exiting the UI) because I checked right after and discard="true" was there but then minutes later I checked again and it wasn't! Which explained why I wasn't seeing TRIM in hdparm.

**EDIT:** also, there are multiple nonrotational="true" entries for snapshots also, make sure you either add discard="true" to all of them (new snapshots inherit it anyway), or that you don't mistakenly only add it to disks in the <Snapshot> ... </Snapshot> sections.

So, before adding discard="true" make sure you exited all virtualbox processes(just to be sure) not just the UI, wait a few seconds (or make sure they're gone from the processes list) maybe even reboot and don't start any virtualbox processes(the vbox modules are okay though).

As soon as discard=true was in .vbox I can confirm hdparm reporting TRIM and thus fstrim worked! (sure,afterwards, I also had to ensure it's in fstab, lvm.conf, /proc/cmdline for luks, as per arch linux wiki Solid_state_drive#TRIM (can't include urls, yet, forum won't let me) but that's all secondary! hdparm must report it or all else matters not!)
** DO NOT EDIT THIS FILE.
** If you make changes to this file while any VirtualBox related application
** is running, your changes will be overwritten later, without taking effect.
** Use VBoxManage or the VirtualBox Manager GUI to make changes.
I didn't try the VBoxManage variant.

VirtualBox 6.0.6 r129722
PS: --compact didn't work (while hdparm reported no TRIM), but later with TRIM in hdparm and fstrim the vdi disk file was shrunk pretty fast. But I realize now why: even though I did sudo dd if=/dev/zero of=/here; sync; sudo rm /here, since that was inside the luks root, this didn't zero data on the /dev/sda device(which would've made --compact work), but rather it zeroed the decrypted /dev/sda device (seen as /dev/mapper/luks* and mounted as root aka / ) thus all those zeroes inside luks would show up as garbage/randomness when looking at /dev/sda since they were encrypted. Hence why fstrim was the only way to shrink the vdi due to LUKS!
buzard
Posts: 1
Joined: 26. Oct 2020, 19:31

Re: Trim does not work on VirtualBox 5.2.8

Post by buzard »

Hello,

I know its a bit late, but I achieved it working (size of vdi reduced from 55Gb to 29Gb)

host side : (vm powered off) I had actually to dettach an reattach the drive (with --discard option); to be able to activate TRIM feature (otherwise adding discard="true" manually in vbox file didn't do, it was reseted at each restart). using a command line command like (adapt options to your configuration), nonrotational stand for ssd

Code: Select all

> VBoxManage storageattach "vm-name" --storagectl "SATA" --port 0 --device 0 --nonrotational on --discard on --medium "C:\path\to\file.vdi" --type hdd
guest side : I had to enable triming, following Erik Wrammer directions => look for shrink-virtualbox-vdi-files-with-trim (not able to post links), more or less :

Code: Select all

    in /etc/lvm/lvm.conf : devices { ... issue_discards = 1
    in /etc/fstab : adding discard option to revelant mount points

/dev/mapper/centos-root /                       xfs     defaults,discard       0 0
/dev/mapper/centos-home /home                   xfs     defaults,discard        0 0
then after reboot, executing fstrim to cut free blocks :

Code: Select all

# fstrim -v /
/ : 29,7 GiB (31874830336 octets) taillés
# fstrim -v /home
/home : 18,6 GiB (19996790784 octets) taillés
the vdi file reduces immediatly after the fstrim (also my virtual SATA controller is configured to use host IO Cache, dunno if it helps).

there still remains an overhead around 5Gb (with actually 2gb of it beeing swap partition) but this is much more acceptable than an overhead of 25Gb

Code: Select all

# df -Th
Sys. de fichiers        Type     Taille Utilisé Dispo Uti% Monté sur
/dev/mapper/centos-root xfs         48G     19G   30G  38% /
/dev/mapper/centos-home xfs         24G    4,8G   19G  21% /home
final result : 28 771 328 ko (initially more than 55)


also may be usefull : I recently discovered that it is possible to declare immutable disk in virtualbox media manager, ideal for swap partition, but unusable in this vm because it was declared as first or second partition should it be declared last that i would i have moved it already :


with this you can avoid creating a permanent swap partition or file (depending on how much ram you allocate), thus saving some more space on the principal vdi. the drives is only some Mb footprint , and the snapshots for each vm using it are reverted to the immutable state on restart. (still need some space on heavy load, even if i don't see it grown ever)

the process for creating this immutable swap partition is a bit troublesome, but needed once for creation :
- need to create the drive normal type
- attach it to the vm
- partition the new drive with swap partition type : fdisk /dev/sdX (with X corrresponding to the new drive lsblk should help if not sure)
- format it for swap : mkswap /dev/sdX1
- add it to /etc/fstab (/dev/sdX1 none swap defaults 0 0)
- do not swapon it immediately (need to create empty immutable version)
- turn off vm, dettach the drive
- modify its type to immutable in media manager in virtualbox interface
- reattach it and voilà
- I'm not sure if swapon is required on reboot since it is in fstab
- after checking that new partition is swapped on with : swapon -s

once the master is made, it is possible to attach same swap.vdi to other vm, there is no need to restrict it to 1gb, like I did, so you can use it directly for vm for which you provision greater RAM size :
- attach the swap.vdi to the vm (it will automatically create a new snapshot over the immutable version)
- use lsblk to discover device naming
- add it to fstab and swapon

hope it helps
Post Reply