Skip to content

Commit

Permalink
Make _create_netns work first time (on some systems)
Browse files Browse the repository at this point in the history
  • Loading branch information
tailhook committed Sep 10, 2018
1 parent e340748 commit c0c3378
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/launcher/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,30 @@ pub fn create_netns(_config: &Config, mut args: Vec<String>)
return Err("Namespaces already created".to_string());
}

let mut commands = vec!();

let mut nforward = String::with_capacity(100);
File::open(&Path::new("/proc/sys/net/ipv4/ip_forward"))
.and_then(|mut f| f.read_to_string(&mut nforward))
.map_err(|e| format!("Can't read sysctl: {}", e))?;

if nforward[..].trim() == "0" {
let mut cmd = sudo_sysctl();
cmd.arg("net.ipv4.ip_forward=1");
commands.push(cmd);
} else {
info!("Sysctl is ok [{}]", nforward[..].trim());
}

if !dry_run && !commands.is_empty() {
println!("");
println!("The following commands will be run first:");
// need to setup ip_forward before creating new namespaces
for cmd in commands.drain(..) {
run_success(cmd)?;
}
}

let mut cmd = Command::new(env::current_exe().unwrap());
cmd.arg("__setup_netns__");
cmd.unshare(&[Namespace::Net]);
Expand Down Expand Up @@ -184,8 +208,6 @@ pub fn create_netns(_config: &Config, mut args: Vec<String>)
println!("We will run network setup commands with sudo.");
println!("You may need to enter your password.");

let mut commands = vec!();

let mut cmd = sudo_ip_cmd();
cmd.args(&["link", "add", "vagga_guest", "type", "veth",
"peer", "name", &interface_name[..]]);
Expand All @@ -205,19 +227,6 @@ pub fn create_netns(_config: &Config, mut args: Vec<String>)
cmd.args(&["link", "set", &interface_name, "up"]);
commands.push(cmd);

let mut nforward = String::with_capacity(100);
File::open(&Path::new("/proc/sys/net/ipv4/ip_forward"))
.and_then(|mut f| f.read_to_string(&mut nforward))
.map_err(|e| format!("Can't read sysctl: {}", e))?;

if nforward[..].trim() == "0" {
let mut cmd = sudo_sysctl();
cmd.arg("net.ipv4.ip_forward=1");
commands.push(cmd);
} else {
info!("Sysctl is ok [{}]", nforward[..].trim());
}

let mut cmd = sudo_sysctl();
cmd.arg("net.ipv4.conf.vagga.route_localnet=1");
commands.push(cmd);
Expand Down Expand Up @@ -272,7 +281,6 @@ pub fn create_netns(_config: &Config, mut args: Vec<String>)
}



if iptables {
println!("");
println!("The following iptables rules will be established:");
Expand Down

0 comments on commit c0c3378

Please sign in to comment.