1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 08:56:07 +00:00

feat(instances): USB devices of the host given to a VM

- A Host devices tab on the VM page lists the VM's USB host devices, each
  once: how it is chosen (its address, or any device of its model), the
  device the running VM holds and whether its next start has it; its other
  host devices (PCI, SCSI, mediated) are listed for the XML editor.
- Superusers add a USB device of the host and take it back, on a running
  VM too. The add form is loaded when the tab opens, as its choices read
  every VM of the host; a hub, a device the host has its own disks or
  network interfaces on, or one another VM names is not offered, with the
  reason, and refused on the server under the compute's claim. A device is
  chosen by model when the host has one of it, else by its address.
- A VM without a USB controller is refused; a device is taken from each
  definition as that definition describes it.
- A clone gets none of the VM's host devices (the clone form says so), and
  a VM with host devices is not migrated. A snapshot revert is refused when
  it would give the VM a device another VM names or the host uses.
- A NIC that is a host device is no longer edited or removed from the
  Network tab: the XML editor changes it.
This commit is contained in:
catborise 2026-10-29 13:16:45 +03:00
parent 0977aef587
commit ac8ff62e7e
20 changed files with 679 additions and 94 deletions

View file

@ -49,6 +49,7 @@ from vrtManager import util
from vrtManager.create import cpu_choices, wvmCreate
from vrtManager.instance import USAGE_MAX_WINDOW
from vrtManager.interface import wvmInterface
from vrtManager.nodedevs import hostdev_problems, node_devices, usb_names
from vrtManager.storage import wvmStorage
from vrtManager.util import randomPasswd
@ -308,6 +309,15 @@ def _instance_page(request, pk, instance, compute):
instance.drbd = drbd_status(request, pk)
instance.save()
usb_hostdevs, other_hostdevs = instance.proxy.get_hostdevs()
if usb_hostdevs:
names = usb_names(instance.proxy)
for row in usb_hostdevs:
row["name"] = names.get((row["vendor"], row["product"]), "")
# owners see the devices their VM has; only superusers change them
show_hostdevs = request.user.is_superuser or bool(usb_hostdevs or other_hostdevs)
show_settings = show_settings or show_hostdevs
job_resource = claims.instance_key(instance.uuid)
return render(request, "instance.html", locals(),)
@ -1785,6 +1795,65 @@ def delete_network(request, pk):
return _back(request, pk, "network")
def _host_usb(instance):
"""The host's USB devices, each with why it cannot be given to the VM
(hostdev_problems; none: it can) and how the VM would name it: by its
address when the host has more than one of its model, else by model."""
_pci, usb = node_devices(instance.proxy)
for device in usb:
address = (device["bus"], device["device"]) if device["same_model"] else None
device["selector"] = (*device["model"], address)
device["problems"] = hostdev_problems(instance.name, [], [device], set(), [device["selector"]])
if instance.name in device["vms"]:
device["problems"].append(_("The VM has it"))
return usb
@superuser_only
def host_usb(request, pk):
"""The add form of the Host devices tab, loaded when the tab opens: its
choices scan every VM of the host."""
instance = get_instance(request.user, pk)
try:
context = {"instance": instance, "host_usb": _host_usb(instance)}
except libvirtError as err:
context = {"instance": instance, "error": err}
return render(request, "instances/host_usb_form.html", context)
@require_POST
@superuser_only
@serialize_compute_mutation
def add_usb_hostdev(request, pk):
"""Gives the VM a USB device of its host; checked under the compute's
claim, so no other VM gets it meanwhile."""
instance = get_instance(request.user, pk)
chosen = request.POST.get("usb_device", "")
device = next((d for d in _host_usb(instance) if f"{d['bus']}/{d['device']}" == chosen), None)
if device is None:
messages.error(request, _("The host has no USB device %(device)s") % {"device": chosen})
elif device["problems"]:
messages.error(request, "; ".join(device["problems"]))
else:
vendor, product, address = device["selector"]
with audit_vm(request, instance, f"add USB device {device['ids']} ({device['product']})"):
instance.proxy.add_usb_hostdev(vendor, product, address)
return _back(request, pk, "hostdevs")
@require_POST
@superuser_only
@serialize_instance_mutation
def delete_usb_hostdev(request, pk):
instance = get_instance(request.user, pk)
vendor, product = request.POST.get("vendor", ""), request.POST.get("product", "")
bus, _sep, device = request.POST.get("address", "").partition("/")
address = (int(bus), int(device)) if bus.isdigit() and device.isdigit() else None
with audit_vm(request, instance, f"remove USB device {vendor[2:]}:{product[2:]}"):
instance.proxy.delete_usb_hostdev((vendor, product, address))
return _back(request, pk, "hostdevs")
@require_POST
@superuser_only
@serialize_instance_mutation