- static int initialized = 0;
- if (!initialized) {
- initialized = 1;
-
- if (virInitialize () == -1)
- return -1;
-
- xmlInitParser ();
- LIBXML_TEST_VERSION;
- }
-
- int r = -1, nr_added = 0;
- virErrorPtr err;
- virConnectPtr conn = NULL;
- virDomainPtr dom = NULL;
- xmlDocPtr doc = NULL;
- xmlXPathContextPtr xpathCtx = NULL;
- xmlXPathObjectPtr xpathObj = NULL;
- char *xml = NULL;
-
- /* Connect to libvirt, find the domain. */
- conn = virConnectOpenReadOnly (libvirt_uri);
- if (!conn) {
- err = virGetLastError ();
- fprintf (stderr, _("guestfish: could not connect to libvirt (code %d, domain %d): %s\n"),
- err->code, err->domain, err->message);
- goto cleanup;
- }
-
- dom = virDomainLookupByName (conn, guest);
- if (!dom) {
- err = virConnGetLastError (conn);
- fprintf (stderr, _("guestfish: no libvirt domain called '%s': %s\n"),
- guest, err->message);
- goto cleanup;
- }
- if (!read_only) {
- virDomainInfo info;
- if (virDomainGetInfo (dom, &info) == -1) {
- err = virConnGetLastError (conn);
- fprintf (stderr, _("guestfish: error getting domain info about '%s': %s\n"),
- guest, err->message);
- goto cleanup;
- }
- if (info.state != VIR_DOMAIN_SHUTOFF) {
- fprintf (stderr, _("guestfish: error: '%s' is a live virtual machine.\nYou must use '--ro' because write access to a running virtual machine can\ncause disk corruption.\n"),
- guest);
- goto cleanup;
- }
- }
-
- /* Domain XML. */
- xml = virDomainGetXMLDesc (dom, 0);
-
- if (!xml) {
- err = virConnGetLastError (conn);
- fprintf (stderr, _("guestfish: error reading libvirt XML information about '%s': %s\n"),
- guest, err->message);
- goto cleanup;
- }
-
- /* Now the horrible task of parsing out the fields we need from the XML.
- * http://www.xmlsoft.org/examples/xpath1.c
- */
- doc = xmlParseMemory (xml, strlen (xml));
- if (doc == NULL) {
- fprintf (stderr, _("guestfish: unable to parse XML information returned by libvirt\n"));
- goto cleanup;
- }