From cd44fb8d84fc9927e81fc1a760946a58aaa3dab1 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Fri, 19 Feb 2021 17:49:50 +0100 Subject: [PATCH] tape: create tmp dirs early at server startup --- src/bin/proxmox-backup-api.rs | 2 ++ src/tape/changer/mod.rs | 8 +++---- src/tape/drive/mod.rs | 8 +++---- src/tape/mod.rs | 42 ++++++++++++++++++++++++++++++++--- 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/bin/proxmox-backup-api.rs b/src/bin/proxmox-backup-api.rs index cf61b85c..7d800259 100644 --- a/src/bin/proxmox-backup-api.rs +++ b/src/bin/proxmox-backup-api.rs @@ -39,6 +39,8 @@ async fn run() -> Result<(), Error> { proxmox_backup::rrd::create_rrdb_dir()?; proxmox_backup::server::jobstate::create_jobstate_dir()?; proxmox_backup::tape::create_tape_status_dir()?; + proxmox_backup::tape::create_drive_state_dir()?; + proxmox_backup::tape::create_changer_state_dir()?; if let Err(err) = generate_auth_key() { bail!("unable to generate auth key - {}", err); diff --git a/src/tape/changer/mod.rs b/src/tape/changer/mod.rs index e1dda621..af921728 100644 --- a/src/tape/changer/mod.rs +++ b/src/tape/changer/mod.rs @@ -463,10 +463,12 @@ fn save_changer_state_cache( changer: &str, state: &MtxStatus, ) -> Result<(), Error> { - let mut path = PathBuf::from("/run/proxmox-backup/changer-state"); - std::fs::create_dir_all(&path)?; + + let mut path = PathBuf::from(crate::tape::CHANGER_STATE_DIR); path.push(changer); + let state = serde_json::to_string_pretty(state)?; + let backup_user = crate::backup::backup_user()?; let mode = nix::sys::stat::Mode::from_bits_truncate(0o0644); let options = CreateOptions::new() @@ -474,8 +476,6 @@ fn save_changer_state_cache( .owner(backup_user.uid) .group(backup_user.gid); - let state = serde_json::to_string_pretty(state)?; - replace_file(path, state.as_bytes(), options) } diff --git a/src/tape/drive/mod.rs b/src/tape/drive/mod.rs index 4abb95de..a7e0b2c6 100644 --- a/src/tape/drive/mod.rs +++ b/src/tape/drive/mod.rs @@ -20,6 +20,7 @@ mod mam; pub use mam::*; use std::os::unix::io::AsRawFd; +use std::path::PathBuf; use anyhow::{bail, format_err, Error}; use ::serde::{Deserialize}; @@ -470,10 +471,9 @@ 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 mut path = PathBuf::from(crate::tape::DRIVE_STATE_DIR); + path.push(drive); let backup_user = crate::backup::backup_user()?; let mode = nix::sys::stat::Mode::from_bits_truncate(0o0644); diff --git a/src/tape/mod.rs b/src/tape/mod.rs index 98af0bd7..ed03d8a7 100644 --- a/src/tape/mod.rs +++ b/src/tape/mod.rs @@ -46,6 +46,12 @@ pub use pool_writer::*; /// Directory path where we store all tape status information pub const TAPE_STATUS_DIR: &str = "/var/lib/proxmox-backup/tape"; +/// Directory path where we store temporary drive state +pub const DRIVE_STATE_DIR: &str = concat!(PROXMOX_BACKUP_RUN_DIR_M!(), "/drive-state"); + +/// Directory path where we store cached changer state +pub const CHANGER_STATE_DIR: &str = concat!(PROXMOX_BACKUP_RUN_DIR_M!(), "/changer-state"); + /// We limit chunk archive size, so that we can faster restore a /// specific chunk (The catalog only store file numbers, so we /// need to read the whole archive to restore a single chunk) @@ -58,14 +64,44 @@ pub const COMMIT_BLOCK_SIZE: usize = 128*1024*1024*1024; // 128 GiB /// Create tape status dir with correct permission pub fn create_tape_status_dir() -> Result<(), Error> { let backup_user = crate::backup::backup_user()?; - let mode = nix::sys::stat::Mode::from_bits_truncate(0o0640); - let opts = CreateOptions::new() + let mode = nix::sys::stat::Mode::from_bits_truncate(0o0750); + let options = CreateOptions::new() .perm(mode) .owner(backup_user.uid) .group(backup_user.gid); - create_path(TAPE_STATUS_DIR, None, Some(opts)) + create_path(TAPE_STATUS_DIR, None, Some(options)) .map_err(|err: Error| format_err!("unable to create tape status dir - {}", err))?; Ok(()) } + +/// Create drive state dir with correct permission +pub fn create_drive_state_dir() -> Result<(), Error> { + let backup_user = crate::backup::backup_user()?; + let mode = nix::sys::stat::Mode::from_bits_truncate(0o0750); + let options = CreateOptions::new() + .perm(mode) + .owner(backup_user.uid) + .group(backup_user.gid); + + create_path(DRIVE_STATE_DIR, None, Some(options)) + .map_err(|err: Error| format_err!("unable to create drive state dir - {}", err))?; + + Ok(()) +} + +/// Create changer state cache dir with correct permission +pub fn create_changer_state_dir() -> Result<(), Error> { + let backup_user = crate::backup::backup_user()?; + let mode = nix::sys::stat::Mode::from_bits_truncate(0o0750); + let options = CreateOptions::new() + .perm(mode) + .owner(backup_user.uid) + .group(backup_user.gid); + + create_path(CHANGER_STATE_DIR, None, Some(options)) + .map_err(|err: Error| format_err!("unable to create changer state dir - {}", err))?; + + Ok(()) +}