TRIM in Qubes OS

Hello!

I have a SSD and followed the disk-trim documentation. Everything seems work fine. I just have a question about TRIM in Qubes OS. If i followed the documents. does it mean that if i delete something in any qube (AppVM, Template, etc) the TRIM service will handle it? Or i should make make the trim service in the qubes itself? In other words: if i’d like to run a manual TRIM command with fstrim -av is it enough to run in dom0, or i need to run it in the AppVM itself?

Thanks any help!

3 Likes

I also have this question if anyone else has answer?

1 Like

Run it in the VM.

2 Likes

In all versions of Qubes, you may want to set up a periodic job in dom0 to trim the disk. This can be done with either systemd (weekly only) or cron (daily or weekly).

I really need help understanding this. I followed the instructions to enable via “cron” in dom0 a daily periodic job to trim the disk.

I don’t see anything about having to run that command in each VM?

1 Like

This is an omission. Those docs are a community effort - you could help
by changing them.

fstrim is often used to allow a VM to tell the host what part of the
storage is no longer needed. This allows the host to actually regain
free space when guest machines do.
This applies across many virtualisation methods including Xen.

I never presume to speak for the Qubes team.
When I comment in the Forum or in the mailing lists I speak for myself.
3 Likes

I be really happy to update the documentation. Is the following edit sufficient:

## Configuration

In all versions of Qubes, you may want to set up a periodic job in dom0 to trim the disk. This can be done with either systemd (weekly only) or cron (daily or weekly). You should also add this service to each VM you want the trim function to work in.

the rest...(left as is)
  • Systemd From a terminal as a regular user:
systemctl enable fstrim.timer
systemctl start fstrim.timer
  • Cron This can be done from a terminal as root, by creating a trim file in /etc/cron.daily (or /etc/cron.weekly ). Add the following contents:
#!/bin/bash
/sbin/fstrim --all

And mark it as executable with chmod 755 /etc/cron.daily/trim .

Note Although discards can be issued on every delete inside dom0 by adding the discard mount option to /etc/fstab , this option can hurt performance so the above procedure is recommended instead. However, inside App and Template qubes, the discard mount option is on by default to notify the LVM thin pool driver that the space is no longer needed and can be zeroed and re-used.

If you are using Qubes with LVM, you may also want to set issue_discards = 1 in /etc/lvm/lvm.conf . Setting this option will permit LVM to issue discards to the SSD when logical volumes are shrunk or deleted. In R4.x, LVM Logical volumes are frequently deleted (every time a disposable VM is shut down, for example) so you may want to set issue_discards = 1 if using an SSD, but see the article linked in the first section of this page. However, this is relatively rare in R3.x.

1 Like

Hi @Dum0 ,

yes it is sufficient, I did the pull request PR#156.

Next time, you could do the pull request by yourself, see the how-to-edit-the-documentation guide.

2 Likes

Pull request merged, now the disk-trim documentation contains the new sentence, thanks @Dum0 for your submission.

4 Likes

From a dom0 context:

Note that issue_discards only applies to regular LVs in LVM, and not to LVs found in a thin pool. Default storage configuration for Qubes (through at least R4.1) is LVM thin pool.

So, discards are not issued automatically when thin LVs are shrunk or removed, even if the above flag is set to 1.

[However, I still enabled that option on my system specifically to force discards for when thin pools are removed (since they are treated as an LV in the VG from an allocation perspective). I do create and remove thin pools from time to time for projects. But most users do not need this.]

Due to the inability to configure lvm itself to issue discards downstream implicitly when removing an LV in a thin pool, Qubes now issues a formal blkdiscard call before LV removal (added in a point release of R4.0). This is helpful for both the health/performance of the underlying SSD, but may also provide some opportunistic anti-forensics (e.g. for disposable VMs).

Fast forward to present day: under R4.1 w/ standard LVM configuration, when a volume is removed by qubes, discards are automatically passed down through the stack to the hardware if it supports them.

B

2 Likes

Qubes default fedora-34 TemplateVM does not have a /etc/cron.daily/ directory so these instructions did not work for me. I googled around and according to official documentation this folder should exist by default. It is possible I screwed up on my TemplateVM and need to download a fresh copy.

FYI - encountered no problem following these instructions on Debian based templates

didnt know Qubes didnt automatically have Trim setup, thanks for bringing it to my attention

1 Like

I wonder, if my laptop isn’t always on, how does daily cron jobs work? One quick article search online says that daily cron jobs execute once per day at 3:05 AM. However, my laptop isn’t running at that hour. So, when does the daily cron job for trim get executed?

I am asking this because, whenever I execute:
$ sudo fstrim -av
in dom0, the terminal replies by saying it just trimmed 15.3 GiB on / (which is /dev/mapper/qubes_dom0-root).

You can use anacron.

How do I use it? Is there a QubesOS specific guide/helper?

This link may help to get cron working on a per VM basis: Newb Cron Question - #37 by JustAnotherQubesUser

1 Like

It isnt anything specific to Qubes - it’s a standard package that you
can install.
The only Qubes specific part would be that you install it in dom0 and in
any templates where you think it may be needed.

I see anacron package is already installed in dom0.

Then, I took a look at how to use it online, and this guide says, the anacron cannot run bash scripts.

I also see that anacron seems to track the usual /etc/cron.daily , /etc/cron.weekly , and /etc/cron.monthly folders, similar to normal cron. Am I correct in thinking that simply placing sh scripts under these folders will make anacron to execute them? For example, changing the trim script into

#!/bin/sh
/sbin/fstrim -av

Would this enable anacron to daily execute this script command whether the laptop is running during the 3:05AM or not?