Trim does not work on VirtualBox 5.2.8

Discussions about using Linux guests in VirtualBox.

Trim does not work on VirtualBox 5.2.8

Postby Löscha » 6. May 2018, 03:53

I have this in the .VBOX file:

Code: Select all   Expand viewCollapse view
<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   Expand viewCollapse view
fstrim -v /

I'll get this error:
Code: Select all   Expand viewCollapse view
fstrim: /: the discard operation is not supported

The harddisk is physically a ssd drive

ssd-drive.png
ssd-drive.png (45.42 KiB) Viewed 1912 times

Code: Select all   Expand viewCollapse view
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".
Löscha
 
Posts: 3
Joined: 10. Apr 2018, 11:57

Re: Trim does not work on VirtualBox 5.2.8

Postby socratis » 6. May 2018, 07:26

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.
socratis
Site Moderator
 
Posts: 26882
Joined: 22. Oct 2010, 11:03
Location: Greece
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5

Re: Trim does not work on VirtualBox 5.2.8

Postby Löscha » 6. May 2018, 15:50

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   Expand viewCollapse view
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/10/17/shrink-virtualbox-vdi-files-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+discard+nonrotational+virtualbox but i didn't get it running.

Why?

Thank you!
Löscha
 
Posts: 3
Joined: 10. Apr 2018, 11:57

Re: Trim does not work on VirtualBox 5.2.8

Postby socratis » 6. May 2018, 17:34

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   Expand viewCollapse view
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.
socratis
Site Moderator
 
Posts: 26882
Joined: 22. Oct 2010, 11:03
Location: Greece
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5

Re: Trim does not work on VirtualBox 5.2.8

Postby howaboutsynergy » 2. May 2019, 12:37

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   Expand viewCollapse view
      *   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   Expand viewCollapse view
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!
howaboutsynergy
 
Posts: 1
Joined: 2. May 2019, 11:56


Return to Linux Guests

Who is online

Users browsing this forum: No registered users and 12 guests