Skip to content

Commit

Permalink
Introduce virt-xml into filesystem creation/deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
skobyda committed Sep 28, 2021
1 parent 9c9c88c commit 94f8c9d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 88 deletions.
6 changes: 3 additions & 3 deletions src/components/vm/filesystems/vmFilesystemsCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import "./vmFilesystemsCard.scss";

const _ = cockpit.gettext;

export const VmFilesystemsCard = ({ connectionName, vmName, vmState, objPath, filesystems }) => {
export const VmFilesystemsCard = ({ connectionName, vmName, vmState, filesystems }) => {
const [deleteDialogProps, setDeleteDialogProps] = useState(undefined);
const columnTitles = [_("Source path"), _("Mount tag"), ""];

Expand All @@ -62,7 +62,7 @@ export const VmFilesystemsCard = ({ connectionName, vmName, vmState, objPath, fi
actionName: _("Remove"),
objectName: filesystemTarget,
onClose: () => setDeleteDialogProps(undefined),
deleteHandler: () => domainDeleteFilesystem({ connectionName, objPath, target: filesystemTarget }),
deleteHandler: () => domainDeleteFilesystem({ connectionName, vmName, target: filesystemTarget }),
})}
overlayText={_("Deleting shared directories is possible only when the guest is shut off")} />
</div>
Expand Down Expand Up @@ -143,7 +143,7 @@ const VmFilesystemAddModal = ({ connectionName, memory, objPath, setIsOpen, vmNa
memory
})
.then(() => domainCreateFilesystem({
connectionName, objPath,
connectionName, vmName,
source, target: mountTag,
xattr,
}))
Expand Down
1 change: 0 additions & 1 deletion src/components/vm/vmDetailsPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ export const VmDetailsPage = ({
memory={vm.memory} />,
body: <VmFilesystemsCard connectionName={vm.connectionName}
filesystems={vm.filesystems}
objPath={vm.id}
vmName={vm.name}
vmState={vm.state} />
}
Expand Down
30 changes: 0 additions & 30 deletions src/libvirt-xml-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,36 +231,6 @@ export function getSnapshotXML(name, description) {
return new XMLSerializer().serializeToString(doc.documentElement);
}

export function getFilesystemXML(source, target, xattr) {
const doc = document.implementation.createDocument('', '', null);

const filesystemElem = doc.createElement('filesystem');

const driverElem = doc.createElement('driver');
driverElem.setAttribute('type', 'virtiofs');
filesystemElem.appendChild(driverElem);

if (xattr) {
const binaryElem = doc.createElement('binary');
binaryElem.setAttribute('xattr', 'on');
filesystemElem.appendChild(binaryElem);
}

const sourceElem = doc.createElement('source');
sourceElem.appendChild(doc.createTextNode(name));
sourceElem.setAttribute('dir', source);
filesystemElem.appendChild(sourceElem);

const targetElem = doc.createElement('target');
targetElem.appendChild(doc.createTextNode(name));
targetElem.setAttribute('dir', target);
filesystemElem.appendChild(targetElem);

doc.appendChild(filesystemElem);

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

export function getMemoryBackingXML(type, memory) {
const doc = document.implementation.createDocument('', '', null);

Expand Down
25 changes: 0 additions & 25 deletions src/libvirt-xml-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,28 +339,3 @@ export function updateMaxMemory(domXml, maxMemory) {

return s.serializeToString(doc);
}

export function deleteFilesystem(domXml, target) {
const s = new XMLSerializer();
const doc = getDoc(domXml);
const domainElem = doc.firstElementChild;

if (!domainElem)
throw new Error("deleteFilesystem: domXML has no domain element");

const devicesElem = domainElem.getElementsByTagName('devices')[0];
const filesystemElems = devicesElem.getElementsByTagName('filesystem');

if (filesystemElems) {
for (let i = 0; i < filesystemElems.length; i++) {
const filesystemElem = filesystemElems[i];
const targetElem = filesystemElem.getElementsByTagName('target')[0];
const dir = targetElem.getAttribute('dir');
if (dir === target) {
filesystemElem.remove();
break;
}
}
}
return s.serializeToString(doc);
}
54 changes: 25 additions & 29 deletions src/libvirtApi/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import {
} from '../actions/store-actions.js';
import {
getDiskXML,
getFilesystemXML,
getMemoryBackingXML,
} from '../libvirt-xml-create.js';
import {
Expand All @@ -65,7 +64,6 @@ import {
parseDumpxml,
} from '../libvirt-xml-parse.js';
import {
deleteFilesystem as deleteFilesystemXML,
updateBootOrder,
updateDisk,
updateMaxMemory,
Expand Down Expand Up @@ -320,24 +318,22 @@ export function domainCreate({
});
}

export function domainCreateFilesystem({ connectionName, objPath, source, target, xattr }) {
return call(connectionName, objPath, 'org.libvirt.Domain', 'GetXMLDesc', [Enum.VIR_DOMAIN_XML_INACTIVE], { timeout, type: 'u' })
.then(domXml => {
const xmlDesc = getFilesystemXML(source, target, xattr);
if (!xmlDesc) {
return Promise.reject(new Error("Could not generate filesystem device XML"));
} else {
const doc = getDoc(domXml);
const domainElem = doc.firstElementChild;
const deviceElem = domainElem.getElementsByTagName("devices")[0];
const filesystemElem = getElem(xmlDesc);
const s = new XMLSerializer();

deviceElem.appendChild(filesystemElem);

return call(connectionName, '/org/libvirt/QEMU', 'org.libvirt.Connect', 'DomainDefineXML', [s.serializeToString(doc)], { timeout, type: 's' });
}
});
export function domainCreateFilesystem({ connectionName, vmName, source, target, xattr }) {
const options = { err: "message" };
if (connectionName === "system")
options.superuser = "try";

let xattrOption = "";
if (xattr)
xattrOption = ",binary.xattr=on";

return cockpit.spawn(
[
'virt-xml', '-c', `qemu:///${connectionName}`, vmName, '--add-device', '--filesystem',
`type=mount,accessmode=passthrough,driver.type=virtiofs,source.dir=${source},target.dir=${target}${xattrOption}`
],
options
);
}

export function domainDelete({
Expand Down Expand Up @@ -409,15 +405,15 @@ export function domainDelete({
}
}

export function domainDeleteFilesystem({ connectionName, objPath, target }) {
return call(connectionName, objPath, 'org.libvirt.Domain', 'GetXMLDesc', [Enum.VIR_DOMAIN_XML_INACTIVE], { timeout, type: 'u' })
.then(domXml => {
const xmlDesc = deleteFilesystemXML(domXml[0], target);
if (!xmlDesc)
return Promise.reject(new Error("Could not delete filesystem device"));
else
return call(connectionName, '/org/libvirt/QEMU', 'org.libvirt.Connect', 'DomainDefineXML', [xmlDesc], { timeout, type: 's' });
});
export function domainDeleteFilesystem({ connectionName, vmName, target }) {
const options = { err: "message" };
if (connectionName === "system")
options.superuser = "try";

return cockpit.spawn(
['virt-xml', '-c', `qemu:///${connectionName}`, vmName, '--remove-device', '--filesystem', `target.dir=${target}`],
options
);
}

/*
Expand Down

0 comments on commit 94f8c9d

Please sign in to comment.