Use this function in the qemu, uml, lxc, and test drivers.
return NULL;
}
+/*
+ * virDomainObjIsDuplicate:
+ * @doms : virDomainObjListPtr to search
+ * @def : virDomainDefPtr definition of domain to lookup
+ * @check_active: If true, ensure that domain is not active
+ *
+ * Returns: -1 on error
+ * 0 if domain is new
+ * 1 if domain is a duplicate
+ */
+int
+virDomainObjIsDuplicate(virDomainObjListPtr doms,
+ virDomainDefPtr def,
+ unsigned int check_active)
+{
+ int ret = -1;
+ int dupVM = 0;
+ virDomainObjPtr vm = NULL;
+
+ /* See if a VM with matching UUID already exists */
+ vm = virDomainFindByUUID(doms, def->uuid);
+ if (vm) {
+ /* UUID matches, but if names don't match, refuse it */
+ if (STRNEQ(vm->def->name, def->name)) {
+ char uuidstr[VIR_UUID_STRING_BUFLEN];
+ virUUIDFormat(vm->def->uuid, uuidstr);
+ virDomainReportError(NULL, VIR_ERR_OPERATION_FAILED,
+ _("domain '%s' is already defined with uuid %s"),
+ vm->def->name, uuidstr);
+ goto cleanup;
+ }
+
+ if (check_active) {
+ /* UUID & name match, but if VM is already active, refuse it */
+ if (virDomainObjIsActive(vm)) {
+ virDomainReportError(NULL, VIR_ERR_OPERATION_INVALID,
+ _("domain is already active as '%s'"),
+ vm->def->name);
+ goto cleanup;
+ }
+ }
+
+ dupVM = 1;
+ virDomainObjUnlock(vm);
+ } else {
+ /* UUID does not match, but if a name matches, refuse it */
+ vm = virDomainFindByName(doms, def->name);
+ if (vm) {
+ char uuidstr[VIR_UUID_STRING_BUFLEN];
+ virUUIDFormat(vm->def->uuid, uuidstr);
+ virDomainReportError(NULL, VIR_ERR_OPERATION_FAILED,
+ _("domain '%s' already exists with uuid %s"),
+ def->name, uuidstr);
+ goto cleanup;
+ }
+ }
+
+ ret = dupVM;
+cleanup:
+ if (vm)
+ virDomainObjUnlock(vm);
+ return ret;
+}
+
void virDomainObjLock(virDomainObjPtr obj)
{
int virDomainVideoDefaultType(virDomainDefPtr def);
int virDomainVideoDefaultRAM(virDomainDefPtr def, int type);
+int virDomainObjIsDuplicate(virDomainObjListPtr doms,
+ virDomainDefPtr def,
+ unsigned int check_active);
+
void virDomainObjLock(virDomainObjPtr obj);
void virDomainObjUnlock(virDomainObjPtr obj);
virDomainObjUnlock;
virDomainStateTypeToString;
virDomainStateTypeFromString;
+virDomainObjIsDuplicate;
virDomainObjListGetInactiveNames;
virDomainObjListGetActiveIDs;
virDomainObjListNumOfDomains;
virDomainObjPtr vm = NULL;
virDomainPtr dom = NULL;
virDomainEventPtr event = NULL;
- int newVM = 1;
+ int dupVM;
lxcDriverLock(driver);
if (!(def = virDomainDefParseString(conn, driver->caps, xml,
VIR_DOMAIN_XML_INACTIVE)))
goto cleanup;
- /* See if a VM with matching UUID already exists */
- vm = virDomainFindByUUID(&driver->domains, def->uuid);
- if (vm) {
- /* UUID matches, but if names don't match, refuse it */
- if (STRNEQ(vm->def->name, def->name)) {
- char uuidstr[VIR_UUID_STRING_BUFLEN];
- virUUIDFormat(vm->def->uuid, uuidstr);
- lxcError(conn, NULL, VIR_ERR_OPERATION_FAILED,
- _("Domain '%s' is already defined with uuid %s"),
- vm->def->name, uuidstr);
- goto cleanup;
- }
-
- /* UUID & name match */
- virDomainObjUnlock(vm);
- newVM = 0;
- } else {
- /* UUID does not match, but if a name matches, refuse it */
- vm = virDomainFindByName(&driver->domains, def->name);
- if (vm) {
- char uuidstr[VIR_UUID_STRING_BUFLEN];
- virUUIDFormat(vm->def->uuid, uuidstr);
- lxcError(conn, NULL, VIR_ERR_OPERATION_FAILED,
- _("Domain '%s' is already defined with uuid %s"),
- def->name, uuidstr);
- goto cleanup;
- }
- }
+ if ((dupVM = virDomainObjIsDuplicate(&driver->domains, def, 0)) < 0)
+ goto cleanup;
if ((def->nets != NULL) && !(driver->have_netns)) {
lxcError(conn, NULL, VIR_ERR_NO_SUPPORT,
event = virDomainEventNewFromObj(vm,
VIR_DOMAIN_EVENT_DEFINED,
- newVM ?
+ !dupVM ?
VIR_DOMAIN_EVENT_DEFINED_ADDED :
VIR_DOMAIN_EVENT_DEFINED_UPDATED);
VIR_DOMAIN_XML_INACTIVE)))
goto cleanup;
- /* See if a VM with matching UUID already exists */
- vm = virDomainFindByUUID(&driver->domains, def->uuid);
- if (vm) {
- /* UUID matches, but if names don't match, refuse it */
- if (STRNEQ(vm->def->name, def->name)) {
- char uuidstr[VIR_UUID_STRING_BUFLEN];
- virUUIDFormat(vm->def->uuid, uuidstr);
- lxcError(conn, NULL, VIR_ERR_OPERATION_FAILED,
- _("Domain '%s' is already defined with uuid %s"),
- vm->def->name, uuidstr);
- goto cleanup;
- }
-
- /* UUID & name match, but if VM is already active, refuse it */
- if (virDomainObjIsActive(vm)) {
- lxcError(conn, NULL, VIR_ERR_OPERATION_FAILED,
- _("Domain is already active as '%s'"), vm->def->name);
- goto cleanup;
- }
- virDomainObjUnlock(vm);
- } else {
- /* UUID does not match, but if a name matches, refuse it */
- vm = virDomainFindByName(&driver->domains, def->name);
- if (vm) {
- char uuidstr[VIR_UUID_STRING_BUFLEN];
- virUUIDFormat(vm->def->uuid, uuidstr);
- lxcError(conn, NULL, VIR_ERR_OPERATION_FAILED,
- _("Domain '%s' is already defined with uuid %s"),
- def->name, uuidstr);
- goto cleanup;
- }
- }
+ if (virDomainObjIsDuplicate(&driver->domains, def, 1) < 0)
+ goto cleanup;
if ((def->nets != NULL) && !(driver->have_netns)) {
lxcError(conn, NULL, VIR_ERR_NO_SUPPORT,
if (virSecurityDriverVerify(conn, def) < 0)
goto cleanup;
- /* See if a VM with matching UUID already exists */
- vm = virDomainFindByUUID(&driver->domains, def->uuid);
- if (vm) {
- /* UUID matches, but if names don't match, refuse it */
- if (STRNEQ(vm->def->name, def->name)) {
- char uuidstr[VIR_UUID_STRING_BUFLEN];
- virUUIDFormat(vm->def->uuid, uuidstr);
- qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
- _("domain '%s' is already defined with uuid %s"),
- vm->def->name, uuidstr);
- goto cleanup;
- }
-
- /* UUID & name match, but if VM is already active, refuse it */
- if (virDomainObjIsActive(vm)) {
- qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
- _("domain is already active as '%s'"), vm->def->name);
- goto cleanup;
- }
- virDomainObjUnlock(vm);
- } else {
- /* UUID does not match, but if a name matches, refuse it */
- vm = virDomainFindByName(&driver->domains, def->name);
- if (vm) {
- char uuidstr[VIR_UUID_STRING_BUFLEN];
- virUUIDFormat(vm->def->uuid, uuidstr);
- qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
- _("domain '%s' is already defined with uuid %s"),
- def->name, uuidstr);
- goto cleanup;
- }
- }
+ if (virDomainObjIsDuplicate(&driver->domains, def, 1) < 0)
+ goto cleanup;
if (!(vm = virDomainAssignDef(conn,
driver->caps,
goto cleanup;
}
- /* See if a VM with matching UUID already exists */
- vm = virDomainFindByUUID(&driver->domains, def->uuid);
- if (vm) {
- /* UUID matches, but if names don't match, refuse it */
- if (STRNEQ(vm->def->name, def->name)) {
- char uuidstr[VIR_UUID_STRING_BUFLEN];
- virUUIDFormat(vm->def->uuid, uuidstr);
- qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
- _("domain '%s' is already defined with uuid %s"),
- vm->def->name, uuidstr);
- goto cleanup;
- }
-
- /* UUID & name match, but if VM is already active, refuse it */
- if (virDomainObjIsActive(vm)) {
- qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_INVALID,
- _("domain is already active as '%s'"), vm->def->name);
- goto cleanup;
- }
- virDomainObjUnlock(vm);
- } else {
- /* UUID does not match, but if a name matches, refuse it */
- vm = virDomainFindByName(&driver->domains, def->name);
- if (vm) {
- char uuidstr[VIR_UUID_STRING_BUFLEN];
- virUUIDFormat(vm->def->uuid, uuidstr);
- qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
- _("domain '%s' is already defined with uuid %s"),
- def->name, uuidstr);
- goto cleanup;
- }
- }
+ if (virDomainObjIsDuplicate(&driver->domains, def, 1) < 0)
+ goto cleanup;
if (!(vm = virDomainAssignDef(conn,
driver->caps,
virDomainObjPtr vm = NULL;
virDomainPtr dom = NULL;
virDomainEventPtr event = NULL;
- int newVM = 1;
+ int dupVM;
qemuDriverLock(driver);
if (!(def = virDomainDefParseString(conn, driver->caps, xml,
if (virSecurityDriverVerify(conn, def) < 0)
goto cleanup;
- /* See if a VM with matching UUID already exists */
- vm = virDomainFindByUUID(&driver->domains, def->uuid);
- if (vm) {
- /* UUID matches, but if names don't match, refuse it */
- if (STRNEQ(vm->def->name, def->name)) {
- char uuidstr[VIR_UUID_STRING_BUFLEN];
- virUUIDFormat(vm->def->uuid, uuidstr);
- qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
- _("domain '%s' is already defined with uuid %s"),
- vm->def->name, uuidstr);
- goto cleanup;
- }
-
- /* UUID & name match */
- virDomainObjUnlock(vm);
- newVM = 0;
- } else {
- /* UUID does not match, but if a name matches, refuse it */
- vm = virDomainFindByName(&driver->domains, def->name);
- if (vm) {
- char uuidstr[VIR_UUID_STRING_BUFLEN];
- virUUIDFormat(vm->def->uuid, uuidstr);
- qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
- _("domain '%s' is already defined with uuid %s"),
- def->name, uuidstr);
- goto cleanup;
- }
- }
+ if ((dupVM = virDomainObjIsDuplicate(&driver->domains, def, 0)) < 0)
+ goto cleanup;
if (qemudCanonicalizeMachine(driver, def) < 0)
goto cleanup;
event = virDomainEventNewFromObj(vm,
VIR_DOMAIN_EVENT_DEFINED,
- newVM ?
+ !dupVM ?
VIR_DOMAIN_EVENT_DEFINED_ADDED :
VIR_DOMAIN_EVENT_DEFINED_UPDATED);
VIR_DOMAIN_XML_INACTIVE)) == NULL)
goto cleanup;
+ if (virDomainObjIsDuplicate(&privconn->domains, def, 1) < 0)
+ goto cleanup;
+
if (testDomainGenerateIfnames(conn, def) < 0)
goto cleanup;
if (!(dom = virDomainAssignDef(conn, privconn->caps,
if (!def)
goto cleanup;
+ if (virDomainObjIsDuplicate(&privconn->domains, def, 1) < 0)
+ goto cleanup;
+
if (testDomainGenerateIfnames(conn, def) < 0)
goto cleanup;
if (!(dom = virDomainAssignDef(conn, privconn->caps,
virDomainDefPtr def;
virDomainObjPtr dom = NULL;
virDomainEventPtr event = NULL;
+ int dupVM;
testDriverLock(privconn);
if ((def = virDomainDefParseString(conn, privconn->caps, xml,
VIR_DOMAIN_XML_INACTIVE)) == NULL)
goto cleanup;
+ if ((dupVM = virDomainObjIsDuplicate(&privconn->domains, def, 0)) < 0)
+ goto cleanup;
+
if (testDomainGenerateIfnames(conn, def) < 0)
goto cleanup;
if (!(dom = virDomainAssignDef(conn, privconn->caps,
event = virDomainEventNewFromObj(dom,
VIR_DOMAIN_EVENT_DEFINED,
- VIR_DOMAIN_EVENT_DEFINED_ADDED);
+ !dupVM ?
+ VIR_DOMAIN_EVENT_DEFINED_ADDED :
+ VIR_DOMAIN_EVENT_DEFINED_UPDATED);
ret = virGetDomain(conn, dom->def->name, dom->def->uuid);
if (ret)
VIR_DOMAIN_XML_INACTIVE)))
goto cleanup;
- vm = virDomainFindByName(&driver->domains, def->name);
- if (vm) {
- umlReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
- _("domain '%s' is already defined"),
- def->name);
+ if (virDomainObjIsDuplicate(&driver->domains, def, 1) < 0)
goto cleanup;
- }
- vm = virDomainFindByUUID(&driver->domains, def->uuid);
- if (vm) {
- char uuidstr[VIR_UUID_STRING_BUFLEN];
-
- virUUIDFormat(def->uuid, uuidstr);
- umlReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED,
- _("domain with uuid '%s' is already defined"),
- uuidstr);
- goto cleanup;
- }
if (!(vm = virDomainAssignDef(conn,
driver->caps,
VIR_DOMAIN_XML_INACTIVE)))
goto cleanup;
+ if (virDomainObjIsDuplicate(&driver->domains, def, 0) < 0)
+ goto cleanup;
+
if (!(vm = virDomainAssignDef(conn,
driver->caps,
&driver->domains,
# Output a valid definition, to be used as input.
$abs_top_builddir/tools/virsh -c test:///default dumpxml 1 > xml || fail=1
+# Change the VM name
+sed -i -e "s|<name>test</name>|<name>newtest</name>|g" xml
+
for i in before after; do
# The largest BUFSIZ I've seen is 128K. This is slightly larger.
printf %132000s ' ' > sp || fail=1
( test $i = before && cat sp xml || cat xml sp ) > $in || fail=1
$abs_top_builddir/tools/virsh --connect test:///default define $in > out || fail=1
- printf "Domain test defined from $in\n\n" > exp || fail=1
+ printf "Domain newtest defined from $in\n\n" > exp || fail=1
compare exp out || fail=1
done