Add Opensuse and zypper detection support
[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 (fs->distro == 0 &&
218              STREQ (lines[i], "DISTRIB_ID=\"Mageia\"")) {
219       fs->distro = OS_DISTRO_MAGEIA;
220       r = 1;
221     }
222     else if (STRPREFIX (lines[i], "DISTRIB_RELEASE=")) {
223       char *major, *minor;
224       if (match2 (g, &lines[i][16], re_major_minor, &major, &minor)) {
225         fs->major_version = guestfs___parse_unsigned_int (g, major);
226         free (major);
227         if (fs->major_version == -1) {
228           free (minor);
229           guestfs___free_string_list (lines);
230           return -1;
231         }
232         fs->minor_version = guestfs___parse_unsigned_int (g, minor);
233         free (minor);
234         if (fs->minor_version == -1) {
235           guestfs___free_string_list (lines);
236           return -1;
237         }
238       }
239     }
240     else if (fs->product_name == NULL &&
241              (STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=\"") ||
242               STRPREFIX (lines[i], "DISTRIB_DESCRIPTION='"))) {
243       size_t len = strlen (lines[i]) - 21 - 1;
244       fs->product_name = safe_strndup (g, &lines[i][21], len);
245       r = 1;
246     }
247     else if (fs->product_name == NULL &&
248              STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=")) {
249       size_t len = strlen (lines[i]) - 20;
250       fs->product_name = safe_strndup (g, &lines[i][20], len);
251       r = 1;
252     }
253   }
254
255   guestfs___free_string_list (lines);
256   return r;
257 }
258
259 /* The currently mounted device is known to be a Linux root.  Try to
260  * determine from this the distro, version, etc.  Also parse
261  * /etc/fstab to determine the arrangement of mountpoints and
262  * associated devices.
263  */
264 int
265 guestfs___check_linux_root (guestfs_h *g, struct inspect_fs *fs)
266 {
267   int r;
268
269   fs->type = OS_TYPE_LINUX;
270
271   if (guestfs_exists (g, "/etc/lsb-release") > 0) {
272     r = parse_lsb_release (g, fs);
273     if (r == -1)        /* error */
274       return -1;
275     if (r == 1)         /* ok - detected the release from this file */
276       goto skip_release_checks;
277   }
278
279   if (guestfs_exists (g, "/etc/redhat-release") > 0) {
280     fs->distro = OS_DISTRO_REDHAT_BASED; /* Something generic Red Hat-like. */
281
282     if (parse_release_file (g, fs, "/etc/redhat-release") == -1)
283       return -1;
284
285     char *major, *minor;
286     if ((major = match1 (g, fs->product_name, re_fedora)) != NULL) {
287       fs->distro = OS_DISTRO_FEDORA;
288       fs->major_version = guestfs___parse_unsigned_int (g, major);
289       free (major);
290       if (fs->major_version == -1)
291         return -1;
292     }
293     else if (match2 (g, fs->product_name, re_rhel_old, &major, &minor) ||
294              match2 (g, fs->product_name, re_rhel, &major, &minor)) {
295       fs->distro = OS_DISTRO_RHEL;
296       fs->major_version = guestfs___parse_unsigned_int (g, major);
297       free (major);
298       if (fs->major_version == -1) {
299         free (minor);
300         return -1;
301       }
302       fs->minor_version = guestfs___parse_unsigned_int (g, minor);
303       free (minor);
304       if (fs->minor_version == -1)
305         return -1;
306     }
307     else if ((major = match1 (g, fs->product_name, re_rhel_no_minor)) != NULL) {
308       fs->distro = OS_DISTRO_RHEL;
309       fs->major_version = guestfs___parse_unsigned_int (g, major);
310       free (major);
311       if (fs->major_version == -1)
312         return -1;
313       fs->minor_version = 0;
314     }
315     else if (match2 (g, fs->product_name, re_centos_old, &major, &minor) ||
316              match2 (g, fs->product_name, re_centos, &major, &minor)) {
317       fs->distro = OS_DISTRO_CENTOS;
318       fs->major_version = guestfs___parse_unsigned_int (g, major);
319       free (major);
320       if (fs->major_version == -1) {
321         free (minor);
322         return -1;
323       }
324       fs->minor_version = guestfs___parse_unsigned_int (g, minor);
325       free (minor);
326       if (fs->minor_version == -1)
327         return -1;
328     }
329     else if ((major = match1 (g, fs->product_name, re_centos_no_minor)) != NULL) {
330       fs->distro = OS_DISTRO_CENTOS;
331       fs->major_version = guestfs___parse_unsigned_int (g, major);
332       free (major);
333       if (fs->major_version == -1)
334         return -1;
335       fs->minor_version = 0;
336     }
337     else if (match2 (g, fs->product_name, re_scientific_linux_old, &major, &minor) ||
338              match2 (g, fs->product_name, re_scientific_linux, &major, &minor)) {
339       fs->distro = OS_DISTRO_SCIENTIFIC_LINUX;
340       fs->major_version = guestfs___parse_unsigned_int (g, major);
341       free (major);
342       if (fs->major_version == -1) {
343         free (minor);
344         return -1;
345       }
346       fs->minor_version = guestfs___parse_unsigned_int (g, minor);
347       free (minor);
348       if (fs->minor_version == -1)
349         return -1;
350     }
351     else if ((major = match1 (g, fs->product_name, re_scientific_linux_no_minor)) != NULL) {
352       fs->distro = OS_DISTRO_SCIENTIFIC_LINUX;
353       fs->major_version = guestfs___parse_unsigned_int (g, major);
354       free (major);
355       if (fs->major_version == -1)
356         return -1;
357       fs->minor_version = 0;
358     }
359   }
360   else if (guestfs_exists (g, "/etc/debian_version") > 0) {
361     fs->distro = OS_DISTRO_DEBIAN;
362
363     if (parse_release_file (g, fs, "/etc/debian_version") == -1)
364       return -1;
365
366     if (guestfs___parse_major_minor (g, fs) == -1)
367       return -1;
368   }
369   else if (guestfs_exists (g, "/etc/pardus-release") > 0) {
370     fs->distro = OS_DISTRO_PARDUS;
371
372     if (parse_release_file (g, fs, "/etc/pardus-release") == -1)
373       return -1;
374
375     if (guestfs___parse_major_minor (g, fs) == -1)
376       return -1;
377   }
378   else if (guestfs_exists (g, "/etc/arch-release") > 0) {
379     fs->distro = OS_DISTRO_ARCHLINUX;
380
381     /* /etc/arch-release file is empty and I can't see a way to
382      * determine the actual release or product string.
383      */
384   }
385   else if (guestfs_exists (g, "/etc/gentoo-release") > 0) {
386     fs->distro = OS_DISTRO_GENTOO;
387
388     if (parse_release_file (g, fs, "/etc/gentoo-release") == -1)
389       return -1;
390
391     if (guestfs___parse_major_minor (g, fs) == -1)
392       return -1;
393   }
394   else if (guestfs_exists (g, "/etc/meego-release") > 0) {
395     fs->distro = OS_DISTRO_MEEGO;
396
397     if (parse_release_file (g, fs, "/etc/meego-release") == -1)
398       return -1;
399
400     if (guestfs___parse_major_minor (g, fs) == -1)
401       return -1;
402   }
403   else if (guestfs_exists (g, "/etc/slackware-version") > 0) {
404     fs->distro = OS_DISTRO_SLACKWARE;
405
406     if (parse_release_file (g, fs, "/etc/slackware-version") == -1)
407       return -1;
408
409     if (guestfs___parse_major_minor (g, fs) == -1)
410       return -1;
411   }
412   else if (guestfs_exists (g, "/etc/ttylinux-target") > 0) {
413     fs->distro = OS_DISTRO_TTYLINUX;
414
415     fs->product_name = guestfs___first_line_of_file (g, "/etc/ttylinux-target");
416     if (fs->product_name == NULL)
417       return -1;
418
419     if (guestfs___parse_major_minor (g, fs) == -1)
420       return -1;
421   }
422   else if (guestfs_exists (g, "/etc/SuSE-release") > 0) {
423     fs->distro = OS_DISTRO_OPENSUSE;
424
425     if (parse_release_file (g, fs, "/etc/SuSE-release") == -1)
426       return -1;
427
428     if (guestfs___parse_major_minor (g, fs) == -1)
429       return -1;
430   }
431
432
433  skip_release_checks:;
434
435   /* Determine the architecture. */
436   check_architecture (g, fs);
437
438   /* We already know /etc/fstab exists because it's part of the test
439    * for Linux root above.  We must now parse this file to determine
440    * which filesystems are used by the operating system and how they
441    * are mounted.
442    */
443   if (inspect_with_augeas (g, fs, "/etc/fstab", check_fstab) == -1)
444     return -1;
445
446   /* Determine hostname. */
447   if (check_hostname_unix (g, fs) == -1)
448     return -1;
449
450   return 0;
451 }
452
453 /* The currently mounted device is known to be a FreeBSD root. */
454 int
455 guestfs___check_freebsd_root (guestfs_h *g, struct inspect_fs *fs)
456 {
457   fs->type = OS_TYPE_FREEBSD;
458
459   /* FreeBSD has no authoritative version file.  The version number is
460    * in /etc/motd, which the system administrator might edit, but
461    * we'll use that anyway.
462    */
463
464   if (guestfs_exists (g, "/etc/motd") > 0) {
465     if (parse_release_file (g, fs, "/etc/motd") == -1)
466       return -1;
467
468     if (guestfs___parse_major_minor (g, fs) == -1)
469       return -1;
470   }
471
472   /* Determine the architecture. */
473   check_architecture (g, fs);
474
475   /* We already know /etc/fstab exists because it's part of the test above. */
476   if (inspect_with_augeas (g, fs, "/etc/fstab", check_fstab) == -1)
477     return -1;
478
479   /* Determine hostname. */
480   if (check_hostname_unix (g, fs) == -1)
481     return -1;
482
483   return 0;
484 }
485
486 static void
487 check_architecture (guestfs_h *g, struct inspect_fs *fs)
488 {
489   const char *binaries[] =
490     { "/bin/bash", "/bin/ls", "/bin/echo", "/bin/rm", "/bin/sh" };
491   size_t i;
492
493   for (i = 0; i < sizeof binaries / sizeof binaries[0]; ++i) {
494     if (guestfs_is_file (g, binaries[i]) > 0) {
495       /* Ignore errors from file_architecture call. */
496       guestfs_error_handler_cb old_error_cb = g->error_cb;
497       g->error_cb = NULL;
498       char *arch = guestfs_file_architecture (g, binaries[i]);
499       g->error_cb = old_error_cb;
500
501       if (arch) {
502         /* String will be owned by handle, freed by
503          * guestfs___free_inspect_info.
504          */
505         fs->arch = arch;
506         break;
507       }
508     }
509   }
510 }
511
512 /* Try several methods to determine the hostname from a Linux or
513  * FreeBSD guest.  Note that type and distro have been set, so we can
514  * use that information to direct the search.
515  */
516 static int
517 check_hostname_unix (guestfs_h *g, struct inspect_fs *fs)
518 {
519   switch (fs->type) {
520   case OS_TYPE_LINUX:
521     /* Red Hat-derived would be in /etc/sysconfig/network, and
522      * Debian-derived in the file /etc/hostname.  Very old Debian and
523      * SUSE use /etc/HOSTNAME.  It's best to just look for each of
524      * these files in turn, rather than try anything clever based on
525      * distro.
526      */
527     if (guestfs_is_file (g, "/etc/HOSTNAME")) {
528       fs->hostname = guestfs___first_line_of_file (g, "/etc/HOSTNAME");
529       if (fs->hostname == NULL)
530         return -1;
531     }
532     else if (guestfs_is_file (g, "/etc/hostname")) {
533       fs->hostname = guestfs___first_line_of_file (g, "/etc/hostname");
534       if (fs->hostname == NULL)
535         return -1;
536     }
537     else if (guestfs_is_file (g, "/etc/sysconfig/network")) {
538       if (inspect_with_augeas (g, fs, "/etc/sysconfig/network",
539                                check_hostname_redhat) == -1)
540         return -1;
541     }
542     break;
543
544   case OS_TYPE_FREEBSD:
545     /* /etc/rc.conf contains the hostname, but there is no Augeas lens
546      * for this file.
547      */
548     if (guestfs_is_file (g, "/etc/rc.conf")) {
549       if (check_hostname_freebsd (g, fs) == -1)
550         return -1;
551     }
552     break;
553
554   case OS_TYPE_WINDOWS: /* not here, see check_windows_system_registry */
555   case OS_TYPE_UNKNOWN:
556   default:
557     /* nothing, keep GCC warnings happy */;
558   }
559
560   return 0;
561 }
562
563 /* Parse the hostname from /etc/sysconfig/network.  This must be called
564  * from the inspect_with_augeas wrapper.
565  */
566 static int
567 check_hostname_redhat (guestfs_h *g, struct inspect_fs *fs)
568 {
569   char *hostname;
570
571   /* Errors here are not fatal (RHBZ#726739), since it could be
572    * just missing HOSTNAME field in the file.
573    */
574   guestfs_error_handler_cb old_error_cb = g->error_cb;
575   g->error_cb = NULL;
576   hostname = guestfs_aug_get (g, "/files/etc/sysconfig/network/HOSTNAME");
577   g->error_cb = old_error_cb;
578
579   /* This is freed by guestfs___free_inspect_info.  Note that hostname
580    * could be NULL because we ignored errors above.
581    */
582   fs->hostname = hostname;
583   return 0;
584 }
585
586 /* Parse the hostname from /etc/rc.conf.  On FreeBSD this file
587  * contains comments, blank lines and:
588  *   hostname="freebsd8.example.com"
589  *   ifconfig_re0="DHCP"
590  *   keymap="uk.iso"
591  *   sshd_enable="YES"
592  */
593 static int
594 check_hostname_freebsd (guestfs_h *g, struct inspect_fs *fs)
595 {
596   const char *filename = "/etc/rc.conf";
597   int64_t size;
598   char **lines;
599   size_t i;
600
601   /* Don't trust guestfs_read_lines not to break with very large files.
602    * Check the file size is something reasonable first.
603    */
604   size = guestfs_filesize (g, filename);
605   if (size == -1)
606     /* guestfs_filesize failed and has already set error in handle */
607     return -1;
608   if (size > MAX_SMALL_FILE_SIZE) {
609     error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
610            filename, size);
611     return -1;
612   }
613
614   lines = guestfs_read_lines (g, filename);
615   if (lines == NULL)
616     return -1;
617
618   for (i = 0; lines[i] != NULL; ++i) {
619     if (STRPREFIX (lines[i], "hostname=\"") ||
620         STRPREFIX (lines[i], "hostname='")) {
621       size_t len = strlen (lines[i]) - 10 - 1;
622       fs->hostname = safe_strndup (g, &lines[i][10], len);
623       break;
624     } else if (STRPREFIX (lines[i], "hostname=")) {
625       size_t len = strlen (lines[i]) - 9;
626       fs->hostname = safe_strndup (g, &lines[i][9], len);
627       break;
628     }
629   }
630
631   guestfs___free_string_list (lines);
632   return 0;
633 }
634
635 static int
636 check_fstab (guestfs_h *g, struct inspect_fs *fs)
637 {
638   char **lines = guestfs_aug_ls (g, "/files/etc/fstab");
639   if (lines == NULL)
640     return -1;
641
642   if (lines[0] == NULL) {
643     error (g, _("could not parse /etc/fstab or empty file"));
644     guestfs___free_string_list (lines);
645     return -1;
646   }
647
648   size_t i;
649   char augpath[256];
650   for (i = 0; lines[i] != NULL; ++i) {
651     /* Ignore comments.  Only care about sequence lines which
652      * match m{/\d+$}.
653      */
654     if (match (g, lines[i], re_aug_seq)) {
655       snprintf (augpath, sizeof augpath, "%s/spec", lines[i]);
656       char *spec = guestfs_aug_get (g, augpath);
657       if (spec == NULL) {
658         guestfs___free_string_list (lines);
659         return -1;
660       }
661
662       snprintf (augpath, sizeof augpath, "%s/file", lines[i]);
663       char *mp = guestfs_aug_get (g, augpath);
664       if (mp == NULL) {
665         guestfs___free_string_list (lines);
666         free (spec);
667         return -1;
668       }
669
670       int r = add_fstab_entry (g, fs, spec, mp);
671       free (spec);
672       free (mp);
673
674       if (r == -1) {
675         guestfs___free_string_list (lines);
676         return -1;
677       }
678     }
679   }
680
681   guestfs___free_string_list (lines);
682   return 0;
683 }
684
685 /* Add a filesystem and possibly a mountpoint entry for
686  * the root filesystem 'fs'.
687  *
688  * 'spec' is the fstab spec field, which might be a device name or a
689  * pseudodevice or 'UUID=...' or 'LABEL=...'.
690  *
691  * 'mp' is the mount point, which could also be 'swap' or 'none'.
692  */
693 static int
694 add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
695                  const char *spec, const char *mp)
696 {
697   /* Ignore certain mountpoints. */
698   if (STRPREFIX (mp, "/dev/") ||
699       STREQ (mp, "/dev") ||
700       STRPREFIX (mp, "/media/") ||
701       STRPREFIX (mp, "/proc/") ||
702       STREQ (mp, "/proc") ||
703       STRPREFIX (mp, "/selinux/") ||
704       STREQ (mp, "/selinux") ||
705       STRPREFIX (mp, "/sys/") ||
706       STREQ (mp, "/sys"))
707     return 0;
708
709   /* Ignore /dev/fd (floppy disks) (RHBZ#642929) and CD-ROM drives. */
710   if ((STRPREFIX (spec, "/dev/fd") && c_isdigit (spec[7])) ||
711       STREQ (spec, "/dev/floppy") ||
712       STREQ (spec, "/dev/cdrom"))
713     return 0;
714
715   /* Resolve UUID= and LABEL= to the actual device. */
716   char *device = NULL;
717   if (STRPREFIX (spec, "UUID="))
718     device = guestfs_findfs_uuid (g, &spec[5]);
719   else if (STRPREFIX (spec, "LABEL="))
720     device = guestfs_findfs_label (g, &spec[6]);
721   /* Ignore "/.swap" (Pardus) and pseudo-devices like "tmpfs". */
722   else if (STREQ (spec, "/dev/root"))
723     /* Resolve /dev/root to the current device. */
724     device = safe_strdup (g, fs->device);
725   else if (STRPREFIX (spec, "/dev/"))
726     /* Resolve guest block device names. */
727     device = resolve_fstab_device (g, spec);
728
729   /* If we haven't resolved the device successfully by this point,
730    * we don't care, just ignore it.
731    */
732   if (device == NULL)
733     return 0;
734
735   char *mountpoint = safe_strdup (g, mp);
736
737   /* Add this to the fstab entry in 'fs'.
738    * Note these are further filtered by guestfs_inspect_get_mountpoints
739    * and guestfs_inspect_get_filesystems.
740    */
741   size_t n = fs->nr_fstab + 1;
742   struct inspect_fstab_entry *p;
743
744   p = realloc (fs->fstab, n * sizeof (struct inspect_fstab_entry));
745   if (p == NULL) {
746     perrorf (g, "realloc");
747     free (device);
748     free (mountpoint);
749     return -1;
750   }
751
752   fs->fstab = p;
753   fs->nr_fstab = n;
754
755   /* These are owned by the handle and freed by guestfs___free_inspect_info. */
756   fs->fstab[n-1].device = device;
757   fs->fstab[n-1].mountpoint = mountpoint;
758
759   debug (g, "fstab: device=%s mountpoint=%s", device, mountpoint);
760
761   return 0;
762 }
763
764 /* Resolve block device name to the libguestfs device name, eg.
765  * /dev/xvdb1 => /dev/vdb1; and /dev/mapper/VG-LV => /dev/VG/LV.  This
766  * assumes that disks were added in the same order as they appear to
767  * the real VM, which is a reasonable assumption to make.  Return
768  * anything we don't recognize unchanged.
769  */
770 static char *
771 resolve_fstab_device (guestfs_h *g, const char *spec)
772 {
773   char *a1;
774   char *device = NULL;
775   char *bsddisk, *bsdslice, *bsdpart;
776
777   if (STRPREFIX (spec, "/dev/mapper/")) {
778     /* LVM2 does some strange munging on /dev/mapper paths for VGs and
779      * LVs which contain '-' character:
780      *
781      * ><fs> lvcreate LV--test VG--test 32
782      * ><fs> debug ls /dev/mapper
783      * VG----test-LV----test
784      *
785      * This makes it impossible to reverse those paths directly, so
786      * we have implemented lvm_canonical_lv_name in the daemon.
787      */
788     device = guestfs_lvm_canonical_lv_name (g, spec);
789   }
790   else if ((a1 = match1 (g, spec, re_xdev)) != NULL) {
791     char **devices = guestfs_list_devices (g);
792     if (devices == NULL)
793       return NULL;
794
795     size_t count;
796     for (count = 0; devices[count] != NULL; count++)
797       ;
798
799     size_t i = a1[0] - 'a'; /* a1[0] is always [a-z] because of regex. */
800     if (i < count) {
801       size_t len = strlen (devices[i]) + strlen (a1) + 16;
802       device = safe_malloc (g, len);
803       snprintf (device, len, "%s%s", devices[i], &a1[1]);
804     }
805
806     free (a1);
807     guestfs___free_string_list (devices);
808   }
809   else if (match3 (g, spec, re_freebsd, &bsddisk, &bsdslice, &bsdpart)) {
810     /* FreeBSD disks are organized quite differently.  See:
811      * http://www.freebsd.org/doc/handbook/disk-organization.html
812      * FreeBSD "partitions" are exposed as quasi-extended partitions
813      * numbered from 5 in Linux.  I have no idea what happens when you
814      * have multiple "slices" (the FreeBSD term for MBR partitions).
815      */
816     int disk = guestfs___parse_unsigned_int (g, bsddisk);
817     int slice = guestfs___parse_unsigned_int (g, bsdslice);
818     int part = bsdpart[0] - 'a' /* counting from 0 */;
819     free (bsddisk);
820     free (bsdslice);
821     free (bsdpart);
822
823     if (disk == -1 || disk > 26 ||
824         slice <= 0 || slice > 1 /* > 4 .. see comment above */ ||
825         part < 0 || part >= 26)
826       goto out;
827
828     device = safe_asprintf (g, "/dev/sd%c%d", disk + 'a', part + 5);
829   }
830
831  out:
832   /* Didn't match device pattern, return original spec unchanged. */
833   if (device == NULL)
834     device = safe_strdup (g, spec);
835
836   return device;
837 }
838
839 /* Call 'f' with Augeas opened and having parsed 'filename' (this file
840  * must exist).  As a security measure, this bails if the file is too
841  * large for a reasonable configuration file.  After the call to 'f'
842  * Augeas is closed.
843  */
844 static int
845 inspect_with_augeas (guestfs_h *g, struct inspect_fs *fs, const char *filename,
846                      int (*f) (guestfs_h *, struct inspect_fs *))
847 {
848   /* Security: Refuse to do this if filename is too large. */
849   int64_t size = guestfs_filesize (g, filename);
850   if (size == -1)
851     /* guestfs_filesize failed and has already set error in handle */
852     return -1;
853   if (size > MAX_AUGEAS_FILE_SIZE) {
854     error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
855            filename, size);
856     return -1;
857   }
858
859   /* If !feature_available (g, "augeas") then the next call will fail.
860    * Arguably we might want to fall back to a non-Augeas method in
861    * this case.
862    */
863   if (guestfs_aug_init (g, "/", 16|32) == -1)
864     return -1;
865
866   int r = -1;
867
868   /* Tell Augeas to only load one file (thanks RaphaĆ«l Pinson). */
869   char buf[strlen (filename) + 64];
870   snprintf (buf, strlen (filename) + 64, "/augeas/load//incl[. != \"%s\"]",
871             filename);
872   if (guestfs_aug_rm (g, buf) == -1)
873     goto out;
874
875   if (guestfs_aug_load (g) == -1)
876     goto out;
877
878   r = f (g, fs);
879
880  out:
881   guestfs_aug_close (g);
882
883   return r;
884 }
885
886 #endif /* defined(HAVE_HIVEX) */