Factor out common libvirt code so that it can be reused by multiple backends
This commit is contained in:
parent
4d29ef93ea
commit
60800fe973
@ -80,7 +80,7 @@ virt_list_update(virConnectPtr vp, virt_list_t **vl, int my_id)
|
||||
virt_list_t *list = NULL;
|
||||
if (*vl)
|
||||
vl_free(*vl);
|
||||
list = vl_get(vp, my_id);
|
||||
list = vl_get(vp, 1, my_id);
|
||||
*vl = list;
|
||||
|
||||
if (!list)
|
||||
|
337
server/libvirt.c
337
server/libvirt.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
Copyright Red Hat, Inc. 2006-2014
|
||||
Copyright Red Hat, Inc. 2006-2017
|
||||
|
||||
This program is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU General Public License as published by the
|
||||
@ -80,67 +80,6 @@ do {\
|
||||
} while(0)
|
||||
|
||||
|
||||
static inline int
|
||||
wait_domain(const char *vm_name, virConnectPtr vp, int timeout)
|
||||
{
|
||||
int tries = 0;
|
||||
int response = 1;
|
||||
int ret;
|
||||
virDomainPtr vdp;
|
||||
virDomainInfo vdi;
|
||||
int uuid_check;
|
||||
|
||||
uuid_check = is_uuid(vm_name);
|
||||
|
||||
if (uuid_check) {
|
||||
vdp = virDomainLookupByUUIDString(vp, (const char *)vm_name);
|
||||
} else {
|
||||
vdp = virDomainLookupByName(vp, vm_name);
|
||||
}
|
||||
if (!vdp)
|
||||
return 0;
|
||||
|
||||
/* Check domain liveliness. If the domain is still here,
|
||||
we return failure, and the client must then retry */
|
||||
/* XXX On the xen 3.0.4 API, we will be able to guarantee
|
||||
synchronous virDomainDestroy, so this check will not
|
||||
be necessary */
|
||||
do {
|
||||
if (++tries > timeout)
|
||||
break;
|
||||
|
||||
sleep(1);
|
||||
if (uuid_check) {
|
||||
vdp = virDomainLookupByUUIDString(vp, (const char *)vm_name);
|
||||
} else {
|
||||
vdp = virDomainLookupByName(vp, vm_name);
|
||||
}
|
||||
if (!vdp) {
|
||||
dbg_printf(2, "Domain no longer exists\n");
|
||||
response = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
memset(&vdi, 0, sizeof(vdi));
|
||||
ret = virDomainGetInfo(vdp, &vdi);
|
||||
virDomainFree(vdp);
|
||||
if (ret < 0)
|
||||
continue;
|
||||
|
||||
if (vdi.state == VIR_DOMAIN_SHUTOFF) {
|
||||
dbg_printf(2, "Domain has been shut off\n");
|
||||
response = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
dbg_printf(4, "Domain still exists (state %d) after %d seconds\n",
|
||||
vdi.state, tries);
|
||||
} while (1);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
libvirt_null(const char *vm_name, void *priv)
|
||||
{
|
||||
@ -154,61 +93,11 @@ static int
|
||||
libvirt_off(const char *vm_name, const char *src, uint32_t seqno, void *priv)
|
||||
{
|
||||
struct libvirt_info *info = (struct libvirt_info *)priv;
|
||||
virDomainPtr vdp = NULL;
|
||||
virDomainInfo vdi;
|
||||
virDomainPtr (*virt_lookup_fn)(virConnectPtr, const char *);
|
||||
int ret = -1;
|
||||
int i;
|
||||
|
||||
dbg_printf(5, "ENTER %s %s %u\n", __FUNCTION__, vm_name, seqno);
|
||||
VALIDATE(info);
|
||||
|
||||
if (is_uuid(vm_name))
|
||||
virt_lookup_fn = virDomainLookupByUUIDString;
|
||||
else
|
||||
virt_lookup_fn = virDomainLookupByName;
|
||||
|
||||
for (i = 0 ; i < info->vp_count ; i++) {
|
||||
vdp = virt_lookup_fn(info->vp[i], vm_name);
|
||||
if (vdp)
|
||||
break;
|
||||
}
|
||||
|
||||
if (!vdp) {
|
||||
dbg_printf(2, "[libvirt:OFF] Domain %s does not exist\n", vm_name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (virDomainGetInfo(vdp, &vdi) == 0 && vdi.state == VIR_DOMAIN_SHUTOFF) {
|
||||
dbg_printf(2, "[libvirt:OFF] Nothing to do - "
|
||||
"domain %s is already off\n",
|
||||
vm_name);
|
||||
virDomainFree(vdp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
syslog(LOG_NOTICE, "Destroying domain %s\n", vm_name);
|
||||
dbg_printf(2, "[libvirt:OFF] Calling virDomainDestroy for %s\n", vm_name);
|
||||
|
||||
ret = virDomainDestroy(vdp);
|
||||
virDomainFree(vdp);
|
||||
|
||||
if (ret < 0) {
|
||||
syslog(LOG_NOTICE, "Failed to destroy domain %s: %d\n", vm_name, ret);
|
||||
dbg_printf(2, "[libvirt:OFF] Failed to destroy domain: %s %d\n",
|
||||
vm_name, ret);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ret) {
|
||||
syslog(LOG_NOTICE, "Domain %s still exists; fencing failed\n", vm_name);
|
||||
dbg_printf(2, "[libvirt:OFF] Domain %s still exists; fencing failed\n",
|
||||
vm_name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
dbg_printf(2, "[libvirt:OFF] Success for %s\n", vm_name);
|
||||
return 0;
|
||||
return vm_off(info->vp, info->vp_count, vm_name);
|
||||
}
|
||||
|
||||
|
||||
@ -216,60 +105,11 @@ static int
|
||||
libvirt_on(const char *vm_name, const char *src, uint32_t seqno, void *priv)
|
||||
{
|
||||
struct libvirt_info *info = (struct libvirt_info *)priv;
|
||||
virDomainPtr vdp = NULL;
|
||||
virDomainInfo vdi;
|
||||
virDomainPtr (*virt_lookup_fn)(virConnectPtr, const char *);
|
||||
int ret = -1;
|
||||
int i;
|
||||
|
||||
dbg_printf(5, "ENTER %s %s %u\n", __FUNCTION__, vm_name, seqno);
|
||||
VALIDATE(info);
|
||||
|
||||
if (is_uuid(vm_name))
|
||||
virt_lookup_fn = virDomainLookupByUUIDString;
|
||||
else
|
||||
virt_lookup_fn = virDomainLookupByName;
|
||||
|
||||
for (i = 0 ; i < info->vp_count ; i++) {
|
||||
vdp = virt_lookup_fn(info->vp[i], vm_name);
|
||||
if (vdp)
|
||||
break;
|
||||
}
|
||||
|
||||
if (!vdp) {
|
||||
dbg_printf(2, "[libvirt:ON] Domain %s does not exist\n", vm_name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (virDomainGetInfo(vdp, &vdi) == 0 && vdi.state != VIR_DOMAIN_SHUTOFF) {
|
||||
dbg_printf(2, "Nothing to do - domain %s is already running\n",
|
||||
vm_name);
|
||||
virDomainFree(vdp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
syslog(LOG_NOTICE, "Starting domain %s\n", vm_name);
|
||||
dbg_printf(2, "[libvirt:ON] Calling virDomainCreate for %s\n", vm_name);
|
||||
|
||||
ret = virDomainCreate(vdp);
|
||||
virDomainFree(vdp);
|
||||
|
||||
if (ret < 0) {
|
||||
syslog(LOG_NOTICE, "Failed to start domain %s: %d\n", vm_name, ret);
|
||||
dbg_printf(2, "[libvirt:ON] virDomainCreate() failed for %s: %d\n",
|
||||
vm_name, ret);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ret) {
|
||||
syslog(LOG_NOTICE, "Domain %s did not start\n", vm_name);
|
||||
dbg_printf(2, "[libvirt:ON] Domain %s did not start\n", vm_name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
syslog(LOG_NOTICE, "Domain %s started\n", vm_name);
|
||||
dbg_printf(2, "[libvirt:ON] Success for %s\n", vm_name);
|
||||
return 0;
|
||||
return vm_on(info->vp, info->vp_count, vm_name);
|
||||
}
|
||||
|
||||
|
||||
@ -288,39 +128,11 @@ static int
|
||||
libvirt_status(const char *vm_name, void *priv)
|
||||
{
|
||||
struct libvirt_info *info = (struct libvirt_info *)priv;
|
||||
virDomainPtr vdp = NULL;
|
||||
virDomainInfo vdi;
|
||||
int ret = 0;
|
||||
int i;
|
||||
virDomainPtr (*virt_lookup_fn)(virConnectPtr, const char *);
|
||||
|
||||
dbg_printf(5, "ENTER %s %s\n", __FUNCTION__, vm_name);
|
||||
VALIDATE(info);
|
||||
|
||||
if (is_uuid(vm_name))
|
||||
virt_lookup_fn = virDomainLookupByUUIDString;
|
||||
else
|
||||
virt_lookup_fn = virDomainLookupByName;
|
||||
|
||||
for (i = 0 ; i < info->vp_count ; i++) {
|
||||
vdp = virt_lookup_fn(info->vp[i], vm_name);
|
||||
if (vdp)
|
||||
break;
|
||||
}
|
||||
|
||||
if (!vdp) {
|
||||
dbg_printf(2, "[libvirt:STATUS] Unknown VM %s - return OFF\n", vm_name);
|
||||
return RESP_OFF;
|
||||
}
|
||||
|
||||
if (virDomainGetInfo(vdp, &vdi) == 0 && vdi.state == VIR_DOMAIN_SHUTOFF) {
|
||||
dbg_printf(2, "[libvirt:STATUS] VM %s is OFF\n", vm_name);
|
||||
ret = RESP_OFF;
|
||||
}
|
||||
|
||||
if (vdp)
|
||||
virDomainFree(vdp);
|
||||
return ret;
|
||||
return vm_status(info->vp, info->vp_count, vm_name);
|
||||
}
|
||||
|
||||
|
||||
@ -328,117 +140,11 @@ static int
|
||||
libvirt_reboot(const char *vm_name, const char *src, uint32_t seqno, void *priv)
|
||||
{
|
||||
struct libvirt_info *info = (struct libvirt_info *)priv;
|
||||
virDomainPtr vdp = NULL, nvdp;
|
||||
virDomainInfo vdi;
|
||||
char *domain_desc;
|
||||
virConnectPtr vcp = NULL;
|
||||
virDomainPtr (*virt_lookup_fn)(virConnectPtr, const char *);
|
||||
int ret;
|
||||
int i;
|
||||
|
||||
dbg_printf(5, "ENTER %s %s %u\n", __FUNCTION__, vm_name, seqno);
|
||||
VALIDATE(info);
|
||||
|
||||
if (is_uuid(vm_name))
|
||||
virt_lookup_fn = virDomainLookupByUUIDString;
|
||||
else
|
||||
virt_lookup_fn = virDomainLookupByName;
|
||||
|
||||
for (i = 0 ; i < info->vp_count ; i++) {
|
||||
vdp = virt_lookup_fn(info->vp[i], vm_name);
|
||||
if (vdp) {
|
||||
vcp = info->vp[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!vdp || !vcp) {
|
||||
dbg_printf(2,
|
||||
"[libvirt:REBOOT] Nothing to do - domain %s does not exist\n",
|
||||
vm_name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (virDomainGetInfo(vdp, &vdi) == 0 && vdi.state == VIR_DOMAIN_SHUTOFF) {
|
||||
dbg_printf(2, "[libvirt:REBOOT] Nothing to do - domain %s is off\n",
|
||||
vm_name);
|
||||
virDomainFree(vdp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
syslog(LOG_NOTICE, "Rebooting domain %s\n", vm_name);
|
||||
dbg_printf(5, "[libvirt:REBOOT] Rebooting domain %s...\n", vm_name);
|
||||
|
||||
domain_desc = virDomainGetXMLDesc(vdp, 0);
|
||||
|
||||
if (!domain_desc) {
|
||||
dbg_printf(5, "[libvirt:REBOOT] Failed getting domain description "
|
||||
"from libvirt for %s...\n", vm_name);
|
||||
}
|
||||
|
||||
dbg_printf(2, "[libvirt:REBOOT] Calling virDomainDestroy(%p) for %s\n",
|
||||
vdp, vm_name);
|
||||
|
||||
ret = virDomainDestroy(vdp);
|
||||
if (ret < 0) {
|
||||
dbg_printf(2,
|
||||
"[libvirt:REBOOT] virDomainDestroy() failed for %s: %d/%d\n",
|
||||
vm_name, ret, errno);
|
||||
|
||||
if (domain_desc)
|
||||
free(domain_desc);
|
||||
virDomainFree(vdp);
|
||||
return 1;
|
||||
}
|
||||
|
||||
ret = wait_domain(vm_name, vcp, 15);
|
||||
|
||||
if (ret) {
|
||||
syslog(LOG_NOTICE, "Domain %s still exists; fencing failed\n", vm_name);
|
||||
dbg_printf(2,
|
||||
"[libvirt:REBOOT] Domain %s still exists; fencing failed\n",
|
||||
vm_name);
|
||||
|
||||
if (domain_desc)
|
||||
free(domain_desc);
|
||||
virDomainFree(vdp);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!domain_desc)
|
||||
return 0;
|
||||
|
||||
/* 'on' is not a failure */
|
||||
ret = 0;
|
||||
|
||||
dbg_printf(3, "[[ XML Domain Info ]]\n");
|
||||
dbg_printf(3, "%s\n[[ XML END ]]\n", domain_desc);
|
||||
|
||||
dbg_printf(2, "[libvirt:REBOOT] Calling virDomainCreateLinux() for %s\n",
|
||||
vm_name);
|
||||
|
||||
nvdp = virDomainCreateLinux(vcp, domain_desc, 0);
|
||||
if (nvdp == NULL) {
|
||||
/* More recent versions of libvirt or perhaps the
|
||||
* KVM back-end do not let you create a domain from
|
||||
* XML if there is already a defined domain description
|
||||
* with the same name that it knows about. You must
|
||||
* then call virDomainCreate() */
|
||||
dbg_printf(2,
|
||||
"[libvirt:REBOOT] virDomainCreateLinux() failed for %s; "
|
||||
"Trying virDomainCreate()\n",
|
||||
vm_name);
|
||||
|
||||
if (virDomainCreate(vdp) < 0) {
|
||||
syslog(LOG_NOTICE, "Could not restart %s\n", vm_name);
|
||||
dbg_printf(1, "[libvirt:REBOOT] Failed to recreate guest %s!\n",
|
||||
vm_name);
|
||||
}
|
||||
}
|
||||
|
||||
free(domain_desc);
|
||||
virDomainFree(vdp);
|
||||
return ret;
|
||||
return vm_reboot(info->vp, info->vp_count, vm_name);
|
||||
}
|
||||
|
||||
|
||||
@ -446,33 +152,28 @@ static int
|
||||
libvirt_hostlist(hostlist_callback callback, void *arg, void *priv)
|
||||
{
|
||||
struct libvirt_info *info = (struct libvirt_info *)priv;
|
||||
int i;
|
||||
virt_list_t *vl;
|
||||
int x;
|
||||
|
||||
dbg_printf(5, "ENTER %s\n", __FUNCTION__);
|
||||
VALIDATE(info);
|
||||
|
||||
for (i = 0 ; i < info->vp_count ; i++) {
|
||||
int x;
|
||||
virt_list_t *vl;
|
||||
vl = vl_get(info->vp, info->vp_count, 1);
|
||||
if (!vl)
|
||||
return 0;
|
||||
|
||||
vl = vl_get(info->vp[i], 1);
|
||||
if (!vl)
|
||||
continue;
|
||||
for (x = 0; x < vl->vm_count; x++) {
|
||||
callback(vl->vm_states[x].v_name,
|
||||
vl->vm_states[x].v_uuid,
|
||||
vl->vm_states[x].v_state.s_state, arg);
|
||||
|
||||
for (x = 0; x < vl->vm_count; x++) {
|
||||
callback(vl->vm_states[x].v_name,
|
||||
vl->vm_states[x].v_uuid,
|
||||
vl->vm_states[x].v_state.s_state, arg);
|
||||
|
||||
dbg_printf(10, "[libvirt:HOSTLIST] Sent %s %s %d\n",
|
||||
vl->vm_states[x].v_name,
|
||||
vl->vm_states[x].v_uuid,
|
||||
vl->vm_states[x].v_state.s_state);
|
||||
}
|
||||
|
||||
vl_free(vl);
|
||||
dbg_printf(10, "[libvirt:HOSTLIST] Sent %s %s %d\n",
|
||||
vl->vm_states[x].v_name,
|
||||
vl->vm_states[x].v_uuid,
|
||||
vl->vm_states[x].v_state.s_state);
|
||||
}
|
||||
|
||||
vl_free(vl);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
468
server/virt.c
468
server/virt.c
@ -1,5 +1,5 @@
|
||||
/*
|
||||
Copyright Red Hat, Inc. 2006
|
||||
Copyright Red Hat, Inc. 2006-2017
|
||||
|
||||
This program is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU General Public License as published by the
|
||||
@ -16,7 +16,9 @@
|
||||
Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
|
||||
MA 02139, USA.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <stdlib.h>
|
||||
#include <libvirt/libvirt.h>
|
||||
@ -24,8 +26,11 @@
|
||||
#include <malloc.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
#include "virt.h"
|
||||
#include <syslog.h>
|
||||
|
||||
#include "debug.h"
|
||||
#include "uuid-test.h"
|
||||
#include "virt.h"
|
||||
|
||||
static int
|
||||
_compare_virt(const void *_left, const void *_right)
|
||||
@ -37,96 +42,85 @@ _compare_virt(const void *_left, const void *_right)
|
||||
}
|
||||
|
||||
|
||||
virt_list_t *vl_get(virConnectPtr vp, int my_id)
|
||||
static void
|
||||
_free_dom_list(virDomainPtr *dom_list, int len) {
|
||||
int x;
|
||||
|
||||
if (!dom_list || len <= 0)
|
||||
return;
|
||||
for (x = 0 ; x < len; x++)
|
||||
virDomainFree(dom_list[x]);
|
||||
|
||||
free(dom_list);
|
||||
}
|
||||
|
||||
|
||||
virt_list_t *vl_get(virConnectPtr *vp, int vp_count, int my_id)
|
||||
{
|
||||
virt_list_t *vl = NULL;
|
||||
int d_count, x, saved_errno;
|
||||
virDomainPtr *dom_list;
|
||||
int d_count = 0;
|
||||
int i;
|
||||
|
||||
errno = EINVAL;
|
||||
if (!vp)
|
||||
if (!vp || vp_count < 1)
|
||||
return NULL;
|
||||
|
||||
d_count = virConnectListAllDomains(vp, &dom_list, 0);
|
||||
if (d_count <= 0)
|
||||
goto out_fail;
|
||||
for (i = 0 ; i < vp_count ; i++) {
|
||||
int x;
|
||||
virDomainPtr *dom_list;
|
||||
|
||||
vl = malloc(sizeof(uint32_t) + sizeof(virt_state_t) * d_count);
|
||||
if (!vl)
|
||||
goto out_fail;
|
||||
int ret = virConnectListAllDomains(vp[i], &dom_list, 0);
|
||||
if (ret <= 0)
|
||||
continue;
|
||||
|
||||
vl->vm_count = d_count;
|
||||
d_count += ret;
|
||||
vl = realloc(vl, sizeof(uint32_t) + sizeof(virt_state_t) * d_count);
|
||||
if (!vl) {
|
||||
_free_dom_list(dom_list, ret);
|
||||
return NULL;
|
||||
}
|
||||
vl->vm_count = d_count;
|
||||
|
||||
/* Ok, we have the domain IDs - let's get their names and states */
|
||||
for (x = 0; x < d_count; x++) {
|
||||
char *d_name;
|
||||
virDomainInfo d_info;
|
||||
char d_uuid[MAX_DOMAINNAME_LENGTH];
|
||||
virDomainPtr dom = dom_list[x];
|
||||
/* Ok, we have the domain IDs - let's get their names and states */
|
||||
for (x = 0; x < ret; x++) {
|
||||
char *d_name;
|
||||
virDomainInfo d_info;
|
||||
char d_uuid[MAX_DOMAINNAME_LENGTH];
|
||||
virDomainPtr dom = dom_list[x];
|
||||
|
||||
if (!(d_name = (char *)virDomainGetName(dom)))
|
||||
goto out_fail;
|
||||
if (!(d_name = (char *)virDomainGetName(dom))) {
|
||||
_free_dom_list(dom_list, ret);
|
||||
free(vl);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (virDomainGetUUIDString(dom, d_uuid) != 0)
|
||||
goto out_fail;
|
||||
if (virDomainGetUUIDString(dom, d_uuid) != 0) {
|
||||
_free_dom_list(dom_list, ret);
|
||||
free(vl);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (virDomainGetInfo(dom, &d_info) < 0)
|
||||
goto out_fail;
|
||||
if (virDomainGetInfo(dom, &d_info) < 0) {
|
||||
_free_dom_list(dom_list, ret);
|
||||
free(vl);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Store the name & state */
|
||||
strncpy(vl->vm_states[x].v_name, d_name, MAX_DOMAINNAME_LENGTH);
|
||||
strncpy(vl->vm_states[x].v_uuid, d_uuid, MAX_DOMAINNAME_LENGTH);
|
||||
vl->vm_states[x].v_state.s_state = d_info.state;
|
||||
vl->vm_states[x].v_state.s_owner = my_id;
|
||||
/* Store the name & state */
|
||||
strncpy(vl->vm_states[x].v_name, d_name, MAX_DOMAINNAME_LENGTH);
|
||||
strncpy(vl->vm_states[x].v_uuid, d_uuid, MAX_DOMAINNAME_LENGTH);
|
||||
vl->vm_states[x].v_state.s_state = d_info.state;
|
||||
vl->vm_states[x].v_state.s_owner = my_id;
|
||||
}
|
||||
|
||||
_free_dom_list(dom_list, ret);
|
||||
}
|
||||
|
||||
for (x = 0 ; x < d_count; x++)
|
||||
virDomainFree(dom_list[x]);
|
||||
free(dom_list);
|
||||
|
||||
/* We have all the locally running domains & states now */
|
||||
/* Sort */
|
||||
qsort(&vl->vm_states[0], vl->vm_count, sizeof(vl->vm_states[0]),
|
||||
_compare_virt);
|
||||
return vl;
|
||||
|
||||
out_fail:
|
||||
saved_errno = errno;
|
||||
for (x = 0 ; x < d_count; x++)
|
||||
virDomainFree(dom_list[x]);
|
||||
free(dom_list);
|
||||
|
||||
if (vl)
|
||||
free(vl);
|
||||
errno = saved_errno;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
/* Returns 0 if equal, nonzero if not */
|
||||
int
|
||||
vl_cmp(virt_list_t *left, virt_list_t *right)
|
||||
{
|
||||
int x;
|
||||
|
||||
/* Quick checks */
|
||||
if (!left->vm_count && !right->vm_count)
|
||||
return 1;
|
||||
if (left->vm_count != right->vm_count)
|
||||
return 0;
|
||||
|
||||
for (x = 0; x < left->vm_count; x++) {
|
||||
if (strcmp(left->vm_states[x].v_name,
|
||||
right->vm_states[x].v_name))
|
||||
return 1;
|
||||
/*
|
||||
if (left->vm_states[x].v_state.s_state !=
|
||||
right->vm_states[x].v_state.s_state)
|
||||
return 1;
|
||||
*/
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@ -192,3 +186,333 @@ vl_free(virt_list_t *old)
|
||||
{
|
||||
free(old);
|
||||
}
|
||||
|
||||
|
||||
static inline int
|
||||
wait_domain(const char *vm_name, virConnectPtr vp, int timeout)
|
||||
{
|
||||
int tries = 0;
|
||||
int response = 1;
|
||||
int ret;
|
||||
virDomainPtr vdp;
|
||||
virDomainInfo vdi;
|
||||
int uuid_check;
|
||||
|
||||
uuid_check = is_uuid(vm_name);
|
||||
|
||||
if (uuid_check) {
|
||||
vdp = virDomainLookupByUUIDString(vp, (const char *)vm_name);
|
||||
} else {
|
||||
vdp = virDomainLookupByName(vp, vm_name);
|
||||
}
|
||||
if (!vdp)
|
||||
return 0;
|
||||
|
||||
/* Check domain liveliness. If the domain is still here,
|
||||
we return failure, and the client must then retry */
|
||||
/* XXX On the xen 3.0.4 API, we will be able to guarantee
|
||||
synchronous virDomainDestroy, so this check will not
|
||||
be necessary */
|
||||
do {
|
||||
if (++tries > timeout)
|
||||
break;
|
||||
|
||||
sleep(1);
|
||||
if (uuid_check) {
|
||||
vdp = virDomainLookupByUUIDString(vp, (const char *)vm_name);
|
||||
} else {
|
||||
vdp = virDomainLookupByName(vp, vm_name);
|
||||
}
|
||||
if (!vdp) {
|
||||
dbg_printf(2, "Domain no longer exists\n");
|
||||
response = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
memset(&vdi, 0, sizeof(vdi));
|
||||
ret = virDomainGetInfo(vdp, &vdi);
|
||||
virDomainFree(vdp);
|
||||
if (ret < 0)
|
||||
continue;
|
||||
|
||||
if (vdi.state == VIR_DOMAIN_SHUTOFF) {
|
||||
dbg_printf(2, "Domain has been shut off\n");
|
||||
response = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
dbg_printf(4, "Domain still exists (state %d) after %d seconds\n",
|
||||
vdi.state, tries);
|
||||
} while (1);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
vm_off(virConnectPtr *vp, int vp_count, const char *vm_name)
|
||||
{
|
||||
virDomainPtr vdp = NULL;
|
||||
virDomainInfo vdi;
|
||||
virDomainPtr (*virt_lookup_fn)(virConnectPtr, const char *);
|
||||
int ret = -1;
|
||||
int i;
|
||||
|
||||
if (is_uuid(vm_name))
|
||||
virt_lookup_fn = virDomainLookupByUUIDString;
|
||||
else
|
||||
virt_lookup_fn = virDomainLookupByName;
|
||||
|
||||
for (i = 0 ; i < vp_count ; i++) {
|
||||
vdp = virt_lookup_fn(vp[i], vm_name);
|
||||
if (vdp)
|
||||
break;
|
||||
}
|
||||
|
||||
if (!vdp) {
|
||||
dbg_printf(2, "[virt:OFF] Domain %s does not exist\n", vm_name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (virDomainGetInfo(vdp, &vdi) == 0 && vdi.state == VIR_DOMAIN_SHUTOFF)
|
||||
{
|
||||
dbg_printf(2, "[virt:OFF] Nothing to do - "
|
||||
"domain %s is already off\n",
|
||||
vm_name);
|
||||
virDomainFree(vdp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
syslog(LOG_NOTICE, "Destroying domain %s\n", vm_name);
|
||||
dbg_printf(2, "[virt:OFF] Calling virDomainDestroy for %s\n", vm_name);
|
||||
|
||||
ret = virDomainDestroy(vdp);
|
||||
virDomainFree(vdp);
|
||||
|
||||
if (ret < 0) {
|
||||
syslog(LOG_NOTICE,
|
||||
"Failed to destroy domain %s: %d\n", vm_name, ret);
|
||||
dbg_printf(2, "[virt:OFF] Failed to destroy domain: %s %d\n",
|
||||
vm_name, ret);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ret) {
|
||||
syslog(LOG_NOTICE, "Domain %s still exists; fencing failed\n",
|
||||
vm_name);
|
||||
dbg_printf(2,
|
||||
"[virt:OFF] Domain %s still exists; fencing failed\n",
|
||||
vm_name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
dbg_printf(2, "[virt:OFF] Success for %s\n", vm_name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
vm_on(virConnectPtr *vp, int vp_count, const char *vm_name)
|
||||
{
|
||||
virDomainPtr vdp = NULL;
|
||||
virDomainInfo vdi;
|
||||
virDomainPtr (*virt_lookup_fn)(virConnectPtr, const char *);
|
||||
int ret = -1;
|
||||
int i;
|
||||
|
||||
if (is_uuid(vm_name))
|
||||
virt_lookup_fn = virDomainLookupByUUIDString;
|
||||
else
|
||||
virt_lookup_fn = virDomainLookupByName;
|
||||
|
||||
for (i = 0 ; i < vp_count ; i++) {
|
||||
vdp = virt_lookup_fn(vp[i], vm_name);
|
||||
if (vdp)
|
||||
break;
|
||||
}
|
||||
|
||||
if (!vdp) {
|
||||
dbg_printf(2, "[virt:ON] Domain %s does not exist\n", vm_name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (virDomainGetInfo(vdp, &vdi) == 0 && vdi.state != VIR_DOMAIN_SHUTOFF) {
|
||||
dbg_printf(2, "Nothing to do - domain %s is already running\n",
|
||||
vm_name);
|
||||
virDomainFree(vdp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
syslog(LOG_NOTICE, "Starting domain %s\n", vm_name);
|
||||
dbg_printf(2, "[virt:ON] Calling virDomainCreate for %s\n", vm_name);
|
||||
|
||||
ret = virDomainCreate(vdp);
|
||||
virDomainFree(vdp);
|
||||
|
||||
if (ret < 0) {
|
||||
syslog(LOG_NOTICE, "Failed to start domain %s: %d\n", vm_name, ret);
|
||||
dbg_printf(2, "[virt:ON] virDomainCreate() failed for %s: %d\n",
|
||||
vm_name, ret);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ret) {
|
||||
syslog(LOG_NOTICE, "Domain %s did not start\n", vm_name);
|
||||
dbg_printf(2, "[virt:ON] Domain %s did not start\n", vm_name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
syslog(LOG_NOTICE, "Domain %s started\n", vm_name);
|
||||
dbg_printf(2, "[virt:ON] Success for %s\n", vm_name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
vm_status(virConnectPtr *vp, int vp_count, const char *vm_name)
|
||||
{
|
||||
virDomainPtr vdp = NULL;
|
||||
virDomainInfo vdi;
|
||||
int ret = 0;
|
||||
int i;
|
||||
virDomainPtr (*virt_lookup_fn)(virConnectPtr, const char *);
|
||||
|
||||
if (is_uuid(vm_name))
|
||||
virt_lookup_fn = virDomainLookupByUUIDString;
|
||||
else
|
||||
virt_lookup_fn = virDomainLookupByName;
|
||||
|
||||
for (i = 0 ; i < vp_count ; i++) {
|
||||
vdp = virt_lookup_fn(vp[i], vm_name);
|
||||
if (vdp)
|
||||
break;
|
||||
}
|
||||
|
||||
if (!vdp) {
|
||||
dbg_printf(2, "[virt:STATUS] Unknown VM %s - return OFF\n", vm_name);
|
||||
return RESP_OFF;
|
||||
}
|
||||
|
||||
if (virDomainGetInfo(vdp, &vdi) == 0 && vdi.state == VIR_DOMAIN_SHUTOFF) {
|
||||
dbg_printf(2, "[virt:STATUS] VM %s is OFF\n", vm_name);
|
||||
ret = RESP_OFF;
|
||||
}
|
||||
|
||||
if (vdp)
|
||||
virDomainFree(vdp);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
vm_reboot(virConnectPtr *vp, int vp_count, const char *vm_name)
|
||||
{
|
||||
virDomainPtr vdp = NULL, nvdp;
|
||||
virDomainInfo vdi;
|
||||
char *domain_desc;
|
||||
virConnectPtr vcp = NULL;
|
||||
virDomainPtr (*virt_lookup_fn)(virConnectPtr, const char *);
|
||||
int ret;
|
||||
int i;
|
||||
|
||||
if (is_uuid(vm_name))
|
||||
virt_lookup_fn = virDomainLookupByUUIDString;
|
||||
else
|
||||
virt_lookup_fn = virDomainLookupByName;
|
||||
|
||||
for (i = 0 ; i < vp_count ; i++) {
|
||||
vdp = virt_lookup_fn(vp[i], vm_name);
|
||||
if (vdp) {
|
||||
vcp = vp[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!vdp || !vcp) {
|
||||
dbg_printf(2,
|
||||
"[virt:REBOOT] Nothing to do - domain %s does not exist\n",
|
||||
vm_name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (virDomainGetInfo(vdp, &vdi) == 0 && vdi.state == VIR_DOMAIN_SHUTOFF) {
|
||||
dbg_printf(2, "[virt:REBOOT] Nothing to do - domain %s is off\n",
|
||||
vm_name);
|
||||
virDomainFree(vdp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
syslog(LOG_NOTICE, "Rebooting domain %s\n", vm_name);
|
||||
dbg_printf(5, "[virt:REBOOT] Rebooting domain %s...\n", vm_name);
|
||||
|
||||
domain_desc = virDomainGetXMLDesc(vdp, 0);
|
||||
|
||||
if (!domain_desc) {
|
||||
dbg_printf(5, "[virt:REBOOT] Failed getting domain description "
|
||||
"from libvirt for %s...\n", vm_name);
|
||||
}
|
||||
|
||||
dbg_printf(2, "[virt:REBOOT] Calling virDomainDestroy(%p) for %s\n",
|
||||
vdp, vm_name);
|
||||
|
||||
ret = virDomainDestroy(vdp);
|
||||
if (ret < 0) {
|
||||
dbg_printf(2,
|
||||
"[virt:REBOOT] virDomainDestroy() failed for %s: %d/%d\n",
|
||||
vm_name, ret, errno);
|
||||
|
||||
if (domain_desc)
|
||||
free(domain_desc);
|
||||
virDomainFree(vdp);
|
||||
return 1;
|
||||
}
|
||||
|
||||
ret = wait_domain(vm_name, vcp, 15);
|
||||
|
||||
if (ret) {
|
||||
syslog(LOG_NOTICE, "Domain %s still exists; fencing failed\n", vm_name);
|
||||
dbg_printf(2,
|
||||
"[virt:REBOOT] Domain %s still exists; fencing failed\n",
|
||||
vm_name);
|
||||
|
||||
if (domain_desc)
|
||||
free(domain_desc);
|
||||
virDomainFree(vdp);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!domain_desc)
|
||||
return 0;
|
||||
|
||||
/* 'on' is not a failure */
|
||||
ret = 0;
|
||||
|
||||
dbg_printf(3, "[[ XML Domain Info ]]\n");
|
||||
dbg_printf(3, "%s\n[[ XML END ]]\n", domain_desc);
|
||||
|
||||
dbg_printf(2, "[virt:REBOOT] Calling virDomainCreateLinux() for %s\n",
|
||||
vm_name);
|
||||
|
||||
nvdp = virDomainCreateLinux(vcp, domain_desc, 0);
|
||||
if (nvdp == NULL) {
|
||||
/* More recent versions of libvirt or perhaps the
|
||||
* KVM back-end do not let you create a domain from
|
||||
* XML if there is already a defined domain description
|
||||
* with the same name that it knows about. You must
|
||||
* then call virDomainCreate() */
|
||||
dbg_printf(2,
|
||||
"[virt:REBOOT] virDomainCreateLinux() failed for %s; "
|
||||
"Trying virDomainCreate()\n",
|
||||
vm_name);
|
||||
|
||||
if (virDomainCreate(vdp) < 0) {
|
||||
syslog(LOG_NOTICE, "Could not restart %s\n", vm_name);
|
||||
dbg_printf(1, "[virt:REBOOT] Failed to recreate guest %s!\n",
|
||||
vm_name);
|
||||
}
|
||||
}
|
||||
|
||||
free(domain_desc);
|
||||
virDomainFree(vdp);
|
||||
return ret;
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
Copyright Red Hat, Inc. 2006
|
||||
Copyright Red Hat, Inc. 2006-2017
|
||||
|
||||
This program is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU General Public License as published by the
|
||||
@ -16,11 +16,13 @@
|
||||
Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
|
||||
MA 02139, USA.
|
||||
*/
|
||||
|
||||
#ifndef _VIRT_H
|
||||
#define _VIRT_H
|
||||
#include <libvirt/libvirt.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <netinet/in.h>
|
||||
#include <libvirt/libvirt.h>
|
||||
|
||||
#include "xvm.h"
|
||||
|
||||
@ -59,15 +61,16 @@ typedef struct _virt_list {
|
||||
virt_state_t vm_states[0];
|
||||
} virt_list_t;
|
||||
|
||||
virt_list_t *vl_get(virConnectPtr vp, int my_id);
|
||||
|
||||
int vl_cmp(virt_list_t *left, virt_list_t *right);
|
||||
|
||||
virt_list_t *vl_get(virConnectPtr *vp, int vp_count, int my_id);
|
||||
void vl_print(virt_list_t *vl);
|
||||
void vl_free(virt_list_t *old);
|
||||
virt_state_t * vl_find_uuid(virt_list_t *vl, const char *name);
|
||||
virt_state_t * vl_find_name(virt_list_t *vl, const char *name);
|
||||
virt_state_t *vl_find_uuid(virt_list_t *vl, const char *name);
|
||||
virt_state_t *vl_find_name(virt_list_t *vl, const char *name);
|
||||
|
||||
int vm_off(virConnectPtr *vp, int vp_count, const char *vm_name);
|
||||
int vm_on(virConnectPtr *vp, int vp_count, const char *vm_name);
|
||||
int vm_status(virConnectPtr *vp, int vp_count, const char *vm_name);
|
||||
int vm_reboot(virConnectPtr *vp, int vp_count, const char *vm_name);
|
||||
|
||||
typedef void ckpt_handle;
|
||||
int ckpt_read(void *hp, const char *secid, void *buf, size_t maxlen);
|
||||
|
Loading…
Reference in New Issue
Block a user