Require PCRE library.
[libguestfs.git] / src / inspect_fs_unix.c
1 /* libguestfs
2  * Copyright (C) 2010-2011 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 <fcntl.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <errno.h>
30 #include <endian.h>
31
32 #include <pcre.h>
33
34 #ifdef HAVE_HIVEX
35 #include <hivex.h>
36 #endif
37
38 #include "c-ctype.h"
39 #include "ignore-value.h"
40 #include "xstrtol.h"
41
42 #include "guestfs.h"
43 #include "guestfs-internal.h"
44 #include "guestfs-internal-actions.h"
45 #include "guestfs_protocol.h"
46
47 #if defined(HAVE_HIVEX)
48
49 /* Compile all the regular expressions once when the shared library is
50  * loaded.  PCRE is thread safe so we're supposedly OK here if
51  * multiple threads call into the libguestfs API functions below
52  * simultaneously.
53  */
54 static pcre *re_fedora;
55 static pcre *re_rhel_old;
56 static pcre *re_rhel;
57 static pcre *re_rhel_no_minor;
58 static pcre *re_centos_old;
59 static pcre *re_centos;
60 static pcre *re_centos_no_minor;
61 static pcre *re_scientific_linux_old;
62 static pcre *re_scientific_linux;
63 static pcre *re_scientific_linux_no_minor;
64 static pcre *re_major_minor;
65 static pcre *re_aug_seq;
66 static pcre *re_xdev;
67 static pcre *re_first_partition;
68 static pcre *re_freebsd;
69
70 static void compile_regexps (void) __attribute__((constructor));
71 static void free_regexps (void) __attribute__((destructor));
72
73 static void
74 compile_regexps (void)
75 {
76   const char *err;
77   int offset;
78
79 #define COMPILE(re,pattern,options)                                     \
80   do {                                                                  \
81     re = pcre_compile ((pattern), (options), &err, &offset, NULL);      \
82     if (re == NULL) {                                                   \
83       ignore_value (write (2, err, strlen (err)));                      \
84       abort ();                                                         \
85     }                                                                   \
86   } while (0)
87
88   COMPILE (re_fedora, "Fedora release (\\d+)", 0);
89   COMPILE (re_rhel_old,
90            "Red Hat.*release (\\d+).*Update (\\d+)", 0);
91   COMPILE (re_rhel,
92            "Red Hat.*release (\\d+)\\.(\\d+)", 0);
93   COMPILE (re_rhel_no_minor,
94            "Red Hat.*release (\\d+)", 0);
95   COMPILE (re_centos_old,
96            "CentOS.*release (\\d+).*Update (\\d+)", 0);
97   COMPILE (re_centos,
98            "CentOS.*release (\\d+)\\.(\\d+)", 0);
99   COMPILE (re_centos_no_minor,
100            "CentOS.*release (\\d+)", 0);
101   COMPILE (re_scientific_linux_old,
102            "Scientific Linux.*release (\\d+).*Update (\\d+)", 0);
103   COMPILE (re_scientific_linux,
104            "Scientific Linux.*release (\\d+)\\.(\\d+)", 0);
105   COMPILE (re_scientific_linux_no_minor,
106            "Scientific Linux.*release (\\d+)", 0);
107   COMPILE (re_major_minor, "(\\d+)\\.(\\d+)", 0);
108   COMPILE (re_aug_seq, "/\\d+$", 0);
109   COMPILE (re_xdev, "^/dev/(?:h|s|v|xv)d([a-z]\\d*)$", 0);
110   COMPILE (re_freebsd, "^/dev/ad(\\d+)s(\\d+)([a-z])$", 0);
111 }
112
113 static void
114 free_regexps (void)
115 {
116   pcre_free (re_fedora);
117   pcre_free (re_rhel_old);
118   pcre_free (re_rhel);
119   pcre_free (re_rhel_no_minor);
120   pcre_free (re_centos_old);
121   pcre_free (re_centos);
122   pcre_free (re_centos_no_minor);
123   pcre_free (re_scientific_linux_old);
124   pcre_free (re_scientific_linux);
125   pcre_free (re_scientific_linux_no_minor);
126   pcre_free (re_major_minor);
127   pcre_free (re_aug_seq);
128   pcre_free (re_xdev);
129   pcre_free (re_freebsd);
130 }
131
132 static void check_architecture (guestfs_h *g, struct inspect_fs *fs);
133 static int check_hostname_unix (guestfs_h *g, struct inspect_fs *fs);
134 static int check_hostname_redhat (guestfs_h *g, struct inspect_fs *fs);
135 static int check_hostname_freebsd (guestfs_h *g, struct inspect_fs *fs);
136 static int check_fstab (guestfs_h *g, struct inspect_fs *fs);
137 static int add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
138                             const char *spec, const char *mp);
139 static char *resolve_fstab_device (guestfs_h *g, const char *spec);
140 static int inspect_with_augeas (guestfs_h *g, struct inspect_fs *fs, const char *filename, int (*f) (guestfs_h *, struct inspect_fs *));
141
142 /* Set fs->product_name to the first line of the release file. */
143 static int
144 parse_release_file (guestfs_h *g, struct inspect_fs *fs,
145                     const char *release_filename)
146 {
147   fs->product_name = guestfs___first_line_of_file (g, release_filename);
148   if (fs->product_name == NULL)
149     return -1;
150   return 0;
151 }
152
153 /* Ubuntu has /etc/lsb-release containing:
154  *   DISTRIB_ID=Ubuntu                                # Distro
155  *   DISTRIB_RELEASE=10.04                            # Version
156  *   DISTRIB_CODENAME=lucid
157  *   DISTRIB_DESCRIPTION="Ubuntu 10.04.1 LTS"         # Product name
158  *
159  * [Ubuntu-derived ...] Linux Mint was found to have this:
160  *   DISTRIB_ID=LinuxMint
161  *   DISTRIB_RELEASE=10
162  *   DISTRIB_CODENAME=julia
163  *   DISTRIB_DESCRIPTION="Linux Mint 10 Julia"
164  * Linux Mint also has /etc/linuxmint/info with more information,
165  * but we can use the LSB file.
166  *
167  * Mandriva has:
168  *   LSB_VERSION=lsb-4.0-amd64:lsb-4.0-noarch
169  *   DISTRIB_ID=MandrivaLinux
170  *   DISTRIB_RELEASE=2010.1
171  *   DISTRIB_CODENAME=Henry_Farman
172  *   DISTRIB_DESCRIPTION="Mandriva Linux 2010.1"
173  * Mandriva also has a normal release file called /etc/mandriva-release.
174  */
175 static int
176 parse_lsb_release (guestfs_h *g, struct inspect_fs *fs)
177 {
178   const char *filename = "/etc/lsb-release";
179   int64_t size;
180   char **lines;
181   size_t i;
182   int r = 0;
183
184   /* Don't trust guestfs_head_n not to break with very large files.
185    * Check the file size is something reasonable first.
186    */
187   size = guestfs_filesize (g, filename);
188   if (size == -1)
189     /* guestfs_filesize failed and has already set error in handle */
190     return -1;
191   if (size > MAX_SMALL_FILE_SIZE) {
192     error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
193            filename, size);
194     return -1;
195   }
196
197   lines = guestfs_head_n (g, 10, filename);
198   if (lines == NULL)
199     return -1;
200
201   for (i = 0; lines[i] != NULL; ++i) {
202     if (fs->distro == 0 &&
203         STREQ (lines[i], "DISTRIB_ID=Ubuntu")) {
204       fs->distro = OS_DISTRO_UBUNTU;
205       r = 1;
206     }
207     else if (fs->distro == 0 &&
208              STREQ (lines[i], "DISTRIB_ID=LinuxMint")) {
209       fs->distro = OS_DISTRO_LINUX_MINT;
210       r = 1;
211     }
212     else if (fs->distro == 0 &&
213              STREQ (lines[i], "DISTRIB_ID=MandrivaLinux")) {
214       fs->distro = OS_DISTRO_MANDRIVA;
215       r = 1;
216     }
217     else if (STRPREFIX (lines[i], "DISTRIB_RELEASE=")) {
218       char *major, *minor;
219       if (match2 (g, &lines[i][16], re_major_minor, &major, &minor)) {
220         fs->major_version = guestfs___parse_unsigned_int (g, major);
221         free (major);
222         if (fs->major_version == -1) {
223           free (minor);
224           guestfs___free_string_list (lines);
225           return -1;
226         }
227         fs->minor_version = guestfs___parse_unsigned_int (g, minor);
228         free (minor);
229         if (fs->minor_version == -1) {
230           guestfs___free_string_list (lines);
231           return -1;
232         }
233       }
234     }
235     else if (fs->product_name == NULL &&
236              (STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=\"") ||
237               STRPREFIX (lines[i], "DISTRIB_DESCRIPTION='"))) {
238       size_t len = strlen (lines[i]) - 21 - 1;
239       fs->product_name = safe_strndup (g, &lines[i][21], len);
240       r = 1;
241     }
242     else if (fs->product_name == NULL &&
243              STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=")) {
244       size_t len = strlen (lines[i]) - 20;
245       fs->product_name = safe_strndup (g, &lines[i][20], len);
246       r = 1;
247     }
248   }
249
250   guestfs___free_string_list (lines);
251   return r;
252 }
253
254 /* The currently mounted device is known to be a Linux root.  Try to
255  * determine from this the distro, version, etc.  Also parse
256  * /etc/fstab to determine the arrangement of mountpoints and
257  * associated devices.
258  */
259 int
260 guestfs___check_linux_root (guestfs_h *g, struct inspect_fs *fs)
261 {
262   int r;
263
264   fs->type = OS_TYPE_LINUX;
265
266   if (guestfs_exists (g, "/etc/lsb-release") > 0) {
267     r = parse_lsb_release (g, fs);
268     if (r == -1)        /* error */
269       return -1;
270     if (r == 1)         /* ok - detected the release from this file */
271       goto skip_release_checks;
272   }
273
274   if (guestfs_exists (g, "/etc/redhat-release") > 0) {
275     fs->distro = OS_DISTRO_REDHAT_BASED; /* Something generic Red Hat-like. */
276
277     if (parse_release_file (g, fs, "/etc/redhat-release") == -1)
278       return -1;
279
280     char *major, *minor;
281     if ((major = match1 (g, fs->product_name, re_fedora)) != NULL) {
282       fs->distro = OS_DISTRO_FEDORA;
283       fs->major_version = guestfs___parse_unsigned_int (g, major);
284       free (major);
285       if (fs->major_version == -1)
286         return -1;
287     }
288     else if (match2 (g, fs->product_name, re_rhel_old, &major, &minor) ||
289              match2 (g, fs->product_name, re_rhel, &major, &minor)) {
290       fs->distro = OS_DISTRO_RHEL;
291       fs->major_version = guestfs___parse_unsigned_int (g, major);
292       free (major);
293       if (fs->major_version == -1) {
294         free (minor);
295         return -1;
296       }
297       fs->minor_version = guestfs___parse_unsigned_int (g, minor);
298       free (minor);
299       if (fs->minor_version == -1)
300         return -1;
301     }
302     else if ((major = match1 (g, fs->product_name, re_rhel_no_minor)) != NULL) {
303       fs->distro = OS_DISTRO_RHEL;
304       fs->major_version = guestfs___parse_unsigned_int (g, major);
305       free (major);
306       if (fs->major_version == -1)
307         return -1;
308       fs->minor_version = 0;
309     }
310     else if (match2 (g, fs->product_name, re_centos_old, &major, &minor) ||
311              match2 (g, fs->product_name, re_centos, &major, &minor)) {
312       fs->distro = OS_DISTRO_CENTOS;
313       fs->major_version = guestfs___parse_unsigned_int (g, major);
314       free (major);
315       if (fs->major_version == -1) {
316         free (minor);
317         return -1;
318       }
319       fs->minor_version = guestfs___parse_unsigned_int (g, minor);
320       free (minor);
321       if (fs->minor_version == -1)
322         return -1;
323     }
324     else if ((major = match1 (g, fs->product_name, re_centos_no_minor)) != NULL) {
325       fs->distro = OS_DISTRO_CENTOS;
326       fs->major_version = guestfs___parse_unsigned_int (g, major);
327       free (major);
328       if (fs->major_version == -1)
329         return -1;
330       fs->minor_version = 0;
331     }
332     else if (match2 (g, fs->product_name, re_scientific_linux_old, &major, &minor) ||
333              match2 (g, fs->product_name, re_scientific_linux, &major, &minor)) {
334       fs->distro = OS_DISTRO_SCIENTIFIC_LINUX;
335       fs->major_version = guestfs___parse_unsigned_int (g, major);
336       free (major);
337       if (fs->major_version == -1) {
338         free (minor);
339         return -1;
340       }
341       fs->minor_version = guestfs___parse_unsigned_int (g, minor);
342       free (minor);
343       if (fs->minor_version == -1)
344         return -1;
345     }
346     else if ((major = match1 (g, fs->product_name, re_scientific_linux_no_minor)) != NULL) {
347       fs->distro = OS_DISTRO_SCIENTIFIC_LINUX;
348       fs->major_version = guestfs___parse_unsigned_int (g, major);
349       free (major);
350       if (fs->major_version == -1)
351         return -1;
352       fs->minor_version = 0;
353     }
354   }
355   else if (guestfs_exists (g, "/etc/debian_version") > 0) {
356     fs->distro = OS_DISTRO_DEBIAN;
357
358     if (parse_release_file (g, fs, "/etc/debian_version") == -1)
359       return -1;
360
361     if (guestfs___parse_major_minor (g, fs) == -1)
362       return -1;
363   }
364   else if (guestfs_exists (g, "/etc/pardus-release") > 0) {
365     fs->distro = OS_DISTRO_PARDUS;
366
367     if (parse_release_file (g, fs, "/etc/pardus-release") == -1)
368       return -1;
369
370     if (guestfs___parse_major_minor (g, fs) == -1)
371       return -1;
372   }
373   else if (guestfs_exists (g, "/etc/arch-release") > 0) {
374     fs->distro = OS_DISTRO_ARCHLINUX;
375
376     /* /etc/arch-release file is empty and I can't see a way to
377      * determine the actual release or product string.
378      */
379   }
380   else if (guestfs_exists (g, "/etc/gentoo-release") > 0) {
381     fs->distro = OS_DISTRO_GENTOO;
382
383     if (parse_release_file (g, fs, "/etc/gentoo-release") == -1)
384       return -1;
385
386     if (guestfs___parse_major_minor (g, fs) == -1)
387       return -1;
388   }
389   else if (guestfs_exists (g, "/etc/meego-release") > 0) {
390     fs->distro = OS_DISTRO_MEEGO;
391
392     if (parse_release_file (g, fs, "/etc/meego-release") == -1)
393       return -1;
394
395     if (guestfs___parse_major_minor (g, fs) == -1)
396       return -1;
397   }
398   else if (guestfs_exists (g, "/etc/slackware-version") > 0) {
399     fs->distro = OS_DISTRO_SLACKWARE;
400
401     if (parse_release_file (g, fs, "/etc/slackware-version") == -1)
402       return -1;
403
404     if (guestfs___parse_major_minor (g, fs) == -1)
405       return -1;
406   }
407
408  skip_release_checks:;
409
410   /* Determine the architecture. */
411   check_architecture (g, fs);
412
413   /* We already know /etc/fstab exists because it's part of the test
414    * for Linux root above.  We must now parse this file to determine
415    * which filesystems are used by the operating system and how they
416    * are mounted.
417    */
418   if (inspect_with_augeas (g, fs, "/etc/fstab", check_fstab) == -1)
419     return -1;
420
421   /* Determine hostname. */
422   if (check_hostname_unix (g, fs) == -1)
423     return -1;
424
425   return 0;
426 }
427
428 /* The currently mounted device is known to be a FreeBSD root. */
429 int
430 guestfs___check_freebsd_root (guestfs_h *g, struct inspect_fs *fs)
431 {
432   fs->type = OS_TYPE_FREEBSD;
433
434   /* FreeBSD has no authoritative version file.  The version number is
435    * in /etc/motd, which the system administrator might edit, but
436    * we'll use that anyway.
437    */
438
439   if (guestfs_exists (g, "/etc/motd") > 0) {
440     if (parse_release_file (g, fs, "/etc/motd") == -1)
441       return -1;
442
443     if (guestfs___parse_major_minor (g, fs) == -1)
444       return -1;
445   }
446
447   /* Determine the architecture. */
448   check_architecture (g, fs);
449
450   /* We already know /etc/fstab exists because it's part of the test above. */
451   if (inspect_with_augeas (g, fs, "/etc/fstab", check_fstab) == -1)
452     return -1;
453
454   /* Determine hostname. */
455   if (check_hostname_unix (g, fs) == -1)
456     return -1;
457
458   return 0;
459 }
460
461 static void
462 check_architecture (guestfs_h *g, struct inspect_fs *fs)
463 {
464   const char *binaries[] =
465     { "/bin/bash", "/bin/ls", "/bin/echo", "/bin/rm", "/bin/sh" };
466   size_t i;
467
468   for (i = 0; i < sizeof binaries / sizeof binaries[0]; ++i) {
469     if (guestfs_is_file (g, binaries[i]) > 0) {
470       /* Ignore errors from file_architecture call. */
471       guestfs_error_handler_cb old_error_cb = g->error_cb;
472       g->error_cb = NULL;
473       char *arch = guestfs_file_architecture (g, binaries[i]);
474       g->error_cb = old_error_cb;
475
476       if (arch) {
477         /* String will be owned by handle, freed by
478          * guestfs___free_inspect_info.
479          */
480         fs->arch = arch;
481         break;
482       }
483     }
484   }
485 }
486
487 /* Try several methods to determine the hostname from a Linux or
488  * FreeBSD guest.  Note that type and distro have been set, so we can
489  * use that information to direct the search.
490  */
491 static int
492 check_hostname_unix (guestfs_h *g, struct inspect_fs *fs)
493 {
494   switch (fs->type) {
495   case OS_TYPE_LINUX:
496     /* Red Hat-derived would be in /etc/sysconfig/network, and
497      * Debian-derived in the file /etc/hostname.  Very old Debian and
498      * SUSE use /etc/HOSTNAME.  It's best to just look for each of
499      * these files in turn, rather than try anything clever based on
500      * distro.
501      */
502     if (guestfs_is_file (g, "/etc/HOSTNAME")) {
503       fs->hostname = guestfs___first_line_of_file (g, "/etc/HOSTNAME");
504       if (fs->hostname == NULL)
505         return -1;
506     }
507     else if (guestfs_is_file (g, "/etc/hostname")) {
508       fs->hostname = guestfs___first_line_of_file (g, "/etc/hostname");
509       if (fs->hostname == NULL)
510         return -1;
511     }
512     else if (guestfs_is_file (g, "/etc/sysconfig/network")) {
513       if (inspect_with_augeas (g, fs, "/etc/sysconfig/network",
514                                check_hostname_redhat) == -1)
515         return -1;
516     }
517     break;
518
519   case OS_TYPE_FREEBSD:
520     /* /etc/rc.conf contains the hostname, but there is no Augeas lens
521      * for this file.
522      */
523     if (guestfs_is_file (g, "/etc/rc.conf")) {
524       if (check_hostname_freebsd (g, fs) == -1)
525         return -1;
526     }
527     break;
528
529   case OS_TYPE_WINDOWS: /* not here, see check_windows_system_registry */
530   case OS_TYPE_UNKNOWN:
531   default:
532     /* nothing, keep GCC warnings happy */;
533   }
534
535   return 0;
536 }
537
538 /* Parse the hostname from /etc/sysconfig/network.  This must be called
539  * from the inspect_with_augeas wrapper.
540  */
541 static int
542 check_hostname_redhat (guestfs_h *g, struct inspect_fs *fs)
543 {
544   char *hostname;
545
546   hostname = guestfs_aug_get (g, "/files/etc/sysconfig/network/HOSTNAME");
547   if (!hostname)
548     return -1;
549
550   fs->hostname = hostname;  /* freed by guestfs___free_inspect_info */
551   return 0;
552 }
553
554 /* Parse the hostname from /etc/rc.conf.  On FreeBSD this file
555  * contains comments, blank lines and:
556  *   hostname="freebsd8.example.com"
557  *   ifconfig_re0="DHCP"
558  *   keymap="uk.iso"
559  *   sshd_enable="YES"
560  */
561 static int
562 check_hostname_freebsd (guestfs_h *g, struct inspect_fs *fs)
563 {
564   const char *filename = "/etc/rc.conf";
565   int64_t size;
566   char **lines;
567   size_t i;
568
569   /* Don't trust guestfs_read_lines not to break with very large files.
570    * Check the file size is something reasonable first.
571    */
572   size = guestfs_filesize (g, filename);
573   if (size == -1)
574     /* guestfs_filesize failed and has already set error in handle */
575     return -1;
576   if (size > MAX_SMALL_FILE_SIZE) {
577     error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
578            filename, size);
579     return -1;
580   }
581
582   lines = guestfs_read_lines (g, filename);
583   if (lines == NULL)
584     return -1;
585
586   for (i = 0; lines[i] != NULL; ++i) {
587     if (STRPREFIX (lines[i], "hostname=\"") ||
588         STRPREFIX (lines[i], "hostname='")) {
589       size_t len = strlen (lines[i]) - 10 - 1;
590       fs->hostname = safe_strndup (g, &lines[i][10], len);
591       break;
592     } else if (STRPREFIX (lines[i], "hostname=")) {
593       size_t len = strlen (lines[i]) - 9;
594       fs->hostname = safe_strndup (g, &lines[i][9], len);
595       break;
596     }
597   }
598
599   guestfs___free_string_list (lines);
600   return 0;
601 }
602
603 static int
604 check_fstab (guestfs_h *g, struct inspect_fs *fs)
605 {
606   char **lines = guestfs_aug_ls (g, "/files/etc/fstab");
607   if (lines == NULL)
608     return -1;
609
610   if (lines[0] == NULL) {
611     error (g, _("could not parse /etc/fstab or empty file"));
612     guestfs___free_string_list (lines);
613     return -1;
614   }
615
616   size_t i;
617   char augpath[256];
618   for (i = 0; lines[i] != NULL; ++i) {
619     /* Ignore comments.  Only care about sequence lines which
620      * match m{/\d+$}.
621      */
622     if (match (g, lines[i], re_aug_seq)) {
623       snprintf (augpath, sizeof augpath, "%s/spec", lines[i]);
624       char *spec = guestfs_aug_get (g, augpath);
625       if (spec == NULL) {
626         guestfs___free_string_list (lines);
627         return -1;
628       }
629
630       snprintf (augpath, sizeof augpath, "%s/file", lines[i]);
631       char *mp = guestfs_aug_get (g, augpath);
632       if (mp == NULL) {
633         guestfs___free_string_list (lines);
634         free (spec);
635         return -1;
636       }
637
638       int r = add_fstab_entry (g, fs, spec, mp);
639       free (spec);
640       free (mp);
641
642       if (r == -1) {
643         guestfs___free_string_list (lines);
644         return -1;
645       }
646     }
647   }
648
649   guestfs___free_string_list (lines);
650   return 0;
651 }
652
653 /* Add a filesystem and possibly a mountpoint entry for
654  * the root filesystem 'fs'.
655  *
656  * 'spec' is the fstab spec field, which might be a device name or a
657  * pseudodevice or 'UUID=...' or 'LABEL=...'.
658  *
659  * 'mp' is the mount point, which could also be 'swap' or 'none'.
660  */
661 static int
662 add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
663                  const char *spec, const char *mp)
664 {
665   /* Ignore certain mountpoints. */
666   if (STRPREFIX (mp, "/dev/") ||
667       STREQ (mp, "/dev") ||
668       STRPREFIX (mp, "/media/") ||
669       STRPREFIX (mp, "/proc/") ||
670       STREQ (mp, "/proc") ||
671       STRPREFIX (mp, "/selinux/") ||
672       STREQ (mp, "/selinux") ||
673       STRPREFIX (mp, "/sys/") ||
674       STREQ (mp, "/sys"))
675     return 0;
676
677   /* Ignore /dev/fd (floppy disks) (RHBZ#642929) and CD-ROM drives. */
678   if ((STRPREFIX (spec, "/dev/fd") && c_isdigit (spec[7])) ||
679       STREQ (spec, "/dev/floppy") ||
680       STREQ (spec, "/dev/cdrom"))
681     return 0;
682
683   /* Resolve UUID= and LABEL= to the actual device. */
684   char *device = NULL;
685   if (STRPREFIX (spec, "UUID="))
686     device = guestfs_findfs_uuid (g, &spec[5]);
687   else if (STRPREFIX (spec, "LABEL="))
688     device = guestfs_findfs_label (g, &spec[6]);
689   /* Ignore "/.swap" (Pardus) and pseudo-devices like "tmpfs". */
690   else if (STRPREFIX (spec, "/dev/"))
691     /* Resolve guest block device names. */
692     device = resolve_fstab_device (g, spec);
693
694   /* If we haven't resolved the device successfully by this point,
695    * we don't care, just ignore it.
696    */
697   if (device == NULL)
698     return 0;
699
700   char *mountpoint = safe_strdup (g, mp);
701
702   /* Add this to the fstab entry in 'fs'.
703    * Note these are further filtered by guestfs_inspect_get_mountpoints
704    * and guestfs_inspect_get_filesystems.
705    */
706   size_t n = fs->nr_fstab + 1;
707   struct inspect_fstab_entry *p;
708
709   p = realloc (fs->fstab, n * sizeof (struct inspect_fstab_entry));
710   if (p == NULL) {
711     perrorf (g, "realloc");
712     free (device);
713     free (mountpoint);
714     return -1;
715   }
716
717   fs->fstab = p;
718   fs->nr_fstab = n;
719
720   /* These are owned by the handle and freed by guestfs___free_inspect_info. */
721   fs->fstab[n-1].device = device;
722   fs->fstab[n-1].mountpoint = mountpoint;
723
724   debug (g, "fstab: device=%s mountpoint=%s", device, mountpoint);
725
726   return 0;
727 }
728
729 /* Resolve block device name to the libguestfs device name, eg.
730  * /dev/xvdb1 => /dev/vdb1; and /dev/mapper/VG-LV => /dev/VG/LV.  This
731  * assumes that disks were added in the same order as they appear to
732  * the real VM, which is a reasonable assumption to make.  Return
733  * anything we don't recognize unchanged.
734  */
735 static char *
736 resolve_fstab_device (guestfs_h *g, const char *spec)
737 {
738   char *a1;
739   char *device = NULL;
740   char *bsddisk, *bsdslice, *bsdpart;
741
742   if (STRPREFIX (spec, "/dev/mapper/")) {
743     /* LVM2 does some strange munging on /dev/mapper paths for VGs and
744      * LVs which contain '-' character:
745      *
746      * ><fs> lvcreate LV--test VG--test 32
747      * ><fs> debug ls /dev/mapper
748      * VG----test-LV----test
749      *
750      * This makes it impossible to reverse those paths directly, so
751      * we have implemented lvm_canonical_lv_name in the daemon.
752      */
753     device = guestfs_lvm_canonical_lv_name (g, spec);
754   }
755   else if ((a1 = match1 (g, spec, re_xdev)) != NULL) {
756     char **devices = guestfs_list_devices (g);
757     if (devices == NULL)
758       return NULL;
759
760     size_t count;
761     for (count = 0; devices[count] != NULL; count++)
762       ;
763
764     size_t i = a1[0] - 'a'; /* a1[0] is always [a-z] because of regex. */
765     if (i < count) {
766       size_t len = strlen (devices[i]) + strlen (a1) + 16;
767       device = safe_malloc (g, len);
768       snprintf (device, len, "%s%s", devices[i], &a1[1]);
769     }
770
771     free (a1);
772     guestfs___free_string_list (devices);
773   }
774   else if (match3 (g, spec, re_freebsd, &bsddisk, &bsdslice, &bsdpart)) {
775     /* FreeBSD disks are organized quite differently.  See:
776      * http://www.freebsd.org/doc/handbook/disk-organization.html
777      * FreeBSD "partitions" are exposed as quasi-extended partitions
778      * numbered from 5 in Linux.  I have no idea what happens when you
779      * have multiple "slices" (the FreeBSD term for MBR partitions).
780      */
781     int disk = guestfs___parse_unsigned_int (g, bsddisk);
782     int slice = guestfs___parse_unsigned_int (g, bsdslice);
783     int part = bsdpart[0] - 'a' /* counting from 0 */;
784     free (bsddisk);
785     free (bsdslice);
786     free (bsdpart);
787
788     if (disk == -1 || disk > 26 ||
789         slice <= 0 || slice > 1 /* > 4 .. see comment above */ ||
790         part < 0 || part >= 26)
791       goto out;
792
793     device = safe_asprintf (g, "/dev/sd%c%d", disk + 'a', part + 5);
794   }
795
796  out:
797   /* Didn't match device pattern, return original spec unchanged. */
798   if (device == NULL)
799     device = safe_strdup (g, spec);
800
801   return device;
802 }
803
804 /* Call 'f' with Augeas opened and having parsed 'filename' (this file
805  * must exist).  As a security measure, this bails if the file is too
806  * large for a reasonable configuration file.  After the call to 'f'
807  * Augeas is closed.
808  */
809 static int
810 inspect_with_augeas (guestfs_h *g, struct inspect_fs *fs, const char *filename,
811                      int (*f) (guestfs_h *, struct inspect_fs *))
812 {
813   /* Security: Refuse to do this if filename is too large. */
814   int64_t size = guestfs_filesize (g, filename);
815   if (size == -1)
816     /* guestfs_filesize failed and has already set error in handle */
817     return -1;
818   if (size > MAX_AUGEAS_FILE_SIZE) {
819     error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
820            filename, size);
821     return -1;
822   }
823
824   /* If !feature_available (g, "augeas") then the next call will fail.
825    * Arguably we might want to fall back to a non-Augeas method in
826    * this case.
827    */
828   if (guestfs_aug_init (g, "/", 16|32) == -1)
829     return -1;
830
831   int r = -1;
832
833   /* Tell Augeas to only load one file (thanks RaphaĆ«l Pinson). */
834   char buf[strlen (filename) + 64];
835   snprintf (buf, strlen (filename) + 64, "/augeas/load//incl[. != \"%s\"]",
836             filename);
837   if (guestfs_aug_rm (g, buf) == -1)
838     goto out;
839
840   if (guestfs_aug_load (g) == -1)
841     goto out;
842
843   r = f (g, fs);
844
845  out:
846   guestfs_aug_close (g);
847
848   return r;
849 }
850
851 #endif /* defined(HAVE_HIVEX) */