all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Hannes Laimer <h.laimer@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [PATCH proxmox-backup v2 2/3] api: datastore: add option to run garbage collection before unmount
Date: Mon, 20 Apr 2026 09:42:56 +0200	[thread overview]
Message-ID: <20260420074257.9492-3-h.laimer@proxmox.com> (raw)
In-Reply-To: <20260420074257.9492-1-h.laimer@proxmox.com>

Removable datastores set up for auto-unmount have no natural point at
which to run garbage collection, since the drive is unmounted right
after jobs finish. Expose a gc-on-unmount option so GC can be triggered
as part of the unmount for those setups.

Relies on the active write operation by garbage collection to block
the unmount task until GC completes (among possible other active
operations). No other active operation can occur in the meantime
since the datastore remains in 'unmount' maintenance mode.

Signed-off-by: Hannes Laimer <h.laimer@proxmox.com>
---
 src/api2/admin/datastore.rs  | 23 ++++++++++++++++++++---
 src/api2/config/datastore.rs |  9 +++++++++
 2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs
index 757b3114..212a48da 100644
--- a/src/api2/admin/datastore.rs
+++ b/src/api2/admin/datastore.rs
@@ -2641,9 +2641,8 @@ fn do_unmount_device(
 }
 
 async fn do_unmount(store: String, auth_id: Authid, to_stdout: bool) -> Result<Value, Error> {
-    let _lock = pbs_config::datastore::lock_config()?;
-    let (mut section_config, _digest) = pbs_config::datastore::config()?;
-    let mut datastore: DataStoreConfig = section_config.lookup("datastore", &store)?;
+    let (section_config, _digest) = pbs_config::datastore::config()?;
+    let datastore: DataStoreConfig = section_config.lookup("datastore", &store)?;
 
     if datastore.backing_device.is_none() {
         bail!("datastore '{store}' is not removable");
@@ -2651,6 +2650,24 @@ async fn do_unmount(store: String, auth_id: Authid, to_stdout: bool) -> Result<V
 
     ensure_datastore_is_mounted(&datastore)?;
 
+    // Setting gc-on-unmount requires Datastore.Modify (or Datastore.Allocate at creation), the
+    // same level needed to start GC directly, so no privilege escalation from triggering it here.
+    if datastore.gc_on_unmount.unwrap_or(false) {
+        let client = crate::client_helpers::connect_to_localhost()
+            .context("failed to connect to localhost for starting GC")?;
+        match client
+            .post(&format!("api2/json/admin/datastore/{store}/gc"), None)
+            .await
+        {
+            Ok(_) => info!("started garbage collection, unmount will wait for it to finish"),
+            Err(err) => warn!("unable to start garbage collection before unmount: {err}"),
+        }
+    }
+
+    let _lock = pbs_config::datastore::lock_config()?;
+    let (mut section_config, _digest) = pbs_config::datastore::config()?;
+    let mut datastore: DataStoreConfig = section_config.lookup("datastore", &store)?;
+
     datastore.set_maintenance_mode(Some(MaintenanceMode {
         ty: MaintenanceType::Unmount,
         message: None,
diff --git a/src/api2/config/datastore.rs b/src/api2/config/datastore.rs
index c44d50d1..c0be0296 100644
--- a/src/api2/config/datastore.rs
+++ b/src/api2/config/datastore.rs
@@ -409,6 +409,8 @@ pub enum DeletableProperty {
     Comment,
     /// Delete the garbage collection schedule.
     GcSchedule,
+    /// Delete the gc-on-unmount property.
+    GcOnUnmount,
     /// Delete the prune job schedule.
     PruneSchedule,
     /// Delete the keep-last property
@@ -495,6 +497,9 @@ pub fn update_datastore(
                 DeletableProperty::GcSchedule => {
                     data.gc_schedule = None;
                 }
+                DeletableProperty::GcOnUnmount => {
+                    data.gc_on_unmount = None;
+                }
                 DeletableProperty::PruneSchedule => {
                     data.prune_schedule = None;
                 }
@@ -560,6 +565,10 @@ pub fn update_datastore(
         data.gc_schedule = update.gc_schedule;
     }
 
+    if update.gc_on_unmount.is_some() {
+        data.gc_on_unmount = update.gc_on_unmount;
+    }
+
     macro_rules! prune_disabled {
         ($(($param:literal, $($member:tt)+)),+) => {
             $(
-- 
2.47.3





  parent reply	other threads:[~2026-04-20  7:43 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-20  7:42 [PATCH proxmox{,-backup} v2 0/3] fixes #7465: add gc-on-unmount flag Hannes Laimer
2026-04-20  7:42 ` [PATCH proxmox v2 1/3] pbs-api-types: add gc-on-unmount flag for removable datastores Hannes Laimer
2026-04-21 22:17   ` applied: " Thomas Lamprecht
2026-04-20  7:42 ` Hannes Laimer [this message]
2026-04-20  8:32   ` [PATCH proxmox-backup v2 2/3] api: datastore: add option to run garbage collection before unmount Shannon Sterz
2026-04-20  8:45     ` Hannes Laimer
2026-04-20  8:52       ` Shannon Sterz
2026-04-20  7:42 ` [PATCH proxmox-backup v2 3/3] ui: datastore: expose gc-on-unmount setting Hannes Laimer
2026-04-21 11:34 ` [PATCH proxmox{,-backup} v2 0/3] fixes #7465: add gc-on-unmount flag 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=20260420074257.9492-3-h.laimer@proxmox.com \
    --to=h.laimer@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal