api2/tape/drive: add optional label-text to erase

if given, erases the tape only iff the inserted tape contains that label
used to safeguard tape erasing from ui for standalone drives

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2021-02-16 09:35:24 +01:00 committed by Dietmar Maurer
parent dbaef7d04d
commit be61c56c21
1 changed files with 22 additions and 2 deletions

View File

@ -202,19 +202,23 @@ pub async fn unload(
optional: true, optional: true,
default: true, default: true,
}, },
"label-text": {
schema: MEDIA_LABEL_SCHEMA,
optional: true,
},
}, },
}, },
returns: { returns: {
schema: UPID_SCHEMA, schema: UPID_SCHEMA,
}, },
)] )]
/// Erase media /// Erase media. Check for label-text if given (cancels if wrong media).
pub fn erase_media( pub fn erase_media(
drive: String, drive: String,
fast: Option<bool>, fast: Option<bool>,
label_text: Option<String>,
rpcenv: &mut dyn RpcEnvironment, rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> { ) -> Result<Value, Error> {
let (config, _digest) = config::drive::config()?; let (config, _digest) = config::drive::config()?;
// early check/lock before starting worker // early check/lock before starting worker
@ -236,16 +240,32 @@ pub fn erase_media(
match drive.read_label() { match drive.read_label() {
Err(err) => { Err(err) => {
if let Some(label) = label_text {
bail!("expected label '{}', found unrelated data", label);
}
/* assume drive contains no or unrelated data */ /* assume drive contains no or unrelated data */
task_log!(worker, "unable to read media label: {}", err); task_log!(worker, "unable to read media label: {}", err);
task_log!(worker, "erase anyways"); task_log!(worker, "erase anyways");
drive.erase_media(fast.unwrap_or(true))?; drive.erase_media(fast.unwrap_or(true))?;
} }
Ok((None, _)) => { Ok((None, _)) => {
if let Some(label) = label_text {
bail!("expected label '{}', found empty tape", label);
}
task_log!(worker, "found empty media - erase anyways"); task_log!(worker, "found empty media - erase anyways");
drive.erase_media(fast.unwrap_or(true))?; drive.erase_media(fast.unwrap_or(true))?;
} }
Ok((Some(media_id), _key_config)) => { Ok((Some(media_id), _key_config)) => {
if let Some(label_text) = label_text {
if media_id.label.label_text != label_text {
bail!(
"expected label '{}', found '{}', aborting",
label_text,
media_id.label.label_text
);
}
}
task_log!( task_log!(
worker, worker,
"found media '{}' with uuid '{}'", "found media '{}' with uuid '{}'",