From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pbs-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9])
	by lore.proxmox.com (Postfix) with ESMTPS id 31B9E1FF15C
	for <inbox@lore.proxmox.com>; Wed, 13 Nov 2024 16:01:20 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id DCBF9186ED;
	Wed, 13 Nov 2024 16:01:16 +0100 (CET)
From: Hannes Laimer <h.laimer@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Wed, 13 Nov 2024 16:00:44 +0100
Message-Id: <20241113150102.164820-9-h.laimer@proxmox.com>
X-Mailer: git-send-email 2.39.5
In-Reply-To: <20241113150102.164820-1-h.laimer@proxmox.com>
References: <20241113150102.164820-1-h.laimer@proxmox.com>
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.021 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pbs-devel] [PATCH proxmox-backup v13 08/26] api: removable
 datastore creation
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox Backup Server development discussion
 <pbs-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pbs-devel-bounces@lists.proxmox.com
Sender: "pbs-devel" <pbs-devel-bounces@lists.proxmox.com>

Devices can contains multiple datastores, the only limitations is that
they are not allowed to be nested.
If the specified path already contains a datastore, `reuse datastore` has
to be set so it'll be added without creating a chunckstore.

Signed-off-by: Hannes Laimer <h.laimer@proxmox.com>
---
changes since v12:
 * use recently added 'reuse datastore'
 * allow creation even if device is already used by datastore, just no
    nesting

 src/api2/config/datastore.rs | 50 +++++++++++++++++++++++++++++++-----
 1 file changed, 44 insertions(+), 6 deletions(-)

diff --git a/src/api2/config/datastore.rs b/src/api2/config/datastore.rs
index 374c302f..9140a7a4 100644
--- a/src/api2/config/datastore.rs
+++ b/src/api2/config/datastore.rs
@@ -20,7 +20,8 @@ use pbs_config::BackupLockGuard;
 use pbs_datastore::chunk_store::ChunkStore;
 
 use crate::api2::admin::{
-    prune::list_prune_jobs, sync::list_sync_jobs, verify::list_verification_jobs,
+    datastore::do_mount_device, prune::list_prune_jobs, sync::list_sync_jobs,
+    verify::list_verification_jobs,
 };
 use crate::api2::config::prune::{delete_prune_job, do_create_prune_job};
 use crate::api2::config::sync::delete_sync_job;
@@ -31,6 +32,7 @@ use pbs_config::CachedUserInfo;
 use proxmox_rest_server::WorkerTask;
 
 use crate::server::jobstate;
+use crate::tools::disks::unmount_by_mountpoint;
 
 #[api(
     input: {
@@ -72,7 +74,11 @@ pub(crate) fn do_create_datastore(
     datastore: DataStoreConfig,
     reuse_datastore: bool,
 ) -> Result<(), Error> {
-    let path: PathBuf = datastore.path.clone().into();
+    let path: PathBuf = datastore.absolute_path().into();
+    let need_unmount = datastore.get_mount_point().is_some() && {
+        do_mount_device(datastore.clone())?;
+        true
+    };
 
     if path.parent().is_none() {
         bail!("cannot create datastore in root path");
@@ -84,24 +90,32 @@ pub(crate) fn do_create_datastore(
     )?;
 
     if reuse_datastore {
-        ChunkStore::verify_chunkstore(&path)?;
+        if let Err(e) = ChunkStore::verify_chunkstore(&path) {
+            let _ = need_unmount && unmount_by_mountpoint(&path).is_ok();
+            return Err(e);
+        }
     } else {
         if let Ok(dir) = std::fs::read_dir(&path) {
             for file in dir {
                 let name = file?.file_name();
                 if !name.to_str().map_or(false, |name| name.starts_with('.')) {
+                    let _ = need_unmount && unmount_by_mountpoint(&path).is_ok();
                     bail!("datastore path is not empty");
                 }
             }
         }
         let backup_user = pbs_config::backup_user()?;
-        let _store = ChunkStore::create(
+        let res = ChunkStore::create(
             &datastore.name,
-            path,
+            path.clone(),
             backup_user.uid,
             backup_user.gid,
             tuning.sync_level.unwrap_or_default(),
-        )?;
+        );
+        if let Err(e) = res {
+            let _ = need_unmount && unmount_by_mountpoint(&path).is_ok();
+            return Err(e);
+        }
     }
 
     config.set_data(&datastore.name, "datastore", &datastore)?;
@@ -145,6 +159,30 @@ pub fn create_datastore(
         param_bail!("name", "datastore '{}' already exists.", config.name);
     }
 
+    if !config.path.starts_with("/") {
+        param_bail!("path", "expected an abolute path, '{}' is not", config.path);
+    }
+
+    if let Some(uuid) = &config.backing_device {
+        for (store_name, (_, store_config)) in &section_config.sections {
+            if let (Some(store_uuid), Some(store_path)) = (
+                store_config["backing-device"].as_str(),
+                store_config["path"].as_str(),
+            ) {
+                // We don't allow two datastores to be nested in each other, so if
+                //  ds1: /a/b -> can't create new one at /, /a or /a/b/..., /a/c is fine
+                if store_uuid == uuid
+                    && (store_path.starts_with(&config.path) || config.path.starts_with(store_path))
+                {
+                    param_bail!(
+                        "path",
+                        "can't nest datastores, '{store_name}' already in '{store_path}'",
+                    );
+                }
+            };
+        }
+    }
+
     let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
     let to_stdout = rpcenv.env_type() == RpcEnvironmentType::CLI;
 
-- 
2.39.5



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel