1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
|
/* virt-v2v
* Copyright (C) 2009-2016 Red Hat Inc.
*
* 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 Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/**
* This module implements various C<virsh>-like commands, but with
* non-broken authentication handling.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <libintl.h>
#include <caml/alloc.h>
#include <caml/fail.h>
#include <caml/memory.h>
#include <caml/mlvalues.h>
#ifdef HAVE_LIBVIRT
#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>
#endif
#include "guestfs.h"
#include "guestfs-internal-frontend.h"
#pragma GCC diagnostic ignored "-Wmissing-prototypes"
#ifdef HAVE_LIBVIRT
#define ERROR_MESSAGE_LEN 512
static void
ignore_errors (void *ignore, virErrorPtr ignore2)
{
/* empty */
}
/* Get the remote domain state (running, etc.). Use virDomainGetState
* which is most efficient, but if it's not implemented, fall back to
* virDomainGetInfo. See equivalent code in virsh.
*/
static int
get_dom_state (virDomainPtr dom)
{
int state, reason;
virErrorPtr err;
virDomainInfo info;
if (virDomainGetState (dom, &state, &reason, 0) == 0)
return state;
err = virGetLastError ();
if (!err || err->code != VIR_ERR_NO_SUPPORT)
return -1;
if (virDomainGetInfo (dom, &info) == 0)
return info.state;
return -1;
}
/* See src/libvirt-auth.c for why we need this. */
static int
libvirt_auth_default_wrapper (virConnectCredentialPtr cred,
unsigned int ncred,
void *passwordvp)
{
const char *password = passwordvp;
unsigned int i;
if (password) {
/* If --password-file was specified on the command line, and the
* libvirt handler is asking for a password, return that.
*/
for (i = 0; i < ncred; ++i) {
if (cred[i].type == VIR_CRED_PASSPHRASE) {
cred[i].result = strdup (password);
cred[i].resultlen = strlen (password);
}
else {
cred[i].result = NULL;
cred[i].resultlen = 0;
}
}
return 0;
}
else {
/* No --password-file so call the default handler. */
return virConnectAuthPtrDefault->cb (cred, ncred,
virConnectAuthPtrDefault->cbdata);
}
}
virStoragePoolPtr
connect_and_load_pool (value connv, value poolnamev)
{
CAMLparam2 (connv, poolnamev);
const char *conn_uri = NULL;
const char *poolname;
/* We have to assemble the error on the stack because a dynamic
* string couldn't be freed.
*/
char errmsg[ERROR_MESSAGE_LEN];
virErrorPtr err;
virConnectPtr conn;
virStoragePoolPtr pool;
if (connv != Val_int (0))
conn_uri = String_val (Field (connv, 0)); /* Some conn */
/* We have to call the default authentication handler, not least
* since it handles all the PolicyKit crap. However it also makes
* coding this simpler.
*/
conn = virConnectOpenAuth (conn_uri, virConnectAuthPtrDefault,
VIR_CONNECT_RO);
if (conn == NULL) {
if (conn_uri)
snprintf (errmsg, sizeof errmsg,
_("cannot open libvirt connection '%s'"), conn_uri);
else
snprintf (errmsg, sizeof errmsg, _("cannot open libvirt connection"));
caml_invalid_argument (errmsg);
}
/* Suppress default behaviour of printing errors to stderr. Note
* you can't set this to NULL to ignore errors; setting it to NULL
* restores the default error handler ...
*/
virConnSetErrorFunc (conn, NULL, ignore_errors);
/* Look up the pool. */
poolname = String_val (poolnamev);
pool = virStoragePoolLookupByUUIDString (conn, poolname);
if (!pool)
pool = virStoragePoolLookupByName (conn, poolname);
if (!pool) {
err = virGetLastError ();
snprintf (errmsg, sizeof errmsg,
_("cannot find libvirt pool '%s': %s\n\nUse `virsh pool-list --all' to list all available pools, and `virsh pool-dumpxml <pool>' to display details about a particular pool.\n\nTo set the pool which virt-v2v uses, add the `-os <pool>' option."),
poolname, err->message);
virConnectClose (conn);
caml_invalid_argument (errmsg);
}
CAMLreturnT (virStoragePoolPtr, pool);
}
value
v2v_dumpxml (value passwordv, value connv, value domnamev)
{
CAMLparam3 (passwordv, connv, domnamev);
CAMLlocal1 (retv);
const char *password = NULL;
const char *conn_uri = NULL;
const char *domname;
virConnectAuth authdata;
/* We have to assemble the error on the stack because a dynamic
* string couldn't be freed.
*/
char errmsg[ERROR_MESSAGE_LEN];
virErrorPtr err;
virConnectPtr conn;
virDomainPtr dom;
int is_test_uri = 0;
char *xml;
if (passwordv != Val_int (0))
password = String_val (Field (passwordv, 0)); /* Some password */
if (connv != Val_int (0)) {
conn_uri = String_val (Field (connv, 0)); /* Some conn */
is_test_uri = STRPREFIX (conn_uri, "test:");
}
/* Set up authentication wrapper. */
authdata = *virConnectAuthPtrDefault;
authdata.cb = libvirt_auth_default_wrapper;
authdata.cbdata = (void *) password;
/* Note this cannot be a read-only connection since we need to use
* the VIR_DOMAIN_XML_SECURE flag below.
*/
conn = virConnectOpenAuth (conn_uri, &authdata, 0);
if (conn == NULL) {
if (conn_uri)
snprintf (errmsg, sizeof errmsg,
_("cannot open libvirt connection '%s'"), conn_uri);
else
snprintf (errmsg, sizeof errmsg, _("cannot open libvirt connection"));
caml_invalid_argument (errmsg);
}
/* Suppress default behaviour of printing errors to stderr. Note
* you can't set this to NULL to ignore errors; setting it to NULL
* restores the default error handler ...
*/
virConnSetErrorFunc (conn, NULL, ignore_errors);
/* Look up the domain. */
domname = String_val (domnamev);
dom = virDomainLookupByUUIDString (conn, domname);
if (!dom)
dom = virDomainLookupByName (conn, domname);
if (!dom) {
err = virGetLastError ();
snprintf (errmsg, sizeof errmsg,
_("cannot find libvirt domain '%s': %s"), domname, err->message);
virConnectClose (conn);
caml_invalid_argument (errmsg);
}
/* As a side-effect we check that the domain is shut down. Of course
* this is only appropriate for virt-v2v. (RHBZ#1138586)
*/
if (!is_test_uri) {
const int state = get_dom_state (dom);
if (state == VIR_DOMAIN_RUNNING ||
state == VIR_DOMAIN_BLOCKED ||
state == VIR_DOMAIN_PAUSED) {
snprintf (errmsg, sizeof errmsg,
_("libvirt domain '%s' is running or paused. It must be shut down in order to perform virt-v2v conversion"),
domname);
virDomainFree (dom);
virConnectClose (conn);
caml_invalid_argument (errmsg);
}
}
/* Use VIR_DOMAIN_XML_SECURE to get passwords (RHBZ#1174123). */
xml = virDomainGetXMLDesc (dom, VIR_DOMAIN_XML_SECURE);
if (xml == NULL) {
err = virGetLastError ();
snprintf (errmsg, sizeof errmsg,
_("cannot fetch XML description of guest '%s': %s"),
domname, err->message);
virDomainFree (dom);
virConnectClose (conn);
caml_invalid_argument (errmsg);
}
virDomainFree (dom);
virConnectClose (conn);
retv = caml_copy_string (xml);
free (xml);
CAMLreturn (retv);
}
value
v2v_pool_dumpxml (value connv, value poolnamev)
{
CAMLparam2 (connv, poolnamev);
CAMLlocal1 (retv);
/* We have to assemble the error on the stack because a dynamic
* string couldn't be freed.
*/
char errmsg[ERROR_MESSAGE_LEN];
virErrorPtr err;
virConnectPtr conn;
virStoragePoolPtr pool;
char *xml;
/* Look up the pool. */
pool = connect_and_load_pool (connv, poolnamev);
conn = virStoragePoolGetConnect (pool);
xml = virStoragePoolGetXMLDesc (pool, 0);
if (xml == NULL) {
err = virGetLastError ();
snprintf (errmsg, sizeof errmsg,
_("cannot fetch XML description of pool '%s': %s"),
String_val (poolnamev), err->message);
virStoragePoolFree (pool);
virConnectClose (conn);
caml_invalid_argument (errmsg);
}
virStoragePoolFree (pool);
virConnectClose (conn);
retv = caml_copy_string (xml);
free (xml);
CAMLreturn (retv);
}
value
v2v_vol_dumpxml (value connv, value poolnamev, value volnamev)
{
CAMLparam3 (connv, poolnamev, volnamev);
CAMLlocal1 (retv);
const char *volname;
/* We have to assemble the error on the stack because a dynamic
* string couldn't be freed.
*/
char errmsg[ERROR_MESSAGE_LEN];
virErrorPtr err;
virConnectPtr conn;
virStoragePoolPtr pool;
virStorageVolPtr vol;
char *xml;
/* Look up the pool. */
pool = connect_and_load_pool (connv, poolnamev);
conn = virStoragePoolGetConnect (pool);
/* Look up the volume. */
volname = String_val (volnamev);
vol = virStorageVolLookupByName (pool, volname);
if (!vol) {
err = virGetLastError ();
snprintf (errmsg, sizeof errmsg,
_("cannot find libvirt volume '%s': %s"), volname, err->message);
virStoragePoolFree (pool);
virConnectClose (conn);
caml_invalid_argument (errmsg);
}
xml = virStorageVolGetXMLDesc (vol, 0);
if (xml == NULL) {
err = virGetLastError ();
snprintf (errmsg, sizeof errmsg,
_("cannot fetch XML description of volume '%s': %s"),
volname, err->message);
virStorageVolFree (vol);
virStoragePoolFree (pool);
virConnectClose (conn);
caml_invalid_argument (errmsg);
}
virStorageVolFree (vol);
virStoragePoolFree (pool);
virConnectClose (conn);
retv = caml_copy_string (xml);
free (xml);
CAMLreturn (retv);
}
value
v2v_capabilities (value connv, value unitv)
{
CAMLparam2 (connv, unitv);
CAMLlocal1 (capabilitiesv);
const char *conn_uri = NULL;
char *capabilities;
/* We have to assemble the error on the stack because a dynamic
* string couldn't be freed.
*/
char errmsg[ERROR_MESSAGE_LEN];
virErrorPtr err;
virConnectPtr conn;
if (connv != Val_int (0))
conn_uri = String_val (Field (connv, 0)); /* Some conn */
/* We have to call the default authentication handler, not least
* since it handles all the PolicyKit crap. However it also makes
* coding this simpler.
*/
conn = virConnectOpenAuth (conn_uri, virConnectAuthPtrDefault,
VIR_CONNECT_RO);
if (conn == NULL) {
if (conn_uri)
snprintf (errmsg, sizeof errmsg,
_("cannot open libvirt connection '%s'"), conn_uri);
else
snprintf (errmsg, sizeof errmsg, _("cannot open libvirt connection"));
caml_invalid_argument (errmsg);
}
/* Suppress default behaviour of printing errors to stderr. Note
* you can't set this to NULL to ignore errors; setting it to NULL
* restores the default error handler ...
*/
virConnSetErrorFunc (conn, NULL, ignore_errors);
capabilities = virConnectGetCapabilities (conn);
if (!capabilities) {
err = virGetLastError ();
snprintf (errmsg, sizeof errmsg,
_("cannot get libvirt hypervisor capabilities: %s"),
err->message);
virConnectClose (conn);
caml_invalid_argument (errmsg);
}
capabilitiesv = caml_copy_string (capabilities);
free (capabilities);
virConnectClose (conn);
CAMLreturn (capabilitiesv);
}
value
v2v_domain_exists (value connv, value domnamev)
{
CAMLparam2 (connv, domnamev);
const char *conn_uri = NULL;
const char *domname;
/* We have to assemble the error on the stack because a dynamic
* string couldn't be freed.
*/
char errmsg[ERROR_MESSAGE_LEN];
virErrorPtr err;
virConnectPtr conn;
virDomainPtr dom;
int domain_exists;
if (connv != Val_int (0))
conn_uri = String_val (Field (connv, 0)); /* Some conn */
/* We have to call the default authentication handler, not least
* since it handles all the PolicyKit crap. However it also makes
* coding this simpler.
*/
conn = virConnectOpenAuth (conn_uri, virConnectAuthPtrDefault,
VIR_CONNECT_RO);
if (conn == NULL) {
if (conn_uri)
snprintf (errmsg, sizeof errmsg,
_("cannot open libvirt connection '%s'"), conn_uri);
else
snprintf (errmsg, sizeof errmsg, _("cannot open libvirt connection"));
caml_invalid_argument (errmsg);
}
/* Suppress default behaviour of printing errors to stderr. Note
* you can't set this to NULL to ignore errors; setting it to NULL
* restores the default error handler ...
*/
virConnSetErrorFunc (conn, NULL, ignore_errors);
/* Look up the domain. */
domname = String_val (domnamev);
dom = virDomainLookupByName (conn, domname);
if (dom) {
domain_exists = 1;
virDomainFree (dom);
}
else {
err = virGetLastError ();
if (err->code == VIR_ERR_NO_DOMAIN)
domain_exists = 0;
else {
snprintf (errmsg, sizeof errmsg,
_("cannot find libvirt domain '%s': %s"),
domname, err->message);
virConnectClose (conn);
caml_invalid_argument (errmsg);
}
}
virConnectClose (conn);
CAMLreturn (Val_bool (domain_exists));
}
/* XXX This function is stuffed here for convenience (accessing
* libvirt), not because it belongs logically with the rest of the
* functions in this file.
*/
value
v2v_libvirt_get_version (value unitv)
{
CAMLparam1 (unitv);
CAMLlocal1 (rv);
int major, minor, release;
/* We have to assemble the error on the stack because a dynamic
* string couldn't be freed.
*/
char errmsg[ERROR_MESSAGE_LEN];
unsigned long ver;
virErrorPtr err;
if (virGetVersion (&ver, NULL, NULL) == -1) {
err = virGetLastError ();
snprintf (errmsg, sizeof errmsg,
_("cannot get libvirt library version: %s"),
err->message);
caml_invalid_argument (errmsg);
}
major = ver / 1000000UL;
minor = ver / 1000UL % 1000UL;
release = ver % 1000UL;
rv = caml_alloc (3, 0);
Store_field (rv, 0, Val_int (major));
Store_field (rv, 1, Val_int (minor));
Store_field (rv, 2, Val_int (release));
CAMLreturn (rv);
}
#else /* !HAVE_LIBVIRT */
#define NO_LIBVIRT(proto) \
proto __attribute__((noreturn)); \
proto \
{ \
caml_invalid_argument ("virt-v2v was compiled without libvirt support"); \
}
NO_LIBVIRT (value v2v_dumpxml (value connv, value domv))
NO_LIBVIRT (value v2v_pool_dumpxml (value connv, value poolv))
NO_LIBVIRT (value v2v_vol_dumpxml (value connv, value poolnamev, value volnamev))
NO_LIBVIRT (value v2v_capabilities (value connv, value unitv))
NO_LIBVIRT (value v2v_domain_exists (value connv, value domnamev))
NO_LIBVIRT (value v2v_libvirt_get_version (value unitv))
#endif /* !HAVE_LIBVIRT */
|