Skip to content

Commit 1316ba1

Browse files
committed
Add resolv.conf to namespaces.
1 parent 6a698cd commit 1316ba1

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

test-utils/src/binaries/net-setup.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ fn main() -> Result<()> {
4646
if !d.delay.is_zero() {
4747
dev.delay(&d.delay, &d.jitter)?
4848
}
49+
if c.nat.is_some() {
50+
dev.add_resolv_conf()?
51+
}
4952
}
5053
if let Some(nat) = c.nat {
5154
run_command(TRACE, ["iptables",
@@ -62,7 +65,11 @@ fn main() -> Result<()> {
6265
let c: Config = toml::from_slice(&t)?;
6366
let b = Bridge::new(&c.bridge.name, c.bridge.cidr);
6467
for d in c.device {
65-
Device::new(&d).delete()?
68+
let dev = Device::new(&d);
69+
if c.nat.is_some() {
70+
dev.del_resolv_conf()?
71+
}
72+
dev.delete()?;
6673
}
6774
b.delete()?;
6875
if let Some(nat) = c.nat {
@@ -113,6 +120,42 @@ impl Device {
113120
])
114121
}
115122

123+
fn add_resolv_conf(&self) -> Result<()> {
124+
const RESOLV_CONF: &str = "nameserver 1.1.1.1\nnameserver 8.8.8.8\n";
125+
126+
let dir = PathBuf::from(format!("/etc/netns/{}", self.space));
127+
if !dir.exists() {
128+
if TRACE {
129+
eprintln!("> creating {dir:?}")
130+
}
131+
fs::create_dir_all(&dir)?
132+
}
133+
let file = dir.join("resolv.conf");
134+
if TRACE {
135+
eprintln!("> writing {file:?}")
136+
}
137+
fs::write(file, RESOLV_CONF)?;
138+
Ok(())
139+
}
140+
141+
fn del_resolv_conf(&self) -> Result<()> {
142+
let dir = PathBuf::from(format!("/etc/netns/{}", self.space));
143+
if dir.exists() {
144+
let file = dir.join("resolv.conf");
145+
if file.exists() {
146+
if TRACE {
147+
eprintln!("> removing {file:?}")
148+
}
149+
fs::remove_file(file)?
150+
}
151+
if TRACE {
152+
eprintln!("> removing {dir:?}")
153+
}
154+
fs::remove_dir(dir)?
155+
}
156+
Ok(())
157+
}
158+
116159
fn delete(self) -> Result<()> {
117160
run_command(TRACE, ["ip", "link", "delete", &self.name])?;
118161
run_command(TRACE, ["ip", "netns", "delete", &self.space])

0 commit comments

Comments
 (0)