diff --git a/proxmox-file-restore/src/block_driver_qemu.rs b/proxmox-file-restore/src/block_driver_qemu.rs index ac61f917..4f430620 100644 --- a/proxmox-file-restore/src/block_driver_qemu.rs +++ b/proxmox-file-restore/src/block_driver_qemu.rs @@ -10,7 +10,7 @@ use serde_json::json; use proxmox_sys::fs::lock_file; -use pbs_client::{DEFAULT_VSOCK_PORT, BackupRepository, VsockClient}; +use pbs_client::{BackupRepository, VsockClient, DEFAULT_VSOCK_PORT}; use pbs_datastore::backup_info::BackupDir; use pbs_datastore::catalog::ArchiveEntry; @@ -269,10 +269,8 @@ impl BlockRestoreDriver for QemuBlockDriver { } } None => { - let err = format!( - "invalid JSON received from /status call: {}", - status - ); + let err = + format!("invalid JSON received from /status call: {}", status); extra["error"] = json!(err); } }, diff --git a/proxmox-file-restore/src/cpio.rs b/proxmox-file-restore/src/cpio.rs index c08baed0..d645f761 100644 --- a/proxmox-file-restore/src/cpio.rs +++ b/proxmox-file-restore/src/cpio.rs @@ -3,7 +3,7 @@ //! https://www.kernel.org/doc/html/latest/driver-api/early-userspace/buffer-format.html //! This does not provide full support for the format, only what is needed to include files in an //! initramfs intended for a linux kernel. -use std::ffi::{CString, CStr}; +use std::ffi::{CStr, CString}; use anyhow::{bail, Error}; use tokio::io::{copy, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}; @@ -40,7 +40,7 @@ pub async fn append_file( print_cpio_hex(&mut target, 0).await?; // c_check (ignored for newc) target.write_all(name).await?; - let header_size = 6 + 8*13 + name.len(); + let header_size = 6 + 8 * 13 + name.len(); let mut name_pad = header_size; while name_pad & 3 != 0 { target.write_u8(0).await?; @@ -70,5 +70,8 @@ pub async fn append_trailer(target: W) -> Result<(), Erro } async fn print_cpio_hex(target: &mut W, value: u64) -> Result<(), Error> { - target.write_all(format!("{:08x}", value).as_bytes()).await.map_err(|e| e.into()) + target + .write_all(format!("{:08x}", value).as_bytes()) + .await + .map_err(|e| e.into()) } diff --git a/proxmox-file-restore/src/main.rs b/proxmox-file-restore/src/main.rs index 0ada15e6..8876ecd0 100644 --- a/proxmox-file-restore/src/main.rs +++ b/proxmox-file-restore/src/main.rs @@ -6,26 +6,17 @@ use std::sync::Arc; use anyhow::{bail, format_err, Error}; use serde_json::{json, Value}; -use proxmox_sys::fs::{create_path, CreateOptions}; use proxmox_router::cli::{ - complete_file_name, default_table_format_options, - format_and_print_result_full, get_output_format, - run_cli_command, - CliCommand, CliCommandMap, CliEnvironment, ColumnConfig, OUTPUT_FORMAT, + complete_file_name, default_table_format_options, format_and_print_result_full, + get_output_format, run_cli_command, CliCommand, CliCommandMap, CliEnvironment, ColumnConfig, + OUTPUT_FORMAT, }; use proxmox_schema::api; +use proxmox_sys::fs::{create_path, CreateOptions}; use pxar::accessor::aio::Accessor; use pxar::decoder::aio::Decoder; -use pbs_tools::crypt_config::CryptConfig; use pbs_api_types::CryptMode; -use pbs_datastore::CATALOG_NAME; -use pbs_datastore::backup_info::BackupDir; -use pbs_datastore::catalog::{ArchiveEntry, CatalogReader, DirEntryAttribute}; -use pbs_datastore::dynamic_index::{BufferedDynamicReader, LocalDynamicReadAt}; -use pbs_datastore::index::IndexFile; -use pbs_config::key_config::decrypt_key; -use pbs_client::{BackupReader, RemoteChunkReader}; use pbs_client::pxar::{create_zip, extract_sub_dir, extract_sub_dir_seq}; use pbs_client::tools::{ complete_group_or_snapshot, complete_repository, connect, extract_repository_from_value, @@ -35,14 +26,22 @@ use pbs_client::tools::{ }, REPO_URL_SCHEMA, }; +use pbs_client::{BackupReader, RemoteChunkReader}; +use pbs_config::key_config::decrypt_key; +use pbs_datastore::backup_info::BackupDir; +use pbs_datastore::catalog::{ArchiveEntry, CatalogReader, DirEntryAttribute}; +use pbs_datastore::dynamic_index::{BufferedDynamicReader, LocalDynamicReadAt}; +use pbs_datastore::index::IndexFile; +use pbs_datastore::CATALOG_NAME; +use pbs_tools::crypt_config::CryptConfig; pub mod block_driver; pub use block_driver::*; pub mod cpio; -mod qemu_helper; mod block_driver_qemu; +mod qemu_helper; enum ExtractPath { ListArchives, @@ -146,12 +145,7 @@ fn keyfile_path(param: &Value) -> Option { } )] /// List a directory from a backup snapshot. -async fn list( - snapshot: String, - path: String, - base64: bool, - param: Value, -) -> Result<(), Error> { +async fn list(snapshot: String, path: String, base64: bool, param: Value) -> Result<(), Error> { let repo = extract_repository_from_value(¶m)?; let snapshot: BackupDir = snapshot.parse()?; let path = parse_path(path, base64)?; @@ -199,7 +193,11 @@ async fn list( } else { None }; - entries.push(ArchiveEntry::new_with_size(path.as_bytes(), attr, Some(file.size))); + entries.push(ArchiveEntry::new_with_size( + path.as_bytes(), + attr, + Some(file.size), + )); } Ok(entries) @@ -506,7 +504,10 @@ fn create_run_dir() -> Result<(), Error> { /// Return User info for the 'backup' user (``getpwnam_r(3)``) pub fn backup_user() -> Result { - nix::unistd::User::from_name(pbs_buildcfg::BACKUP_USER_NAME)? - .ok_or_else(|| format_err!("Unable to lookup '{}' user.", pbs_buildcfg::BACKUP_USER_NAME)) + nix::unistd::User::from_name(pbs_buildcfg::BACKUP_USER_NAME)?.ok_or_else(|| { + format_err!( + "Unable to lookup '{}' user.", + pbs_buildcfg::BACKUP_USER_NAME + ) + }) } - diff --git a/proxmox-file-restore/src/qemu_helper.rs b/proxmox-file-restore/src/qemu_helper.rs index 99c45859..b7d4eec7 100644 --- a/proxmox-file-restore/src/qemu_helper.rs +++ b/proxmox-file-restore/src/qemu_helper.rs @@ -3,7 +3,7 @@ use std::fs::{File, OpenOptions}; use std::io::prelude::*; use std::os::unix::io::AsRawFd; use std::path::PathBuf; -use std::time::{Instant, Duration}; +use std::time::{Duration, Instant}; use anyhow::{bail, format_err, Error}; use tokio::time; @@ -11,14 +11,14 @@ use tokio::time; use nix::sys::signal::{kill, Signal}; use nix::unistd::Pid; -use proxmox_sys::fs::{create_path, file_read_string, make_tmp_file, CreateOptions}; use proxmox_sys::fd::fd_change_cloexec; +use proxmox_sys::fs::{create_path, file_read_string, make_tmp_file, CreateOptions}; use proxmox_sys::logrotate::LogRotate; use pbs_client::{VsockClient, DEFAULT_VSOCK_PORT}; -use crate::{cpio, backup_user}; use super::SnapRestoreDetails; +use crate::{backup_user, cpio}; const PBS_VM_NAME: &str = "pbs-restore-vm"; const MAX_CID_TRIES: u64 = 32; @@ -142,7 +142,8 @@ pub async fn start_vm( validate_img_existance(debug)?; let pid; - let (mut pid_file, pid_path) = make_tmp_file("/tmp/file-restore-qemu.pid.tmp", CreateOptions::new())?; + let (mut pid_file, pid_path) = + make_tmp_file("/tmp/file-restore-qemu.pid.tmp", CreateOptions::new())?; nix::unistd::unlink(&pid_path)?; fd_change_cloexec(pid_file.as_raw_fd(), false)?; @@ -319,7 +320,8 @@ pub async fn start_vm( } return Ok((pid, cid as i32)); } - if kill(pid_t, None).is_err() { // check if QEMU process exited in between + if kill(pid_t, None).is_err() { + // check if QEMU process exited in between bail!("VM exited before connection could be established"); } if Instant::now().duration_since(start_poll) > Duration::from_secs(25) {