start to implementig clone to create namespaces
This commit is contained in:
43
src/namespace/create_ns.rs
Normal file
43
src/namespace/create_ns.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
use std::fs::create_dir;
|
||||
use std::io::{Result, ErrorKind};
|
||||
use std::fs::File;
|
||||
use crate::namespace::consts::{
|
||||
NET_NS_DIR,
|
||||
PROC_NS_DIR
|
||||
};
|
||||
use nix::mount::{
|
||||
mount,
|
||||
MsFlags
|
||||
};
|
||||
|
||||
pub fn create_ns() -> Result<()> {
|
||||
create_ns_dir()?;
|
||||
let nsdir = format!("{}/{}",NET_NS_DIR,"fake-net");
|
||||
File::create(nsdir.clone())?;
|
||||
bind_ns_file(nsdir)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn create_ns_dir() -> Result<()> {
|
||||
match create_dir(NET_NS_DIR) {
|
||||
Err(e) if e.kind() != ErrorKind::AlreadyExists => {
|
||||
Err(e)
|
||||
},
|
||||
_ => {
|
||||
Ok(())
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn bind_ns_file(ns_file: String) -> Result<()> {
|
||||
match create_mount(ns_file) {
|
||||
Ok(_mount) => {
|
||||
Ok(())
|
||||
},
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
fn create_mount(ns_file: String) -> Result<()> {
|
||||
Ok(mount::<str, str, str, str>(Some(PROC_NS_DIR), ns_file.as_str(), None, MsFlags::MS_BIND, None)?)
|
||||
}
|
||||
Reference in New Issue
Block a user