public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Christian Ebner <c.ebner@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [PATCH proxmox-backup 3/5] bin: proxy: periodically schedule fstrim on datastore's filesystems
Date: Thu, 19 Mar 2026 15:36:47 +0100	[thread overview]
Message-ID: <20260319143649.681937-5-c.ebner@proxmox.com> (raw)
In-Reply-To: <20260319143649.681937-1-c.ebner@proxmox.com>

Run the fstrim command as scheduled job on datastores having a
schedule defined in the config.

This is done since by default the systemd service to execute fstrim
invokes the command with:

`/sbin/fstrim --listed-in /etc/fstab:/proc/self/mountinfo ...`

which however does not cover datastores created on top of a disk,
since these are mounted via systemd mount units. fstrim however only
evaluates the given list above up to the first non-empty file as
stated in the man page [0].

[0] https://www.man7.org/linux/man-pages/man8/fstrim.8.html

Fixes: https://forum.proxmox.com/threads/181764/
Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
 src/bin/proxmox-backup-proxy.rs | 59 +++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)

diff --git a/src/bin/proxmox-backup-proxy.rs b/src/bin/proxmox-backup-proxy.rs
index c1fe3ac15..1d375e69d 100644
--- a/src/bin/proxmox-backup-proxy.rs
+++ b/src/bin/proxmox-backup-proxy.rs
@@ -508,6 +508,7 @@ async fn schedule_tasks() -> Result<(), Error> {
     schedule_datastore_verify_jobs().await;
     schedule_tape_backup_jobs().await;
     schedule_task_log_rotate().await;
+    schedule_fstrim().await;
 
     Ok(())
 }
@@ -879,6 +880,64 @@ async fn schedule_task_log_rotate() {
     }
 }
 
+async fn schedule_fstrim() {
+    let config = match pbs_config::datastore::config() {
+        Err(err) => {
+            eprintln!("unable to read datastore config - {err}");
+            return;
+        }
+        Ok((config, _digest)) => config,
+    };
+
+    for (store, (_, store_config)) in config.sections {
+        let store_config: DataStoreConfig = match serde_json::from_value(store_config) {
+            Ok(c) => c,
+            Err(err) => {
+                eprintln!("datastore config from_value failed - {err}");
+                continue;
+            }
+        };
+
+        let event_schedule = match &store_config.fstrim_schedule {
+            Some(event_schedule) => event_schedule,
+            None => continue,
+        };
+
+        let worker_type = "fstrim";
+        if check_schedule(worker_type, event_schedule, &store) {
+            let mut job = match Job::new(worker_type, &store) {
+                Ok(job) => job,
+                Err(_) => continue, // could not get lock
+            };
+
+            if let Err(err) = WorkerTask::new_thread(
+                worker_type,
+                None,
+                Authid::root_auth_id().to_string(),
+                false,
+                move |worker| {
+                    job.start(&worker.upid().to_string())?;
+                    info!("executing fstrim on filesystem for {store}");
+
+                    let path = store_config.absolute_path();
+                    let result = proxmox_backup::tools::disks::fstrim(Path::new(&path))
+                        .map(|output| log::info!("{output}"));
+
+                    let status = worker.create_state(&result);
+
+                    if let Err(err) = job.finish(status) {
+                        eprintln!("could not finish job state for {worker_type}: {err}");
+                    }
+
+                    result
+                },
+            ) {
+                eprintln!("unable to start fstrim task: {err}");
+            }
+        }
+    }
+}
+
 async fn command_reopen_access_logfiles() -> Result<(), Error> {
     // only care about the most recent daemon instance for each, proxy & api, as other older ones
     // should not respond to new requests anyway, but only finish their current one and then exit.
-- 
2.47.3





  parent reply	other threads:[~2026-03-19 14:37 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-19 14:36 [RFC proxmox{,-backup} 0/6] add scheduled fstrim job for datastore's backing filesystems Christian Ebner
2026-03-19 14:36 ` [PATCH proxmox 1/1] pbs-api-types: define fstrim schedule on datastore config Christian Ebner
2026-03-19 14:36 ` [PATCH proxmox-backup 1/5] tools: add helper to run fstrim command on path or mountpoint Christian Ebner
2026-03-19 14:36 ` [PATCH proxmox-backup 2/5] api: config: expose fstrim schedule for datastores Christian Ebner
2026-03-19 14:36 ` Christian Ebner [this message]
2026-03-19 15:02   ` [PATCH proxmox-backup 3/5] bin: proxy: periodically schedule fstrim on datastore's filesystems Christian Ebner
2026-03-19 14:36 ` [PATCH proxmox-backup 4/5] ui: expose per-datastore fstrim job schedule Christian Ebner
2026-03-19 14:36 ` [PATCH proxmox-backup 5/5] api: set default fstrim schedule on datastore create Christian Ebner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260319143649.681937-5-c.ebner@proxmox.com \
    --to=c.ebner@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal