don't remove namespaces on daemon exit

This commit is contained in:
Robin Appelman 2025-11-01 15:58:25 +01:00
commit 3a8b684600
6 changed files with 52 additions and 56 deletions

View file

@ -2,11 +2,9 @@ mod raw;
use crate::config::NamespaceName;
use crate::link::{LinkError, link_up_ns};
use crate::namespace::raw::{
NamespaceSetupError, create_network_namespace,
};
use crate::namespace::raw::{NamespaceSetupError, create_network_namespace};
use nix::errno::Errno;
use nix::mount::{MsFlags, mount, umount2, MntFlags};
use nix::mount::{MntFlags, MsFlags, mount, umount2};
use std::fs::{File, create_dir_all, remove_file};
use std::io::{Error as IoError, ErrorKind};
use std::path::{Path, PathBuf};
@ -40,10 +38,7 @@ impl NetNs {
let ns = create_network_namespace(move |ns| {
bind_namespace(&ns, &path)?;
Result::<_, NamespaceError>::Ok(NetNs {
name,
path,
})
Result::<_, NamespaceError>::Ok(NetNs { name, path })
})?;
ns.setup_interfaces()?;
@ -73,18 +68,16 @@ impl NetNs {
link_up_ns(&self.path, "lo")?;
Ok(())
}
}
impl Drop for NetNs {
fn drop(&mut self) {
pub fn delete(self) -> Result<(), NamespaceError> {
let name = self.path.file_name().unwrap().to_str().unwrap();
info!(name, "deleting network namespace");
if let Err(error) = umount2(&self.path, MntFlags::MNT_DETACH) {
error!(%error, path = %self.path.display(), "Failed to unmount network namespace");
}
if let Err(error) = remove_file(&self.path) {
error!(%error, path = %self.path.display(), "Failed to remove namespace file");
}
umount2(&self.path, MntFlags::MNT_DETACH).map_err(NamespaceError::UnMount)?;
remove_file(&self.path).map_err(|error| NamespaceError::Delete {
error,
path: self.path
})?;
Ok(())
}
}
@ -94,19 +87,20 @@ pub enum NamespaceError {
Parent(IoError),
#[error("Failed to create namespace file {}: {error:#}", path.display())]
Create { path: PathBuf, error: IoError },
#[error("Failed to delete namespace file {}: {error:#}", path.display())]
Delete { path: PathBuf, error: IoError },
#[error("Failed to setup namespace: {0:#}")]
Setup(#[from] NamespaceSetupError),
#[error("Failed to bind-mount netns handle: {0:?}")]
Mount(Errno),
#[error("Failed to unmount netns handle: {0:?}")]
UnMount(Errno),
#[error("Failed to setup loopback inside namespace: {0:#}")]
Link(#[from] LinkError),
}
impl NamespaceError {
fn from_create(path: PathBuf, error: IoError) -> Self {
NamespaceError::Create {
path,
error,
}
NamespaceError::Create { path, error }
}
}