2021-01-21 12:19:07 +00:00
|
|
|
//! Driver for Linux SCSI tapes
|
|
|
|
|
2020-12-07 07:27:15 +00:00
|
|
|
use std::fs::{OpenOptions, File};
|
|
|
|
use std::os::unix::fs::OpenOptionsExt;
|
2020-12-28 10:10:25 +00:00
|
|
|
use std::os::unix::io::{AsRawFd, FromRawFd};
|
2020-12-07 07:27:15 +00:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
|
|
|
use anyhow::{bail, format_err, Error};
|
|
|
|
use nix::fcntl::{fcntl, FcntlArg, OFlag};
|
|
|
|
|
|
|
|
use proxmox::sys::error::SysResult;
|
2021-01-20 16:27:01 +00:00
|
|
|
use proxmox::tools::Uuid;
|
2020-12-07 07:27:15 +00:00
|
|
|
|
|
|
|
use crate::{
|
2021-01-18 06:16:06 +00:00
|
|
|
config,
|
2021-01-19 05:19:18 +00:00
|
|
|
backup::{
|
|
|
|
Fingerprint,
|
|
|
|
KeyConfig,
|
|
|
|
},
|
2020-12-28 10:10:25 +00:00
|
|
|
tools::run_command,
|
2020-12-22 09:42:22 +00:00
|
|
|
api2::types::{
|
|
|
|
TapeDensity,
|
2020-12-23 08:44:53 +00:00
|
|
|
MamAttribute,
|
2020-12-29 07:39:06 +00:00
|
|
|
LinuxDriveAndMediaStatus,
|
2020-12-22 09:42:22 +00:00
|
|
|
},
|
2020-12-07 07:27:15 +00:00
|
|
|
tape::{
|
|
|
|
TapeRead,
|
|
|
|
TapeWrite,
|
|
|
|
drive::{
|
2021-01-21 16:12:01 +00:00
|
|
|
linux_mtio::*,
|
2020-12-07 07:27:15 +00:00
|
|
|
LinuxTapeDrive,
|
|
|
|
TapeDriver,
|
2021-01-21 16:12:01 +00:00
|
|
|
TapeAlertFlags,
|
|
|
|
Lp17VolumeStatistics,
|
|
|
|
read_mam_attributes,
|
|
|
|
mam_extract_media_usage,
|
|
|
|
read_tape_alert_flags,
|
|
|
|
read_volume_statistics,
|
|
|
|
set_encryption,
|
2020-12-07 07:27:15 +00:00
|
|
|
},
|
|
|
|
file_formats::{
|
|
|
|
PROXMOX_TAPE_BLOCK_SIZE,
|
|
|
|
MediaSetLabel,
|
|
|
|
MediaContentHeader,
|
|
|
|
PROXMOX_BACKUP_MEDIA_SET_LABEL_MAGIC_1_0,
|
|
|
|
},
|
|
|
|
helpers::{
|
|
|
|
BlockedReader,
|
|
|
|
BlockedWriter,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-01-21 12:19:07 +00:00
|
|
|
/// Linux tape drive status
|
2020-12-07 07:27:15 +00:00
|
|
|
#[derive(Debug)]
|
2020-12-22 09:42:22 +00:00
|
|
|
pub struct LinuxDriveStatus {
|
2021-01-21 12:19:07 +00:00
|
|
|
/// Size 0 is variable block size mode (default)
|
2020-12-07 07:27:15 +00:00
|
|
|
pub blocksize: u32,
|
2021-01-21 12:19:07 +00:00
|
|
|
/// Drive status flags
|
2020-12-07 07:27:15 +00:00
|
|
|
pub status: GMTStatusFlags,
|
2021-01-21 12:19:07 +00:00
|
|
|
/// Tape densitiy code (if drive media loaded)
|
2020-12-29 08:10:30 +00:00
|
|
|
pub density: Option<TapeDensity>,
|
2021-01-21 12:19:07 +00:00
|
|
|
/// Current file position if known (or -1)
|
2020-12-29 08:10:30 +00:00
|
|
|
pub file_number: Option<u32>,
|
2021-01-21 12:19:07 +00:00
|
|
|
/// Current block number if known (or -1)
|
2020-12-29 08:10:30 +00:00
|
|
|
pub block_number: Option<u32>,
|
2020-12-07 07:27:15 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 09:42:22 +00:00
|
|
|
impl LinuxDriveStatus {
|
2020-12-07 07:27:15 +00:00
|
|
|
pub fn tape_is_ready(&self) -> bool {
|
|
|
|
self.status.contains(GMTStatusFlags::ONLINE) &&
|
|
|
|
!self.status.contains(GMTStatusFlags::DRIVE_OPEN)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LinuxTapeDrive {
|
|
|
|
|
2020-12-25 12:45:26 +00:00
|
|
|
/// Open a tape device
|
|
|
|
///
|
|
|
|
/// This does additional checks:
|
|
|
|
///
|
2020-12-25 14:38:29 +00:00
|
|
|
/// - check if it is a non-rewinding tape device
|
2020-12-25 12:45:26 +00:00
|
|
|
/// - check if drive is ready (tape loaded)
|
|
|
|
/// - check block size
|
2021-01-12 08:49:05 +00:00
|
|
|
/// - for autoloader only, try to reload ejected tapes
|
2020-12-07 07:27:15 +00:00
|
|
|
pub fn open(&self) -> Result<LinuxTapeHandle, Error> {
|
|
|
|
|
2021-01-15 10:43:17 +00:00
|
|
|
proxmox::try_block!({
|
|
|
|
let file = open_linux_tape_device(&self.path)?;
|
2020-12-07 07:27:15 +00:00
|
|
|
|
2021-01-15 10:43:17 +00:00
|
|
|
let mut handle = LinuxTapeHandle::new(file);
|
2020-12-07 07:27:15 +00:00
|
|
|
|
2021-01-15 10:43:17 +00:00
|
|
|
let mut drive_status = handle.get_drive_status()?;
|
2021-01-12 08:49:05 +00:00
|
|
|
|
2021-01-15 10:43:17 +00:00
|
|
|
if !drive_status.tape_is_ready() {
|
|
|
|
// for autoloader only, try to reload ejected tapes
|
|
|
|
if self.changer.is_some() {
|
|
|
|
let _ = handle.mtload(); // just try, ignore error
|
|
|
|
drive_status = handle.get_drive_status()?;
|
|
|
|
}
|
2021-01-12 08:49:05 +00:00
|
|
|
}
|
2021-01-15 10:43:17 +00:00
|
|
|
|
|
|
|
if !drive_status.tape_is_ready() {
|
|
|
|
bail!("tape not ready (no tape loaded)");
|
|
|
|
}
|
|
|
|
|
|
|
|
if drive_status.blocksize == 0 {
|
|
|
|
// device is variable block size - OK
|
2021-01-19 10:06:26 +00:00
|
|
|
} else if drive_status.blocksize != PROXMOX_TAPE_BLOCK_SIZE as u32 {
|
|
|
|
eprintln!("device is in fixed block size mode with wrong size ({} bytes)", drive_status.blocksize);
|
|
|
|
eprintln!("trying to set variable block size mode...");
|
|
|
|
if handle.set_block_size(0).is_err() {
|
|
|
|
bail!("set variable block size mod failed - device uses wrong blocksize.");
|
2021-01-15 10:43:17 +00:00
|
|
|
}
|
2021-01-19 10:06:26 +00:00
|
|
|
} else {
|
|
|
|
// device is in fixed block size mode with correct block size
|
2020-12-07 07:27:15 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 10:43:17 +00:00
|
|
|
// Only root can set driver options, so we cannot
|
|
|
|
// handle.set_default_options()?;
|
2020-12-07 07:27:15 +00:00
|
|
|
|
2021-01-15 10:43:17 +00:00
|
|
|
Ok(handle)
|
|
|
|
}).map_err(|err| format_err!("open drive '{}' ({}) failed - {}", self.name, self.path, err))
|
2020-12-07 07:27:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-25 12:45:26 +00:00
|
|
|
/// Linux Tape device handle
|
2020-12-07 07:27:15 +00:00
|
|
|
pub struct LinuxTapeHandle {
|
|
|
|
file: File,
|
|
|
|
//_lock: File,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LinuxTapeHandle {
|
|
|
|
|
2020-12-25 12:45:26 +00:00
|
|
|
/// Creates a new instance
|
|
|
|
pub fn new(file: File) -> Self {
|
|
|
|
Self { file }
|
2020-12-07 07:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set all options we need/want
|
|
|
|
pub fn set_default_options(&self) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let mut opts = SetDrvBufferOptions::empty();
|
|
|
|
|
|
|
|
// fixme: ? man st(4) claims we need to clear this for reliable multivolume
|
|
|
|
opts.set(SetDrvBufferOptions::MT_ST_BUFFER_WRITES, true);
|
|
|
|
|
|
|
|
// fixme: ?man st(4) claims we need to clear this for reliable multivolume
|
|
|
|
opts.set(SetDrvBufferOptions::MT_ST_ASYNC_WRITES, true);
|
|
|
|
|
|
|
|
opts.set(SetDrvBufferOptions::MT_ST_READ_AHEAD, true);
|
|
|
|
|
|
|
|
self.set_drive_buffer_options(opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// call MTSETDRVBUFFER to set boolean options
|
|
|
|
///
|
|
|
|
/// Note: this uses MT_ST_BOOLEANS, so missing options are cleared!
|
|
|
|
pub fn set_drive_buffer_options(&self, opts: SetDrvBufferOptions) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let cmd = mtop {
|
|
|
|
mt_op: MTCmd::MTSETDRVBUFFER,
|
|
|
|
mt_count: (SetDrvBufferCmd::MT_ST_BOOLEANS as i32) | opts.bits(),
|
|
|
|
};
|
|
|
|
unsafe {
|
|
|
|
mtioctop(self.file.as_raw_fd(), &cmd)
|
|
|
|
}.map_err(|err| format_err!("MTSETDRVBUFFER options failed - {}", err))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This flushes the driver's buffer as a side effect. Should be
|
|
|
|
/// used before reading status with MTIOCGET.
|
|
|
|
fn mtnop(&self) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let cmd = mtop { mt_op: MTCmd::MTNOP, mt_count: 1, };
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
mtioctop(self.file.as_raw_fd(), &cmd)
|
|
|
|
}.map_err(|err| format_err!("MTNOP failed - {}", err))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-01-12 08:49:05 +00:00
|
|
|
fn mtload(&mut self) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let cmd = mtop { mt_op: MTCmd::MTLOAD, mt_count: 1, };
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
mtioctop(self.file.as_raw_fd(), &cmd)
|
|
|
|
}.map_err(|err| format_err!("MTLOAD failed - {}", err))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:57:49 +00:00
|
|
|
fn forward_space_count_files(&mut self, count: i32) -> Result<(), Error> {
|
2020-12-18 11:07:22 +00:00
|
|
|
|
|
|
|
let cmd = mtop { mt_op: MTCmd::MTFSF, mt_count: count, };
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
mtioctop(self.file.as_raw_fd(), &cmd)
|
|
|
|
}.map_err(|err| format_err!("tape fsf {} failed - {}", count, err))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-12-07 07:27:15 +00:00
|
|
|
/// Set tape compression feature
|
|
|
|
pub fn set_compression(&self, on: bool) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let cmd = mtop { mt_op: MTCmd::MTCOMPRESSION, mt_count: if on { 1 } else { 0 } };
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
mtioctop(self.file.as_raw_fd(), &cmd)
|
|
|
|
}.map_err(|err| format_err!("set compression to {} failed - {}", on, err))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Write a single EOF mark
|
|
|
|
pub fn write_eof_mark(&self) -> Result<(), Error> {
|
|
|
|
tape_write_eof_mark(&self.file)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the drive's block length to the value specified.
|
|
|
|
///
|
|
|
|
/// A block length of zero sets the drive to variable block
|
|
|
|
/// size mode.
|
|
|
|
pub fn set_block_size(&self, block_length: usize) -> Result<(), Error> {
|
|
|
|
|
|
|
|
if block_length > 256*1024*1024 {
|
|
|
|
bail!("block_length too large (> max linux scsii block length)");
|
|
|
|
}
|
|
|
|
|
|
|
|
let cmd = mtop { mt_op: MTCmd::MTSETBLK, mt_count: block_length as i32 };
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
mtioctop(self.file.as_raw_fd(), &cmd)
|
|
|
|
}.map_err(|err| format_err!("MTSETBLK failed - {}", err))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-12-29 07:39:06 +00:00
|
|
|
/// Get Tape and Media status
|
|
|
|
pub fn get_drive_and_media_status(&mut self) -> Result<LinuxDriveAndMediaStatus, Error> {
|
2020-12-07 07:27:15 +00:00
|
|
|
|
2020-12-29 07:39:06 +00:00
|
|
|
let drive_status = self.get_drive_status()?;
|
|
|
|
|
2021-01-04 12:15:30 +00:00
|
|
|
let alert_flags = self.tape_alert_flags()
|
|
|
|
.map(|flags| format!("{:?}", flags))
|
|
|
|
.ok();
|
|
|
|
|
2020-12-29 07:39:06 +00:00
|
|
|
let mut status = LinuxDriveAndMediaStatus {
|
|
|
|
blocksize: drive_status.blocksize,
|
|
|
|
density: drive_status.density,
|
|
|
|
status: format!("{:?}", drive_status.status),
|
2021-01-04 12:15:30 +00:00
|
|
|
alert_flags,
|
2020-12-29 07:39:06 +00:00
|
|
|
file_number: drive_status.file_number,
|
|
|
|
block_number: drive_status.block_number,
|
|
|
|
manufactured: None,
|
|
|
|
bytes_read: None,
|
|
|
|
bytes_written: None,
|
2021-01-05 12:43:17 +00:00
|
|
|
medium_passes: None,
|
2021-01-22 07:06:25 +00:00
|
|
|
medium_wearout: None,
|
2021-01-05 12:43:17 +00:00
|
|
|
volume_mounts: None,
|
2020-12-29 07:39:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if drive_status.tape_is_ready() {
|
|
|
|
|
2021-01-04 12:15:30 +00:00
|
|
|
if let Ok(mam) = self.cartridge_memory() {
|
2020-12-29 07:39:06 +00:00
|
|
|
|
2021-01-04 12:15:30 +00:00
|
|
|
let usage = mam_extract_media_usage(&mam)?;
|
2020-12-29 07:39:06 +00:00
|
|
|
|
2021-01-04 12:15:30 +00:00
|
|
|
status.manufactured = Some(usage.manufactured);
|
|
|
|
status.bytes_read = Some(usage.bytes_read);
|
|
|
|
status.bytes_written = Some(usage.bytes_written);
|
2021-01-05 12:43:17 +00:00
|
|
|
|
2021-01-06 08:20:36 +00:00
|
|
|
if let Ok(volume_stats) = self.volume_statistics() {
|
2021-01-05 12:43:17 +00:00
|
|
|
|
2021-01-22 07:06:25 +00:00
|
|
|
let passes = std::cmp::max(
|
2021-01-06 08:20:36 +00:00
|
|
|
volume_stats.beginning_of_medium_passes,
|
|
|
|
volume_stats.middle_of_tape_passes,
|
2021-01-22 07:06:25 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// assume max. 16000 medium passes
|
|
|
|
// see: https://en.wikipedia.org/wiki/Linear_Tape-Open
|
|
|
|
let wearout: f64 = (passes as f64)/(16000.0 as f64);
|
|
|
|
|
|
|
|
status.medium_passes = Some(passes);
|
|
|
|
status.medium_wearout = Some(wearout);
|
2021-01-05 12:43:17 +00:00
|
|
|
|
2021-01-06 08:20:36 +00:00
|
|
|
status.volume_mounts = Some(volume_stats.volume_mounts);
|
|
|
|
}
|
2021-01-05 12:43:17 +00:00
|
|
|
}
|
2020-12-29 07:39:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get Tape status/configuration with MTIOCGET ioctl
|
|
|
|
pub fn get_drive_status(&mut self) -> Result<LinuxDriveStatus, Error> {
|
|
|
|
|
|
|
|
let _ = self.mtnop(); // ignore errors (i.e. no tape loaded)
|
2020-12-07 07:27:15 +00:00
|
|
|
|
|
|
|
let mut status = mtget::default();
|
|
|
|
|
|
|
|
if let Err(err) = unsafe { mtiocget(self.file.as_raw_fd(), &mut status) } {
|
|
|
|
bail!("MTIOCGET failed - {}", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
let gmt = GMTStatusFlags::from_bits_truncate(status.mt_gstat);
|
|
|
|
|
|
|
|
let blocksize;
|
|
|
|
|
|
|
|
if status.mt_type == MT_TYPE_ISSCSI1 || status.mt_type == MT_TYPE_ISSCSI2 {
|
|
|
|
blocksize = ((status.mt_dsreg & MT_ST_BLKSIZE_MASK) >> MT_ST_BLKSIZE_SHIFT) as u32;
|
|
|
|
} else {
|
|
|
|
bail!("got unsupported tape type {}", status.mt_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
let density = ((status.mt_dsreg & MT_ST_DENSITY_MASK) >> MT_ST_DENSITY_SHIFT) as u8;
|
|
|
|
|
2020-12-22 09:42:22 +00:00
|
|
|
Ok(LinuxDriveStatus {
|
2020-12-07 07:27:15 +00:00
|
|
|
blocksize,
|
|
|
|
status: gmt,
|
2020-12-29 08:10:30 +00:00
|
|
|
density: if density != 0 {
|
|
|
|
Some(TapeDensity::try_from(density)?)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
file_number: if status.mt_fileno > 0 {
|
|
|
|
Some(status.mt_fileno as u32)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
block_number: if status.mt_blkno > 0 {
|
|
|
|
Some(status.mt_blkno as u32)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
2020-12-07 07:27:15 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-23 08:44:53 +00:00
|
|
|
/// Read Cartridge Memory (MAM Attributes)
|
2020-12-28 10:10:25 +00:00
|
|
|
///
|
|
|
|
/// Note: Only 'root' user may run RAW SG commands, so we need to
|
|
|
|
/// spawn setuid binary 'sg-tape-cmd'.
|
2020-12-23 08:44:53 +00:00
|
|
|
pub fn cartridge_memory(&mut self) -> Result<Vec<MamAttribute>, Error> {
|
2020-12-28 10:10:25 +00:00
|
|
|
|
|
|
|
if nix::unistd::Uid::effective().is_root() {
|
|
|
|
return read_mam_attributes(&mut self.file);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut command = std::process::Command::new(
|
|
|
|
"/usr/lib/x86_64-linux-gnu/proxmox-backup/sg-tape-cmd");
|
|
|
|
command.args(&["cartridge-memory"]);
|
2021-01-14 12:05:26 +00:00
|
|
|
command.args(&["--stdin"]);
|
2020-12-28 10:10:25 +00:00
|
|
|
command.stdin(unsafe { std::process::Stdio::from_raw_fd(self.file.as_raw_fd())});
|
|
|
|
let output = run_command(command, None)?;
|
|
|
|
let result: Result<Vec<MamAttribute>, String> = serde_json::from_str(&output)?;
|
|
|
|
result.map_err(|err| format_err!("{}", err))
|
2020-12-23 08:44:53 +00:00
|
|
|
}
|
2021-01-03 09:09:43 +00:00
|
|
|
|
2021-01-05 11:58:18 +00:00
|
|
|
/// Read Volume Statistics
|
|
|
|
///
|
|
|
|
/// Note: Only 'root' user may run RAW SG commands, so we need to
|
|
|
|
/// spawn setuid binary 'sg-tape-cmd'.
|
|
|
|
pub fn volume_statistics(&mut self) -> Result<Lp17VolumeStatistics, Error> {
|
|
|
|
|
|
|
|
if nix::unistd::Uid::effective().is_root() {
|
|
|
|
return read_volume_statistics(&mut self.file);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut command = std::process::Command::new(
|
|
|
|
"/usr/lib/x86_64-linux-gnu/proxmox-backup/sg-tape-cmd");
|
|
|
|
command.args(&["volume-statistics"]);
|
2021-01-14 12:05:26 +00:00
|
|
|
command.args(&["--stdin"]);
|
2021-01-05 11:58:18 +00:00
|
|
|
command.stdin(unsafe { std::process::Stdio::from_raw_fd(self.file.as_raw_fd())});
|
|
|
|
let output = run_command(command, None)?;
|
|
|
|
let result: Result<Lp17VolumeStatistics, String> = serde_json::from_str(&output)?;
|
|
|
|
result.map_err(|err| format_err!("{}", err))
|
|
|
|
}
|
2020-12-07 07:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl TapeDriver for LinuxTapeHandle {
|
|
|
|
|
|
|
|
fn sync(&mut self) -> Result<(), Error> {
|
|
|
|
|
|
|
|
println!("SYNC/FLUSH TAPE");
|
|
|
|
// MTWEOF with count 0 => flush
|
|
|
|
let cmd = mtop { mt_op: MTCmd::MTWEOF, mt_count: 0 };
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
mtioctop(self.file.as_raw_fd(), &cmd)
|
|
|
|
}.map_err(|err| proxmox::io_format_err!("MT sync failed - {}", err))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Go to the end of the recorded media (for appending files).
|
|
|
|
fn move_to_eom(&mut self) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let cmd = mtop { mt_op: MTCmd::MTEOM, mt_count: 1, };
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
mtioctop(self.file.as_raw_fd(), &cmd)
|
|
|
|
}.map_err(|err| format_err!("MTEOM failed - {}", err))?;
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rewind(&mut self) -> Result<(), Error> {
|
|
|
|
|
|
|
|
let cmd = mtop { mt_op: MTCmd::MTREW, mt_count: 1, };
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
mtioctop(self.file.as_raw_fd(), &cmd)
|
|
|
|
}.map_err(|err| format_err!("tape rewind failed - {}", err))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-12-18 06:44:50 +00:00
|
|
|
fn current_file_number(&mut self) -> Result<u64, Error> {
|
2020-12-07 07:27:15 +00:00
|
|
|
let mut status = mtget::default();
|
|
|
|
|
|
|
|
self.mtnop()?;
|
|
|
|
|
|
|
|
if let Err(err) = unsafe { mtiocget(self.file.as_raw_fd(), &mut status) } {
|
|
|
|
bail!("current_file_number MTIOCGET failed - {}", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if status.mt_fileno < 0 {
|
|
|
|
bail!("current_file_number failed (got {})", status.mt_fileno);
|
|
|
|
}
|
2020-12-18 06:44:50 +00:00
|
|
|
Ok(status.mt_fileno as u64)
|
2020-12-07 07:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn erase_media(&mut self, fast: bool) -> Result<(), Error> {
|
|
|
|
|
|
|
|
self.rewind()?; // important - erase from BOT
|
|
|
|
|
|
|
|
let cmd = mtop { mt_op: MTCmd::MTERASE, mt_count: if fast { 0 } else { 1 } };
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
mtioctop(self.file.as_raw_fd(), &cmd)
|
|
|
|
}.map_err(|err| format_err!("MTERASE failed - {}", err))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_next_file<'a>(&'a mut self) -> Result<Option<Box<dyn TapeRead + 'a>>, std::io::Error> {
|
|
|
|
match BlockedReader::open(&mut self.file)? {
|
|
|
|
Some(reader) => Ok(Some(Box::new(reader))),
|
|
|
|
None => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_file<'a>(&'a mut self) -> Result<Box<dyn TapeWrite + 'a>, std::io::Error> {
|
|
|
|
|
|
|
|
let handle = TapeWriterHandle {
|
|
|
|
writer: BlockedWriter::new(&mut self.file),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Box::new(handle))
|
|
|
|
}
|
|
|
|
|
2021-01-19 05:19:18 +00:00
|
|
|
fn write_media_set_label(
|
|
|
|
&mut self,
|
|
|
|
media_set_label: &MediaSetLabel,
|
|
|
|
key_config: Option<&KeyConfig>,
|
|
|
|
) -> Result<(), Error> {
|
2020-12-07 07:27:15 +00:00
|
|
|
|
2020-12-18 11:07:22 +00:00
|
|
|
let file_number = self.current_file_number()?;
|
|
|
|
if file_number != 1 {
|
|
|
|
self.rewind()?;
|
2020-12-18 15:57:49 +00:00
|
|
|
self.forward_space_count_files(1)?; // skip label
|
2020-12-18 11:07:22 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 07:27:15 +00:00
|
|
|
let file_number = self.current_file_number()?;
|
|
|
|
if file_number != 1 {
|
|
|
|
bail!("write_media_set_label failed - got wrong file number ({} != 1)", file_number);
|
|
|
|
}
|
|
|
|
|
2021-01-18 06:42:50 +00:00
|
|
|
self.set_encryption(None)?;
|
|
|
|
|
2020-12-07 07:27:15 +00:00
|
|
|
let mut handle = TapeWriterHandle {
|
|
|
|
writer: BlockedWriter::new(&mut self.file),
|
|
|
|
};
|
2021-01-19 05:19:18 +00:00
|
|
|
|
|
|
|
let mut value = serde_json::to_value(media_set_label)?;
|
|
|
|
if media_set_label.encryption_key_fingerprint.is_some() {
|
|
|
|
match key_config {
|
|
|
|
Some(key_config) => {
|
|
|
|
value["key-config"] = serde_json::to_value(key_config)?;
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
bail!("missing encryption key config");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let raw = serde_json::to_string_pretty(&value)?;
|
2020-12-07 07:27:15 +00:00
|
|
|
|
|
|
|
let header = MediaContentHeader::new(PROXMOX_BACKUP_MEDIA_SET_LABEL_MAGIC_1_0, raw.len() as u32);
|
|
|
|
handle.write_header(&header, raw.as_bytes())?;
|
|
|
|
handle.finish(false)?;
|
|
|
|
|
|
|
|
self.sync()?; // sync data to tape
|
|
|
|
|
2020-12-16 12:27:53 +00:00
|
|
|
Ok(())
|
2020-12-07 07:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Rewind and put the drive off line (Eject media).
|
|
|
|
fn eject_media(&mut self) -> Result<(), Error> {
|
|
|
|
let cmd = mtop { mt_op: MTCmd::MTOFFL, mt_count: 1 };
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
mtioctop(self.file.as_raw_fd(), &cmd)
|
|
|
|
}.map_err(|err| format_err!("MTOFFL failed - {}", err))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-01-09 11:34:00 +00:00
|
|
|
|
|
|
|
/// Read Tape Alert Flags
|
|
|
|
///
|
|
|
|
/// Note: Only 'root' user may run RAW SG commands, so we need to
|
|
|
|
/// spawn setuid binary 'sg-tape-cmd'.
|
|
|
|
fn tape_alert_flags(&mut self) -> Result<TapeAlertFlags, Error> {
|
|
|
|
|
|
|
|
if nix::unistd::Uid::effective().is_root() {
|
|
|
|
return read_tape_alert_flags(&mut self.file);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut command = std::process::Command::new(
|
|
|
|
"/usr/lib/x86_64-linux-gnu/proxmox-backup/sg-tape-cmd");
|
|
|
|
command.args(&["tape-alert-flags"]);
|
2021-01-14 12:05:26 +00:00
|
|
|
command.args(&["--stdin"]);
|
2021-01-09 11:34:00 +00:00
|
|
|
command.stdin(unsafe { std::process::Stdio::from_raw_fd(self.file.as_raw_fd())});
|
|
|
|
let output = run_command(command, None)?;
|
|
|
|
let result: Result<u64, String> = serde_json::from_str(&output)?;
|
|
|
|
result
|
|
|
|
.map_err(|err| format_err!("{}", err))
|
2021-01-15 14:21:34 +00:00
|
|
|
.map(TapeAlertFlags::from_bits_truncate)
|
2021-01-09 11:34:00 +00:00
|
|
|
}
|
2021-01-18 06:16:06 +00:00
|
|
|
|
|
|
|
/// Set or clear encryption key
|
|
|
|
///
|
|
|
|
/// Note: Only 'root' user may run RAW SG commands, so we need to
|
|
|
|
/// spawn setuid binary 'sg-tape-cmd'. Also, encryption key file
|
|
|
|
/// is only readable by root.
|
2021-01-20 16:27:01 +00:00
|
|
|
fn set_encryption(
|
|
|
|
&mut self,
|
|
|
|
key_fingerprint: Option<(Fingerprint, Uuid)>,
|
|
|
|
) -> Result<(), Error> {
|
2021-01-18 06:16:06 +00:00
|
|
|
|
|
|
|
if nix::unistd::Uid::effective().is_root() {
|
|
|
|
|
2021-01-20 16:27:01 +00:00
|
|
|
if let Some((ref key_fingerprint, ref uuid)) = key_fingerprint {
|
2021-01-18 06:16:06 +00:00
|
|
|
|
|
|
|
let (key_map, _digest) = config::tape_encryption_keys::load_keys()?;
|
|
|
|
match key_map.get(key_fingerprint) {
|
|
|
|
Some(item) => {
|
2021-01-20 16:27:01 +00:00
|
|
|
|
|
|
|
// derive specialized key for each media-set
|
|
|
|
|
|
|
|
let mut tape_key = [0u8; 32];
|
|
|
|
|
|
|
|
let uuid_bytes: [u8; 16] = uuid.as_bytes().clone();
|
|
|
|
|
|
|
|
openssl::pkcs5::pbkdf2_hmac(
|
|
|
|
&item.key,
|
|
|
|
&uuid_bytes,
|
|
|
|
10,
|
|
|
|
openssl::hash::MessageDigest::sha256(),
|
|
|
|
&mut tape_key)?;
|
|
|
|
|
|
|
|
return set_encryption(&mut self.file, Some(tape_key));
|
2021-01-18 06:16:06 +00:00
|
|
|
}
|
|
|
|
None => bail!("unknown tape encryption key '{}'", key_fingerprint),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return set_encryption(&mut self.file, None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut command = std::process::Command::new(
|
|
|
|
"/usr/lib/x86_64-linux-gnu/proxmox-backup/sg-tape-cmd");
|
|
|
|
command.args(&["encryption"]);
|
2021-01-20 16:27:01 +00:00
|
|
|
if let Some((fingerprint, uuid)) = key_fingerprint {
|
2021-01-18 06:16:06 +00:00
|
|
|
let fingerprint = crate::tools::format::as_fingerprint(fingerprint.bytes());
|
|
|
|
command.args(&["--fingerprint", &fingerprint]);
|
2021-01-20 16:27:01 +00:00
|
|
|
command.args(&["--uuid", &uuid.to_string()]);
|
2021-01-18 06:16:06 +00:00
|
|
|
}
|
|
|
|
command.args(&["--stdin"]);
|
|
|
|
command.stdin(unsafe { std::process::Stdio::from_raw_fd(self.file.as_raw_fd())});
|
|
|
|
let output = run_command(command, None)?;
|
|
|
|
let result: Result<(), String> = serde_json::from_str(&output)?;
|
|
|
|
result.map_err(|err| format_err!("{}", err))
|
|
|
|
}
|
2020-12-07 07:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Write a single EOF mark without flushing buffers
|
|
|
|
fn tape_write_eof_mark(file: &File) -> Result<(), std::io::Error> {
|
|
|
|
|
|
|
|
println!("WRITE EOF MARK");
|
|
|
|
let cmd = mtop { mt_op: MTCmd::MTWEOFI, mt_count: 1 };
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
mtioctop(file.as_raw_fd(), &cmd)
|
|
|
|
}.map_err(|err| proxmox::io_format_err!("MTWEOFI failed - {}", err))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-12-25 12:45:26 +00:00
|
|
|
/// Check for correct Major/Minor numbers
|
2020-12-24 14:51:49 +00:00
|
|
|
pub fn check_tape_is_linux_tape_device(file: &File) -> Result<(), Error> {
|
2020-12-07 07:27:15 +00:00
|
|
|
|
2020-12-24 14:51:49 +00:00
|
|
|
let stat = nix::sys::stat::fstat(file.as_raw_fd())?;
|
|
|
|
|
|
|
|
let devnum = stat.st_rdev;
|
2020-12-07 07:27:15 +00:00
|
|
|
|
|
|
|
let major = unsafe { libc::major(devnum) };
|
|
|
|
let minor = unsafe { libc::minor(devnum) };
|
|
|
|
|
2020-12-25 14:38:29 +00:00
|
|
|
if major != 9 {
|
2020-12-24 14:51:49 +00:00
|
|
|
bail!("not a tape device");
|
|
|
|
}
|
2020-12-07 07:27:15 +00:00
|
|
|
if (minor & 128) == 0 {
|
2020-12-24 14:51:49 +00:00
|
|
|
bail!("Detected rewinding tape. Please use non-rewinding tape devices (/dev/nstX).");
|
2020-12-07 07:27:15 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 14:51:49 +00:00
|
|
|
Ok(())
|
2020-12-07 07:27:15 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 10:24:45 +00:00
|
|
|
/// Opens a Linux tape device
|
|
|
|
///
|
|
|
|
/// The open call use O_NONBLOCK, but that flag is cleard after open
|
|
|
|
/// succeeded. This also checks if the device is a non-rewinding tape
|
|
|
|
/// device.
|
|
|
|
pub fn open_linux_tape_device(
|
|
|
|
path: &str,
|
|
|
|
) -> Result<File, Error> {
|
|
|
|
|
|
|
|
let file = OpenOptions::new()
|
|
|
|
.read(true)
|
|
|
|
.write(true)
|
|
|
|
.custom_flags(libc::O_NONBLOCK)
|
|
|
|
.open(path)?;
|
|
|
|
|
|
|
|
// clear O_NONBLOCK from now on.
|
|
|
|
|
|
|
|
let flags = fcntl(file.as_raw_fd(), FcntlArg::F_GETFL)
|
|
|
|
.into_io_result()?;
|
|
|
|
|
|
|
|
let mut flags = OFlag::from_bits_truncate(flags);
|
|
|
|
flags.remove(OFlag::O_NONBLOCK);
|
|
|
|
|
|
|
|
fcntl(file.as_raw_fd(), FcntlArg::F_SETFL(flags))
|
|
|
|
.into_io_result()?;
|
|
|
|
|
2020-12-24 14:51:49 +00:00
|
|
|
check_tape_is_linux_tape_device(&file)
|
|
|
|
.map_err(|err| format_err!("device type check {:?} failed - {}", path, err))?;
|
2020-12-24 10:24:45 +00:00
|
|
|
|
|
|
|
Ok(file)
|
|
|
|
}
|
|
|
|
|
2020-12-07 07:27:15 +00:00
|
|
|
/// like BlockedWriter, but writes EOF mark on finish
|
|
|
|
pub struct TapeWriterHandle<'a> {
|
|
|
|
writer: BlockedWriter<&'a mut File>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TapeWrite for TapeWriterHandle<'_> {
|
|
|
|
|
|
|
|
fn write_all(&mut self, data: &[u8]) -> Result<bool, std::io::Error> {
|
|
|
|
self.writer.write_all(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bytes_written(&self) -> usize {
|
|
|
|
self.writer.bytes_written()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn finish(&mut self, incomplete: bool) -> Result<bool, std::io::Error> {
|
|
|
|
println!("FINISH TAPE HANDLE");
|
|
|
|
let leof = self.writer.finish(incomplete)?;
|
|
|
|
tape_write_eof_mark(self.writer.writer_ref_mut())?;
|
|
|
|
Ok(leof)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn logical_end_of_media(&self) -> bool {
|
|
|
|
self.writer.logical_end_of_media()
|
|
|
|
}
|
|
|
|
}
|