vmm: extract a helper to send the VM config

This further decreases boilerplate code in lib.rs while keeping the
behavior.

On-behalf-of: SAP sebastian.eydam@sap.com
Signed-off-by: Sebastian Eydam <sebastian.eydam@cyberus-technology.de>
This commit is contained in:
Sebastian Eydam
2026-03-09 10:41:27 +01:00
committed by Bo Chen
parent d4a8d55074
commit 196e48af30
2 changed files with 23 additions and 11 deletions
+22 -2
View File
@@ -3,17 +3,19 @@
// SPDX-License-Identifier: Apache-2.0
//
use std::io::Write;
use std::net::{TcpListener, TcpStream};
use std::os::unix::net::{UnixListener, UnixStream};
use std::path::PathBuf;
use std::result::Result;
use anyhow::anyhow;
use anyhow::{Context, anyhow};
use log::info;
use serde_json;
use vm_migration::MigratableError;
use vm_migration::protocol::{Request, Response};
use crate::SocketStream;
use crate::{SocketStream, VmMigrationConfig};
/// Extract a UNIX socket path from a "unix:" migration URL.
fn socket_url_to_path(url: &str) -> Result<PathBuf, anyhow::Error> {
@@ -97,3 +99,21 @@ pub(crate) fn send_request_expect_ok(
request.write_to(socket)?;
expect_ok_response(socket, error)
}
/// Serialize and send the VM configuration payload.
pub(crate) fn send_config(
socket: &mut SocketStream,
config: &VmMigrationConfig,
) -> Result<(), MigratableError> {
let config_data = serde_json::to_vec(config)
.context("Error serializing VM migration config")
.map_err(MigratableError::MigrateSend)?;
Request::config(config_data.len() as u64).write_to(socket)?;
socket
.write_all(&config_data)
.map_err(MigratableError::MigrateSocket)?;
expect_ok_response(
socket,
MigratableError::MigrateSend(anyhow!("Error during config migration")),
)
}