X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=src%2Finspect.c;h=bce0fd6e7e1b2972b7b3e11bcc219aa4df429344;hb=8ea62c8d7f3f7f7e4057b93105cf979271aa13f4;hp=d1bb7bb42802d88daf6f6d53d1c83e277e1223f5;hpb=8289aa1ad68ec94c87fc4d538f638d8816052d92;p=libguestfs.git diff --git a/src/inspect.c b/src/inspect.c index d1bb7bb..bce0fd6 100644 --- a/src/inspect.c +++ b/src/inspect.c @@ -57,6 +57,8 @@ static pcre *re_xdev; static pcre *re_windows_version; static void compile_regexps (void) __attribute__((constructor)); +static void free_regexps (void) __attribute__((destructor)); + static void compile_regexps (void) { @@ -88,6 +90,22 @@ compile_regexps (void) COMPILE (re_windows_version, "^(\\d+)\\.(\\d+)", 0); } +static void +free_regexps (void) +{ + pcre_free (re_file_elf); + pcre_free (re_file_win64); + pcre_free (re_elf_ppc64); + pcre_free (re_fedora); + pcre_free (re_rhel_old); + pcre_free (re_rhel); + pcre_free (re_rhel_no_minor); + pcre_free (re_debian); + pcre_free (re_aug_seq); + pcre_free (re_xdev); + pcre_free (re_windows_version); +} + /* Match a regular expression which contains no captures. Returns * true if it matches or false if it doesn't. */ @@ -1193,7 +1211,7 @@ guestfs__inspect_get_distro (guestfs_h *g, const char *root) return ret; } -int +int guestfs__inspect_get_major_version (guestfs_h *g, const char *root) { struct inspect_fs *fs = search_for_root (g, root); @@ -1203,7 +1221,7 @@ guestfs__inspect_get_major_version (guestfs_h *g, const char *root) return fs->major_version; } -int +int guestfs__inspect_get_minor_version (guestfs_h *g, const char *root) { struct inspect_fs *fs = search_for_root (g, root); @@ -1295,3 +1313,140 @@ guestfs__inspect_get_filesystems (guestfs_h *g, const char *root) return ret; } + +/* 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; + size_t ret_size; + + ret = safe_malloc (g, sizeof (char *)); + ret[0] = NULL; + ret_size = 0; + + /* 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. + */ + char **devices; + devices = guestfs_list_devices (g); + if (devices == NULL) { + free_string_list (ret); + return NULL; + } + char **partitions; + partitions = guestfs_list_partitions (g); + if (partitions == NULL) { + free_string_list (devices); + free_string_list (ret); + return NULL; + } + + 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); + free_string_list (devices); + + /* 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); + free_string_list (partitions); + + if (feature_available (g, "lvm2")) { + /* Use vfs-type to check for filesystems on LVs. */ + char **lvs; + lvs = guestfs_lvs (g); + if (lvs == NULL) { + free_string_list (ret); + return NULL; + } + + for (i = 0; lvs[i] != NULL; ++i) + check_with_vfs_type (g, lvs[i], &ret, &ret_size); + free_string_list (lvs); + } + + return ret; +} + +/* 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 { + /* 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; +}