2020-12-28 10:10:25 +00:00
|
|
|
/// Tape command implemented using scsi-generic raw commands
|
|
|
|
///
|
|
|
|
/// SCSI-generic command needs root priviledges, so this binary need
|
|
|
|
/// to be setuid root.
|
|
|
|
///
|
|
|
|
/// This command can use STDIN as tape device handle.
|
|
|
|
|
|
|
|
use std::fs::File;
|
|
|
|
use std::os::unix::io::{AsRawFd, FromRawFd};
|
|
|
|
|
2021-01-18 12:36:11 +00:00
|
|
|
use anyhow::{bail, Error};
|
2021-01-14 12:05:26 +00:00
|
|
|
use serde_json::Value;
|
2020-12-28 10:10:25 +00:00
|
|
|
|
|
|
|
use proxmox::{
|
|
|
|
api::{
|
|
|
|
api,
|
|
|
|
cli::*,
|
|
|
|
RpcEnvironment,
|
|
|
|
},
|
2021-01-20 16:27:01 +00:00
|
|
|
tools::Uuid,
|
2020-12-28 10:10:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
use proxmox_backup::{
|
2021-01-14 12:05:26 +00:00
|
|
|
config,
|
2021-01-18 06:16:06 +00:00
|
|
|
backup::Fingerprint,
|
2020-12-28 10:10:25 +00:00
|
|
|
api2::types::{
|
|
|
|
LINUX_DRIVE_PATH_SCHEMA,
|
2021-01-14 12:05:26 +00:00
|
|
|
DRIVE_NAME_SCHEMA,
|
2021-01-18 06:16:06 +00:00
|
|
|
TAPE_ENCRYPTION_KEY_FINGERPRINT_SCHEMA,
|
2021-01-20 16:27:01 +00:00
|
|
|
MEDIA_SET_UUID_SCHEMA,
|
2021-01-14 12:05:26 +00:00
|
|
|
LinuxTapeDrive,
|
2020-12-28 10:10:25 +00:00
|
|
|
},
|
|
|
|
tape::{
|
2021-01-21 16:12:01 +00:00
|
|
|
drive::{
|
|
|
|
TapeDriver,
|
2020-12-28 10:10:25 +00:00
|
|
|
LinuxTapeHandle,
|
|
|
|
open_linux_tape_device,
|
|
|
|
check_tape_is_linux_tape_device,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-01-14 12:05:26 +00:00
|
|
|
fn get_tape_handle(param: &Value) -> Result<LinuxTapeHandle, Error> {
|
|
|
|
|
|
|
|
let handle = if let Some(name) = param["drive"].as_str() {
|
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
let drive: LinuxTapeDrive = config.lookup("linux", &name)?;
|
|
|
|
eprintln!("using device {}", drive.path);
|
2021-01-18 06:16:06 +00:00
|
|
|
drive.open()?
|
2021-01-14 12:05:26 +00:00
|
|
|
} else if let Some(device) = param["device"].as_str() {
|
|
|
|
eprintln!("using device {}", device);
|
|
|
|
LinuxTapeHandle::new(open_linux_tape_device(&device)?)
|
|
|
|
} else if let Some(true) = param["stdin"].as_bool() {
|
|
|
|
eprintln!("using stdin");
|
2020-12-28 10:10:25 +00:00
|
|
|
let fd = std::io::stdin().as_raw_fd();
|
|
|
|
let file = unsafe { File::from_raw_fd(fd) };
|
|
|
|
check_tape_is_linux_tape_device(&file)?;
|
2021-01-14 12:05:26 +00:00
|
|
|
LinuxTapeHandle::new(file)
|
2021-01-19 10:37:49 +00:00
|
|
|
} else if let Ok(name) = std::env::var("PROXMOX_TAPE_DRIVE") {
|
2021-01-14 12:05:26 +00:00
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
let drive: LinuxTapeDrive = config.lookup("linux", &name)?;
|
|
|
|
eprintln!("using device {}", drive.path);
|
2021-01-18 06:16:06 +00:00
|
|
|
drive.open()?
|
2021-01-14 12:05:26 +00:00
|
|
|
} else {
|
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
|
|
|
|
let mut drive_names = Vec::new();
|
|
|
|
for (name, (section_type, _)) in config.sections.iter() {
|
|
|
|
if section_type != "linux" { continue; }
|
|
|
|
drive_names.push(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if drive_names.len() == 1 {
|
|
|
|
let name = drive_names[0];
|
|
|
|
let drive: LinuxTapeDrive = config.lookup("linux", &name)?;
|
|
|
|
eprintln!("using device {}", drive.path);
|
2021-01-18 06:16:06 +00:00
|
|
|
drive.open()?
|
2021-01-14 12:05:26 +00:00
|
|
|
} else {
|
|
|
|
bail!("no drive/device specified");
|
|
|
|
}
|
2020-12-28 10:10:25 +00:00
|
|
|
};
|
2021-01-14 12:05:26 +00:00
|
|
|
|
|
|
|
Ok(handle)
|
2020-12-28 10:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
2021-01-14 12:05:26 +00:00
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
2020-12-28 10:10:25 +00:00
|
|
|
device: {
|
|
|
|
schema: LINUX_DRIVE_PATH_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
2021-01-14 12:05:26 +00:00
|
|
|
stdin: {
|
|
|
|
description: "Use standard input as device handle.",
|
|
|
|
type: bool,
|
|
|
|
optional: true,
|
|
|
|
},
|
2020-12-28 10:10:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Tape/Media Status
|
|
|
|
fn status(
|
2021-01-14 12:05:26 +00:00
|
|
|
param: Value,
|
2020-12-28 10:10:25 +00:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let result = proxmox::try_block!({
|
2021-01-14 12:05:26 +00:00
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
2020-12-29 07:39:06 +00:00
|
|
|
handle.get_drive_and_media_status()
|
|
|
|
}).map_err(|err: Error| err.to_string());
|
2020-12-28 10:10:25 +00:00
|
|
|
|
|
|
|
println!("{}", serde_json::to_string_pretty(&result)?);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
2021-01-14 12:05:26 +00:00
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
2020-12-28 10:10:25 +00:00
|
|
|
device: {
|
|
|
|
schema: LINUX_DRIVE_PATH_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
2021-01-14 12:05:26 +00:00
|
|
|
stdin: {
|
|
|
|
description: "Use standard input as device handle.",
|
|
|
|
type: bool,
|
|
|
|
optional: true,
|
|
|
|
},
|
2020-12-28 10:10:25 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Read Cartridge Memory (Medium auxiliary memory attributes)
|
|
|
|
fn cartridge_memory(
|
2021-01-14 12:05:26 +00:00
|
|
|
param: Value,
|
2020-12-28 10:10:25 +00:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let result = proxmox::try_block!({
|
2021-01-14 12:05:26 +00:00
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
2020-12-28 10:10:25 +00:00
|
|
|
|
|
|
|
handle.cartridge_memory()
|
|
|
|
}).map_err(|err| err.to_string());
|
|
|
|
|
|
|
|
println!("{}", serde_json::to_string_pretty(&result)?);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-01-03 09:09:43 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
2021-01-14 12:05:26 +00:00
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
2021-01-03 09:09:43 +00:00
|
|
|
device: {
|
|
|
|
schema: LINUX_DRIVE_PATH_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
2021-01-14 12:05:26 +00:00
|
|
|
stdin: {
|
|
|
|
description: "Use standard input as device handle.",
|
|
|
|
type: bool,
|
|
|
|
optional: true,
|
|
|
|
},
|
2021-01-03 09:09:43 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Read Tape Alert Flags
|
|
|
|
fn tape_alert_flags(
|
2021-01-14 12:05:26 +00:00
|
|
|
param: Value,
|
2021-01-03 09:09:43 +00:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let result = proxmox::try_block!({
|
2021-01-14 12:05:26 +00:00
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
2021-01-03 09:09:43 +00:00
|
|
|
|
|
|
|
let flags = handle.tape_alert_flags()?;
|
|
|
|
Ok(flags.bits())
|
|
|
|
}).map_err(|err: Error| err.to_string());
|
|
|
|
|
|
|
|
println!("{}", serde_json::to_string_pretty(&result)?);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-01-18 06:16:06 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
fingerprint: {
|
|
|
|
schema: TAPE_ENCRYPTION_KEY_FINGERPRINT_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
2021-01-20 16:27:01 +00:00
|
|
|
uuid: {
|
|
|
|
schema: MEDIA_SET_UUID_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
2021-01-18 06:16:06 +00:00
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
device: {
|
|
|
|
schema: LINUX_DRIVE_PATH_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
stdin: {
|
|
|
|
description: "Use standard input as device handle.",
|
|
|
|
type: bool,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Set or clear encryption key
|
|
|
|
fn set_encryption(
|
|
|
|
fingerprint: Option<Fingerprint>,
|
2021-01-20 16:27:01 +00:00
|
|
|
uuid: Option<Uuid>,
|
2021-01-18 06:16:06 +00:00
|
|
|
param: Value,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let result = proxmox::try_block!({
|
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
|
|
|
|
2021-01-20 16:27:01 +00:00
|
|
|
match (fingerprint, uuid) {
|
|
|
|
(Some(fingerprint), Some(uuid)) => {
|
|
|
|
handle.set_encryption(Some((fingerprint, uuid)))?;
|
|
|
|
}
|
|
|
|
(Some(_), None) => {
|
|
|
|
bail!("missing media set uuid");
|
|
|
|
}
|
|
|
|
(None, _) => {
|
|
|
|
handle.set_encryption(None)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 06:16:06 +00:00
|
|
|
Ok(())
|
|
|
|
}).map_err(|err: Error| err.to_string());
|
|
|
|
|
|
|
|
println!("{}", serde_json::to_string_pretty(&result)?);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-01-05 11:58:18 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
2021-01-14 12:05:26 +00:00
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
2021-01-05 11:58:18 +00:00
|
|
|
device: {
|
|
|
|
schema: LINUX_DRIVE_PATH_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
2021-01-14 12:05:26 +00:00
|
|
|
stdin: {
|
|
|
|
description: "Use standard input as device handle.",
|
|
|
|
type: bool,
|
|
|
|
optional: true,
|
|
|
|
},
|
2021-01-05 11:58:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Read volume statistics
|
|
|
|
fn volume_statistics(
|
2021-01-14 12:05:26 +00:00
|
|
|
param: Value,
|
2021-01-05 11:58:18 +00:00
|
|
|
) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let result = proxmox::try_block!({
|
2021-01-14 12:05:26 +00:00
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
2021-01-05 11:58:18 +00:00
|
|
|
handle.volume_statistics()
|
|
|
|
}).map_err(|err: Error| err.to_string());
|
|
|
|
|
|
|
|
println!("{}", serde_json::to_string_pretty(&result)?);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-12-28 10:10:25 +00:00
|
|
|
fn main() -> Result<(), Error> {
|
|
|
|
|
|
|
|
// check if we are user root or backup
|
|
|
|
let backup_uid = proxmox_backup::backup::backup_user()?.uid;
|
|
|
|
let backup_gid = proxmox_backup::backup::backup_group()?.gid;
|
|
|
|
let running_uid = nix::unistd::Uid::current();
|
|
|
|
let running_gid = nix::unistd::Gid::current();
|
|
|
|
|
|
|
|
let effective_uid = nix::unistd::Uid::effective();
|
|
|
|
if !effective_uid.is_root() {
|
|
|
|
bail!("this program needs to be run with setuid root");
|
|
|
|
}
|
|
|
|
|
2021-01-19 10:06:26 +00:00
|
|
|
if !running_uid.is_root() && (running_uid != backup_uid || running_gid != backup_gid) {
|
|
|
|
bail!(
|
|
|
|
"Not running as backup user or group (got uid {} gid {})",
|
|
|
|
running_uid, running_gid,
|
|
|
|
);
|
2020-12-28 10:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let cmd_def = CliCommandMap::new()
|
|
|
|
.insert(
|
|
|
|
"status",
|
|
|
|
CliCommand::new(&API_METHOD_STATUS)
|
|
|
|
)
|
|
|
|
.insert(
|
|
|
|
"cartridge-memory",
|
|
|
|
CliCommand::new(&API_METHOD_CARTRIDGE_MEMORY)
|
|
|
|
)
|
2021-01-03 09:09:43 +00:00
|
|
|
.insert(
|
|
|
|
"tape-alert-flags",
|
|
|
|
CliCommand::new(&API_METHOD_TAPE_ALERT_FLAGS)
|
|
|
|
)
|
2021-01-05 11:58:18 +00:00
|
|
|
.insert(
|
|
|
|
"volume-statistics",
|
|
|
|
CliCommand::new(&API_METHOD_VOLUME_STATISTICS)
|
|
|
|
)
|
2021-01-18 06:16:06 +00:00
|
|
|
.insert(
|
|
|
|
"encryption",
|
|
|
|
CliCommand::new(&API_METHOD_SET_ENCRYPTION)
|
|
|
|
)
|
2020-12-28 10:10:25 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
let mut rpcenv = CliEnvironment::new();
|
|
|
|
rpcenv.set_auth_id(Some(String::from("root@pam")));
|
|
|
|
|
|
|
|
run_cli_command(cmd_def, rpcenv, None);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|