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 3C7C0603D8 for ; Wed, 7 Oct 2020 13:53:19 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id BF3742E307 for ; Wed, 7 Oct 2020 13:53:18 +0200 (CEST) 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 3401C2E282 for ; Wed, 7 Oct 2020 13:53:15 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id F2C8745C32 for ; Wed, 7 Oct 2020 13:53:14 +0200 (CEST) From: Stefan Reiter To: pbs-devel@lists.proxmox.com Date: Wed, 7 Oct 2020 13:53:06 +0200 Message-Id: <20201007115308.6275-6-s.reiter@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20201007115308.6275-1-s.reiter@proxmox.com> References: <20201007115308.6275-1-s.reiter@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.040 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 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [mount.rs] Subject: [pbs-devel] [PATCH proxmox-backup 5/7] fuse_loop: add automatic cleanup of run files and dangling instances 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: Wed, 07 Oct 2020 11:53:19 -0000 A 'map' call will only clean up what it needs, that is only leftover files or dangling instances of it's own name. For a full cleanup the user can call 'unmap' without any arguments. The 'cleanup on error' behaviour of map_loop is removed. It is no longer needed (since the next call will clean up anyway), and in fact fixes a bug where trying to map an image twice would result in an error, but also cleanup the .pid file of the running instance, causing 'unmap' to fail afterwards. Signed-off-by: Stefan Reiter --- src/bin/proxmox_backup_client/mount.rs | 4 +- src/tools/fuse_loop.rs | 88 ++++++++++++++++---------- 2 files changed, 59 insertions(+), 33 deletions(-) diff --git a/src/bin/proxmox_backup_client/mount.rs b/src/bin/proxmox_backup_client/mount.rs index ad06cba4..8d4c7133 100644 --- a/src/bin/proxmox_backup_client/mount.rs +++ b/src/bin/proxmox_backup_client/mount.rs @@ -83,7 +83,8 @@ const API_METHOD_UNMAP: ApiMethod = ApiMethod::new( "Unmap a loop device mapped with 'map' and release all resources.", &sorted!([ ("name", true, &StringSchema::new( - "Archive name, path to loopdev (/dev/loopX) or loop device number. Omit to list all current mappings." + concat!("Archive name, path to loopdev (/dev/loopX) or loop device number. ", + "Omit to list all current mappings and force cleaning up leftover instances.") ).schema()), ]), ) @@ -337,6 +338,7 @@ fn unmap( let mut name = match param["name"].as_str() { Some(name) => name.to_owned(), None => { + tools::fuse_loop::cleanup_unused_run_files(None); let mut any = false; for (backing, loopdev) in tools::fuse_loop::find_all_mappings()? { let name = tools::systemd::unescape_unit(&backing)?; diff --git a/src/tools/fuse_loop.rs b/src/tools/fuse_loop.rs index f0d19acc..da186b4c 100644 --- a/src/tools/fuse_loop.rs +++ b/src/tools/fuse_loop.rs @@ -15,7 +15,7 @@ use tokio::io::{AsyncRead, AsyncSeek, AsyncReadExt, AsyncSeekExt}; use futures::stream::{StreamExt, TryStreamExt}; use futures::channel::mpsc::{Sender, Receiver}; -use proxmox::{try_block, const_regex}; +use proxmox::const_regex; use proxmox_fuse::{*, requests::FuseRequest}; use super::loopdev; use super::fs; @@ -55,6 +55,11 @@ impl FuseLoopSession { let mut pid_path = path.clone(); pid_path.set_extension("pid"); + // cleanup previous instance with same name + // if loopdev is actually still mapped, this will do nothing and the + // create_new below will fail as intended + cleanup_unused_run_files(Some(name.as_ref().to_owned())); + match OpenOptions::new().write(true).create_new(true).open(&path) { Ok(_) => { /* file created, continue on */ }, Err(e) => { @@ -66,40 +71,27 @@ impl FuseLoopSession { }, } - let res: Result<(Fuse, String), Error> = try_block!{ - let session = Fuse::builder("pbs-block-dev")? - .options_os(options)? - .enable_read() - .build()? - .mount(&path)?; + let session = Fuse::builder("pbs-block-dev")? + .options_os(options)? + .enable_read() + .build()? + .mount(&path)?; - let loopdev_path = loopdev::get_or_create_free_dev().map_err(|err| { - format_err!("loop-control GET_FREE failed - {}", err) - })?; + let loopdev_path = loopdev::get_or_create_free_dev().map_err(|err| { + format_err!("loop-control GET_FREE failed - {}", err) + })?; - // write pidfile so unmap can later send us a signal to exit - Self::write_pidfile(&pid_path)?; + // write pidfile so unmap can later send us a signal to exit + Self::write_pidfile(&pid_path)?; - Ok((session, loopdev_path)) - }; - - match res { - Ok((session, loopdev_path)) => - Ok(Self { - session: Some(session), - reader, - stat: minimal_stat(size as i64), - fuse_path: path.to_string_lossy().into_owned(), - pid_path: pid_path.to_string_lossy().into_owned(), - loopdev_path, - }), - Err(e) => { - // best-effort temp file cleanup in case of error - let _ = remove_file(&path); - let _ = remove_file(&pid_path); - Err(e) - } - } + Ok(Self { + session: Some(session), + reader, + stat: minimal_stat(size as i64), + fuse_path: path.to_string_lossy().into_owned(), + pid_path: pid_path.to_string_lossy().into_owned(), + loopdev_path, + }) } fn write_pidfile(path: &Path) -> Result<(), Error> { @@ -229,6 +221,38 @@ impl FuseLoopSession { } } +/// Clean up leftover files as well as FUSE instances without a loop device +/// connected. Best effort, never returns an error. +/// If filter_name is Some("..."), only this name will be cleaned up. +pub fn cleanup_unused_run_files(filter_name: Option) { + if let Ok(maps) = find_all_mappings() { + for (name, loopdev) in maps { + if loopdev.is_none() && + (filter_name.is_none() || &name == filter_name.as_ref().unwrap()) + { + let mut path = PathBuf::from(RUN_DIR); + path.push(&name); + + // clean leftover FUSE instances (e.g. user called 'losetup -d' or similar) + // does nothing if files are already stagnant (e.g. instance crashed etc...) + if let Ok(_) = unmap_from_backing(&path) { + // we have reaped some leftover instance, tell the user + eprintln!( + "Cleaned up dangling mapping '{}': no loop device assigned", + &name + ); + } + + // remove remnant files + // these we're not doing anything, so no need to inform the user + let _ = remove_file(&path); + path.set_extension("pid"); + let _ = remove_file(&path); + } + } + } +} + fn get_backing_file(loopdev: &str) -> Result { let num = loopdev.split_at(9).1.parse::().map_err(|err| format_err!("malformed loopdev path, does not end with valid number - {}", err))?; -- 2.20.1