vmm: extract a helper to send the VM state

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:54:06 +01:00
committed by Bo Chen
parent 196e48af30
commit 693cdccbb0
2 changed files with 20 additions and 10 deletions

View File

@@ -1453,15 +1453,7 @@ impl Vmm {
// Capture snapshot and send it
let vm_snapshot = vm.snapshot()?;
let snapshot_data = serde_json::to_vec(&vm_snapshot).unwrap();
Request::state(snapshot_data.len() as u64).write_to(&mut socket)?;
socket
.write_all(&snapshot_data)
.map_err(MigratableError::MigrateSocket)?;
migration_transport::expect_ok_response(
&mut socket,
MigratableError::MigrateSend(anyhow!("Error during state migration")),
)?;
migration_transport::send_state(&mut socket, &vm_snapshot)?;
// Complete the migration
// At this step, the receiving VMM will acquire disk locks again.
migration_transport::send_request_expect_ok(

View File

@@ -12,8 +12,8 @@ use std::result::Result;
use anyhow::{Context, anyhow};
use log::info;
use serde_json;
use vm_migration::MigratableError;
use vm_migration::protocol::{Request, Response};
use vm_migration::{MigratableError, Snapshot};
use crate::{SocketStream, VmMigrationConfig};
@@ -117,3 +117,21 @@ pub(crate) fn send_config(
MigratableError::MigrateSend(anyhow!("Error during config migration")),
)
}
/// Serialize and send the VM snapshot payload.
pub(crate) fn send_state(
socket: &mut SocketStream,
snapshot: &Snapshot,
) -> Result<(), MigratableError> {
let snapshot_data = serde_json::to_vec(snapshot)
.context("Error serializing VM snapshot")
.map_err(MigratableError::MigrateSend)?;
Request::state(snapshot_data.len() as u64).write_to(socket)?;
socket
.write_all(&snapshot_data)
.map_err(MigratableError::MigrateSocket)?;
expect_ok_response(
socket,
MigratableError::MigrateSend(anyhow!("Error during state migration")),
)
}