Skip to content

Commit

Permalink
Introduce virt-xml into disk attachement
Browse files Browse the repository at this point in the history
  • Loading branch information
skobyda committed Apr 4, 2023
1 parent 0328182 commit 257ff3b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 67 deletions.
2 changes: 0 additions & 2 deletions src/components/vm/disks/diskAdd.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,6 @@ export class AddDiskModalBody extends React.Component {
permanent: this.state.permanent,
hotplug: this.state.hotplug,
vmName: vm.name,
vmId: vm.id,
cacheMode: this.state.cacheMode,
busType: this.state.busType,
serial: clearSerial(this.state.serial)
Expand Down Expand Up @@ -697,7 +696,6 @@ export class AddDiskModalBody extends React.Component {
permanent: this.state.permanent,
hotplug: this.state.hotplug,
vmName: vm.name,
vmId: vm.id,
cacheMode: this.state.cacheMode,
shareable: volume && volume.format === "raw" && isVolumeUsed[this.state.existingVolumeName].length > 0,
busType: this.state.busType,
Expand Down
45 changes: 0 additions & 45 deletions src/libvirt-xml-create.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,3 @@
export function getDiskXML(type, file, device, poolName, volumeName, format, target, cacheMode, shareable, busType, serial) {
const doc = document.implementation.createDocument('', '', null);

const diskElem = doc.createElement('disk');
diskElem.setAttribute('type', type);
diskElem.setAttribute('device', device);

const driverElem = doc.createElement('driver');
driverElem.setAttribute('name', 'qemu');
if (format && ['qcow2', 'raw'].includes(format))
driverElem.setAttribute('type', format);
driverElem.setAttribute('cache', cacheMode);
driverElem.setAttribute('discard', 'unmap');
diskElem.appendChild(driverElem);

const sourceElem = doc.createElement('source');
if (type === 'file') {
sourceElem.setAttribute('file', file);
} else {
sourceElem.setAttribute('volume', volumeName);
sourceElem.setAttribute('pool', poolName);
}
diskElem.appendChild(sourceElem);

const targetElem = doc.createElement('target');
targetElem.setAttribute('dev', target);
targetElem.setAttribute('bus', busType);
diskElem.appendChild(targetElem);

if (shareable) {
const shareableElem = doc.createElement('shareable');
diskElem.appendChild(shareableElem);
}

if (serial) {
const serialElem = doc.createElement('serial');
serialElem.appendChild(doc.createTextNode(serial));
diskElem.appendChild(serialElem);
}

doc.appendChild(diskElem);

return new XMLSerializer().serializeToString(doc.documentElement);
}

export function getNetworkXML({ name, forwardMode, device, ipv4, netmask, ipv6, prefix, ipv4DhcpRangeStart, ipv4DhcpRangeEnd, ipv6DhcpRangeStart, ipv6DhcpRangeEnd }) {
const doc = document.implementation.createDocument('', '', null);

Expand Down
41 changes: 24 additions & 17 deletions src/libvirtApi/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ import {
undefineVm,
updateOrAddVm,
} from '../actions/store-actions.js';
import {
getDiskXML,
} from '../libvirt-xml-create.js';
import {
finishVmCreateInProgress,
setVmCreateInProgress,
Expand Down Expand Up @@ -97,17 +94,6 @@ function buildConsoleVVFile(consoleDetail) {
'fullscreen=0\n';
}

function domainAttachDevice({ connectionName, vmId, permanent, hotplug, xmlDesc }) {
let flags = Enum.VIR_DOMAIN_AFFECT_CURRENT;
if (hotplug)
flags |= Enum.VIR_DOMAIN_AFFECT_LIVE;
if (permanent)
flags |= Enum.VIR_DOMAIN_AFFECT_CONFIG;

// Error handling is done from the calling side
return call(connectionName, vmId, 'org.libvirt.Domain', 'AttachDevice', [xmlDesc, flags], { timeout, type: 'su' });
}

export function getPythonPath() {
return cockpit.spawn(["/bin/sh", "-c", "which /usr/libexec/platform-python 2>/dev/null || which python3 2>/dev/null || which python"]).then(pyexe => { pythonPath = pyexe.trim() });
}
Expand All @@ -121,7 +107,6 @@ export function domainAttachDisk({
volumeName,
format,
target,
vmId,
vmName,
permanent,
hotplug,
Expand All @@ -130,9 +115,31 @@ export function domainAttachDisk({
busType,
serial,
}) {
const xmlDesc = getDiskXML(type, file, device, poolName, volumeName, format, target, cacheMode, shareable, busType, serial);
const options = { err: "message" };
if (connectionName === "system")
options.superuser = "try";
let define = "--define";
if (hotplug && !permanent)
define = "--no-define";
let source = "";
if (type === 'file')
source = `,source.file=${file}`;
else
source = `,source.pool=${poolName},source.volume=${volumeName}`;
let driverType = "";
if (format && ['qcow2', 'raw'].includes(format))
driverType = `,driver.type=${format}`;
let serialNum = "";
if (serial)
serialNum = `,serial=${serial}`;
const shareableOption = shareable ? "yes" : "no";

return domainAttachDevice({ connectionName, vmId, permanent, hotplug, xmlDesc });
const args = ["virt-xml", "-c", `qemu:///${connectionName}`, vmName, "--add-device", "--disk", `type=${type},shareable=${shareableOption},target.bus=${busType},target.dev=${target},driver.name=qemu,driver.discard=unmap,cache=${cacheMode},device=${device}${source}${driverType}${serialNum}`, define];

if (hotplug)
args.push("--update");

return cockpit.spawn(args, options);
}

export function domainAttachHostDevices({ connectionName, vmName, live, devices }) {
Expand Down
5 changes: 2 additions & 3 deletions src/libvirtApi/storageVolume.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export function storageVolumeCreateAndAttach({
size,
format,
target,
vmId,
vmName,
permanent,
hotplug,
Expand All @@ -63,8 +62,8 @@ export function storageVolumeCreateAndAttach({
return storagePoolRefresh({ connectionName, objPath: storagePoolPath[0] });
});
})
.then((volPath) => {
return domainAttachDisk({ connectionName, type: "volume", device: "disk", poolName, volumeName, format, target, vmId, permanent, hotplug, cacheMode, busType, serial });
.then(() => {
return domainAttachDisk({ connectionName, type: "volume", device: "disk", poolName, volumeName, format, target, vmName, permanent, hotplug, cacheMode, busType, serial });
});
}

Expand Down

0 comments on commit 257ff3b

Please sign in to comment.