2 * Copyright (C) 2010 Red Hat Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #include "guestfs-internal.h"
31 #include "guestfs-internal-actions.h"
32 #include "guestfs_protocol.h"
36 * The current implementation just uses guestfs_vfs_type and doesn't
37 * try mounting anything, but we reserve the right in future to try
38 * mounting filesystems.
41 static void remove_from_list (char **list, const char *item);
42 static void check_with_vfs_type (guestfs_h *g, const char *dev, char ***ret, size_t *ret_size);
45 guestfs__list_filesystems (guestfs_h *g)
51 ret = safe_malloc (g, sizeof (char *));
55 /* Look to see if any devices directly contain filesystems
56 * (RHBZ#590167). However vfs-type will fail to tell us anything
57 * useful about devices which just contain partitions, so we also
58 * get the list of partitions and exclude the corresponding devices
59 * by using part-to-dev.
62 devices = guestfs_list_devices (g);
63 if (devices == NULL) {
64 guestfs___free_string_list (ret);
68 partitions = guestfs_list_partitions (g);
69 if (partitions == NULL) {
70 guestfs___free_string_list (devices);
71 guestfs___free_string_list (ret);
75 for (i = 0; partitions[i] != NULL; ++i) {
76 char *dev = guestfs_part_to_dev (g, partitions[i]);
78 remove_from_list (devices, dev);
82 /* Use vfs-type to check for filesystems on devices. */
83 for (i = 0; devices[i] != NULL; ++i)
84 check_with_vfs_type (g, devices[i], &ret, &ret_size);
85 guestfs___free_string_list (devices);
87 /* Use vfs-type to check for filesystems on partitions. */
88 for (i = 0; partitions[i] != NULL; ++i)
89 check_with_vfs_type (g, partitions[i], &ret, &ret_size);
90 guestfs___free_string_list (partitions);
92 if (guestfs___feature_available (g, "lvm2")) {
93 /* Use vfs-type to check for filesystems on LVs. */
95 lvs = guestfs_lvs (g);
97 guestfs___free_string_list (ret);
101 for (i = 0; lvs[i] != NULL; ++i)
102 check_with_vfs_type (g, lvs[i], &ret, &ret_size);
103 guestfs___free_string_list (lvs);
109 /* If 'item' occurs in 'list', remove and free it. */
111 remove_from_list (char **list, const char *item)
115 for (i = 0; list[i] != NULL; ++i)
116 if (STREQ (list[i], item)) {
118 for (; list[i+1] != NULL; ++i)
125 /* Use vfs-type to look for a filesystem of some sort on 'dev'.
126 * Apart from some types which we ignore, add the result to the
130 check_with_vfs_type (guestfs_h *g, const char *device,
131 char ***ret, size_t *ret_size)
135 guestfs_error_handler_cb old_error_cb = g->error_cb;
137 char *vfs_type = guestfs_vfs_type (g, device);
138 g->error_cb = old_error_cb;
141 v = safe_strdup (g, "unknown");
143 /* Ignore all "*_member" strings. In libblkid these are returned
144 * for things which are members of some RAID or LVM set, most
145 * importantly "LVM2_member" which is a PV.
147 size_t n = strlen (vfs_type);
148 if (n >= 7 && STREQ (&vfs_type[n-7], "_member")) {
153 /* Ignore LUKS-encrypted partitions. These are also containers. */
154 if (STREQ (vfs_type, "crypto_LUKS")) {
162 /* Extend the return array. */
163 size_t i = *ret_size;
165 *ret = safe_realloc (g, *ret, (*ret_size + 1) * sizeof (char *));
166 (*ret)[i] = safe_strdup (g, device);