1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 04:36:48 +00:00

feat(instances): migrate a VM with its disks copied into pools of the destination

A migration needed every disk on storage both hosts share: it was refused
when the destination lacked a disk file, and a file of the same name
there was taken for the VM's own disk.

The Migrate tab now lists the VM's disk files for the chosen destination.
For each disk it offers the destination's pools that keep disk files
(directory, filesystem and NFS pools). A disk on storage both hosts share
can also stay where it is.

A directory counts as shared when both hosts have it as a storage pool
and a marker file created on the source exists on the destination. A
directory the probe cannot tell is never taken as shared.

libvirt copies the chosen disks during the migration: migrate3 with
NON_SHARED_DISK for those targets, and destination and persistent
definitions naming the new paths, where libvirt creates the files.

- A shut-off VM is started paused for the copy and shut off on the
  destination; its CPUs never run. Its UEFI variables are copied first,
  as the destination would start from the firmware's template. If the
  migration fails, the VM is shut off on the source again.
- Refused before anything changes, each disk with its reason:
  - a disk neither shared nor given a pool, also one only the next
    start's definition has;
  - CD-ROM images, read-only or encrypted disks, formats other than raw
    and qcow2, and disks with a backing file;
  - a pool that is not of disk files, or that has a file of that name,
    which libvirt would reuse and overwrite;
  - two disks copied to one file.
- The job claims each copy's name in its pool, and the source volume.
- A failed or cancelled migration deletes the copies libvirt made, and
  the UEFI variables copy, unless a VM on the destination has them.
- The source files stay and are named in the log. With "Delete the
  source files of copied disks" they go, except one another VM or volume
  uses; a failed delete is logged and the migration stands.

The NVRAM sharing check uses the same probe: a shut-off move stops when
it cannot be told, and the source file stays.

The API's migrate call takes disk_pools ({target: pool}) and
delete_source.
This commit is contained in:
catborise 2026-10-29 18:20:50 +03:00
parent 10eb0d1c2e
commit 51d24fce30
21 changed files with 828 additions and 42 deletions

View file

@ -110,6 +110,9 @@ class FakePool:
def isActive(self):
return self.active
def XMLDesc(self, flags=0):
return "<pool><target><path>/elsewhere</path></target></pool>"
def refresh(self, flags=0):
self.host.refreshes += 1
self.host.volumes |= self.host.unlisted
@ -291,7 +294,7 @@ class MigrationModeTestCase(unittest.TestCase):
def migrate(self, state, **request):
source = FakeSource(state)
options = {"live": False, "unsafe": False, "offline": False, **request}
mode = destination().moveto(source, "vm", **options)
mode, _copied = destination().moveto(source, "vm", **options)
return mode, source.instance.calls[0][0] if source.instance.calls else None
def test_refused_requests(self):
@ -344,7 +347,14 @@ FILE_DISK = "<disk type='file' device='disk'><source file='/pool/vm.qcow2'/><tar
class DestinationDisksTestCase(unittest.TestCase):
"""Every disk of the VM must be a volume the destination knows; the
VM is refused before anything changes otherwise, naming the disks."""
VM is refused before anything changes otherwise, naming the disks.
(Which disks are copied, and that the others are shared, is
test_disk_copy_migration's.)"""
def setUp(self):
patcher = mock.patch.object(wvmInstances, "disk_copies", return_value=[])
patcher.start()
self.addCleanup(patcher.stop)
def migrate(self, host, xml, state=1, inactive_xml=None):
source = FakeSource(state, xml=xml)
@ -450,7 +460,7 @@ class NvramCopyTestCase(unittest.TestCase):
self.dest_pool = mock.MagicMock()
self.dest._nvram_pool = lambda: contextlib.nullcontext(self.dest_pool)
self.dest.wvm.newStream = mock.MagicMock()
self.shared = mock.patch.object(wvmInstances, "_nvram_shared", return_value=False)
self.shared = mock.patch.object(wvmInstances, "same_directory", return_value=False)
self.shared.start()
self.addCleanup(self.shared.stop)
@ -466,11 +476,19 @@ class NvramCopyTestCase(unittest.TestCase):
def test_a_shared_directory_is_not_copied(self):
self.shared.stop()
with mock.patch.object(wvmInstances, "_nvram_shared", return_value=True):
with mock.patch.object(wvmInstances, "same_directory", return_value=True):
self.move()
self.dest_pool.createXML.assert_not_called()
self.shared.start()
def test_a_directory_whose_sharing_cannot_be_told_stops_the_move(self):
self.shared.stop()
with mock.patch.object(wvmInstances, "same_directory", side_effect=libvirt.libvirtError("no marker")):
with self.assertRaisesRegex(libvirt.libvirtError, "no marker"):
self.move()
self.shared.start()
self.assertEqual(self.host.defined, [])
def test_the_copy_is_deleted_when_the_definition_fails(self):
# the raised error is this one: its message can be another libvirt
# error the thread recorded (a closed connection of an earlier live test)