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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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, int readonly, 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,
291 const char *filename, const char *format, int readonly,
294 struct domain *domain = domain_vp;
297 disk = malloc (sizeof *disk);
303 disk->next = domain->disks;
304 domain->disks = disk;
306 disk->filename = strdup (filename);
307 if (disk->filename == NULL) {
312 disk->format = strdup (format);
313 if (disk->format == NULL) {
325 count_strings (char **argv)
329 for (i = 0; argv[i] != NULL; ++i)
334 static void reset_guestfs_handle (void);
335 static void add_disks_to_handle_reverse (struct disk *disk);
337 /* Perform 'df' operation on the domain(s) given in the list. */
339 multi_df (struct domain *domains, size_t n)
346 /* Add all the disks to the handle (since they were added in reverse
347 * order, we must add them here in reverse too).
349 for (i = 0; i < n; ++i)
350 add_disks_to_handle_reverse (domains[i].disks);
352 /* Launch the handle. */
353 if (guestfs_launch (g) == -1)
356 devices = guestfs_list_devices (g);
360 /* Check the number of disks we think we added is the same as the
361 * number of devices returned by libguestfs.
364 for (i = 0; i < n; ++i)
365 nd += domains[i].nr_disks;
366 assert (nd == count_strings (devices));
369 for (i = 0; i < n; ++i) {
370 /* So that &devices[nd] is a NULL-terminated list of strings. */
371 char *p = devices[nd + domains[i].nr_disks];
372 devices[nd + domains[i].nr_disks] = NULL;
374 r = df_on_handle (domains[i].name, domains[i].uuid, &devices[nd], nd);
376 /* Restore devices to original. */
377 devices[nd + domains[i].nr_disks] = p;
378 nd += domains[i].nr_disks;
380 /* Something broke in df_on_handle. Give up on the remaining
381 * devices for this handle, but keep going on the next handle.
387 for (i = 0; devices[i] != NULL; ++i)
391 /* Reset the handle. */
392 reset_guestfs_handle ();
396 add_disks_to_handle_reverse (struct disk *disk)
401 add_disks_to_handle_reverse (disk->next);
403 struct guestfs_add_drive_opts_argv optargs = { .bitmask = 0 };
405 optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK;
406 optargs.readonly = 1;
409 optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_FORMAT_BITMASK;
410 optargs.format = disk->format;
413 if (guestfs_add_drive_opts_argv (g, disk->filename, &optargs) == -1)
417 /* Close and reopen the libguestfs handle. */
419 reset_guestfs_handle (void)
421 /* Copy the settings from the old handle. */
422 int verbose = guestfs_get_verbose (g);
423 int trace = guestfs_get_trace (g);
427 g = guestfs_create ();
429 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
433 guestfs_set_verbose (g, verbose);
434 guestfs_set_trace (g, trace);