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