Jump to content

ilarion question about vdisk trim


Recommended Posts

On 4/29/2017 at 2:03 PM, JorgeB said:

How do I keep my sparse vdisk as small as possible?

How do enable trim on my Windows 8/10 or Windows Server 2012/2016 VM?

 

NOTE: according to this post by @aim60virtio devices also support discard on recent versions of qemu, so you just need to add the discard='unmap' option to the XML.

 

Still going to leave the older info here for now just in case.

 

 

 

By default vdisks are sparse, i.e., you can chose 30GB capacity but it will only allocate the actual required space and use more as required, you can see the current capacity vs allocated size by clicking on the VM name, problem is that over time as files are written and deleted, updates installed, etc, the vdisk grows and it doesn't recover from the deleted files.

 

This has two consequences, space is wasted and if the vdisk in on an SSD that unused space is not trimmed, it's possible to "re-sparsify" the vdisk e.g., by cping it to another file, but this it's not very practical and there's a better way.

 

You can use the vitio-scsi controller together with discard='unmap', this allows Windows 8/10 to detect the vdisk as "thin provisioned drive", and any files deleted on the vdisk are immediately recovered as free space on the host (might not work if the vdisk is on a HDD), and this also allows fstrim to then trim those now free sectors when the vdisk is on an SSD.

 

On an existing vdisk it's also possible to run Windows defrag to recover all unused space after changing to that controller.

 

Steps to change an existing Windows8/10 VM (also works for Windows Server 2012/2016):

 

1) First we need to install the SCSI controller, shutdown the VM (For Windows 8/10 I recommend disabling Windows fast Startup -> Control Panel\All Control Panel Items\Power Options\System Settings before shutdown, or else the VM might crash on first boot after changing the controller).

 

Then edit the VM in form mode (toggle between form and XML views is on the upper right side), and change an existing device other than your main vdisk or virtio driver cdrom to SCSI, for example your OS installation device if you still have it, if not you can also add a second small vdisk and chose SCSI as the vdisk bus, save changes

 

2) Start the VM and install the driver for the new "SCSI controller", look for it on the virtio driver ISO (e.g., vioscsi\w10)

 

3) Shutdown the VM, edit the VM again, again using the form view and change the main vdisk controller to "SCSI", now change view to XML and add "discard='unmap'" to the SCSI controller:

 

Add after cache='writeback', e.g. before:

 

 

After:

 

 

4) Start the VM (if you added a 2nd vdisk you can remove it now before starting), it's should boot normally, you can re-enable Windows fast startup.

 

5) Run Windows Defrag and Optimize drives, check that he disk is now detected as "Thin provisioned drive" and run optimize to recover all previous unused space.

 

From now on all deleted files on the vdisk should be immediately trimmed.

 

Note: If after this you edit the VM using the GUI editor these changes will be lost and will need to be redone.

 

 

It is all working for me with the exception of that the host dosen`t reclaim actual space. When i use "ls" the qcaw2 file is still the size before the optimization in the guest and trim on the host. Only "du" shows  the trimmed size, but that is not usable space on host. So how to reclaim any tangible space on the host? Do i have to use LVM or some kind of esoteric fs as btrfs or zfs so that to work ?

space.jpg

Link to comment
3 hours ago, JorgeB said:

@ilarionsplit you post here, as mentioned there FAQ thread is not for questions.

 

It works with btrfs or xfs, which filesystem are you using?

Sorry, i use XFS. So what is actually has to happen? Doesn't it have to make allocation size same with actual(logical) so that i can use this space for other files?

Link to comment
On 8/8/2022 at 7:54 PM, JorgeB said:

It should discard and free any unused space when you run trim in the VM OS.

I think it is, it is just the diference between allocated space and block space. In main page in used space shows block space and in file manager shows block space. In smb share from windows explorer shows block space too. And on top of that there is the difference between GiB and GB that confuse me more at the start of the investigation because a forgot about that thing. But in the end i don`t know if this space is usable. So what i have to believe "du" or "ls" ?

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...