2 * Copyright (C) 2010 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <libvirt/libvirt.h>
28 #include <libvirt/virterror.h>
33 #define GUESTFS_PRIVATE_FOR_EACH_DISK 1
41 /* Limit the number of devices we will ever add to the appliance. The
42 * overall limit in current libguestfs is 25: 26 = number of letters
43 * in the English alphabet since we are only confident that
44 * /dev/sd[a-z] will work because of various limits, minus 1 because
45 * that may be used by the ext2 initial filesystem. (RHBZ#635373).
49 /* The list of domains and disks that we build up in
50 * get_domains_from_libvirt.
55 char *format; /* could be NULL */
65 struct domain *domains = NULL;
69 compare_domain_names (const void *p1, const void *p2)
71 const struct domain *d1 = p1;
72 const struct domain *d2 = p2;
74 return strcmp (d1->name, d2->name);
78 free_domain (struct domain *domain)
80 struct disk *disk, *next;
82 for (disk = domain->disks; disk; disk = next) {
84 free (disk->filename);
93 static void add_domains_by_id (virConnectPtr conn, int *ids, size_t n);
94 static void add_domains_by_name (virConnectPtr conn, char **names, size_t n);
95 static void add_domain (virDomainPtr dom);
96 static int add_disk (guestfs_h *g, const char *filename, const char *format, void *domain_vp);
97 static void multi_df (struct domain *, size_t n);
100 get_domains_from_libvirt (void)
105 size_t i, j, nr_disks_added;
110 /* Get the list of all domains. */
111 conn = virConnectOpenReadOnly (libvirt_uri);
113 err = virGetLastError ();
115 _("%s: could not connect to libvirt (code %d, domain %d): %s"),
116 program_name, err->code, err->domain, err->message);
120 n = virConnectNumOfDomains (conn);
122 err = virGetLastError ();
124 _("%s: could not get number of running domains (code %d, domain %d): %s"),
125 program_name, err->code, err->domain, err->message);
130 n = virConnectListDomains (conn, ids, n);
132 err = virGetLastError ();
134 _("%s: could not list running domains (code %d, domain %d): %s"),
135 program_name, err->code, err->domain, err->message);
139 add_domains_by_id (conn, ids, n);
141 n = virConnectNumOfDefinedDomains (conn);
143 err = virGetLastError ();
145 _("%s: could not get number of inactive domains (code %d, domain %d): %s"),
146 program_name, err->code, err->domain, err->message);
151 n = virConnectListDefinedDomains (conn, names, n);
153 err = virGetLastError ();
155 _("%s: could not list inactive domains (code %d, domain %d): %s"),
156 program_name, err->code, err->domain, err->message);
160 add_domains_by_name (conn, names, n);
162 /* You must free these even though the libvirt documentation doesn't
165 for (i = 0; i < (size_t) n; ++i)
168 virConnectClose (conn);
174 /* Sort the domains alphabetically by name for display. */
175 qsort (domains, nr_domains, sizeof (struct domain), compare_domain_names);
179 /* To minimize the number of times we have to launch the appliance,
180 * shuffle as many domains together as we can, but not exceeding
181 * MAX_DISKS per request. If --one-per-guest was requested then only
182 * request disks from a single guest each time.
183 * Interesting application for NP-complete knapsack problem here.
186 for (i = 0; i < nr_domains; ++i)
187 multi_df (&domains[i], 1);
189 for (i = 0; i < nr_domains; /**/) {
192 /* Make a request with domains [i..j-1]. */
193 for (j = i; j < nr_domains; ++j) {
194 if (nr_disks_added + domains[j].nr_disks > MAX_DISKS)
196 nr_disks_added += domains[j].nr_disks;
198 multi_df (&domains[i], j-i);
204 /* Free up domains structure. */
205 for (i = 0; i < nr_domains; ++i)
206 free_domain (&domains[i]);
211 add_domains_by_id (virConnectPtr conn, int *ids, size_t n)
216 for (i = 0; i < n; ++i) {
217 if (ids[i] != 0) { /* RHBZ#538041 */
218 dom = virDomainLookupByID (conn, ids[i]);
219 if (dom) { /* transient errors are possible here, ignore them */
228 add_domains_by_name (virConnectPtr conn, char **names, size_t n)
233 for (i = 0; i < n; ++i) {
234 dom = virDomainLookupByName (conn, names[i]);
235 if (dom) { /* transient errors are possible here, ignore them */
243 add_domain (virDomainPtr dom)
245 struct domain *domain;
247 domains = realloc (domains, (nr_domains + 1) * sizeof (struct domain));
248 if (domains == NULL) {
253 domain = &domains[nr_domains];
256 domain->name = strdup (virDomainGetName (dom));
257 if (domain->name == NULL) {
262 char uuid[VIR_UUID_STRING_BUFLEN];
263 if (virDomainGetUUIDString (dom, uuid) == 0) {
264 domain->uuid = strdup (uuid);
265 if (domain->uuid == NULL) {
273 domain->disks = NULL;
274 int n = guestfs___for_each_disk (g, dom, add_disk, domain);
277 domain->nr_disks = n;
279 if (domain->nr_disks > MAX_DISKS) {
281 _("%s: ignoring %s, it has too many disks (%zu > %d)"),
282 program_name, domain->name, domain->nr_disks, MAX_DISKS);
283 free_domain (domain);
290 add_disk (guestfs_h *g, const char *filename, const char *format,
293 struct domain *domain = domain_vp;
296 disk = malloc (sizeof *disk);
302 disk->next = domain->disks;
303 domain->disks = disk;
305 disk->filename = strdup (filename);
306 if (disk->filename == NULL) {
311 disk->format = strdup (format);
312 if (disk->format == NULL) {
324 count_strings (char **argv)
328 for (i = 0; argv[i] != NULL; ++i)
333 static void reset_guestfs_handle (void);
334 static void add_disks_to_handle_reverse (struct disk *disk);
336 /* Perform 'df' operation on the domain(s) given in the list. */
338 multi_df (struct domain *domains, size_t n)
345 /* Add all the disks to the handle (since they were added in reverse
346 * order, we must add them here in reverse too).
348 for (i = 0; i < n; ++i)
349 add_disks_to_handle_reverse (domains[i].disks);
351 /* Launch the handle. */
352 if (guestfs_launch (g) == -1)
355 devices = guestfs_list_devices (g);
359 /* Check the number of disks we think we added is the same as the
360 * number of devices returned by libguestfs.
363 for (i = 0; i < n; ++i)
364 nd += domains[i].nr_disks;
365 assert (nd == count_strings (devices));
368 for (i = 0; i < n; ++i) {
369 /* So that &devices[nd] is a NULL-terminated list of strings. */
370 char *p = devices[nd + domains[i].nr_disks];
371 devices[nd + domains[i].nr_disks] = NULL;
373 r = df_on_handle (domains[i].name, domains[i].uuid, &devices[nd], nd);
375 /* Restore devices to original. */
376 devices[nd + domains[i].nr_disks] = p;
377 nd += domains[i].nr_disks;
379 /* Something broke in df_on_handle. Give up on the remaining
380 * devices for this handle, but keep going on the next handle.
386 for (i = 0; devices[i] != NULL; ++i)
390 /* Reset the handle. */
391 reset_guestfs_handle ();
395 add_disks_to_handle_reverse (struct disk *disk)
400 add_disks_to_handle_reverse (disk->next);
402 struct guestfs_add_drive_opts_argv optargs = { .bitmask = 0 };
404 optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK;
405 optargs.readonly = 1;
408 optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_FORMAT_BITMASK;
409 optargs.format = disk->format;
412 if (guestfs_add_drive_opts_argv (g, disk->filename, &optargs) == -1)
416 /* Close and reopen the libguestfs handle. */
418 reset_guestfs_handle (void)
420 /* Copy the settings from the old handle. */
421 int verbose = guestfs_get_verbose (g);
422 int trace = guestfs_get_trace (g);
426 g = guestfs_create ();
428 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
432 guestfs_set_verbose (g, verbose);
433 guestfs_set_trace (g, trace);