[pbs-devel] [PATCH proxmox-backup 07/26] api2: datastore create: don't init chunkstore if removable

Hannes Laimer h.laimer at proxmox.com
Tue Jul 5 15:08:15 CEST 2022


.. instead set the maintenance mode to unplugged. So on creation every
removable datastore is unplugged until a removable device is associated
with it.

Signed-off-by: Hannes Laimer <h.laimer at proxmox.com>
---
 src/api2/config/datastore.rs | 52 +++++++++++++++++++++++++++---------
 1 file changed, 39 insertions(+), 13 deletions(-)

diff --git a/src/api2/config/datastore.rs b/src/api2/config/datastore.rs
index 2d769722..24d7a37a 100644
--- a/src/api2/config/datastore.rs
+++ b/src/api2/config/datastore.rs
@@ -1,19 +1,20 @@
 use std::path::PathBuf;
 
 use ::serde::{Deserialize, Serialize};
-use anyhow::Error;
+use anyhow::{bail, Error};
 use hex::FromHex;
 use serde_json::Value;
 
 use proxmox_router::{http_bail, Permission, Router, RpcEnvironment, RpcEnvironmentType};
 use proxmox_schema::{api, param_bail, ApiType};
 use proxmox_section_config::SectionConfigData;
+use proxmox_sys::fs::CreateOptions;
 use proxmox_sys::WorkerTaskContext;
 
 use pbs_api_types::{
-    Authid, DataStoreConfig, DataStoreConfigUpdater, DatastoreNotify, DATASTORE_SCHEMA,
-    PRIV_DATASTORE_ALLOCATE, PRIV_DATASTORE_AUDIT, PRIV_DATASTORE_MODIFY,
-    PROXMOX_CONFIG_DIGEST_SCHEMA,
+    Authid, DataStoreConfig, DataStoreConfigUpdater, DatastoreNotify, MaintenanceMode,
+    MaintenanceType, DATASTORE_SCHEMA, PRIV_DATASTORE_ALLOCATE, PRIV_DATASTORE_AUDIT,
+    PRIV_DATASTORE_MODIFY, PROXMOX_CONFIG_DIGEST_SCHEMA,
 };
 use pbs_config::BackupLockGuard;
 use pbs_datastore::chunk_store::ChunkStore;
@@ -65,19 +66,37 @@ pub fn list_datastores(
 pub(crate) fn do_create_datastore(
     _lock: BackupLockGuard,
     mut config: SectionConfigData,
-    datastore: DataStoreConfig,
+    mut datastore: DataStoreConfig,
     worker: Option<&dyn WorkerTaskContext>,
 ) -> Result<(), Error> {
     let path: PathBuf = datastore.path.clone().into();
-
     let backup_user = pbs_config::backup_user()?;
-    let _store = ChunkStore::create(
-        &datastore.name,
-        path,
-        backup_user.uid,
-        backup_user.gid,
-        worker,
-    )?;
+    let _store;
+
+    if !datastore.removable {
+        _store = ChunkStore::create(
+            &datastore.name,
+            path,
+            backup_user.uid,
+            backup_user.gid,
+            worker,
+        )?;
+    } else {
+        let options = CreateOptions::new()
+            .owner(backup_user.uid)
+            .group(backup_user.gid);
+        let default_options = CreateOptions::new();
+        match proxmox_sys::fs::create_path(&datastore.path, Some(default_options), Some(options)) {
+            Err(err) => bail!("unable to create directory '{}' - {}", &datastore.path, err),
+            Ok(res) => {
+                if !res {
+                    nix::unistd::chown(&path, Some(backup_user.uid), Some(backup_user.gid))?
+                }
+            }
+        }
+
+        datastore.set_maintenance_mode(MaintenanceMode::new(MaintenanceType::Unplugged, None));
+    }
 
     config.set_data(&datastore.name, "datastore", &datastore)?;
 
@@ -351,6 +370,13 @@ pub fn update_datastore(
     }
 
     if update.maintenance_mode.is_some() {
+        if let Some(maintenance) = data.get_maintenance_mode() {
+            if data.removable && maintenance.ty == MaintenanceType::Unplugged {
+                bail!(
+                    "can't change the maintenance mode as long as no removable device is present"
+                );
+            }
+        }
         data.maintenance_mode = update.maintenance_mode;
     }
 
-- 
2.30.2






More information about the pbs-devel mailing list