fuse_loop: add automatic cleanup of run files and dangling instances

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 <s.reiter@proxmox.com>
This commit is contained in:
Stefan Reiter 2020-10-07 13:53:06 +02:00 committed by Dietmar Maurer
parent 2d7d6e61be
commit 2deee0e01f
2 changed files with 59 additions and 33 deletions

View File

@ -83,7 +83,8 @@ const API_METHOD_UNMAP: ApiMethod = ApiMethod::new(
"Unmap a loop device mapped with 'map' and release all resources.", "Unmap a loop device mapped with 'map' and release all resources.",
&sorted!([ &sorted!([
("name", true, &StringSchema::new( ("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()), ).schema()),
]), ]),
) )
@ -337,6 +338,7 @@ fn unmap(
let mut name = match param["name"].as_str() { let mut name = match param["name"].as_str() {
Some(name) => name.to_owned(), Some(name) => name.to_owned(),
None => { None => {
tools::fuse_loop::cleanup_unused_run_files(None);
let mut any = false; let mut any = false;
for (backing, loopdev) in tools::fuse_loop::find_all_mappings()? { for (backing, loopdev) in tools::fuse_loop::find_all_mappings()? {
let name = tools::systemd::unescape_unit(&backing)?; let name = tools::systemd::unescape_unit(&backing)?;

View File

@ -15,7 +15,7 @@ use tokio::io::{AsyncRead, AsyncSeek, AsyncReadExt, AsyncSeekExt};
use futures::stream::{StreamExt, TryStreamExt}; use futures::stream::{StreamExt, TryStreamExt};
use futures::channel::mpsc::{Sender, Receiver}; use futures::channel::mpsc::{Sender, Receiver};
use proxmox::{try_block, const_regex}; use proxmox::const_regex;
use proxmox_fuse::{*, requests::FuseRequest}; use proxmox_fuse::{*, requests::FuseRequest};
use super::loopdev; use super::loopdev;
use super::fs; use super::fs;
@ -55,6 +55,11 @@ impl<R: AsyncRead + AsyncSeek + Unpin> FuseLoopSession<R> {
let mut pid_path = path.clone(); let mut pid_path = path.clone();
pid_path.set_extension("pid"); 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) { match OpenOptions::new().write(true).create_new(true).open(&path) {
Ok(_) => { /* file created, continue on */ }, Ok(_) => { /* file created, continue on */ },
Err(e) => { Err(e) => {
@ -66,7 +71,6 @@ impl<R: AsyncRead + AsyncSeek + Unpin> FuseLoopSession<R> {
}, },
} }
let res: Result<(Fuse, String), Error> = try_block!{
let session = Fuse::builder("pbs-block-dev")? let session = Fuse::builder("pbs-block-dev")?
.options_os(options)? .options_os(options)?
.enable_read() .enable_read()
@ -80,11 +84,6 @@ impl<R: AsyncRead + AsyncSeek + Unpin> FuseLoopSession<R> {
// write pidfile so unmap can later send us a signal to exit // write pidfile so unmap can later send us a signal to exit
Self::write_pidfile(&pid_path)?; Self::write_pidfile(&pid_path)?;
Ok((session, loopdev_path))
};
match res {
Ok((session, loopdev_path)) =>
Ok(Self { Ok(Self {
session: Some(session), session: Some(session),
reader, reader,
@ -92,14 +91,7 @@ impl<R: AsyncRead + AsyncSeek + Unpin> FuseLoopSession<R> {
fuse_path: path.to_string_lossy().into_owned(), fuse_path: path.to_string_lossy().into_owned(),
pid_path: pid_path.to_string_lossy().into_owned(), pid_path: pid_path.to_string_lossy().into_owned(),
loopdev_path, loopdev_path,
}), })
Err(e) => {
// best-effort temp file cleanup in case of error
let _ = remove_file(&path);
let _ = remove_file(&pid_path);
Err(e)
}
}
} }
fn write_pidfile(path: &Path) -> Result<(), Error> { fn write_pidfile(path: &Path) -> Result<(), Error> {
@ -229,6 +221,38 @@ impl<R: AsyncRead + AsyncSeek + Unpin> FuseLoopSession<R> {
} }
} }
/// 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<String>) {
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<String, Error> { fn get_backing_file(loopdev: &str) -> Result<String, Error> {
let num = loopdev.split_at(9).1.parse::<u8>().map_err(|err| let num = loopdev.split_at(9).1.parse::<u8>().map_err(|err|
format_err!("malformed loopdev path, does not end with valid number - {}", err))?; format_err!("malformed loopdev path, does not end with valid number - {}", err))?;