Don't use kernel module whitelist with ext2-based appliance.
[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   TMP_TEMPLATE_ON_STACK (dir);
231 #define dir_len (strlen (dir))
232 #define initrd_len (dir_len + 16)
233   char initrd[initrd_len];
234 #define cmd_len (dir_len + 256)
235   char cmd[cmd_len];
236 #define bin_len (dir_len + 32)
237   char bin[bin_len];
238
239   char *ret = NULL;
240
241   const char *method;
242   if (strstr (file, "gzip"))
243     method = "zcat";
244   else if (strstr (file, "bzip2"))
245     method = "bzcat";
246   else
247     method = "cat";
248
249   if (mkdtemp (dir) == NULL) {
250     perrorf (g, "mkdtemp");
251     goto out;
252   }
253
254   snprintf (initrd, initrd_len, "%s/initrd", dir);
255   if (guestfs_download (g, path, initrd) == -1)
256     goto out;
257
258   snprintf (cmd, cmd_len,
259             "cd %s && %s initrd | cpio --quiet -id " INITRD_BINARIES1,
260             dir, method);
261   int r = system (cmd);
262   if (r == -1 || WEXITSTATUS (r) != 0) {
263     perrorf (g, "cpio command failed");
264     goto out;
265   }
266
267   const char *bins[] = INITRD_BINARIES2;
268   size_t i;
269   for (i = 0; i < sizeof bins / sizeof bins[0]; ++i) {
270     snprintf (bin, bin_len, "%s/%s", dir, bins[i]);
271
272     if (is_regular_file (bin)) {
273       int flags = g->verbose ? MAGIC_DEBUG : 0;
274       flags |= MAGIC_ERROR | MAGIC_RAW;
275
276       magic_t m = magic_open (flags);
277       if (m == NULL) {
278         perrorf (g, "magic_open");
279         goto out;
280       }
281
282       if (magic_load (m, NULL) == -1) {
283         perrorf (g, "magic_load: default magic database file");
284         magic_close (m);
285         goto out;
286       }
287
288       const char *line = magic_file (m, bin);
289       if (line == NULL) {
290         perrorf (g, "magic_file: %s", bin);
291         magic_close (m);
292         goto out;
293       }
294
295       char *elf_arch;
296       if ((elf_arch = match1 (g, line, re_file_elf)) != NULL) {
297         ret = canonical_elf_arch (g, elf_arch);
298         free (elf_arch);
299         magic_close (m);
300         goto out;
301       }
302       magic_close (m);
303     }
304   }
305   error (g, "file_architecture: could not determine architecture of cpio archive");
306
307  out:
308   /* Free up the temporary directory.  Note the directory name cannot
309    * contain shell meta-characters because of the way it was
310    * constructed above.
311    */
312   snprintf (cmd, cmd_len, "rm -rf %s", dir);
313   ignore_value (system (cmd));
314
315   return ret;
316 #undef dir_len
317 #undef initrd_len
318 #undef cmd_len
319 #undef bin_len
320 }
321
322 char *
323 guestfs__file_architecture (guestfs_h *g, const char *path)
324 {
325   char *file = NULL;
326   char *elf_arch = NULL;
327   char *ret = NULL;
328
329   /* Get the output of the "file" command.  Note that because this
330    * runs in the daemon, LANG=C so it's in English.
331    */
332   file = guestfs_file (g, path);
333   if (file == NULL)
334     return NULL;
335
336   if ((elf_arch = match1 (g, file, re_file_elf)) != NULL)
337     ret = canonical_elf_arch (g, elf_arch);
338   else if (strstr (file, "PE32 executable"))
339     ret = safe_strdup (g, "i386");
340   else if (strstr (file, "PE32+ executable"))
341     ret = safe_strdup (g, "x86_64");
342   else if (strstr (file, "cpio archive"))
343     ret = cpio_arch (g, file, path);
344   else
345     error (g, "file_architecture: unknown architecture: %s", path);
346
347   free (file);
348   free (elf_arch);
349   return ret;                   /* caller frees */
350 }
351
352 /* The main inspection code. */
353 static int feature_available (guestfs_h *g, const char *feature);
354 static void free_string_list (char **);
355 static int check_for_filesystem_on (guestfs_h *g, const char *device);
356
357 char **
358 guestfs__inspect_os (guestfs_h *g)
359 {
360   /* Remove any information previously stored in the handle. */
361   guestfs___free_inspect_info (g);
362
363   if (guestfs_umount_all (g) == -1)
364     return NULL;
365
366   /* Iterate over all possible devices.  Try to mount each
367    * (read-only).  Examine ones which contain filesystems and add that
368    * information to the handle.
369    */
370   /* Look to see if any devices directly contain filesystems (RHBZ#590167). */
371   char **devices;
372   devices = guestfs_list_devices (g);
373   if (devices == NULL)
374     return NULL;
375
376   size_t i;
377   for (i = 0; devices[i] != NULL; ++i) {
378     if (check_for_filesystem_on (g, devices[i]) == -1) {
379       free_string_list (devices);
380       guestfs___free_inspect_info (g);
381       return NULL;
382     }
383   }
384   free_string_list (devices);
385
386   /* Look at all partitions. */
387   char **partitions;
388   partitions = guestfs_list_partitions (g);
389   if (partitions == NULL) {
390     guestfs___free_inspect_info (g);
391     return NULL;
392   }
393
394   for (i = 0; partitions[i] != NULL; ++i) {
395     if (check_for_filesystem_on (g, partitions[i]) == -1) {
396       free_string_list (partitions);
397       guestfs___free_inspect_info (g);
398       return NULL;
399     }
400   }
401   free_string_list (partitions);
402
403   /* Look at all LVs. */
404   if (feature_available (g, "lvm2")) {
405     char **lvs;
406     lvs = guestfs_lvs (g);
407     if (lvs == NULL) {
408       guestfs___free_inspect_info (g);
409       return NULL;
410     }
411
412     for (i = 0; lvs[i] != NULL; ++i) {
413       if (check_for_filesystem_on (g, lvs[i]) == -1) {
414         free_string_list (lvs);
415         guestfs___free_inspect_info (g);
416         return NULL;
417       }
418     }
419     free_string_list (lvs);
420   }
421
422   /* At this point we have, in the handle, a list of all filesystems
423    * found and data about each one.  Now we assemble the list of
424    * filesystems which are root devices and return that to the user.
425    */
426   size_t count = 0;
427   for (i = 0; i < g->nr_fses; ++i)
428     if (g->fses[i].is_root)
429       count++;
430
431   char **ret = calloc (count+1, sizeof (char *));
432   if (ret == NULL) {
433     perrorf (g, "calloc");
434     guestfs___free_inspect_info (g);
435     return NULL;
436   }
437
438   count = 0;
439   for (i = 0; i < g->nr_fses; ++i) {
440     if (g->fses[i].is_root) {
441       ret[count] = safe_strdup (g, g->fses[i].device);
442       count++;
443     }
444   }
445   ret[count] = NULL;
446
447   return ret;
448 }
449
450 void
451 guestfs___free_inspect_info (guestfs_h *g)
452 {
453   size_t i;
454   for (i = 0; i < g->nr_fses; ++i) {
455     free (g->fses[i].device);
456     free (g->fses[i].product_name);
457     free (g->fses[i].arch);
458     size_t j;
459     for (j = 0; j < g->fses[i].nr_fstab; ++j) {
460       free (g->fses[i].fstab[j].device);
461       free (g->fses[i].fstab[j].mountpoint);
462     }
463     free (g->fses[i].fstab);
464   }
465   free (g->fses);
466   g->nr_fses = 0;
467   g->fses = NULL;
468 }
469
470 static void
471 free_string_list (char **argv)
472 {
473   size_t i;
474   for (i = 0; argv[i] != NULL; ++i)
475     free (argv[i]);
476   free (argv);
477 }
478
479 /* In the Perl code this is a public function. */
480 static int
481 feature_available (guestfs_h *g, const char *feature)
482 {
483   /* If there's an error we should ignore it, so to do that we have to
484    * temporarily replace the error handler with a null one.
485    */
486   guestfs_error_handler_cb old_error_cb = g->error_cb;
487   g->error_cb = NULL;
488
489   const char *groups[] = { feature, NULL };
490   int r = guestfs_available (g, (char * const *) groups);
491
492   g->error_cb = old_error_cb;
493
494   return r == 0 ? 1 : 0;
495 }
496
497 /* Find out if 'device' contains a filesystem.  If it does, add
498  * another entry in g->fses.
499  */
500 static int check_filesystem (guestfs_h *g, const char *device);
501 static int check_linux_root (guestfs_h *g, struct inspect_fs *fs);
502 static int check_fstab (guestfs_h *g, struct inspect_fs *fs);
503 static int check_windows_root (guestfs_h *g, struct inspect_fs *fs);
504 static int check_windows_arch (guestfs_h *g, struct inspect_fs *fs,
505                                const char *systemroot);
506 static int check_windows_registry (guestfs_h *g, struct inspect_fs *fs,
507                                    const char *systemroot);
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   /* Resolve UUID= and LABEL= to the actual device. */
835   char *device = NULL;
836   if (STRPREFIX (spec, "UUID="))
837     device = guestfs_findfs_uuid (g, &spec[5]);
838   else if (STRPREFIX (spec, "LABEL="))
839     device = guestfs_findfs_label (g, &spec[6]);
840   /* Resolve guest block device names. */
841   else if (spec[0] == '/')
842     device = resolve_fstab_device (g, spec);
843   /* Also ignore pseudo-devices completely, like spec == "tmpfs".
844    * If we haven't resolved the device successfully by this point,
845    * we don't care, just ignore it.
846    */
847   if (device == NULL)
848     return 0;
849
850   char *mountpoint = safe_strdup (g, mp);
851
852   /* Add this to the fstab entry in 'fs'.
853    * Note these are further filtered by guestfs_inspect_get_mountpoints
854    * and guestfs_inspect_get_filesystems.
855    */
856   size_t n = fs->nr_fstab + 1;
857   struct inspect_fstab_entry *p;
858
859   p = realloc (fs->fstab, n * sizeof (struct inspect_fstab_entry));
860   if (p == NULL) {
861     perrorf (g, "realloc");
862     free (device);
863     free (mountpoint);
864     return -1;
865   }
866
867   fs->fstab = p;
868   fs->nr_fstab = n;
869
870   /* These are owned by the handle and freed by guestfs___free_inspect_info. */
871   fs->fstab[n-1].device = device;
872   fs->fstab[n-1].mountpoint = mountpoint;
873
874   if (g->verbose)
875     fprintf (stderr, "fstab: device=%s mountpoint=%s\n", device, mountpoint);
876
877   return 0;
878 }
879
880 /* Resolve block device name to the libguestfs device name, eg.
881  * /dev/xvdb1 => /dev/vdb1.  This assumes that disks were added in the
882  * same order as they appear to the real VM, which is a reasonable
883  * assumption to make.  Return things like LV names unchanged (or
884  * anything we don't recognize).
885  */
886 static char *
887 resolve_fstab_device (guestfs_h *g, const char *spec)
888 {
889   char **devices = guestfs_list_devices (g);
890   if (devices == NULL)
891     return NULL;
892
893   size_t count;
894   for (count = 0; devices[count] != NULL; count++)
895     ;
896
897   char *device = NULL;
898   char *a1 = match1 (g, spec, re_xdev);
899   if (a1) {
900     size_t i = a1[0] - 'a'; /* a1[0] is always [a-z] because of regex. */
901     if (i < count) {
902       size_t len = strlen (devices[i]) + strlen (a1) + 16;
903       device = safe_malloc (g, len);
904       snprintf (device, len, "%s%s", devices[i], &a1[1]);
905     }
906   } else {
907     /* Didn't match device pattern, return original spec unchanged. */
908     device = safe_strdup (g, spec);
909   }
910
911   free (a1);
912   free_string_list (devices);
913
914   return device;
915 }
916
917 /* XXX Handling of boot.ini in the Perl version was pretty broken.  It
918  * essentially didn't do anything for modern Windows guests.
919  * Therefore I've omitted all that code.
920  */
921 static int
922 check_windows_root (guestfs_h *g, struct inspect_fs *fs)
923 {
924   fs->type = OS_TYPE_WINDOWS;
925   fs->distro = OS_DISTRO_WINDOWS;
926
927   /* Try to find Windows systemroot using some common locations. */
928   const char *systemroots[] =
929     { "/windows", "/winnt", "/win32", "/win" };
930   size_t i;
931   char *systemroot = NULL;
932   for (i = 0;
933        systemroot == NULL && i < sizeof systemroots / sizeof systemroots[0];
934        ++i) {
935     systemroot = resolve_windows_path_silently (g, systemroots[i]);
936   }
937
938   if (!systemroot) {
939     error (g, _("cannot resolve Windows %%SYSTEMROOT%%"));
940     return -1;
941   }
942
943   /* XXX There is a case for exposing systemroot and many variables
944    * from the registry through the libguestfs API.
945    */
946
947   if (g->verbose)
948     fprintf (stderr, "windows %%SYSTEMROOT%% = %s", systemroot);
949
950   if (check_windows_arch (g, fs, systemroot) == -1) {
951     free (systemroot);
952     return -1;
953   }
954
955   if (check_windows_registry (g, fs, systemroot) == -1) {
956     free (systemroot);
957     return -1;
958   }
959
960   free (systemroot);
961   return 0;
962 }
963
964 static int
965 check_windows_arch (guestfs_h *g, struct inspect_fs *fs,
966                     const char *systemroot)
967 {
968   size_t len = strlen (systemroot) + 32;
969   char cmd_exe[len];
970   snprintf (cmd_exe, len, "%s/system32/cmd.exe", systemroot);
971
972   char *cmd_exe_path = resolve_windows_path_silently (g, cmd_exe);
973   if (!cmd_exe_path)
974     return 0;
975
976   char *arch = guestfs_file_architecture (g, cmd_exe_path);
977   free (cmd_exe_path);
978
979   if (arch)
980     fs->arch = arch;        /* freed by guestfs___free_inspect_info */
981
982   return 0;
983 }
984
985 /* At the moment, pull just the ProductName and version numbers from
986  * the registry.  In future there is a case for making many more
987  * registry fields available to callers.
988  */
989 static int
990 check_windows_registry (guestfs_h *g, struct inspect_fs *fs,
991                         const char *systemroot)
992 {
993   TMP_TEMPLATE_ON_STACK (dir);
994 #define dir_len (strlen (dir))
995 #define software_hive_len (dir_len + 16)
996   char software_hive[software_hive_len];
997 #define cmd_len (dir_len + 16)
998   char cmd[cmd_len];
999
1000   size_t len = strlen (systemroot) + 64;
1001   char software[len];
1002   snprintf (software, len, "%s/system32/config/software", systemroot);
1003
1004   char *software_path = resolve_windows_path_silently (g, software);
1005   if (!software_path)
1006     /* If the software hive doesn't exist, just accept that we cannot
1007      * find product_name etc.
1008      */
1009     return 0;
1010
1011   int ret = -1;
1012   hive_h *h = NULL;
1013   hive_value_h *values = NULL;
1014
1015   if (mkdtemp (dir) == NULL) {
1016     perrorf (g, "mkdtemp");
1017     goto out;
1018   }
1019
1020   snprintf (software_hive, software_hive_len, "%s/software", dir);
1021
1022   if (guestfs_download (g, software_path, software_hive) == -1)
1023     goto out;
1024
1025   h = hivex_open (software_hive, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
1026   if (h == NULL) {
1027     perrorf (g, "hivex_open");
1028     goto out;
1029   }
1030
1031   hive_node_h node = hivex_root (h);
1032   const char *hivepath[] =
1033     { "Microsoft", "Windows NT", "CurrentVersion" };
1034   size_t i;
1035   for (i = 0;
1036        node != 0 && i < sizeof hivepath / sizeof hivepath[0];
1037        ++i) {
1038     node = hivex_node_get_child (h, node, hivepath[i]);
1039   }
1040
1041   if (node == 0) {
1042     perrorf (g, "hivex: cannot locate HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
1043     goto out;
1044   }
1045
1046   values = hivex_node_values (h, node);
1047
1048   for (i = 0; values[i] != 0; ++i) {
1049     char *key = hivex_value_key (h, values[i]);
1050     if (key == NULL) {
1051       perrorf (g, "hivex_value_key");
1052       goto out;
1053     }
1054
1055     if (STRCASEEQ (key, "ProductName")) {
1056       fs->product_name = hivex_value_string (h, values[i]);
1057       if (!fs->product_name) {
1058         perrorf (g, "hivex_value_string");
1059         free (key);
1060         goto out;
1061       }
1062     }
1063     else if (STRCASEEQ (key, "CurrentVersion")) {
1064       char *version = hivex_value_string (h, values[i]);
1065       if (!version) {
1066         perrorf (g, "hivex_value_string");
1067         free (key);
1068         goto out;
1069       }
1070       char *major, *minor;
1071       if (match2 (g, version, re_windows_version, &major, &minor)) {
1072         fs->major_version = parse_unsigned_int (g, major);
1073         free (major);
1074         if (fs->major_version == -1) {
1075           free (minor);
1076           free (key);
1077           free (version);
1078           goto out;
1079         }
1080         fs->minor_version = parse_unsigned_int (g, minor);
1081         free (minor);
1082         if (fs->minor_version == -1) {
1083           free (key);
1084           free (version);
1085           return -1;
1086         }
1087       }
1088
1089       free (version);
1090     }
1091
1092     free (key);
1093   }
1094
1095   ret = 0;
1096
1097  out:
1098   if (h) hivex_close (h);
1099   free (values);
1100   free (software_path);
1101
1102   /* Free up the temporary directory.  Note the directory name cannot
1103    * contain shell meta-characters because of the way it was
1104    * constructed above.
1105    */
1106   snprintf (cmd, cmd_len, "rm -rf %s", dir);
1107   ignore_value (system (cmd));
1108 #undef dir_len
1109 #undef software_hive_len
1110 #undef cmd_len
1111
1112   return ret;
1113 }
1114
1115 static char *
1116 resolve_windows_path_silently (guestfs_h *g, const char *path)
1117 {
1118   guestfs_error_handler_cb old_error_cb = g->error_cb;
1119   g->error_cb = NULL;
1120   char *ret = guestfs_case_sensitive_path (g, path);
1121   g->error_cb = old_error_cb;
1122   return ret;
1123 }
1124
1125 static int
1126 extend_fses (guestfs_h *g)
1127 {
1128   size_t n = g->nr_fses + 1;
1129   struct inspect_fs *p;
1130
1131   p = realloc (g->fses, n * sizeof (struct inspect_fs));
1132   if (p == NULL) {
1133     perrorf (g, "realloc");
1134     return -1;
1135   }
1136
1137   g->fses = p;
1138   g->nr_fses = n;
1139
1140   memset (&g->fses[n-1], 0, sizeof (struct inspect_fs));
1141
1142   return 0;
1143 }
1144
1145 /* Parse small, unsigned ints, as used in version numbers. */
1146 static int
1147 parse_unsigned_int (guestfs_h *g, const char *str)
1148 {
1149   long ret;
1150   int r = xstrtol (str, NULL, 10, &ret, "");
1151   if (r != LONGINT_OK) {
1152     error (g, "could not parse integer in version number: %s", str);
1153     return -1;
1154   }
1155   return ret;
1156 }
1157
1158 static struct inspect_fs *
1159 search_for_root (guestfs_h *g, const char *root)
1160 {
1161   if (g->nr_fses == 0) {
1162     error (g, _("no inspection data: call guestfs_inspect_os first"));
1163     return NULL;
1164   }
1165
1166   size_t i;
1167   struct inspect_fs *fs;
1168   for (i = 0; i < g->nr_fses; ++i) {
1169     fs = &g->fses[i];
1170     if (fs->is_root && STREQ (root, fs->device))
1171       return fs;
1172   }
1173
1174   error (g, _("%s: root device not found: only call this function with a root device previously returned by guestfs_inspect_os"),
1175          root);
1176   return NULL;
1177 }
1178
1179 char *
1180 guestfs__inspect_get_type (guestfs_h *g, const char *root)
1181 {
1182   struct inspect_fs *fs = search_for_root (g, root);
1183   if (!fs)
1184     return NULL;
1185
1186   char *ret;
1187   switch (fs->type) {
1188   case OS_TYPE_LINUX: ret = safe_strdup (g, "linux"); break;
1189   case OS_TYPE_WINDOWS: ret = safe_strdup (g, "windows"); break;
1190   case OS_TYPE_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1191   }
1192
1193   return ret;
1194 }
1195
1196 char *
1197 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
1198 {
1199   struct inspect_fs *fs = search_for_root (g, root);
1200   if (!fs)
1201     return NULL;
1202
1203   return safe_strdup (g, fs->arch ? : "unknown");
1204 }
1205
1206 char *
1207 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
1208 {
1209   struct inspect_fs *fs = search_for_root (g, root);
1210   if (!fs)
1211     return NULL;
1212
1213   char *ret;
1214   switch (fs->distro) {
1215   case OS_DISTRO_DEBIAN: ret = safe_strdup (g, "debian"); break;
1216   case OS_DISTRO_FEDORA: ret = safe_strdup (g, "fedora"); break;
1217   case OS_DISTRO_REDHAT_BASED: ret = safe_strdup (g, "redhat-based"); break;
1218   case OS_DISTRO_RHEL: ret = safe_strdup (g, "rhel"); break;
1219   case OS_DISTRO_WINDOWS: ret = safe_strdup (g, "windows"); break;
1220   case OS_DISTRO_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1221   }
1222
1223   return ret;
1224 }
1225
1226 int
1227 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
1228 {
1229   struct inspect_fs *fs = search_for_root (g, root);
1230   if (!fs)
1231     return -1;
1232
1233   return fs->major_version;
1234 }
1235
1236 int
1237 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
1238 {
1239   struct inspect_fs *fs = search_for_root (g, root);
1240   if (!fs)
1241     return -1;
1242
1243   return fs->minor_version;
1244 }
1245
1246 char *
1247 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
1248 {
1249   struct inspect_fs *fs = search_for_root (g, root);
1250   if (!fs)
1251     return NULL;
1252
1253   return safe_strdup (g, fs->product_name ? : "unknown");
1254 }
1255
1256 char **
1257 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
1258 {
1259   struct inspect_fs *fs = search_for_root (g, root);
1260   if (!fs)
1261     return NULL;
1262
1263   char **ret;
1264
1265   /* If no fstab information (Windows) return just the root. */
1266   if (fs->nr_fstab == 0) {
1267     ret = calloc (3, sizeof (char *));
1268     ret[0] = safe_strdup (g, "/");
1269     ret[1] = safe_strdup (g, root);
1270     ret[2] = NULL;
1271     return ret;
1272   }
1273
1274 #define CRITERION fs->fstab[i].mountpoint[0] == '/'
1275   size_t i, count = 0;
1276   for (i = 0; i < fs->nr_fstab; ++i)
1277     if (CRITERION)
1278       count++;
1279
1280   /* Hashtables have 2N+1 entries. */
1281   ret = calloc (2*count+1, sizeof (char *));
1282   if (ret == NULL) {
1283     perrorf (g, "calloc");
1284     return NULL;
1285   }
1286
1287   count = 0;
1288   for (i = 0; i < fs->nr_fstab; ++i)
1289     if (CRITERION) {
1290       ret[2*count] = safe_strdup (g, fs->fstab[i].mountpoint);
1291       ret[2*count+1] = safe_strdup (g, fs->fstab[i].device);
1292       count++;
1293     }
1294 #undef CRITERION
1295
1296   return ret;
1297 }
1298
1299 char **
1300 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
1301 {
1302   struct inspect_fs *fs = search_for_root (g, root);
1303   if (!fs)
1304     return NULL;
1305
1306   char **ret;
1307
1308   /* If no fstab information (Windows) return just the root. */
1309   if (fs->nr_fstab == 0) {
1310     ret = calloc (2, sizeof (char *));
1311     ret[0] = safe_strdup (g, root);
1312     ret[1] = NULL;
1313     return ret;
1314   }
1315
1316   ret = calloc (fs->nr_fstab + 1, sizeof (char *));
1317   if (ret == NULL) {
1318     perrorf (g, "calloc");
1319     return NULL;
1320   }
1321
1322   size_t i;
1323   for (i = 0; i < fs->nr_fstab; ++i)
1324     ret[i] = safe_strdup (g, fs->fstab[i].device);
1325
1326   return ret;
1327 }
1328
1329 /* List filesystems.
1330  *
1331  * The current implementation just uses guestfs_vfs_type and doesn't
1332  * try mounting anything, but we reserve the right in future to try
1333  * mounting filesystems.
1334  */
1335
1336 static void remove_from_list (char **list, const char *item);
1337 static void check_with_vfs_type (guestfs_h *g, const char *dev, char ***ret, size_t *ret_size);
1338
1339 char **
1340 guestfs__list_filesystems (guestfs_h *g)
1341 {
1342   size_t i;
1343   char **ret;
1344   size_t ret_size;
1345
1346   ret = safe_malloc (g, sizeof (char *));
1347   ret[0] = NULL;
1348   ret_size = 0;
1349
1350   /* Look to see if any devices directly contain filesystems
1351    * (RHBZ#590167).  However vfs-type will fail to tell us anything
1352    * useful about devices which just contain partitions, so we also
1353    * get the list of partitions and exclude the corresponding devices
1354    * by using part-to-dev.
1355    */
1356   char **devices;
1357   devices = guestfs_list_devices (g);
1358   if (devices == NULL) {
1359     free_string_list (ret);
1360     return NULL;
1361   }
1362   char **partitions;
1363   partitions = guestfs_list_partitions (g);
1364   if (partitions == NULL) {
1365     free_string_list (devices);
1366     free_string_list (ret);
1367     return NULL;
1368   }
1369
1370   for (i = 0; partitions[i] != NULL; ++i) {
1371     char *dev = guestfs_part_to_dev (g, partitions[i]);
1372     if (dev)
1373       remove_from_list (devices, dev);
1374     free (dev);
1375   }
1376
1377   /* Use vfs-type to check for filesystems on devices. */
1378   for (i = 0; devices[i] != NULL; ++i)
1379     check_with_vfs_type (g, devices[i], &ret, &ret_size);
1380   free_string_list (devices);
1381
1382   /* Use vfs-type to check for filesystems on partitions. */
1383   for (i = 0; partitions[i] != NULL; ++i)
1384     check_with_vfs_type (g, partitions[i], &ret, &ret_size);
1385   free_string_list (partitions);
1386
1387   if (feature_available (g, "lvm2")) {
1388     /* Use vfs-type to check for filesystems on LVs. */
1389     char **lvs;
1390     lvs = guestfs_lvs (g);
1391     if (lvs == NULL) {
1392       free_string_list (ret);
1393       return NULL;
1394     }
1395
1396     for (i = 0; lvs[i] != NULL; ++i)
1397       check_with_vfs_type (g, lvs[i], &ret, &ret_size);
1398     free_string_list (lvs);
1399   }
1400
1401   return ret;
1402 }
1403
1404 /* If 'item' occurs in 'list', remove and free it. */
1405 static void
1406 remove_from_list (char **list, const char *item)
1407 {
1408   size_t i;
1409
1410   for (i = 0; list[i] != NULL; ++i)
1411     if (STREQ (list[i], item)) {
1412       free (list[i]);
1413       for (; list[i+1] != NULL; ++i)
1414         list[i] = list[i+1];
1415       list[i] = NULL;
1416       return;
1417     }
1418 }
1419
1420 /* Use vfs-type to look for a filesystem of some sort on 'dev'.
1421  * Apart from some types which we ignore, add the result to the
1422  * 'ret' string list.
1423  */
1424 static void
1425 check_with_vfs_type (guestfs_h *g, const char *device,
1426                      char ***ret, size_t *ret_size)
1427 {
1428   char *v;
1429
1430   guestfs_error_handler_cb old_error_cb = g->error_cb;
1431   g->error_cb = NULL;
1432   char *vfs_type = guestfs_vfs_type (g, device);
1433   g->error_cb = old_error_cb;
1434
1435   if (!vfs_type)
1436     v = safe_strdup (g, "unknown");
1437   else {
1438     /* Ignore all "*_member" strings.  In libblkid these are returned
1439      * for things which are members of some RAID or LVM set, most
1440      * importantly "LVM2_member" which is a PV.
1441      */
1442     size_t n = strlen (vfs_type);
1443     if (n >= 7 && STREQ (&vfs_type[n-7], "_member")) {
1444       free (vfs_type);
1445       return;
1446     }
1447
1448     /* Ignore LUKS-encrypted partitions.  These are also containers. */
1449     if (STREQ (vfs_type, "crypto_LUKS")) {
1450       free (vfs_type);
1451       return;
1452     }
1453
1454     v = vfs_type;
1455   }
1456
1457   /* Extend the return array. */
1458   size_t i = *ret_size;
1459   *ret_size += 2;
1460   *ret = safe_realloc (g, *ret, (*ret_size + 1) * sizeof (char *));
1461   (*ret)[i] = safe_strdup (g, device);
1462   (*ret)[i+1] = v;
1463   (*ret)[i+2] = NULL;
1464 }