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