runtime-rs: set network namespace for qemu process and netdev.

We need ensure the add_network_device happens in netns and
move qemu process into netns which keeps the qemu process
running in this net namespace.

Fixes: #8865

Signed-off-by: Alex Lyn <alex.lyn@antgroup.com>
This commit is contained in:
Alex Lyn 2024-03-12 22:21:43 +08:00
parent 69a5e5b955
commit 63786934f4

View File

@ -62,6 +62,7 @@ impl QemuInner {
pub(crate) async fn start_vm(&mut self, _timeout: i32) -> Result<()> {
info!(sl!(), "Starting QEMU VM");
let netns = self.netns.clone().unwrap_or_default();
let mut cmdline = QemuCmdLine::new(&self.id, &self.config)?;
@ -110,6 +111,9 @@ impl QemuInner {
DeviceType::Network(network) => {
let network_info = &self.config.network_info;
// we need ensure add_network_device happens in netns.
let _netns_guard = NetnsGuard::new(&netns).context("new netns guard")?;
_fds_for_qemu = cmdline.add_network_device(&network.config, network_info)?;
}
_ => info!(sl!(), "qemu cmdline: unsupported device: {:?}", device),
@ -125,6 +129,16 @@ impl QemuInner {
command.args(cmdline.build().await?);
info!(sl!(), "qemu cmd: {:?}", command);
// we need move the qemu process into Network Namespace.
unsafe {
let _pre_exec = command.pre_exec(move || {
let _ = enter_netns(&netns);
Ok(())
});
}
self.qemu_process = Some(command.stderr(Stdio::piped()).spawn()?);
info!(sl!(), "qemu process started");