mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-11 12:46:49 +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:
parent
10eb0d1c2e
commit
51d24fce30
21 changed files with 828 additions and 42 deletions
|
|
@ -117,6 +117,9 @@ class MigrateSerializer(serializers.ModelSerializer):
|
|||
autoconverge = serializers.BooleanField(default=True)
|
||||
compress = serializers.BooleanField(default=False)
|
||||
unsafe = serializers.BooleanField(default=False)
|
||||
# {target: pool of the destination} of the disks copied there; delete_source deletes their source files
|
||||
disk_pools = serializers.DictField(child=serializers.CharField(allow_blank=True), default=dict)
|
||||
delete_source = serializers.BooleanField(default=False)
|
||||
submission_key = serializers.CharField(write_only=True)
|
||||
|
||||
class Meta:
|
||||
|
|
@ -129,5 +132,7 @@ class MigrateSerializer(serializers.ModelSerializer):
|
|||
"autoconverge",
|
||||
"compress",
|
||||
"unsafe",
|
||||
"disk_pools",
|
||||
"delete_source",
|
||||
"submission_key",
|
||||
]
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ from computes.models import Compute
|
|||
from django.http import Http404
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.urls import reverse
|
||||
from instances.jobs import MIGRATE_OPTIONS
|
||||
from instances.models import Flavor, Instance
|
||||
from instances.views import destroy as instance_destroy
|
||||
from instances.views import (
|
||||
|
|
@ -195,8 +196,8 @@ class MigrateViewSet(viewsets.ViewSet):
|
|||
if not serializer.is_valid():
|
||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||
data = serializer.validated_data
|
||||
raw = {"instance": data["instance"].pk, "target": data["target_compute"].pk,
|
||||
**{name: data.get(name, False) for name in ("live", "unsafe", "offline", "autoconverge", "compress")}}
|
||||
raw = {"instance": data["instance"].pk, "target": data["target_compute"].pk, "disk_pools": data["disk_pools"],
|
||||
**{name: data.get(name, False) for name in MIGRATE_OPTIONS}}
|
||||
op = jobs.submit("instance.migrate", request.user, raw, data["submission_key"])
|
||||
return Response(OperationSerializer(op, context={"request": request}).data, status=status.HTTP_202_ACCEPTED,
|
||||
headers={"Location": reverse("operation-detail", args=[op.pk])})
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
"""Jobs of the instances app (operations framework, spec section 5)."""
|
||||
|
||||
import math
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
|
||||
|
|
@ -809,7 +810,7 @@ class CreateInstance(Kind):
|
|||
return _log(op, state, "create")
|
||||
|
||||
|
||||
MIGRATE_OPTIONS = ("live", "unsafe", "offline", "autoconverge", "compress")
|
||||
MIGRATE_OPTIONS = ("live", "unsafe", "offline", "autoconverge", "compress", "delete_source")
|
||||
|
||||
|
||||
def _target(instance, target_pk):
|
||||
|
|
@ -841,7 +842,9 @@ class MigrateInstance(Kind):
|
|||
progress_bytes = True # libvirt job data processed/total
|
||||
|
||||
def normalize(self, raw):
|
||||
# disk_pools: {target: pool of the destination} of the disks to copy there
|
||||
return {"instance": int(raw["instance"]), "target": int(raw["target"]),
|
||||
"disk_pools": {str(k): str(v) for k, v in (raw.get("disk_pools") or {}).items() if v},
|
||||
**{k: bool(raw.get(k)) for k in MIGRATE_OPTIONS}}
|
||||
|
||||
def validate(self, user, params):
|
||||
|
|
@ -855,6 +858,13 @@ class MigrateInstance(Kind):
|
|||
keys.update({claims.compute_key(instance.compute_id): Claim.SHARED,
|
||||
claims.reconcile_key(instance.compute_id): Claim.EXCLUSIVE,
|
||||
claims.instance_key(instance.uuid): Claim.EXCLUSIVE})
|
||||
if params["disk_pools"]:
|
||||
# a copy takes its file's name in the chosen pool; the source file may go
|
||||
for disk in instance.proxy.get_disk_devices():
|
||||
if pool := params["disk_pools"].get(disk["dev"]):
|
||||
keys[claims.volume_key(params["target"], pool, os.path.basename(disk["path"]))] = Claim.EXCLUSIVE
|
||||
if disk["storage"]:
|
||||
keys[claims.volume_key(instance.compute_id, disk["storage"], disk["image"])] = Claim.EXCLUSIVE
|
||||
return Plan(claims=keys, resource=claims.instance_key(instance.uuid),
|
||||
label=f"{instance.compute.name}/{instance.name}",
|
||||
intent={"compute": instance.compute_id, "uuid": instance.uuid})
|
||||
|
|
@ -875,6 +885,7 @@ class MigrateInstance(Kind):
|
|||
try:
|
||||
# migrate_instance logs the outcome
|
||||
utils.migrate_instance(Compute.objects.get(pk=p["target"]), instance, op.user,
|
||||
disk_pools=p["disk_pools"],
|
||||
**{k: p[k] for k in MIGRATE_OPTIONS})
|
||||
except libvirtError:
|
||||
op.refresh_from_db(fields=["cancel_requested"])
|
||||
|
|
|
|||
|
|
@ -444,6 +444,40 @@
|
|||
});
|
||||
});
|
||||
}
|
||||
// the Migrate tab's disks: loaded for the chosen destination when the tab opens or the choice changes
|
||||
// the form is not sent until the disks of the chosen destination are shown
|
||||
var migratePlan = document.getElementById("migrate-plan");
|
||||
var migrateCompute = document.getElementById("migrate_compute");
|
||||
var migrateLoading = null;
|
||||
function loadMigratePlan() {
|
||||
if (!migrateCompute.value) return;
|
||||
if (migrateLoading) migrateLoading.abort();
|
||||
var loading = migrateLoading = new AbortController();
|
||||
var submit = migratePlan.closest("form").querySelector('button[name="migrate"]');
|
||||
submit.disabled = true;
|
||||
migratePlan.textContent = "{{ _("Checking the disks…")|escapejs }}";
|
||||
fetch(migratePlan.dataset.url + "?compute_id=" + encodeURIComponent(migrateCompute.value),
|
||||
{signal: loading.signal}).then(function (response) {
|
||||
if (!response.ok) throw new Error(response.statusText);
|
||||
return response.text();
|
||||
}).then(function (html) {
|
||||
migratePlan.innerHTML = html;
|
||||
submit.disabled = false;
|
||||
}).catch(function () {
|
||||
if (loading.signal.aborted) return; // another destination was chosen
|
||||
migratePlan.textContent = "{{ _("The disks could not be checked.")|escapejs }}";
|
||||
submit.disabled = false; // the migration checks them again
|
||||
});
|
||||
}
|
||||
if (migratePlan && migrateCompute) {
|
||||
document.querySelector('[data-bs-target="#migrate"]').addEventListener("shown.bs.tab", function () {
|
||||
if (!migratePlan.dataset.loaded) {
|
||||
migratePlan.dataset.loaded = "true";
|
||||
loadMigratePlan();
|
||||
}
|
||||
});
|
||||
migrateCompute.addEventListener("change", loadMigratePlan);
|
||||
}
|
||||
function get_osinfo() {
|
||||
show_tab('#settings');
|
||||
show_tab('#osinfo');
|
||||
|
|
|
|||
43
instances/templates/instances/migrate_plan.html
Normal file
43
instances/templates/instances/migrate_plan.html
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
{% load i18n %}
|
||||
{# the disks of the Migrate tab for the chosen destination: where each goes #}
|
||||
{% if error %}
|
||||
<div class="alert alert-warning mb-0">{% blocktrans %}The disks could not be checked: {{ error }}{% endblocktrans %}</div>
|
||||
{% elif not disks %}
|
||||
<p class="small text-body-secondary mb-0">{% trans "The VM has no disk files: its disks stay where they are." %}</p>
|
||||
{% else %}
|
||||
<div class="table-responsive">
|
||||
<table class="table table-sm align-middle mb-1">
|
||||
<thead>
|
||||
<tr><th scope="col">{% trans "Disk" %}</th><th scope="col">{% trans "Size" %}</th><th scope="col">{% trans "On the destination" %}</th></tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for disk in disks %}
|
||||
<tr>
|
||||
<td><span class="fw-semibold">{{ disk.target }}</span><div class="small text-body-secondary text-break">{{ disk.path }}</div></td>
|
||||
<td class="text-nowrap">{{ disk.capacity|filesizeformat }}</td>
|
||||
<td>
|
||||
{% if disk.shared or not disk.refusal and pools %}
|
||||
<select name="pool-{{ disk.target }}" class="form-select form-select-sm" aria-label="{% blocktrans with target=disk.target %}Where {{ target }} goes{% endblocktrans %}">
|
||||
{% if disk.shared %}<option value="" selected>{% trans "Stays on shared storage" %}</option>{% endif %}
|
||||
{% if not disk.refusal %}
|
||||
{% for pool in pools %}
|
||||
<option value="{{ pool.name }}"{% if not disk.shared and pool.name == disk.default %} selected{% endif %}>{% blocktrans with name=pool.name free=pool.free|filesizeformat %}Copy to {{ name }} ({{ free }} free){% endblocktrans %}</option>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
</select>
|
||||
{% elif disk.refusal %}
|
||||
<span class="small text-danger">{% blocktrans with reason=disk.refusal %}Cannot be copied: {{ reason }}{% endblocktrans %}</span>
|
||||
{% else %}
|
||||
<span class="small text-danger">{% trans "No pool of the destination host can take it" %}</span>
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<p class="form-text mb-0">
|
||||
{% trans "A copy is made during the migration and takes as long as the disk's data needs. Disks on network storage stay where they are." %}
|
||||
{% if shut_off %}{% trans "A shut-off VM is started paused for a copy, its CPUs never run, and is shut off on the destination." %}{% endif %}
|
||||
</p>
|
||||
{% endif %}
|
||||
|
|
@ -697,7 +697,7 @@
|
|||
<div class="row">
|
||||
<label class="col-sm-3 col-form-label">{% trans "Host migration" %}</label>
|
||||
<div class="col-sm-6">
|
||||
<select name="compute_id" class="form-select">
|
||||
<select name="compute_id" class="form-select" id="migrate_compute">
|
||||
{% if computes_count != 1 %}
|
||||
{% for comp in computes %}
|
||||
{% if comp.id != compute.id %}
|
||||
|
|
@ -708,6 +708,10 @@
|
|||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<label class="col-sm-3 col-form-label">{% trans "Disks" %}</label>
|
||||
<div class="col-sm-9 py-2" id="migrate-plan" data-url="{% url 'instances:migrate_plan' instance.id %}" aria-live="polite"></div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-sm-6 offset-sm-3">
|
||||
<div class="form-check form-switch">
|
||||
|
|
@ -748,6 +752,14 @@
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-sm-6 offset-sm-3">
|
||||
<div class="form-check form-switch">
|
||||
<input class="form-check-input" type="checkbox" name="delete_source" value="true" id="delete_source">
|
||||
<label class="form-check-label" for="delete_source">{% trans "Delete the source files of copied disks" %}</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% if computes_count != 1 %}
|
||||
<button type="submit" class="btn btn-lg btn-success float-end" name="migrate" >{% trans "Migrate" %}</button>
|
||||
{% else %}
|
||||
|
|
|
|||
|
|
@ -172,6 +172,31 @@ class LiveJobsTestCase(TransactionTestCase):
|
|||
self.assertFalse(self.conn.lookupByUUIDString(uuid).isActive()) # defined on the source again
|
||||
self.assertEqual(Instance.objects.get(uuid=uuid).compute, self.compute)
|
||||
|
||||
def test_a_disk_neither_host_shares_is_copied_there_and_back(self):
|
||||
"""The test pool is a directory of each host's own: the disk is
|
||||
copied into it, running there with its source file deleted on
|
||||
request, shut off back (started paused for the copy) with it kept."""
|
||||
dest, dest_conn = self.second_host()
|
||||
self.addCleanup(livetest.remove_pool, dest_conn)
|
||||
livetest.ensure_pool(dest_conn)
|
||||
path = livetest.create_volume(self.conn, P + "copy")
|
||||
dom = livetest.define_vm(self.conn, P + "copy", [path])
|
||||
dom.create()
|
||||
uuid = dom.UUIDString()
|
||||
instance = Instance.objects.create(compute=self.compute, name=dom.name(), uuid=uuid)
|
||||
pools = {"vda": livetest.POOL}
|
||||
there = {"instance": instance.pk, "target": dest.pk, "live": True, "disk_pools": pools, "delete_source": True}
|
||||
self.run_job(jobs.submit("instance.migrate", self.admin, there, "key-copy0001"))
|
||||
self.assertTrue(dest_conn.lookupByUUIDString(uuid).isActive())
|
||||
self.assertEqual((livetest.volume_exists(self.conn, path), livetest.volume_exists(dest_conn, path)),
|
||||
(False, True))
|
||||
dest_conn.lookupByUUIDString(uuid).destroy()
|
||||
back = {"instance": instance.pk, "target": self.compute.pk, "offline": True, "disk_pools": pools}
|
||||
self.run_job(jobs.submit("instance.migrate", self.admin, back, "key-copy0002"))
|
||||
self.assertFalse(self.conn.lookupByUUIDString(uuid).isActive())
|
||||
self.assertEqual((livetest.volume_exists(self.conn, path), livetest.volume_exists(dest_conn, path)),
|
||||
(True, True))
|
||||
|
||||
def test_a_shut_off_uefi_vm_migrates_with_its_nvram_there_and_back(self):
|
||||
dest, _dest_conn = self.second_host()
|
||||
hosts = {self.compute.pk: wvmConnect(*self.args), dest.pk: wvmConnect(dest.hostname, dest.login, "", 2)}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ class MigrateJobTestCase(TransactionTestCase):
|
|||
patch.object(Compute, "status", True).start()
|
||||
patch.object(Instance, "proxy", proxy).start()
|
||||
self.dest_conn = patch("instances.utils.wvmInstances").start().return_value
|
||||
self.dest_conn.moveto.return_value = "live"
|
||||
self.dest_conn.moveto.return_value = ("live", [])
|
||||
self.addCleanup(patch.stopall)
|
||||
|
||||
def queue(self, **form):
|
||||
|
|
@ -76,6 +76,41 @@ class MigrateJobTestCase(TransactionTestCase):
|
|||
self.vm.refresh_from_db()
|
||||
self.assertEqual(self.vm.compute, self.dest)
|
||||
|
||||
def test_a_copy_claims_its_volume_on_both_computes(self):
|
||||
Instance.proxy.get_disk_devices.return_value = [
|
||||
{"dev": "vda", "storage": "default", "image": "mig-vm.qcow2", "path": "/i/mig-vm.qcow2"},
|
||||
{"dev": "vdb", "storage": None, "image": "/dev/sdb", "path": "/dev/sdb"}]
|
||||
op = self.queue(live_migrate="true", delete_source="true", **{"pool-vda": "fast", "pool-vdb": ""})
|
||||
self.assertTrue({(f"volume:{self.source.pk}:default/mig-vm.qcow2", "exclusive"),
|
||||
(f"volume:{self.dest.pk}:fast/mig-vm.qcow2", "exclusive")}
|
||||
<= set(Claim.objects.values_list("resource", "mode")))
|
||||
self.assertEqual((op.params["disk_pools"], op.params["delete_source"]), ({"vda": "fast"}, True))
|
||||
ctx = self.start(op)
|
||||
get_kind(op.kind).run(op, ctx)
|
||||
self.assertEqual(self.dest_conn.moveto.call_args.kwargs["pools"], {"vda": "fast"})
|
||||
|
||||
def test_the_migrate_tab_lists_each_disk_with_where_it_can_go(self):
|
||||
Instance.proxy.instance.XMLDesc.return_value = "<domain/>"
|
||||
Instance.proxy.get_status.return_value = 5
|
||||
with patch("instances.views.wvmInstances") as instances:
|
||||
dest = instances.return_value
|
||||
fast, default = MagicMock(), MagicMock()
|
||||
fast.info.return_value = default.info.return_value = [2, 0, 0, 8 << 30]
|
||||
dest.file_pools.return_value = {"fast": (fast, "/srv/fast"), "default": (default, "/i")}
|
||||
dest.disk_plan.return_value = [
|
||||
{"target": "vda", "path": "/i/mig-vm.qcow2", "capacity": 1 << 30, "shared": False, "refusal": None},
|
||||
{"target": "sda", "path": "/i/os.iso", "capacity": 1 << 20, "shared": False,
|
||||
"refusal": "an image is not copied; eject it first"}]
|
||||
page = self.client.get(reverse("instances:migrate_plan", args=[self.vm.pk]),
|
||||
{"compute_id": self.dest.pk}).content.decode()
|
||||
dest.close.assert_called_once_with()
|
||||
# the pool on the disk's directory is chosen
|
||||
self.assertIn('<option value="default" selected>Copy to default (8.0\xa0GB free)</option>', page)
|
||||
self.assertIn('<option value="fast">Copy to fast (8.0\xa0GB free)</option>', page)
|
||||
self.assertIn("Cannot be copied: an image is not copied; eject it first", page)
|
||||
self.assertIn("A shut-off VM is started paused for a copy", page)
|
||||
self.assertEqual(self.client.get(reverse("instances:migrate_plan", args=[self.vm.pk])).status_code, 404)
|
||||
|
||||
def test_a_cancelled_migration_ends_cancelled_and_the_vm_stays(self):
|
||||
op = self.queue(live_migrate="true")
|
||||
ctx = self.start(op)
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ class MigrationBookkeepingTests(TestCase):
|
|||
patch("instances.utils.wvmInstances") as instances, \
|
||||
patch("instances.utils.wvmInstance") as instance, \
|
||||
patch.object(Instance, "proxy", proxy):
|
||||
instances.return_value.moveto.return_value = "live"
|
||||
instances.return_value.moveto.return_value = ("live", [])
|
||||
if autostart_error:
|
||||
instance.return_value.set_autostart.side_effect = autostart_error
|
||||
self.moveto = instances.return_value.moveto
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ class MigrationLogTests(TestCase):
|
|||
self.dest = Compute.objects.create(name="ml-dst", hostname="198.51.100.9", login="root", password="", type=2)
|
||||
self.vm = Instance.objects.create(compute=self.source, name="vm", uuid="u-ml")
|
||||
|
||||
def migrate(self, outcome, nvram=None, remove_nvram=True, **request):
|
||||
def migrate(self, outcome, nvram=None, remove_nvram=True, copied=(), delete_fails=None, in_use=(), **request):
|
||||
self.proxy = MagicMock()
|
||||
self.proxy.get_autostart.return_value = False
|
||||
self.proxy.get_nvram.return_value = nvram
|
||||
|
|
@ -34,6 +34,9 @@ class MigrationLogTests(TestCase):
|
|||
self.proxy.remove_nvram.side_effect = remove_nvram
|
||||
else:
|
||||
self.proxy.remove_nvram.return_value = remove_nvram
|
||||
self.proxy.paths_in_use.return_value = set(in_use)
|
||||
if delete_fails:
|
||||
self.proxy.get_volume_by_path.return_value.delete.side_effect = delete_fails
|
||||
with patch.object(Compute, "status", True), \
|
||||
patch("instances.utils.wvmInstances") as instances, \
|
||||
patch("instances.utils.wvmInstance"), \
|
||||
|
|
@ -42,7 +45,7 @@ class MigrationLogTests(TestCase):
|
|||
if isinstance(outcome, Exception):
|
||||
instances.return_value.moveto.side_effect = outcome
|
||||
else:
|
||||
instances.return_value.moveto.return_value = outcome
|
||||
instances.return_value.moveto.return_value = (outcome, list(copied))
|
||||
return migrate_instance(self.dest, self.vm, self.admin, **request)
|
||||
|
||||
def test_the_nvram_file_left_on_the_source_is_removed(self):
|
||||
|
|
@ -100,3 +103,37 @@ class MigrationLogTests(TestCase):
|
|||
with self.assertRaisesRegex(util.OperationError, "A shut-off VM cannot be migrated live"):
|
||||
self.migrate(util.OperationError("A shut-off VM cannot be migrated live"), live=True)
|
||||
self.assertEqual(rows(), [("VM vm: migrate to 198.51.100.9: A shut-off VM cannot be migrated live", False)])
|
||||
|
||||
def test_the_source_copies_stay_unless_deleted(self):
|
||||
disks = ["/var/lib/libvirt/images/vm.qcow2", "/var/lib/libvirt/images/vm-data.qcow2"]
|
||||
self.migrate("live, disks copied: vda to default, vdb to default", copied=disks, live=True)
|
||||
self.assertFalse(self.proxy.get_volume_by_path.called)
|
||||
self.assertEqual(rows(), [
|
||||
("VM vm: migrate (live, disks copied: vda to default, vdb to default) to 198.51.100.9", True),
|
||||
("VM vm: keep the source copy /var/lib/libvirt/images/vm.qcow2", True),
|
||||
("VM vm: keep the source copy /var/lib/libvirt/images/vm-data.qcow2", True),
|
||||
])
|
||||
|
||||
def test_deleted_source_copies_and_a_failed_delete_are_logged(self):
|
||||
self.migrate("live, disks copied: vda", copied=["/var/lib/libvirt/images/vm.qcow2"], live=True,
|
||||
delete_source=True)
|
||||
self.proxy.get_volume_by_path.assert_called_once_with("/var/lib/libvirt/images/vm.qcow2")
|
||||
self.assertEqual(rows()[-1], ("VM vm: delete the source copy /var/lib/libvirt/images/vm.qcow2", True))
|
||||
self.vm.compute = self.source
|
||||
self.vm.save()
|
||||
self.migrate("live, disks copied: vda", copied=["/var/lib/libvirt/images/vm.qcow2"], live=True,
|
||||
delete_source=True, delete_fails=libvirtError("cannot delete"))
|
||||
self.assertEqual(rows()[-1], ("VM vm: delete the source copy /var/lib/libvirt/images/vm.qcow2: cannot delete",
|
||||
False))
|
||||
self.vm.refresh_from_db()
|
||||
self.assertEqual(self.vm.compute, self.dest)
|
||||
|
||||
def test_a_source_copy_another_vm_or_volume_uses_stays(self):
|
||||
disks = ["/var/lib/libvirt/images/vm.qcow2", "/var/lib/libvirt/images/base.qcow2"]
|
||||
self.migrate("live, disks copied: vda, vdb", copied=disks, in_use={disks[1]}, live=True,
|
||||
delete_source=True)
|
||||
self.proxy.get_volume_by_path.assert_called_once_with(disks[0])
|
||||
self.assertEqual(rows()[-2:], [
|
||||
("VM vm: delete the source copy /var/lib/libvirt/images/vm.qcow2", True),
|
||||
("VM vm: keep the source copy /var/lib/libvirt/images/base.qcow2: another VM or volume uses it", True),
|
||||
])
|
||||
|
|
|
|||
|
|
@ -138,6 +138,7 @@ ALLOWED_HTML_SINKS = {
|
|||
("accounts/templates/accounts/otp_login.html", "$btn.html("): "static spinner markup",
|
||||
("instances/templates/instance.html", "//sto_input.innerHTML = pool;"): "comment",
|
||||
("instances/templates/instance.html", "hostDevices.innerHTML = html;"): "the panel's own fragment, host_devices_form.html, escaped by Django",
|
||||
("instances/templates/instance.html", "migratePlan.innerHTML = html;"): "the panel's own fragment, migrate_plan.html, escaped by Django",
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -58,6 +58,7 @@ urlpatterns = [
|
|||
path("<int:pk>/add_network/", views.add_network, name="add_network"),
|
||||
path("<int:pk>/delete_network/", views.delete_network, name="delete_network"),
|
||||
path("<int:pk>/host_devices/", views.host_devices, name="host_devices"),
|
||||
path("<int:pk>/migrate_plan/", views.migrate_plan, name="migrate_plan"),
|
||||
path("<int:pk>/add_usb_hostdev/", views.add_usb_hostdev, name="add_usb_hostdev"),
|
||||
path("<int:pk>/delete_usb_hostdev/", views.delete_usb_hostdev, name="delete_usb_hostdev"),
|
||||
path("<int:pk>/add_pci_hostdev/", views.add_pci_hostdev, name="add_pci_hostdev"),
|
||||
|
|
|
|||
|
|
@ -158,10 +158,15 @@ def migrate_instance(
|
|||
offline=False,
|
||||
autoconverge=False,
|
||||
compress=False,
|
||||
delete_source=False,
|
||||
disk_pools=None,
|
||||
):
|
||||
"""Migrates instance to new_compute and logs the outcome (the mode
|
||||
used, or the error), then each later step that failed in a row of its
|
||||
own. Returns the mode."""
|
||||
own. disk_pools: {target: pool} of the disks copied into pools of
|
||||
new_compute (moveto's pools); their source files go with delete_source
|
||||
unless another VM or volume uses them, and are named in the log
|
||||
otherwise. Returns the mode."""
|
||||
source, target = instance.compute, migration_target(new_compute)
|
||||
|
||||
def log(action, error=None):
|
||||
|
|
@ -179,7 +184,7 @@ def migrate_instance(
|
|||
|
||||
autostart = instance.proxy.get_autostart()
|
||||
nvram = instance.proxy.get_nvram()
|
||||
mode = conn_migrate.moveto(
|
||||
mode, copied = conn_migrate.moveto(
|
||||
instance.proxy,
|
||||
instance.name,
|
||||
live,
|
||||
|
|
@ -188,6 +193,7 @@ def migrate_instance(
|
|||
autoconverge,
|
||||
compress,
|
||||
uri=new_compute.migration_uri,
|
||||
pools=disk_pools,
|
||||
)
|
||||
except Exception as error:
|
||||
log(f"migrate to {target}", error)
|
||||
|
|
@ -217,6 +223,24 @@ def migrate_instance(
|
|||
if conn_new is not None:
|
||||
conn_new.close()
|
||||
|
||||
in_use = set()
|
||||
if copied and delete_source:
|
||||
try:
|
||||
in_use = instance.proxy.paths_in_use()
|
||||
except libvirtError as error:
|
||||
log("check the source copies before deleting them", error)
|
||||
delete_source = False
|
||||
for path in copied:
|
||||
if not delete_source or path in in_use:
|
||||
log(f"keep the source copy {path}" + (": another VM or volume uses it" if path in in_use else ""))
|
||||
continue
|
||||
try:
|
||||
instance.proxy.get_volume_by_path(path).delete(0)
|
||||
except libvirtError as error:
|
||||
log(f"delete the source copy {path}", error)
|
||||
else:
|
||||
log(f"delete the source copy {path}")
|
||||
|
||||
# libvirt keeps the NVRAM file on the source; a VM created there
|
||||
# later with the same name would start with these variables.
|
||||
if nvram:
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ from operations.utils import busy_message, summary
|
|||
from webvirtcloud.middleware import error_text, same_origin_referer
|
||||
from vrtManager import util
|
||||
from vrtManager.create import cpu_choices, wvmCreate
|
||||
from vrtManager.instance import USAGE_MAX_WINDOW
|
||||
from vrtManager.instance import USAGE_MAX_WINDOW, wvmInstances
|
||||
from vrtManager.interface import wvmInterface
|
||||
from vrtManager.nodedevs import device_names, hostdev_problems, node_devices, pci_problems
|
||||
from vrtManager.storage import wvmStorage
|
||||
|
|
@ -850,7 +850,10 @@ def migrate(request, pk):
|
|||
target_host = get_object_or_404(Compute, pk=compute_id)
|
||||
raw = {"instance": pk, "target": target_host.pk, "live": request.POST.get("live_migrate"),
|
||||
"unsafe": request.POST.get("unsafe_migrate"), "offline": request.POST.get("offline_migrate"),
|
||||
"autoconverge": request.POST.get("autoconverge"), "compress": request.POST.get("compress")}
|
||||
"autoconverge": request.POST.get("autoconverge"), "compress": request.POST.get("compress"),
|
||||
"delete_source": request.POST.get("delete_source"),
|
||||
"disk_pools": {key.removeprefix("pool-"): value for key, value in request.POST.items()
|
||||
if key.startswith("pool-")}}
|
||||
try:
|
||||
op = jobs.submit("instance.migrate", request.user, raw, request.POST.get("submission_key", ""))
|
||||
except claims.Busy as busy:
|
||||
|
|
@ -863,6 +866,36 @@ def migrate(request, pk):
|
|||
return get_safe_redirect(request, default=reverse("instances:instance", args=[pk]))
|
||||
|
||||
|
||||
@superuser_only
|
||||
def migrate_plan(request, pk):
|
||||
"""The disks of the Migrate tab for the chosen destination, loaded when
|
||||
one is chosen: each with the pools of the destination it can be copied
|
||||
into, or the storage both hosts share."""
|
||||
instance = get_instance(request.user, pk)
|
||||
compute_id = request.GET.get("compute_id", "")
|
||||
if not compute_id.isdigit():
|
||||
raise Http404
|
||||
target = get_object_or_404(Compute, pk=compute_id)
|
||||
dest = None
|
||||
try:
|
||||
dest = wvmInstances(target.hostname, target.login, target.password, target.type)
|
||||
pools = [{"name": name, "directory": directory, "free": pool.info()[3]}
|
||||
for name, (pool, directory) in sorted(dest.file_pools().items())]
|
||||
disks = dest.disk_plan(instance.proxy, instance.proxy.instance.XMLDesc(0))
|
||||
for disk in disks:
|
||||
# a copy goes by default to the pool on the same directory, else the first
|
||||
names = [pool["name"] for pool in pools if pool["directory"] == os.path.dirname(disk["path"])]
|
||||
names = names or [pool["name"] for pool in pools]
|
||||
disk["default"] = names[0] if names else None
|
||||
context = {"disks": disks, "pools": pools, "shut_off": instance.proxy.get_status() == 5}
|
||||
except libvirtError as err:
|
||||
context = {"error": error_text(request, err)}
|
||||
finally:
|
||||
if dest is not None:
|
||||
dest.close()
|
||||
return render(request, "instances/migrate_plan.html", context)
|
||||
|
||||
|
||||
GSTFSD_PORT = 16510
|
||||
GSTFSD_TIMEOUT = 120 # gstfsd answers within its 100 s limit for a request (GSTFSD_DEADLINE)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue