2 * Copyright (C) 2010 Red Hat Inc.
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.
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.
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
38 #include "ignore-value.h"
42 #include "guestfs-internal.h"
43 #include "guestfs-internal-actions.h"
44 #include "guestfs_protocol.h"
46 #if defined(HAVE_PCRE) && defined(HAVE_HIVEX)
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
53 static pcre *re_fedora;
54 static pcre *re_rhel_old;
56 static pcre *re_rhel_no_minor;
57 static pcre *re_major_minor;
58 static pcre *re_aug_seq;
60 static pcre *re_first_partition;
61 static pcre *re_freebsd;
62 static pcre *re_windows_version;
64 static void compile_regexps (void) __attribute__((constructor));
65 static void free_regexps (void) __attribute__((destructor));
68 compile_regexps (void)
73 #define COMPILE(re,pattern,options) \
75 re = pcre_compile ((pattern), (options), &err, &offset, NULL); \
77 ignore_value (write (2, err, strlen (err))); \
82 COMPILE (re_fedora, "Fedora release (\\d+)", 0);
84 "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+).*Update (\\d+)", 0);
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);
100 pcre_free (re_fedora);
101 pcre_free (re_rhel_old);
103 pcre_free (re_rhel_no_minor);
104 pcre_free (re_major_minor);
105 pcre_free (re_aug_seq);
107 pcre_free (re_first_partition);
108 pcre_free (re_freebsd);
109 pcre_free (re_windows_version);
112 /* The main inspection code. */
113 static int check_for_filesystem_on (guestfs_h *g, const char *device);
116 guestfs__inspect_os (guestfs_h *g)
118 /* Remove any information previously stored in the handle. */
119 guestfs___free_inspect_info (g);
121 if (guestfs_umount_all (g) == -1)
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.
128 /* Look to see if any devices directly contain filesystems (RHBZ#590167). */
130 devices = guestfs_list_devices (g);
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);
142 guestfs___free_string_list (devices);
144 /* Look at all partitions. */
146 partitions = guestfs_list_partitions (g);
147 if (partitions == NULL) {
148 guestfs___free_inspect_info (g);
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);
159 guestfs___free_string_list (partitions);
161 /* Look at all LVs. */
162 if (guestfs___feature_available (g, "lvm2")) {
164 lvs = guestfs_lvs (g);
166 guestfs___free_inspect_info (g);
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);
177 guestfs___free_string_list (lvs);
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.
185 char **ret = guestfs__inspect_get_roots (g);
187 guestfs___free_inspect_info (g);
191 /* Find out if 'device' contains a filesystem. If it does, add
192 * another entry in g->fses.
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);
212 check_for_filesystem_on (guestfs_h *g, const char *device)
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.
218 guestfs_error_handler_cb old_error_cb = g->error_cb;
220 char *vfs_type = guestfs_vfs_type (g, device);
221 g->error_cb = old_error_cb;
223 int is_swap = vfs_type && STREQ (vfs_type, "swap");
226 fprintf (stderr, "check_for_filesystem_on: %s (%s)\n",
227 device, vfs_type ? vfs_type : "failed to get vfs type");
231 if (extend_fses (g) == -1)
233 g->fses[g->nr_fses-1].is_swap = 1;
237 /* Try mounting the device. As above, ignore errors. */
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, "/");
243 g->error_cb = old_error_cb;
247 /* Do the rest of the checks. */
248 r = check_filesystem (g, device);
250 /* Unmount the filesystem. */
251 if (guestfs_umount_all (g) == -1)
258 check_filesystem (guestfs_h *g, const char *device)
260 if (extend_fses (g) == -1)
263 struct inspect_fs *fs = &g->fses[g->nr_fses-1];
265 fs->device = safe_strdup (g, device);
266 fs->is_mountable = 1;
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;
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;
278 else if (is_dir_etc &&
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)
286 if (match (g, device, re_first_partition))
290 fs->content = FS_CONTENT_FREEBSD_ROOT;
291 if (check_freebsd_root (g, fs) == -1)
295 else if (is_dir_etc &&
297 guestfs_is_file (g, "/etc/fstab") > 0) {
299 fs->content = FS_CONTENT_LINUX_ROOT;
300 if (check_linux_root (g, fs) == -1)
303 /* Linux /usr/local? */
304 else if (is_dir_etc &&
307 guestfs_exists (g, "/local") == 0 &&
308 guestfs_is_file (g, "/etc/fstab") == 0)
309 fs->content = FS_CONTENT_LINUX_USR_LOCAL;
311 else if (is_dir_etc &&
314 guestfs_exists (g, "/local") > 0 &&
315 guestfs_is_file (g, "/etc/fstab") == 0)
316 fs->content = FS_CONTENT_LINUX_USR;
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;
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) {
335 fs->content = FS_CONTENT_WINDOWS_ROOT;
336 if (check_windows_root (g, fs) == -1)
343 /* Set fs->product_name to the first line of the release file. */
345 parse_release_file (guestfs_h *g, struct inspect_fs *fs,
346 const char *release_filename)
348 char **product_name = guestfs_head_n (g, 1, release_filename);
349 if (product_name == NULL)
351 if (product_name[0] == NULL) {
352 error (g, _("%s: file is empty"), release_filename);
353 guestfs___free_string_list (product_name);
357 /* Note that this string becomes owned by the handle and will
358 * be freed by guestfs___free_inspect_info.
360 fs->product_name = product_name[0];
366 /* Parse generic MAJOR.MINOR from the fs->product_name string. */
368 parse_major_minor (guestfs_h *g, struct inspect_fs *fs)
372 if (match2 (g, fs->product_name, re_major_minor, &major, &minor)) {
373 fs->major_version = parse_unsigned_int (g, major);
375 if (fs->major_version == -1) {
379 fs->minor_version = parse_unsigned_int (g, minor);
381 if (fs->minor_version == -1)
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.
395 parse_lsb_release (guestfs_h *g, struct inspect_fs *fs)
401 lines = guestfs_head_n (g, 10, "/etc/lsb-release");
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;
411 else if (STRPREFIX (lines[i], "DISTRIB_RELEASE=")) {
413 if (match2 (g, &lines[i][16], re_major_minor, &major, &minor)) {
414 fs->major_version = parse_unsigned_int (g, major);
416 if (fs->major_version == -1) {
418 guestfs___free_string_list (lines);
421 fs->minor_version = parse_unsigned_int (g, minor);
423 if (fs->minor_version == -1) {
424 guestfs___free_string_list (lines);
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);
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);
444 guestfs___free_string_list (lines);
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.
454 check_linux_root (guestfs_h *g, struct inspect_fs *fs)
458 fs->type = OS_TYPE_LINUX;
460 if (guestfs_exists (g, "/etc/lsb-release") > 0) {
461 r = parse_lsb_release (g, fs);
462 if (r == -1) /* error */
464 if (r == 1) /* ok - detected the release from this file */
465 goto skip_release_checks;
468 if (guestfs_exists (g, "/etc/redhat-release") > 0) {
469 fs->distro = OS_DISTRO_REDHAT_BASED; /* Something generic Red Hat-like. */
471 if (parse_release_file (g, fs, "/etc/redhat-release") == -1)
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);
479 if (fs->major_version == -1)
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);
487 if (fs->major_version == -1) {
491 fs->minor_version = parse_unsigned_int (g, minor);
493 if (fs->minor_version == -1)
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);
500 if (fs->major_version == -1)
502 fs->minor_version = 0;
505 else if (guestfs_exists (g, "/etc/debian_version") > 0) {
506 fs->distro = OS_DISTRO_DEBIAN;
508 if (parse_release_file (g, fs, "/etc/debian_version") == -1)
511 if (parse_major_minor (g, fs) == -1)
514 else if (guestfs_exists (g, "/etc/pardus-release") > 0) {
515 fs->distro = OS_DISTRO_PARDUS;
517 if (parse_release_file (g, fs, "/etc/pardus-release") == -1)
520 if (parse_major_minor (g, fs) == -1)
523 else if (guestfs_exists (g, "/etc/arch-release") > 0) {
524 fs->distro = OS_DISTRO_ARCHLINUX;
526 /* /etc/arch-release file is empty and I can't see a way to
527 * determine the actual release or product string.
530 else if (guestfs_exists (g, "/etc/gentoo-release") > 0) {
531 fs->distro = OS_DISTRO_GENTOO;
533 if (parse_release_file (g, fs, "/etc/gentoo-release") == -1)
536 if (parse_major_minor (g, fs) == -1)
539 else if (guestfs_exists (g, "/etc/meego-release") > 0) {
540 fs->distro = OS_DISTRO_MEEGO;
542 if (parse_release_file (g, fs, "/etc/meego-release") == -1)
545 if (parse_major_minor (g, fs) == -1)
549 skip_release_checks:;
551 /* If distro test above was successful, work out the package format. */
552 check_package_format (g, fs);
553 check_package_management (g, fs);
555 /* Determine the architecture. */
556 check_architecture (g, fs);
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
563 if (check_fstab (g, fs) == -1)
569 /* The currently mounted device is known to be a FreeBSD root. */
571 check_freebsd_root (guestfs_h *g, struct inspect_fs *fs)
575 fs->type = OS_TYPE_FREEBSD;
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.
582 if (guestfs_exists (g, "/etc/motd") > 0) {
583 if (parse_release_file (g, fs, "/etc/motd") == -1)
586 if (parse_major_minor (g, fs) == -1)
590 /* Determine the architecture. */
591 check_architecture (g, fs);
593 /* We already know /etc/fstab exists because it's part of the test above. */
594 if (check_fstab (g, fs) == -1)
601 check_architecture (guestfs_h *g, struct inspect_fs *fs)
603 const char *binaries[] =
604 { "/bin/bash", "/bin/ls", "/bin/echo", "/bin/rm", "/bin/sh" };
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;
612 char *arch = guestfs_file_architecture (g, binaries[i]);
613 g->error_cb = old_error_cb;
616 /* String will be owned by handle, freed by
617 * guestfs___free_inspect_info.
626 static int check_fstab_aug_open (guestfs_h *g, struct inspect_fs *fs);
629 check_fstab (guestfs_h *g, struct inspect_fs *fs)
634 /* Security: Refuse to do this if /etc/fstab is huge. */
635 size = guestfs_filesize (g, "/etc/fstab");
636 if (size == -1 || size > 100000) {
637 error (g, _("size of /etc/fstab unreasonable (%" PRIi64 " bytes)"), size);
641 /* XXX What if !feature_available (g, "augeas")? */
642 if (guestfs_aug_init (g, "/", 16|32) == -1)
645 /* Tell Augeas to only load /etc/fstab (thanks Raphaël Pinson). */
646 guestfs_aug_rm (g, "/augeas/load//incl[. != \"/etc/fstab\"]");
647 guestfs_aug_load (g);
649 r = check_fstab_aug_open (g, fs);
650 guestfs_aug_close (g);
658 check_fstab_aug_open (guestfs_h *g, struct inspect_fs *fs)
660 char **lines = guestfs_aug_ls (g, "/files/etc/fstab");
664 if (lines[0] == NULL) {
665 error (g, _("could not parse /etc/fstab or empty file"));
666 guestfs___free_string_list (lines);
672 for (i = 0; lines[i] != NULL; ++i) {
673 /* Ignore comments. Only care about sequence lines which
676 if (match (g, lines[i], re_aug_seq)) {
677 snprintf (augpath, sizeof augpath, "%s/spec", lines[i]);
678 char *spec = guestfs_aug_get (g, augpath);
680 guestfs___free_string_list (lines);
684 snprintf (augpath, sizeof augpath, "%s/file", lines[i]);
685 char *mp = guestfs_aug_get (g, augpath);
687 guestfs___free_string_list (lines);
692 int r = add_fstab_entry (g, fs, spec, mp);
697 guestfs___free_string_list (lines);
703 guestfs___free_string_list (lines);
707 /* Add a filesystem and possibly a mountpoint entry for
708 * the root filesystem 'fs'.
710 * 'spec' is the fstab spec field, which might be a device name or a
711 * pseudodevice or 'UUID=...' or 'LABEL=...'.
713 * 'mp' is the mount point, which could also be 'swap' or 'none'.
716 add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
717 const char *spec, const char *mp)
719 /* Ignore certain mountpoints. */
720 if (STRPREFIX (mp, "/dev/") ||
721 STREQ (mp, "/dev") ||
722 STRPREFIX (mp, "/media/") ||
723 STRPREFIX (mp, "/proc/") ||
724 STREQ (mp, "/proc") ||
725 STRPREFIX (mp, "/selinux/") ||
726 STREQ (mp, "/selinux") ||
727 STRPREFIX (mp, "/sys/") ||
731 /* Ignore /dev/fd (floppy disks) (RHBZ#642929) and CD-ROM drives. */
732 if ((STRPREFIX (spec, "/dev/fd") && c_isdigit (spec[7])) ||
733 STREQ (spec, "/dev/floppy") ||
734 STREQ (spec, "/dev/cdrom"))
737 /* Resolve UUID= and LABEL= to the actual device. */
739 if (STRPREFIX (spec, "UUID="))
740 device = guestfs_findfs_uuid (g, &spec[5]);
741 else if (STRPREFIX (spec, "LABEL="))
742 device = guestfs_findfs_label (g, &spec[6]);
743 /* Ignore "/.swap" (Pardus) and pseudo-devices like "tmpfs". */
744 else if (STRPREFIX (spec, "/dev/"))
745 /* Resolve guest block device names. */
746 device = resolve_fstab_device (g, spec);
748 /* If we haven't resolved the device successfully by this point,
749 * we don't care, just ignore it.
754 char *mountpoint = safe_strdup (g, mp);
756 /* Add this to the fstab entry in 'fs'.
757 * Note these are further filtered by guestfs_inspect_get_mountpoints
758 * and guestfs_inspect_get_filesystems.
760 size_t n = fs->nr_fstab + 1;
761 struct inspect_fstab_entry *p;
763 p = realloc (fs->fstab, n * sizeof (struct inspect_fstab_entry));
765 perrorf (g, "realloc");
774 /* These are owned by the handle and freed by guestfs___free_inspect_info. */
775 fs->fstab[n-1].device = device;
776 fs->fstab[n-1].mountpoint = mountpoint;
779 fprintf (stderr, "fstab: device=%s mountpoint=%s\n", device, mountpoint);
784 /* Resolve block device name to the libguestfs device name, eg.
785 * /dev/xvdb1 => /dev/vdb1; and /dev/mapper/VG-LV => /dev/VG/LV. This
786 * assumes that disks were added in the same order as they appear to
787 * the real VM, which is a reasonable assumption to make. Return
788 * anything we don't recognize unchanged.
791 resolve_fstab_device (guestfs_h *g, const char *spec)
795 char *bsddisk, *bsdslice, *bsdpart;
797 if (STRPREFIX (spec, "/dev/mapper/")) {
798 /* LVM2 does some strange munging on /dev/mapper paths for VGs and
799 * LVs which contain '-' character:
801 * ><fs> lvcreate LV--test VG--test 32
802 * ><fs> debug ls /dev/mapper
803 * VG----test-LV----test
805 * This makes it impossible to reverse those paths directly, so
806 * we have implemented lvm_canonical_lv_name in the daemon.
808 device = guestfs_lvm_canonical_lv_name (g, spec);
810 else if ((a1 = match1 (g, spec, re_xdev)) != NULL) {
811 char **devices = guestfs_list_devices (g);
816 for (count = 0; devices[count] != NULL; count++)
819 size_t i = a1[0] - 'a'; /* a1[0] is always [a-z] because of regex. */
821 size_t len = strlen (devices[i]) + strlen (a1) + 16;
822 device = safe_malloc (g, len);
823 snprintf (device, len, "%s%s", devices[i], &a1[1]);
827 guestfs___free_string_list (devices);
829 else if (match3 (g, spec, re_freebsd, &bsddisk, &bsdslice, &bsdpart)) {
830 /* FreeBSD disks are organized quite differently. See:
831 * http://www.freebsd.org/doc/handbook/disk-organization.html
832 * FreeBSD "partitions" are exposed as quasi-extended partitions
833 * numbered from 5 in Linux. I have no idea what happens when you
834 * have multiple "slices" (the FreeBSD term for MBR partitions).
836 int disk = parse_unsigned_int (g, bsddisk);
837 int slice = parse_unsigned_int (g, bsdslice);
838 int part = bsdpart[0] - 'a' /* counting from 0 */;
843 if (disk == -1 || disk > 26 ||
844 slice <= 0 || slice > 1 /* > 4 .. see comment above */ ||
845 part < 0 || part >= 26)
848 device = safe_asprintf (g, "/dev/sd%c%d", disk + 'a', part + 5);
852 /* Didn't match device pattern, return original spec unchanged. */
854 device = safe_strdup (g, spec);
859 /* XXX Handling of boot.ini in the Perl version was pretty broken. It
860 * essentially didn't do anything for modern Windows guests.
861 * Therefore I've omitted all that code.
864 check_windows_root (guestfs_h *g, struct inspect_fs *fs)
866 fs->type = OS_TYPE_WINDOWS;
867 fs->distro = OS_DISTRO_WINDOWS;
869 /* Try to find Windows systemroot using some common locations. */
870 const char *systemroots[] =
871 { "/windows", "/winnt", "/win32", "/win" };
873 char *systemroot = NULL;
875 systemroot == NULL && i < sizeof systemroots / sizeof systemroots[0];
877 systemroot = resolve_windows_path_silently (g, systemroots[i]);
881 error (g, _("cannot resolve Windows %%SYSTEMROOT%%"));
886 fprintf (stderr, "windows %%SYSTEMROOT%% = %s", systemroot);
888 /* Freed by guestfs___free_inspect_info. */
889 fs->windows_systemroot = systemroot;
891 if (check_windows_arch (g, fs) == -1)
894 if (check_windows_registry (g, fs) == -1)
897 check_package_format (g, fs);
898 check_package_management (g, fs);
904 check_windows_arch (guestfs_h *g, struct inspect_fs *fs)
906 size_t len = strlen (fs->windows_systemroot) + 32;
908 snprintf (cmd_exe, len, "%s/system32/cmd.exe", fs->windows_systemroot);
910 char *cmd_exe_path = resolve_windows_path_silently (g, cmd_exe);
914 char *arch = guestfs_file_architecture (g, cmd_exe_path);
918 fs->arch = arch; /* freed by guestfs___free_inspect_info */
923 /* At the moment, pull just the ProductName and version numbers from
924 * the registry. In future there is a case for making many more
925 * registry fields available to callers.
928 check_windows_registry (guestfs_h *g, struct inspect_fs *fs)
930 TMP_TEMPLATE_ON_STACK (dir);
931 #define dir_len (strlen (dir))
932 #define software_hive_len (dir_len + 16)
933 char software_hive[software_hive_len];
934 #define cmd_len (dir_len + 16)
937 size_t len = strlen (fs->windows_systemroot) + 64;
939 snprintf (software, len, "%s/system32/config/software",
940 fs->windows_systemroot);
942 char *software_path = resolve_windows_path_silently (g, software);
944 /* If the software hive doesn't exist, just accept that we cannot
945 * find product_name etc.
951 hive_value_h *values = NULL;
953 /* Security: Refuse to download registry if it is huge. */
954 int64_t size = guestfs_filesize (g, software_path);
955 if (size == -1 || size > 100000000) {
956 error (g, _("size of %s unreasonable (%" PRIi64 " bytes)"),
957 software_path, size);
961 if (mkdtemp (dir) == NULL) {
962 perrorf (g, "mkdtemp");
966 snprintf (software_hive, software_hive_len, "%s/software", dir);
968 if (guestfs_download (g, software_path, software_hive) == -1)
971 h = hivex_open (software_hive, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
973 perrorf (g, "hivex_open");
977 hive_node_h node = hivex_root (h);
978 const char *hivepath[] =
979 { "Microsoft", "Windows NT", "CurrentVersion" };
982 node != 0 && i < sizeof hivepath / sizeof hivepath[0];
984 node = hivex_node_get_child (h, node, hivepath[i]);
988 perrorf (g, "hivex: cannot locate HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
992 values = hivex_node_values (h, node);
994 for (i = 0; values[i] != 0; ++i) {
995 char *key = hivex_value_key (h, values[i]);
997 perrorf (g, "hivex_value_key");
1001 if (STRCASEEQ (key, "ProductName")) {
1002 fs->product_name = hivex_value_string (h, values[i]);
1003 if (!fs->product_name) {
1004 perrorf (g, "hivex_value_string");
1009 else if (STRCASEEQ (key, "CurrentVersion")) {
1010 char *version = hivex_value_string (h, values[i]);
1012 perrorf (g, "hivex_value_string");
1016 char *major, *minor;
1017 if (match2 (g, version, re_windows_version, &major, &minor)) {
1018 fs->major_version = parse_unsigned_int (g, major);
1020 if (fs->major_version == -1) {
1026 fs->minor_version = parse_unsigned_int (g, minor);
1028 if (fs->minor_version == -1) {
1044 if (h) hivex_close (h);
1046 free (software_path);
1048 /* Free up the temporary directory. Note the directory name cannot
1049 * contain shell meta-characters because of the way it was
1050 * constructed above.
1052 snprintf (cmd, cmd_len, "rm -rf %s", dir);
1053 ignore_value (system (cmd));
1055 #undef software_hive_len
1062 resolve_windows_path_silently (guestfs_h *g, const char *path)
1064 guestfs_error_handler_cb old_error_cb = g->error_cb;
1066 char *ret = guestfs_case_sensitive_path (g, path);
1067 g->error_cb = old_error_cb;
1072 extend_fses (guestfs_h *g)
1074 size_t n = g->nr_fses + 1;
1075 struct inspect_fs *p;
1077 p = realloc (g->fses, n * sizeof (struct inspect_fs));
1079 perrorf (g, "realloc");
1086 memset (&g->fses[n-1], 0, sizeof (struct inspect_fs));
1091 /* Parse small, unsigned ints, as used in version numbers. */
1093 parse_unsigned_int (guestfs_h *g, const char *str)
1096 int r = xstrtol (str, NULL, 10, &ret, "");
1097 if (r != LONGINT_OK) {
1098 error (g, _("could not parse integer in version number: %s"), str);
1104 /* At the moment, package format and package management is just a
1105 * simple function of the distro and major_version fields, so these
1106 * can never return an error. We might be cleverer in future.
1109 check_package_format (guestfs_h *g, struct inspect_fs *fs)
1111 switch (fs->distro) {
1112 case OS_DISTRO_FEDORA:
1113 case OS_DISTRO_MEEGO:
1114 case OS_DISTRO_REDHAT_BASED:
1115 case OS_DISTRO_RHEL:
1116 fs->package_format = OS_PACKAGE_FORMAT_RPM;
1119 case OS_DISTRO_DEBIAN:
1120 case OS_DISTRO_UBUNTU:
1121 fs->package_format = OS_PACKAGE_FORMAT_DEB;
1124 case OS_DISTRO_ARCHLINUX:
1125 fs->package_format = OS_PACKAGE_FORMAT_PACMAN;
1127 case OS_DISTRO_GENTOO:
1128 fs->package_format = OS_PACKAGE_FORMAT_EBUILD;
1130 case OS_DISTRO_PARDUS:
1131 fs->package_format = OS_PACKAGE_FORMAT_PISI;
1134 case OS_DISTRO_WINDOWS:
1135 case OS_DISTRO_UNKNOWN:
1137 fs->package_format = OS_PACKAGE_FORMAT_UNKNOWN;
1143 check_package_management (guestfs_h *g, struct inspect_fs *fs)
1145 switch (fs->distro) {
1146 case OS_DISTRO_FEDORA:
1147 case OS_DISTRO_MEEGO:
1148 fs->package_management = OS_PACKAGE_MANAGEMENT_YUM;
1151 case OS_DISTRO_REDHAT_BASED:
1152 case OS_DISTRO_RHEL:
1153 if (fs->major_version >= 5)
1154 fs->package_management = OS_PACKAGE_MANAGEMENT_YUM;
1156 fs->package_management = OS_PACKAGE_MANAGEMENT_UP2DATE;
1159 case OS_DISTRO_DEBIAN:
1160 case OS_DISTRO_UBUNTU:
1161 fs->package_management = OS_PACKAGE_MANAGEMENT_APT;
1164 case OS_DISTRO_ARCHLINUX:
1165 fs->package_management = OS_PACKAGE_MANAGEMENT_PACMAN;
1167 case OS_DISTRO_GENTOO:
1168 fs->package_management = OS_PACKAGE_MANAGEMENT_PORTAGE;
1170 case OS_DISTRO_PARDUS:
1171 fs->package_management = OS_PACKAGE_MANAGEMENT_PISI;
1174 case OS_DISTRO_WINDOWS:
1175 case OS_DISTRO_UNKNOWN:
1177 fs->package_management = OS_PACKAGE_MANAGEMENT_UNKNOWN;
1182 static struct inspect_fs *
1183 search_for_root (guestfs_h *g, const char *root)
1185 if (g->nr_fses == 0) {
1186 error (g, _("no inspection data: call guestfs_inspect_os first"));
1191 struct inspect_fs *fs;
1192 for (i = 0; i < g->nr_fses; ++i) {
1194 if (fs->is_root && STREQ (root, fs->device))
1198 error (g, _("%s: root device not found: only call this function with a root device previously returned by guestfs_inspect_os"),
1204 guestfs__inspect_get_roots (guestfs_h *g)
1206 /* NB. Doesn't matter if g->nr_fses == 0. We just return an empty
1207 * list in this case.
1212 for (i = 0; i < g->nr_fses; ++i)
1213 if (g->fses[i].is_root)
1216 char **ret = calloc (count+1, sizeof (char *));
1218 perrorf (g, "calloc");
1223 for (i = 0; i < g->nr_fses; ++i) {
1224 if (g->fses[i].is_root) {
1225 ret[count] = safe_strdup (g, g->fses[i].device);
1235 guestfs__inspect_get_type (guestfs_h *g, const char *root)
1237 struct inspect_fs *fs = search_for_root (g, root);
1243 case OS_TYPE_LINUX: ret = safe_strdup (g, "linux"); break;
1244 case OS_TYPE_WINDOWS: ret = safe_strdup (g, "windows"); break;
1245 case OS_TYPE_FREEBSD: ret = safe_strdup (g, "freebsd"); break;
1246 case OS_TYPE_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1253 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
1255 struct inspect_fs *fs = search_for_root (g, root);
1259 return safe_strdup (g, fs->arch ? : "unknown");
1263 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
1265 struct inspect_fs *fs = search_for_root (g, root);
1270 switch (fs->distro) {
1271 case OS_DISTRO_ARCHLINUX: ret = safe_strdup (g, "archlinux"); break;
1272 case OS_DISTRO_DEBIAN: ret = safe_strdup (g, "debian"); break;
1273 case OS_DISTRO_FEDORA: ret = safe_strdup (g, "fedora"); break;
1274 case OS_DISTRO_GENTOO: ret = safe_strdup (g, "gentoo"); break;
1275 case OS_DISTRO_MEEGO: ret = safe_strdup (g, "meego"); break;
1276 case OS_DISTRO_PARDUS: ret = safe_strdup (g, "pardus"); break;
1277 case OS_DISTRO_REDHAT_BASED: ret = safe_strdup (g, "redhat-based"); break;
1278 case OS_DISTRO_RHEL: ret = safe_strdup (g, "rhel"); break;
1279 case OS_DISTRO_WINDOWS: ret = safe_strdup (g, "windows"); break;
1280 case OS_DISTRO_UBUNTU: ret = safe_strdup (g, "ubuntu"); break;
1281 case OS_DISTRO_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1288 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
1290 struct inspect_fs *fs = search_for_root (g, root);
1294 return fs->major_version;
1298 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
1300 struct inspect_fs *fs = search_for_root (g, root);
1304 return fs->minor_version;
1308 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
1310 struct inspect_fs *fs = search_for_root (g, root);
1314 return safe_strdup (g, fs->product_name ? : "unknown");
1318 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
1320 struct inspect_fs *fs = search_for_root (g, root);
1324 if (!fs->windows_systemroot) {
1325 error (g, _("not a Windows guest, or systemroot could not be determined"));
1329 return safe_strdup (g, fs->windows_systemroot);
1333 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
1335 struct inspect_fs *fs = search_for_root (g, root);
1341 /* If no fstab information (Windows) return just the root. */
1342 if (fs->nr_fstab == 0) {
1343 ret = calloc (3, sizeof (char *));
1344 ret[0] = safe_strdup (g, "/");
1345 ret[1] = safe_strdup (g, root);
1350 #define CRITERION fs->fstab[i].mountpoint[0] == '/'
1351 size_t i, count = 0;
1352 for (i = 0; i < fs->nr_fstab; ++i)
1356 /* Hashtables have 2N+1 entries. */
1357 ret = calloc (2*count+1, sizeof (char *));
1359 perrorf (g, "calloc");
1364 for (i = 0; i < fs->nr_fstab; ++i)
1366 ret[2*count] = safe_strdup (g, fs->fstab[i].mountpoint);
1367 ret[2*count+1] = safe_strdup (g, fs->fstab[i].device);
1376 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
1378 struct inspect_fs *fs = search_for_root (g, root);
1384 /* If no fstab information (Windows) return just the root. */
1385 if (fs->nr_fstab == 0) {
1386 ret = calloc (2, sizeof (char *));
1387 ret[0] = safe_strdup (g, root);
1392 ret = calloc (fs->nr_fstab + 1, sizeof (char *));
1394 perrorf (g, "calloc");
1399 for (i = 0; i < fs->nr_fstab; ++i)
1400 ret[i] = safe_strdup (g, fs->fstab[i].device);
1406 guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
1408 struct inspect_fs *fs = search_for_root (g, root);
1413 switch (fs->package_format) {
1414 case OS_PACKAGE_FORMAT_RPM: ret = safe_strdup (g, "rpm"); break;
1415 case OS_PACKAGE_FORMAT_DEB: ret = safe_strdup (g, "deb"); break;
1416 case OS_PACKAGE_FORMAT_PACMAN: ret = safe_strdup (g, "pacman"); break;
1417 case OS_PACKAGE_FORMAT_EBUILD: ret = safe_strdup (g, "ebuild"); break;
1418 case OS_PACKAGE_FORMAT_PISI: ret = safe_strdup (g, "pisi"); break;
1419 case OS_PACKAGE_FORMAT_UNKNOWN:
1421 ret = safe_strdup (g, "unknown");
1429 guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
1431 struct inspect_fs *fs = search_for_root (g, root);
1436 switch (fs->package_management) {
1437 case OS_PACKAGE_MANAGEMENT_YUM: ret = safe_strdup (g, "yum"); break;
1438 case OS_PACKAGE_MANAGEMENT_UP2DATE: ret = safe_strdup (g, "up2date"); break;
1439 case OS_PACKAGE_MANAGEMENT_APT: ret = safe_strdup (g, "apt"); break;
1440 case OS_PACKAGE_MANAGEMENT_PACMAN: ret = safe_strdup (g, "pacman"); break;
1441 case OS_PACKAGE_MANAGEMENT_PORTAGE: ret = safe_strdup (g, "portage"); break;
1442 case OS_PACKAGE_MANAGEMENT_PISI: ret = safe_strdup (g, "pisi"); break;
1443 case OS_PACKAGE_MANAGEMENT_UNKNOWN:
1445 ret = safe_strdup (g, "unknown");
1452 #else /* no PCRE or hivex at compile time */
1454 /* XXX These functions should be in an optgroup. */
1456 #define NOT_IMPL(r) \
1457 error (g, _("inspection API not available since this version of libguestfs was compiled without PCRE or hivex libraries")); \
1461 guestfs__inspect_os (guestfs_h *g)
1467 guestfs__inspect_get_roots (guestfs_h *g)
1473 guestfs__inspect_get_type (guestfs_h *g, const char *root)
1479 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
1485 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
1491 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
1497 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
1503 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
1509 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
1515 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
1521 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
1527 guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
1533 guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
1538 #endif /* no PCRE or hivex at compile time */
1541 guestfs___free_inspect_info (guestfs_h *g)
1544 for (i = 0; i < g->nr_fses; ++i) {
1545 free (g->fses[i].device);
1546 free (g->fses[i].product_name);
1547 free (g->fses[i].arch);
1548 free (g->fses[i].windows_systemroot);
1550 for (j = 0; j < g->fses[i].nr_fstab; ++j) {
1551 free (g->fses[i].fstab[j].device);
1552 free (g->fses[i].fstab[j].mountpoint);
1554 free (g->fses[i].fstab);
1561 /* In the Perl code this is a public function. */
1563 guestfs___feature_available (guestfs_h *g, const char *feature)
1565 /* If there's an error we should ignore it, so to do that we have to
1566 * temporarily replace the error handler with a null one.
1568 guestfs_error_handler_cb old_error_cb = g->error_cb;
1571 const char *groups[] = { feature, NULL };
1572 int r = guestfs_available (g, (char * const *) groups);
1574 g->error_cb = old_error_cb;
1576 return r == 0 ? 1 : 0;
1581 /* Match a regular expression which contains no captures. Returns
1582 * true if it matches or false if it doesn't.
1585 guestfs___match (guestfs_h *g, const char *str, const pcre *re)
1587 size_t len = strlen (str);
1590 r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
1591 if (r == PCRE_ERROR_NOMATCH)
1594 /* Internal error -- should not happen. */
1595 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1596 __FILE__, __func__, r, str);
1603 /* Match a regular expression which contains exactly one capture. If
1604 * the string matches, return the capture, otherwise return NULL. The
1605 * caller must free the result.
1608 guestfs___match1 (guestfs_h *g, const char *str, const pcre *re)
1610 size_t len = strlen (str);
1613 r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
1614 if (r == PCRE_ERROR_NOMATCH)
1617 /* Internal error -- should not happen. */
1618 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1619 __FILE__, __func__, r, str);
1623 return safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
1626 /* Match a regular expression which contains exactly two captures. */
1628 guestfs___match2 (guestfs_h *g, const char *str, const pcre *re,
1629 char **ret1, char **ret2)
1631 size_t len = strlen (str);
1634 r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
1635 if (r == PCRE_ERROR_NOMATCH)
1638 /* Internal error -- should not happen. */
1639 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1640 __FILE__, __func__, r, str);
1644 *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
1645 *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
1650 /* Match a regular expression which contains exactly three captures. */
1652 guestfs___match3 (guestfs_h *g, const char *str, const pcre *re,
1653 char **ret1, char **ret2, char **ret3)
1655 size_t len = strlen (str);
1658 r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
1659 if (r == PCRE_ERROR_NOMATCH)
1662 /* Internal error -- should not happen. */
1663 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1664 __FILE__, __func__, r, str);
1668 *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
1669 *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
1670 *ret3 = safe_strndup (g, &str[vec[6]], vec[7]-vec[6]);
1675 #endif /* HAVE_PCRE */