2020-12-08 08:04:56 +00:00
|
|
|
use anyhow::{bail, Error};
|
2021-01-06 10:06:50 +00:00
|
|
|
use ::serde::{Deserialize, Serialize};
|
2020-12-08 08:04:56 +00:00
|
|
|
use serde_json::Value;
|
|
|
|
|
2021-01-06 10:06:50 +00:00
|
|
|
use proxmox::api::{
|
|
|
|
api,
|
|
|
|
Router,
|
|
|
|
RpcEnvironment,
|
2021-03-03 11:10:00 +00:00
|
|
|
Permission,
|
2021-01-06 10:06:50 +00:00
|
|
|
schema::parse_property_string,
|
|
|
|
};
|
2020-12-08 08:04:56 +00:00
|
|
|
|
2021-09-03 07:10:18 +00:00
|
|
|
use pbs_api_types::{
|
2021-09-09 08:32:44 +00:00
|
|
|
Authid, ScsiTapeChanger, ScsiTapeChangerUpdater, LtoTapeDrive,
|
|
|
|
PROXMOX_CONFIG_DIGEST_SCHEMA, CHANGER_NAME_SCHEMA, SLOT_ARRAY_SCHEMA,
|
|
|
|
PRIV_TAPE_AUDIT, PRIV_TAPE_MODIFY,
|
2021-09-03 07:10:18 +00:00
|
|
|
};
|
|
|
|
|
2020-12-08 08:04:56 +00:00
|
|
|
use crate::{
|
2021-09-09 08:32:44 +00:00
|
|
|
config::cached_user_info::CachedUserInfo,
|
2021-01-21 16:25:32 +00:00
|
|
|
tape::{
|
2020-12-08 08:04:56 +00:00
|
|
|
linux_tape_changer_list,
|
|
|
|
check_drive_path,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
#[api(
|
2020-12-09 11:02:55 +00:00
|
|
|
protected: true,
|
2020-12-08 08:04:56 +00:00
|
|
|
input: {
|
|
|
|
properties: {
|
2021-09-08 07:19:35 +00:00
|
|
|
config: {
|
|
|
|
type: ScsiTapeChanger,
|
|
|
|
flatten: true,
|
2021-01-06 10:06:50 +00:00
|
|
|
},
|
2020-12-08 08:04:56 +00:00
|
|
|
},
|
|
|
|
},
|
2021-03-03 11:10:00 +00:00
|
|
|
access: {
|
2021-03-05 09:06:19 +00:00
|
|
|
permission: &Permission::Privilege(&["tape", "device"], PRIV_TAPE_MODIFY, false),
|
2021-03-03 11:10:00 +00:00
|
|
|
},
|
2020-12-08 08:04:56 +00:00
|
|
|
)]
|
|
|
|
/// Create a new changer device
|
2021-09-08 07:19:35 +00:00
|
|
|
pub fn create_changer(config: ScsiTapeChanger) -> Result<(), Error> {
|
2020-12-08 08:04:56 +00:00
|
|
|
|
2021-09-03 07:10:18 +00:00
|
|
|
let _lock = pbs_config::drive::lock()?;
|
2020-12-08 08:04:56 +00:00
|
|
|
|
2021-09-08 07:19:35 +00:00
|
|
|
let (mut section_config, _digest) = pbs_config::drive::config()?;
|
2020-12-08 08:04:56 +00:00
|
|
|
|
|
|
|
let linux_changers = linux_tape_changer_list();
|
|
|
|
|
2021-09-08 07:19:35 +00:00
|
|
|
check_drive_path(&linux_changers, &config.path)?;
|
2020-12-08 08:04:56 +00:00
|
|
|
|
2021-09-08 07:19:35 +00:00
|
|
|
let existing: Vec<ScsiTapeChanger> = section_config.convert_to_typed_array("changer")?;
|
2021-01-28 11:59:44 +00:00
|
|
|
|
|
|
|
for changer in existing {
|
2021-09-08 07:19:35 +00:00
|
|
|
if changer.name == config.name {
|
|
|
|
bail!("Entry '{}' already exists", config.name);
|
2021-01-28 11:59:44 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 07:19:35 +00:00
|
|
|
if changer.path == config.path {
|
|
|
|
bail!("Path '{}' already in use by '{}'", config.path, changer.name);
|
2021-01-28 11:59:44 +00:00
|
|
|
}
|
2020-12-08 08:04:56 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 07:19:35 +00:00
|
|
|
section_config.set_data(&config.name, "changer", &config)?;
|
2020-12-08 08:04:56 +00:00
|
|
|
|
2021-09-08 07:19:35 +00:00
|
|
|
pbs_config::drive::save_config(§ion_config)?;
|
2020-12-08 08:04:56 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
name: {
|
2020-12-13 08:22:08 +00:00
|
|
|
schema: CHANGER_NAME_SCHEMA,
|
2020-12-08 08:04:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
type: ScsiTapeChanger,
|
|
|
|
},
|
2021-03-03 11:10:00 +00:00
|
|
|
access: {
|
2021-03-05 09:06:19 +00:00
|
|
|
permission: &Permission::Privilege(&["tape", "device", "{name}"], PRIV_TAPE_AUDIT, false),
|
2021-03-03 11:10:00 +00:00
|
|
|
},
|
2020-12-08 08:04:56 +00:00
|
|
|
)]
|
|
|
|
/// Get tape changer configuration
|
|
|
|
pub fn get_config(
|
|
|
|
name: String,
|
|
|
|
_param: Value,
|
|
|
|
mut rpcenv: &mut dyn RpcEnvironment,
|
|
|
|
) -> Result<ScsiTapeChanger, Error> {
|
|
|
|
|
2021-09-03 07:10:18 +00:00
|
|
|
let (config, digest) = pbs_config::drive::config()?;
|
2020-12-08 08:04:56 +00:00
|
|
|
|
|
|
|
let data: ScsiTapeChanger = config.lookup("changer", &name)?;
|
|
|
|
|
|
|
|
rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into();
|
|
|
|
|
|
|
|
Ok(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {},
|
|
|
|
},
|
|
|
|
returns: {
|
|
|
|
description: "The list of configured changers (with config digest).",
|
|
|
|
type: Array,
|
|
|
|
items: {
|
2021-01-30 08:36:54 +00:00
|
|
|
type: ScsiTapeChanger,
|
2020-12-08 08:04:56 +00:00
|
|
|
},
|
|
|
|
},
|
2021-03-03 11:10:00 +00:00
|
|
|
access: {
|
|
|
|
description: "List configured tape changer filtered by Tape.Audit privileges",
|
|
|
|
permission: &Permission::Anybody,
|
|
|
|
},
|
2020-12-08 08:04:56 +00:00
|
|
|
)]
|
|
|
|
/// List changers
|
|
|
|
pub fn list_changers(
|
|
|
|
_param: Value,
|
|
|
|
mut rpcenv: &mut dyn RpcEnvironment,
|
2021-01-30 08:36:54 +00:00
|
|
|
) -> Result<Vec<ScsiTapeChanger>, Error> {
|
2021-03-03 11:10:00 +00:00
|
|
|
let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
|
|
|
|
let user_info = CachedUserInfo::new()?;
|
2020-12-08 08:04:56 +00:00
|
|
|
|
2021-09-03 07:10:18 +00:00
|
|
|
let (config, digest) = pbs_config::drive::config()?;
|
2020-12-08 08:04:56 +00:00
|
|
|
|
2021-01-30 08:36:54 +00:00
|
|
|
let list: Vec<ScsiTapeChanger> = config.convert_to_typed_array("changer")?;
|
2020-12-08 08:04:56 +00:00
|
|
|
|
2021-03-03 11:10:00 +00:00
|
|
|
let list = list
|
|
|
|
.into_iter()
|
|
|
|
.filter(|changer| {
|
2021-03-05 09:06:19 +00:00
|
|
|
let privs = user_info.lookup_privs(&auth_id, &["tape", "device", &changer.name]);
|
2021-03-03 11:10:00 +00:00
|
|
|
privs & PRIV_TAPE_AUDIT != 0
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2020-12-08 08:04:56 +00:00
|
|
|
rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into();
|
2021-01-30 08:36:54 +00:00
|
|
|
|
2020-12-08 08:04:56 +00:00
|
|
|
Ok(list)
|
|
|
|
}
|
2021-01-06 10:06:50 +00:00
|
|
|
#[api()]
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
#[serde(rename_all = "kebab-case")]
|
|
|
|
/// Deletable property name
|
|
|
|
pub enum DeletableProperty {
|
|
|
|
/// Delete export-slots.
|
|
|
|
export_slots,
|
|
|
|
}
|
2020-12-08 08:04:56 +00:00
|
|
|
|
|
|
|
#[api(
|
2020-12-09 11:02:55 +00:00
|
|
|
protected: true,
|
2020-12-08 08:04:56 +00:00
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
name: {
|
2020-12-13 08:22:08 +00:00
|
|
|
schema: CHANGER_NAME_SCHEMA,
|
2020-12-08 08:04:56 +00:00
|
|
|
},
|
2021-09-08 07:19:35 +00:00
|
|
|
update: {
|
|
|
|
type: ScsiTapeChangerUpdater,
|
|
|
|
flatten: true,
|
2021-01-06 10:06:50 +00:00
|
|
|
},
|
|
|
|
delete: {
|
|
|
|
description: "List of properties to delete.",
|
|
|
|
type: Array,
|
|
|
|
optional: true,
|
|
|
|
items: {
|
|
|
|
type: DeletableProperty,
|
|
|
|
},
|
|
|
|
},
|
2020-12-08 10:24:38 +00:00
|
|
|
digest: {
|
|
|
|
schema: PROXMOX_CONFIG_DIGEST_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
2020-12-08 08:04:56 +00:00
|
|
|
},
|
2021-03-03 11:10:00 +00:00
|
|
|
access: {
|
2021-03-05 09:06:19 +00:00
|
|
|
permission: &Permission::Privilege(&["tape", "device", "{name}"], PRIV_TAPE_MODIFY, false),
|
2021-03-03 11:10:00 +00:00
|
|
|
},
|
2020-12-08 08:04:56 +00:00
|
|
|
)]
|
|
|
|
/// Update a tape changer configuration
|
|
|
|
pub fn update_changer(
|
|
|
|
name: String,
|
2021-09-08 07:19:35 +00:00
|
|
|
update: ScsiTapeChangerUpdater,
|
2021-01-06 10:06:50 +00:00
|
|
|
delete: Option<Vec<DeletableProperty>>,
|
2020-12-08 10:24:38 +00:00
|
|
|
digest: Option<String>,
|
2020-12-08 08:04:56 +00:00
|
|
|
_param: Value,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
2021-09-03 07:10:18 +00:00
|
|
|
let _lock = pbs_config::drive::lock()?;
|
2020-12-08 08:04:56 +00:00
|
|
|
|
2021-09-03 07:10:18 +00:00
|
|
|
let (mut config, expected_digest) = pbs_config::drive::config()?;
|
2020-12-08 10:24:38 +00:00
|
|
|
|
|
|
|
if let Some(ref digest) = digest {
|
|
|
|
let digest = proxmox::tools::hex_to_digest(digest)?;
|
|
|
|
crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?;
|
|
|
|
}
|
2020-12-08 08:04:56 +00:00
|
|
|
|
|
|
|
let mut data: ScsiTapeChanger = config.lookup("changer", &name)?;
|
|
|
|
|
2021-01-06 10:06:50 +00:00
|
|
|
if let Some(delete) = delete {
|
|
|
|
for delete_prop in delete {
|
|
|
|
match delete_prop {
|
|
|
|
DeletableProperty::export_slots => {
|
|
|
|
data.export_slots = None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 07:19:35 +00:00
|
|
|
if let Some(path) = update.path {
|
2020-12-08 08:04:56 +00:00
|
|
|
let changers = linux_tape_changer_list();
|
|
|
|
check_drive_path(&changers, &path)?;
|
|
|
|
data.path = path;
|
|
|
|
}
|
|
|
|
|
2021-09-08 07:19:35 +00:00
|
|
|
if let Some(export_slots) = update.export_slots {
|
2021-01-06 10:06:50 +00:00
|
|
|
let slots: Value = parse_property_string(
|
|
|
|
&export_slots, &SLOT_ARRAY_SCHEMA
|
|
|
|
)?;
|
|
|
|
let mut slots: Vec<String> = slots
|
|
|
|
.as_array()
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.map(|v| v.to_string())
|
|
|
|
.collect();
|
|
|
|
slots.sort();
|
|
|
|
|
|
|
|
if slots.is_empty() {
|
|
|
|
data.export_slots = None;
|
|
|
|
} else {
|
|
|
|
let slots = slots.join(",");
|
|
|
|
data.export_slots = Some(slots);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 08:04:56 +00:00
|
|
|
config.set_data(&name, "changer", &data)?;
|
|
|
|
|
2021-09-03 07:10:18 +00:00
|
|
|
pbs_config::drive::save_config(&config)?;
|
2020-12-08 08:04:56 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
2020-12-09 11:02:55 +00:00
|
|
|
protected: true,
|
2020-12-08 08:04:56 +00:00
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
name: {
|
2020-12-13 08:22:08 +00:00
|
|
|
schema: CHANGER_NAME_SCHEMA,
|
2020-12-08 08:04:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-03-03 11:10:00 +00:00
|
|
|
access: {
|
2021-03-05 09:06:19 +00:00
|
|
|
permission: &Permission::Privilege(&["tape", "device", "{name}"], PRIV_TAPE_MODIFY, false),
|
2021-03-03 11:10:00 +00:00
|
|
|
},
|
2020-12-08 08:04:56 +00:00
|
|
|
)]
|
|
|
|
/// Delete a tape changer configuration
|
|
|
|
pub fn delete_changer(name: String, _param: Value) -> Result<(), Error> {
|
|
|
|
|
2021-09-03 07:10:18 +00:00
|
|
|
let _lock = pbs_config::drive::lock()?;
|
2020-12-08 08:04:56 +00:00
|
|
|
|
2021-09-03 07:10:18 +00:00
|
|
|
let (mut config, _digest) = pbs_config::drive::config()?;
|
2020-12-08 08:04:56 +00:00
|
|
|
|
|
|
|
match config.sections.get(&name) {
|
|
|
|
Some((section_type, _)) => {
|
|
|
|
if section_type != "changer" {
|
|
|
|
bail!("Entry '{}' exists, but is not a changer device", name);
|
|
|
|
}
|
|
|
|
config.sections.remove(&name);
|
|
|
|
},
|
|
|
|
None => bail!("Delete changer '{}' failed - no such entry", name),
|
|
|
|
}
|
|
|
|
|
2021-03-30 15:07:59 +00:00
|
|
|
let drive_list: Vec<LtoTapeDrive> = config.convert_to_typed_array("lto")?;
|
2020-12-09 11:55:54 +00:00
|
|
|
for drive in drive_list {
|
|
|
|
if let Some(changer) = drive.changer {
|
|
|
|
if changer == name {
|
|
|
|
bail!("Delete changer '{}' failed - used by drive '{}'", name, drive.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-03 07:10:18 +00:00
|
|
|
pbs_config::drive::save_config(&config)?;
|
2020-12-08 08:04:56 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
const ITEM_ROUTER: Router = Router::new()
|
|
|
|
.get(&API_METHOD_GET_CONFIG)
|
|
|
|
.put(&API_METHOD_UPDATE_CHANGER)
|
|
|
|
.delete(&API_METHOD_DELETE_CHANGER);
|
|
|
|
|
|
|
|
|
|
|
|
pub const ROUTER: Router = Router::new()
|
|
|
|
.get(&API_METHOD_LIST_CHANGERS)
|
|
|
|
.post(&API_METHOD_CREATE_CHANGER)
|
|
|
|
.match_all("name", &ITEM_ROUTER);
|