1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00

feat(instances): a CD-ROM is added on the bus chosen, with its ISO

A running VM got its new CD-ROM on SCSI, the one bus the panel could plug
in then, and Windows does not see a virtio-scsi drive before the driver
on that ISO is installed. Add CD-ROM is now a dialog that chooses the bus
and the ISO: a running VM takes USB, first, which every guest sees and
which hot-plugs, or SCSI; a shut-off one SATA (IDE on pc), SCSI or USB.
libvirt has no empty USB drive, so a USB CD-ROM takes its ISO when it is
added and is detached instead of ejected, also while the VM runs.

A USB CD-ROM is a CD drive on QEMU's usb-bot only. Where QEMU has none
(RHEL 9), libvirt took usb-storage, which shows the guest a hard disk
that Windows does not read; the drive now asks for usb-bot, and such a
host refuses it with libvirt's message.
This commit is contained in:
catborise 2026-11-01 15:06:54 +03:00
parent d510650f92
commit 04dbf016d4
9 changed files with 313 additions and 165 deletions

View file

@ -0,0 +1,43 @@
{% load i18n %}
{% load bootstrap_icons %}
<button type="button" class="btn btn-sm btn-secondary" data-bs-toggle="modal" data-bs-target="#addInstanceCdrom"{% if not cdrom_buses %} disabled{% endif %}>{% bs_icon 'plus' %} {% trans "Add CD-ROM" %}</button>
<div class="modal fade" id="addInstanceCdrom" tabindex="-1" role="dialog" aria-labelledby="addInstanceCdromLabel" aria-hidden="true">
<div class="modal-dialog">
<form class="modal-content" action="{% url 'instances:add_cdrom' instance.id %}" method="post" aria-label="{% trans "Add CD-ROM form" %}">{% csrf_token %}
<div class="modal-header">
<h5 class="modal-title" id="addInstanceCdromLabel">{% trans "Add CD-ROM" %}</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="{% trans "Close" %}"></button>
</div>
<div class="modal-body">
<div class="row g-3">
<div class="col-sm-4">
<label class="form-label" for="add-cdrom-bus">{% trans "Bus" %}</label>
<select class="form-select" id="add-cdrom-bus" name="bus" aria-describedby="add-cdrom-help">
{% for bus in cdrom_buses %}
<option value="{{ bus }}">{{ bus }}</option>
{% endfor %}
</select>
</div>
<div class="col-sm-8">
<label class="form-label" for="add-cdrom-media">{% trans "ISO image" %}</label>
<select class="form-select" id="add-cdrom-media" name="media">
{% for iso in instance.media_iso %}
<option value="{{ iso }}">{{ iso }}</option>
{% endfor %}
<option value="">{% trans "No media" %}</option>
</select>
</div>
<div class="col-12 form-text mt-2" id="add-cdrom-help">
{% trans "Windows sees USB, SATA and IDE drives without a driver; SCSI ones need the virtio driver." %}
{% trans "A running VM takes USB and SCSI drives. A USB drive needs an ISO image, is detached instead of ejected, and needs a QEMU with usb-bot (not RHEL 9's)." %}
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">{% trans "Cancel" %}</button>
<button type="submit" class="btn btn-primary">{% trans "Add" %}</button>
</div>
</form>
</div>
</div>

View file

@ -147,14 +147,12 @@
<div role="tabpanel" class="tab-pane" id="disks">
{# its CD-ROM drives, then its disks as list rows; the forms are those of the actions #}
{% if instance.status != 5 %}
<p class="small text-body-secondary">{% bs_icon 'lock' %} {% trans "The VM is running: CD-ROM drives added now use the SCSI bus. Shut it down to detach other CD-ROM drives, or to detach or delete disks." %}</p>
<p class="small text-body-secondary">{% bs_icon 'lock' %} {% trans "The VM is running: CD-ROM drives added now use the USB or SCSI bus. Shut it down to detach a SATA or IDE CD-ROM drive, or to detach or delete disks." %}</p>
{% endif %}
<section class="mb-4" aria-labelledby="media-heading">
<div class="d-flex flex-wrap justify-content-between align-items-center gap-2 mb-2">
<h2 class="h6 mb-0" id="media-heading">{% trans "Removable media" %}</h2>
<form action="{% url 'instances:add_cdrom' instance.id %}" method="post" aria-label="{% trans "Add CD-ROM form" %}">{% csrf_token %}
<button class="btn btn-sm btn-secondary" type="submit">{% bs_icon 'plus' %} {% trans "Add CD-ROM" %}</button>
</form>
{% include 'add_instance_cdrom.html' %}
</div>
{% for cd in instance.media %}
<div class="card mb-2">
@ -168,11 +166,13 @@
<div class="small text-body-secondary">{{ cd.dev }} · {{ cd.bus }} · {% if cd.image %}{{ cd.image }}{% else %}{% trans "No media" %}{% endif %}</div>
</div>
</div>
{% if not cd.image %}
<button type="submit" class="btn btn-sm btn-secondary" formaction="{% url 'instances:detach_cdrom' instance.id cd.dev %}" formmethod="post"{% if instance.status != 5 and cd.bus != "scsi" %} disabled{% endif %}>{% trans "Detach" %}</button>
{% if not cd.image or cd.bus == "usb" %}
<button type="submit" class="btn btn-sm btn-secondary" formaction="{% url 'instances:detach_cdrom' instance.id cd.dev %}" formmethod="post"{% if instance.status != 5 and cd.bus != "scsi" and cd.bus != "usb" %} disabled{% endif %}>{% trans "Detach" %}</button>
{% endif %}
</div>
{% if cd.image %}
{% if cd.bus == "usb" %}
{# libvirt has no empty USB drive: it is detached, not ejected #}
{% elif cd.image %}
<input type="hidden" name="path" value="{{ cd.path }}">
<button type="submit" class="btn btn-sm btn-primary" value="{{ cd.dev }}" name="umount_iso">{% bs_icon 'eject' %} {% trans "Unmount" %}</button>
{% else %}

View file

@ -601,6 +601,33 @@ class LiveDataLossTestCase(TestCase):
tree = etree.fromstring(dom.XMLDesc(flags))
self.assertEqual(tree.xpath("devices/disk[@device='cdrom']/target/@dev"), ["sda"])
def test_a_usb_cdrom_with_its_iso_is_plugged_into_a_running_vm_and_detached(self):
from lxml import etree
pool = self.conn.storagePoolLookupByName(livetest.POOL)
iso = pool.createXML(f"<volume><name>{P}usb.iso</name><capacity unit='MiB'>1</capacity>"
"<target><format type='raw'/></target></volume>", 0)
dom, inst = self.vm("cdrom-usb", [], extra_devices="<controller type='usb' model='qemu-xhci'/>")
dom.create()
def cdroms(flags):
tree = etree.fromstring(dom.XMLDesc(flags))
return [(d.find("target").get("bus"), d.get("model"), d.xpath("string(source/@file)"))
for d in tree.xpath("devices/disk[@device='cdrom']")]
response = self.client.post(reverse("instances:add_cdrom", args=[inst.id]),
{"bus": "usb", "media": f"{P}usb.iso"}, follow=True)
if not cdroms(0): # a QEMU without usb-bot (RHEL 9): refused, nothing added
self.assertIn("usb-bot", " ".join(str(m) for m in response.context["messages"]))
self.assertEqual(cdroms(libvirt.VIR_DOMAIN_XML_INACTIVE), [])
return
for flags in (0, libvirt.VIR_DOMAIN_XML_INACTIVE):
self.assertEqual(cdroms(flags), [("usb", "usb-bot", iso.path())])
self.client.post(reverse("instances:detach_cdrom", args=[inst.id, "sda"]),
HTTP_REFERER=reverse("instances:instance", args=[inst.id]))
for flags in (0, libvirt.VIR_DOMAIN_XML_INACTIVE):
self.assertEqual(cdroms(flags), [])
def test_pending_qos_change_is_shown_and_kept_by_the_next_edit(self):
from lxml import etree, html

View file

@ -30,7 +30,8 @@ class NewDiskNameTestCase(TestCase):
proxy = wvm.return_value
proxy.get_disk_devices.side_effect = lambda **kw: list((config if kw.get("config") else live)[0])
proxy.get_media_devices.side_effect = lambda **kw: list((config if kw.get("config") else live)[1])
proxy.get_disk_bus_types.return_value = ["virtio", "sata", "scsi"]
proxy.get_disk_bus_types.return_value = ["virtio", "sata", "scsi", "usb"]
proxy.get_iso_path.side_effect = lambda image: f"/iso/{image}"
proxy.get_cache_modes.return_value = {"default": "", "none": ""}
proxy.get_machine_type.return_value = "q35"
proxy.get_status.return_value = status
@ -39,14 +40,14 @@ class NewDiskNameTestCase(TestCase):
storage.return_value.get_volume_type.return_value = "file"
storage.return_value.get_volume_format_type.return_value = "qcow2"
create.return_value.create_volume.return_value = "/pool/data.qcow2"
self.client.post(reverse(f"instances:{view}", args=[self.instance.id]), data)
proxy.attach_disk.assert_called_once()
self.response = self.client.post(reverse(f"instances:{view}", args=[self.instance.id]), data, follow=True)
self.proxy = proxy
return proxy.attach_disk.call_args
VIEWS = (
("add_new_vol", {"storage": "default", "name": "data", "format": "qcow2", "size": "1", "cache": "default"}),
("add_existing_vol", {"selected_storage": "default", "vols": "data.qcow2", "cache": "default"}),
("add_cdrom", {}),
("add_new_vol", {"storage": "default", "name": "data", "format": "qcow2", "size": "1", "cache": "default"}, 1),
("add_existing_vol", {"selected_storage": "default", "vols": "data.qcow2", "cache": "default"}, 1),
("add_cdrom", {}, 5), # shut off: a running VM takes USB and SCSI CD-ROMs only
)
def test_a_name_either_definition_uses_is_not_given_again(self):
@ -58,12 +59,28 @@ class NewDiskNameTestCase(TestCase):
("virtio", ([vda, dev("vdb", "virtio")], []), ([vda], []), "vdc"), # not yet released by the guest
("virtio", ([vda], []), ([vda], []), "vdb"), # both the same, as on a stopped VM
)
for view, data in self.VIEWS:
for view, data, status in self.VIEWS:
for bus, live, config, expected in cases:
if view == "add_cdrom" and bus == "virtio": # no virtio CD-ROM
continue
with self.subTest(view=view, bus=bus, expected=expected):
self.assertEqual(self.attach(view, {**data, "bus": bus}, live, config).args[0], expected)
call = self.attach(view, {**data, "bus": bus}, live, config, status=status)
self.assertEqual(call.args[0], expected)
def test_a_cdrom_of_a_running_vm_is_on_the_bus_that_can_be_hotplugged(self):
for status, bus in ((1, "scsi"), (3, "scsi"), (5, "sata")): # running, paused, shut off
def test_a_cdrom_of_a_running_vm_is_on_a_bus_that_can_be_hotplugged(self):
# running, paused: USB first, which every guest sees; shut off: SATA
for status, bus in ((1, "usb"), (3, "usb"), (5, "sata")):
with self.subTest(status=status):
self.assertEqual(self.attach("add_cdrom", {}, status=status).kwargs["target_bus"], bus)
call = self.attach("add_cdrom", {"media": "virtio-win.iso"}, status=status)
self.assertEqual((call.kwargs["target_bus"], call.args[1]), (bus, "/iso/virtio-win.iso"))
def test_a_running_vm_refuses_a_sata_cdrom_and_a_usb_one_without_media(self):
for data in ({"bus": "sata", "media": "virtio-win.iso"}, {"bus": "usb", "media": ""}):
with self.subTest(data):
self.attach("add_cdrom", data, status=1)
self.proxy.attach_disk.assert_not_called()
self.assertTrue(list(self.response.context["messages"]))
def test_a_cdrom_without_media_is_empty(self):
call = self.attach("add_cdrom", {"bus": "scsi", "media": ""}, status=1)
self.assertEqual((call.kwargs["target_bus"], call.args[1]), ("scsi", ""))

View file

@ -307,6 +307,7 @@ def _instance_page(request, pk, instance, compute):
vcpu_host = len(instance.vcpu_range)
memory_host = instance.proxy.get_max_memory()
bus_host = instance.proxy.get_disk_bus_types(instance.arch, instance.machine)
cdrom_buses = cdrom_bus_choices(instance)
networks_host = sorted(instance.proxy.get_networks())
interfaces_host = sorted(instance.proxy.get_ifaces())
nwfilters_host = instance.proxy.get_nwfilters()
@ -1575,31 +1576,49 @@ def detach_vol(request, pk):
@serialize_instance_mutation
def add_cdrom(request, pk):
instance = get_instance(request.user, pk, perm_type="change")
if instance.status == 5:
default_bus = "ide" if instance.machine == "pc" else "sata"
else: # IDE and SATA drives cannot be plugged into a running VM, SCSI ones can
default_bus = "scsi"
bus = request.POST.get("bus", default_bus)
invalid = invalid_disk_options(instance, bus=bus)
if invalid:
return reject_disk_options(request, pk, invalid)
buses = cdrom_bus_choices(instance)
bus = request.POST.get("bus", buses[0] if buses else "")
image = request.POST.get("media", "")
if bus not in buses:
messages.error(request, _("This VM cannot take a CD-ROM on the %(bus)s bus now") % {"bus": bus})
return _back(request, pk, "disks")
if bus == "usb" and not image:
messages.error(request, _("A USB CD-ROM needs an ISO image: libvirt has no empty USB drive"))
return _back(request, pk, "disks")
target = utils.get_new_disk_dev(
instance.media + instance.config_media, instance.disks + instance.config_disks, bus
)
with audit_vm(request, instance, f"add CD-ROM {target}"):
instance.proxy.attach_disk(
target,
"",
disk_device="cdrom",
cache_mode="none",
target_bus=bus,
readonly=True,
)
try:
with audit_vm(request, instance, f"add CD-ROM {target}" + (f" with {image}" if image else "")):
instance.proxy.attach_disk(
target,
instance.proxy.get_iso_path(image) if image else "",
disk_device="cdrom",
cache_mode="none",
target_bus=bus,
readonly=True,
)
except (libvirtError, util.OperationError) as err:
messages.error(request, err)
return _back(request, pk, "disks")
def cdrom_bus_choices(instance):
"""The buses a CD-ROM can be added on now, the default first. A running VM
takes USB and SCSI drives only; Windows sees USB, SATA and IDE ones
without a driver."""
if instance.status != 5:
order = ("usb", "scsi")
elif instance.machine == "pc":
order = ("ide", "sata", "scsi", "usb")
else:
order = ("sata", "scsi", "usb")
offered = instance.proxy.get_disk_bus_types(instance.arch, instance.machine)
return [bus for bus in order if bus in offered]
@require_POST
@superuser_only
@serialize_instance_mutation