58a49a64aa76e09889ce0f18552b88445f27d1ef
[libguestfs.git] / src / inspect.c
1 /* libguestfs
2  * Copyright (C) 2010 Red Hat Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <inttypes.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <sys/stat.h>
28
29 #ifdef HAVE_PCRE
30 #include <pcre.h>
31 #endif
32
33 #ifdef HAVE_HIVEX
34 #include <hivex.h>
35 #endif
36
37 #include "c-ctype.h"
38 #include "ignore-value.h"
39 #include "xstrtol.h"
40
41 #include "guestfs.h"
42 #include "guestfs-internal.h"
43 #include "guestfs-internal-actions.h"
44 #include "guestfs_protocol.h"
45
46 #if defined(HAVE_PCRE) && defined(HAVE_HIVEX)
47
48 /* Compile all the regular expressions once when the shared library is
49  * loaded.  PCRE is thread safe so we're supposedly OK here if
50  * multiple threads call into the libguestfs API functions below
51  * simultaneously.
52  */
53 static pcre *re_fedora;
54 static pcre *re_rhel_old;
55 static pcre *re_rhel;
56 static pcre *re_rhel_no_minor;
57 static pcre *re_major_minor;
58 static pcre *re_aug_seq;
59 static pcre *re_xdev;
60 static pcre *re_first_partition;
61 static pcre *re_freebsd;
62 static pcre *re_windows_version;
63
64 static void compile_regexps (void) __attribute__((constructor));
65 static void free_regexps (void) __attribute__((destructor));
66
67 static void
68 compile_regexps (void)
69 {
70   const char *err;
71   int offset;
72
73 #define COMPILE(re,pattern,options)                                     \
74   do {                                                                  \
75     re = pcre_compile ((pattern), (options), &err, &offset, NULL);      \
76     if (re == NULL) {                                                   \
77       ignore_value (write (2, err, strlen (err)));                      \
78       abort ();                                                         \
79     }                                                                   \
80   } while (0)
81
82   COMPILE (re_fedora, "Fedora release (\\d+)", 0);
83   COMPILE (re_rhel_old,
84            "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+).*Update (\\d+)", 0);
85   COMPILE (re_rhel,
86            "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+)\\.(\\d+)", 0);
87   COMPILE (re_rhel_no_minor,
88            "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+)", 0);
89   COMPILE (re_major_minor, "(\\d+)\\.(\\d+)", 0);
90   COMPILE (re_aug_seq, "/\\d+$", 0);
91   COMPILE (re_xdev, "^/dev/(?:h|s|v|xv)d([a-z]\\d*)$", 0);
92   COMPILE (re_first_partition, "^/dev/(?:h|s|v)d.1$", 0);
93   COMPILE (re_freebsd, "^/dev/ad(\\d+)s(\\d+)([a-z])$", 0);
94   COMPILE (re_windows_version, "^(\\d+)\\.(\\d+)", 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_first_partition);
108   pcre_free (re_freebsd);
109   pcre_free (re_windows_version);
110 }
111
112 /* The main inspection code. */
113 static int check_for_filesystem_on (guestfs_h *g, const char *device);
114
115 char **
116 guestfs__inspect_os (guestfs_h *g)
117 {
118   /* Remove any information previously stored in the handle. */
119   guestfs___free_inspect_info (g);
120
121   if (guestfs_umount_all (g) == -1)
122     return NULL;
123
124   /* Iterate over all possible devices.  Try to mount each
125    * (read-only).  Examine ones which contain filesystems and add that
126    * information to the handle.
127    */
128   /* Look to see if any devices directly contain filesystems (RHBZ#590167). */
129   char **devices;
130   devices = guestfs_list_devices (g);
131   if (devices == NULL)
132     return NULL;
133
134   size_t i;
135   for (i = 0; devices[i] != NULL; ++i) {
136     if (check_for_filesystem_on (g, devices[i]) == -1) {
137       guestfs___free_string_list (devices);
138       guestfs___free_inspect_info (g);
139       return NULL;
140     }
141   }
142   guestfs___free_string_list (devices);
143
144   /* Look at all partitions. */
145   char **partitions;
146   partitions = guestfs_list_partitions (g);
147   if (partitions == NULL) {
148     guestfs___free_inspect_info (g);
149     return NULL;
150   }
151
152   for (i = 0; partitions[i] != NULL; ++i) {
153     if (check_for_filesystem_on (g, partitions[i]) == -1) {
154       guestfs___free_string_list (partitions);
155       guestfs___free_inspect_info (g);
156       return NULL;
157     }
158   }
159   guestfs___free_string_list (partitions);
160
161   /* Look at all LVs. */
162   if (guestfs___feature_available (g, "lvm2")) {
163     char **lvs;
164     lvs = guestfs_lvs (g);
165     if (lvs == NULL) {
166       guestfs___free_inspect_info (g);
167       return NULL;
168     }
169
170     for (i = 0; lvs[i] != NULL; ++i) {
171       if (check_for_filesystem_on (g, lvs[i]) == -1) {
172         guestfs___free_string_list (lvs);
173         guestfs___free_inspect_info (g);
174         return NULL;
175       }
176     }
177     guestfs___free_string_list (lvs);
178   }
179
180   /* At this point we have, in the handle, a list of all filesystems
181    * found and data about each one.  Now we assemble the list of
182    * filesystems which are root devices and return that to the user.
183    * Fall through to guestfs__inspect_get_roots to do that.
184    */
185   char **ret = guestfs__inspect_get_roots (g);
186   if (ret == NULL)
187     guestfs___free_inspect_info (g);
188   return ret;
189 }
190
191 /* Find out if 'device' contains a filesystem.  If it does, add
192  * another entry in g->fses.
193  */
194 static int check_filesystem (guestfs_h *g, const char *device);
195 static int check_linux_root (guestfs_h *g, struct inspect_fs *fs);
196 static int check_freebsd_root (guestfs_h *g, struct inspect_fs *fs);
197 static void check_architecture (guestfs_h *g, struct inspect_fs *fs);
198 static int check_fstab (guestfs_h *g, struct inspect_fs *fs);
199 static int check_windows_root (guestfs_h *g, struct inspect_fs *fs);
200 static int check_windows_arch (guestfs_h *g, struct inspect_fs *fs);
201 static int check_windows_registry (guestfs_h *g, struct inspect_fs *fs);
202 static char *resolve_windows_path_silently (guestfs_h *g, const char *);
203 static int extend_fses (guestfs_h *g);
204 static int parse_unsigned_int (guestfs_h *g, const char *str);
205 static int add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
206                             const char *spec, const char *mp);
207 static char *resolve_fstab_device (guestfs_h *g, const char *spec);
208 static void check_package_format (guestfs_h *g, struct inspect_fs *fs);
209 static void check_package_management (guestfs_h *g, struct inspect_fs *fs);
210
211 static int
212 check_for_filesystem_on (guestfs_h *g, const char *device)
213 {
214   /* Get vfs-type in order to check if it's a Linux(?) swap device.
215    * If there's an error we should ignore it, so to do that we have to
216    * temporarily replace the error handler with a null one.
217    */
218   guestfs_error_handler_cb old_error_cb = g->error_cb;
219   g->error_cb = NULL;
220   char *vfs_type = guestfs_vfs_type (g, device);
221   g->error_cb = old_error_cb;
222
223   int is_swap = vfs_type && STREQ (vfs_type, "swap");
224
225   if (g->verbose)
226     fprintf (stderr, "check_for_filesystem_on: %s (%s)\n",
227              device, vfs_type ? vfs_type : "failed to get vfs type");
228
229   if (is_swap) {
230     free (vfs_type);
231     if (extend_fses (g) == -1)
232       return -1;
233     g->fses[g->nr_fses-1].is_swap = 1;
234     return 0;
235   }
236
237   /* Try mounting the device.  As above, ignore errors. */
238   g->error_cb = NULL;
239   int r = guestfs_mount_ro (g, device, "/");
240   if (r == -1 && vfs_type && STREQ (vfs_type, "ufs")) /* Hack for the *BSDs. */
241     r = guestfs_mount_vfs (g, "ro,ufstype=ufs2", "ufs", device, "/");
242   free (vfs_type);
243   g->error_cb = old_error_cb;
244   if (r == -1)
245     return 0;
246
247   /* Do the rest of the checks. */
248   r = check_filesystem (g, device);
249
250   /* Unmount the filesystem. */
251   if (guestfs_umount_all (g) == -1)
252     return -1;
253
254   return r;
255 }
256
257 static int
258 check_filesystem (guestfs_h *g, const char *device)
259 {
260   if (extend_fses (g) == -1)
261     return -1;
262
263   struct inspect_fs *fs = &g->fses[g->nr_fses-1];
264
265   fs->device = safe_strdup (g, device);
266   fs->is_mountable = 1;
267
268   /* Optimize some of the tests by avoiding multiple tests of the same thing. */
269   int is_dir_etc = guestfs_is_dir (g, "/etc") > 0;
270   int is_dir_bin = guestfs_is_dir (g, "/bin") > 0;
271   int is_dir_share = guestfs_is_dir (g, "/share") > 0;
272
273   /* Grub /boot? */
274   if (guestfs_is_file (g, "/grub/menu.lst") > 0 ||
275       guestfs_is_file (g, "/grub/grub.conf") > 0)
276     fs->content = FS_CONTENT_LINUX_BOOT;
277   /* FreeBSD root? */
278   else if (is_dir_etc &&
279            is_dir_bin &&
280            guestfs_is_file (g, "/etc/freebsd-update.conf") > 0 &&
281            guestfs_is_file (g, "/etc/fstab") > 0) {
282     /* Ignore /dev/sda1 which is a shadow of the real root filesystem
283      * that is probably /dev/sda5 (see:
284      * http://www.freebsd.org/doc/handbook/disk-organization.html)
285      */
286     if (match (g, device, re_first_partition))
287       return 0;
288
289     fs->is_root = 1;
290     fs->content = FS_CONTENT_FREEBSD_ROOT;
291     if (check_freebsd_root (g, fs) == -1)
292       return -1;
293   }
294   /* Linux root? */
295   else if (is_dir_etc &&
296            is_dir_bin &&
297            guestfs_is_file (g, "/etc/fstab") > 0) {
298     fs->is_root = 1;
299     fs->content = FS_CONTENT_LINUX_ROOT;
300     if (check_linux_root (g, fs) == -1)
301       return -1;
302   }
303   /* Linux /usr/local? */
304   else if (is_dir_etc &&
305            is_dir_bin &&
306            is_dir_share &&
307            guestfs_exists (g, "/local") == 0 &&
308            guestfs_is_file (g, "/etc/fstab") == 0)
309     fs->content = FS_CONTENT_LINUX_USR_LOCAL;
310   /* Linux /usr? */
311   else if (is_dir_etc &&
312            is_dir_bin &&
313            is_dir_share &&
314            guestfs_exists (g, "/local") > 0 &&
315            guestfs_is_file (g, "/etc/fstab") == 0)
316     fs->content = FS_CONTENT_LINUX_USR;
317   /* Linux /var? */
318   else if (guestfs_is_dir (g, "/log") > 0 &&
319            guestfs_is_dir (g, "/run") > 0 &&
320            guestfs_is_dir (g, "/spool") > 0)
321     fs->content = FS_CONTENT_LINUX_VAR;
322   /* Windows root? */
323   else if (guestfs_is_file (g, "/AUTOEXEC.BAT") > 0 ||
324            guestfs_is_file (g, "/autoexec.bat") > 0 ||
325            guestfs_is_dir (g, "/Program Files") > 0 ||
326            guestfs_is_dir (g, "/WINDOWS") > 0 ||
327            guestfs_is_dir (g, "/Windows") > 0 ||
328            guestfs_is_dir (g, "/windows") > 0 ||
329            guestfs_is_dir (g, "/WIN32") > 0 ||
330            guestfs_is_dir (g, "/Win32") > 0 ||
331            guestfs_is_dir (g, "/WINNT") > 0 ||
332            guestfs_is_file (g, "/boot.ini") > 0 ||
333            guestfs_is_file (g, "/ntldr") > 0) {
334     fs->is_root = 1;
335     fs->content = FS_CONTENT_WINDOWS_ROOT;
336     if (check_windows_root (g, fs) == -1)
337       return -1;
338   }
339
340   return 0;
341 }
342
343 /* Set fs->product_name to the first line of the release file. */
344 static int
345 parse_release_file (guestfs_h *g, struct inspect_fs *fs,
346                     const char *release_filename)
347 {
348   char **product_name = guestfs_head_n (g, 1, release_filename);
349   if (product_name == NULL)
350     return -1;
351   if (product_name[0] == NULL) {
352     error (g, "%s: file is empty", release_filename);
353     guestfs___free_string_list (product_name);
354     return -1;
355   }
356
357   /* Note that this string becomes owned by the handle and will
358    * be freed by guestfs___free_inspect_info.
359    */
360   fs->product_name = product_name[0];
361   free (product_name);
362
363   return 0;
364 }
365
366 /* Parse generic MAJOR.MINOR from the fs->product_name string. */
367 static int
368 parse_major_minor (guestfs_h *g, struct inspect_fs *fs)
369 {
370   char *major, *minor;
371
372   if (match2 (g, fs->product_name, re_major_minor, &major, &minor)) {
373     fs->major_version = parse_unsigned_int (g, major);
374     free (major);
375     if (fs->major_version == -1) {
376       free (minor);
377       return -1;
378     }
379     fs->minor_version = parse_unsigned_int (g, minor);
380     free (minor);
381     if (fs->minor_version == -1)
382       return -1;
383   }
384   return 0;
385 }
386
387 /* Ubuntu has /etc/lsb-release containing:
388  *   DISTRIB_ID=Ubuntu                                # Distro
389  *   DISTRIB_RELEASE=10.04                            # Version
390  *   DISTRIB_CODENAME=lucid
391  *   DISTRIB_DESCRIPTION="Ubuntu 10.04.1 LTS"         # Product name
392  * In theory other distros could have this LSB file, but none do.
393  */
394 static int
395 parse_lsb_release (guestfs_h *g, struct inspect_fs *fs)
396 {
397   char **lines;
398   size_t i;
399   int r = 0;
400
401   lines = guestfs_head_n (g, 10, "/etc/lsb-release");
402   if (lines == NULL)
403     return -1;
404
405   for (i = 0; lines[i] != NULL; ++i) {
406     if (fs->distro == 0 &&
407         STREQ (lines[i], "DISTRIB_ID=Ubuntu")) {
408       fs->distro = OS_DISTRO_UBUNTU;
409       r = 1;
410     }
411     else if (STRPREFIX (lines[i], "DISTRIB_RELEASE=")) {
412       char *major, *minor;
413       if (match2 (g, &lines[i][16], re_major_minor, &major, &minor)) {
414         fs->major_version = parse_unsigned_int (g, major);
415         free (major);
416         if (fs->major_version == -1) {
417           free (minor);
418           guestfs___free_string_list (lines);
419           return -1;
420         }
421         fs->minor_version = parse_unsigned_int (g, minor);
422         free (minor);
423         if (fs->minor_version == -1) {
424           guestfs___free_string_list (lines);
425           return -1;
426         }
427       }
428     }
429     else if (fs->product_name == NULL &&
430              (STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=\"") ||
431               STRPREFIX (lines[i], "DISTRIB_DESCRIPTION='"))) {
432       size_t len = strlen (lines[i]) - 21 - 1;
433       fs->product_name = safe_strndup (g, &lines[i][21], len);
434       r = 1;
435     }
436     else if (fs->product_name == NULL &&
437              STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=")) {
438       size_t len = strlen (lines[i]) - 20;
439       fs->product_name = safe_strndup (g, &lines[i][20], len);
440       r = 1;
441     }
442   }
443
444   guestfs___free_string_list (lines);
445   return r;
446 }
447
448 /* The currently mounted device is known to be a Linux root.  Try to
449  * determine from this the distro, version, etc.  Also parse
450  * /etc/fstab to determine the arrangement of mountpoints and
451  * associated devices.
452  */
453 static int
454 check_linux_root (guestfs_h *g, struct inspect_fs *fs)
455 {
456   int r;
457
458   fs->type = OS_TYPE_LINUX;
459
460   if (guestfs_exists (g, "/etc/lsb-release") > 0) {
461     r = parse_lsb_release (g, fs);
462     if (r == -1)        /* error */
463       return -1;
464     if (r == 1)         /* ok - detected the release from this file */
465       goto skip_release_checks;
466   }
467
468   if (guestfs_exists (g, "/etc/redhat-release") > 0) {
469     fs->distro = OS_DISTRO_REDHAT_BASED; /* Something generic Red Hat-like. */
470
471     if (parse_release_file (g, fs, "/etc/redhat-release") == -1)
472       return -1;
473
474     char *major, *minor;
475     if ((major = match1 (g, fs->product_name, re_fedora)) != NULL) {
476       fs->distro = OS_DISTRO_FEDORA;
477       fs->major_version = parse_unsigned_int (g, major);
478       free (major);
479       if (fs->major_version == -1)
480         return -1;
481     }
482     else if (match2 (g, fs->product_name, re_rhel_old, &major, &minor) ||
483              match2 (g, fs->product_name, re_rhel, &major, &minor)) {
484       fs->distro = OS_DISTRO_RHEL;
485       fs->major_version = parse_unsigned_int (g, major);
486       free (major);
487       if (fs->major_version == -1) {
488         free (minor);
489         return -1;
490       }
491       fs->minor_version = parse_unsigned_int (g, minor);
492       free (minor);
493       if (fs->minor_version == -1)
494         return -1;
495     }
496     else if ((major = match1 (g, fs->product_name, re_rhel_no_minor)) != NULL) {
497       fs->distro = OS_DISTRO_RHEL;
498       fs->major_version = parse_unsigned_int (g, major);
499       free (major);
500       if (fs->major_version == -1)
501         return -1;
502       fs->minor_version = 0;
503     }
504   }
505   else if (guestfs_exists (g, "/etc/debian_version") > 0) {
506     fs->distro = OS_DISTRO_DEBIAN;
507
508     if (parse_release_file (g, fs, "/etc/debian_version") == -1)
509       return -1;
510
511     if (parse_major_minor (g, fs) == -1)
512       return -1;
513   }
514   else if (guestfs_exists (g, "/etc/pardus-release") > 0) {
515     fs->distro = OS_DISTRO_PARDUS;
516
517     if (parse_release_file (g, fs, "/etc/pardus-release") == -1)
518       return -1;
519
520     if (parse_major_minor (g, fs) == -1)
521       return -1;
522   }
523   else if (guestfs_exists (g, "/etc/arch-release") > 0) {
524     fs->distro = OS_DISTRO_ARCHLINUX;
525
526     /* /etc/arch-release file is empty and I can't see a way to
527      * determine the actual release or product string.
528      */
529   }
530   else if (guestfs_exists (g, "/etc/gentoo-release") > 0) {
531     fs->distro = OS_DISTRO_GENTOO;
532
533     if (parse_release_file (g, fs, "/etc/gentoo-release") == -1)
534       return -1;
535
536     if (parse_major_minor (g, fs) == -1)
537       return -1;
538   }
539   else if (guestfs_exists (g, "/etc/meego-release") > 0) {
540     fs->distro = OS_DISTRO_MEEGO;
541
542     if (parse_release_file (g, fs, "/etc/meego-release") == -1)
543       return -1;
544
545     if (parse_major_minor (g, fs) == -1)
546       return -1;
547   }
548
549  skip_release_checks:;
550
551   /* If distro test above was successful, work out the package format. */
552   check_package_format (g, fs);
553   check_package_management (g, fs);
554
555   /* Determine the architecture. */
556   check_architecture (g, fs);
557
558   /* We already know /etc/fstab exists because it's part of the test
559    * for Linux root above.  We must now parse this file to determine
560    * which filesystems are used by the operating system and how they
561    * are mounted.
562    */
563   if (check_fstab (g, fs) == -1)
564     return -1;
565
566   return 0;
567 }
568
569 /* The currently mounted device is known to be a FreeBSD root. */
570 static int
571 check_freebsd_root (guestfs_h *g, struct inspect_fs *fs)
572 {
573   int r;
574
575   fs->type = OS_TYPE_FREEBSD;
576
577   /* FreeBSD has no authoritative version file.  The version number is
578    * in /etc/motd, which the system administrator might edit, but
579    * we'll use that anyway.
580    */
581
582   if (guestfs_exists (g, "/etc/motd") > 0) {
583     if (parse_release_file (g, fs, "/etc/motd") == -1)
584       return -1;
585
586     if (parse_major_minor (g, fs) == -1)
587       return -1;
588   }
589
590   /* Determine the architecture. */
591   check_architecture (g, fs);
592
593   /* We already know /etc/fstab exists because it's part of the test above. */
594   if (check_fstab (g, fs) == -1)
595     return -1;
596
597   return 0;
598 }
599
600 static void
601 check_architecture (guestfs_h *g, struct inspect_fs *fs)
602 {
603   const char *binaries[] =
604     { "/bin/bash", "/bin/ls", "/bin/echo", "/bin/rm", "/bin/sh" };
605   size_t i;
606
607   for (i = 0; i < sizeof binaries / sizeof binaries[0]; ++i) {
608     if (guestfs_is_file (g, binaries[i]) > 0) {
609       /* Ignore errors from file_architecture call. */
610       guestfs_error_handler_cb old_error_cb = g->error_cb;
611       g->error_cb = NULL;
612       char *arch = guestfs_file_architecture (g, binaries[i]);
613       g->error_cb = old_error_cb;
614
615       if (arch) {
616         /* String will be owned by handle, freed by
617          * guestfs___free_inspect_info.
618          */
619         fs->arch = arch;
620         break;
621       }
622     }
623   }
624 }
625
626 static int check_fstab_aug_open (guestfs_h *g, struct inspect_fs *fs);
627
628 static int
629 check_fstab (guestfs_h *g, struct inspect_fs *fs)
630 {
631   int r;
632
633   /* XXX What if !feature_available (g, "augeas")? */
634   if (guestfs_aug_init (g, "/", 16|32) == -1)
635     return -1;
636
637   /* Tell Augeas to only load /etc/fstab (thanks Raphaël Pinson). */
638   guestfs_aug_rm (g, "/augeas/load//incl[. != \"/etc/fstab\"]");
639   guestfs_aug_load (g);
640
641   r = check_fstab_aug_open (g, fs);
642   guestfs_aug_close (g);
643   if (r == -1)
644     return -1;
645
646   return 0;
647 }
648
649 static int
650 check_fstab_aug_open (guestfs_h *g, struct inspect_fs *fs)
651 {
652   char **lines = guestfs_aug_ls (g, "/files/etc/fstab");
653   if (lines == NULL)
654     return -1;
655
656   if (lines[0] == NULL) {
657     error (g, "could not parse /etc/fstab or empty file");
658     guestfs___free_string_list (lines);
659     return -1;
660   }
661
662   size_t i;
663   char augpath[256];
664   for (i = 0; lines[i] != NULL; ++i) {
665     /* Ignore comments.  Only care about sequence lines which
666      * match m{/\d+$}.
667      */
668     if (match (g, lines[i], re_aug_seq)) {
669       snprintf (augpath, sizeof augpath, "%s/spec", lines[i]);
670       char *spec = guestfs_aug_get (g, augpath);
671       if (spec == NULL) {
672         guestfs___free_string_list (lines);
673         return -1;
674       }
675
676       snprintf (augpath, sizeof augpath, "%s/file", lines[i]);
677       char *mp = guestfs_aug_get (g, augpath);
678       if (mp == NULL) {
679         guestfs___free_string_list (lines);
680         free (spec);
681         return -1;
682       }
683
684       int r = add_fstab_entry (g, fs, spec, mp);
685       free (spec);
686       free (mp);
687
688       if (r == -1) {
689         guestfs___free_string_list (lines);
690         return -1;
691       }
692     }
693   }
694
695   guestfs___free_string_list (lines);
696   return 0;
697 }
698
699 /* Add a filesystem and possibly a mountpoint entry for
700  * the root filesystem 'fs'.
701  *
702  * 'spec' is the fstab spec field, which might be a device name or a
703  * pseudodevice or 'UUID=...' or 'LABEL=...'.
704  *
705  * 'mp' is the mount point, which could also be 'swap' or 'none'.
706  */
707 static int
708 add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
709                  const char *spec, const char *mp)
710 {
711   /* Ignore certain mountpoints. */
712   if (STRPREFIX (mp, "/dev/") ||
713       STREQ (mp, "/dev") ||
714       STRPREFIX (mp, "/media/") ||
715       STRPREFIX (mp, "/proc/") ||
716       STREQ (mp, "/proc") ||
717       STRPREFIX (mp, "/selinux/") ||
718       STREQ (mp, "/selinux") ||
719       STRPREFIX (mp, "/sys/") ||
720       STREQ (mp, "/sys"))
721     return 0;
722
723   /* Ignore /dev/fd (floppy disks) (RHBZ#642929) and CD-ROM drives. */
724   if ((STRPREFIX (spec, "/dev/fd") && c_isdigit (spec[7])) ||
725       STREQ (spec, "/dev/floppy") ||
726       STREQ (spec, "/dev/cdrom"))
727     return 0;
728
729   /* Resolve UUID= and LABEL= to the actual device. */
730   char *device = NULL;
731   if (STRPREFIX (spec, "UUID="))
732     device = guestfs_findfs_uuid (g, &spec[5]);
733   else if (STRPREFIX (spec, "LABEL="))
734     device = guestfs_findfs_label (g, &spec[6]);
735   /* Ignore "/.swap" (Pardus) and pseudo-devices like "tmpfs". */
736   else if (STRPREFIX (spec, "/dev/"))
737     /* Resolve guest block device names. */
738     device = resolve_fstab_device (g, spec);
739
740   /* If we haven't resolved the device successfully by this point,
741    * we don't care, just ignore it.
742    */
743   if (device == NULL)
744     return 0;
745
746   char *mountpoint = safe_strdup (g, mp);
747
748   /* Add this to the fstab entry in 'fs'.
749    * Note these are further filtered by guestfs_inspect_get_mountpoints
750    * and guestfs_inspect_get_filesystems.
751    */
752   size_t n = fs->nr_fstab + 1;
753   struct inspect_fstab_entry *p;
754
755   p = realloc (fs->fstab, n * sizeof (struct inspect_fstab_entry));
756   if (p == NULL) {
757     perrorf (g, "realloc");
758     free (device);
759     free (mountpoint);
760     return -1;
761   }
762
763   fs->fstab = p;
764   fs->nr_fstab = n;
765
766   /* These are owned by the handle and freed by guestfs___free_inspect_info. */
767   fs->fstab[n-1].device = device;
768   fs->fstab[n-1].mountpoint = mountpoint;
769
770   if (g->verbose)
771     fprintf (stderr, "fstab: device=%s mountpoint=%s\n", device, mountpoint);
772
773   return 0;
774 }
775
776 /* Resolve block device name to the libguestfs device name, eg.
777  * /dev/xvdb1 => /dev/vdb1; and /dev/mapper/VG-LV => /dev/VG/LV.  This
778  * assumes that disks were added in the same order as they appear to
779  * the real VM, which is a reasonable assumption to make.  Return
780  * anything we don't recognize unchanged.
781  */
782 static char *
783 resolve_fstab_device (guestfs_h *g, const char *spec)
784 {
785   char *a1;
786   char *device = NULL;
787   char *bsddisk, *bsdslice, *bsdpart;
788
789   if (STRPREFIX (spec, "/dev/mapper/")) {
790     /* LVM2 does some strange munging on /dev/mapper paths for VGs and
791      * LVs which contain '-' character:
792      *
793      * ><fs> lvcreate LV--test VG--test 32
794      * ><fs> debug ls /dev/mapper
795      * VG----test-LV----test
796      *
797      * This makes it impossible to reverse those paths directly, so
798      * we have implemented lvm_canonical_lv_name in the daemon.
799      */
800     device = guestfs_lvm_canonical_lv_name (g, spec);
801   }
802   else if ((a1 = match1 (g, spec, re_xdev)) != NULL) {
803     char **devices = guestfs_list_devices (g);
804     if (devices == NULL)
805       return NULL;
806
807     size_t count;
808     for (count = 0; devices[count] != NULL; count++)
809       ;
810
811     size_t i = a1[0] - 'a'; /* a1[0] is always [a-z] because of regex. */
812     if (i < count) {
813       size_t len = strlen (devices[i]) + strlen (a1) + 16;
814       device = safe_malloc (g, len);
815       snprintf (device, len, "%s%s", devices[i], &a1[1]);
816     }
817
818     free (a1);
819     guestfs___free_string_list (devices);
820   }
821   else if (match3 (g, spec, re_freebsd, &bsddisk, &bsdslice, &bsdpart)) {
822     /* FreeBSD disks are organized quite differently.  See:
823      * http://www.freebsd.org/doc/handbook/disk-organization.html
824      * FreeBSD "partitions" are exposed as quasi-extended partitions
825      * numbered from 5 in Linux.  I have no idea what happens when you
826      * have multiple "slices" (the FreeBSD term for MBR partitions).
827      */
828     int disk = parse_unsigned_int (g, bsddisk);
829     int slice = parse_unsigned_int (g, bsdslice);
830     int part = bsdpart[0] - 'a' /* counting from 0 */;
831     free (bsddisk);
832     free (bsdslice);
833     free (bsdpart);
834
835     if (disk == -1 || disk > 26 ||
836         slice <= 0 || slice > 1 /* > 4 .. see comment above */ ||
837         part < 0 || part >= 26)
838       goto out;
839
840     device = safe_asprintf (g, "/dev/sd%c%d", disk + 'a', part + 5);
841   }
842
843  out:
844   /* Didn't match device pattern, return original spec unchanged. */
845   if (device == NULL)
846     device = safe_strdup (g, spec);
847
848   return device;
849 }
850
851 /* XXX Handling of boot.ini in the Perl version was pretty broken.  It
852  * essentially didn't do anything for modern Windows guests.
853  * Therefore I've omitted all that code.
854  */
855 static int
856 check_windows_root (guestfs_h *g, struct inspect_fs *fs)
857 {
858   fs->type = OS_TYPE_WINDOWS;
859   fs->distro = OS_DISTRO_WINDOWS;
860
861   /* Try to find Windows systemroot using some common locations. */
862   const char *systemroots[] =
863     { "/windows", "/winnt", "/win32", "/win" };
864   size_t i;
865   char *systemroot = NULL;
866   for (i = 0;
867        systemroot == NULL && i < sizeof systemroots / sizeof systemroots[0];
868        ++i) {
869     systemroot = resolve_windows_path_silently (g, systemroots[i]);
870   }
871
872   if (!systemroot) {
873     error (g, _("cannot resolve Windows %%SYSTEMROOT%%"));
874     return -1;
875   }
876
877   if (g->verbose)
878     fprintf (stderr, "windows %%SYSTEMROOT%% = %s", systemroot);
879
880   /* Freed by guestfs___free_inspect_info. */
881   fs->windows_systemroot = systemroot;
882
883   if (check_windows_arch (g, fs) == -1)
884     return -1;
885
886   if (check_windows_registry (g, fs) == -1)
887     return -1;
888
889   check_package_format (g, fs);
890   check_package_management (g, fs);
891
892   return 0;
893 }
894
895 static int
896 check_windows_arch (guestfs_h *g, struct inspect_fs *fs)
897 {
898   size_t len = strlen (fs->windows_systemroot) + 32;
899   char cmd_exe[len];
900   snprintf (cmd_exe, len, "%s/system32/cmd.exe", fs->windows_systemroot);
901
902   char *cmd_exe_path = resolve_windows_path_silently (g, cmd_exe);
903   if (!cmd_exe_path)
904     return 0;
905
906   char *arch = guestfs_file_architecture (g, cmd_exe_path);
907   free (cmd_exe_path);
908
909   if (arch)
910     fs->arch = arch;        /* freed by guestfs___free_inspect_info */
911
912   return 0;
913 }
914
915 /* At the moment, pull just the ProductName and version numbers from
916  * the registry.  In future there is a case for making many more
917  * registry fields available to callers.
918  */
919 static int
920 check_windows_registry (guestfs_h *g, struct inspect_fs *fs)
921 {
922   TMP_TEMPLATE_ON_STACK (dir);
923 #define dir_len (strlen (dir))
924 #define software_hive_len (dir_len + 16)
925   char software_hive[software_hive_len];
926 #define cmd_len (dir_len + 16)
927   char cmd[cmd_len];
928
929   size_t len = strlen (fs->windows_systemroot) + 64;
930   char software[len];
931   snprintf (software, len, "%s/system32/config/software",
932             fs->windows_systemroot);
933
934   char *software_path = resolve_windows_path_silently (g, software);
935   if (!software_path)
936     /* If the software hive doesn't exist, just accept that we cannot
937      * find product_name etc.
938      */
939     return 0;
940
941   int ret = -1;
942   hive_h *h = NULL;
943   hive_value_h *values = NULL;
944
945   if (mkdtemp (dir) == NULL) {
946     perrorf (g, "mkdtemp");
947     goto out;
948   }
949
950   snprintf (software_hive, software_hive_len, "%s/software", dir);
951
952   if (guestfs_download (g, software_path, software_hive) == -1)
953     goto out;
954
955   h = hivex_open (software_hive, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
956   if (h == NULL) {
957     perrorf (g, "hivex_open");
958     goto out;
959   }
960
961   hive_node_h node = hivex_root (h);
962   const char *hivepath[] =
963     { "Microsoft", "Windows NT", "CurrentVersion" };
964   size_t i;
965   for (i = 0;
966        node != 0 && i < sizeof hivepath / sizeof hivepath[0];
967        ++i) {
968     node = hivex_node_get_child (h, node, hivepath[i]);
969   }
970
971   if (node == 0) {
972     perrorf (g, "hivex: cannot locate HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
973     goto out;
974   }
975
976   values = hivex_node_values (h, node);
977
978   for (i = 0; values[i] != 0; ++i) {
979     char *key = hivex_value_key (h, values[i]);
980     if (key == NULL) {
981       perrorf (g, "hivex_value_key");
982       goto out;
983     }
984
985     if (STRCASEEQ (key, "ProductName")) {
986       fs->product_name = hivex_value_string (h, values[i]);
987       if (!fs->product_name) {
988         perrorf (g, "hivex_value_string");
989         free (key);
990         goto out;
991       }
992     }
993     else if (STRCASEEQ (key, "CurrentVersion")) {
994       char *version = hivex_value_string (h, values[i]);
995       if (!version) {
996         perrorf (g, "hivex_value_string");
997         free (key);
998         goto out;
999       }
1000       char *major, *minor;
1001       if (match2 (g, version, re_windows_version, &major, &minor)) {
1002         fs->major_version = parse_unsigned_int (g, major);
1003         free (major);
1004         if (fs->major_version == -1) {
1005           free (minor);
1006           free (key);
1007           free (version);
1008           goto out;
1009         }
1010         fs->minor_version = parse_unsigned_int (g, minor);
1011         free (minor);
1012         if (fs->minor_version == -1) {
1013           free (key);
1014           free (version);
1015           return -1;
1016         }
1017       }
1018
1019       free (version);
1020     }
1021
1022     free (key);
1023   }
1024
1025   ret = 0;
1026
1027  out:
1028   if (h) hivex_close (h);
1029   free (values);
1030   free (software_path);
1031
1032   /* Free up the temporary directory.  Note the directory name cannot
1033    * contain shell meta-characters because of the way it was
1034    * constructed above.
1035    */
1036   snprintf (cmd, cmd_len, "rm -rf %s", dir);
1037   ignore_value (system (cmd));
1038 #undef dir_len
1039 #undef software_hive_len
1040 #undef cmd_len
1041
1042   return ret;
1043 }
1044
1045 static char *
1046 resolve_windows_path_silently (guestfs_h *g, const char *path)
1047 {
1048   guestfs_error_handler_cb old_error_cb = g->error_cb;
1049   g->error_cb = NULL;
1050   char *ret = guestfs_case_sensitive_path (g, path);
1051   g->error_cb = old_error_cb;
1052   return ret;
1053 }
1054
1055 static int
1056 extend_fses (guestfs_h *g)
1057 {
1058   size_t n = g->nr_fses + 1;
1059   struct inspect_fs *p;
1060
1061   p = realloc (g->fses, n * sizeof (struct inspect_fs));
1062   if (p == NULL) {
1063     perrorf (g, "realloc");
1064     return -1;
1065   }
1066
1067   g->fses = p;
1068   g->nr_fses = n;
1069
1070   memset (&g->fses[n-1], 0, sizeof (struct inspect_fs));
1071
1072   return 0;
1073 }
1074
1075 /* Parse small, unsigned ints, as used in version numbers. */
1076 static int
1077 parse_unsigned_int (guestfs_h *g, const char *str)
1078 {
1079   long ret;
1080   int r = xstrtol (str, NULL, 10, &ret, "");
1081   if (r != LONGINT_OK) {
1082     error (g, "could not parse integer in version number: %s", str);
1083     return -1;
1084   }
1085   return ret;
1086 }
1087
1088 /* At the moment, package format and package management is just a
1089  * simple function of the distro and major_version fields, so these
1090  * can never return an error.  We might be cleverer in future.
1091  */
1092 static void
1093 check_package_format (guestfs_h *g, struct inspect_fs *fs)
1094 {
1095   switch (fs->distro) {
1096   case OS_DISTRO_FEDORA:
1097   case OS_DISTRO_MEEGO:
1098   case OS_DISTRO_REDHAT_BASED:
1099   case OS_DISTRO_RHEL:
1100     fs->package_format = OS_PACKAGE_FORMAT_RPM;
1101     break;
1102
1103   case OS_DISTRO_DEBIAN:
1104   case OS_DISTRO_UBUNTU:
1105     fs->package_format = OS_PACKAGE_FORMAT_DEB;
1106     break;
1107
1108   case OS_DISTRO_ARCHLINUX:
1109     fs->package_format = OS_PACKAGE_FORMAT_PACMAN;
1110     break;
1111   case OS_DISTRO_GENTOO:
1112     fs->package_format = OS_PACKAGE_FORMAT_EBUILD;
1113     break;
1114   case OS_DISTRO_PARDUS:
1115     fs->package_format = OS_PACKAGE_FORMAT_PISI;
1116     break;
1117
1118   case OS_DISTRO_WINDOWS:
1119   case OS_DISTRO_UNKNOWN:
1120   default:
1121     fs->package_format = OS_PACKAGE_FORMAT_UNKNOWN;
1122     break;
1123   }
1124 }
1125
1126 static void
1127 check_package_management (guestfs_h *g, struct inspect_fs *fs)
1128 {
1129   switch (fs->distro) {
1130   case OS_DISTRO_FEDORA:
1131   case OS_DISTRO_MEEGO:
1132     fs->package_management = OS_PACKAGE_MANAGEMENT_YUM;
1133     break;
1134
1135   case OS_DISTRO_REDHAT_BASED:
1136   case OS_DISTRO_RHEL:
1137     if (fs->major_version >= 5)
1138       fs->package_management = OS_PACKAGE_MANAGEMENT_YUM;
1139     else
1140       fs->package_management = OS_PACKAGE_MANAGEMENT_UP2DATE;
1141     break;
1142
1143   case OS_DISTRO_DEBIAN:
1144   case OS_DISTRO_UBUNTU:
1145     fs->package_management = OS_PACKAGE_MANAGEMENT_APT;
1146     break;
1147
1148   case OS_DISTRO_ARCHLINUX:
1149     fs->package_management = OS_PACKAGE_MANAGEMENT_PACMAN;
1150     break;
1151   case OS_DISTRO_GENTOO:
1152     fs->package_management = OS_PACKAGE_MANAGEMENT_PORTAGE;
1153     break;
1154   case OS_DISTRO_PARDUS:
1155     fs->package_management = OS_PACKAGE_MANAGEMENT_PISI;
1156     break;
1157
1158   case OS_DISTRO_WINDOWS:
1159   case OS_DISTRO_UNKNOWN:
1160   default:
1161     fs->package_management = OS_PACKAGE_MANAGEMENT_UNKNOWN;
1162     break;
1163   }
1164 }
1165
1166 static struct inspect_fs *
1167 search_for_root (guestfs_h *g, const char *root)
1168 {
1169   if (g->nr_fses == 0) {
1170     error (g, _("no inspection data: call guestfs_inspect_os first"));
1171     return NULL;
1172   }
1173
1174   size_t i;
1175   struct inspect_fs *fs;
1176   for (i = 0; i < g->nr_fses; ++i) {
1177     fs = &g->fses[i];
1178     if (fs->is_root && STREQ (root, fs->device))
1179       return fs;
1180   }
1181
1182   error (g, _("%s: root device not found: only call this function with a root device previously returned by guestfs_inspect_os"),
1183          root);
1184   return NULL;
1185 }
1186
1187 char **
1188 guestfs__inspect_get_roots (guestfs_h *g)
1189 {
1190   /* NB. Doesn't matter if g->nr_fses == 0.  We just return an empty
1191    * list in this case.
1192    */
1193
1194   size_t i;
1195   size_t count = 0;
1196   for (i = 0; i < g->nr_fses; ++i)
1197     if (g->fses[i].is_root)
1198       count++;
1199
1200   char **ret = calloc (count+1, sizeof (char *));
1201   if (ret == NULL) {
1202     perrorf (g, "calloc");
1203     return NULL;
1204   }
1205
1206   count = 0;
1207   for (i = 0; i < g->nr_fses; ++i) {
1208     if (g->fses[i].is_root) {
1209       ret[count] = safe_strdup (g, g->fses[i].device);
1210       count++;
1211     }
1212   }
1213   ret[count] = NULL;
1214
1215   return ret;
1216 }
1217
1218 char *
1219 guestfs__inspect_get_type (guestfs_h *g, const char *root)
1220 {
1221   struct inspect_fs *fs = search_for_root (g, root);
1222   if (!fs)
1223     return NULL;
1224
1225   char *ret;
1226   switch (fs->type) {
1227   case OS_TYPE_LINUX: ret = safe_strdup (g, "linux"); break;
1228   case OS_TYPE_WINDOWS: ret = safe_strdup (g, "windows"); break;
1229   case OS_TYPE_FREEBSD: ret = safe_strdup (g, "freebsd"); break;
1230   case OS_TYPE_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1231   }
1232
1233   return ret;
1234 }
1235
1236 char *
1237 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
1238 {
1239   struct inspect_fs *fs = search_for_root (g, root);
1240   if (!fs)
1241     return NULL;
1242
1243   return safe_strdup (g, fs->arch ? : "unknown");
1244 }
1245
1246 char *
1247 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
1248 {
1249   struct inspect_fs *fs = search_for_root (g, root);
1250   if (!fs)
1251     return NULL;
1252
1253   char *ret;
1254   switch (fs->distro) {
1255   case OS_DISTRO_ARCHLINUX: ret = safe_strdup (g, "archlinux"); break;
1256   case OS_DISTRO_DEBIAN: ret = safe_strdup (g, "debian"); break;
1257   case OS_DISTRO_FEDORA: ret = safe_strdup (g, "fedora"); break;
1258   case OS_DISTRO_GENTOO: ret = safe_strdup (g, "gentoo"); break;
1259   case OS_DISTRO_MEEGO: ret = safe_strdup (g, "meego"); break;
1260   case OS_DISTRO_PARDUS: ret = safe_strdup (g, "pardus"); break;
1261   case OS_DISTRO_REDHAT_BASED: ret = safe_strdup (g, "redhat-based"); break;
1262   case OS_DISTRO_RHEL: ret = safe_strdup (g, "rhel"); break;
1263   case OS_DISTRO_WINDOWS: ret = safe_strdup (g, "windows"); break;
1264   case OS_DISTRO_UBUNTU: ret = safe_strdup (g, "ubuntu"); break;
1265   case OS_DISTRO_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1266   }
1267
1268   return ret;
1269 }
1270
1271 int
1272 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
1273 {
1274   struct inspect_fs *fs = search_for_root (g, root);
1275   if (!fs)
1276     return -1;
1277
1278   return fs->major_version;
1279 }
1280
1281 int
1282 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
1283 {
1284   struct inspect_fs *fs = search_for_root (g, root);
1285   if (!fs)
1286     return -1;
1287
1288   return fs->minor_version;
1289 }
1290
1291 char *
1292 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
1293 {
1294   struct inspect_fs *fs = search_for_root (g, root);
1295   if (!fs)
1296     return NULL;
1297
1298   return safe_strdup (g, fs->product_name ? : "unknown");
1299 }
1300
1301 char *
1302 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
1303 {
1304   struct inspect_fs *fs = search_for_root (g, root);
1305   if (!fs)
1306     return NULL;
1307
1308   if (!fs->windows_systemroot) {
1309     error (g, _("not a Windows guest, or systemroot could not be determined"));
1310     return NULL;
1311   }
1312
1313   return safe_strdup (g, fs->windows_systemroot);
1314 }
1315
1316 char **
1317 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
1318 {
1319   struct inspect_fs *fs = search_for_root (g, root);
1320   if (!fs)
1321     return NULL;
1322
1323   char **ret;
1324
1325   /* If no fstab information (Windows) return just the root. */
1326   if (fs->nr_fstab == 0) {
1327     ret = calloc (3, sizeof (char *));
1328     ret[0] = safe_strdup (g, "/");
1329     ret[1] = safe_strdup (g, root);
1330     ret[2] = NULL;
1331     return ret;
1332   }
1333
1334 #define CRITERION fs->fstab[i].mountpoint[0] == '/'
1335   size_t i, count = 0;
1336   for (i = 0; i < fs->nr_fstab; ++i)
1337     if (CRITERION)
1338       count++;
1339
1340   /* Hashtables have 2N+1 entries. */
1341   ret = calloc (2*count+1, sizeof (char *));
1342   if (ret == NULL) {
1343     perrorf (g, "calloc");
1344     return NULL;
1345   }
1346
1347   count = 0;
1348   for (i = 0; i < fs->nr_fstab; ++i)
1349     if (CRITERION) {
1350       ret[2*count] = safe_strdup (g, fs->fstab[i].mountpoint);
1351       ret[2*count+1] = safe_strdup (g, fs->fstab[i].device);
1352       count++;
1353     }
1354 #undef CRITERION
1355
1356   return ret;
1357 }
1358
1359 char **
1360 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
1361 {
1362   struct inspect_fs *fs = search_for_root (g, root);
1363   if (!fs)
1364     return NULL;
1365
1366   char **ret;
1367
1368   /* If no fstab information (Windows) return just the root. */
1369   if (fs->nr_fstab == 0) {
1370     ret = calloc (2, sizeof (char *));
1371     ret[0] = safe_strdup (g, root);
1372     ret[1] = NULL;
1373     return ret;
1374   }
1375
1376   ret = calloc (fs->nr_fstab + 1, sizeof (char *));
1377   if (ret == NULL) {
1378     perrorf (g, "calloc");
1379     return NULL;
1380   }
1381
1382   size_t i;
1383   for (i = 0; i < fs->nr_fstab; ++i)
1384     ret[i] = safe_strdup (g, fs->fstab[i].device);
1385
1386   return ret;
1387 }
1388
1389 char *
1390 guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
1391 {
1392   struct inspect_fs *fs = search_for_root (g, root);
1393   if (!fs)
1394     return NULL;
1395
1396   char *ret;
1397   switch (fs->package_format) {
1398   case OS_PACKAGE_FORMAT_RPM: ret = safe_strdup (g, "rpm"); break;
1399   case OS_PACKAGE_FORMAT_DEB: ret = safe_strdup (g, "deb"); break;
1400   case OS_PACKAGE_FORMAT_PACMAN: ret = safe_strdup (g, "pacman"); break;
1401   case OS_PACKAGE_FORMAT_EBUILD: ret = safe_strdup (g, "ebuild"); break;
1402   case OS_PACKAGE_FORMAT_PISI: ret = safe_strdup (g, "pisi"); break;
1403   case OS_PACKAGE_FORMAT_UNKNOWN:
1404   default:
1405     ret = safe_strdup (g, "unknown");
1406     break;
1407   }
1408
1409   return ret;
1410 }
1411
1412 char *
1413 guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
1414 {
1415   struct inspect_fs *fs = search_for_root (g, root);
1416   if (!fs)
1417     return NULL;
1418
1419   char *ret;
1420   switch (fs->package_management) {
1421   case OS_PACKAGE_MANAGEMENT_YUM: ret = safe_strdup (g, "yum"); break;
1422   case OS_PACKAGE_MANAGEMENT_UP2DATE: ret = safe_strdup (g, "up2date"); break;
1423   case OS_PACKAGE_MANAGEMENT_APT: ret = safe_strdup (g, "apt"); break;
1424   case OS_PACKAGE_MANAGEMENT_PACMAN: ret = safe_strdup (g, "pacman"); break;
1425   case OS_PACKAGE_MANAGEMENT_PORTAGE: ret = safe_strdup (g, "portage"); break;
1426   case OS_PACKAGE_MANAGEMENT_PISI: ret = safe_strdup (g, "pisi"); break;
1427   case OS_PACKAGE_MANAGEMENT_UNKNOWN:
1428   default:
1429     ret = safe_strdup (g, "unknown");
1430     break;
1431   }
1432
1433   return ret;
1434 }
1435
1436 #else /* no PCRE or hivex at compile time */
1437
1438 /* XXX These functions should be in an optgroup. */
1439
1440 #define NOT_IMPL(r)                                                     \
1441   error (g, _("inspection API not available since this version of libguestfs was compiled without PCRE or hivex libraries")); \
1442   return r
1443
1444 char **
1445 guestfs__inspect_os (guestfs_h *g)
1446 {
1447   NOT_IMPL(NULL);
1448 }
1449
1450 char **
1451 guestfs__inspect_get_roots (guestfs_h *g)
1452 {
1453   NOT_IMPL(NULL);
1454 }
1455
1456 char *
1457 guestfs__inspect_get_type (guestfs_h *g, const char *root)
1458 {
1459   NOT_IMPL(NULL);
1460 }
1461
1462 char *
1463 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
1464 {
1465   NOT_IMPL(NULL);
1466 }
1467
1468 char *
1469 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
1470 {
1471   NOT_IMPL(NULL);
1472 }
1473
1474 int
1475 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
1476 {
1477   NOT_IMPL(-1);
1478 }
1479
1480 int
1481 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
1482 {
1483   NOT_IMPL(-1);
1484 }
1485
1486 char *
1487 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
1488 {
1489   NOT_IMPL(NULL);
1490 }
1491
1492 char *
1493 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
1494 {
1495   NOT_IMPL(NULL);
1496 }
1497
1498 char **
1499 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
1500 {
1501   NOT_IMPL(NULL);
1502 }
1503
1504 char **
1505 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
1506 {
1507   NOT_IMPL(NULL);
1508 }
1509
1510 char *
1511 guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
1512 {
1513   NOT_IMPL(NULL);
1514 }
1515
1516 char *
1517 guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
1518 {
1519   NOT_IMPL(NULL);
1520 }
1521
1522 #endif /* no PCRE or hivex at compile time */
1523
1524 void
1525 guestfs___free_inspect_info (guestfs_h *g)
1526 {
1527   size_t i;
1528   for (i = 0; i < g->nr_fses; ++i) {
1529     free (g->fses[i].device);
1530     free (g->fses[i].product_name);
1531     free (g->fses[i].arch);
1532     free (g->fses[i].windows_systemroot);
1533     size_t j;
1534     for (j = 0; j < g->fses[i].nr_fstab; ++j) {
1535       free (g->fses[i].fstab[j].device);
1536       free (g->fses[i].fstab[j].mountpoint);
1537     }
1538     free (g->fses[i].fstab);
1539   }
1540   free (g->fses);
1541   g->nr_fses = 0;
1542   g->fses = NULL;
1543 }
1544
1545 /* In the Perl code this is a public function. */
1546 int
1547 guestfs___feature_available (guestfs_h *g, const char *feature)
1548 {
1549   /* If there's an error we should ignore it, so to do that we have to
1550    * temporarily replace the error handler with a null one.
1551    */
1552   guestfs_error_handler_cb old_error_cb = g->error_cb;
1553   g->error_cb = NULL;
1554
1555   const char *groups[] = { feature, NULL };
1556   int r = guestfs_available (g, (char * const *) groups);
1557
1558   g->error_cb = old_error_cb;
1559
1560   return r == 0 ? 1 : 0;
1561 }
1562
1563 #ifdef HAVE_PCRE
1564
1565 /* Match a regular expression which contains no captures.  Returns
1566  * true if it matches or false if it doesn't.
1567  */
1568 int
1569 guestfs___match (guestfs_h *g, const char *str, const pcre *re)
1570 {
1571   size_t len = strlen (str);
1572   int vec[30], r;
1573
1574   r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
1575   if (r == PCRE_ERROR_NOMATCH)
1576     return 0;
1577   if (r != 1) {
1578     /* Internal error -- should not happen. */
1579     fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1580              __FILE__, __func__, r, str);
1581     return 0;
1582   }
1583
1584   return 1;
1585 }
1586
1587 /* Match a regular expression which contains exactly one capture.  If
1588  * the string matches, return the capture, otherwise return NULL.  The
1589  * caller must free the result.
1590  */
1591 char *
1592 guestfs___match1 (guestfs_h *g, const char *str, const pcre *re)
1593 {
1594   size_t len = strlen (str);
1595   int vec[30], r;
1596
1597   r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
1598   if (r == PCRE_ERROR_NOMATCH)
1599     return NULL;
1600   if (r != 2) {
1601     /* Internal error -- should not happen. */
1602     fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1603              __FILE__, __func__, r, str);
1604     return NULL;
1605   }
1606
1607   return safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
1608 }
1609
1610 /* Match a regular expression which contains exactly two captures. */
1611 int
1612 guestfs___match2 (guestfs_h *g, const char *str, const pcre *re,
1613                   char **ret1, char **ret2)
1614 {
1615   size_t len = strlen (str);
1616   int vec[30], r;
1617
1618   r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
1619   if (r == PCRE_ERROR_NOMATCH)
1620     return 0;
1621   if (r != 3) {
1622     /* Internal error -- should not happen. */
1623     fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1624              __FILE__, __func__, r, str);
1625     return 0;
1626   }
1627
1628   *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
1629   *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
1630
1631   return 1;
1632 }
1633
1634 /* Match a regular expression which contains exactly three captures. */
1635 int
1636 guestfs___match3 (guestfs_h *g, const char *str, const pcre *re,
1637                   char **ret1, char **ret2, char **ret3)
1638 {
1639   size_t len = strlen (str);
1640   int vec[30], r;
1641
1642   r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
1643   if (r == PCRE_ERROR_NOMATCH)
1644     return 0;
1645   if (r != 4) {
1646     /* Internal error -- should not happen. */
1647     fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1648              __FILE__, __func__, r, str);
1649     return 0;
1650   }
1651
1652   *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
1653   *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
1654   *ret3 = safe_strndup (g, &str[vec[6]], vec[7]-vec[6]);
1655
1656   return 1;
1657 }
1658
1659 #endif /* HAVE_PCRE */