all lists on 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 v4 12/14] pbs-config: add helper to check new datastore config sections
Date: Mon,  3 Aug 2026 11:07:45 +0200	[thread overview]
Message-ID: <20260803090747.265683-13-c.ebner@proxmox.com> (raw)
In-Reply-To: <20260803090747.265683-1-c.ebner@proxmox.com>

Factor out the datastore config check so this can be done independent
from the datastore creation, which is going to happen without holding
the datastore config lock but rather within the maintenance type
create.

While moving the code, unify it with other call sites to have all
checks performed in a central location.

The nesting check is now no longer performed within the datastore
creation task but rather early on and for datastore creations
combined with storage from disks as well.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
 pbs-config/src/datastore.rs      | 45 +++++++++++++++++++++++++++++++-
 src/api2/config/datastore.rs     | 40 ++++------------------------
 src/api2/node/disks/directory.rs |  5 +---
 src/api2/node/disks/zfs.rs       |  5 +---
 4 files changed, 51 insertions(+), 44 deletions(-)

diff --git a/pbs-config/src/datastore.rs b/pbs-config/src/datastore.rs
index 2a200b35e..de397302f 100644
--- a/pbs-config/src/datastore.rs
+++ b/pbs-config/src/datastore.rs
@@ -1,10 +1,11 @@
 use std::collections::HashMap;
+use std::path::Path;
 use std::sync::LazyLock;
 
 use anyhow::Error;
 
 use proxmox_product_config::replace_privileged_config;
-use proxmox_schema::{AllOfSchema, ApiType};
+use proxmox_schema::{AllOfSchema, ApiType, param_bail};
 use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin};
 
 use pbs_api_types::{DATASTORE_SCHEMA, DataStoreConfig, DatastoreBackendConfig, DatastoreTuning};
@@ -132,3 +133,45 @@ pub fn datastore_backend_type(store: &str) -> Result<pbs_api_types::DatastoreBac
 
     Ok(parse_backend_config(&store_config)?.ty.unwrap_or_default())
 }
+
+/// Validate that the provided datastore config can be safely added as additional config section.
+pub fn validate_new_datastore_config(
+    store: &DataStoreConfig,
+    config: &SectionConfigData,
+    _config_lock: &BackupLockGuard,
+) -> Result<(), Error> {
+    if config.sections.contains_key(&store.name) {
+        param_bail!("name", "datastore '{}' already exists.", store.name);
+    }
+
+    let path = Path::new(&store.path);
+    if store.backing_device.is_none() {
+        if path.is_relative() {
+            param_bail!("path", "expected an absolute path, '{}' is not", store.path);
+        }
+        if path.parent().is_none() {
+            param_bail!("path", "cannot create datastore in root path",);
+        }
+        if store.gc_on_unmount.unwrap_or(false) {
+            param_bail!(
+                "gc-on-unmount",
+                "GC on unmount is only supported on removable datastores",
+            );
+        }
+    } else {
+        if path.is_absolute() {
+            param_bail!(
+                "path",
+                "expected a relative on-device path, '{}' is not",
+                store.path
+            );
+        }
+    }
+
+    let existing_stores = config.convert_to_typed_array("datastore")?;
+    if let Err(err) = store.ensure_not_nested(&existing_stores) {
+        param_bail!("path", err);
+    }
+
+    Ok(())
+}
diff --git a/src/api2/config/datastore.rs b/src/api2/config/datastore.rs
index 74118de92..7505e8c78 100644
--- a/src/api2/config/datastore.rs
+++ b/src/api2/config/datastore.rs
@@ -1,7 +1,7 @@
 use std::path::{Path, PathBuf};
 
 use ::serde::{Deserialize, Serialize};
-use anyhow::{Context, Error, bail, format_err};
+use anyhow::{Context, Error, format_err};
 use serde_json::Value;
 use tracing::warn;
 
@@ -93,26 +93,17 @@ impl Drop for UnmountGuard {
 }
 
 pub(crate) fn do_create_datastore(
-    _lock: BackupLockGuard,
+    lock: BackupLockGuard,
     mut config: SectionConfigData,
     mut datastore: DataStoreConfig,
     reuse_datastore: bool,
     overwrite_in_use: bool,
 ) -> Result<(), Error> {
-    let path: PathBuf = datastore.absolute_path().into();
-
-    if path.parent().is_none() && datastore.backing_device.is_none() {
-        bail!("cannot create datastore in root path");
-    }
-
-    let existing_stores = config.convert_to_typed_array("datastore")?;
-    if let Err(err) = datastore.ensure_not_nested(&existing_stores) {
-        param_bail!("path", err);
-    }
+    pbs_config::datastore::validate_new_datastore_config(&datastore, &config, &lock)?;
 
     let unmount_guard = if datastore.backing_device.is_some() {
         do_mount_device(datastore.clone())?;
-        UnmountGuard::new(Some(path.clone()))
+        UnmountGuard::new(Some(datastore.absolute_path().into()))
     } else {
         UnmountGuard::new(None)
     };
@@ -182,28 +173,7 @@ pub fn create_datastore(
     let lock = pbs_config::datastore::lock_config()?;
 
     let (section_config, _digest) = pbs_config::datastore::config()?;
-
-    if section_config.sections.contains_key(&config.name) {
-        param_bail!("name", "datastore '{}' already exists.", config.name);
-    }
-
-    if config.backing_device.is_none() && !config.path.starts_with("/") {
-        param_bail!(
-            "path",
-            "expected an absolute path, '{}' is not",
-            config.path
-        );
-    }
-    if config.backing_device.is_some() && config.path.starts_with("/") {
-        param_bail!(
-            "path",
-            "expected a relative on-device path, '{}' is not",
-            config.path
-        );
-    }
-    if config.backing_device.is_none() && config.gc_on_unmount.unwrap_or(false) {
-        param_bail!("gc-on-unmount", "only supported on removable datastores",);
-    }
+    pbs_config::datastore::validate_new_datastore_config(&config, &section_config, &lock)?;
 
     let mut prune_job_config = None;
     if config.keep.keeps_something() || !has_prune_job(&config.name)? {
diff --git a/src/api2/node/disks/directory.rs b/src/api2/node/disks/directory.rs
index 573e11744..9979355e5 100644
--- a/src/api2/node/disks/directory.rs
+++ b/src/api2/node/disks/directory.rs
@@ -248,10 +248,7 @@ pub fn create_datastore_disk(
                     serde_json::from_value(json!({ "name": name, "path": mount_point }))?
                 };
                 let (config, _digest) = pbs_config::datastore::config()?;
-
-                if config.sections.contains_key(&datastore.name) {
-                    bail!("datastore '{}' already exists.", datastore.name);
-                }
+                pbs_config::datastore::validate_new_datastore_config(&datastore, &config, &lock)?;
 
                 crate::api2::config::datastore::do_create_datastore(
                     lock, config, datastore, false, false,
diff --git a/src/api2/node/disks/zfs.rs b/src/api2/node/disks/zfs.rs
index 7a62e3f70..0e8ce75b5 100644
--- a/src/api2/node/disks/zfs.rs
+++ b/src/api2/node/disks/zfs.rs
@@ -308,10 +308,7 @@ pub fn create_zpool(
                     serde_json::from_value(json!({ "name": name, "path": mount_point }))?;
 
                 let (config, _digest) = pbs_config::datastore::config()?;
-
-                if config.sections.contains_key(&datastore.name) {
-                    bail!("datastore '{}' already exists.", datastore.name);
-                }
+                pbs_config::datastore::validate_new_datastore_config(&datastore, &config, &lock)?;
 
                 crate::api2::config::datastore::do_create_datastore(
                     lock, config, datastore, false, false,
-- 
2.47.3





  parent reply	other threads:[~2026-08-03  9:08 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-03  9:07 [PATCH proxmox{,-backup} v4 00/14] keep datastore config unlocked during long running operations Christian Ebner
2026-08-03  9:07 ` [PATCH proxmox v4 01/14] pbs-api-types: add datastore create maintenance-mode type Christian Ebner
2026-08-03  9:07 ` [PATCH proxmox-backup v4 02/14] api: config: early perform user access checks for datastore creation Christian Ebner
2026-08-03  9:07 ` [PATCH proxmox-backup v4 03/14] api: config: unlocked s3 bucket access check " Christian Ebner
2026-08-03  9:07 ` [PATCH proxmox-backup v4 04/14] api: config: rearrange independent code block " Christian Ebner
2026-08-03  9:07 ` [PATCH proxmox-backup v4 05/14] api/datastore: refactor datastore creation helper logic Christian Ebner
2026-08-03  9:07 ` [PATCH proxmox-backup v4 06/14] datastore: restrict chunk store scope to pbs-datastore crate Christian Ebner
2026-08-03  9:07 ` [PATCH proxmox-backup v4 07/14] datastore: move lock files base path constant to central location Christian Ebner
2026-08-03  9:07 ` [PATCH proxmox-backup v4 08/14] datastore: move file lock helper to centralized place Christian Ebner
2026-08-03  9:07 ` [PATCH proxmox-backup v4 09/14] datastore: add additional check for device number on lock helper Christian Ebner
2026-08-03  9:07 ` [PATCH proxmox-backup v4 10/14] datastore: create lockdir with correct mode for backup user access Christian Ebner
2026-08-03  9:07 ` [PATCH proxmox-backup v4 11/14] api/datastore: use maintenance-mode lock to protect against changes Christian Ebner
2026-08-03  9:07 ` Christian Ebner [this message]
2026-08-03  9:07 ` [PATCH proxmox-backup v4 13/14] api: datastore: move prune job and s3 backend logic to create helper Christian Ebner
2026-08-03  9:07 ` [PATCH proxmox-backup v4 14/14] datastore: protect datastore creation by maintenance-mode 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=20260803090747.265683-13-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 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