From f1467d23f34da1e6c296abd6810d4631e2e393f1 Mon Sep 17 00:00:00 2001 From: Dominik Csapak Date: Mon, 22 Jan 2024 12:50:30 +0100 Subject: [PATCH] tape: use SgTape in sg-tape-cmd instead of LtoTapeHandle. This way, we can simply always call the binary from LtoTapeHandle, and don't have to concern ourselves with the sg_tape calling. Signed-off-by: Dominik Csapak --- src/bin/sg-tape-cmd.rs | 19 ++++++++++--------- src/tape/drive/lto/mod.rs | 10 ---------- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/src/bin/sg-tape-cmd.rs b/src/bin/sg-tape-cmd.rs index d3e4cce7..56399044 100644 --- a/src/bin/sg-tape-cmd.rs +++ b/src/bin/sg-tape-cmd.rs @@ -6,6 +6,8 @@ use std::fs::File; use std::os::unix::io::{AsRawFd, FromRawFd}; use anyhow::{bail, Error}; +use pbs_tape::sg_tape::SgTape; +use proxmox_backup::tape::encryption_keys::load_key; use serde_json::Value; use proxmox_router::{cli::*, RpcEnvironment}; @@ -19,28 +21,26 @@ use pbs_api_types::{ use pbs_tape::linux_list_drives::{check_tape_is_lto_tape_device, open_lto_tape_device}; -use proxmox_backup::tape::drive::{LtoTapeHandle, TapeDriver}; - -fn get_tape_handle(param: &Value) -> Result { +fn get_tape_handle(param: &Value) -> Result { let handle = if let Some(name) = param["drive"].as_str() { let (config, _digest) = pbs_config::drive::config()?; let drive: LtoTapeDrive = config.lookup("lto", name)?; log::info!("using device {}", drive.path); - LtoTapeHandle::open_lto_drive(&drive)? + SgTape::open_lto_drive(&drive)? } else if let Some(device) = param["device"].as_str() { log::info!("using device {}", device); - LtoTapeHandle::new(open_lto_tape_device(device)?)? + SgTape::new(open_lto_tape_device(device)?)? } else if let Some(true) = param["stdin"].as_bool() { log::info!("using stdin"); let fd = std::io::stdin().as_raw_fd(); let file = unsafe { File::from_raw_fd(fd) }; check_tape_is_lto_tape_device(&file)?; - LtoTapeHandle::new(file)? + SgTape::new(file)? } else if let Ok(name) = std::env::var("PROXMOX_TAPE_DRIVE") { let (config, _digest) = pbs_config::drive::config()?; let drive: LtoTapeDrive = config.lookup("lto", &name)?; log::info!("using device {}", drive.path); - LtoTapeHandle::open_lto_drive(&drive)? + SgTape::open_lto_drive(&drive)? } else { let (config, _digest) = pbs_config::drive::config()?; @@ -56,7 +56,7 @@ fn get_tape_handle(param: &Value) -> Result { let name = drive_names[0]; let drive: LtoTapeDrive = config.lookup("lto", name)?; log::info!("using device {}", drive.path); - LtoTapeHandle::open_lto_drive(&drive)? + SgTape::open_lto_drive(&drive)? } else { bail!("no drive/device specified"); } @@ -103,7 +103,8 @@ fn set_encryption( match (fingerprint, uuid) { (Some(fingerprint), Some(uuid)) => { - handle.set_encryption(Some((fingerprint, uuid)))?; + let key = load_key(&fingerprint)?; + handle.set_encryption(Some((key, uuid)))?; } (Some(_), None) => { bail!("missing media set uuid"); diff --git a/src/tape/drive/lto/mod.rs b/src/tape/drive/lto/mod.rs index 2a8699c0..4b113f7d 100644 --- a/src/tape/drive/lto/mod.rs +++ b/src/tape/drive/lto/mod.rs @@ -271,16 +271,6 @@ impl TapeDriver for LtoTapeHandle { &mut self, key_fingerprint: Option<(Fingerprint, Uuid)>, ) -> Result<(), Error> { - if nix::unistd::Uid::effective().is_root() { - let key_data = if let Some((ref key_fingerprint, ref uuid)) = key_fingerprint { - let key = crate::tape::encryption_keys::load_key(key_fingerprint)?; - Some((key, uuid.clone())) - } else { - None - }; - return self.sg_tape.set_encryption(key_data); - } - let output = if let Some((fingerprint, uuid)) = key_fingerprint { let fingerprint = fingerprint.signature(); run_sg_tape_cmd(