Hi,
"attach-disk" does not give an error,
when the domain which is attached has already used the source which attach.
This has danger of the data corruption.
I make the patch to add the check of this.
This patch outputs an error,
when the domain which is attached has already used the source which attach.
Thanks,
Shigeki Sakamoto.
Index: src/virsh.c
===================================================================
RCS file: /data/cvs/libvirt/src/virsh.c,v
retrieving revision 1.170
diff -u -p -r1.170 virsh.c
--- src/virsh.c 13 Oct 2008 16:46:29 -0000 1.170
+++ src/virsh.c 4 Nov 2008 07:28:20 -0000
@@ -4993,6 +4993,11 @@ cmdAttachDisk(vshControl *ctl, const vsh
char *source, *target, *driver, *subdriver, *type, *mode;
int isFile = 0, ret = FALSE;
char *buf = NULL, *tmp = NULL;
+ xmlDocPtr xml = NULL;
+ xmlXPathObjectPtr obj = NULL;
+ xmlXPathContextPtr ctxt = NULL;
+ char *doc;
+ char xpath[PATH_MAX];
if (!vshConnectionUsability(ctl, ctl->conn, TRUE))
goto cleanup;
@@ -5034,6 +5039,35 @@ cmdAttachDisk(vshControl *ctl, const vsh
}
}
+ if (source) {
+ doc = virDomainGetXMLDesc(dom, 0);
+ if (!doc)
+ goto cleanup;
+
+ xml = xmlReadDoc((const xmlChar *) doc, "domain.xml", NULL,
+ XML_PARSE_NOENT | XML_PARSE_NONET |
+ XML_PARSE_NOWARNING);
+ free(doc);
+ if (!xml)
+ goto cleanup;
+ ctxt = xmlXPathNewContext(xml);
+ if (!ctxt)
+ goto cleanup;
+
+ sprintf(xpath,
"string(/domain/devices/disk/source[@dev='%s']/@dev)", source);
+ obj = xmlXPathEval(BAD_CAST xpath, ctxt);
+ if (!strcmp(obj->stringval, source)) {
+ vshError(ctl, FALSE, _("Disk %s is already in use by own guest"),
source);
+ goto cleanup;
+ }
+ sprintf(xpath,
"string(/domain/devices/disk/source[@file='%s']/@file)", source);
+ obj = xmlXPathEval(BAD_CAST xpath, ctxt);
+ if (!strcmp(obj->stringval, source)) {
+ vshError(ctl, FALSE, _("Disk %s is already in use by own guest"),
source);
+ goto cleanup;
+ }
+ }
+
/* Make XML of disk */
tmp = vshMalloc(ctl, 1);
if (!tmp) goto cleanup;
@@ -5123,6 +5157,10 @@ cmdAttachDisk(vshControl *ctl, const vsh
ret = TRUE;
cleanup:
+ xmlXPathFreeObject(obj);
+ xmlXPathFreeContext(ctxt);
+ if (xml)
+ xmlFreeDoc(xml);
if (dom)
virDomainFree(dom);
free(buf);