From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id D1F346B3F8 for ; Thu, 18 Feb 2021 15:41:02 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id A6CE5F669 for ; Thu, 18 Feb 2021 15:40:32 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 496A4F62A for ; Thu, 18 Feb 2021 15:40:31 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id DBCFF41FCC for ; Thu, 18 Feb 2021 15:40:30 +0100 (CET) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Thu, 18 Feb 2021 15:40:21 +0100 Message-Id: <20210218144030.16778-2-d.csapak@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210218144030.16778-1-d.csapak@proxmox.com> References: <20210218144030.16778-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.217 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH proxmox-backup 02/11] tape/drive: add get/set status functions X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 18 Feb 2021 14:41:02 -0000 simply writes into/reads from a file in /run, we will use this for writing the upid (or potential other states) per drive Signed-off-by: Dominik Csapak --- src/tape/drive/mod.rs | 56 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/src/tape/drive/mod.rs b/src/tape/drive/mod.rs index faa89953..4abb95de 100644 --- a/src/tape/drive/mod.rs +++ b/src/tape/drive/mod.rs @@ -29,7 +29,12 @@ use proxmox::{ tools::{ Uuid, io::ReadExt, - fs::fchown, + fs::{ + fchown, + file_read_optional_string, + replace_file, + CreateOptions, + } }, api::section_config::SectionConfigData, }; @@ -453,7 +458,53 @@ pub fn lock_tape_device( config: &SectionConfigData, drive: &str, ) -> Result { + let path = tape_device_path(config, drive)?; + lock_device_path(&path) + .map_err(|err| format_err!("unable to lock drive '{}' - {}", drive, err)) +} + +/// Writes the given state for the specified drive +/// +/// This function does not lock, so make sure the drive is locked +pub fn set_tape_device_state( + drive: &str, + state: &str, +) -> Result<(), Error> { + let mut path = "/run/proxmox-backup/drive-state".to_string(); + std::fs::create_dir_all(&path)?; + use std::fmt::Write; + write!(path, "/{}", drive)?; + + let backup_user = crate::backup::backup_user()?; + let mode = nix::sys::stat::Mode::from_bits_truncate(0o0644); + let options = CreateOptions::new() + .perm(mode) + .owner(backup_user.uid) + .group(backup_user.gid); + + replace_file(path, state.as_bytes(), options) +} +/// Get the device state +pub fn get_tape_device_state( + config: &SectionConfigData, + drive: &str, +) -> Result, Error> { + let path = format!("/run/proxmox-backup/drive-state/{}", drive); + let state = file_read_optional_string(path)?; + + let device_path = tape_device_path(config, drive)?; + if test_device_path_lock(&device_path)? { + Ok(state) + } else { + Ok(None) + } +} + +fn tape_device_path( + config: &SectionConfigData, + drive: &str, +) -> Result { match config.sections.get(drive) { Some((section_type_name, config)) => { let path = match section_type_name.as_ref() { @@ -465,8 +516,7 @@ pub fn lock_tape_device( } _ => bail!("unknown drive type '{}' - internal error"), }; - lock_device_path(&path) - .map_err(|err| format_err!("unable to lock drive '{}' - {}", drive, err)) + Ok(path) } None => { bail!("no such drive '{}'", drive); -- 2.20.1