2 * Copyright (C) 2010-2011 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.
28 #include <libvirt/libvirt.h>
29 #include <libvirt/virterror.h>
38 static void try_df (const char *name, const char *uuid, const char *dev, int offset);
39 static int find_dev_in_devices (const char *dev, char **devices);
41 /* Since we want this function to be robust against very bad failure
42 * cases (hello, https://bugzilla.kernel.org/show_bug.cgi?id=18792) it
43 * won't exit on guestfs failures.
46 df_on_handle (const char *name, const char *uuid, char **devices, int offset)
51 int free_devices = 0, is_lv;
54 fprintf (stderr, "df_on_handle %s devices=", name);
57 for (i = 0; devices[i] != NULL; ++i) {
60 fputs (devices[i], stderr);
65 fprintf (stderr, "null");
69 if (devices == NULL) {
70 devices = guestfs_list_devices (g);
75 /* Mask LVM for just the devices in the set. */
76 if (guestfs_lvm_set_filter (g, devices) == -1)
80 /* list-filesystems will return filesystems on every device ... */
81 fses = guestfs_list_filesystems (g);
85 /* ... so we need to filter out only the devices we are interested in. */
86 for (i = 0; fses[i] != NULL; i += 2) {
87 if (STRNEQ (fses[i+1], "") &&
88 STRNEQ (fses[i+1], "swap") &&
89 STRNEQ (fses[i+1], "unknown")) {
90 is_lv = guestfs_is_lv (g, fses[i]);
91 if (is_lv > 0) /* LVs are OK because of the LVM filter */
92 try_df (name, uuid, fses[i], -1);
93 else if (is_lv == 0) {
94 if (find_dev_in_devices (fses[i], devices))
95 try_df (name, uuid, fses[i], offset);
104 for (i = 0; fses[i] != NULL; ++i)
110 for (i = 0; devices[i] != NULL; ++i)
119 find_dev_in_devices (const char *dev, char **devices)
123 for (i = 0; devices[i] != NULL; ++i) {
124 if (STRPREFIX (dev, devices[i]))
132 try_df (const char *name, const char *uuid,
133 const char *dev, int offset)
135 struct guestfs_statvfs *stat = NULL;
136 guestfs_error_handler_cb old_error_cb;
137 void *old_error_data;
140 fprintf (stderr, "try_df %s %s %d\n", name, dev, offset);
142 /* Try mounting and stating the device. This might reasonably fail,
143 * so don't show errors.
145 old_error_cb = guestfs_get_error_handler (g, &old_error_data);
146 guestfs_set_error_handler (g, NULL, NULL);
148 if (guestfs_mount_ro (g, dev, "/") == 0) {
149 stat = guestfs_statvfs (g, "/");
150 guestfs_umount_all (g);
153 guestfs_set_error_handler (g, old_error_cb, old_error_data);
156 print_stat (name, uuid, dev, offset, stat);
157 guestfs_free_statvfs (stat);