void get_all_libvirt_domains (const char *libvirt_uri) { virErrorPtr err; int n; size_t i; /* Get the list of all domains. */ conn = virConnectOpenAuth (libvirt_uri, virConnectAuthPtrDefault, VIR_CONNECT_RO); if (!conn) { err = virGetLastError (); fprintf (stderr, _("%s: could not connect to libvirt (code %d, domain %d): %s\n"), guestfs_int_program_name, err->code, err->domain, err->message); exit (EXIT_FAILURE); } n = virConnectNumOfDomains (conn); if (n == -1) { err = virGetLastError (); fprintf (stderr, _("%s: could not get number of running domains (code %d, domain %d): %s\n"), guestfs_int_program_name, err->code, err->domain, err->message); exit (EXIT_FAILURE); } int ids[n]; n = virConnectListDomains (conn, ids, n); if (n == -1) { err = virGetLastError (); fprintf (stderr, _("%s: could not list running domains (code %d, domain %d): %s\n"), guestfs_int_program_name, err->code, err->domain, err->message); exit (EXIT_FAILURE); } add_domains_by_id (conn, ids, n); n = virConnectNumOfDefinedDomains (conn); if (n == -1) { err = virGetLastError (); fprintf (stderr, _("%s: could not get number of inactive domains (code %d, domain %d): %s\n"), guestfs_int_program_name, err->code, err->domain, err->message); exit (EXIT_FAILURE); } char *names[n]; n = virConnectListDefinedDomains (conn, names, n); if (n == -1) { err = virGetLastError (); fprintf (stderr, _("%s: could not list inactive domains (code %d, domain %d): %s\n"), guestfs_int_program_name, err->code, err->domain, err->message); exit (EXIT_FAILURE); } add_domains_by_name (conn, names, n); /* You must free these even though the libvirt documentation doesn't * mention it. */ for (i = 0; i < (size_t) n; ++i) free (names[i]); /* No domains? */ if (nr_domains == 0) return; /* Sort the domains alphabetically by name for display. */ qsort (domains, nr_domains, sizeof (struct domain), compare_domain_names); }
void get_domains_from_libvirt (void) { virErrorPtr err; virConnectPtr conn; int n; size_t i, j, nr_disks_added; nr_domains = 0; domains = NULL; /* Get the list of all domains. */ conn = virConnectOpenReadOnly (libvirt_uri); if (!conn) { err = virGetLastError (); fprintf (stderr, _("%s: could not connect to libvirt (code %d, domain %d): %s"), program_name, err->code, err->domain, err->message); exit (EXIT_FAILURE); } n = virConnectNumOfDomains (conn); if (n == -1) { err = virGetLastError (); fprintf (stderr, _("%s: could not get number of running domains (code %d, domain %d): %s"), program_name, err->code, err->domain, err->message); exit (EXIT_FAILURE); } int ids[n]; n = virConnectListDomains (conn, ids, n); if (n == -1) { err = virGetLastError (); fprintf (stderr, _("%s: could not list running domains (code %d, domain %d): %s"), program_name, err->code, err->domain, err->message); exit (EXIT_FAILURE); } add_domains_by_id (conn, ids, n); n = virConnectNumOfDefinedDomains (conn); if (n == -1) { err = virGetLastError (); fprintf (stderr, _("%s: could not get number of inactive domains (code %d, domain %d): %s"), program_name, err->code, err->domain, err->message); exit (EXIT_FAILURE); } char *names[n]; n = virConnectListDefinedDomains (conn, names, n); if (n == -1) { err = virGetLastError (); fprintf (stderr, _("%s: could not list inactive domains (code %d, domain %d): %s"), program_name, err->code, err->domain, err->message); exit (EXIT_FAILURE); } add_domains_by_name (conn, names, n); /* You must free these even though the libvirt documentation doesn't * mention it. */ for (i = 0; i < (size_t) n; ++i) free (names[i]); virConnectClose (conn); /* No domains? */ if (nr_domains == 0) return; /* Sort the domains alphabetically by name for display. */ qsort (domains, nr_domains, sizeof (struct domain), compare_domain_names); print_title (); /* To minimize the number of times we have to launch the appliance, * shuffle as many domains together as we can, but not exceeding * MAX_DISKS per request. If --one-per-guest was requested then only * request disks from a single guest each time. * Interesting application for NP-complete knapsack problem here. */ if (one_per_guest) { for (i = 0; i < nr_domains; ++i) multi_df (&domains[i], 1); } else { for (i = 0; i < nr_domains; /**/) { nr_disks_added = 0; /* Make a request with domains [i..j-1]. */ for (j = i; j < nr_domains; ++j) { if (nr_disks_added + domains[j].nr_disks > MAX_DISKS) break; nr_disks_added += domains[j].nr_disks; } multi_df (&domains[i], j-i); i = j; } } /* Free up domains structure. */ for (i = 0; i < nr_domains; ++i) free_domain (&domains[i]); free (domains); }