From 4d2b944d5886d80563d6d086f5ae75974b5141fa Mon Sep 17 00:00:00 2001 From: Simon Kobyda Date: Fri, 15 Oct 2021 14:45:59 +0200 Subject: [PATCH] Introduce virt-xml to disk update API --- src/components/vm/disks/diskEdit.jsx | 4 ++- src/libvirt-xml-update.js | 51 ---------------------------- src/libvirtApi/domain.js | 29 ++++++++++++---- 3 files changed, 25 insertions(+), 59 deletions(-) diff --git a/src/components/vm/disks/diskEdit.jsx b/src/components/vm/disks/diskEdit.jsx index 14d6c25de..fec24af47 100644 --- a/src/components/vm/disks/diskEdit.jsx +++ b/src/components/vm/disks/diskEdit.jsx @@ -197,10 +197,12 @@ export class EditDiskModal extends React.Component { domainUpdateDiskAttributes({ connectionName: vm.connectionName, - objPath: vm.id, target: disk.target, + vmName: vm.name, + target: disk.target, readonly: this.state.access == "readonly", shareable: this.state.access == "shareable", busType: this.state.busType, + oldBusType: disk.bus, cache: this.state.cacheMode, existingTargets }) diff --git a/src/libvirt-xml-update.js b/src/libvirt-xml-update.js index 09efda891..60e6281bc 100644 --- a/src/libvirt-xml-update.js +++ b/src/libvirt-xml-update.js @@ -1,55 +1,4 @@ import { getDoc, getSingleOptionalElem } from './libvirt-xml-parse.js'; -import { getNextAvailableTarget } from './helpers.js'; - -export function updateDisk({ domXml, diskTarget, readonly, shareable, busType, existingTargets, cache }) { - const s = new XMLSerializer(); - const doc = getDoc(domXml); - const domainElem = doc.firstElementChild; - if (!domainElem) - throw new Error("updateBootOrder: domXML has no domain element"); - - const deviceElem = domainElem.getElementsByTagName("devices")[0]; - const disks = deviceElem.getElementsByTagName("disk"); - - for (let i = 0; i < disks.length; i++) { - const disk = disks[i]; - const target = disk.getElementsByTagName("target")[0].getAttribute("dev"); - if (target == diskTarget) { - let shareAbleElem = getSingleOptionalElem(disk, "shareable"); - if (!shareAbleElem && shareable) { - shareAbleElem = doc.createElement("shareable"); - disk.appendChild(shareAbleElem); - } else if (shareAbleElem && !shareable) { - shareAbleElem.remove(); - } - - let readOnlyElem = getSingleOptionalElem(disk, "readonly"); - if (!readOnlyElem && readonly) { - readOnlyElem = doc.createElement("readonly"); - disk.appendChild(readOnlyElem); - } else if (readOnlyElem && !readonly) { - readOnlyElem.remove(); - } - - const targetElem = disk.getElementsByTagName("target")[0]; - const oldBusType = targetElem.getAttribute("bus"); - if (busType && oldBusType !== busType) { - targetElem.setAttribute("bus", busType); - const newTarget = getNextAvailableTarget(existingTargets, busType); - targetElem.setAttribute("dev", newTarget); - - const addressElem = getSingleOptionalElem(disk, "address"); - addressElem.remove(); - } - - const driverElem = disk.getElementsByTagName("driver")[0]; - if (cache) - driverElem.setAttribute("cache", cache); - } - } - - return s.serializeToString(doc); -} export function updateBootOrder(domXml, devices) { const s = new XMLSerializer(); diff --git a/src/libvirtApi/domain.js b/src/libvirtApi/domain.js index 55cd3943f..dd606b487 100644 --- a/src/libvirtApi/domain.js +++ b/src/libvirtApi/domain.js @@ -47,6 +47,7 @@ import { import { convertToUnit, DOMAINSTATE, + getNextAvailableTarget, fileDownload, getHostDevSourceObject, getNodeDevSource, @@ -62,7 +63,6 @@ import { } from '../libvirt-xml-parse.js'; import { updateBootOrder, - updateDisk, updateMaxMemory, } from '../libvirt-xml-update.js'; import { storagePoolRefresh } from './storagePool.js'; @@ -821,10 +821,25 @@ export function domainStart({ connectionName, id: objPath }) { return call(connectionName, objPath, 'org.libvirt.Domain', 'Create', [0], { timeout, type: 'u' }); } -export function domainUpdateDiskAttributes({ connectionName, objPath, target, readonly, shareable, busType, existingTargets, cache }) { - return call(connectionName, objPath, 'org.libvirt.Domain', 'GetXMLDesc', [Enum.VIR_DOMAIN_XML_INACTIVE], { timeout, type: 'u' }) - .then(domXml => { - const updatedXML = updateDisk({ diskTarget: target, domXml, readonly, shareable, busType, existingTargets, cache }); - return call(connectionName, '/org/libvirt/QEMU', 'org.libvirt.Connect', 'DomainDefineXML', [updatedXML], { timeout, type: 's' }); - }); +export function domainUpdateDiskAttributes({ connectionName, vmName, target, readonly, shareable, busType, oldBusType, existingTargets, cache }) { + const options = { err: "message" }; + if (connectionName === "system") + options.superuser = "try"; + + const shareableOption = shareable ? "yes" : "no"; + const readonlyOption = readonly ? "yes" : "no"; + let newTarget = target; + if (busType !== oldBusType) + newTarget = getNextAvailableTarget(existingTargets, busType); + + const args = [ + "virt-xml", "-c", `qemu:///${connectionName}`, + vmName, "--edit", `target.dev=${target}`, "--disk", + `shareable=${shareableOption},readonly=${readonlyOption},target.bus=${busType},target.dev=${newTarget}` + ]; + + if (cache) + args[7].push(`cache=${cache}`); + + return cockpit.spawn(args, options); }