tape: backup - implement --eject-media option

This commit is contained in:
Dietmar Maurer 2021-01-09 15:17:03 +01:00
parent 5843268c47
commit 42967bf185
3 changed files with 39 additions and 3 deletions

View File

@ -51,6 +51,11 @@ use crate::{
pool: { pool: {
schema: MEDIA_POOL_NAME_SCHEMA, schema: MEDIA_POOL_NAME_SCHEMA,
}, },
"eject-media": {
description: "Eject media upon job completion.",
type: bool,
optional: true,
},
}, },
}, },
returns: { returns: {
@ -61,6 +66,7 @@ use crate::{
pub fn backup( pub fn backup(
store: String, store: String,
pool: String, pool: String,
eject_media: Option<bool>,
rpcenv: &mut dyn RpcEnvironment, rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> { ) -> Result<Value, Error> {
@ -77,20 +83,20 @@ pub fn backup(
let to_stdout = if rpcenv.env_type() == RpcEnvironmentType::CLI { true } else { false }; let to_stdout = if rpcenv.env_type() == RpcEnvironmentType::CLI { true } else { false };
let eject_media = eject_media.unwrap_or(false);
let upid_str = WorkerTask::new_thread( let upid_str = WorkerTask::new_thread(
"tape-backup", "tape-backup",
Some(store.clone()), Some(store.clone()),
auth_id, auth_id,
to_stdout, to_stdout,
move |worker| { move |worker| {
backup_worker(&worker, datastore, &pool_config)?; backup_worker(&worker, datastore, &pool_config, eject_media)?;
Ok(()) Ok(())
} }
)?; )?;
Ok(upid_str.into()) Ok(upid_str.into())
} }
pub const ROUTER: Router = Router::new() pub const ROUTER: Router = Router::new()
@ -101,6 +107,7 @@ fn backup_worker(
worker: &WorkerTask, worker: &WorkerTask,
datastore: Arc<DataStore>, datastore: Arc<DataStore>,
pool_config: &MediaPoolConfig, pool_config: &MediaPoolConfig,
eject_media: bool,
) -> Result<(), Error> { ) -> Result<(), Error> {
let status_path = Path::new(TAPE_STATUS_DIR); let status_path = Path::new(TAPE_STATUS_DIR);
@ -135,6 +142,11 @@ fn backup_worker(
pool_writer.commit()?; pool_writer.commit()?;
if eject_media {
worker.log(format!("ejection backup media"));
pool_writer.eject_media()?;
}
Ok(()) Ok(())
} }

View File

@ -718,6 +718,11 @@ async fn clean_drive(
pool: { pool: {
schema: MEDIA_POOL_NAME_SCHEMA, schema: MEDIA_POOL_NAME_SCHEMA,
}, },
"eject-media": {
description: "Eject media upon job completion.",
type: bool,
optional: true,
},
}, },
}, },
)] )]

View File

@ -26,6 +26,7 @@ use crate::{
tape_write_snapshot_archive, tape_write_snapshot_archive,
request_and_load_media, request_and_load_media,
tape_alert_flags_critical, tape_alert_flags_critical,
media_changer,
file_formats::MediaSetLabel, file_formats::MediaSetLabel,
}, },
}; };
@ -106,6 +107,24 @@ impl PoolWriter {
self.media_set_catalog.contains_snapshot(snapshot) self.media_set_catalog.contains_snapshot(snapshot)
} }
/// Eject media and drop PoolWriterState (close drive)
pub fn eject_media(&mut self) -> Result<(), Error> {
let mut status = match self.status.take() {
Some(status) => status,
None => return Ok(()), // no media loaded
};
let (drive_config, _digest) = crate::config::drive::config()?;
if let Some((mut changer, _)) = media_changer(&drive_config, &self.drive_name)? {
changer.unload_media(None)?;
} else {
status.drive.eject_media()?;
}
Ok(())
}
/// commit changes to tape and catalog /// commit changes to tape and catalog
/// ///
/// This is done automatically during a backupsession, but needs to /// This is done automatically during a backupsession, but needs to