2021-01-31 15:19:53 +00:00
|
|
|
/// Control magnetic tape drive operation
|
|
|
|
///
|
2021-03-30 15:07:59 +00:00
|
|
|
/// This is a Rust implementation, using the Proxmox userspace tape
|
|
|
|
/// driver. This is meant as replacement fot the 'mt' command line
|
|
|
|
/// tool.
|
2021-01-31 15:19:53 +00:00
|
|
|
///
|
|
|
|
/// Features:
|
|
|
|
///
|
|
|
|
/// - written in Rust
|
2021-03-30 15:07:59 +00:00
|
|
|
/// - use Proxmox userspace driver (using SG_IO)
|
2021-01-31 15:19:53 +00:00
|
|
|
/// - optional json output format
|
|
|
|
/// - support tape alert flags
|
|
|
|
/// - support volume statistics
|
|
|
|
/// - read cartridge memory
|
|
|
|
|
|
|
|
use anyhow::{bail, Error};
|
|
|
|
use serde_json::Value;
|
|
|
|
|
|
|
|
use proxmox::{
|
|
|
|
api::{
|
|
|
|
api,
|
|
|
|
cli::*,
|
2021-01-31 16:33:07 +00:00
|
|
|
schema::{
|
|
|
|
Schema,
|
|
|
|
IntegerSchema,
|
2021-02-02 07:58:02 +00:00
|
|
|
StringSchema,
|
|
|
|
ArraySchema,
|
2021-01-31 16:33:07 +00:00
|
|
|
},
|
2021-01-31 15:19:53 +00:00
|
|
|
RpcEnvironment,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-01-31 16:33:07 +00:00
|
|
|
pub const FILE_MARK_COUNT_SCHEMA: Schema =
|
|
|
|
IntegerSchema::new("File mark count.")
|
|
|
|
.minimum(1)
|
2021-02-01 09:21:25 +00:00
|
|
|
.maximum(i32::MAX as isize)
|
2021-01-31 16:33:07 +00:00
|
|
|
.schema();
|
|
|
|
|
2021-02-01 09:39:04 +00:00
|
|
|
pub const RECORD_COUNT_SCHEMA: Schema =
|
|
|
|
IntegerSchema::new("Record count.")
|
|
|
|
.minimum(1)
|
|
|
|
.maximum(i32::MAX as isize)
|
|
|
|
.schema();
|
|
|
|
|
2021-02-02 07:58:02 +00:00
|
|
|
pub const DRIVE_OPTION_SCHEMA: Schema = StringSchema::new(
|
2021-03-30 15:07:59 +00:00
|
|
|
"Lto Tape Driver Option, either numeric value or option name.")
|
2021-02-02 07:58:02 +00:00
|
|
|
.schema();
|
|
|
|
|
|
|
|
pub const DRIVE_OPTION_LIST_SCHEMA: Schema =
|
|
|
|
ArraySchema::new("Drive Option List.", &DRIVE_OPTION_SCHEMA)
|
2021-02-03 07:54:12 +00:00
|
|
|
.min_length(1)
|
2021-02-02 07:58:02 +00:00
|
|
|
.schema();
|
|
|
|
|
2021-01-31 15:19:53 +00:00
|
|
|
use proxmox_backup::{
|
|
|
|
config::{
|
|
|
|
self,
|
|
|
|
drive::complete_drive_name,
|
|
|
|
},
|
|
|
|
api2::types::{
|
2021-03-30 15:07:59 +00:00
|
|
|
LTO_DRIVE_PATH_SCHEMA,
|
2021-01-31 15:19:53 +00:00
|
|
|
DRIVE_NAME_SCHEMA,
|
2021-03-30 15:07:59 +00:00
|
|
|
LtoTapeDrive,
|
2021-01-31 15:19:53 +00:00
|
|
|
},
|
|
|
|
tape::{
|
|
|
|
complete_drive_path,
|
2021-03-30 15:07:59 +00:00
|
|
|
lto_tape_device_list,
|
2021-01-31 15:19:53 +00:00
|
|
|
drive::{
|
|
|
|
TapeDriver,
|
2021-03-30 15:07:59 +00:00
|
|
|
LtoTapeHandle,
|
|
|
|
open_lto_tape_device,
|
2021-01-31 16:02:55 +00:00
|
|
|
},
|
2021-01-31 15:19:53 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-03-30 15:07:59 +00:00
|
|
|
fn get_tape_handle(param: &Value) -> Result<LtoTapeHandle, Error> {
|
2021-01-31 15:19:53 +00:00
|
|
|
|
|
|
|
if let Some(name) = param["drive"].as_str() {
|
|
|
|
let (config, _digest) = config::drive::config()?;
|
2021-03-30 15:07:59 +00:00
|
|
|
let drive: LtoTapeDrive = config.lookup("lto", &name)?;
|
2021-01-31 15:19:53 +00:00
|
|
|
eprintln!("using device {}", drive.path);
|
2021-03-30 15:07:59 +00:00
|
|
|
return LtoTapeHandle::new(open_lto_tape_device(&drive.path)?);
|
2021-01-31 15:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(device) = param["device"].as_str() {
|
|
|
|
eprintln!("using device {}", device);
|
2021-03-30 15:07:59 +00:00
|
|
|
return LtoTapeHandle::new(open_lto_tape_device(&device)?);
|
2021-01-31 15:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Ok(name) = std::env::var("PROXMOX_TAPE_DRIVE") {
|
|
|
|
let (config, _digest) = config::drive::config()?;
|
2021-03-30 15:07:59 +00:00
|
|
|
let drive: LtoTapeDrive = config.lookup("lto", &name)?;
|
2021-01-31 15:19:53 +00:00
|
|
|
eprintln!("using device {}", drive.path);
|
2021-03-30 15:07:59 +00:00
|
|
|
return LtoTapeHandle::new(open_lto_tape_device(&drive.path)?);
|
2021-01-31 15:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Ok(device) = std::env::var("TAPE") {
|
|
|
|
eprintln!("using device {}", device);
|
2021-03-30 15:07:59 +00:00
|
|
|
return LtoTapeHandle::new(open_lto_tape_device(&device)?);
|
2021-01-31 15:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let (config, _digest) = config::drive::config()?;
|
|
|
|
|
|
|
|
let mut drive_names = Vec::new();
|
|
|
|
for (name, (section_type, _)) in config.sections.iter() {
|
2021-03-30 15:07:59 +00:00
|
|
|
if section_type != "lto" { continue; }
|
2021-01-31 15:19:53 +00:00
|
|
|
drive_names.push(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if drive_names.len() == 1 {
|
|
|
|
let name = drive_names[0];
|
2021-03-30 15:07:59 +00:00
|
|
|
let drive: LtoTapeDrive = config.lookup("lto", &name)?;
|
2021-01-31 15:19:53 +00:00
|
|
|
eprintln!("using device {}", drive.path);
|
2021-03-30 15:07:59 +00:00
|
|
|
return LtoTapeHandle::new(open_lto_tape_device(&drive.path)?);
|
2021-01-31 15:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bail!("no drive/device specified");
|
|
|
|
}
|
|
|
|
|
2021-02-01 09:32:21 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
device: {
|
2021-03-30 15:07:59 +00:00
|
|
|
schema: LTO_DRIVE_PATH_SCHEMA,
|
2021-02-01 09:32:21 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
count: {
|
|
|
|
schema: FILE_MARK_COUNT_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Position the tape at the beginning of the count file.
|
|
|
|
///
|
|
|
|
/// Positioning is done by first rewinding the tape and then spacing
|
|
|
|
/// forward over count file marks.
|
2021-03-25 09:10:16 +00:00
|
|
|
fn asf(count: usize, param: Value) -> Result<(), Error> {
|
2021-02-01 09:32:21 +00:00
|
|
|
|
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
|
|
|
|
|
|
|
handle.rewind()?;
|
|
|
|
|
|
|
|
handle.forward_space_count_files(count)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-31 16:00:15 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
device: {
|
2021-03-30 15:07:59 +00:00
|
|
|
schema: LTO_DRIVE_PATH_SCHEMA,
|
2021-01-31 16:00:15 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
count: {
|
2021-01-31 16:33:07 +00:00
|
|
|
schema: FILE_MARK_COUNT_SCHEMA,
|
2021-01-31 16:00:15 +00:00
|
|
|
},
|
2021-01-31 16:33:07 +00:00
|
|
|
},
|
2021-01-31 16:00:15 +00:00
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Backward space count files (position before file mark).
|
|
|
|
///
|
|
|
|
/// The tape is positioned on the last block of the previous file.
|
2021-03-25 09:10:16 +00:00
|
|
|
fn bsf(count: usize, param: Value) -> Result<(), Error> {
|
2021-01-31 16:00:15 +00:00
|
|
|
|
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
|
|
|
|
|
|
|
handle.backward_space_count_files(count)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-02-01 09:18:18 +00:00
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
device: {
|
2021-03-30 15:07:59 +00:00
|
|
|
schema: LTO_DRIVE_PATH_SCHEMA,
|
2021-02-01 09:18:18 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
count: {
|
|
|
|
schema: FILE_MARK_COUNT_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Backward space count files, then forward space one record (position after file mark).
|
|
|
|
///
|
|
|
|
/// This leaves the tape positioned at the first block of the file
|
|
|
|
/// that is count - 1 files before the current file.
|
2021-03-30 15:07:59 +00:00
|
|
|
fn bsfm(count: usize, param: Value) -> Result<(), Error> {
|
2021-02-01 09:18:18 +00:00
|
|
|
|
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
|
|
|
|
2021-03-30 15:07:59 +00:00
|
|
|
handle.backward_space_count_files(count)?;
|
|
|
|
handle.forward_space_count_files(1)?;
|
2021-02-01 09:18:18 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-01 09:39:04 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
device: {
|
2021-03-30 15:07:59 +00:00
|
|
|
schema: LTO_DRIVE_PATH_SCHEMA,
|
2021-02-01 09:39:04 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
count: {
|
|
|
|
schema: RECORD_COUNT_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Backward space records.
|
|
|
|
fn bsr(count: i32, param: Value) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
|
|
|
|
2021-03-30 15:07:59 +00:00
|
|
|
unimplemented!();
|
|
|
|
|
|
|
|
// fixme: handle.mtop(MTCmd::MTBSR, count, "backward space records")?;
|
2021-02-01 09:39:04 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-31 15:19:53 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
device: {
|
2021-03-30 15:07:59 +00:00
|
|
|
schema: LTO_DRIVE_PATH_SCHEMA,
|
2021-01-31 15:19:53 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
"output-format": {
|
|
|
|
schema: OUTPUT_FORMAT,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Read Cartridge Memory
|
|
|
|
fn cartridge_memory(param: Value) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let output_format = get_output_format(¶m);
|
|
|
|
|
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
|
|
|
let result = handle.cartridge_memory();
|
|
|
|
|
|
|
|
if output_format == "json-pretty" {
|
|
|
|
let result = result.map_err(|err: Error| err.to_string());
|
|
|
|
println!("{}", serde_json::to_string_pretty(&result)?);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if output_format == "json" {
|
|
|
|
let result = result.map_err(|err: Error| err.to_string());
|
|
|
|
println!("{}", serde_json::to_string(&result)?);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if output_format != "text" {
|
|
|
|
bail!("unknown output format '{}'", output_format);
|
|
|
|
}
|
|
|
|
|
|
|
|
let list = result?;
|
|
|
|
|
|
|
|
for item in list {
|
|
|
|
println!("{}|{}|{}", item.id, item.name, item.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-03-08 06:37:05 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
device: {
|
2021-03-30 15:07:59 +00:00
|
|
|
schema: LTO_DRIVE_PATH_SCHEMA,
|
2021-03-08 06:37:05 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
"output-format": {
|
|
|
|
schema: OUTPUT_FORMAT,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Read Tape Alert Flags
|
|
|
|
fn tape_alert_flags(param: Value) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let output_format = get_output_format(¶m);
|
|
|
|
|
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
|
|
|
let result = handle.tape_alert_flags()
|
|
|
|
.map(|flags| format!("{:?}", flags));
|
|
|
|
|
|
|
|
if output_format == "json-pretty" {
|
|
|
|
let result = result.map_err(|err: Error| err.to_string());
|
|
|
|
println!("{}", serde_json::to_string_pretty(&result)?);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if output_format == "json" {
|
|
|
|
let result = result.map_err(|err: Error| err.to_string());
|
|
|
|
println!("{}", serde_json::to_string(&result)?);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if output_format != "text" {
|
|
|
|
bail!("unknown output format '{}'", output_format);
|
|
|
|
}
|
|
|
|
|
|
|
|
let flags = result?;
|
|
|
|
println!("Tape Alert Flags: {}", flags);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-01-31 15:19:53 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
device: {
|
2021-03-30 15:07:59 +00:00
|
|
|
schema: LTO_DRIVE_PATH_SCHEMA,
|
2021-01-31 15:19:53 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Eject drive media
|
|
|
|
fn eject(param: Value) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
|
|
|
handle.eject_media()?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
device: {
|
2021-03-30 15:07:59 +00:00
|
|
|
schema: LTO_DRIVE_PATH_SCHEMA,
|
2021-01-31 15:19:53 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Move to end of media
|
|
|
|
fn eod(param: Value) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
|
|
|
handle.move_to_eom()?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-31 15:34:10 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
device: {
|
2021-03-30 15:07:59 +00:00
|
|
|
schema: LTO_DRIVE_PATH_SCHEMA,
|
2021-01-31 15:34:10 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
fast: {
|
|
|
|
description: "Use fast erase.",
|
|
|
|
type: bool,
|
|
|
|
optional: true,
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
2021-03-31 07:19:19 +00:00
|
|
|
/// Erase media (from current position)
|
2021-01-31 15:34:10 +00:00
|
|
|
fn erase(fast: Option<bool>, param: Value) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
|
|
|
handle.erase_media(fast.unwrap_or(true))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-03-31 07:19:19 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
device: {
|
|
|
|
schema: LTO_DRIVE_PATH_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
fast: {
|
|
|
|
description: "Use fast erase.",
|
|
|
|
type: bool,
|
|
|
|
optional: true,
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Format media, single partition
|
|
|
|
fn format(fast: Option<bool>, param: Value) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
|
|
|
handle.format_media(fast.unwrap_or(true))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-01-31 15:49:48 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
device: {
|
2021-03-30 15:07:59 +00:00
|
|
|
schema: LTO_DRIVE_PATH_SCHEMA,
|
2021-01-31 15:49:48 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
count: {
|
2021-01-31 16:33:07 +00:00
|
|
|
schema: FILE_MARK_COUNT_SCHEMA,
|
2021-01-31 15:49:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Forward space count files (position after file mark).
|
|
|
|
///
|
|
|
|
/// The tape is positioned on the first block of the next file.
|
2021-03-25 09:10:16 +00:00
|
|
|
fn fsf(count: usize, param: Value) -> Result<(), Error> {
|
2021-01-31 15:49:48 +00:00
|
|
|
|
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
|
|
|
|
|
|
|
handle.forward_space_count_files(count)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-02-01 09:18:18 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
device: {
|
2021-03-30 15:07:59 +00:00
|
|
|
schema: LTO_DRIVE_PATH_SCHEMA,
|
2021-02-01 09:18:18 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
count: {
|
|
|
|
schema: FILE_MARK_COUNT_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Forward space count files, then backward space one record (position before file mark).
|
|
|
|
///
|
|
|
|
/// This leaves the tape positioned at the last block of the file that
|
|
|
|
/// is count - 1 files past the current file.
|
2021-03-30 15:07:59 +00:00
|
|
|
fn fsfm(count: usize, param: Value) -> Result<(), Error> {
|
2021-02-01 09:18:18 +00:00
|
|
|
|
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
|
|
|
|
2021-03-30 15:07:59 +00:00
|
|
|
handle.forward_space_count_files(count)?;
|
|
|
|
handle.backward_space_count_files(1)?;
|
2021-02-01 09:18:18 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-01-31 15:34:10 +00:00
|
|
|
|
2021-02-01 09:39:04 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
device: {
|
2021-03-30 15:07:59 +00:00
|
|
|
schema: LTO_DRIVE_PATH_SCHEMA,
|
2021-02-01 09:39:04 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
count: {
|
|
|
|
schema: RECORD_COUNT_SCHEMA,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Forward space records.
|
|
|
|
fn fsr(count: i32, param: Value) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
|
|
|
|
2021-03-30 15:07:59 +00:00
|
|
|
unimplemented!();
|
|
|
|
// fixme: handle.mtop(MTCmd::MTFSR, count, "forward space records")?;
|
2021-02-01 09:39:04 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-31 15:19:53 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
device: {
|
2021-03-30 15:07:59 +00:00
|
|
|
schema: LTO_DRIVE_PATH_SCHEMA,
|
2021-01-31 15:19:53 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Load media
|
|
|
|
fn load(param: Value) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
2021-03-30 15:07:59 +00:00
|
|
|
handle.load()?;
|
2021-01-31 15:19:53 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-01 11:18:20 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
device: {
|
2021-03-30 15:07:59 +00:00
|
|
|
schema: LTO_DRIVE_PATH_SCHEMA,
|
2021-02-01 11:18:20 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Lock the tape drive door
|
|
|
|
fn lock(param: Value) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
|
|
|
|
2021-03-30 15:07:59 +00:00
|
|
|
unimplemented!();
|
|
|
|
// fixme: handle.mtop(MTCmd::MTLOCK, 1, "lock tape drive door")?;
|
2021-02-01 11:18:20 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-31 15:19:53 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
device: {
|
2021-03-30 15:07:59 +00:00
|
|
|
schema: LTO_DRIVE_PATH_SCHEMA,
|
2021-01-31 15:19:53 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Rewind the tape
|
|
|
|
fn rewind(param: Value) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
|
|
|
handle.rewind()?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
"output-format": {
|
|
|
|
schema: OUTPUT_FORMAT,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Scan for existing tape changer devices
|
|
|
|
fn scan(param: Value) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let output_format = get_output_format(¶m);
|
|
|
|
|
2021-03-30 15:07:59 +00:00
|
|
|
let list = lto_tape_device_list();
|
2021-01-31 15:19:53 +00:00
|
|
|
|
|
|
|
if output_format == "json-pretty" {
|
|
|
|
println!("{}", serde_json::to_string_pretty(&list)?);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if output_format == "json" {
|
|
|
|
println!("{}", serde_json::to_string(&list)?);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if output_format != "text" {
|
|
|
|
bail!("unknown output format '{}'", output_format);
|
|
|
|
}
|
|
|
|
|
|
|
|
for item in list.iter() {
|
|
|
|
println!("{} ({}/{}/{})", item.path, item.vendor, item.model, item.serial);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
device: {
|
2021-03-30 15:07:59 +00:00
|
|
|
schema: LTO_DRIVE_PATH_SCHEMA,
|
2021-01-31 15:19:53 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
"output-format": {
|
|
|
|
schema: OUTPUT_FORMAT,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Drive Status
|
|
|
|
fn status(param: Value) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let output_format = get_output_format(¶m);
|
|
|
|
|
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
|
|
|
let result = handle.get_drive_and_media_status();
|
|
|
|
|
|
|
|
if output_format == "json-pretty" {
|
|
|
|
let result = result.map_err(|err: Error| err.to_string());
|
|
|
|
println!("{}", serde_json::to_string_pretty(&result)?);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if output_format == "json" {
|
|
|
|
let result = result.map_err(|err: Error| err.to_string());
|
|
|
|
println!("{}", serde_json::to_string(&result)?);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if output_format != "text" {
|
|
|
|
bail!("unknown output format '{}'", output_format);
|
|
|
|
}
|
|
|
|
|
|
|
|
let status = result?;
|
|
|
|
|
|
|
|
println!("{}", serde_json::to_string_pretty(&status)?);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-02-01 11:18:20 +00:00
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
device: {
|
2021-03-30 15:07:59 +00:00
|
|
|
schema: LTO_DRIVE_PATH_SCHEMA,
|
2021-02-01 11:18:20 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Unlock the tape drive door
|
|
|
|
fn unlock(param: Value) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
|
|
|
|
2021-03-30 15:07:59 +00:00
|
|
|
unimplemented!();
|
|
|
|
//handle.mtop(MTCmd::MTUNLOCK, 1, "unlock tape drive door")?;
|
2021-02-01 11:18:20 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-31 15:19:53 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
device: {
|
2021-03-30 15:07:59 +00:00
|
|
|
schema: LTO_DRIVE_PATH_SCHEMA,
|
2021-01-31 15:19:53 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
"output-format": {
|
|
|
|
schema: OUTPUT_FORMAT,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Volume Statistics
|
|
|
|
fn volume_statistics(param: Value) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let output_format = get_output_format(¶m);
|
|
|
|
|
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
|
|
|
let result = handle.volume_statistics();
|
|
|
|
|
|
|
|
if output_format == "json-pretty" {
|
|
|
|
let result = result.map_err(|err: Error| err.to_string());
|
|
|
|
println!("{}", serde_json::to_string_pretty(&result)?);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if output_format == "json" {
|
|
|
|
let result = result.map_err(|err: Error| err.to_string());
|
|
|
|
println!("{}", serde_json::to_string(&result)?);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
if output_format != "text" {
|
|
|
|
bail!("unknown output format '{}'", output_format);
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = result?;
|
|
|
|
|
|
|
|
println!("{}", serde_json::to_string_pretty(&data)?);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-01-31 16:33:07 +00:00
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
drive: {
|
|
|
|
schema: DRIVE_NAME_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
device: {
|
2021-03-30 15:07:59 +00:00
|
|
|
schema: LTO_DRIVE_PATH_SCHEMA,
|
2021-01-31 16:33:07 +00:00
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
count: {
|
|
|
|
schema: FILE_MARK_COUNT_SCHEMA,
|
|
|
|
optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
|
|
|
/// Write count (default 1) EOF marks at current position.
|
2021-03-30 15:07:59 +00:00
|
|
|
fn weof(count: Option<usize>, param: Value) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let count = count.unwrap_or(1);
|
2021-01-31 16:33:07 +00:00
|
|
|
|
|
|
|
let mut handle = get_tape_handle(¶m)?;
|
2021-03-30 15:07:59 +00:00
|
|
|
|
|
|
|
handle.write_filemarks(count)?;
|
2021-01-31 16:33:07 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-01-31 15:19:53 +00:00
|
|
|
fn main() -> Result<(), Error> {
|
|
|
|
|
|
|
|
let uid = nix::unistd::Uid::current();
|
|
|
|
|
|
|
|
let username = match nix::unistd::User::from_uid(uid)? {
|
|
|
|
Some(user) => user.name,
|
|
|
|
None => bail!("unable to get user name"),
|
|
|
|
};
|
|
|
|
|
|
|
|
let std_cmd = |method| {
|
|
|
|
CliCommand::new(method)
|
|
|
|
.completion_cb("drive", complete_drive_name)
|
|
|
|
.completion_cb("device", complete_drive_path)
|
|
|
|
};
|
|
|
|
|
|
|
|
let cmd_def = CliCommandMap::new()
|
2021-02-05 10:12:52 +00:00
|
|
|
.usage_skip_options(&["device", "drive", "output-format"])
|
2021-02-01 09:32:21 +00:00
|
|
|
.insert("asf", std_cmd(&API_METHOD_ASF).arg_param(&["count"]))
|
2021-02-01 09:18:18 +00:00
|
|
|
.insert("bsf", std_cmd(&API_METHOD_BSF).arg_param(&["count"]))
|
|
|
|
.insert("bsfm", std_cmd(&API_METHOD_BSFM).arg_param(&["count"]))
|
2021-02-01 09:39:04 +00:00
|
|
|
.insert("bsr", std_cmd(&API_METHOD_BSR).arg_param(&["count"]))
|
2021-01-31 15:19:53 +00:00
|
|
|
.insert("cartridge-memory", std_cmd(&API_METHOD_CARTRIDGE_MEMORY))
|
|
|
|
.insert("eject", std_cmd(&API_METHOD_EJECT))
|
|
|
|
.insert("eod", std_cmd(&API_METHOD_EOD))
|
2021-01-31 15:34:10 +00:00
|
|
|
.insert("erase", std_cmd(&API_METHOD_ERASE))
|
2021-03-31 07:19:19 +00:00
|
|
|
.insert("format", std_cmd(&API_METHOD_FORMAT))
|
2021-02-01 09:18:18 +00:00
|
|
|
.insert("fsf", std_cmd(&API_METHOD_FSF).arg_param(&["count"]))
|
|
|
|
.insert("fsfm", std_cmd(&API_METHOD_FSFM).arg_param(&["count"]))
|
2021-02-01 09:39:04 +00:00
|
|
|
.insert("fsr", std_cmd(&API_METHOD_FSR).arg_param(&["count"]))
|
2021-01-31 15:19:53 +00:00
|
|
|
.insert("load", std_cmd(&API_METHOD_LOAD))
|
2021-02-01 11:18:20 +00:00
|
|
|
.insert("lock", std_cmd(&API_METHOD_LOCK))
|
2021-01-31 15:19:53 +00:00
|
|
|
.insert("rewind", std_cmd(&API_METHOD_REWIND))
|
|
|
|
.insert("scan", CliCommand::new(&API_METHOD_SCAN))
|
|
|
|
.insert("status", std_cmd(&API_METHOD_STATUS))
|
2021-03-08 06:37:05 +00:00
|
|
|
.insert("tape-alert-flags", std_cmd(&API_METHOD_TAPE_ALERT_FLAGS))
|
2021-02-01 11:18:20 +00:00
|
|
|
.insert("unlock", std_cmd(&API_METHOD_UNLOCK))
|
2021-01-31 15:19:53 +00:00
|
|
|
.insert("volume-statistics", std_cmd(&API_METHOD_VOLUME_STATISTICS))
|
2021-02-01 09:18:18 +00:00
|
|
|
.insert("weof", std_cmd(&API_METHOD_WEOF).arg_param(&["count"]))
|
2021-01-31 15:19:53 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
let mut rpcenv = CliEnvironment::new();
|
|
|
|
rpcenv.set_auth_id(Some(format!("{}@pam", username)));
|
|
|
|
|
|
|
|
run_cli_command(cmd_def, rpcenv, None);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|