2020-12-10 11:30:27 +00:00
|
|
|
use std::path::Path;
|
2020-12-11 08:10:22 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2021-01-18 06:16:06 +00:00
|
|
|
use anyhow::{bail, Error};
|
2020-12-08 14:42:50 +00:00
|
|
|
use serde_json::Value;
|
|
|
|
|
2020-12-10 11:30:27 +00:00
|
|
|
use proxmox::{
|
|
|
|
sortable,
|
|
|
|
identity,
|
|
|
|
list_subdirs_api_method,
|
|
|
|
tools::Uuid,
|
|
|
|
sys::error::SysError,
|
|
|
|
api::{
|
|
|
|
api,
|
2020-12-11 08:10:22 +00:00
|
|
|
RpcEnvironment,
|
2020-12-30 18:01:39 +00:00
|
|
|
RpcEnvironmentType,
|
2020-12-10 11:30:27 +00:00
|
|
|
Router,
|
|
|
|
SubdirMap,
|
|
|
|
},
|
|
|
|
};
|
2020-12-08 14:42:50 +00:00
|
|
|
|
|
|
|
use crate::{
|
2020-12-11 09:42:29 +00:00
|
|
|
config::{
|
|
|
|
self,
|
|
|
|
drive::check_drive_exists,
|
|
|
|
},
|
2020-12-30 08:48:18 +00:00
|
|
|
api2::{
|
|
|
|
types::{
|
|
|
|
UPID_SCHEMA,
|
|
|
|
DRIVE_NAME_SCHEMA,
|
|
|
|
MEDIA_LABEL_SCHEMA,
|
|
|
|
MEDIA_POOL_NAME_SCHEMA,
|
|
|
|
Authid,
|
|
|
|
LinuxTapeDrive,
|
|
|
|
TapeDeviceInfo,
|
|
|
|
MediaIdFlat,
|
|
|
|
LabelUuidMap,
|
|
|
|
MamAttribute,
|
|
|
|
LinuxDriveAndMediaStatus,
|
|
|
|
},
|
|
|
|
tape::restore::restore_media,
|
2020-12-08 14:42:50 +00:00
|
|
|
},
|
2020-12-11 08:10:22 +00:00
|
|
|
server::WorkerTask,
|
2020-12-08 14:42:50 +00:00
|
|
|
tape::{
|
2020-12-10 11:30:27 +00:00
|
|
|
TAPE_STATUS_DIR,
|
|
|
|
TapeDriver,
|
2020-12-30 08:48:18 +00:00
|
|
|
MediaPool,
|
2020-12-10 11:30:27 +00:00
|
|
|
Inventory,
|
2020-12-29 09:55:20 +00:00
|
|
|
MediaCatalog,
|
2020-12-10 11:30:27 +00:00
|
|
|
MediaId,
|
2020-12-08 14:42:50 +00:00
|
|
|
linux_tape_device_list,
|
2020-12-09 16:35:31 +00:00
|
|
|
open_drive,
|
2020-12-09 16:50:48 +00:00
|
|
|
media_changer,
|
2020-12-30 16:16:57 +00:00
|
|
|
required_media_changer,
|
2020-12-11 06:39:28 +00:00
|
|
|
update_changer_online_status,
|
2020-12-29 07:39:06 +00:00
|
|
|
linux_tape::{
|
|
|
|
LinuxTapeHandle,
|
|
|
|
open_linux_tape_device,
|
|
|
|
},
|
2020-12-22 09:42:22 +00:00
|
|
|
file_formats::{
|
2020-12-14 16:37:16 +00:00
|
|
|
MediaLabel,
|
2020-12-10 11:30:27 +00:00
|
|
|
MediaSetLabel,
|
|
|
|
},
|
2020-12-08 14:42:50 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
2020-12-10 07:04:55 +00:00
|
|
|
drive: {
|
2020-12-13 08:18:16 +00:00
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
2020-12-08 14:42:50 +00:00
|
|
|
},
|
2021-01-13 12:26:59 +00:00
|
|
|
"label-text": {
|
2021-01-07 13:26:43 +00:00
|
|
|
schema: MEDIA_LABEL_SCHEMA,
|
2020-12-08 14:42:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
2021-01-07 13:26:43 +00:00
|
|
|
/// Load media with specified label
|
|
|
|
///
|
|
|
|
/// Issue a media load request to the associated changer device.
|
2021-01-13 12:26:59 +00:00
|
|
|
pub async fn load_media(drive: String, label_text: String) -> Result<(), Error> {
|
2020-12-08 14:42:50 +00:00
|
|
|
|
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
|
2020-12-12 08:20:04 +00:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2021-01-07 13:26:43 +00:00
|
|
|
let (mut changer, _) = required_media_changer(&config, &drive)?;
|
2021-01-13 12:26:59 +00:00
|
|
|
changer.load_media(&label_text)
|
2020-12-12 08:20:04 +00:00
|
|
|
}).await?
|
2020-12-08 14:42:50 +00:00
|
|
|
}
|
2021-01-10 12:44:44 +00:00
|
|
|
|
2020-12-10 06:52:56 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
2020-12-13 08:18:16 +00:00
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
2020-12-10 06:52:56 +00:00
|
|
|
},
|
2021-01-07 13:26:43 +00:00
|
|
|
"source-slot": {
|
|
|
|
description: "Source slot number.",
|
|
|
|
minimum: 1,
|
2020-12-10 06:52:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
2021-01-07 13:26:43 +00:00
|
|
|
/// Load media from the specified slot
|
2020-12-10 06:52:56 +00:00
|
|
|
///
|
|
|
|
/// Issue a media load request to the associated changer device.
|
2021-01-07 13:26:43 +00:00
|
|
|
pub async fn load_slot(drive: String, source_slot: u64) -> Result<(), Error> {
|
2020-12-10 06:52:56 +00:00
|
|
|
|
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
|
2020-12-12 08:20:04 +00:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2020-12-30 16:16:57 +00:00
|
|
|
let (mut changer, _) = required_media_changer(&config, &drive)?;
|
2021-01-07 13:26:43 +00:00
|
|
|
changer.load_media_from_slot(source_slot)
|
2020-12-12 08:20:04 +00:00
|
|
|
}).await?
|
2020-12-10 06:52:56 +00:00
|
|
|
}
|
|
|
|
|
2021-01-10 12:44:44 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
},
|
2021-01-13 12:26:59 +00:00
|
|
|
"label-text": {
|
2021-01-10 12:44:44 +00:00
|
|
|
schema: MEDIA_LABEL_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
description: "The import-export slot number the media was transfered to.",
|
|
|
|
type: u64,
|
|
|
|
minimum: 1,
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Export media with specified label
|
2021-01-13 12:26:59 +00:00
|
|
|
pub async fn export_media(drive: String, label_text: String) -> Result<u64, Error> {
|
2021-01-10 12:44:44 +00:00
|
|
|
|
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
|
|
|
|
tokio::task::spawn_blocking(move || {
|
|
|
|
let (mut changer, changer_name) = required_media_changer(&config, &drive)?;
|
2021-01-13 12:26:59 +00:00
|
|
|
match changer.export_media(&label_text)? {
|
2021-01-10 12:44:44 +00:00
|
|
|
Some(slot) => Ok(slot),
|
2021-01-13 12:26:59 +00:00
|
|
|
None => bail!("media '{}' is not online (via changer '{}')", label_text, changer_name),
|
2021-01-10 12:44:44 +00:00
|
|
|
}
|
|
|
|
}).await?
|
|
|
|
}
|
|
|
|
|
2020-12-08 14:42:50 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
2020-12-10 07:35:11 +00:00
|
|
|
drive: {
|
2020-12-13 08:18:16 +00:00
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
2020-12-08 14:42:50 +00:00
|
|
|
},
|
2021-01-07 13:26:43 +00:00
|
|
|
"target-slot": {
|
2020-12-08 14:42:50 +00:00
|
|
|
description: "Target slot number. If omitted, defaults to the slot that the drive was loaded from.",
|
|
|
|
minimum: 1,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Unload media via changer
|
2020-12-12 08:20:04 +00:00
|
|
|
pub async fn unload(
|
2020-12-10 07:35:11 +00:00
|
|
|
drive: String,
|
2021-01-07 13:26:43 +00:00
|
|
|
target_slot: Option<u64>,
|
2020-12-08 14:42:50 +00:00
|
|
|
_param: Value,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
|
2020-12-12 08:20:04 +00:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2021-01-10 09:02:01 +00:00
|
|
|
let (mut changer, _) = required_media_changer(&config, &drive)?;
|
|
|
|
changer.unload_media(target_slot)
|
2020-12-12 08:20:04 +00:00
|
|
|
}).await?
|
2020-12-08 14:42:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
description: "The list of autodetected tape drives.",
|
|
|
|
type: Array,
|
|
|
|
items: {
|
|
|
|
type: TapeDeviceInfo,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Scan tape drives
|
|
|
|
pub fn scan_drives(_param: Value) -> Result<Vec<TapeDeviceInfo>, Error> {
|
|
|
|
|
|
|
|
let list = linux_tape_device_list();
|
|
|
|
|
|
|
|
Ok(list)
|
|
|
|
}
|
|
|
|
|
2020-12-09 16:35:31 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
2020-12-13 08:18:16 +00:00
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
2020-12-09 16:35:31 +00:00
|
|
|
},
|
|
|
|
fast: {
|
|
|
|
description: "Use fast erase.",
|
|
|
|
type: bool,
|
|
|
|
optional: true,
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-12-11 10:15:58 +00:00
|
|
|
returns: {
|
|
|
|
schema: UPID_SCHEMA,
|
|
|
|
},
|
2020-12-09 16:35:31 +00:00
|
|
|
)]
|
|
|
|
/// Erase media
|
2020-12-11 10:15:58 +00:00
|
|
|
pub fn erase_media(
|
|
|
|
drive: String,
|
|
|
|
fast: Option<bool>,
|
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
2020-12-09 16:35:31 +00:00
|
|
|
|
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
|
2020-12-11 10:15:58 +00:00
|
|
|
check_drive_exists(&config, &drive)?; // early check before starting worker
|
2020-12-09 16:35:31 +00:00
|
|
|
|
2020-12-11 10:15:58 +00:00
|
|
|
let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
|
2020-12-09 16:35:31 +00:00
|
|
|
|
2020-12-30 18:01:39 +00:00
|
|
|
let to_stdout = if rpcenv.env_type() == RpcEnvironmentType::CLI { true } else { false };
|
|
|
|
|
2020-12-11 10:15:58 +00:00
|
|
|
let upid_str = WorkerTask::new_thread(
|
|
|
|
"erase-media",
|
|
|
|
Some(drive.clone()),
|
|
|
|
auth_id,
|
2020-12-30 18:01:39 +00:00
|
|
|
to_stdout,
|
2020-12-11 10:15:58 +00:00
|
|
|
move |_worker| {
|
|
|
|
let mut drive = open_drive(&config, &drive)?;
|
|
|
|
drive.erase_media(fast.unwrap_or(true))?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(upid_str.into())
|
2020-12-09 16:35:31 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 16:43:38 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
2020-12-13 08:18:16 +00:00
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
2020-12-09 16:43:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-12-11 10:15:58 +00:00
|
|
|
returns: {
|
|
|
|
schema: UPID_SCHEMA,
|
|
|
|
},
|
2020-12-09 16:43:38 +00:00
|
|
|
)]
|
|
|
|
/// Rewind tape
|
2020-12-11 10:15:58 +00:00
|
|
|
pub fn rewind(
|
|
|
|
drive: String,
|
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
2020-12-09 16:43:38 +00:00
|
|
|
|
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
|
2020-12-11 10:15:58 +00:00
|
|
|
check_drive_exists(&config, &drive)?; // early check before starting worker
|
2020-12-09 16:43:38 +00:00
|
|
|
|
2020-12-11 10:15:58 +00:00
|
|
|
let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
|
2020-12-09 16:43:38 +00:00
|
|
|
|
2020-12-30 18:01:39 +00:00
|
|
|
let to_stdout = if rpcenv.env_type() == RpcEnvironmentType::CLI { true } else { false };
|
|
|
|
|
2020-12-11 10:15:58 +00:00
|
|
|
let upid_str = WorkerTask::new_thread(
|
|
|
|
"rewind-media",
|
|
|
|
Some(drive.clone()),
|
|
|
|
auth_id,
|
2020-12-30 18:01:39 +00:00
|
|
|
to_stdout,
|
2020-12-11 10:15:58 +00:00
|
|
|
move |_worker| {
|
|
|
|
let mut drive = open_drive(&config, &drive)?;
|
|
|
|
drive.rewind()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(upid_str.into())
|
2020-12-09 16:43:38 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 16:50:48 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
2020-12-13 08:18:16 +00:00
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
2020-12-09 16:50:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Eject/Unload drive media
|
2020-12-12 08:20:04 +00:00
|
|
|
pub async fn eject_media(drive: String) -> Result<(), Error> {
|
2020-12-09 16:50:48 +00:00
|
|
|
|
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
|
2020-12-12 08:20:04 +00:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2020-12-30 16:16:57 +00:00
|
|
|
if let Some((mut changer, _)) = media_changer(&config, &drive)? {
|
2021-01-07 13:26:43 +00:00
|
|
|
changer.unload_media(None)?;
|
2020-12-30 16:16:57 +00:00
|
|
|
} else {
|
2020-12-12 08:20:04 +00:00
|
|
|
let mut drive = open_drive(&config, &drive)?;
|
|
|
|
drive.eject_media()?;
|
|
|
|
}
|
2020-12-30 16:16:57 +00:00
|
|
|
Ok(())
|
2020-12-12 08:20:04 +00:00
|
|
|
}).await?
|
2020-12-09 16:50:48 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 11:30:27 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
2020-12-13 08:18:16 +00:00
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
2020-12-10 11:30:27 +00:00
|
|
|
},
|
2021-01-13 12:26:59 +00:00
|
|
|
"label-text": {
|
2020-12-10 11:30:27 +00:00
|
|
|
schema: MEDIA_LABEL_SCHEMA,
|
|
|
|
},
|
|
|
|
pool: {
|
|
|
|
schema: MEDIA_POOL_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-12-11 08:10:22 +00:00
|
|
|
returns: {
|
|
|
|
schema: UPID_SCHEMA,
|
|
|
|
},
|
2020-12-10 11:30:27 +00:00
|
|
|
)]
|
|
|
|
/// Label media
|
|
|
|
///
|
|
|
|
/// Write a new media label to the media in 'drive'. The media is
|
|
|
|
/// assigned to the specified 'pool', or else to the free media pool.
|
|
|
|
///
|
|
|
|
/// Note: The media need to be empty (you may want to erase it first).
|
|
|
|
pub fn label_media(
|
|
|
|
drive: String,
|
|
|
|
pool: Option<String>,
|
2021-01-13 12:26:59 +00:00
|
|
|
label_text: String,
|
2020-12-11 08:10:22 +00:00
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
|
2020-12-10 11:30:27 +00:00
|
|
|
|
|
|
|
if let Some(ref pool) = pool {
|
|
|
|
let (pool_config, _digest) = config::media_pool::config()?;
|
|
|
|
|
|
|
|
if pool_config.sections.get(pool).is_none() {
|
|
|
|
bail!("no such pool ('{}')", pool);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
|
2020-12-30 18:01:39 +00:00
|
|
|
let to_stdout = if rpcenv.env_type() == RpcEnvironmentType::CLI { true } else { false };
|
|
|
|
|
2020-12-11 08:10:22 +00:00
|
|
|
let upid_str = WorkerTask::new_thread(
|
|
|
|
"label-media",
|
|
|
|
Some(drive.clone()),
|
|
|
|
auth_id,
|
2020-12-30 18:01:39 +00:00
|
|
|
to_stdout,
|
2020-12-11 08:10:22 +00:00
|
|
|
move |worker| {
|
|
|
|
|
|
|
|
let mut drive = open_drive(&config, &drive)?;
|
|
|
|
|
|
|
|
drive.rewind()?;
|
|
|
|
|
|
|
|
match drive.read_next_file() {
|
|
|
|
Ok(Some(_file)) => bail!("media is not empty (erase first)"),
|
|
|
|
Ok(None) => { /* EOF mark at BOT, assume tape is empty */ },
|
|
|
|
Err(err) => {
|
|
|
|
if err.is_errno(nix::errno::Errno::ENOSPC) || err.is_errno(nix::errno::Errno::EIO) {
|
|
|
|
/* assume tape is empty */
|
|
|
|
} else {
|
|
|
|
bail!("media read error - {}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-10 11:30:27 +00:00
|
|
|
|
2020-12-11 08:10:22 +00:00
|
|
|
let ctime = proxmox::tools::time::epoch_i64();
|
2020-12-14 16:37:16 +00:00
|
|
|
let label = MediaLabel {
|
2021-01-13 12:26:59 +00:00
|
|
|
label_text: label_text.to_string(),
|
2020-12-11 08:10:22 +00:00
|
|
|
uuid: Uuid::generate(),
|
|
|
|
ctime,
|
|
|
|
};
|
2020-12-10 11:30:27 +00:00
|
|
|
|
2020-12-11 08:10:22 +00:00
|
|
|
write_media_label(worker, &mut drive, label, pool)
|
2020-12-10 11:30:27 +00:00
|
|
|
}
|
2020-12-11 08:10:22 +00:00
|
|
|
)?;
|
2020-12-10 11:30:27 +00:00
|
|
|
|
2020-12-11 08:10:22 +00:00
|
|
|
Ok(upid_str.into())
|
2020-12-10 11:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write_media_label(
|
2020-12-11 08:10:22 +00:00
|
|
|
worker: Arc<WorkerTask>,
|
2020-12-10 11:30:27 +00:00
|
|
|
drive: &mut Box<dyn TapeDriver>,
|
2020-12-14 16:37:16 +00:00
|
|
|
label: MediaLabel,
|
2020-12-10 11:30:27 +00:00
|
|
|
pool: Option<String>,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
drive.label_tape(&label)?;
|
|
|
|
|
|
|
|
let mut media_set_label = None;
|
|
|
|
|
|
|
|
if let Some(ref pool) = pool {
|
|
|
|
// assign media to pool by writing special media set label
|
2021-01-13 12:26:59 +00:00
|
|
|
worker.log(format!("Label media '{}' for pool '{}'", label.label_text, pool));
|
2021-01-18 12:36:11 +00:00
|
|
|
let set = MediaSetLabel::with_data(&pool, [0u8; 16].into(), 0, label.ctime, None);
|
2020-12-10 11:30:27 +00:00
|
|
|
|
2021-01-19 05:19:18 +00:00
|
|
|
drive.write_media_set_label(&set, None)?;
|
2020-12-10 11:30:27 +00:00
|
|
|
media_set_label = Some(set);
|
|
|
|
} else {
|
2021-01-13 12:26:59 +00:00
|
|
|
worker.log(format!("Label media '{}' (no pool assignment)", label.label_text));
|
2020-12-10 11:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let media_id = MediaId { label, media_set_label };
|
|
|
|
|
2020-12-29 09:55:20 +00:00
|
|
|
let status_path = Path::new(TAPE_STATUS_DIR);
|
|
|
|
|
|
|
|
// Create the media catalog
|
|
|
|
MediaCatalog::overwrite(status_path, &media_id, false)?;
|
|
|
|
|
|
|
|
let mut inventory = Inventory::load(status_path)?;
|
2021-01-01 15:15:13 +00:00
|
|
|
inventory.store(media_id.clone(), false)?;
|
2020-12-10 11:30:27 +00:00
|
|
|
|
|
|
|
drive.rewind()?;
|
|
|
|
|
|
|
|
match drive.read_label() {
|
2021-01-19 05:19:18 +00:00
|
|
|
Ok((Some(info), _)) => {
|
2020-12-10 11:30:27 +00:00
|
|
|
if info.label.uuid != media_id.label.uuid {
|
|
|
|
bail!("verify label failed - got wrong label uuid");
|
|
|
|
}
|
|
|
|
if let Some(ref pool) = pool {
|
|
|
|
match info.media_set_label {
|
2020-12-16 12:27:53 +00:00
|
|
|
Some(set) => {
|
2020-12-10 11:30:27 +00:00
|
|
|
if set.uuid != [0u8; 16].into() {
|
|
|
|
bail!("verify media set label failed - got wrong set uuid");
|
|
|
|
}
|
|
|
|
if &set.pool != pool {
|
|
|
|
bail!("verify media set label failed - got wrong pool");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
bail!("verify media set label failed (missing set label)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2021-01-19 05:19:18 +00:00
|
|
|
Ok((None, _)) => bail!("verify label failed (got empty media)"),
|
2020-12-10 11:30:27 +00:00
|
|
|
Err(err) => bail!("verify label failed - {}", err),
|
|
|
|
};
|
|
|
|
|
|
|
|
drive.rewind()?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-12-10 12:20:39 +00:00
|
|
|
#[api(
|
2021-01-19 05:19:18 +00:00
|
|
|
protected: true,
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
},
|
|
|
|
password: {
|
|
|
|
description: "Encryption key password.",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Try to restore a tape encryption key
|
|
|
|
pub async fn restore_key(
|
|
|
|
drive: String,
|
|
|
|
password: String,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
|
|
|
|
tokio::task::spawn_blocking(move || {
|
|
|
|
let mut drive = open_drive(&config, &drive)?;
|
|
|
|
|
|
|
|
let (_media_id, key_config) = drive.read_label()?;
|
|
|
|
|
|
|
|
if let Some(key_config) = key_config {
|
|
|
|
let password_fn = || { Ok(password.as_bytes().to_vec()) };
|
2021-01-21 06:13:56 +00:00
|
|
|
let (key, ..) = key_config.decrypt(&password_fn)?;
|
2021-01-21 06:46:21 +00:00
|
|
|
config::tape_encryption_keys::insert_key(key, key_config, true)?;
|
2021-01-19 05:19:18 +00:00
|
|
|
} else {
|
|
|
|
bail!("media does not contain any encryption key configuration");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}).await?
|
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
2020-12-10 12:20:39 +00:00
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
2020-12-13 08:18:16 +00:00
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
2020-12-10 12:20:39 +00:00
|
|
|
},
|
2021-01-14 10:51:23 +00:00
|
|
|
inventorize: {
|
|
|
|
description: "Inventorize media",
|
|
|
|
optional: true,
|
|
|
|
},
|
2020-12-10 12:20:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
2020-12-16 12:27:53 +00:00
|
|
|
type: MediaIdFlat,
|
2020-12-10 12:20:39 +00:00
|
|
|
},
|
|
|
|
)]
|
2021-01-19 05:19:18 +00:00
|
|
|
/// Read media label (optionally inventorize media)
|
2021-01-14 10:51:23 +00:00
|
|
|
pub async fn read_label(
|
|
|
|
drive: String,
|
|
|
|
inventorize: Option<bool>,
|
|
|
|
) -> Result<MediaIdFlat, Error> {
|
2020-12-10 12:20:39 +00:00
|
|
|
|
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
|
2020-12-12 08:20:04 +00:00
|
|
|
tokio::task::spawn_blocking(move || {
|
|
|
|
let mut drive = open_drive(&config, &drive)?;
|
2020-12-10 12:20:39 +00:00
|
|
|
|
2021-01-19 05:19:18 +00:00
|
|
|
let (media_id, _key_config) = drive.read_label()?;
|
2020-12-12 08:20:04 +00:00
|
|
|
|
2020-12-16 12:27:53 +00:00
|
|
|
let media_id = match media_id {
|
|
|
|
Some(media_id) => {
|
2021-01-18 12:36:11 +00:00
|
|
|
let mut flat = MediaIdFlat {
|
2021-01-20 16:53:06 +00:00
|
|
|
uuid: media_id.label.uuid.clone(),
|
2021-01-13 12:26:59 +00:00
|
|
|
label_text: media_id.label.label_text.clone(),
|
2020-12-16 12:27:53 +00:00
|
|
|
ctime: media_id.label.ctime,
|
2020-12-12 08:20:04 +00:00
|
|
|
media_set_ctime: None,
|
|
|
|
media_set_uuid: None,
|
2021-01-18 12:36:11 +00:00
|
|
|
encryption_key_fingerprint: None,
|
2020-12-12 08:20:04 +00:00
|
|
|
pool: None,
|
|
|
|
seq_nr: None,
|
|
|
|
};
|
2021-01-14 10:51:23 +00:00
|
|
|
if let Some(ref set) = media_id.media_set_label {
|
2020-12-12 08:20:04 +00:00
|
|
|
flat.pool = Some(set.pool.clone());
|
|
|
|
flat.seq_nr = Some(set.seq_nr);
|
2021-01-20 16:53:06 +00:00
|
|
|
flat.media_set_uuid = Some(set.uuid.clone());
|
2020-12-12 08:20:04 +00:00
|
|
|
flat.media_set_ctime = Some(set.ctime);
|
2021-01-18 12:36:11 +00:00
|
|
|
flat.encryption_key_fingerprint = set
|
|
|
|
.encryption_key_fingerprint
|
|
|
|
.as_ref()
|
|
|
|
.map(|fp| crate::tools::format::as_fingerprint(fp.bytes()));
|
2021-01-21 09:30:29 +00:00
|
|
|
|
|
|
|
let encrypt_fingerprint = set.encryption_key_fingerprint.clone()
|
|
|
|
.map(|fp| (fp, set.uuid.clone()));
|
|
|
|
|
|
|
|
if let Err(err) = drive.set_encryption(encrypt_fingerprint) {
|
|
|
|
// try, but ignore errors. just log to stderr
|
|
|
|
eprintln!("uable to load encryption key: {}", err);
|
|
|
|
}
|
2020-12-12 08:20:04 +00:00
|
|
|
}
|
2021-01-14 10:51:23 +00:00
|
|
|
|
|
|
|
if let Some(true) = inventorize {
|
|
|
|
let state_path = Path::new(TAPE_STATUS_DIR);
|
|
|
|
let mut inventory = Inventory::load(state_path)?;
|
|
|
|
inventory.store(media_id, false)?;
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:20:04 +00:00
|
|
|
flat
|
2020-12-10 12:20:39 +00:00
|
|
|
}
|
2020-12-12 08:20:04 +00:00
|
|
|
None => {
|
|
|
|
bail!("Media is empty (no label).");
|
|
|
|
}
|
|
|
|
};
|
2020-12-10 12:20:39 +00:00
|
|
|
|
2020-12-16 12:27:53 +00:00
|
|
|
Ok(media_id)
|
2020-12-12 08:20:04 +00:00
|
|
|
}).await?
|
2020-12-10 12:20:39 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 10:32:56 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
schema: UPID_SCHEMA,
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Clean drive
|
|
|
|
pub fn clean_drive(
|
|
|
|
drive: String,
|
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
|
|
|
|
check_drive_exists(&config, &drive)?; // early check before starting worker
|
|
|
|
|
|
|
|
let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
|
|
|
|
|
|
|
|
let to_stdout = if rpcenv.env_type() == RpcEnvironmentType::CLI { true } else { false };
|
|
|
|
|
|
|
|
let upid_str = WorkerTask::new_thread(
|
|
|
|
"clean-drive",
|
|
|
|
Some(drive.clone()),
|
|
|
|
auth_id,
|
|
|
|
to_stdout,
|
|
|
|
move |worker| {
|
|
|
|
|
|
|
|
let (mut changer, _changer_name) = required_media_changer(&config, &drive)?;
|
|
|
|
|
|
|
|
worker.log("Starting drive clean");
|
|
|
|
|
|
|
|
changer.clean_drive()?;
|
|
|
|
|
|
|
|
worker.log("Drive cleaned sucessfully");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(upid_str.into())
|
|
|
|
}
|
|
|
|
|
2020-12-11 06:39:28 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
2020-12-13 08:18:16 +00:00
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
2020-12-11 06:39:28 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
description: "The list of media labels with associated media Uuid (if any).",
|
|
|
|
type: Array,
|
|
|
|
items: {
|
|
|
|
type: LabelUuidMap,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
2020-12-11 09:42:29 +00:00
|
|
|
/// List known media labels (Changer Inventory)
|
2020-12-11 06:39:28 +00:00
|
|
|
///
|
|
|
|
/// Note: Only useful for drives with associated changer device.
|
|
|
|
///
|
2020-12-11 09:42:29 +00:00
|
|
|
/// This method queries the changer to get a list of media labels.
|
|
|
|
///
|
|
|
|
/// Note: This updates the media online status.
|
2020-12-12 08:20:04 +00:00
|
|
|
pub async fn inventory(
|
2020-12-11 06:39:28 +00:00
|
|
|
drive: String,
|
|
|
|
) -> Result<Vec<LabelUuidMap>, Error> {
|
|
|
|
|
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
|
2020-12-12 08:20:04 +00:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2021-01-07 13:26:43 +00:00
|
|
|
let (mut changer, changer_name) = required_media_changer(&config, &drive)?;
|
2020-12-11 06:39:28 +00:00
|
|
|
|
2021-01-13 12:26:59 +00:00
|
|
|
let label_text_list = changer.online_media_label_texts()?;
|
2020-12-11 06:39:28 +00:00
|
|
|
|
2020-12-12 08:20:04 +00:00
|
|
|
let state_path = Path::new(TAPE_STATUS_DIR);
|
2020-12-11 06:39:28 +00:00
|
|
|
|
2020-12-12 08:20:04 +00:00
|
|
|
let mut inventory = Inventory::load(state_path)?;
|
2020-12-11 06:39:28 +00:00
|
|
|
|
2020-12-12 08:20:04 +00:00
|
|
|
update_changer_online_status(
|
|
|
|
&config,
|
|
|
|
&mut inventory,
|
|
|
|
&changer_name,
|
2021-01-13 12:26:59 +00:00
|
|
|
&label_text_list,
|
2020-12-12 08:20:04 +00:00
|
|
|
)?;
|
2020-12-11 06:39:28 +00:00
|
|
|
|
2020-12-12 08:20:04 +00:00
|
|
|
let mut list = Vec::new();
|
2020-12-11 06:39:28 +00:00
|
|
|
|
2021-01-13 12:26:59 +00:00
|
|
|
for label_text in label_text_list.iter() {
|
|
|
|
if label_text.starts_with("CLN") {
|
2020-12-12 08:20:04 +00:00
|
|
|
// skip cleaning unit
|
|
|
|
continue;
|
|
|
|
}
|
2020-12-11 06:39:28 +00:00
|
|
|
|
2021-01-13 12:26:59 +00:00
|
|
|
let label_text = label_text.to_string();
|
2020-12-11 06:39:28 +00:00
|
|
|
|
2021-01-13 12:26:59 +00:00
|
|
|
if let Some(media_id) = inventory.find_media_by_label_text(&label_text) {
|
2021-01-20 16:53:06 +00:00
|
|
|
list.push(LabelUuidMap { label_text, uuid: Some(media_id.label.uuid.clone()) });
|
2020-12-12 08:20:04 +00:00
|
|
|
} else {
|
2021-01-13 12:26:59 +00:00
|
|
|
list.push(LabelUuidMap { label_text, uuid: None });
|
2020-12-12 08:20:04 +00:00
|
|
|
}
|
2020-12-11 06:39:28 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 08:20:04 +00:00
|
|
|
Ok(list)
|
|
|
|
}).await?
|
2020-12-11 09:42:29 +00:00
|
|
|
}
|
2020-12-11 06:39:28 +00:00
|
|
|
|
2020-12-11 09:42:29 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
2020-12-13 08:18:16 +00:00
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
2020-12-11 09:42:29 +00:00
|
|
|
},
|
|
|
|
"read-all-labels": {
|
|
|
|
description: "Load all tapes and try read labels (even if already inventoried)",
|
|
|
|
type: bool,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
schema: UPID_SCHEMA,
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Update inventory
|
|
|
|
///
|
|
|
|
/// Note: Only useful for drives with associated changer device.
|
|
|
|
///
|
|
|
|
/// This method queries the changer to get a list of media labels. It
|
|
|
|
/// then loads any unknown media into the drive, reads the label, and
|
|
|
|
/// store the result to the media database.
|
|
|
|
///
|
|
|
|
/// Note: This updates the media online status.
|
|
|
|
pub fn update_inventory(
|
|
|
|
drive: String,
|
|
|
|
read_all_labels: Option<bool>,
|
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
2020-12-11 06:39:28 +00:00
|
|
|
|
2020-12-11 09:42:29 +00:00
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
|
|
|
|
check_drive_exists(&config, &drive)?; // early check before starting worker
|
|
|
|
|
|
|
|
let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
|
|
|
|
|
2020-12-30 18:01:39 +00:00
|
|
|
let to_stdout = if rpcenv.env_type() == RpcEnvironmentType::CLI { true } else { false };
|
|
|
|
|
2020-12-11 09:42:29 +00:00
|
|
|
let upid_str = WorkerTask::new_thread(
|
|
|
|
"inventory-update",
|
|
|
|
Some(drive.clone()),
|
|
|
|
auth_id,
|
2020-12-30 18:01:39 +00:00
|
|
|
to_stdout,
|
2020-12-11 09:42:29 +00:00
|
|
|
move |worker| {
|
|
|
|
|
2020-12-30 16:16:57 +00:00
|
|
|
let (mut changer, changer_name) = required_media_changer(&config, &drive)?;
|
2020-12-11 06:39:28 +00:00
|
|
|
|
2021-01-13 12:26:59 +00:00
|
|
|
let label_text_list = changer.online_media_label_texts()?;
|
|
|
|
if label_text_list.is_empty() {
|
2020-12-11 09:42:29 +00:00
|
|
|
worker.log(format!("changer device does not list any media labels"));
|
2020-12-11 06:39:28 +00:00
|
|
|
}
|
2020-12-11 09:42:29 +00:00
|
|
|
|
|
|
|
let state_path = Path::new(TAPE_STATUS_DIR);
|
|
|
|
|
|
|
|
let mut inventory = Inventory::load(state_path)?;
|
|
|
|
|
2021-01-13 12:26:59 +00:00
|
|
|
update_changer_online_status(&config, &mut inventory, &changer_name, &label_text_list)?;
|
2020-12-11 09:42:29 +00:00
|
|
|
|
2021-01-13 12:26:59 +00:00
|
|
|
for label_text in label_text_list.iter() {
|
|
|
|
if label_text.starts_with("CLN") {
|
|
|
|
worker.log(format!("skip cleaning unit '{}'", label_text));
|
2020-12-11 09:42:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-01-13 12:26:59 +00:00
|
|
|
let label_text = label_text.to_string();
|
2020-12-11 09:42:29 +00:00
|
|
|
|
|
|
|
if !read_all_labels.unwrap_or(false) {
|
2021-01-13 12:26:59 +00:00
|
|
|
if let Some(_) = inventory.find_media_by_label_text(&label_text) {
|
|
|
|
worker.log(format!("media '{}' already inventoried", label_text));
|
2020-12-11 09:42:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-13 12:26:59 +00:00
|
|
|
if let Err(err) = changer.load_media(&label_text) {
|
|
|
|
worker.warn(format!("unable to load media '{}' - {}", label_text, err));
|
2020-12-11 06:39:28 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-12-11 09:42:29 +00:00
|
|
|
|
|
|
|
let mut drive = open_drive(&config, &drive)?;
|
|
|
|
match drive.read_label() {
|
|
|
|
Err(err) => {
|
2021-01-13 12:26:59 +00:00
|
|
|
worker.warn(format!("unable to read label form media '{}' - {}", label_text, err));
|
2020-12-11 09:42:29 +00:00
|
|
|
}
|
2021-01-19 05:19:18 +00:00
|
|
|
Ok((None, _)) => {
|
2021-01-13 12:26:59 +00:00
|
|
|
worker.log(format!("media '{}' is empty", label_text));
|
2020-12-11 09:42:29 +00:00
|
|
|
}
|
2021-01-19 05:19:18 +00:00
|
|
|
Ok((Some(media_id), _key_config)) => {
|
2021-01-13 12:26:59 +00:00
|
|
|
if label_text != media_id.label.label_text {
|
|
|
|
worker.warn(format!("label text missmatch ({} != {})", label_text, media_id.label.label_text));
|
2020-12-11 09:42:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-01-13 12:26:59 +00:00
|
|
|
worker.log(format!("inventorize media '{}' with uuid '{}'", label_text, media_id.label.uuid));
|
2021-01-01 15:15:13 +00:00
|
|
|
inventory.store(media_id, false)?;
|
2020-12-11 09:42:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-08 10:32:56 +00:00
|
|
|
changer.unload_media(None)?;
|
2020-12-11 06:39:28 +00:00
|
|
|
}
|
2020-12-11 09:42:29 +00:00
|
|
|
Ok(())
|
2020-12-11 06:39:28 +00:00
|
|
|
}
|
2020-12-11 09:42:29 +00:00
|
|
|
)?;
|
2020-12-11 06:39:28 +00:00
|
|
|
|
2020-12-11 09:42:29 +00:00
|
|
|
Ok(upid_str.into())
|
2020-12-11 06:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-11 06:50:19 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
2020-12-13 08:18:16 +00:00
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
2020-12-11 06:50:19 +00:00
|
|
|
},
|
|
|
|
pool: {
|
|
|
|
schema: MEDIA_POOL_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-12-11 08:10:22 +00:00
|
|
|
returns: {
|
|
|
|
schema: UPID_SCHEMA,
|
|
|
|
},
|
2020-12-11 06:50:19 +00:00
|
|
|
)]
|
|
|
|
/// Label media with barcodes from changer device
|
|
|
|
pub fn barcode_label_media(
|
|
|
|
drive: String,
|
|
|
|
pool: Option<String>,
|
2020-12-11 08:10:22 +00:00
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
2020-12-11 06:50:19 +00:00
|
|
|
|
|
|
|
if let Some(ref pool) = pool {
|
|
|
|
let (pool_config, _digest) = config::media_pool::config()?;
|
|
|
|
|
|
|
|
if pool_config.sections.get(pool).is_none() {
|
|
|
|
bail!("no such pool ('{}')", pool);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 08:10:22 +00:00
|
|
|
let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
|
|
|
|
|
2020-12-30 18:01:39 +00:00
|
|
|
let to_stdout = if rpcenv.env_type() == RpcEnvironmentType::CLI { true } else { false };
|
|
|
|
|
2020-12-11 08:10:22 +00:00
|
|
|
let upid_str = WorkerTask::new_thread(
|
|
|
|
"barcode-label-media",
|
|
|
|
Some(drive.clone()),
|
|
|
|
auth_id,
|
2020-12-30 18:01:39 +00:00
|
|
|
to_stdout,
|
2020-12-11 08:10:22 +00:00
|
|
|
move |worker| {
|
|
|
|
barcode_label_media_worker(worker, drive, pool)
|
|
|
|
}
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(upid_str.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn barcode_label_media_worker(
|
|
|
|
worker: Arc<WorkerTask>,
|
|
|
|
drive: String,
|
|
|
|
pool: Option<String>,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
2020-12-11 06:50:19 +00:00
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
|
2020-12-30 16:16:57 +00:00
|
|
|
let (mut changer, changer_name) = required_media_changer(&config, &drive)?;
|
2020-12-11 06:50:19 +00:00
|
|
|
|
2021-01-13 12:26:59 +00:00
|
|
|
let label_text_list = changer.online_media_label_texts()?;
|
2020-12-11 06:50:19 +00:00
|
|
|
|
|
|
|
let state_path = Path::new(TAPE_STATUS_DIR);
|
|
|
|
|
|
|
|
let mut inventory = Inventory::load(state_path)?;
|
|
|
|
|
2021-01-13 12:26:59 +00:00
|
|
|
update_changer_online_status(&config, &mut inventory, &changer_name, &label_text_list)?;
|
2020-12-11 06:50:19 +00:00
|
|
|
|
2021-01-13 12:26:59 +00:00
|
|
|
if label_text_list.is_empty() {
|
2020-12-11 06:50:19 +00:00
|
|
|
bail!("changer device does not list any media labels");
|
|
|
|
}
|
|
|
|
|
2021-01-13 12:26:59 +00:00
|
|
|
for label_text in label_text_list {
|
|
|
|
if label_text.starts_with("CLN") { continue; }
|
2020-12-11 06:50:19 +00:00
|
|
|
|
|
|
|
inventory.reload()?;
|
2021-01-13 12:26:59 +00:00
|
|
|
if inventory.find_media_by_label_text(&label_text).is_some() {
|
|
|
|
worker.log(format!("media '{}' already inventoried (already labeled)", label_text));
|
2020-12-11 06:50:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-01-13 12:26:59 +00:00
|
|
|
worker.log(format!("checking/loading media '{}'", label_text));
|
2020-12-11 06:50:19 +00:00
|
|
|
|
2021-01-13 12:26:59 +00:00
|
|
|
if let Err(err) = changer.load_media(&label_text) {
|
|
|
|
worker.warn(format!("unable to load media '{}' - {}", label_text, err));
|
2020-12-11 06:50:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut drive = open_drive(&config, &drive)?;
|
|
|
|
drive.rewind()?;
|
|
|
|
|
|
|
|
match drive.read_next_file() {
|
|
|
|
Ok(Some(_file)) => {
|
2021-01-13 12:26:59 +00:00
|
|
|
worker.log(format!("media '{}' is not empty (erase first)", label_text));
|
2020-12-11 06:50:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Ok(None) => { /* EOF mark at BOT, assume tape is empty */ },
|
|
|
|
Err(err) => {
|
|
|
|
if err.is_errno(nix::errno::Errno::ENOSPC) || err.is_errno(nix::errno::Errno::EIO) {
|
|
|
|
/* assume tape is empty */
|
|
|
|
} else {
|
2021-01-13 12:26:59 +00:00
|
|
|
worker.warn(format!("media '{}' read error (maybe not empty - erase first)", label_text));
|
2020-12-11 06:50:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let ctime = proxmox::tools::time::epoch_i64();
|
2020-12-14 16:37:16 +00:00
|
|
|
let label = MediaLabel {
|
2021-01-13 12:26:59 +00:00
|
|
|
label_text: label_text.to_string(),
|
2020-12-11 06:50:19 +00:00
|
|
|
uuid: Uuid::generate(),
|
|
|
|
ctime,
|
|
|
|
};
|
|
|
|
|
2020-12-11 08:10:22 +00:00
|
|
|
write_media_label(worker.clone(), &mut drive, label, pool.clone())?
|
2020-12-11 06:50:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-12-21 11:11:52 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
description: "A List of medium auxiliary memory attributes.",
|
|
|
|
type: Array,
|
|
|
|
items: {
|
|
|
|
type: MamAttribute,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
2020-12-22 08:27:34 +00:00
|
|
|
/// Read Cartridge Memory (Medium auxiliary memory attributes)
|
|
|
|
pub fn cartridge_memory(drive: String) -> Result<Vec<MamAttribute>, Error> {
|
2020-12-21 11:11:52 +00:00
|
|
|
|
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
|
|
|
|
let drive_config: LinuxTapeDrive = config.lookup("linux", &drive)?;
|
2021-01-18 06:16:06 +00:00
|
|
|
let mut handle = drive_config.open()?;
|
2020-12-21 11:11:52 +00:00
|
|
|
|
2020-12-23 08:44:53 +00:00
|
|
|
handle.cartridge_memory()
|
2020-12-21 11:11:52 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 09:42:22 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
2020-12-23 10:24:34 +00:00
|
|
|
type: LinuxDriveAndMediaStatus,
|
2020-12-22 09:42:22 +00:00
|
|
|
},
|
|
|
|
)]
|
2020-12-23 10:24:34 +00:00
|
|
|
/// Get drive/media status
|
|
|
|
pub fn status(drive: String) -> Result<LinuxDriveAndMediaStatus, Error> {
|
2020-12-22 09:42:22 +00:00
|
|
|
|
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
|
|
|
|
let drive_config: LinuxTapeDrive = config.lookup("linux", &drive)?;
|
|
|
|
|
2020-12-29 07:39:06 +00:00
|
|
|
// Note: use open_linux_tape_device, because this also works if no medium loaded
|
2021-01-18 06:16:06 +00:00
|
|
|
let file = open_linux_tape_device(&drive_config.path)?;
|
2020-12-22 09:42:22 +00:00
|
|
|
|
2020-12-29 07:39:06 +00:00
|
|
|
let mut handle = LinuxTapeHandle::new(file);
|
2020-12-23 10:24:34 +00:00
|
|
|
|
2020-12-29 07:39:06 +00:00
|
|
|
handle.get_drive_and_media_status()
|
2020-12-22 09:42:22 +00:00
|
|
|
}
|
|
|
|
|
2020-12-30 08:48:18 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
},
|
|
|
|
force: {
|
|
|
|
description: "Force overriding existing index.",
|
|
|
|
type: bool,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
verbose: {
|
|
|
|
description: "Verbose mode - log all found chunks.",
|
|
|
|
type: bool,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
schema: UPID_SCHEMA,
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Scan media and record content
|
|
|
|
pub fn catalog_media(
|
|
|
|
drive: String,
|
|
|
|
force: Option<bool>,
|
|
|
|
verbose: Option<bool>,
|
|
|
|
rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let verbose = verbose.unwrap_or(false);
|
|
|
|
let force = force.unwrap_or(false);
|
|
|
|
|
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
|
|
|
|
check_drive_exists(&config, &drive)?; // early check before starting worker
|
|
|
|
|
|
|
|
let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
|
|
|
|
|
2020-12-30 18:01:39 +00:00
|
|
|
let to_stdout = if rpcenv.env_type() == RpcEnvironmentType::CLI { true } else { false };
|
|
|
|
|
2020-12-30 08:48:18 +00:00
|
|
|
let upid_str = WorkerTask::new_thread(
|
|
|
|
"catalog-media",
|
|
|
|
Some(drive.clone()),
|
|
|
|
auth_id,
|
2020-12-30 18:01:39 +00:00
|
|
|
to_stdout,
|
2020-12-30 08:48:18 +00:00
|
|
|
move |worker| {
|
|
|
|
|
|
|
|
let mut drive = open_drive(&config, &drive)?;
|
|
|
|
|
|
|
|
drive.rewind()?;
|
|
|
|
|
|
|
|
let media_id = match drive.read_label()? {
|
2021-01-19 05:19:18 +00:00
|
|
|
(Some(media_id), key_config) => {
|
2020-12-30 08:48:18 +00:00
|
|
|
worker.log(format!(
|
|
|
|
"found media label: {}",
|
|
|
|
serde_json::to_string_pretty(&serde_json::to_value(&media_id)?)?
|
|
|
|
));
|
2021-01-19 05:19:18 +00:00
|
|
|
if key_config.is_some() {
|
|
|
|
worker.log(format!(
|
|
|
|
"encryption key config: {}",
|
|
|
|
serde_json::to_string_pretty(&serde_json::to_value(&key_config)?)?
|
|
|
|
));
|
|
|
|
}
|
2020-12-30 08:48:18 +00:00
|
|
|
media_id
|
|
|
|
},
|
2021-01-19 05:19:18 +00:00
|
|
|
(None, _) => bail!("media is empty (no media label found)"),
|
2020-12-30 08:48:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let status_path = Path::new(TAPE_STATUS_DIR);
|
|
|
|
|
|
|
|
let mut inventory = Inventory::load(status_path)?;
|
2021-01-01 15:15:13 +00:00
|
|
|
inventory.store(media_id.clone(), false)?;
|
2020-12-30 08:48:18 +00:00
|
|
|
|
|
|
|
let pool = match media_id.media_set_label {
|
|
|
|
None => {
|
|
|
|
worker.log("media is empty");
|
|
|
|
MediaCatalog::destroy(status_path, &media_id.label.uuid)?;
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
Some(ref set) => {
|
|
|
|
if set.uuid.as_ref() == [0u8;16] { // media is empty
|
|
|
|
worker.log("media is empty");
|
|
|
|
MediaCatalog::destroy(status_path, &media_id.label.uuid)?;
|
|
|
|
return Ok(());
|
|
|
|
}
|
2021-01-20 16:27:01 +00:00
|
|
|
let encrypt_fingerprint = set.encryption_key_fingerprint.clone()
|
|
|
|
.map(|fp| (fp, set.uuid.clone()));
|
|
|
|
|
2021-01-18 12:36:11 +00:00
|
|
|
drive.set_encryption(encrypt_fingerprint)?;
|
|
|
|
|
2020-12-30 08:48:18 +00:00
|
|
|
set.pool.clone()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let _lock = MediaPool::lock(status_path, &pool)?;
|
|
|
|
|
|
|
|
if MediaCatalog::exists(status_path, &media_id.label.uuid) {
|
|
|
|
if !force {
|
|
|
|
bail!("media catalog exists (please use --force to overwrite)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
restore_media(&worker, &mut drive, &media_id, None, verbose)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
|
|
}
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(upid_str.into())
|
|
|
|
}
|
|
|
|
|
2020-12-10 09:09:12 +00:00
|
|
|
#[sortable]
|
|
|
|
pub const SUBDIRS: SubdirMap = &sorted!([
|
2020-12-11 06:50:19 +00:00
|
|
|
(
|
|
|
|
"barcode-label-media",
|
|
|
|
&Router::new()
|
|
|
|
.put(&API_METHOD_BARCODE_LABEL_MEDIA)
|
|
|
|
),
|
2020-12-30 08:48:18 +00:00
|
|
|
(
|
|
|
|
"catalog",
|
|
|
|
&Router::new()
|
|
|
|
.put(&API_METHOD_CATALOG_MEDIA)
|
|
|
|
),
|
2021-01-08 10:32:56 +00:00
|
|
|
(
|
|
|
|
"clean",
|
|
|
|
&Router::new()
|
|
|
|
.put(&API_METHOD_CLEAN_DRIVE)
|
|
|
|
),
|
2020-12-09 16:35:31 +00:00
|
|
|
(
|
2020-12-10 09:09:12 +00:00
|
|
|
"eject-media",
|
2020-12-09 16:35:31 +00:00
|
|
|
&Router::new()
|
2020-12-10 09:09:12 +00:00
|
|
|
.put(&API_METHOD_EJECT_MEDIA)
|
2020-12-09 16:35:31 +00:00
|
|
|
),
|
2020-12-09 16:50:48 +00:00
|
|
|
(
|
2020-12-10 09:09:12 +00:00
|
|
|
"erase-media",
|
2020-12-09 16:50:48 +00:00
|
|
|
&Router::new()
|
2020-12-10 09:09:12 +00:00
|
|
|
.put(&API_METHOD_ERASE_MEDIA)
|
2020-12-09 16:50:48 +00:00
|
|
|
),
|
2020-12-11 06:39:28 +00:00
|
|
|
(
|
|
|
|
"inventory",
|
|
|
|
&Router::new()
|
|
|
|
.get(&API_METHOD_INVENTORY)
|
2020-12-11 09:42:29 +00:00
|
|
|
.put(&API_METHOD_UPDATE_INVENTORY)
|
2020-12-11 06:39:28 +00:00
|
|
|
),
|
2020-12-10 11:30:27 +00:00
|
|
|
(
|
|
|
|
"label-media",
|
|
|
|
&Router::new()
|
|
|
|
.put(&API_METHOD_LABEL_MEDIA)
|
|
|
|
),
|
2020-12-08 14:42:50 +00:00
|
|
|
(
|
|
|
|
"load-slot",
|
|
|
|
&Router::new()
|
|
|
|
.put(&API_METHOD_LOAD_SLOT)
|
|
|
|
),
|
2020-12-21 11:11:52 +00:00
|
|
|
(
|
2020-12-22 08:27:34 +00:00
|
|
|
"cartridge-memory",
|
2020-12-21 11:11:52 +00:00
|
|
|
&Router::new()
|
2020-12-22 08:27:34 +00:00
|
|
|
.put(&API_METHOD_CARTRIDGE_MEMORY)
|
2020-12-21 11:11:52 +00:00
|
|
|
),
|
2020-12-11 06:39:28 +00:00
|
|
|
(
|
|
|
|
"read-label",
|
|
|
|
&Router::new()
|
|
|
|
.get(&API_METHOD_READ_LABEL)
|
|
|
|
),
|
2020-12-10 08:09:06 +00:00
|
|
|
(
|
|
|
|
"rewind",
|
|
|
|
&Router::new()
|
|
|
|
.put(&API_METHOD_REWIND)
|
|
|
|
),
|
2020-12-08 14:42:50 +00:00
|
|
|
(
|
|
|
|
"scan",
|
|
|
|
&Router::new()
|
|
|
|
.get(&API_METHOD_SCAN_DRIVES)
|
|
|
|
),
|
2020-12-22 09:42:22 +00:00
|
|
|
(
|
|
|
|
"status",
|
|
|
|
&Router::new()
|
|
|
|
.get(&API_METHOD_STATUS)
|
|
|
|
),
|
2020-12-08 14:42:50 +00:00
|
|
|
(
|
|
|
|
"unload",
|
|
|
|
&Router::new()
|
|
|
|
.put(&API_METHOD_UNLOAD)
|
|
|
|
),
|
2020-12-10 09:09:12 +00:00
|
|
|
]);
|
2020-12-08 14:42:50 +00:00
|
|
|
|
|
|
|
pub const ROUTER: Router = Router::new()
|
|
|
|
.get(&list_subdirs_api_method!(SUBDIRS))
|
|
|
|
.subdirs(SUBDIRS);
|