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