summaryrefslogtreecommitdiffstats
path: root/src/listfs.c
blob: 5ddd1de5b4c2c54bd1393851b128bf1d715ad574 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* libguestfs
 * Copyright (C) 2010 Red Hat Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>

#include "guestfs.h"
#include "guestfs-internal.h"
#include "guestfs-internal-actions.h"
#include "guestfs_protocol.h"

/* List filesystems.
 *
 * The current implementation just uses guestfs_vfs_type and doesn't
 * try mounting anything, but we reserve the right in future to try
 * mounting filesystems.
 */

static void remove_from_list (char **list, const char *item);
static void check_with_vfs_type (guestfs_h *g, const char *dev, char ***ret, size_t *ret_size);

char **
guestfs__list_filesystems (guestfs_h *g)
{
  size_t i;
  char **ret = NULL;
  size_t ret_size = 0;

  char **devices = NULL;
  char **partitions = NULL;
  char **mds = NULL;
  char **lvs = NULL;

  /* Look to see if any devices directly contain filesystems
   * (RHBZ#590167).  However vfs-type will fail to tell us anything
   * useful about devices which just contain partitions, so we also
   * get the list of partitions and exclude the corresponding devices
   * by using part-to-dev.
   */
  devices = guestfs_list_devices (g);
  if (devices == NULL) goto error;
  partitions = guestfs_list_partitions (g);
  if (partitions == NULL) goto error;
  mds = guestfs_list_md_devices (g);
  if (mds == NULL) goto error;

  for (i = 0; partitions[i] != NULL; ++i) {
    char *dev = guestfs_part_to_dev (g, partitions[i]);
    if (dev)
      remove_from_list (devices, dev);
    free (dev);
  }

  /* Use vfs-type to check for filesystems on devices. */
  for (i = 0; devices[i] != NULL; ++i)
    check_with_vfs_type (g, devices[i], &ret, &ret_size);

  /* Use vfs-type to check for filesystems on partitions. */
  for (i = 0; partitions[i] != NULL; ++i)
    check_with_vfs_type (g, partitions[i], &ret, &ret_size);

  /* Use vfs-type to check for filesystems on md devices. */
  for (i = 0; mds[i] != NULL; ++i)
    check_with_vfs_type (g, mds[i], &ret, &ret_size);

  if (guestfs___feature_available (g, "lvm2")) {
    /* Use vfs-type to check for filesystems on LVs. */
    lvs = guestfs_lvs (g);
    if (lvs == NULL) goto error;

    for (i = 0; lvs[i] != NULL; ++i)
      check_with_vfs_type (g, lvs[i], &ret, &ret_size);
  }

  guestfs___free_string_list (devices);
  guestfs___free_string_list (partitions);
  guestfs___free_string_list (mds);
  if (lvs) guestfs___free_string_list (lvs);
  return ret;

 error:
  if (devices) guestfs___free_string_list (devices);
  if (partitions) guestfs___free_string_list (partitions);
  if (mds) guestfs___free_string_list (mds);
  //if (lvs) guestfs___free_string_list (lvs);
  if (ret) guestfs___free_string_list (ret);
  return NULL;
}

/* If 'item' occurs in 'list', remove and free it. */
static void
remove_from_list (char **list, const char *item)
{
  size_t i;

  for (i = 0; list[i] != NULL; ++i)
    if (STREQ (list[i], item)) {
      free (list[i]);
      for (; list[i+1] != NULL; ++i)
        list[i] = list[i+1];
      list[i] = NULL;
      return;
    }
}

/* Use vfs-type to look for a filesystem of some sort on 'dev'.
 * Apart from some types which we ignore, add the result to the
 * 'ret' string list.
 */
static void
check_with_vfs_type (guestfs_h *g, const char *device,
                     char ***ret, size_t *ret_size)
{
  char *v;

  guestfs_error_handler_cb old_error_cb = g->error_cb;
  g->error_cb = NULL;
  char *vfs_type = guestfs_vfs_type (g, device);
  g->error_cb = old_error_cb;

  if (!vfs_type)
    v = safe_strdup (g, "unknown");
  else if (STREQ (vfs_type, "")) {
    free (vfs_type);
    v = safe_strdup (g, "unknown");
  }
  else {
    /* Ignore all "*_member" strings.  In libblkid these are returned
     * for things which are members of some RAID or LVM set, most
     * importantly "LVM2_member" which is a PV.
     */
    size_t n = strlen (vfs_type);
    if (n >= 7 && STREQ (&vfs_type[n-7], "_member")) {
      free (vfs_type);
      return;
    }

    /* Ignore LUKS-encrypted partitions.  These are also containers. */
    if (STREQ (vfs_type, "crypto_LUKS")) {
      free (vfs_type);
      return;
    }

    v = vfs_type;
  }

  /* Extend the return array. */
  size_t i = *ret_size;
  *ret_size += 2;
  *ret = safe_realloc (g, *ret, (*ret_size + 1) * sizeof (char *));
  (*ret)[i] = safe_strdup (g, device);
  (*ret)[i+1] = v;
  (*ret)[i+2] = NULL;
}