Blame SOURCES/bz1384181-make_the_libvirt_backend_survive_libvirtd.patch

cb2ad0
diff -ur a/server/checkpoint.c b/server/checkpoint.c
cb2ad0
--- a/server/checkpoint.c	2014-06-23 15:56:09.000000000 -0400
cb2ad0
+++ b/server/checkpoint.c	2017-08-09 13:30:40.708689303 -0400
cb2ad0
@@ -79,7 +79,7 @@
cb2ad0
 {
cb2ad0
 	virt_list_t *list = NULL;
cb2ad0
 
cb2ad0
-	list = vl_get(vp, my_id);
cb2ad0
+	list = vl_get(vp, 1, my_id);
cb2ad0
 	if (!list)
cb2ad0
 		return -1;
cb2ad0
 
cb2ad0
diff -ur a/server/libvirt.c b/server/libvirt.c
cb2ad0
--- a/server/libvirt.c	2014-06-23 15:56:09.000000000 -0400
cb2ad0
+++ b/server/libvirt.c	2017-08-09 13:30:40.709689288 -0400
cb2ad0
@@ -1,5 +1,5 @@
cb2ad0
 /*
cb2ad0
-  Copyright Red Hat, Inc. 2006
cb2ad0
+  Copyright Red Hat, Inc. 2006-2017
cb2ad0
 
cb2ad0
   This program is free software; you can redistribute it and/or modify it
cb2ad0
   under the terms of the GNU General Public License as published by the
cb2ad0
@@ -61,13 +61,15 @@
cb2ad0
 
cb2ad0
 
cb2ad0
 #define NAME "libvirt"
cb2ad0
-#define VERSION "0.1"
cb2ad0
+#define VERSION "0.3"
cb2ad0
 
cb2ad0
 #define MAGIC 0x1e19317a
cb2ad0
 
cb2ad0
 struct libvirt_info {
cb2ad0
 	int magic;
cb2ad0
-	virConnectPtr vp;
cb2ad0
+	config_object_t *config;
cb2ad0
+	int vp_count;
cb2ad0
+	virConnectPtr *vp;
cb2ad0
 };
cb2ad0
 
cb2ad0
 #define VALIDATE(arg) \
cb2ad0
@@ -79,179 +81,128 @@
cb2ad0
 } while(0)
cb2ad0
 
cb2ad0
 
cb2ad0
-static inline int
cb2ad0
-wait_domain(const char *vm_name, virConnectPtr vp,
cb2ad0
-	    int timeout)
cb2ad0
-{
cb2ad0
-	int tries = 0;
cb2ad0
-	int response = 1;
cb2ad0
-	int ret;
cb2ad0
-	virDomainPtr vdp;
cb2ad0
-	virDomainInfo vdi;
cb2ad0
-	int uuid_check;
cb2ad0
-	
cb2ad0
-	uuid_check = is_uuid(vm_name);
cb2ad0
-
cb2ad0
-	if (uuid_check) {
cb2ad0
-		vdp = virDomainLookupByUUIDString(vp, (const char *)vm_name);
cb2ad0
-	} else {
cb2ad0
-		vdp = virDomainLookupByName(vp, vm_name);
cb2ad0
+static void
cb2ad0
+libvirt_init_libvirt_conf(struct libvirt_info *info) {
cb2ad0
+	config_object_t *config = info->config;
cb2ad0
+	int i = 0;
cb2ad0
+
cb2ad0
+	if (info->vp) {
cb2ad0
+		dbg_printf(2, "Lost libvirtd connection. Reinitializing.\n");
cb2ad0
+		for (i = 0 ; i < info->vp_count ; i++)
cb2ad0
+			virConnectClose(info->vp[i]);
cb2ad0
+		free(info->vp);
cb2ad0
+		info->vp = NULL;
cb2ad0
 	}
cb2ad0
-	if (!vdp)
cb2ad0
-		return 0;
cb2ad0
+	info->vp_count = 0;
cb2ad0
 
cb2ad0
-	/* Check domain liveliness.  If the domain is still here,
cb2ad0
-	   we return failure, and the client must then retry */
cb2ad0
-	/* XXX On the xen 3.0.4 API, we will be able to guarantee
cb2ad0
-	   synchronous virDomainDestroy, so this check will not
cb2ad0
-	   be necessary */
cb2ad0
 	do {
cb2ad0
-		if (++tries > timeout)
cb2ad0
-			break;
cb2ad0
+		virConnectPtr vp;
cb2ad0
+		virConnectPtr *vpl = NULL;
cb2ad0
+		char conf_attr[256];
cb2ad0
+		char value[1024];
cb2ad0
+		char *uri;
cb2ad0
+
cb2ad0
+		if (i != 0) {
cb2ad0
+			snprintf(conf_attr, sizeof(conf_attr),
cb2ad0
+				"backends/libvirt/@uri%d", i);
cb2ad0
+		} else
cb2ad0
+			snprintf(conf_attr, sizeof(conf_attr), "backends/libvirt/@uri");
cb2ad0
+		++i;
cb2ad0
 
cb2ad0
-		sleep(1);
cb2ad0
-		if (uuid_check) {
cb2ad0
-			vdp = virDomainLookupByUUIDString(vp,
cb2ad0
-					(const char *)vm_name);
cb2ad0
-		} else {
cb2ad0
-			vdp = virDomainLookupByName(vp, vm_name);
cb2ad0
-		}
cb2ad0
-		if (!vdp) {
cb2ad0
-			dbg_printf(2, "Domain no longer exists\n");
cb2ad0
-			response = 0;
cb2ad0
+		if (sc_get(config, conf_attr, value, sizeof(value)) != 0)
cb2ad0
 			break;
cb2ad0
-		}
cb2ad0
 
cb2ad0
-		memset(&vdi, 0, sizeof(vdi));
cb2ad0
-		ret = virDomainGetInfo(vdp, &vdi;;
cb2ad0
-		virDomainFree(vdp);
cb2ad0
-		if (ret < 0)
cb2ad0
+		uri = value;
cb2ad0
+		vp = virConnectOpen(uri);
cb2ad0
+		if (!vp) {
cb2ad0
+			dbg_printf(1, "[libvirt:INIT] Failed to connect to URI: %s\n", uri);
cb2ad0
 			continue;
cb2ad0
+		}
cb2ad0
 
cb2ad0
-		if (vdi.state == VIR_DOMAIN_SHUTOFF) {
cb2ad0
-			dbg_printf(2, "Domain has been shut off\n");
cb2ad0
-			response = 0;
cb2ad0
-			break;
cb2ad0
+		vpl = realloc(info->vp, sizeof(*info->vp) * (info->vp_count + 1));
cb2ad0
+		if (!vpl) {
cb2ad0
+			dbg_printf(1, "[libvirt:INIT] Out of memory allocating URI: %s\n",
cb2ad0
+				uri);
cb2ad0
+			virConnectClose(vp);
cb2ad0
+			continue;
cb2ad0
 		}
cb2ad0
-		
cb2ad0
-		dbg_printf(4, "Domain still exists (state %d) "
cb2ad0
-			   "after %d seconds\n",
cb2ad0
-			   vdi.state, tries);
cb2ad0
+
cb2ad0
+		info->vp = vpl;
cb2ad0
+		info->vp[info->vp_count++] = vp;
cb2ad0
+
cb2ad0
+		if (i > 1)
cb2ad0
+			dbg_printf(1, "[libvirt:INIT] Added URI%d %s\n", i - 1, uri);
cb2ad0
+		else
cb2ad0
+			dbg_printf(1, "[libvirt:INIT] Added URI %s\n", uri);
cb2ad0
 	} while (1);
cb2ad0
+}
cb2ad0
 
cb2ad0
-	return response;
cb2ad0
+
cb2ad0
+static int
cb2ad0
+libvirt_bad_connections(struct libvirt_info *info) {
cb2ad0
+	int bad = 0;
cb2ad0
+	int i;
cb2ad0
+
cb2ad0
+	for (i = 0 ; i < info->vp_count ; i++) {
cb2ad0
+		/*
cb2ad0
+		** Send a dummy command to trigger an error if libvirtd
cb2ad0
+		** died or restarted
cb2ad0
+		*/
cb2ad0
+		virConnectNumOfDomains(info->vp[i]);
cb2ad0
+		if (!virConnectIsAlive(info->vp[i])) {
cb2ad0
+			dbg_printf(1, "libvirt connection %d is dead\n", i);
cb2ad0
+			bad++;
cb2ad0
+		}
cb2ad0
+	}
cb2ad0
+
cb2ad0
+	if (info->vp_count < 1 || bad)
cb2ad0
+		libvirt_init_libvirt_conf(info);
cb2ad0
+
cb2ad0
+	return bad || info->vp_count < 1;
cb2ad0
 }
cb2ad0
 
cb2ad0
+static void
cb2ad0
+libvirt_validate_connections(struct libvirt_info *info) {
cb2ad0
+	while (1) {
cb2ad0
+		if (libvirt_bad_connections(info))
cb2ad0
+			sleep(1);
cb2ad0
+		else
cb2ad0
+			break;
cb2ad0
+	}
cb2ad0
+}
cb2ad0
 
cb2ad0
 static int
cb2ad0
 libvirt_null(const char *vm_name, void *priv)
cb2ad0
 {
cb2ad0
-	dbg_printf(5, "%s %s\n", __FUNCTION__, vm_name);
cb2ad0
+	dbg_printf(5, "ENTER %s %s\n", __FUNCTION__, vm_name);
cb2ad0
 	printf("NULL operation: returning failure\n");
cb2ad0
 	return 1;
cb2ad0
 }
cb2ad0
 
cb2ad0
 
cb2ad0
 static int
cb2ad0
-libvirt_off(const char *vm_name, const char *src,
cb2ad0
-	    uint32_t seqno, void *priv)
cb2ad0
+libvirt_off(const char *vm_name, const char *src, uint32_t seqno, void *priv)
cb2ad0
 {
cb2ad0
 	struct libvirt_info *info = (struct libvirt_info *)priv;
cb2ad0
-	virDomainPtr vdp;
cb2ad0
-	virDomainInfo vdi;
cb2ad0
-	int ret = -1;
cb2ad0
 
cb2ad0
-	dbg_printf(5, "%s %s\n", __FUNCTION__, vm_name);
cb2ad0
+	dbg_printf(5, "ENTER %s %s %u\n", __FUNCTION__, vm_name, seqno);
cb2ad0
 	VALIDATE(info);
cb2ad0
 
cb2ad0
-	if (is_uuid(vm_name)) {
cb2ad0
-		vdp = virDomainLookupByUUIDString(info->vp,
cb2ad0
-					    (const char *)vm_name);
cb2ad0
-	} else {
cb2ad0
-		vdp = virDomainLookupByName(info->vp, vm_name);
cb2ad0
-	}
cb2ad0
-
cb2ad0
-	if (!vdp) {
cb2ad0
-		dbg_printf(2, "Nothing to do - domain does not exist\n");
cb2ad0
-		return 1;
cb2ad0
-	}
cb2ad0
-
cb2ad0
-	if (((virDomainGetInfo(vdp, &vdi) == 0) &&
cb2ad0
-	     (vdi.state == VIR_DOMAIN_SHUTOFF))) {
cb2ad0
-		dbg_printf(2, "Nothing to do - domain is off\n");
cb2ad0
-		virDomainFree(vdp);
cb2ad0
-		return 0;
cb2ad0
-	}
cb2ad0
-
cb2ad0
-	syslog(LOG_NOTICE, "Destroying domain %s\n", vm_name);
cb2ad0
-	dbg_printf(2, "[OFF] Calling virDomainDestroy\n");
cb2ad0
-	ret = virDomainDestroy(vdp);
cb2ad0
-	if (ret < 0) {
cb2ad0
-		syslog(LOG_NOTICE, "Failed to destroy domain: %d\n", ret);
cb2ad0
-		printf("virDomainDestroy() failed: %d\n", ret);
cb2ad0
-		return 1;
cb2ad0
-	}
cb2ad0
-
cb2ad0
-	if (ret) {
cb2ad0
-		syslog(LOG_NOTICE,
cb2ad0
-		       "Domain %s still exists; fencing failed\n",
cb2ad0
-		       vm_name);
cb2ad0
-		printf("Domain %s still exists; fencing failed\n", vm_name);
cb2ad0
-		return 1;
cb2ad0
-	}
cb2ad0
-
cb2ad0
-	return 0;
cb2ad0
+	libvirt_validate_connections(info);
cb2ad0
+	return vm_off(info->vp, info->vp_count, vm_name);
cb2ad0
 }
cb2ad0
 
cb2ad0
 
cb2ad0
 static int
cb2ad0
-libvirt_on(const char *vm_name, const char *src,
cb2ad0
-	   uint32_t seqno, void *priv)
cb2ad0
+libvirt_on(const char *vm_name, const char *src, uint32_t seqno, void *priv)
cb2ad0
 {
cb2ad0
 	struct libvirt_info *info = (struct libvirt_info *)priv;
cb2ad0
-	virDomainPtr vdp;
cb2ad0
-	virDomainInfo vdi;
cb2ad0
-	int ret = -1;
cb2ad0
 
cb2ad0
-	dbg_printf(5, "%s %s\n", __FUNCTION__, vm_name);
cb2ad0
+	dbg_printf(5, "ENTER %s %s %u\n", __FUNCTION__, vm_name, seqno);
cb2ad0
 	VALIDATE(info);
cb2ad0
 
cb2ad0
-	if (is_uuid(vm_name)) {
cb2ad0
-		vdp = virDomainLookupByUUIDString(info->vp,
cb2ad0
-					    (const char *)vm_name);
cb2ad0
-	} else {
cb2ad0
-		vdp = virDomainLookupByName(info->vp, vm_name);
cb2ad0
-	}
cb2ad0
-
cb2ad0
-	if (vdp &&
cb2ad0
-	    ((virDomainGetInfo(vdp, &vdi) == 0) &&
cb2ad0
-	     (vdi.state != VIR_DOMAIN_SHUTOFF))) {
cb2ad0
-		dbg_printf(2, "Nothing to do - domain is running\n");
cb2ad0
-
cb2ad0
-		if (vdp)
cb2ad0
-			virDomainFree(vdp);
cb2ad0
-		return 0;
cb2ad0
-	}
cb2ad0
-
cb2ad0
-	syslog(LOG_NOTICE, "Starting domain %s\n", vm_name);
cb2ad0
-	dbg_printf(2, "[ON] Calling virDomainCreate\n");
cb2ad0
-	ret = virDomainCreate(vdp);
cb2ad0
-	if (ret < 0) {
cb2ad0
-		syslog(LOG_NOTICE, "Failed to start domain: %d\n", ret);
cb2ad0
-		printf("virDomainCreate() failed: %d\n", ret);
cb2ad0
-		return 1;
cb2ad0
-	}
cb2ad0
-
cb2ad0
-	if (ret) {
cb2ad0
-		syslog(LOG_NOTICE,
cb2ad0
-		       "Domain %s did not start\n",
cb2ad0
-		       vm_name);
cb2ad0
-		printf("Domain %s did not start\n", vm_name);
cb2ad0
-		return 1;
cb2ad0
-	}
cb2ad0
-	syslog(LOG_NOTICE, "Domain %s started\n", vm_name);
cb2ad0
-
cb2ad0
-	return 0;
cb2ad0
+	libvirt_validate_connections(info);
cb2ad0
+	return vm_on(info->vp, info->vp_count, vm_name);
cb2ad0
 }
cb2ad0
 
cb2ad0
 
cb2ad0
@@ -270,126 +221,25 @@
cb2ad0
 libvirt_status(const char *vm_name, void *priv)
cb2ad0
 {
cb2ad0
 	struct libvirt_info *info = (struct libvirt_info *)priv;
cb2ad0
-	virDomainPtr vdp;
cb2ad0
-	virDomainInfo vdi;
cb2ad0
-	int ret = 0;
cb2ad0
 
cb2ad0
-	dbg_printf(5, "%s %s\n", __FUNCTION__, vm_name);
cb2ad0
+	dbg_printf(5, "ENTER %s %s\n", __FUNCTION__, vm_name);
cb2ad0
 	VALIDATE(info);
cb2ad0
 
cb2ad0
-	if (is_uuid(vm_name)) {
cb2ad0
-		vdp = virDomainLookupByUUIDString(info->vp,
cb2ad0
-					    (const char *)vm_name);
cb2ad0
-	} else {
cb2ad0
-		vdp = virDomainLookupByName(info->vp, vm_name);
cb2ad0
-	}
cb2ad0
-
cb2ad0
-	if (!vdp || ((virDomainGetInfo(vdp, &vdi) == 0) &&
cb2ad0
-	     (vdi.state == VIR_DOMAIN_SHUTOFF))) {
cb2ad0
-		ret = RESP_OFF;
cb2ad0
-	}
cb2ad0
-
cb2ad0
-	if (vdp)
cb2ad0
-		virDomainFree(vdp);
cb2ad0
-	return ret;
cb2ad0
+	libvirt_validate_connections(info);
cb2ad0
+	return vm_status(info->vp, info->vp_count, vm_name);
cb2ad0
 }
cb2ad0
 
cb2ad0
 
cb2ad0
 static int
cb2ad0
-libvirt_reboot(const char *vm_name, const char *src,
cb2ad0
-	       uint32_t seqno, void *priv)
cb2ad0
+libvirt_reboot(const char *vm_name, const char *src, uint32_t seqno, void *priv)
cb2ad0
 {
cb2ad0
 	struct libvirt_info *info = (struct libvirt_info *)priv;
cb2ad0
-	virDomainPtr vdp, nvdp;
cb2ad0
-	virDomainInfo vdi;
cb2ad0
-	char *domain_desc;
cb2ad0
-	int ret;
cb2ad0
 
cb2ad0
-	//uuid_unparse(vm_uuid, uu_string);
cb2ad0
-	dbg_printf(5, "%s %s\n", __FUNCTION__, vm_name);
cb2ad0
+	dbg_printf(5, "ENTER %s %s %u\n", __FUNCTION__, vm_name, seqno);
cb2ad0
 	VALIDATE(info);
cb2ad0
-	
cb2ad0
-	if (is_uuid(vm_name)) {
cb2ad0
-		vdp = virDomainLookupByUUIDString(info->vp,
cb2ad0
-					    (const char *)vm_name);
cb2ad0
-	} else {
cb2ad0
-		vdp = virDomainLookupByName(info->vp, vm_name);
cb2ad0
-	}
cb2ad0
-
cb2ad0
-	if (!vdp) {
cb2ad0
-		dbg_printf(2, "[libvirt:REBOOT] Nothing to "
cb2ad0
-			   "do - domain does not exist\n");
cb2ad0
-		return 1;
cb2ad0
-	}
cb2ad0
-
cb2ad0
-	if (((virDomainGetInfo(vdp, &vdi) == 0) &&
cb2ad0
-	     (vdi.state == VIR_DOMAIN_SHUTOFF))) {
cb2ad0
-			dbg_printf(2, "[libvirt:REBOOT] Nothing to "
cb2ad0
-				   "do - domain is off\n");
cb2ad0
-		virDomainFree(vdp);
cb2ad0
-		return 0;
cb2ad0
-	}
cb2ad0
-
cb2ad0
-
cb2ad0
-	syslog(LOG_NOTICE, "Rebooting domain %s\n", vm_name);
cb2ad0
-	printf("Rebooting domain %s...\n", vm_name);
cb2ad0
-	domain_desc = virDomainGetXMLDesc(vdp, 0);
cb2ad0
-
cb2ad0
-	if (!domain_desc) {
cb2ad0
-		printf("Failed getting domain description from "
cb2ad0
-		       "libvirt\n");
cb2ad0
-	}
cb2ad0
-
cb2ad0
-	dbg_printf(2, "[REBOOT] Calling virDomainDestroy(%p)\n", vdp);
cb2ad0
-	ret = virDomainDestroy(vdp);
cb2ad0
-	if (ret < 0) {
cb2ad0
-		printf("virDomainDestroy() failed: %d/%d\n", ret, errno);
cb2ad0
-		free(domain_desc);
cb2ad0
-		virDomainFree(vdp);
cb2ad0
-		return 1;
cb2ad0
-	}
cb2ad0
-
cb2ad0
-	ret = wait_domain(vm_name, info->vp, 15);
cb2ad0
-
cb2ad0
-	if (ret) {
cb2ad0
-		syslog(LOG_NOTICE, "Domain %s still exists; fencing failed\n",
cb2ad0
-		       vm_name);
cb2ad0
-		printf("Domain %s still exists; fencing failed\n", vm_name);
cb2ad0
-		if (domain_desc)
cb2ad0
-			free(domain_desc);
cb2ad0
-		return 1;
cb2ad0
-	}
cb2ad0
-		
cb2ad0
-	if (!domain_desc)
cb2ad0
-		return 0;
cb2ad0
 
cb2ad0
-	/* 'on' is not a failure */
cb2ad0
-	ret = 0;
cb2ad0
-
cb2ad0
-	dbg_printf(3, "[[ XML Domain Info ]]\n");
cb2ad0
-	dbg_printf(3, "%s\n[[ XML END ]]\n", domain_desc);
cb2ad0
-	dbg_printf(2, "Calling virDomainCreateLinux()...\n");
cb2ad0
-
cb2ad0
-	nvdp = virDomainCreateLinux(info->vp, domain_desc, 0);
cb2ad0
-	if (nvdp == NULL) {
cb2ad0
-		/* More recent versions of libvirt or perhaps the
cb2ad0
-		 * KVM back-end do not let you create a domain from
cb2ad0
-		 * XML if there is already a defined domain description
cb2ad0
-		 * with the same name that it knows about.  You must
cb2ad0
-		 * then call virDomainCreate() */
cb2ad0
-		dbg_printf(2, "Failed; Trying virDomainCreate()...\n");
cb2ad0
-		if (virDomainCreate(vdp) < 0) {
cb2ad0
-			syslog(LOG_NOTICE,
cb2ad0
-			       "Could not restart %s\n",
cb2ad0
-			       vm_name);
cb2ad0
-			dbg_printf(1, "Failed to recreate guest"
cb2ad0
-				   " %s!\n", vm_name);
cb2ad0
-		}
cb2ad0
-	}
cb2ad0
-
cb2ad0
-	free(domain_desc);
cb2ad0
-
cb2ad0
-	return ret;
cb2ad0
+	libvirt_validate_connections(info);
cb2ad0
+	return vm_reboot(info->vp, info->vp_count, vm_name);
cb2ad0
 }
cb2ad0
 
cb2ad0
 
cb2ad0
@@ -400,22 +250,27 @@
cb2ad0
 	virt_list_t *vl;
cb2ad0
 	int x;
cb2ad0
 
cb2ad0
-	dbg_printf(5, "%s\n", __FUNCTION__);
cb2ad0
+	dbg_printf(5, "ENTER %s\n", __FUNCTION__);
cb2ad0
 	VALIDATE(info);
cb2ad0
 
cb2ad0
-	vl = vl_get(info->vp, 1);
cb2ad0
+	libvirt_validate_connections(info);
cb2ad0
+
cb2ad0
+	vl = vl_get(info->vp, info->vp_count, 1);
cb2ad0
 	if (!vl)
cb2ad0
-		return 1;
cb2ad0
+		return 0;
cb2ad0
 
cb2ad0
 	for (x = 0; x < vl->vm_count; x++) {
cb2ad0
-		dbg_printf(10, "Sending %s\n", vl->vm_states[x].v_uuid);
cb2ad0
 		callback(vl->vm_states[x].v_name,
cb2ad0
-			 vl->vm_states[x].v_uuid,
cb2ad0
-			 vl->vm_states[x].v_state.s_state, arg);
cb2ad0
+				 vl->vm_states[x].v_uuid,
cb2ad0
+				 vl->vm_states[x].v_state.s_state, arg);
cb2ad0
+
cb2ad0
+		dbg_printf(10, "[libvirt:HOSTLIST] Sent %s %s %d\n",
cb2ad0
+			vl->vm_states[x].v_name,
cb2ad0
+			vl->vm_states[x].v_uuid,
cb2ad0
+			vl->vm_states[x].v_state.s_state);
cb2ad0
 	}
cb2ad0
 
cb2ad0
 	vl_free(vl);
cb2ad0
-
cb2ad0
 	return 0;
cb2ad0
 }
cb2ad0
 
cb2ad0
@@ -423,46 +278,33 @@
cb2ad0
 static int
cb2ad0
 libvirt_init(backend_context_t *c, config_object_t *config)
cb2ad0
 {
cb2ad0
-	virConnectPtr vp;
cb2ad0
 	char value[256];
cb2ad0
 	struct libvirt_info *info = NULL;
cb2ad0
-	char *uri = NULL;
cb2ad0
 
cb2ad0
-	info = malloc(sizeof(*info));
cb2ad0
+	dbg_printf(5, "ENTER [%s:%d %s]\n", __FILE__, __LINE__, __FUNCTION__);
cb2ad0
+
cb2ad0
+	info = calloc(1, sizeof(*info));
cb2ad0
 	if (!info)
cb2ad0
 		return -1;
cb2ad0
+	info->magic = MAGIC;
cb2ad0
+	info->config = config;
cb2ad0
 
cb2ad0
-	dbg_printf(5, "[%s:%d %s]\n", __FILE__, __LINE__, __FUNCTION__);
cb2ad0
-	memset(info, 0, sizeof(*info));
cb2ad0
+	libvirt_init_libvirt_conf(info);
cb2ad0
 
cb2ad0
 #ifdef _MODULE
cb2ad0
-	if (sc_get(config, "fence_virtd/@debug", value, sizeof(value))==0)
cb2ad0
+	if (sc_get(config, "fence_virtd/@debug", value, sizeof(value)) == 0)
cb2ad0
 		dset(atoi(value));
cb2ad0
 #endif
cb2ad0
 
cb2ad0
-	if (sc_get(config, "backends/libvirt/@uri",
cb2ad0
-		   value, sizeof(value)) == 0) {
cb2ad0
-		uri = strdup(value);
cb2ad0
-		if (!uri) {
cb2ad0
-			free(info);
cb2ad0
-			return -1;
cb2ad0
-		}
cb2ad0
-		dbg_printf(1, "Using %s\n", uri);
cb2ad0
-	}
cb2ad0
-
cb2ad0
-	/* We don't need to store the URI; we only use it once */
cb2ad0
-	vp = virConnectOpen(uri);
cb2ad0
-	if (!vp) {
cb2ad0
-		free(uri);
cb2ad0
+	if (info->vp_count < 1) {
cb2ad0
+		dbg_printf(1, "[libvirt:INIT] Could not connect to any hypervisors\n");
cb2ad0
+		if (info->vp)
cb2ad0
+			free(info->vp);
cb2ad0
 		free(info);
cb2ad0
 		return -1;
cb2ad0
 	}
cb2ad0
-	free(uri);
cb2ad0
-
cb2ad0
-	info->magic = MAGIC;
cb2ad0
-	info->vp = vp;
cb2ad0
 
cb2ad0
-	*c = (void *)info;
cb2ad0
+	*c = (void *) info;
cb2ad0
 	return 0;
cb2ad0
 }
cb2ad0
 
cb2ad0
@@ -471,16 +313,19 @@
cb2ad0
 libvirt_shutdown(backend_context_t c)
cb2ad0
 {
cb2ad0
 	struct libvirt_info *info = (struct libvirt_info *)c;
cb2ad0
+	int i;
cb2ad0
+	int ret = 0;
cb2ad0
 
cb2ad0
 	VALIDATE(info);
cb2ad0
 
cb2ad0
-	if (virConnectClose(info->vp) < 0) {
cb2ad0
-		free(info);
cb2ad0
-		return -errno;
cb2ad0
+	for (i = 0 ; i < info->vp_count ; i++) {
cb2ad0
+		if (virConnectClose(info->vp[i]) < 0)
cb2ad0
+			ret = -errno;
cb2ad0
 	}
cb2ad0
 
cb2ad0
+	free(info->vp);
cb2ad0
 	free(info);
cb2ad0
-	return 0;
cb2ad0
+	return ret;
cb2ad0
 }
cb2ad0
 
cb2ad0
 
cb2ad0
diff -ur a/server/virt.c b/server/virt.c
cb2ad0
--- a/server/virt.c	2014-06-23 15:56:09.000000000 -0400
cb2ad0
+++ b/server/virt.c	2017-08-09 13:35:07.247875913 -0400
cb2ad0
@@ -1,5 +1,5 @@
cb2ad0
 /*
cb2ad0
-  Copyright Red Hat, Inc. 2006
cb2ad0
+  Copyright Red Hat, Inc. 2006-2017
cb2ad0
 
cb2ad0
   This program is free software; you can redistribute it and/or modify it
cb2ad0
   under the terms of the GNU General Public License as published by the
cb2ad0
@@ -16,7 +16,9 @@
cb2ad0
   Free Software Foundation, Inc.,  675 Mass Ave, Cambridge, 
cb2ad0
   MA 02139, USA.
cb2ad0
 */
cb2ad0
+
cb2ad0
 #include <stdio.h>
cb2ad0
+#include <unistd.h>
cb2ad0
 #include <sys/types.h>
cb2ad0
 #include <stdlib.h>
cb2ad0
 #include <libvirt/libvirt.h>
cb2ad0
@@ -24,8 +26,11 @@
cb2ad0
 #include <malloc.h>
cb2ad0
 #include <stdint.h>
cb2ad0
 #include <errno.h>
cb2ad0
-#include "virt.h"
cb2ad0
+#include <syslog.h>
cb2ad0
 
cb2ad0
+#include "debug.h"
cb2ad0
+#include "uuid-test.h"
cb2ad0
+#include "virt.h"
cb2ad0
 
cb2ad0
 static int
cb2ad0
 _compare_virt(const void *_left, const void *_right)
cb2ad0
@@ -37,96 +42,98 @@
cb2ad0
 }
cb2ad0
 
cb2ad0
 
cb2ad0
-virt_list_t *vl_get(virConnectPtr vp, int my_id)
cb2ad0
-{
cb2ad0
-	virt_list_t *vl = NULL;
cb2ad0
-	int d_count, x, saved_errno;
cb2ad0
-	virDomainPtr *dom_list;
cb2ad0
-
cb2ad0
-	errno = EINVAL;
cb2ad0
-	if (!vp)
cb2ad0
-		return NULL;
cb2ad0
-
cb2ad0
-	d_count = virConnectListAllDomains(vp, &dom_list, 0);
cb2ad0
-	if (d_count <= 0)
cb2ad0
-		goto out_fail;
cb2ad0
+static void
cb2ad0
+_free_dom_list(virDomainPtr *dom_list, int len) {
cb2ad0
+	int x;
cb2ad0
 
cb2ad0
-	vl = malloc(sizeof(uint32_t) + sizeof(virt_state_t) * d_count);
cb2ad0
-	if (!vl)
cb2ad0
-		goto out_fail;
cb2ad0
+	if (!dom_list || len <= 0)
cb2ad0
+		return;
cb2ad0
+	for (x = 0 ; x < len; x++)
cb2ad0
+		virDomainFree(dom_list[x]);
cb2ad0
 
cb2ad0
-	vl->vm_count = d_count;
cb2ad0
+	free(dom_list);
cb2ad0
+}
cb2ad0
 
cb2ad0
-	/* Ok, we have the domain IDs - let's get their names and states */
cb2ad0
-	for (x = 0; x < d_count; x++) {
cb2ad0
-		char *d_name;
cb2ad0
-		virDomainInfo d_info;
cb2ad0
-		char d_uuid[MAX_DOMAINNAME_LENGTH];
cb2ad0
-		virDomainPtr dom = dom_list[x];
cb2ad0
 
cb2ad0
-		if (!(d_name = (char *)virDomainGetName(dom)))
cb2ad0
-			goto out_fail;
cb2ad0
+virt_list_t *vl_get(virConnectPtr *vp, int vp_count, int my_id)
cb2ad0
+{
cb2ad0
+	virt_list_t *vl = NULL;
cb2ad0
+	int d_count = 0;
cb2ad0
+	int i;
cb2ad0
 
cb2ad0
-		if (virDomainGetUUIDString(dom, d_uuid) != 0)
cb2ad0
-			goto out_fail;
cb2ad0
+	errno = EINVAL;
cb2ad0
+	if (!vp || vp_count < 1)
cb2ad0
+		return NULL;
cb2ad0
 
cb2ad0
-		if (virDomainGetInfo(dom, &d_info) < 0)
cb2ad0
-			goto out_fail;
cb2ad0
+	for (i = 0 ; i < vp_count ; i++) {
cb2ad0
+		int x;
cb2ad0
+		virDomainPtr *dom_list;
cb2ad0
+		virt_list_t *new_vl;
cb2ad0
+
cb2ad0
+		int ret = virConnectListAllDomains(vp[i], &dom_list, 0);
cb2ad0
+		if (ret == 0)
cb2ad0
+			continue;
cb2ad0
+
cb2ad0
+		if (ret < 0) {
cb2ad0
+			int saved_errno = errno;
cb2ad0
+			dbg_printf(2, "Error: virConnectListAllDomains: %d %d\n",
cb2ad0
+				ret, saved_errno);
cb2ad0
+			if (vl)
cb2ad0
+				free(vl);
cb2ad0
+			errno = saved_errno;
cb2ad0
+			return NULL;
cb2ad0
+		}
cb2ad0
+
cb2ad0
+		d_count += ret;
cb2ad0
+		new_vl = realloc(vl, sizeof(uint32_t) + sizeof(virt_state_t) * d_count);
cb2ad0
+		if (!new_vl) {
cb2ad0
+			_free_dom_list(dom_list, ret);
cb2ad0
+			free(vl);
cb2ad0
+			return NULL;
cb2ad0
+		}
cb2ad0
+		vl = new_vl;
cb2ad0
+		vl->vm_count = d_count;
cb2ad0
+
cb2ad0
+		/* Ok, we have the domain IDs - let's get their names and states */
cb2ad0
+		for (x = 0; x < ret; x++) {
cb2ad0
+			char *d_name;
cb2ad0
+			virDomainInfo d_info;
cb2ad0
+			char d_uuid[MAX_DOMAINNAME_LENGTH];
cb2ad0
+			virDomainPtr dom = dom_list[x];
cb2ad0
+
cb2ad0
+			if (!(d_name = (char *)virDomainGetName(dom))) {
cb2ad0
+				_free_dom_list(dom_list, ret);
cb2ad0
+				free(vl);
cb2ad0
+				return NULL;
cb2ad0
+			}
cb2ad0
+
cb2ad0
+			if (virDomainGetUUIDString(dom, d_uuid) != 0) {
cb2ad0
+				_free_dom_list(dom_list, ret);
cb2ad0
+				free(vl);
cb2ad0
+				return NULL;
cb2ad0
+			}
cb2ad0
+
cb2ad0
+			if (virDomainGetInfo(dom, &d_info) < 0) {
cb2ad0
+				_free_dom_list(dom_list, ret);
cb2ad0
+				free(vl);
cb2ad0
+				return NULL;
cb2ad0
+			}
cb2ad0
+
cb2ad0
+			/* Store the name & state */
cb2ad0
+			strncpy(vl->vm_states[x].v_name, d_name, MAX_DOMAINNAME_LENGTH);
cb2ad0
+			strncpy(vl->vm_states[x].v_uuid, d_uuid, MAX_DOMAINNAME_LENGTH);
cb2ad0
+			vl->vm_states[x].v_state.s_state = d_info.state;
cb2ad0
+			vl->vm_states[x].v_state.s_owner = my_id;
cb2ad0
+		}
cb2ad0
 
cb2ad0
-		/* Store the name & state */
cb2ad0
-		strncpy(vl->vm_states[x].v_name, d_name, MAX_DOMAINNAME_LENGTH);
cb2ad0
-		strncpy(vl->vm_states[x].v_uuid, d_uuid, MAX_DOMAINNAME_LENGTH);
cb2ad0
-		vl->vm_states[x].v_state.s_state = d_info.state;
cb2ad0
-		vl->vm_states[x].v_state.s_owner = my_id;
cb2ad0
+		_free_dom_list(dom_list, ret);
cb2ad0
 	}
cb2ad0
 
cb2ad0
-	for (x = 0 ; x < d_count; x++)
cb2ad0
-		virDomainFree(dom_list[x]);
cb2ad0
-	free(dom_list);
cb2ad0
-
cb2ad0
 	/* We have all the locally running domains & states now */
cb2ad0
 	/* Sort */
cb2ad0
 	qsort(&vl->vm_states[0], vl->vm_count, sizeof(vl->vm_states[0]),
cb2ad0
 	      _compare_virt);
cb2ad0
 	return vl;	
cb2ad0
-
cb2ad0
-out_fail:
cb2ad0
-	saved_errno = errno;
cb2ad0
-	for (x = 0 ; x < d_count; x++)
cb2ad0
-		virDomainFree(dom_list[x]);
cb2ad0
-	free(dom_list);
cb2ad0
-
cb2ad0
-	if (vl)
cb2ad0
-		free(vl);
cb2ad0
-	errno = saved_errno;
cb2ad0
-	return NULL;
cb2ad0
-}
cb2ad0
-
cb2ad0
-
cb2ad0
-/* Returns 0 if equal, nonzero if not */
cb2ad0
-int
cb2ad0
-vl_cmp(virt_list_t *left, virt_list_t *right)
cb2ad0
-{
cb2ad0
-	int x;
cb2ad0
-
cb2ad0
-	/* Quick checks */
cb2ad0
-	if (!left->vm_count && !right->vm_count)
cb2ad0
-		return 1;
cb2ad0
-	if (left->vm_count != right->vm_count)
cb2ad0
-		return 0;
cb2ad0
-
cb2ad0
-	for (x = 0; x < left->vm_count; x++) {
cb2ad0
-		if (strcmp(left->vm_states[x].v_name,
cb2ad0
-			   right->vm_states[x].v_name))
cb2ad0
-			return 1;
cb2ad0
-		/*
cb2ad0
-		if (left->vm_states[x].v_state.s_state !=
cb2ad0
-		    right->vm_states[x].v_state.s_state)
cb2ad0
-			return 1;
cb2ad0
-		 */
cb2ad0
-	}
cb2ad0
-
cb2ad0
-	return 0;
cb2ad0
 }
cb2ad0
 
cb2ad0
 
cb2ad0
@@ -192,3 +199,333 @@
cb2ad0
 {
cb2ad0
 	free(old);
cb2ad0
 }
cb2ad0
+
cb2ad0
+
cb2ad0
+static inline int
cb2ad0
+wait_domain(const char *vm_name, virConnectPtr vp, int timeout)
cb2ad0
+{
cb2ad0
+	int tries = 0;
cb2ad0
+	int response = 1;
cb2ad0
+	int ret;
cb2ad0
+	virDomainPtr vdp;
cb2ad0
+	virDomainInfo vdi;
cb2ad0
+	int uuid_check;
cb2ad0
+
cb2ad0
+	uuid_check = is_uuid(vm_name);
cb2ad0
+
cb2ad0
+	if (uuid_check) {
cb2ad0
+		vdp = virDomainLookupByUUIDString(vp, (const char *)vm_name);
cb2ad0
+	} else {
cb2ad0
+		vdp = virDomainLookupByName(vp, vm_name);
cb2ad0
+	}
cb2ad0
+	if (!vdp)
cb2ad0
+		return 0;
cb2ad0
+
cb2ad0
+	/* Check domain liveliness.  If the domain is still here,
cb2ad0
+	   we return failure, and the client must then retry */
cb2ad0
+	/* XXX On the xen 3.0.4 API, we will be able to guarantee
cb2ad0
+	   synchronous virDomainDestroy, so this check will not
cb2ad0
+	   be necessary */
cb2ad0
+	do {
cb2ad0
+		if (++tries > timeout)
cb2ad0
+			break;
cb2ad0
+
cb2ad0
+		sleep(1);
cb2ad0
+		if (uuid_check) {
cb2ad0
+			vdp = virDomainLookupByUUIDString(vp, (const char *)vm_name);
cb2ad0
+		} else {
cb2ad0
+			vdp = virDomainLookupByName(vp, vm_name);
cb2ad0
+		}
cb2ad0
+		if (!vdp) {
cb2ad0
+			dbg_printf(2, "Domain no longer exists\n");
cb2ad0
+			response = 0;
cb2ad0
+			break;
cb2ad0
+		}
cb2ad0
+
cb2ad0
+		memset(&vdi, 0, sizeof(vdi));
cb2ad0
+		ret = virDomainGetInfo(vdp, &vdi;;
cb2ad0
+		virDomainFree(vdp);
cb2ad0
+		if (ret < 0)
cb2ad0
+			continue;
cb2ad0
+
cb2ad0
+		if (vdi.state == VIR_DOMAIN_SHUTOFF) {
cb2ad0
+			dbg_printf(2, "Domain has been shut off\n");
cb2ad0
+			response = 0;
cb2ad0
+			break;
cb2ad0
+		}
cb2ad0
+
cb2ad0
+		dbg_printf(4, "Domain still exists (state %d) after %d seconds\n",
cb2ad0
+			vdi.state, tries);
cb2ad0
+	} while (1);
cb2ad0
+
cb2ad0
+	return response;
cb2ad0
+}
cb2ad0
+
cb2ad0
+
cb2ad0
+int
cb2ad0
+vm_off(virConnectPtr *vp, int vp_count, const char *vm_name)
cb2ad0
+{
cb2ad0
+	virDomainPtr vdp = NULL;
cb2ad0
+	virDomainInfo vdi;
cb2ad0
+	virDomainPtr (*virt_lookup_fn)(virConnectPtr, const char *);
cb2ad0
+	int ret = -1;
cb2ad0
+	int i;
cb2ad0
+
cb2ad0
+	if (is_uuid(vm_name))
cb2ad0
+		virt_lookup_fn = virDomainLookupByUUIDString;
cb2ad0
+	else
cb2ad0
+		virt_lookup_fn = virDomainLookupByName;
cb2ad0
+
cb2ad0
+	for (i = 0 ; i < vp_count ; i++) {
cb2ad0
+		vdp = virt_lookup_fn(vp[i], vm_name);
cb2ad0
+		if (vdp)
cb2ad0
+			break;
cb2ad0
+	}
cb2ad0
+
cb2ad0
+	if (!vdp) {
cb2ad0
+		dbg_printf(2, "[virt:OFF] Domain %s does not exist\n", vm_name);
cb2ad0
+		return 1;
cb2ad0
+	}
cb2ad0
+
cb2ad0
+	if (virDomainGetInfo(vdp, &vdi) == 0 && vdi.state == VIR_DOMAIN_SHUTOFF)
cb2ad0
+	{
cb2ad0
+		dbg_printf(2, "[virt:OFF] Nothing to do - "
cb2ad0
+			"domain %s is already off\n",
cb2ad0
+			vm_name);
cb2ad0
+		virDomainFree(vdp);
cb2ad0
+		return 0;
cb2ad0
+	}
cb2ad0
+
cb2ad0
+	syslog(LOG_NOTICE, "Destroying domain %s\n", vm_name);
cb2ad0
+	dbg_printf(2, "[virt:OFF] Calling virDomainDestroy for %s\n", vm_name);
cb2ad0
+
cb2ad0
+	ret = virDomainDestroy(vdp);
cb2ad0
+	virDomainFree(vdp);
cb2ad0
+
cb2ad0
+	if (ret < 0) {
cb2ad0
+		syslog(LOG_NOTICE,
cb2ad0
+			"Failed to destroy domain %s: %d\n", vm_name, ret);
cb2ad0
+		dbg_printf(2, "[virt:OFF] Failed to destroy domain: %s %d\n",
cb2ad0
+			vm_name, ret);
cb2ad0
+		return 1;
cb2ad0
+	}
cb2ad0
+
cb2ad0
+	if (ret) {
cb2ad0
+		syslog(LOG_NOTICE, "Domain %s still exists; fencing failed\n",
cb2ad0
+			vm_name);
cb2ad0
+		dbg_printf(2,
cb2ad0
+			"[virt:OFF] Domain %s still exists; fencing failed\n",
cb2ad0
+			vm_name);
cb2ad0
+		return 1;
cb2ad0
+	}
cb2ad0
+
cb2ad0
+	dbg_printf(2, "[virt:OFF] Success for %s\n", vm_name);
cb2ad0
+	return 0;
cb2ad0
+}
cb2ad0
+
cb2ad0
+
cb2ad0
+int
cb2ad0
+vm_on(virConnectPtr *vp, int vp_count, const char *vm_name)
cb2ad0
+{
cb2ad0
+	virDomainPtr vdp = NULL;
cb2ad0
+	virDomainInfo vdi;
cb2ad0
+	virDomainPtr (*virt_lookup_fn)(virConnectPtr, const char *);
cb2ad0
+	int ret = -1;
cb2ad0
+	int i;
cb2ad0
+
cb2ad0
+	if (is_uuid(vm_name))
cb2ad0
+		virt_lookup_fn = virDomainLookupByUUIDString;
cb2ad0
+	else
cb2ad0
+		virt_lookup_fn = virDomainLookupByName;
cb2ad0
+
cb2ad0
+	for (i = 0 ; i < vp_count ; i++) {
cb2ad0
+		vdp = virt_lookup_fn(vp[i], vm_name);
cb2ad0
+		if (vdp)
cb2ad0
+			break;
cb2ad0
+	}
cb2ad0
+
cb2ad0
+	if (!vdp) {
cb2ad0
+		dbg_printf(2, "[virt:ON] Domain %s does not exist\n", vm_name);
cb2ad0
+		return 1;
cb2ad0
+	}
cb2ad0
+
cb2ad0
+	if (virDomainGetInfo(vdp, &vdi) == 0 && vdi.state != VIR_DOMAIN_SHUTOFF) {
cb2ad0
+		dbg_printf(2, "Nothing to do - domain %s is already running\n",
cb2ad0
+			vm_name);
cb2ad0
+		virDomainFree(vdp);
cb2ad0
+		return 0;
cb2ad0
+	}
cb2ad0
+
cb2ad0
+	syslog(LOG_NOTICE, "Starting domain %s\n", vm_name);
cb2ad0
+	dbg_printf(2, "[virt:ON] Calling virDomainCreate for %s\n", vm_name);
cb2ad0
+
cb2ad0
+	ret = virDomainCreate(vdp);
cb2ad0
+	virDomainFree(vdp);
cb2ad0
+
cb2ad0
+	if (ret < 0) {
cb2ad0
+		syslog(LOG_NOTICE, "Failed to start domain %s: %d\n", vm_name, ret);
cb2ad0
+		dbg_printf(2, "[virt:ON] virDomainCreate() failed for %s: %d\n",
cb2ad0
+			vm_name, ret);
cb2ad0
+		return 1;
cb2ad0
+	}
cb2ad0
+
cb2ad0
+	if (ret) {
cb2ad0
+		syslog(LOG_NOTICE, "Domain %s did not start\n", vm_name);
cb2ad0
+		dbg_printf(2, "[virt:ON] Domain %s did not start\n", vm_name);
cb2ad0
+		return 1;
cb2ad0
+	}
cb2ad0
+
cb2ad0
+	syslog(LOG_NOTICE, "Domain %s started\n", vm_name);
cb2ad0
+	dbg_printf(2, "[virt:ON] Success for %s\n", vm_name);
cb2ad0
+	return 0;
cb2ad0
+}
cb2ad0
+
cb2ad0
+
cb2ad0
+int
cb2ad0
+vm_status(virConnectPtr *vp, int vp_count, const char *vm_name)
cb2ad0
+{
cb2ad0
+	virDomainPtr vdp = NULL;
cb2ad0
+	virDomainInfo vdi;
cb2ad0
+	int ret = 0;
cb2ad0
+	int i;
cb2ad0
+	virDomainPtr (*virt_lookup_fn)(virConnectPtr, const char *);
cb2ad0
+
cb2ad0
+	if (is_uuid(vm_name))
cb2ad0
+		virt_lookup_fn = virDomainLookupByUUIDString;
cb2ad0
+	else
cb2ad0
+		virt_lookup_fn = virDomainLookupByName;
cb2ad0
+
cb2ad0
+	for (i = 0 ; i < vp_count ; i++) {
cb2ad0
+		vdp = virt_lookup_fn(vp[i], vm_name);
cb2ad0
+		if (vdp)
cb2ad0
+			break;
cb2ad0
+	}
cb2ad0
+
cb2ad0
+	if (!vdp) {
cb2ad0
+		dbg_printf(2, "[virt:STATUS] Unknown VM %s - return OFF\n", vm_name);
cb2ad0
+		return RESP_OFF;
cb2ad0
+	}
cb2ad0
+
cb2ad0
+	if (virDomainGetInfo(vdp, &vdi) == 0 && vdi.state == VIR_DOMAIN_SHUTOFF) {
cb2ad0
+		dbg_printf(2, "[virt:STATUS] VM %s is OFF\n", vm_name);
cb2ad0
+		ret = RESP_OFF;
cb2ad0
+	}
cb2ad0
+
cb2ad0
+	if (vdp)
cb2ad0
+		virDomainFree(vdp);
cb2ad0
+	return ret;
cb2ad0
+}
cb2ad0
+
cb2ad0
+
cb2ad0
+int
cb2ad0
+vm_reboot(virConnectPtr *vp, int vp_count, const char *vm_name)
cb2ad0
+{
cb2ad0
+	virDomainPtr vdp = NULL, nvdp;
cb2ad0
+	virDomainInfo vdi;
cb2ad0
+	char *domain_desc;
cb2ad0
+	virConnectPtr vcp = NULL;
cb2ad0
+	virDomainPtr (*virt_lookup_fn)(virConnectPtr, const char *);
cb2ad0
+	int ret;
cb2ad0
+	int i;
cb2ad0
+
cb2ad0
+	if (is_uuid(vm_name))
cb2ad0
+		virt_lookup_fn = virDomainLookupByUUIDString;
cb2ad0
+	else
cb2ad0
+		virt_lookup_fn = virDomainLookupByName;
cb2ad0
+
cb2ad0
+	for (i = 0 ; i < vp_count ; i++) {
cb2ad0
+		vdp = virt_lookup_fn(vp[i], vm_name);
cb2ad0
+		if (vdp) {
cb2ad0
+			vcp = vp[i];
cb2ad0
+			break;
cb2ad0
+		}
cb2ad0
+	}
cb2ad0
+
cb2ad0
+	if (!vdp || !vcp) {
cb2ad0
+		dbg_printf(2,
cb2ad0
+			"[virt:REBOOT] Nothing to do - domain %s does not exist\n",
cb2ad0
+			vm_name);
cb2ad0
+		return 1;
cb2ad0
+	}
cb2ad0
+
cb2ad0
+	if (virDomainGetInfo(vdp, &vdi) == 0 && vdi.state == VIR_DOMAIN_SHUTOFF) {
cb2ad0
+		dbg_printf(2, "[virt:REBOOT] Nothing to do - domain %s is off\n",
cb2ad0
+			vm_name);
cb2ad0
+		virDomainFree(vdp);
cb2ad0
+		return 0;
cb2ad0
+	}
cb2ad0
+
cb2ad0
+	syslog(LOG_NOTICE, "Rebooting domain %s\n", vm_name);
cb2ad0
+	dbg_printf(5, "[virt:REBOOT] Rebooting domain %s...\n", vm_name);
cb2ad0
+
cb2ad0
+	domain_desc = virDomainGetXMLDesc(vdp, 0);
cb2ad0
+
cb2ad0
+	if (!domain_desc) {
cb2ad0
+		dbg_printf(5, "[virt:REBOOT] Failed getting domain description "
cb2ad0
+			"from libvirt for %s...\n", vm_name);
cb2ad0
+	}
cb2ad0
+
cb2ad0
+	dbg_printf(2, "[virt:REBOOT] Calling virDomainDestroy(%p) for %s\n",
cb2ad0
+		vdp, vm_name);
cb2ad0
+
cb2ad0
+	ret = virDomainDestroy(vdp);
cb2ad0
+	if (ret < 0) {
cb2ad0
+		dbg_printf(2,
cb2ad0
+			"[virt:REBOOT] virDomainDestroy() failed for %s: %d/%d\n",
cb2ad0
+			vm_name, ret, errno);
cb2ad0
+
cb2ad0
+		if (domain_desc)
cb2ad0
+			free(domain_desc);
cb2ad0
+		virDomainFree(vdp);
cb2ad0
+		return 1;
cb2ad0
+	}
cb2ad0
+
cb2ad0
+	ret = wait_domain(vm_name, vcp, 15);
cb2ad0
+
cb2ad0
+	if (ret) {
cb2ad0
+		syslog(LOG_NOTICE, "Domain %s still exists; fencing failed\n", vm_name);
cb2ad0
+		dbg_printf(2,
cb2ad0
+			"[virt:REBOOT] Domain %s still exists; fencing failed\n",
cb2ad0
+			vm_name);
cb2ad0
+
cb2ad0
+		if (domain_desc)
cb2ad0
+			free(domain_desc);
cb2ad0
+		virDomainFree(vdp);
cb2ad0
+		return 1;
cb2ad0
+	}
cb2ad0
+
cb2ad0
+	if (!domain_desc)
cb2ad0
+		return 0;
cb2ad0
+
cb2ad0
+	/* 'on' is not a failure */
cb2ad0
+	ret = 0;
cb2ad0
+
cb2ad0
+	dbg_printf(3, "[[ XML Domain Info ]]\n");
cb2ad0
+	dbg_printf(3, "%s\n[[ XML END ]]\n", domain_desc);
cb2ad0
+
cb2ad0
+	dbg_printf(2, "[virt:REBOOT] Calling virDomainCreateLinux() for %s\n",
cb2ad0
+		vm_name);
cb2ad0
+
cb2ad0
+	nvdp = virDomainCreateLinux(vcp, domain_desc, 0);
cb2ad0
+	if (nvdp == NULL) {
cb2ad0
+		/* More recent versions of libvirt or perhaps the
cb2ad0
+		 * KVM back-end do not let you create a domain from
cb2ad0
+		 * XML if there is already a defined domain description
cb2ad0
+		 * with the same name that it knows about.  You must
cb2ad0
+		 * then call virDomainCreate() */
cb2ad0
+		dbg_printf(2,
cb2ad0
+			"[virt:REBOOT] virDomainCreateLinux() failed for %s; "
cb2ad0
+			"Trying virDomainCreate()\n",
cb2ad0
+			vm_name);
cb2ad0
+
cb2ad0
+		if (virDomainCreate(vdp) < 0) {
cb2ad0
+			syslog(LOG_NOTICE, "Could not restart %s\n", vm_name);
cb2ad0
+			dbg_printf(1, "[virt:REBOOT] Failed to recreate guest %s!\n",
cb2ad0
+				vm_name);
cb2ad0
+		}
cb2ad0
+	}
cb2ad0
+
cb2ad0
+	free(domain_desc);
cb2ad0
+	virDomainFree(vdp);
cb2ad0
+	return ret;
cb2ad0
+}
cb2ad0
diff -ur a/server/virt.h b/server/virt.h
cb2ad0
--- a/server/virt.h	2014-06-23 15:56:09.000000000 -0400
cb2ad0
+++ b/server/virt.h	2017-08-09 13:30:40.709689288 -0400
cb2ad0
@@ -1,5 +1,5 @@
cb2ad0
 /*
cb2ad0
-  Copyright Red Hat, Inc. 2006
cb2ad0
+  Copyright Red Hat, Inc. 2006-2017
cb2ad0
 
cb2ad0
   This program is free software; you can redistribute it and/or modify it
cb2ad0
   under the terms of the GNU General Public License as published by the
cb2ad0
@@ -16,11 +16,13 @@
cb2ad0
   Free Software Foundation, Inc.,  675 Mass Ave, Cambridge, 
cb2ad0
   MA 02139, USA.
cb2ad0
 */
cb2ad0
+
cb2ad0
 #ifndef _VIRT_H
cb2ad0
 #define _VIRT_H
cb2ad0
-#include <libvirt/libvirt.h>
cb2ad0
+
cb2ad0
 #include <stdint.h>
cb2ad0
 #include <netinet/in.h>
cb2ad0
+#include <libvirt/libvirt.h>
cb2ad0
 
cb2ad0
 #include "xvm.h"
cb2ad0
 
cb2ad0
@@ -59,15 +61,16 @@
cb2ad0
 	virt_state_t	vm_states[0];
cb2ad0
 } virt_list_t;
cb2ad0
 
cb2ad0
-virt_list_t *vl_get(virConnectPtr vp, int my_id);
cb2ad0
-
cb2ad0
-int vl_cmp(virt_list_t *left, virt_list_t *right);
cb2ad0
-
cb2ad0
+virt_list_t *vl_get(virConnectPtr *vp, int vp_count, int my_id);
cb2ad0
 void vl_print(virt_list_t *vl);
cb2ad0
 void vl_free(virt_list_t *old);
cb2ad0
-virt_state_t * vl_find_uuid(virt_list_t *vl, const char *name);
cb2ad0
-virt_state_t * vl_find_name(virt_list_t *vl, const char *name);
cb2ad0
+virt_state_t *vl_find_uuid(virt_list_t *vl, const char *name);
cb2ad0
+virt_state_t *vl_find_name(virt_list_t *vl, const char *name);
cb2ad0
 
cb2ad0
+int vm_off(virConnectPtr *vp, int vp_count, const char *vm_name);
cb2ad0
+int vm_on(virConnectPtr *vp, int vp_count, const char *vm_name);
cb2ad0
+int vm_status(virConnectPtr *vp, int vp_count, const char *vm_name);
cb2ad0
+int vm_reboot(virConnectPtr *vp, int vp_count, const char *vm_name);
cb2ad0
 
cb2ad0
 typedef void ckpt_handle;
cb2ad0
 int ckpt_read(void *hp, const char *secid, void *buf, size_t maxlen);