2021-01-22 10:51:36 +00:00
|
|
|
//! Tape drive/changer configuration
|
|
|
|
//!
|
|
|
|
//! This configuration module is based on [`SectionConfig`], and
|
2021-03-30 15:07:59 +00:00
|
|
|
//! provides a type safe interface to store [`LtoTapeDrive`],
|
2021-01-22 10:51:36 +00:00
|
|
|
//! [`VirtualTapeDrive`] and [`ScsiTapeChanger`] configurations.
|
|
|
|
//!
|
|
|
|
//! Drive type [`VirtualTapeDrive`] is only useful for debugging.
|
|
|
|
//!
|
2021-03-30 15:07:59 +00:00
|
|
|
//! [LtoTapeDrive]: crate::api2::types::LtoTapeDrive
|
2021-01-22 10:51:36 +00:00
|
|
|
//! [VirtualTapeDrive]: crate::api2::types::VirtualTapeDrive
|
|
|
|
//! [ScsiTapeChanger]: crate::api2::types::ScsiTapeChanger
|
|
|
|
//! [SectionConfig]: proxmox::api::section_config::SectionConfig
|
|
|
|
|
2020-12-04 14:42:32 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use anyhow::{bail, Error};
|
|
|
|
use lazy_static::lazy_static;
|
|
|
|
|
|
|
|
use proxmox::{
|
|
|
|
api::{
|
|
|
|
schema::*,
|
|
|
|
section_config::{
|
|
|
|
SectionConfig,
|
|
|
|
SectionConfigData,
|
|
|
|
SectionConfigPlugin,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
tools::fs::{
|
|
|
|
replace_file,
|
|
|
|
CreateOptions,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::{
|
2021-07-20 11:51:54 +00:00
|
|
|
backup::{open_backup_lockfile, BackupLockGuard},
|
2020-12-04 14:42:32 +00:00
|
|
|
api2::types::{
|
2020-12-13 08:18:16 +00:00
|
|
|
DRIVE_NAME_SCHEMA,
|
2020-12-04 14:42:32 +00:00
|
|
|
VirtualTapeDrive,
|
2021-03-30 15:07:59 +00:00
|
|
|
LtoTapeDrive,
|
2020-12-04 14:42:32 +00:00
|
|
|
ScsiTapeChanger,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
lazy_static! {
|
2021-01-22 10:51:36 +00:00
|
|
|
/// Static [`SectionConfig`] to access parser/writer functions.
|
2020-12-04 14:42:32 +00:00
|
|
|
pub static ref CONFIG: SectionConfig = init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn init() -> SectionConfig {
|
2020-12-13 08:18:16 +00:00
|
|
|
let mut config = SectionConfig::new(&DRIVE_NAME_SCHEMA);
|
2020-12-04 14:42:32 +00:00
|
|
|
|
|
|
|
let obj_schema = match VirtualTapeDrive::API_SCHEMA {
|
|
|
|
Schema::Object(ref obj_schema) => obj_schema,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
let plugin = SectionConfigPlugin::new("virtual".to_string(), Some("name".to_string()), obj_schema);
|
|
|
|
config.register_plugin(plugin);
|
|
|
|
|
2021-03-30 15:07:59 +00:00
|
|
|
let obj_schema = match LtoTapeDrive::API_SCHEMA {
|
2020-12-04 14:42:32 +00:00
|
|
|
Schema::Object(ref obj_schema) => obj_schema,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2021-03-30 15:07:59 +00:00
|
|
|
let plugin = SectionConfigPlugin::new("lto".to_string(), Some("name".to_string()), obj_schema);
|
2020-12-04 14:42:32 +00:00
|
|
|
config.register_plugin(plugin);
|
|
|
|
|
|
|
|
let obj_schema = match ScsiTapeChanger::API_SCHEMA {
|
|
|
|
Schema::Object(ref obj_schema) => obj_schema,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
let plugin = SectionConfigPlugin::new("changer".to_string(), Some("name".to_string()), obj_schema);
|
|
|
|
config.register_plugin(plugin);
|
|
|
|
config
|
|
|
|
}
|
|
|
|
|
2021-01-22 10:51:36 +00:00
|
|
|
/// Configuration file name
|
2020-12-04 14:42:32 +00:00
|
|
|
pub const DRIVE_CFG_FILENAME: &str = "/etc/proxmox-backup/tape.cfg";
|
2021-01-22 10:51:36 +00:00
|
|
|
/// Lock file name (used to prevent concurrent access)
|
2020-12-04 14:42:32 +00:00
|
|
|
pub const DRIVE_CFG_LOCKFILE: &str = "/etc/proxmox-backup/.tape.lck";
|
|
|
|
|
2021-01-22 10:51:36 +00:00
|
|
|
/// Get exclusive lock
|
2021-07-20 11:51:54 +00:00
|
|
|
pub fn lock() -> Result<BackupLockGuard, Error> {
|
|
|
|
open_backup_lockfile(DRIVE_CFG_LOCKFILE, None, true)
|
2020-12-04 14:42:32 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 10:51:36 +00:00
|
|
|
/// Read and parse the configuration file
|
2020-12-04 14:42:32 +00:00
|
|
|
pub fn config() -> Result<(SectionConfigData, [u8;32]), Error> {
|
|
|
|
|
2021-01-19 13:04:46 +00:00
|
|
|
let content = proxmox::tools::fs::file_read_optional_string(DRIVE_CFG_FILENAME)?
|
|
|
|
.unwrap_or_else(|| "".to_string());
|
2020-12-04 14:42:32 +00:00
|
|
|
|
|
|
|
let digest = openssl::sha::sha256(content.as_bytes());
|
|
|
|
let data = CONFIG.parse(DRIVE_CFG_FILENAME, &content)?;
|
|
|
|
Ok((data, digest))
|
|
|
|
}
|
|
|
|
|
2021-01-22 10:51:36 +00:00
|
|
|
/// Save the configuration file
|
2020-12-04 14:42:32 +00:00
|
|
|
pub fn save_config(config: &SectionConfigData) -> Result<(), Error> {
|
|
|
|
let raw = CONFIG.write(DRIVE_CFG_FILENAME, &config)?;
|
|
|
|
|
|
|
|
let backup_user = crate::backup::backup_user()?;
|
|
|
|
let mode = nix::sys::stat::Mode::from_bits_truncate(0o0640);
|
|
|
|
// set the correct owner/group/permissions while saving file
|
|
|
|
// owner(rw) = root, group(r)= backup
|
|
|
|
let options = CreateOptions::new()
|
|
|
|
.perm(mode)
|
|
|
|
.owner(nix::unistd::ROOT)
|
|
|
|
.group(backup_user.gid);
|
|
|
|
|
|
|
|
replace_file(DRIVE_CFG_FILENAME, raw.as_bytes(), options)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-01-22 10:51:36 +00:00
|
|
|
/// Check if the specified drive name exists in the config.
|
2020-12-04 14:42:32 +00:00
|
|
|
pub fn check_drive_exists(config: &SectionConfigData, drive: &str) -> Result<(), Error> {
|
|
|
|
match config.sections.get(drive) {
|
|
|
|
Some((section_type, _)) => {
|
2021-03-30 15:07:59 +00:00
|
|
|
if !(section_type == "lto" || section_type == "virtual") {
|
2020-12-04 14:42:32 +00:00
|
|
|
bail!("Entry '{}' exists, but is not a tape drive", drive);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => bail!("Drive '{}' does not exist", drive),
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// shell completion helper
|
|
|
|
|
|
|
|
/// List all drive names
|
|
|
|
pub fn complete_drive_name(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
|
|
|
|
match config() {
|
|
|
|
Ok((data, _digest)) => data.sections.iter()
|
|
|
|
.map(|(id, _)| id.to_string())
|
|
|
|
.collect(),
|
|
|
|
Err(_) => return vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 15:07:59 +00:00
|
|
|
/// List Lto tape drives
|
|
|
|
pub fn complete_lto_drive_name(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
|
2020-12-04 14:42:32 +00:00
|
|
|
match config() {
|
|
|
|
Ok((data, _digest)) => data.sections.iter()
|
|
|
|
.filter(|(_id, (section_type, _))| {
|
2021-03-30 15:07:59 +00:00
|
|
|
section_type == "lto"
|
2020-12-04 14:42:32 +00:00
|
|
|
})
|
|
|
|
.map(|(id, _)| id.to_string())
|
|
|
|
.collect(),
|
|
|
|
Err(_) => return vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// List Scsi tape changer names
|
|
|
|
pub fn complete_changer_name(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
|
|
|
|
match config() {
|
|
|
|
Ok((data, _digest)) => data.sections.iter()
|
|
|
|
.filter(|(_id, (section_type, _))| {
|
|
|
|
section_type == "changer"
|
|
|
|
})
|
|
|
|
.map(|(id, _)| id.to_string())
|
|
|
|
.collect(),
|
|
|
|
Err(_) => return vec![],
|
|
|
|
}
|
|
|
|
}
|