leak: Free PCRE regexps when library is unloaded.
[libguestfs.git] / src / inspect.c
1 /* libguestfs
2  * Copyright (C) 2010 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <inttypes.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <sys/stat.h>
28
29 #include <pcre.h>
30 #include <magic.h>
31 #include <hivex.h>
32 #include <augeas.h>
33
34 #include "ignore-value.h"
35 #include "xstrtol.h"
36
37 #include "guestfs.h"
38 #include "guestfs-internal.h"
39 #include "guestfs-internal-actions.h"
40 #include "guestfs_protocol.h"
41
42 /* Compile all the regular expressions once when the shared library is
43  * loaded.  PCRE is thread safe so we're supposedly OK here if
44  * multiple threads call into the libguestfs API functions below
45  * simultaneously.
46  */
47 static pcre *re_file_elf;
48 static pcre *re_file_win64;
49 static pcre *re_elf_ppc64;
50 static pcre *re_fedora;
51 static pcre *re_rhel_old;
52 static pcre *re_rhel;
53 static pcre *re_rhel_no_minor;
54 static pcre *re_debian;
55 static pcre *re_aug_seq;
56 static pcre *re_xdev;
57 static pcre *re_windows_version;
58
59 static void compile_regexps (void) __attribute__((constructor));
60 static void free_regexps (void) __attribute__((destructor));
61
62 static void
63 compile_regexps (void)
64 {
65   const char *err;
66   int offset;
67
68 #define COMPILE(re,pattern,options)                                     \
69   do {                                                                  \
70     re = pcre_compile ((pattern), (options), &err, &offset, NULL);      \
71     if (re == NULL) {                                                   \
72       ignore_value (write (2, err, strlen (err)));                      \
73       abort ();                                                         \
74     }                                                                   \
75   } while (0)
76
77   COMPILE (re_file_elf,
78            "ELF.*(?:executable|shared object|relocatable), (.+?),", 0);
79   COMPILE (re_elf_ppc64, "64.*PowerPC", 0);
80   COMPILE (re_fedora, "Fedora release (\\d+)", 0);
81   COMPILE (re_rhel_old,
82            "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+).*Update (\\d+)", 0);
83   COMPILE (re_rhel,
84            "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+)\\.(\\d+)", 0);
85   COMPILE (re_rhel_no_minor,
86            "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+)", 0);
87   COMPILE (re_debian, "(\\d+)\\.(\\d+)", 0);
88   COMPILE (re_aug_seq, "/\\d+$", 0);
89   COMPILE (re_xdev, "^/dev/(?:h|s|v|xv)d([a-z]\\d*)$", 0);
90   COMPILE (re_windows_version, "^(\\d+)\\.(\\d+)", 0);
91 }
92
93 static void
94 free_regexps (void)
95 {
96   pcre_free (re_file_elf);
97   pcre_free (re_file_win64);
98   pcre_free (re_elf_ppc64);
99   pcre_free (re_fedora);
100   pcre_free (re_rhel_old);
101   pcre_free (re_rhel);
102   pcre_free (re_rhel_no_minor);
103   pcre_free (re_debian);
104   pcre_free (re_aug_seq);
105   pcre_free (re_xdev);
106   pcre_free (re_windows_version);
107 }
108
109 /* Match a regular expression which contains no captures.  Returns
110  * true if it matches or false if it doesn't.
111  */
112 static int
113 match (guestfs_h *g, const char *str, const pcre *re)
114 {
115   size_t len = strlen (str);
116   int vec[30], r;
117
118   r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
119   if (r == PCRE_ERROR_NOMATCH)
120     return 0;
121   if (r != 1) {
122     /* Internal error -- should not happen. */
123     fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
124              __FILE__, __func__, r, str);
125     return 0;
126   }
127
128   return 1;
129 }
130
131 /* Match a regular expression which contains exactly one capture.  If
132  * the string matches, return the capture, otherwise return NULL.  The
133  * caller must free the result.
134  */
135 static char *
136 match1 (guestfs_h *g, const char *str, const pcre *re)
137 {
138   size_t len = strlen (str);
139   int vec[30], r;
140
141   r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
142   if (r == PCRE_ERROR_NOMATCH)
143     return NULL;
144   if (r != 2) {
145     /* Internal error -- should not happen. */
146     fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
147              __FILE__, __func__, r, str);
148     return NULL;
149   }
150
151   return safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
152 }
153
154 /* Match a regular expression which contains exactly two captures. */
155 static int
156 match2 (guestfs_h *g, const char *str, const pcre *re, char **ret1, char **ret2)
157 {
158   size_t len = strlen (str);
159   int vec[30], r;
160
161   r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
162   if (r == PCRE_ERROR_NOMATCH)
163     return 0;
164   if (r != 3) {
165     /* Internal error -- should not happen. */
166     fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
167              __FILE__, __func__, r, str);
168     return 0;
169   }
170
171   *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
172   *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
173
174   return 1;
175 }
176
177 /* Convert output from 'file' command on ELF files to the canonical
178  * architecture string.  Caller must free the result.
179  */
180 static char *
181 canonical_elf_arch (guestfs_h *g, const char *elf_arch)
182 {
183   const char *r;
184
185   if (strstr (elf_arch, "Intel 80386"))
186     r = "i386";
187   else if (strstr (elf_arch, "Intel 80486"))
188     r = "i486";
189   else if (strstr (elf_arch, "x86-64"))
190     r = "x86_64";
191   else if (strstr (elf_arch, "AMD x86-64"))
192     r = "x86_64";
193   else if (strstr (elf_arch, "SPARC32"))
194     r = "sparc";
195   else if (strstr (elf_arch, "SPARC V9"))
196     r = "sparc64";
197   else if (strstr (elf_arch, "IA-64"))
198     r = "ia64";
199   else if (match (g, elf_arch, re_elf_ppc64))
200     r = "ppc64";
201   else if (strstr (elf_arch, "PowerPC"))
202     r = "ppc";
203   else
204     r = elf_arch;
205
206   char *ret = safe_strdup (g, r);
207   return ret;
208 }
209
210 static int
211 is_regular_file (const char *filename)
212 {
213   struct stat statbuf;
214
215   return lstat (filename, &statbuf) == 0 && S_ISREG (statbuf.st_mode);
216 }
217
218 /* Download and uncompress the cpio file to find binaries within.
219  * Notes:
220  * (1) Two lists must be identical.
221  * (2) Implicit limit of 31 bytes for length of each element (see code
222  * below).
223  */
224 #define INITRD_BINARIES1 "bin/ls bin/rm bin/modprobe sbin/modprobe bin/sh bin/bash bin/dash bin/nash"
225 #define INITRD_BINARIES2 {"bin/ls", "bin/rm", "bin/modprobe", "sbin/modprobe", "bin/sh", "bin/bash", "bin/dash", "bin/nash"}
226
227 static char *
228 cpio_arch (guestfs_h *g, const char *file, const char *path)
229 {
230   char *ret = NULL;
231
232   const char *method;
233   if (strstr (file, "gzip"))
234     method = "zcat";
235   else if (strstr (file, "bzip2"))
236     method = "bzcat";
237   else
238     method = "cat";
239
240   char dir[] = "/tmp/initrd.XXXXXX";
241 #define dir_len (sizeof dir)
242   if (mkdtemp (dir) == NULL) {
243     perrorf (g, "mkdtemp");
244     goto out;
245   }
246
247   char dir_initrd[dir_len + 16];
248   snprintf (dir_initrd, dir_len + 16, "%s/initrd", dir);
249   if (guestfs_download (g, path, dir_initrd) == -1)
250     goto out;
251
252   char cmd[dir_len + 256];
253   snprintf (cmd, dir_len + 256,
254             "cd %s && %s initrd | cpio --quiet -id " INITRD_BINARIES1,
255             dir, method);
256   int r = system (cmd);
257   if (r == -1 || WEXITSTATUS (r) != 0) {
258     perrorf (g, "cpio command failed");
259     goto out;
260   }
261
262   char bin[dir_len + 32];
263   const char *bins[] = INITRD_BINARIES2;
264   size_t i;
265   for (i = 0; i < sizeof bins / sizeof bins[0]; ++i) {
266     snprintf (bin, dir_len + 32, "%s/%s", dir, bins[i]);
267
268     if (is_regular_file (bin)) {
269       int flags = g->verbose ? MAGIC_DEBUG : 0;
270       flags |= MAGIC_ERROR | MAGIC_RAW;
271
272       magic_t m = magic_open (flags);
273       if (m == NULL) {
274         perrorf (g, "magic_open");
275         goto out;
276       }
277
278       if (magic_load (m, NULL) == -1) {
279         perrorf (g, "magic_load: default magic database file");
280         magic_close (m);
281         goto out;
282       }
283
284       const char *line = magic_file (m, bin);
285       if (line == NULL) {
286         perrorf (g, "magic_file: %s", bin);
287         magic_close (m);
288         goto out;
289       }
290
291       char *elf_arch;
292       if ((elf_arch = match1 (g, line, re_file_elf)) != NULL) {
293         ret = canonical_elf_arch (g, elf_arch);
294         free (elf_arch);
295         magic_close (m);
296         goto out;
297       }
298       magic_close (m);
299     }
300   }
301   error (g, "file_architecture: could not determine architecture of cpio archive");
302
303  out:
304   /* Free up the temporary directory.  Note the directory name cannot
305    * contain shell meta-characters because of the way it was
306    * constructed above.
307    */
308   snprintf (cmd, dir_len + 256, "rm -rf %s", dir);
309   ignore_value (system (cmd));
310
311   return ret;
312 #undef dir_len
313 }
314
315 char *
316 guestfs__file_architecture (guestfs_h *g, const char *path)
317 {
318   char *file = NULL;
319   char *elf_arch = NULL;
320   char *ret = NULL;
321
322   /* Get the output of the "file" command.  Note that because this
323    * runs in the daemon, LANG=C so it's in English.
324    */
325   file = guestfs_file (g, path);
326   if (file == NULL)
327     return NULL;
328
329   if ((elf_arch = match1 (g, file, re_file_elf)) != NULL)
330     ret = canonical_elf_arch (g, elf_arch);
331   else if (strstr (file, "PE32 executable"))
332     ret = safe_strdup (g, "i386");
333   else if (strstr (file, "PE32+ executable"))
334     ret = safe_strdup (g, "x86_64");
335   else if (strstr (file, "cpio archive"))
336     ret = cpio_arch (g, file, path);
337   else
338     error (g, "file_architecture: unknown architecture: %s", path);
339
340   free (file);
341   free (elf_arch);
342   return ret;                   /* caller frees */
343 }
344
345 /* The main inspection code. */
346 static int feature_available (guestfs_h *g, const char *feature);
347 static void free_string_list (char **);
348 static int check_for_filesystem_on (guestfs_h *g, const char *device);
349
350 char **
351 guestfs__inspect_os (guestfs_h *g)
352 {
353   /* Remove any information previously stored in the handle. */
354   guestfs___free_inspect_info (g);
355
356   if (guestfs_umount_all (g) == -1)
357     return NULL;
358
359   /* Iterate over all possible devices.  Try to mount each
360    * (read-only).  Examine ones which contain filesystems and add that
361    * information to the handle.
362    */
363   /* Look to see if any devices directly contain filesystems (RHBZ#590167). */
364   char **devices;
365   devices = guestfs_list_devices (g);
366   if (devices == NULL)
367     return NULL;
368
369   size_t i;
370   for (i = 0; devices[i] != NULL; ++i) {
371     if (check_for_filesystem_on (g, devices[i]) == -1) {
372       free_string_list (devices);
373       guestfs___free_inspect_info (g);
374       return NULL;
375     }
376   }
377   free_string_list (devices);
378
379   /* Look at all partitions. */
380   char **partitions;
381   partitions = guestfs_list_partitions (g);
382   if (partitions == NULL) {
383     guestfs___free_inspect_info (g);
384     return NULL;
385   }
386
387   for (i = 0; partitions[i] != NULL; ++i) {
388     if (check_for_filesystem_on (g, partitions[i]) == -1) {
389       free_string_list (partitions);
390       guestfs___free_inspect_info (g);
391       return NULL;
392     }
393   }
394   free_string_list (partitions);
395
396   /* Look at all LVs. */
397   if (feature_available (g, "lvm2")) {
398     char **lvs;
399     lvs = guestfs_lvs (g);
400     if (lvs == NULL) {
401       guestfs___free_inspect_info (g);
402       return NULL;
403     }
404
405     for (i = 0; lvs[i] != NULL; ++i) {
406       if (check_for_filesystem_on (g, lvs[i]) == -1) {
407         free_string_list (lvs);
408         guestfs___free_inspect_info (g);
409         return NULL;
410       }
411     }
412     free_string_list (lvs);
413   }
414
415   /* At this point we have, in the handle, a list of all filesystems
416    * found and data about each one.  Now we assemble the list of
417    * filesystems which are root devices and return that to the user.
418    */
419   size_t count = 0;
420   for (i = 0; i < g->nr_fses; ++i)
421     if (g->fses[i].is_root)
422       count++;
423
424   char **ret = calloc (count+1, sizeof (char *));
425   if (ret == NULL) {
426     perrorf (g, "calloc");
427     guestfs___free_inspect_info (g);
428     return NULL;
429   }
430
431   count = 0;
432   for (i = 0; i < g->nr_fses; ++i) {
433     if (g->fses[i].is_root) {
434       ret[count] = safe_strdup (g, g->fses[i].device);
435       count++;
436     }
437   }
438   ret[count] = NULL;
439
440   return ret;
441 }
442
443 void
444 guestfs___free_inspect_info (guestfs_h *g)
445 {
446   size_t i;
447   for (i = 0; i < g->nr_fses; ++i) {
448     free (g->fses[i].device);
449     free (g->fses[i].product_name);
450     free (g->fses[i].arch);
451     size_t j;
452     for (j = 0; j < g->fses[i].nr_fstab; ++j) {
453       free (g->fses[i].fstab[j].device);
454       free (g->fses[i].fstab[j].mountpoint);
455     }
456     free (g->fses[i].fstab);
457   }
458   free (g->fses);
459   g->nr_fses = 0;
460   g->fses = NULL;
461 }
462
463 static void
464 free_string_list (char **argv)
465 {
466   size_t i;
467   for (i = 0; argv[i] != NULL; ++i)
468     free (argv[i]);
469   free (argv);
470 }
471
472 /* In the Perl code this is a public function. */
473 static int
474 feature_available (guestfs_h *g, const char *feature)
475 {
476   /* If there's an error we should ignore it, so to do that we have to
477    * temporarily replace the error handler with a null one.
478    */
479   guestfs_error_handler_cb old_error_cb = g->error_cb;
480   g->error_cb = NULL;
481
482   const char *groups[] = { feature, NULL };
483   int r = guestfs_available (g, (char * const *) groups);
484
485   g->error_cb = old_error_cb;
486
487   return r == 0 ? 1 : 0;
488 }
489
490 /* Find out if 'device' contains a filesystem.  If it does, add
491  * another entry in g->fses.
492  */
493 static int check_filesystem (guestfs_h *g, const char *device);
494 static int check_linux_root (guestfs_h *g, struct inspect_fs *fs);
495 static int check_fstab (guestfs_h *g, struct inspect_fs *fs);
496 static int check_windows_root (guestfs_h *g, struct inspect_fs *fs);
497 static int check_windows_arch (guestfs_h *g, struct inspect_fs *fs,
498                                const char *systemroot);
499 static int check_windows_registry (guestfs_h *g, struct inspect_fs *fs,
500                                    const char *systemroot);
501 static char *resolve_windows_path_silently (guestfs_h *g, const char *);
502 static int extend_fses (guestfs_h *g);
503 static int parse_unsigned_int (guestfs_h *g, const char *str);
504 static int add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
505                             const char *spec, const char *mp);
506 static char *resolve_fstab_device (guestfs_h *g, const char *spec);
507
508 static int
509 check_for_filesystem_on (guestfs_h *g, const char *device)
510 {
511   /* Get vfs-type in order to check if it's a Linux(?) swap device.
512    * If there's an error we should ignore it, so to do that we have to
513    * temporarily replace the error handler with a null one.
514    */
515   guestfs_error_handler_cb old_error_cb = g->error_cb;
516   g->error_cb = NULL;
517   char *vfs_type = guestfs_vfs_type (g, device);
518   g->error_cb = old_error_cb;
519
520   int is_swap = vfs_type && STREQ (vfs_type, "swap");
521
522   if (g->verbose)
523     fprintf (stderr, "check_for_filesystem_on: %s (%s)\n",
524              device, vfs_type ? vfs_type : "failed to get vfs type");
525
526   if (is_swap) {
527     free (vfs_type);
528     if (extend_fses (g) == -1)
529       return -1;
530     g->fses[g->nr_fses-1].is_swap = 1;
531     return 0;
532   }
533
534   /* Try mounting the device.  As above, ignore errors. */
535   g->error_cb = NULL;
536   int r = guestfs_mount_ro (g, device, "/");
537   if (r == -1 && vfs_type && STREQ (vfs_type, "ufs")) /* Hack for the *BSDs. */
538     r = guestfs_mount_vfs (g, "ro,ufstype=ufs2", "ufs", device, "/");
539   free (vfs_type);
540   g->error_cb = old_error_cb;
541   if (r == -1)
542     return 0;
543
544   /* Do the rest of the checks. */
545   r = check_filesystem (g, device);
546
547   /* Unmount the filesystem. */
548   if (guestfs_umount_all (g) == -1)
549     return -1;
550
551   return r;
552 }
553
554 static int
555 check_filesystem (guestfs_h *g, const char *device)
556 {
557   if (extend_fses (g) == -1)
558     return -1;
559
560   struct inspect_fs *fs = &g->fses[g->nr_fses-1];
561
562   fs->device = safe_strdup (g, device);
563   fs->is_mountable = 1;
564
565   /* Grub /boot? */
566   if (guestfs_is_file (g, "/grub/menu.lst") > 0 ||
567       guestfs_is_file (g, "/grub/grub.conf") > 0)
568     fs->content = FS_CONTENT_LINUX_BOOT;
569   /* Linux root? */
570   else if (guestfs_is_dir (g, "/etc") > 0 &&
571            guestfs_is_dir (g, "/bin") > 0 &&
572            guestfs_is_file (g, "/etc/fstab") > 0) {
573     fs->is_root = 1;
574     fs->content = FS_CONTENT_LINUX_ROOT;
575     if (check_linux_root (g, fs) == -1)
576       return -1;
577   }
578   /* Linux /usr/local? */
579   else if (guestfs_is_dir (g, "/etc") > 0 &&
580            guestfs_is_dir (g, "/bin") > 0 &&
581            guestfs_is_dir (g, "/share") > 0 &&
582            guestfs_exists (g, "/local") == 0 &&
583            guestfs_is_file (g, "/etc/fstab") == 0)
584     fs->content = FS_CONTENT_LINUX_USR_LOCAL;
585   /* Linux /usr? */
586   else if (guestfs_is_dir (g, "/etc") > 0 &&
587            guestfs_is_dir (g, "/bin") > 0 &&
588            guestfs_is_dir (g, "/share") > 0 &&
589            guestfs_exists (g, "/local") > 0 &&
590            guestfs_is_file (g, "/etc/fstab") == 0)
591     fs->content = FS_CONTENT_LINUX_USR;
592   /* Linux /var? */
593   else if (guestfs_is_dir (g, "/log") > 0 &&
594            guestfs_is_dir (g, "/run") > 0 &&
595            guestfs_is_dir (g, "/spool") > 0)
596     fs->content = FS_CONTENT_LINUX_VAR;
597   /* Windows root? */
598   else if (guestfs_is_file (g, "/AUTOEXEC.BAT") > 0 ||
599            guestfs_is_file (g, "/autoexec.bat") > 0 ||
600            guestfs_is_dir (g, "/Program Files") > 0 ||
601            guestfs_is_dir (g, "/WINDOWS") > 0 ||
602            guestfs_is_dir (g, "/Windows") > 0 ||
603            guestfs_is_dir (g, "/windows") > 0 ||
604            guestfs_is_dir (g, "/WIN32") > 0 ||
605            guestfs_is_dir (g, "/Win32") > 0 ||
606            guestfs_is_dir (g, "/WINNT") > 0 ||
607            guestfs_is_file (g, "/boot.ini") > 0 ||
608            guestfs_is_file (g, "/ntldr") > 0) {
609     fs->is_root = 1;
610     fs->content = FS_CONTENT_WINDOWS_ROOT;
611     if (check_windows_root (g, fs) == -1)
612       return -1;
613   }
614
615   return 0;
616 }
617
618 /* The currently mounted device is known to be a Linux root.  Try to
619  * determine from this the distro, version, etc.  Also parse
620  * /etc/fstab to determine the arrangement of mountpoints and
621  * associated devices.
622  */
623 static int
624 check_linux_root (guestfs_h *g, struct inspect_fs *fs)
625 {
626   fs->type = OS_TYPE_LINUX;
627
628   if (guestfs_exists (g, "/etc/redhat-release") > 0) {
629     fs->distro = OS_DISTRO_REDHAT_BASED; /* Something generic Red Hat-like. */
630
631     char **product_name = guestfs_head_n (g, 1, "/etc/redhat-release");
632     if (product_name == NULL)
633       return -1;
634     if (product_name[0] == NULL) {
635       error (g, "/etc/redhat-release file is empty");
636       free_string_list (product_name);
637       return -1;
638     }
639
640     /* Note that this string becomes owned by the handle and will
641      * be freed by guestfs___free_inspect_info.
642      */
643     fs->product_name = product_name[0];
644     free (product_name);
645
646     char *major, *minor;
647     if ((major = match1 (g, fs->product_name, re_fedora)) != NULL) {
648       fs->distro = OS_DISTRO_FEDORA;
649       fs->major_version = parse_unsigned_int (g, major);
650       free (major);
651       if (fs->major_version == -1)
652         return -1;
653     }
654     else if (match2 (g, fs->product_name, re_rhel_old, &major, &minor) ||
655              match2 (g, fs->product_name, re_rhel, &major, &minor)) {
656       fs->distro = OS_DISTRO_RHEL;
657       fs->major_version = parse_unsigned_int (g, major);
658       free (major);
659       if (fs->major_version == -1) {
660         free (minor);
661         return -1;
662       }
663       fs->minor_version = parse_unsigned_int (g, minor);
664       free (minor);
665       if (fs->minor_version == -1)
666         return -1;
667     }
668     else if ((major = match1 (g, fs->product_name, re_rhel_no_minor)) != NULL) {
669       fs->distro = OS_DISTRO_RHEL;
670       fs->major_version = parse_unsigned_int (g, major);
671       free (major);
672       if (fs->major_version == -1)
673         return -1;
674       fs->minor_version = 0;
675     }
676   }
677   else if (guestfs_exists (g, "/etc/debian_version") > 0) {
678     fs->distro = OS_DISTRO_DEBIAN;
679
680     char **product_name = guestfs_head_n (g, 1, "/etc/debian_version");
681     if (product_name == NULL)
682       return -1;
683     if (product_name[0] == NULL) {
684       error (g, "/etc/debian_version file is empty");
685       free_string_list (product_name);
686       return -1;
687     }
688
689     /* Note that this string becomes owned by the handle and will
690      * be freed by guestfs___free_inspect_info.
691      */
692     fs->product_name = product_name[0];
693     free (product_name);
694
695     char *major, *minor;
696     if (match2 (g, fs->product_name, re_debian, &major, &minor)) {
697       fs->major_version = parse_unsigned_int (g, major);
698       free (major);
699       if (fs->major_version == -1) {
700         free (minor);
701         return -1;
702       }
703       fs->minor_version = parse_unsigned_int (g, minor);
704       free (minor);
705       if (fs->minor_version == -1)
706         return -1;
707     }
708   }
709
710   /* Determine the architecture. */
711   const char *binaries[] =
712     { "/bin/bash", "/bin/ls", "/bin/echo", "/bin/rm", "/bin/sh" };
713   size_t i;
714   for (i = 0; i < sizeof binaries / sizeof binaries[0]; ++i) {
715     if (guestfs_is_file (g, binaries[i]) > 0) {
716       /* Ignore errors from file_architecture call. */
717       guestfs_error_handler_cb old_error_cb = g->error_cb;
718       g->error_cb = NULL;
719       char *arch = guestfs_file_architecture (g, binaries[i]);
720       g->error_cb = old_error_cb;
721
722       if (arch) {
723         /* String will be owned by handle, freed by
724          * guestfs___free_inspect_info.
725          */
726         fs->arch = arch;
727         break;
728       }
729     }
730   }
731
732   /* We already know /etc/fstab exists because it's part of the test
733    * for Linux root above.  We must now parse this file to determine
734    * which filesystems are used by the operating system and how they
735    * are mounted.
736    * XXX What if !feature_available (g, "augeas")?
737    */
738   if (guestfs_aug_init (g, "/", AUG_NO_LOAD|AUG_SAVE_NOOP) == -1)
739     return -1;
740
741   /* Tell Augeas to only load /etc/fstab (thanks Raphaël Pinson). */
742   guestfs_aug_rm (g, "/augeas/load//incl[. != \"/etc/fstab\"]");
743   guestfs_aug_load (g);
744
745   int r = check_fstab (g, fs);
746   guestfs_aug_close (g);
747   if (r == -1)
748     return -1;
749
750   return 0;
751 }
752
753 static int
754 check_fstab (guestfs_h *g, struct inspect_fs *fs)
755 {
756   char **lines = guestfs_aug_ls (g, "/files/etc/fstab");
757   if (lines == NULL)
758     return -1;
759
760   if (lines[0] == NULL) {
761     error (g, "could not parse /etc/fstab or empty file");
762     free_string_list (lines);
763     return -1;
764   }
765
766   size_t i;
767   char augpath[256];
768   for (i = 0; lines[i] != NULL; ++i) {
769     /* Ignore comments.  Only care about sequence lines which
770      * match m{/\d+$}.
771      */
772     if (match (g, lines[i], re_aug_seq)) {
773       snprintf (augpath, sizeof augpath, "%s/spec", lines[i]);
774       char *spec = guestfs_aug_get (g, augpath);
775       if (spec == NULL) {
776         free_string_list (lines);
777         return -1;
778       }
779
780       snprintf (augpath, sizeof augpath, "%s/file", lines[i]);
781       char *mp = guestfs_aug_get (g, augpath);
782       if (mp == NULL) {
783         free_string_list (lines);
784         free (spec);
785         return -1;
786       }
787
788       int r = add_fstab_entry (g, fs, spec, mp);
789       free (spec);
790       free (mp);
791
792       if (r == -1) {
793         free_string_list (lines);
794         return -1;
795       }
796     }
797   }
798
799   free_string_list (lines);
800   return 0;
801 }
802
803 /* Add a filesystem and possibly a mountpoint entry for
804  * the root filesystem 'fs'.
805  *
806  * 'spec' is the fstab spec field, which might be a device name or a
807  * pseudodevice or 'UUID=...' or 'LABEL=...'.
808  *
809  * 'mp' is the mount point, which could also be 'swap' or 'none'.
810  */
811 static int
812 add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
813                  const char *spec, const char *mp)
814 {
815   /* Ignore certain mountpoints. */
816   if (STRPREFIX (mp, "/dev/") ||
817       STREQ (mp, "/dev") ||
818       STRPREFIX (mp, "/media/") ||
819       STRPREFIX (mp, "/proc/") ||
820       STREQ (mp, "/proc") ||
821       STRPREFIX (mp, "/selinux/") ||
822       STREQ (mp, "/selinux") ||
823       STRPREFIX (mp, "/sys/") ||
824       STREQ (mp, "/sys"))
825     return 0;
826
827   /* Resolve UUID= and LABEL= to the actual device. */
828   char *device = NULL;
829   if (STRPREFIX (spec, "UUID="))
830     device = guestfs_findfs_uuid (g, &spec[5]);
831   else if (STRPREFIX (spec, "LABEL="))
832     device = guestfs_findfs_label (g, &spec[6]);
833   /* Resolve guest block device names. */
834   else if (spec[0] == '/')
835     device = resolve_fstab_device (g, spec);
836   /* Also ignore pseudo-devices completely, like spec == "tmpfs".
837    * If we haven't resolved the device successfully by this point,
838    * we don't care, just ignore it.
839    */
840   if (device == NULL)
841     return 0;
842
843   char *mountpoint = safe_strdup (g, mp);
844
845   /* Add this to the fstab entry in 'fs'.
846    * Note these are further filtered by guestfs_inspect_get_mountpoints
847    * and guestfs_inspect_get_filesystems.
848    */
849   size_t n = fs->nr_fstab + 1;
850   struct inspect_fstab_entry *p;
851
852   p = realloc (fs->fstab, n * sizeof (struct inspect_fstab_entry));
853   if (p == NULL) {
854     perrorf (g, "realloc");
855     free (device);
856     free (mountpoint);
857     return -1;
858   }
859
860   fs->fstab = p;
861   fs->nr_fstab = n;
862
863   /* These are owned by the handle and freed by guestfs___free_inspect_info. */
864   fs->fstab[n-1].device = device;
865   fs->fstab[n-1].mountpoint = mountpoint;
866
867   if (g->verbose)
868     fprintf (stderr, "fstab: device=%s mountpoint=%s\n", device, mountpoint);
869
870   return 0;
871 }
872
873 /* Resolve block device name to the libguestfs device name, eg.
874  * /dev/xvdb1 => /dev/vdb1.  This assumes that disks were added in the
875  * same order as they appear to the real VM, which is a reasonable
876  * assumption to make.  Return things like LV names unchanged (or
877  * anything we don't recognize).
878  */
879 static char *
880 resolve_fstab_device (guestfs_h *g, const char *spec)
881 {
882   char **devices = guestfs_list_devices (g);
883   if (devices == NULL)
884     return NULL;
885
886   size_t count;
887   for (count = 0; devices[count] != NULL; count++)
888     ;
889
890   char *device = NULL;
891   char *a1 = match1 (g, spec, re_xdev);
892   if (a1) {
893     size_t i = a1[0] - 'a'; /* a1[0] is always [a-z] because of regex. */
894     if (i < count) {
895       size_t len = strlen (devices[i]) + strlen (a1) + 16;
896       device = safe_malloc (g, len);
897       snprintf (device, len, "%s%s", devices[i], &a1[1]);
898     }
899   } else {
900     /* Didn't match device pattern, return original spec unchanged. */
901     device = safe_strdup (g, spec);
902   }
903
904   free (a1);
905   free_string_list (devices);
906
907   return device;
908 }
909
910 /* XXX Handling of boot.ini in the Perl version was pretty broken.  It
911  * essentially didn't do anything for modern Windows guests.
912  * Therefore I've omitted all that code.
913  */
914 static int
915 check_windows_root (guestfs_h *g, struct inspect_fs *fs)
916 {
917   fs->type = OS_TYPE_WINDOWS;
918   fs->distro = OS_DISTRO_WINDOWS;
919
920   /* Try to find Windows systemroot using some common locations. */
921   const char *systemroots[] =
922     { "/windows", "/winnt", "/win32", "/win" };
923   size_t i;
924   char *systemroot = NULL;
925   for (i = 0;
926        systemroot == NULL && i < sizeof systemroots / sizeof systemroots[0];
927        ++i) {
928     systemroot = resolve_windows_path_silently (g, systemroots[i]);
929   }
930
931   if (!systemroot) {
932     error (g, _("cannot resolve Windows %%SYSTEMROOT%%"));
933     return -1;
934   }
935
936   /* XXX There is a case for exposing systemroot and many variables
937    * from the registry through the libguestfs API.
938    */
939
940   if (g->verbose)
941     fprintf (stderr, "windows %%SYSTEMROOT%% = %s", systemroot);
942
943   if (check_windows_arch (g, fs, systemroot) == -1) {
944     free (systemroot);
945     return -1;
946   }
947
948   if (check_windows_registry (g, fs, systemroot) == -1) {
949     free (systemroot);
950     return -1;
951   }
952
953   free (systemroot);
954   return 0;
955 }
956
957 static int
958 check_windows_arch (guestfs_h *g, struct inspect_fs *fs,
959                     const char *systemroot)
960 {
961   size_t len = strlen (systemroot) + 32;
962   char cmd_exe[len];
963   snprintf (cmd_exe, len, "%s/system32/cmd.exe", systemroot);
964
965   char *cmd_exe_path = resolve_windows_path_silently (g, cmd_exe);
966   if (!cmd_exe_path)
967     return 0;
968
969   char *arch = guestfs_file_architecture (g, cmd_exe_path);
970   free (cmd_exe_path);
971
972   if (arch)
973     fs->arch = arch;        /* freed by guestfs___free_inspect_info */
974
975   return 0;
976 }
977
978 /* At the moment, pull just the ProductName and version numbers from
979  * the registry.  In future there is a case for making many more
980  * registry fields available to callers.
981  */
982 static int
983 check_windows_registry (guestfs_h *g, struct inspect_fs *fs,
984                         const char *systemroot)
985 {
986   size_t len = strlen (systemroot) + 64;
987   char software[len];
988   snprintf (software, len, "%s/system32/config/software", systemroot);
989
990   char *software_path = resolve_windows_path_silently (g, software);
991   if (!software_path)
992     /* If the software hive doesn't exist, just accept that we cannot
993      * find product_name etc.
994      */
995     return 0;
996
997   int ret = -1;
998   hive_h *h = NULL;
999   hive_value_h *values = NULL;
1000
1001   char dir[] = "/tmp/winreg.XXXXXX";
1002 #define dir_len 18
1003   if (mkdtemp (dir) == NULL) {
1004     perrorf (g, "mkdtemp");
1005     goto out;
1006   }
1007
1008   char software_hive[dir_len + 16];
1009   snprintf (software_hive, dir_len + 16, "%s/software", dir);
1010
1011   if (guestfs_download (g, software_path, software_hive) == -1)
1012     goto out;
1013
1014   h = hivex_open (software_hive, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
1015   if (h == NULL) {
1016     perrorf (g, "hivex_open");
1017     goto out;
1018   }
1019
1020   hive_node_h node = hivex_root (h);
1021   const char *hivepath[] =
1022     { "Microsoft", "Windows NT", "CurrentVersion" };
1023   size_t i;
1024   for (i = 0;
1025        node != 0 && i < sizeof hivepath / sizeof hivepath[0];
1026        ++i) {
1027     node = hivex_node_get_child (h, node, hivepath[i]);
1028   }
1029
1030   if (node == 0) {
1031     perrorf (g, "hivex: cannot locate HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
1032     goto out;
1033   }
1034
1035   values = hivex_node_values (h, node);
1036
1037   for (i = 0; values[i] != 0; ++i) {
1038     char *key = hivex_value_key (h, values[i]);
1039     if (key == NULL) {
1040       perrorf (g, "hivex_value_key");
1041       goto out;
1042     }
1043
1044     if (STRCASEEQ (key, "ProductName")) {
1045       fs->product_name = hivex_value_string (h, values[i]);
1046       if (!fs->product_name) {
1047         perrorf (g, "hivex_value_string");
1048         free (key);
1049         goto out;
1050       }
1051     }
1052     else if (STRCASEEQ (key, "CurrentVersion")) {
1053       char *version = hivex_value_string (h, values[i]);
1054       if (!version) {
1055         perrorf (g, "hivex_value_string");
1056         free (key);
1057         goto out;
1058       }
1059       char *major, *minor;
1060       if (match2 (g, version, re_windows_version, &major, &minor)) {
1061         fs->major_version = parse_unsigned_int (g, major);
1062         free (major);
1063         if (fs->major_version == -1) {
1064           free (minor);
1065           free (key);
1066           free (version);
1067           goto out;
1068         }
1069         fs->minor_version = parse_unsigned_int (g, minor);
1070         free (minor);
1071         if (fs->minor_version == -1) {
1072           free (key);
1073           free (version);
1074           return -1;
1075         }
1076       }
1077
1078       free (version);
1079     }
1080
1081     free (key);
1082   }
1083
1084   ret = 0;
1085
1086  out:
1087   if (h) hivex_close (h);
1088   free (values);
1089   free (software_path);
1090
1091   /* Free up the temporary directory.  Note the directory name cannot
1092    * contain shell meta-characters because of the way it was
1093    * constructed above.
1094    */
1095   char cmd[dir_len + 16];
1096   snprintf (cmd, dir_len + 16, "rm -rf %s", dir);
1097   ignore_value (system (cmd));
1098 #undef dir_len
1099
1100   return ret;
1101 }
1102
1103 static char *
1104 resolve_windows_path_silently (guestfs_h *g, const char *path)
1105 {
1106   guestfs_error_handler_cb old_error_cb = g->error_cb;
1107   g->error_cb = NULL;
1108   char *ret = guestfs_case_sensitive_path (g, path);
1109   g->error_cb = old_error_cb;
1110   return ret;
1111 }
1112
1113 static int
1114 extend_fses (guestfs_h *g)
1115 {
1116   size_t n = g->nr_fses + 1;
1117   struct inspect_fs *p;
1118
1119   p = realloc (g->fses, n * sizeof (struct inspect_fs));
1120   if (p == NULL) {
1121     perrorf (g, "realloc");
1122     return -1;
1123   }
1124
1125   g->fses = p;
1126   g->nr_fses = n;
1127
1128   memset (&g->fses[n-1], 0, sizeof (struct inspect_fs));
1129
1130   return 0;
1131 }
1132
1133 /* Parse small, unsigned ints, as used in version numbers. */
1134 static int
1135 parse_unsigned_int (guestfs_h *g, const char *str)
1136 {
1137   long ret;
1138   int r = xstrtol (str, NULL, 10, &ret, "");
1139   if (r != LONGINT_OK) {
1140     error (g, "could not parse integer in version number: %s", str);
1141     return -1;
1142   }
1143   return ret;
1144 }
1145
1146 static struct inspect_fs *
1147 search_for_root (guestfs_h *g, const char *root)
1148 {
1149   if (g->nr_fses == 0) {
1150     error (g, _("no inspection data: call guestfs_inspect_os first"));
1151     return NULL;
1152   }
1153
1154   size_t i;
1155   struct inspect_fs *fs;
1156   for (i = 0; i < g->nr_fses; ++i) {
1157     fs = &g->fses[i];
1158     if (fs->is_root && STREQ (root, fs->device))
1159       return fs;
1160   }
1161
1162   error (g, _("%s: root device not found: only call this function with a root device previously returned by guestfs_inspect_os"),
1163          root);
1164   return NULL;
1165 }
1166
1167 char *
1168 guestfs__inspect_get_type (guestfs_h *g, const char *root)
1169 {
1170   struct inspect_fs *fs = search_for_root (g, root);
1171   if (!fs)
1172     return NULL;
1173
1174   char *ret;
1175   switch (fs->type) {
1176   case OS_TYPE_LINUX: ret = safe_strdup (g, "linux"); break;
1177   case OS_TYPE_WINDOWS: ret = safe_strdup (g, "windows"); break;
1178   case OS_TYPE_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1179   }
1180
1181   return ret;
1182 }
1183
1184 char *
1185 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
1186 {
1187   struct inspect_fs *fs = search_for_root (g, root);
1188   if (!fs)
1189     return NULL;
1190
1191   return safe_strdup (g, fs->arch ? : "unknown");
1192 }
1193
1194 char *
1195 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
1196 {
1197   struct inspect_fs *fs = search_for_root (g, root);
1198   if (!fs)
1199     return NULL;
1200
1201   char *ret;
1202   switch (fs->distro) {
1203   case OS_DISTRO_DEBIAN: ret = safe_strdup (g, "debian"); break;
1204   case OS_DISTRO_FEDORA: ret = safe_strdup (g, "fedora"); break;
1205   case OS_DISTRO_REDHAT_BASED: ret = safe_strdup (g, "redhat-based"); break;
1206   case OS_DISTRO_RHEL: ret = safe_strdup (g, "rhel"); break;
1207   case OS_DISTRO_WINDOWS: ret = safe_strdup (g, "windows"); break;
1208   case OS_DISTRO_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1209   }
1210
1211   return ret;
1212 }
1213
1214 int
1215 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
1216 {
1217   struct inspect_fs *fs = search_for_root (g, root);
1218   if (!fs)
1219     return -1;
1220
1221   return fs->major_version;
1222 }
1223
1224 int
1225 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
1226 {
1227   struct inspect_fs *fs = search_for_root (g, root);
1228   if (!fs)
1229     return -1;
1230
1231   return fs->minor_version;
1232 }
1233
1234 char *
1235 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
1236 {
1237   struct inspect_fs *fs = search_for_root (g, root);
1238   if (!fs)
1239     return NULL;
1240
1241   return safe_strdup (g, fs->product_name ? : "unknown");
1242 }
1243
1244 char **
1245 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
1246 {
1247   struct inspect_fs *fs = search_for_root (g, root);
1248   if (!fs)
1249     return NULL;
1250
1251   char **ret;
1252
1253   /* If no fstab information (Windows) return just the root. */
1254   if (fs->nr_fstab == 0) {
1255     ret = calloc (3, sizeof (char *));
1256     ret[0] = safe_strdup (g, "/");
1257     ret[1] = safe_strdup (g, root);
1258     ret[2] = NULL;
1259     return ret;
1260   }
1261
1262 #define CRITERION fs->fstab[i].mountpoint[0] == '/'
1263   size_t i, count = 0;
1264   for (i = 0; i < fs->nr_fstab; ++i)
1265     if (CRITERION)
1266       count++;
1267
1268   /* Hashtables have 2N+1 entries. */
1269   ret = calloc (2*count+1, sizeof (char *));
1270   if (ret == NULL) {
1271     perrorf (g, "calloc");
1272     return NULL;
1273   }
1274
1275   count = 0;
1276   for (i = 0; i < fs->nr_fstab; ++i)
1277     if (CRITERION) {
1278       ret[2*count] = safe_strdup (g, fs->fstab[i].mountpoint);
1279       ret[2*count+1] = safe_strdup (g, fs->fstab[i].device);
1280       count++;
1281     }
1282 #undef CRITERION
1283
1284   return ret;
1285 }
1286
1287 char **
1288 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
1289 {
1290   struct inspect_fs *fs = search_for_root (g, root);
1291   if (!fs)
1292     return NULL;
1293
1294   char **ret;
1295
1296   /* If no fstab information (Windows) return just the root. */
1297   if (fs->nr_fstab == 0) {
1298     ret = calloc (2, sizeof (char *));
1299     ret[0] = safe_strdup (g, root);
1300     ret[1] = NULL;
1301     return ret;
1302   }
1303
1304   ret = calloc (fs->nr_fstab + 1, sizeof (char *));
1305   if (ret == NULL) {
1306     perrorf (g, "calloc");
1307     return NULL;
1308   }
1309
1310   size_t i;
1311   for (i = 0; i < fs->nr_fstab; ++i)
1312     ret[i] = safe_strdup (g, fs->fstab[i].device);
1313
1314   return ret;
1315 }
1316
1317 /* List filesystems.
1318  *
1319  * The current implementation just uses guestfs_vfs_type and doesn't
1320  * try mounting anything, but we reserve the right in future to try
1321  * mounting filesystems.
1322  */
1323
1324 static void remove_from_list (char **list, const char *item);
1325 static void check_with_vfs_type (guestfs_h *g, const char *dev, char ***ret, size_t *ret_size);
1326
1327 char **
1328 guestfs__list_filesystems (guestfs_h *g)
1329 {
1330   size_t i;
1331   char **ret;
1332   size_t ret_size;
1333
1334   ret = safe_malloc (g, sizeof (char *));
1335   ret[0] = NULL;
1336   ret_size = 0;
1337
1338   /* Look to see if any devices directly contain filesystems
1339    * (RHBZ#590167).  However vfs-type will fail to tell us anything
1340    * useful about devices which just contain partitions, so we also
1341    * get the list of partitions and exclude the corresponding devices
1342    * by using part-to-dev.
1343    */
1344   char **devices;
1345   devices = guestfs_list_devices (g);
1346   if (devices == NULL) {
1347     free_string_list (ret);
1348     return NULL;
1349   }
1350   char **partitions;
1351   partitions = guestfs_list_partitions (g);
1352   if (partitions == NULL) {
1353     free_string_list (devices);
1354     free_string_list (ret);
1355     return NULL;
1356   }
1357
1358   for (i = 0; partitions[i] != NULL; ++i) {
1359     char *dev = guestfs_part_to_dev (g, partitions[i]);
1360     if (dev)
1361       remove_from_list (devices, dev);
1362     free (dev);
1363   }
1364
1365   /* Use vfs-type to check for filesystems on devices. */
1366   for (i = 0; devices[i] != NULL; ++i)
1367     check_with_vfs_type (g, devices[i], &ret, &ret_size);
1368   free_string_list (devices);
1369
1370   /* Use vfs-type to check for filesystems on partitions. */
1371   for (i = 0; partitions[i] != NULL; ++i)
1372     check_with_vfs_type (g, partitions[i], &ret, &ret_size);
1373   free_string_list (partitions);
1374
1375   if (feature_available (g, "lvm2")) {
1376     /* Use vfs-type to check for filesystems on LVs. */
1377     char **lvs;
1378     lvs = guestfs_lvs (g);
1379     if (lvs == NULL) {
1380       free_string_list (ret);
1381       return NULL;
1382     }
1383
1384     for (i = 0; lvs[i] != NULL; ++i)
1385       check_with_vfs_type (g, lvs[i], &ret, &ret_size);
1386     free_string_list (lvs);
1387   }
1388
1389   return ret;
1390 }
1391
1392 /* If 'item' occurs in 'list', remove and free it. */
1393 static void
1394 remove_from_list (char **list, const char *item)
1395 {
1396   size_t i;
1397
1398   for (i = 0; list[i] != NULL; ++i)
1399     if (STREQ (list[i], item)) {
1400       free (list[i]);
1401       for (; list[i+1] != NULL; ++i)
1402         list[i] = list[i+1];
1403       list[i] = NULL;
1404       return;
1405     }
1406 }
1407
1408 /* Use vfs-type to look for a filesystem of some sort on 'dev'.
1409  * Apart from some types which we ignore, add the result to the
1410  * 'ret' string list.
1411  */
1412 static void
1413 check_with_vfs_type (guestfs_h *g, const char *device,
1414                      char ***ret, size_t *ret_size)
1415 {
1416   char *v;
1417
1418   guestfs_error_handler_cb old_error_cb = g->error_cb;
1419   g->error_cb = NULL;
1420   char *vfs_type = guestfs_vfs_type (g, device);
1421   g->error_cb = old_error_cb;
1422
1423   if (!vfs_type)
1424     v = safe_strdup (g, "unknown");
1425   else {
1426     /* Ignore all "*_member" strings.  In libblkid these are returned
1427      * for things which are members of some RAID or LVM set, most
1428      * importantly "LVM2_member" which is a PV.
1429      */
1430     size_t n = strlen (vfs_type);
1431     if (n >= 7 && STREQ (&vfs_type[n-7], "_member")) {
1432       free (vfs_type);
1433       return;
1434     }
1435
1436     /* Ignore LUKS-encrypted partitions.  These are also containers. */
1437     if (STREQ (vfs_type, "crypto_LUKS")) {
1438       free (vfs_type);
1439       return;
1440     }
1441
1442     v = vfs_type;
1443   }
1444
1445   /* Extend the return array. */
1446   size_t i = *ret_size;
1447   *ret_size += 2;
1448   *ret = safe_realloc (g, *ret, (*ret_size + 1) * sizeof (char *));
1449   (*ret)[i] = safe_strdup (g, device);
1450   (*ret)[i+1] = v;
1451   (*ret)[i+2] = NULL;
1452 }