2021-02-18 14:40:27 +00:00
|
|
|
use std::collections::HashMap;
|
2020-12-09 09:16:01 +00:00
|
|
|
use std::path::Path;
|
|
|
|
|
2020-12-08 14:42:50 +00:00
|
|
|
use anyhow::Error;
|
|
|
|
use serde_json::Value;
|
|
|
|
|
|
|
|
use proxmox::api::{api, Router, SubdirMap};
|
|
|
|
use proxmox::list_subdirs_api_method;
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
config,
|
|
|
|
api2::types::{
|
2020-12-13 08:22:08 +00:00
|
|
|
CHANGER_NAME_SCHEMA,
|
2021-01-30 08:36:54 +00:00
|
|
|
ChangerListEntry,
|
2021-02-18 14:40:27 +00:00
|
|
|
LinuxTapeDrive,
|
2020-12-08 14:42:50 +00:00
|
|
|
MtxEntryKind,
|
2021-01-28 11:59:41 +00:00
|
|
|
MtxStatusEntry,
|
|
|
|
ScsiTapeChanger,
|
2020-12-08 14:42:50 +00:00
|
|
|
},
|
|
|
|
tape::{
|
2020-12-09 09:16:01 +00:00
|
|
|
TAPE_STATUS_DIR,
|
|
|
|
Inventory,
|
2021-01-21 16:25:32 +00:00
|
|
|
linux_tape_changer_list,
|
2021-01-21 16:12:01 +00:00
|
|
|
changer::{
|
|
|
|
OnlineStatusMap,
|
|
|
|
ElementStatus,
|
2021-01-25 09:15:59 +00:00
|
|
|
ScsiMediaChange,
|
2021-01-21 16:12:01 +00:00
|
|
|
mtx_status_to_online_set,
|
|
|
|
},
|
2021-02-18 14:40:27 +00:00
|
|
|
drive::get_tape_device_state,
|
2021-01-30 08:36:54 +00:00
|
|
|
lookup_device_identification,
|
2020-12-08 14:42:50 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
name: {
|
2020-12-13 08:22:08 +00:00
|
|
|
schema: CHANGER_NAME_SCHEMA,
|
2020-12-08 14:42:50 +00:00
|
|
|
},
|
2021-02-19 15:48:19 +00:00
|
|
|
cache: {
|
|
|
|
description: "Use cached value.",
|
|
|
|
optional: true,
|
|
|
|
default: true,
|
|
|
|
},
|
2020-12-08 14:42:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
description: "A status entry for each drive and slot.",
|
|
|
|
type: Array,
|
|
|
|
items: {
|
|
|
|
type: MtxStatusEntry,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Get tape changer status
|
2021-02-19 15:48:19 +00:00
|
|
|
pub async fn get_status(
|
|
|
|
name: String,
|
|
|
|
cache: bool,
|
|
|
|
) -> Result<Vec<MtxStatusEntry>, Error> {
|
2020-12-08 14:42:50 +00:00
|
|
|
|
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
|
2021-01-25 09:15:59 +00:00
|
|
|
let mut changer_config: ScsiTapeChanger = config.lookup("changer", &name)?;
|
2020-12-08 14:42:50 +00:00
|
|
|
|
2020-12-12 08:45:08 +00:00
|
|
|
let status = tokio::task::spawn_blocking(move || {
|
2021-02-19 15:48:19 +00:00
|
|
|
changer_config.status(cache)
|
2020-12-12 08:45:08 +00:00
|
|
|
}).await??;
|
2020-12-08 14:42:50 +00:00
|
|
|
|
2020-12-09 09:16:01 +00:00
|
|
|
let state_path = Path::new(TAPE_STATUS_DIR);
|
2021-01-01 15:15:13 +00:00
|
|
|
let mut inventory = Inventory::load(state_path)?;
|
2020-12-08 14:42:50 +00:00
|
|
|
|
|
|
|
let mut map = OnlineStatusMap::new(&config)?;
|
|
|
|
let online_set = mtx_status_to_online_set(&status, &inventory);
|
|
|
|
map.update_online_status(&name, online_set)?;
|
|
|
|
|
2021-01-01 15:15:13 +00:00
|
|
|
inventory.update_online_status(&map)?;
|
2020-12-08 14:42:50 +00:00
|
|
|
|
2021-02-18 14:40:27 +00:00
|
|
|
let drive_list: Vec<LinuxTapeDrive> = config.convert_to_typed_array("linux")?;
|
|
|
|
let mut drive_map: HashMap<u64, String> = HashMap::new();
|
|
|
|
|
|
|
|
for drive in drive_list {
|
|
|
|
if let Some(changer) = drive.changer {
|
|
|
|
if changer != name {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let num = drive.changer_drivenum.unwrap_or(0);
|
|
|
|
drive_map.insert(num, drive.name.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 14:42:50 +00:00
|
|
|
let mut list = Vec::new();
|
|
|
|
|
|
|
|
for (id, drive_status) in status.drives.iter().enumerate() {
|
2021-02-18 14:40:27 +00:00
|
|
|
let mut state = None;
|
|
|
|
if let Some(drive) = drive_map.get(&(id as u64)) {
|
|
|
|
state = get_tape_device_state(&config, &drive)?;
|
|
|
|
}
|
2020-12-08 14:42:50 +00:00
|
|
|
let entry = MtxStatusEntry {
|
|
|
|
entry_kind: MtxEntryKind::Drive,
|
|
|
|
entry_id: id as u64,
|
2021-01-13 12:26:59 +00:00
|
|
|
label_text: match &drive_status.status {
|
2020-12-08 14:42:50 +00:00
|
|
|
ElementStatus::Empty => None,
|
|
|
|
ElementStatus::Full => Some(String::new()),
|
|
|
|
ElementStatus::VolumeTag(tag) => Some(tag.to_string()),
|
|
|
|
},
|
|
|
|
loaded_slot: drive_status.loaded_slot,
|
2021-02-18 14:40:27 +00:00
|
|
|
state,
|
2020-12-08 14:42:50 +00:00
|
|
|
};
|
|
|
|
list.push(entry);
|
|
|
|
}
|
|
|
|
|
2021-01-25 09:15:59 +00:00
|
|
|
for (id, slot_info) in status.slots.iter().enumerate() {
|
2020-12-08 14:42:50 +00:00
|
|
|
let entry = MtxStatusEntry {
|
2021-01-25 09:15:59 +00:00
|
|
|
entry_kind: if slot_info.import_export {
|
2020-12-28 12:32:56 +00:00
|
|
|
MtxEntryKind::ImportExport
|
|
|
|
} else {
|
|
|
|
MtxEntryKind::Slot
|
|
|
|
},
|
2020-12-08 14:42:50 +00:00
|
|
|
entry_id: id as u64 + 1,
|
2021-01-25 09:15:59 +00:00
|
|
|
label_text: match &slot_info.status {
|
2020-12-08 14:42:50 +00:00
|
|
|
ElementStatus::Empty => None,
|
|
|
|
ElementStatus::Full => Some(String::new()),
|
|
|
|
ElementStatus::VolumeTag(tag) => Some(tag.to_string()),
|
|
|
|
},
|
|
|
|
loaded_slot: None,
|
2021-02-18 14:40:27 +00:00
|
|
|
state: None,
|
2020-12-08 14:42:50 +00:00
|
|
|
};
|
|
|
|
list.push(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(list)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
name: {
|
2020-12-13 08:22:08 +00:00
|
|
|
schema: CHANGER_NAME_SCHEMA,
|
2020-12-08 14:42:50 +00:00
|
|
|
},
|
|
|
|
from: {
|
|
|
|
description: "Source slot number",
|
|
|
|
minimum: 1,
|
|
|
|
},
|
|
|
|
to: {
|
|
|
|
description: "Destination slot number",
|
|
|
|
minimum: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Transfers media from one slot to another
|
2020-12-12 08:45:08 +00:00
|
|
|
pub async fn transfer(
|
2020-12-08 14:42:50 +00:00
|
|
|
name: String,
|
|
|
|
from: u64,
|
|
|
|
to: u64,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
|
2021-01-25 09:15:59 +00:00
|
|
|
let mut changer_config: ScsiTapeChanger = config.lookup("changer", &name)?;
|
2020-12-08 14:42:50 +00:00
|
|
|
|
2020-12-12 08:45:08 +00:00
|
|
|
tokio::task::spawn_blocking(move || {
|
2021-01-25 09:15:59 +00:00
|
|
|
changer_config.transfer(from, to)
|
2020-12-12 08:45:08 +00:00
|
|
|
}).await?
|
2020-12-08 14:42:50 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 15:30:57 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
description: "The list of configured changers with model information.",
|
|
|
|
type: Array,
|
|
|
|
items: {
|
2021-01-30 08:36:54 +00:00
|
|
|
type: ChangerListEntry,
|
2021-01-25 15:30:57 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// List changers
|
|
|
|
pub fn list_changers(
|
|
|
|
_param: Value,
|
2021-01-30 08:36:54 +00:00
|
|
|
) -> Result<Vec<ChangerListEntry>, Error> {
|
2021-01-25 15:30:57 +00:00
|
|
|
|
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
|
|
|
|
let linux_changers = linux_tape_changer_list();
|
|
|
|
|
|
|
|
let changer_list: Vec<ScsiTapeChanger> = config.convert_to_typed_array("changer")?;
|
|
|
|
|
|
|
|
let mut list = Vec::new();
|
|
|
|
|
|
|
|
for changer in changer_list {
|
2021-01-30 08:36:54 +00:00
|
|
|
let info = lookup_device_identification(&linux_changers, &changer.path);
|
|
|
|
let entry = ChangerListEntry { config: changer, info };
|
2021-01-25 15:30:57 +00:00
|
|
|
list.push(entry);
|
|
|
|
}
|
|
|
|
Ok(list)
|
|
|
|
}
|
|
|
|
|
2020-12-08 14:42:50 +00:00
|
|
|
const SUBDIRS: SubdirMap = &[
|
|
|
|
(
|
2021-01-25 15:30:57 +00:00
|
|
|
"status",
|
|
|
|
&Router::new()
|
|
|
|
.get(&API_METHOD_GET_STATUS)
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"transfer",
|
2020-12-08 14:42:50 +00:00
|
|
|
&Router::new()
|
2021-01-25 15:30:57 +00:00
|
|
|
.post(&API_METHOD_TRANSFER)
|
2020-12-08 14:42:50 +00:00
|
|
|
),
|
|
|
|
];
|
|
|
|
|
2021-01-25 15:30:57 +00:00
|
|
|
const ITEM_ROUTER: Router = Router::new()
|
2020-12-08 14:42:50 +00:00
|
|
|
.get(&list_subdirs_api_method!(SUBDIRS))
|
2021-01-25 15:30:57 +00:00
|
|
|
.subdirs(&SUBDIRS);
|
|
|
|
|
|
|
|
pub const ROUTER: Router = Router::new()
|
|
|
|
.get(&API_METHOD_LIST_CHANGERS)
|
|
|
|
.match_all("name", &ITEM_ROUTER);
|