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_windows_version;
62 static void compile_regexps (void) __attribute__((constructor));
63 static void free_regexps (void) __attribute__((destructor));
66 compile_regexps (void)
71 #define COMPILE(re,pattern,options) \
73 re = pcre_compile ((pattern), (options), &err, &offset, NULL); \
75 ignore_value (write (2, err, strlen (err))); \
80 COMPILE (re_fedora, "Fedora release (\\d+)", 0);
82 "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+).*Update (\\d+)", 0);
84 "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+)\\.(\\d+)", 0);
85 COMPILE (re_rhel_no_minor,
86 "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+)", 0);
87 COMPILE (re_major_minor, "(\\d+)\\.(\\d+)", 0);
88 COMPILE (re_aug_seq, "/\\d+$", 0);
89 COMPILE (re_xdev, "^/dev/(?:h|s|v|xv)d([a-z]\\d*)$", 0);
90 COMPILE (re_windows_version, "^(\\d+)\\.(\\d+)", 0);
96 pcre_free (re_fedora);
97 pcre_free (re_rhel_old);
99 pcre_free (re_rhel_no_minor);
100 pcre_free (re_major_minor);
101 pcre_free (re_aug_seq);
103 pcre_free (re_windows_version);
106 /* The main inspection code. */
107 static int check_for_filesystem_on (guestfs_h *g, const char *device);
110 guestfs__inspect_os (guestfs_h *g)
112 /* Remove any information previously stored in the handle. */
113 guestfs___free_inspect_info (g);
115 if (guestfs_umount_all (g) == -1)
118 /* Iterate over all possible devices. Try to mount each
119 * (read-only). Examine ones which contain filesystems and add that
120 * information to the handle.
122 /* Look to see if any devices directly contain filesystems (RHBZ#590167). */
124 devices = guestfs_list_devices (g);
129 for (i = 0; devices[i] != NULL; ++i) {
130 if (check_for_filesystem_on (g, devices[i]) == -1) {
131 guestfs___free_string_list (devices);
132 guestfs___free_inspect_info (g);
136 guestfs___free_string_list (devices);
138 /* Look at all partitions. */
140 partitions = guestfs_list_partitions (g);
141 if (partitions == NULL) {
142 guestfs___free_inspect_info (g);
146 for (i = 0; partitions[i] != NULL; ++i) {
147 if (check_for_filesystem_on (g, partitions[i]) == -1) {
148 guestfs___free_string_list (partitions);
149 guestfs___free_inspect_info (g);
153 guestfs___free_string_list (partitions);
155 /* Look at all LVs. */
156 if (guestfs___feature_available (g, "lvm2")) {
158 lvs = guestfs_lvs (g);
160 guestfs___free_inspect_info (g);
164 for (i = 0; lvs[i] != NULL; ++i) {
165 if (check_for_filesystem_on (g, lvs[i]) == -1) {
166 guestfs___free_string_list (lvs);
167 guestfs___free_inspect_info (g);
171 guestfs___free_string_list (lvs);
174 /* At this point we have, in the handle, a list of all filesystems
175 * found and data about each one. Now we assemble the list of
176 * filesystems which are root devices and return that to the user.
177 * Fall through to guestfs__inspect_get_roots to do that.
179 char **ret = guestfs__inspect_get_roots (g);
181 guestfs___free_inspect_info (g);
185 /* Find out if 'device' contains a filesystem. If it does, add
186 * another entry in g->fses.
188 static int check_filesystem (guestfs_h *g, const char *device);
189 static int check_linux_root (guestfs_h *g, struct inspect_fs *fs);
190 static int check_fstab (guestfs_h *g, struct inspect_fs *fs);
191 static int check_windows_root (guestfs_h *g, struct inspect_fs *fs);
192 static int check_windows_arch (guestfs_h *g, struct inspect_fs *fs);
193 static int check_windows_registry (guestfs_h *g, struct inspect_fs *fs);
194 static char *resolve_windows_path_silently (guestfs_h *g, const char *);
195 static int extend_fses (guestfs_h *g);
196 static int parse_unsigned_int (guestfs_h *g, const char *str);
197 static int add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
198 const char *spec, const char *mp);
199 static char *resolve_fstab_device (guestfs_h *g, const char *spec);
200 static void check_package_format (guestfs_h *g, struct inspect_fs *fs);
201 static void check_package_management (guestfs_h *g, struct inspect_fs *fs);
204 check_for_filesystem_on (guestfs_h *g, const char *device)
206 /* Get vfs-type in order to check if it's a Linux(?) swap device.
207 * If there's an error we should ignore it, so to do that we have to
208 * temporarily replace the error handler with a null one.
210 guestfs_error_handler_cb old_error_cb = g->error_cb;
212 char *vfs_type = guestfs_vfs_type (g, device);
213 g->error_cb = old_error_cb;
215 int is_swap = vfs_type && STREQ (vfs_type, "swap");
218 fprintf (stderr, "check_for_filesystem_on: %s (%s)\n",
219 device, vfs_type ? vfs_type : "failed to get vfs type");
223 if (extend_fses (g) == -1)
225 g->fses[g->nr_fses-1].is_swap = 1;
229 /* Try mounting the device. As above, ignore errors. */
231 int r = guestfs_mount_ro (g, device, "/");
232 if (r == -1 && vfs_type && STREQ (vfs_type, "ufs")) /* Hack for the *BSDs. */
233 r = guestfs_mount_vfs (g, "ro,ufstype=ufs2", "ufs", device, "/");
235 g->error_cb = old_error_cb;
239 /* Do the rest of the checks. */
240 r = check_filesystem (g, device);
242 /* Unmount the filesystem. */
243 if (guestfs_umount_all (g) == -1)
250 check_filesystem (guestfs_h *g, const char *device)
252 if (extend_fses (g) == -1)
255 struct inspect_fs *fs = &g->fses[g->nr_fses-1];
257 fs->device = safe_strdup (g, device);
258 fs->is_mountable = 1;
260 /* Optimize some of the tests by avoiding multiple tests of the same thing. */
261 int is_dir_etc = guestfs_is_dir (g, "/etc") > 0;
262 int is_dir_bin = guestfs_is_dir (g, "/bin") > 0;
263 int is_dir_share = guestfs_is_dir (g, "/share") > 0;
266 if (guestfs_is_file (g, "/grub/menu.lst") > 0 ||
267 guestfs_is_file (g, "/grub/grub.conf") > 0)
268 fs->content = FS_CONTENT_LINUX_BOOT;
270 else if (is_dir_etc &&
272 guestfs_is_file (g, "/etc/fstab") > 0) {
274 fs->content = FS_CONTENT_LINUX_ROOT;
275 if (check_linux_root (g, fs) == -1)
278 /* Linux /usr/local? */
279 else if (is_dir_etc &&
282 guestfs_exists (g, "/local") == 0 &&
283 guestfs_is_file (g, "/etc/fstab") == 0)
284 fs->content = FS_CONTENT_LINUX_USR_LOCAL;
286 else if (is_dir_etc &&
289 guestfs_exists (g, "/local") > 0 &&
290 guestfs_is_file (g, "/etc/fstab") == 0)
291 fs->content = FS_CONTENT_LINUX_USR;
293 else if (guestfs_is_dir (g, "/log") > 0 &&
294 guestfs_is_dir (g, "/run") > 0 &&
295 guestfs_is_dir (g, "/spool") > 0)
296 fs->content = FS_CONTENT_LINUX_VAR;
298 else if (guestfs_is_file (g, "/AUTOEXEC.BAT") > 0 ||
299 guestfs_is_file (g, "/autoexec.bat") > 0 ||
300 guestfs_is_dir (g, "/Program Files") > 0 ||
301 guestfs_is_dir (g, "/WINDOWS") > 0 ||
302 guestfs_is_dir (g, "/Windows") > 0 ||
303 guestfs_is_dir (g, "/windows") > 0 ||
304 guestfs_is_dir (g, "/WIN32") > 0 ||
305 guestfs_is_dir (g, "/Win32") > 0 ||
306 guestfs_is_dir (g, "/WINNT") > 0 ||
307 guestfs_is_file (g, "/boot.ini") > 0 ||
308 guestfs_is_file (g, "/ntldr") > 0) {
310 fs->content = FS_CONTENT_WINDOWS_ROOT;
311 if (check_windows_root (g, fs) == -1)
318 /* Set fs->product_name to the first line of the release file. */
320 parse_release_file (guestfs_h *g, struct inspect_fs *fs,
321 const char *release_filename)
323 char **product_name = guestfs_head_n (g, 1, release_filename);
324 if (product_name == NULL)
326 if (product_name[0] == NULL) {
327 error (g, "%s: file is empty", release_filename);
328 guestfs___free_string_list (product_name);
332 /* Note that this string becomes owned by the handle and will
333 * be freed by guestfs___free_inspect_info.
335 fs->product_name = product_name[0];
341 /* Parse generic MAJOR.MINOR from the fs->product_name string. */
343 parse_major_minor (guestfs_h *g, struct inspect_fs *fs)
347 if (match2 (g, fs->product_name, re_major_minor, &major, &minor)) {
348 fs->major_version = parse_unsigned_int (g, major);
350 if (fs->major_version == -1) {
354 fs->minor_version = parse_unsigned_int (g, minor);
356 if (fs->minor_version == -1)
362 /* Ubuntu has /etc/lsb-release containing:
363 * DISTRIB_ID=Ubuntu # Distro
364 * DISTRIB_RELEASE=10.04 # Version
365 * DISTRIB_CODENAME=lucid
366 * DISTRIB_DESCRIPTION="Ubuntu 10.04.1 LTS" # Product name
367 * In theory other distros could have this LSB file, but none do.
370 parse_lsb_release (guestfs_h *g, struct inspect_fs *fs)
376 lines = guestfs_head_n (g, 10, "/etc/lsb-release");
380 for (i = 0; lines[i] != NULL; ++i) {
381 if (fs->distro == 0 &&
382 STREQ (lines[i], "DISTRIB_ID=Ubuntu")) {
383 fs->distro = OS_DISTRO_UBUNTU;
386 else if (STRPREFIX (lines[i], "DISTRIB_RELEASE=")) {
388 if (match2 (g, &lines[i][16], re_major_minor, &major, &minor)) {
389 fs->major_version = parse_unsigned_int (g, major);
391 if (fs->major_version == -1) {
393 guestfs___free_string_list (lines);
396 fs->minor_version = parse_unsigned_int (g, minor);
398 if (fs->minor_version == -1) {
399 guestfs___free_string_list (lines);
404 else if (fs->product_name == NULL &&
405 (STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=\"") ||
406 STRPREFIX (lines[i], "DISTRIB_DESCRIPTION='"))) {
407 size_t len = strlen (lines[i]) - 21 - 1;
408 fs->product_name = safe_strndup (g, &lines[i][21], len);
411 else if (fs->product_name == NULL &&
412 STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=")) {
413 size_t len = strlen (lines[i]) - 20;
414 fs->product_name = safe_strndup (g, &lines[i][20], len);
419 guestfs___free_string_list (lines);
423 /* The currently mounted device is known to be a Linux root. Try to
424 * determine from this the distro, version, etc. Also parse
425 * /etc/fstab to determine the arrangement of mountpoints and
426 * associated devices.
429 check_linux_root (guestfs_h *g, struct inspect_fs *fs)
433 fs->type = OS_TYPE_LINUX;
435 if (guestfs_exists (g, "/etc/lsb-release") > 0) {
436 r = parse_lsb_release (g, fs);
437 if (r == -1) /* error */
439 if (r == 1) /* ok - detected the release from this file */
440 goto skip_release_checks;
443 if (guestfs_exists (g, "/etc/redhat-release") > 0) {
444 fs->distro = OS_DISTRO_REDHAT_BASED; /* Something generic Red Hat-like. */
446 if (parse_release_file (g, fs, "/etc/redhat-release") == -1)
450 if ((major = match1 (g, fs->product_name, re_fedora)) != NULL) {
451 fs->distro = OS_DISTRO_FEDORA;
452 fs->major_version = parse_unsigned_int (g, major);
454 if (fs->major_version == -1)
457 else if (match2 (g, fs->product_name, re_rhel_old, &major, &minor) ||
458 match2 (g, fs->product_name, re_rhel, &major, &minor)) {
459 fs->distro = OS_DISTRO_RHEL;
460 fs->major_version = parse_unsigned_int (g, major);
462 if (fs->major_version == -1) {
466 fs->minor_version = parse_unsigned_int (g, minor);
468 if (fs->minor_version == -1)
471 else if ((major = match1 (g, fs->product_name, re_rhel_no_minor)) != NULL) {
472 fs->distro = OS_DISTRO_RHEL;
473 fs->major_version = parse_unsigned_int (g, major);
475 if (fs->major_version == -1)
477 fs->minor_version = 0;
480 else if (guestfs_exists (g, "/etc/debian_version") > 0) {
481 fs->distro = OS_DISTRO_DEBIAN;
483 if (parse_release_file (g, fs, "/etc/debian_version") == -1)
486 if (parse_major_minor (g, fs) == -1)
489 else if (guestfs_exists (g, "/etc/pardus-release") > 0) {
490 fs->distro = OS_DISTRO_PARDUS;
492 if (parse_release_file (g, fs, "/etc/pardus-release") == -1)
495 if (parse_major_minor (g, fs) == -1)
498 else if (guestfs_exists (g, "/etc/arch-release") > 0) {
499 fs->distro = OS_DISTRO_ARCHLINUX;
501 /* /etc/arch-release file is empty and I can't see a way to
502 * determine the actual release or product string.
505 else if (guestfs_exists (g, "/etc/gentoo-release") > 0) {
506 fs->distro = OS_DISTRO_GENTOO;
508 if (parse_release_file (g, fs, "/etc/gentoo-release") == -1)
511 if (parse_major_minor (g, fs) == -1)
514 else if (guestfs_exists (g, "/etc/meego-release") > 0) {
515 fs->distro = OS_DISTRO_MEEGO;
517 if (parse_release_file (g, fs, "/etc/meego-release") == -1)
520 if (parse_major_minor (g, fs) == -1)
524 skip_release_checks:;
526 /* If distro test above was successful, work out the package format. */
527 check_package_format (g, fs);
528 check_package_management (g, fs);
530 /* Determine the architecture. */
531 const char *binaries[] =
532 { "/bin/bash", "/bin/ls", "/bin/echo", "/bin/rm", "/bin/sh" };
534 for (i = 0; i < sizeof binaries / sizeof binaries[0]; ++i) {
535 if (guestfs_is_file (g, binaries[i]) > 0) {
536 /* Ignore errors from file_architecture call. */
537 guestfs_error_handler_cb old_error_cb = g->error_cb;
539 char *arch = guestfs_file_architecture (g, binaries[i]);
540 g->error_cb = old_error_cb;
543 /* String will be owned by handle, freed by
544 * guestfs___free_inspect_info.
552 /* We already know /etc/fstab exists because it's part of the test
553 * for Linux root above. We must now parse this file to determine
554 * which filesystems are used by the operating system and how they
556 * XXX What if !feature_available (g, "augeas")?
558 if (guestfs_aug_init (g, "/", 16|32) == -1)
561 /* Tell Augeas to only load /etc/fstab (thanks Raphaël Pinson). */
562 guestfs_aug_rm (g, "/augeas/load//incl[. != \"/etc/fstab\"]");
563 guestfs_aug_load (g);
565 r = check_fstab (g, fs);
566 guestfs_aug_close (g);
574 check_fstab (guestfs_h *g, struct inspect_fs *fs)
576 char **lines = guestfs_aug_ls (g, "/files/etc/fstab");
580 if (lines[0] == NULL) {
581 error (g, "could not parse /etc/fstab or empty file");
582 guestfs___free_string_list (lines);
588 for (i = 0; lines[i] != NULL; ++i) {
589 /* Ignore comments. Only care about sequence lines which
592 if (match (g, lines[i], re_aug_seq)) {
593 snprintf (augpath, sizeof augpath, "%s/spec", lines[i]);
594 char *spec = guestfs_aug_get (g, augpath);
596 guestfs___free_string_list (lines);
600 snprintf (augpath, sizeof augpath, "%s/file", lines[i]);
601 char *mp = guestfs_aug_get (g, augpath);
603 guestfs___free_string_list (lines);
608 int r = add_fstab_entry (g, fs, spec, mp);
613 guestfs___free_string_list (lines);
619 guestfs___free_string_list (lines);
623 /* Add a filesystem and possibly a mountpoint entry for
624 * the root filesystem 'fs'.
626 * 'spec' is the fstab spec field, which might be a device name or a
627 * pseudodevice or 'UUID=...' or 'LABEL=...'.
629 * 'mp' is the mount point, which could also be 'swap' or 'none'.
632 add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
633 const char *spec, const char *mp)
635 /* Ignore certain mountpoints. */
636 if (STRPREFIX (mp, "/dev/") ||
637 STREQ (mp, "/dev") ||
638 STRPREFIX (mp, "/media/") ||
639 STRPREFIX (mp, "/proc/") ||
640 STREQ (mp, "/proc") ||
641 STRPREFIX (mp, "/selinux/") ||
642 STREQ (mp, "/selinux") ||
643 STRPREFIX (mp, "/sys/") ||
647 /* Ignore /dev/fd (floppy disks) (RHBZ#642929) and CD-ROM drives. */
648 if ((STRPREFIX (spec, "/dev/fd") && c_isdigit (spec[7])) ||
649 STREQ (spec, "/dev/floppy") ||
650 STREQ (spec, "/dev/cdrom"))
653 /* Resolve UUID= and LABEL= to the actual device. */
655 if (STRPREFIX (spec, "UUID="))
656 device = guestfs_findfs_uuid (g, &spec[5]);
657 else if (STRPREFIX (spec, "LABEL="))
658 device = guestfs_findfs_label (g, &spec[6]);
659 /* Ignore "/.swap" (Pardus) and pseudo-devices like "tmpfs". */
660 else if (STRPREFIX (spec, "/dev/"))
661 /* Resolve guest block device names. */
662 device = resolve_fstab_device (g, spec);
664 /* If we haven't resolved the device successfully by this point,
665 * we don't care, just ignore it.
670 char *mountpoint = safe_strdup (g, mp);
672 /* Add this to the fstab entry in 'fs'.
673 * Note these are further filtered by guestfs_inspect_get_mountpoints
674 * and guestfs_inspect_get_filesystems.
676 size_t n = fs->nr_fstab + 1;
677 struct inspect_fstab_entry *p;
679 p = realloc (fs->fstab, n * sizeof (struct inspect_fstab_entry));
681 perrorf (g, "realloc");
690 /* These are owned by the handle and freed by guestfs___free_inspect_info. */
691 fs->fstab[n-1].device = device;
692 fs->fstab[n-1].mountpoint = mountpoint;
695 fprintf (stderr, "fstab: device=%s mountpoint=%s\n", device, mountpoint);
700 /* Resolve block device name to the libguestfs device name, eg.
701 * /dev/xvdb1 => /dev/vdb1; and /dev/mapper/VG-LV => /dev/VG/LV. This
702 * assumes that disks were added in the same order as they appear to
703 * the real VM, which is a reasonable assumption to make. Return
704 * anything we don't recognize unchanged.
707 resolve_fstab_device (guestfs_h *g, const char *spec)
712 if (STRPREFIX (spec, "/dev/mapper/")) {
713 /* LVM2 does some strange munging on /dev/mapper paths for VGs and
714 * LVs which contain '-' character:
716 * ><fs> lvcreate LV--test VG--test 32
717 * ><fs> debug ls /dev/mapper
718 * VG----test-LV----test
720 * This makes it impossible to reverse those paths directly, so
721 * we have implemented lvm_canonical_lv_name in the daemon.
723 device = guestfs_lvm_canonical_lv_name (g, spec);
725 else if ((a1 = match1 (g, spec, re_xdev)) != NULL) {
726 char **devices = guestfs_list_devices (g);
731 for (count = 0; devices[count] != NULL; count++)
734 size_t i = a1[0] - 'a'; /* a1[0] is always [a-z] because of regex. */
736 size_t len = strlen (devices[i]) + strlen (a1) + 16;
737 device = safe_malloc (g, len);
738 snprintf (device, len, "%s%s", devices[i], &a1[1]);
742 guestfs___free_string_list (devices);
745 /* Didn't match device pattern, return original spec unchanged. */
746 device = safe_strdup (g, spec);
752 /* XXX Handling of boot.ini in the Perl version was pretty broken. It
753 * essentially didn't do anything for modern Windows guests.
754 * Therefore I've omitted all that code.
757 check_windows_root (guestfs_h *g, struct inspect_fs *fs)
759 fs->type = OS_TYPE_WINDOWS;
760 fs->distro = OS_DISTRO_WINDOWS;
762 /* Try to find Windows systemroot using some common locations. */
763 const char *systemroots[] =
764 { "/windows", "/winnt", "/win32", "/win" };
766 char *systemroot = NULL;
768 systemroot == NULL && i < sizeof systemroots / sizeof systemroots[0];
770 systemroot = resolve_windows_path_silently (g, systemroots[i]);
774 error (g, _("cannot resolve Windows %%SYSTEMROOT%%"));
779 fprintf (stderr, "windows %%SYSTEMROOT%% = %s", systemroot);
781 /* Freed by guestfs___free_inspect_info. */
782 fs->windows_systemroot = systemroot;
784 if (check_windows_arch (g, fs) == -1)
787 if (check_windows_registry (g, fs) == -1)
790 check_package_format (g, fs);
791 check_package_management (g, fs);
797 check_windows_arch (guestfs_h *g, struct inspect_fs *fs)
799 size_t len = strlen (fs->windows_systemroot) + 32;
801 snprintf (cmd_exe, len, "%s/system32/cmd.exe", fs->windows_systemroot);
803 char *cmd_exe_path = resolve_windows_path_silently (g, cmd_exe);
807 char *arch = guestfs_file_architecture (g, cmd_exe_path);
811 fs->arch = arch; /* freed by guestfs___free_inspect_info */
816 /* At the moment, pull just the ProductName and version numbers from
817 * the registry. In future there is a case for making many more
818 * registry fields available to callers.
821 check_windows_registry (guestfs_h *g, struct inspect_fs *fs)
823 TMP_TEMPLATE_ON_STACK (dir);
824 #define dir_len (strlen (dir))
825 #define software_hive_len (dir_len + 16)
826 char software_hive[software_hive_len];
827 #define cmd_len (dir_len + 16)
830 size_t len = strlen (fs->windows_systemroot) + 64;
832 snprintf (software, len, "%s/system32/config/software",
833 fs->windows_systemroot);
835 char *software_path = resolve_windows_path_silently (g, software);
837 /* If the software hive doesn't exist, just accept that we cannot
838 * find product_name etc.
844 hive_value_h *values = NULL;
846 if (mkdtemp (dir) == NULL) {
847 perrorf (g, "mkdtemp");
851 snprintf (software_hive, software_hive_len, "%s/software", dir);
853 if (guestfs_download (g, software_path, software_hive) == -1)
856 h = hivex_open (software_hive, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
858 perrorf (g, "hivex_open");
862 hive_node_h node = hivex_root (h);
863 const char *hivepath[] =
864 { "Microsoft", "Windows NT", "CurrentVersion" };
867 node != 0 && i < sizeof hivepath / sizeof hivepath[0];
869 node = hivex_node_get_child (h, node, hivepath[i]);
873 perrorf (g, "hivex: cannot locate HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
877 values = hivex_node_values (h, node);
879 for (i = 0; values[i] != 0; ++i) {
880 char *key = hivex_value_key (h, values[i]);
882 perrorf (g, "hivex_value_key");
886 if (STRCASEEQ (key, "ProductName")) {
887 fs->product_name = hivex_value_string (h, values[i]);
888 if (!fs->product_name) {
889 perrorf (g, "hivex_value_string");
894 else if (STRCASEEQ (key, "CurrentVersion")) {
895 char *version = hivex_value_string (h, values[i]);
897 perrorf (g, "hivex_value_string");
902 if (match2 (g, version, re_windows_version, &major, &minor)) {
903 fs->major_version = parse_unsigned_int (g, major);
905 if (fs->major_version == -1) {
911 fs->minor_version = parse_unsigned_int (g, minor);
913 if (fs->minor_version == -1) {
929 if (h) hivex_close (h);
931 free (software_path);
933 /* Free up the temporary directory. Note the directory name cannot
934 * contain shell meta-characters because of the way it was
937 snprintf (cmd, cmd_len, "rm -rf %s", dir);
938 ignore_value (system (cmd));
940 #undef software_hive_len
947 resolve_windows_path_silently (guestfs_h *g, const char *path)
949 guestfs_error_handler_cb old_error_cb = g->error_cb;
951 char *ret = guestfs_case_sensitive_path (g, path);
952 g->error_cb = old_error_cb;
957 extend_fses (guestfs_h *g)
959 size_t n = g->nr_fses + 1;
960 struct inspect_fs *p;
962 p = realloc (g->fses, n * sizeof (struct inspect_fs));
964 perrorf (g, "realloc");
971 memset (&g->fses[n-1], 0, sizeof (struct inspect_fs));
976 /* Parse small, unsigned ints, as used in version numbers. */
978 parse_unsigned_int (guestfs_h *g, const char *str)
981 int r = xstrtol (str, NULL, 10, &ret, "");
982 if (r != LONGINT_OK) {
983 error (g, "could not parse integer in version number: %s", str);
989 /* At the moment, package format and package management is just a
990 * simple function of the distro and major_version fields, so these
991 * can never return an error. We might be cleverer in future.
994 check_package_format (guestfs_h *g, struct inspect_fs *fs)
996 switch (fs->distro) {
997 case OS_DISTRO_FEDORA:
998 case OS_DISTRO_MEEGO:
999 case OS_DISTRO_REDHAT_BASED:
1000 case OS_DISTRO_RHEL:
1001 fs->package_format = OS_PACKAGE_FORMAT_RPM;
1004 case OS_DISTRO_DEBIAN:
1005 case OS_DISTRO_UBUNTU:
1006 fs->package_format = OS_PACKAGE_FORMAT_DEB;
1009 case OS_DISTRO_ARCHLINUX:
1010 fs->package_format = OS_PACKAGE_FORMAT_PACMAN;
1012 case OS_DISTRO_GENTOO:
1013 fs->package_format = OS_PACKAGE_FORMAT_EBUILD;
1015 case OS_DISTRO_PARDUS:
1016 fs->package_format = OS_PACKAGE_FORMAT_PISI;
1019 case OS_DISTRO_WINDOWS:
1020 case OS_DISTRO_UNKNOWN:
1022 fs->package_format = OS_PACKAGE_FORMAT_UNKNOWN;
1028 check_package_management (guestfs_h *g, struct inspect_fs *fs)
1030 switch (fs->distro) {
1031 case OS_DISTRO_FEDORA:
1032 case OS_DISTRO_MEEGO:
1033 fs->package_management = OS_PACKAGE_MANAGEMENT_YUM;
1036 case OS_DISTRO_REDHAT_BASED:
1037 case OS_DISTRO_RHEL:
1038 if (fs->major_version >= 5)
1039 fs->package_management = OS_PACKAGE_MANAGEMENT_YUM;
1041 fs->package_management = OS_PACKAGE_MANAGEMENT_UP2DATE;
1044 case OS_DISTRO_DEBIAN:
1045 case OS_DISTRO_UBUNTU:
1046 fs->package_management = OS_PACKAGE_MANAGEMENT_APT;
1049 case OS_DISTRO_ARCHLINUX:
1050 fs->package_management = OS_PACKAGE_MANAGEMENT_PACMAN;
1052 case OS_DISTRO_GENTOO:
1053 fs->package_management = OS_PACKAGE_MANAGEMENT_PORTAGE;
1055 case OS_DISTRO_PARDUS:
1056 fs->package_management = OS_PACKAGE_MANAGEMENT_PISI;
1059 case OS_DISTRO_WINDOWS:
1060 case OS_DISTRO_UNKNOWN:
1062 fs->package_management = OS_PACKAGE_MANAGEMENT_UNKNOWN;
1067 static struct inspect_fs *
1068 search_for_root (guestfs_h *g, const char *root)
1070 if (g->nr_fses == 0) {
1071 error (g, _("no inspection data: call guestfs_inspect_os first"));
1076 struct inspect_fs *fs;
1077 for (i = 0; i < g->nr_fses; ++i) {
1079 if (fs->is_root && STREQ (root, fs->device))
1083 error (g, _("%s: root device not found: only call this function with a root device previously returned by guestfs_inspect_os"),
1089 guestfs__inspect_get_roots (guestfs_h *g)
1091 /* NB. Doesn't matter if g->nr_fses == 0. We just return an empty
1092 * list in this case.
1097 for (i = 0; i < g->nr_fses; ++i)
1098 if (g->fses[i].is_root)
1101 char **ret = calloc (count+1, sizeof (char *));
1103 perrorf (g, "calloc");
1108 for (i = 0; i < g->nr_fses; ++i) {
1109 if (g->fses[i].is_root) {
1110 ret[count] = safe_strdup (g, g->fses[i].device);
1120 guestfs__inspect_get_type (guestfs_h *g, const char *root)
1122 struct inspect_fs *fs = search_for_root (g, root);
1128 case OS_TYPE_LINUX: ret = safe_strdup (g, "linux"); break;
1129 case OS_TYPE_WINDOWS: ret = safe_strdup (g, "windows"); break;
1130 case OS_TYPE_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1137 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
1139 struct inspect_fs *fs = search_for_root (g, root);
1143 return safe_strdup (g, fs->arch ? : "unknown");
1147 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
1149 struct inspect_fs *fs = search_for_root (g, root);
1154 switch (fs->distro) {
1155 case OS_DISTRO_ARCHLINUX: ret = safe_strdup (g, "archlinux"); break;
1156 case OS_DISTRO_DEBIAN: ret = safe_strdup (g, "debian"); break;
1157 case OS_DISTRO_FEDORA: ret = safe_strdup (g, "fedora"); break;
1158 case OS_DISTRO_GENTOO: ret = safe_strdup (g, "gentoo"); break;
1159 case OS_DISTRO_MEEGO: ret = safe_strdup (g, "meego"); break;
1160 case OS_DISTRO_PARDUS: ret = safe_strdup (g, "pardus"); break;
1161 case OS_DISTRO_REDHAT_BASED: ret = safe_strdup (g, "redhat-based"); break;
1162 case OS_DISTRO_RHEL: ret = safe_strdup (g, "rhel"); break;
1163 case OS_DISTRO_WINDOWS: ret = safe_strdup (g, "windows"); break;
1164 case OS_DISTRO_UBUNTU: ret = safe_strdup (g, "ubuntu"); break;
1165 case OS_DISTRO_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1172 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
1174 struct inspect_fs *fs = search_for_root (g, root);
1178 return fs->major_version;
1182 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
1184 struct inspect_fs *fs = search_for_root (g, root);
1188 return fs->minor_version;
1192 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
1194 struct inspect_fs *fs = search_for_root (g, root);
1198 return safe_strdup (g, fs->product_name ? : "unknown");
1202 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
1204 struct inspect_fs *fs = search_for_root (g, root);
1208 if (!fs->windows_systemroot) {
1209 error (g, _("not a Windows guest, or systemroot could not be determined"));
1213 return safe_strdup (g, fs->windows_systemroot);
1217 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
1219 struct inspect_fs *fs = search_for_root (g, root);
1225 /* If no fstab information (Windows) return just the root. */
1226 if (fs->nr_fstab == 0) {
1227 ret = calloc (3, sizeof (char *));
1228 ret[0] = safe_strdup (g, "/");
1229 ret[1] = safe_strdup (g, root);
1234 #define CRITERION fs->fstab[i].mountpoint[0] == '/'
1235 size_t i, count = 0;
1236 for (i = 0; i < fs->nr_fstab; ++i)
1240 /* Hashtables have 2N+1 entries. */
1241 ret = calloc (2*count+1, sizeof (char *));
1243 perrorf (g, "calloc");
1248 for (i = 0; i < fs->nr_fstab; ++i)
1250 ret[2*count] = safe_strdup (g, fs->fstab[i].mountpoint);
1251 ret[2*count+1] = safe_strdup (g, fs->fstab[i].device);
1260 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
1262 struct inspect_fs *fs = search_for_root (g, root);
1268 /* If no fstab information (Windows) return just the root. */
1269 if (fs->nr_fstab == 0) {
1270 ret = calloc (2, sizeof (char *));
1271 ret[0] = safe_strdup (g, root);
1276 ret = calloc (fs->nr_fstab + 1, sizeof (char *));
1278 perrorf (g, "calloc");
1283 for (i = 0; i < fs->nr_fstab; ++i)
1284 ret[i] = safe_strdup (g, fs->fstab[i].device);
1290 guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
1292 struct inspect_fs *fs = search_for_root (g, root);
1297 switch (fs->package_format) {
1298 case OS_PACKAGE_FORMAT_RPM: ret = safe_strdup (g, "rpm"); break;
1299 case OS_PACKAGE_FORMAT_DEB: ret = safe_strdup (g, "deb"); break;
1300 case OS_PACKAGE_FORMAT_PACMAN: ret = safe_strdup (g, "pacman"); break;
1301 case OS_PACKAGE_FORMAT_EBUILD: ret = safe_strdup (g, "ebuild"); break;
1302 case OS_PACKAGE_FORMAT_PISI: ret = safe_strdup (g, "pisi"); break;
1303 case OS_PACKAGE_FORMAT_UNKNOWN:
1305 ret = safe_strdup (g, "unknown");
1313 guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
1315 struct inspect_fs *fs = search_for_root (g, root);
1320 switch (fs->package_management) {
1321 case OS_PACKAGE_MANAGEMENT_YUM: ret = safe_strdup (g, "yum"); break;
1322 case OS_PACKAGE_MANAGEMENT_UP2DATE: ret = safe_strdup (g, "up2date"); break;
1323 case OS_PACKAGE_MANAGEMENT_APT: ret = safe_strdup (g, "apt"); break;
1324 case OS_PACKAGE_MANAGEMENT_PACMAN: ret = safe_strdup (g, "pacman"); break;
1325 case OS_PACKAGE_MANAGEMENT_PORTAGE: ret = safe_strdup (g, "portage"); break;
1326 case OS_PACKAGE_MANAGEMENT_PISI: ret = safe_strdup (g, "pisi"); break;
1327 case OS_PACKAGE_MANAGEMENT_UNKNOWN:
1329 ret = safe_strdup (g, "unknown");
1336 #else /* no PCRE or hivex at compile time */
1338 /* XXX These functions should be in an optgroup. */
1340 #define NOT_IMPL(r) \
1341 error (g, _("inspection API not available since this version of libguestfs was compiled without PCRE or hivex libraries")); \
1345 guestfs__inspect_os (guestfs_h *g)
1351 guestfs__inspect_get_roots (guestfs_h *g)
1357 guestfs__inspect_get_type (guestfs_h *g, const char *root)
1363 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
1369 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
1375 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
1381 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
1387 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
1393 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
1399 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
1405 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
1411 guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
1417 guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
1422 #endif /* no PCRE or hivex at compile time */
1425 guestfs___free_inspect_info (guestfs_h *g)
1428 for (i = 0; i < g->nr_fses; ++i) {
1429 free (g->fses[i].device);
1430 free (g->fses[i].product_name);
1431 free (g->fses[i].arch);
1432 free (g->fses[i].windows_systemroot);
1434 for (j = 0; j < g->fses[i].nr_fstab; ++j) {
1435 free (g->fses[i].fstab[j].device);
1436 free (g->fses[i].fstab[j].mountpoint);
1438 free (g->fses[i].fstab);
1445 /* In the Perl code this is a public function. */
1447 guestfs___feature_available (guestfs_h *g, const char *feature)
1449 /* If there's an error we should ignore it, so to do that we have to
1450 * temporarily replace the error handler with a null one.
1452 guestfs_error_handler_cb old_error_cb = g->error_cb;
1455 const char *groups[] = { feature, NULL };
1456 int r = guestfs_available (g, (char * const *) groups);
1458 g->error_cb = old_error_cb;
1460 return r == 0 ? 1 : 0;
1465 /* Match a regular expression which contains no captures. Returns
1466 * true if it matches or false if it doesn't.
1469 guestfs___match (guestfs_h *g, const char *str, const pcre *re)
1471 size_t len = strlen (str);
1474 r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
1475 if (r == PCRE_ERROR_NOMATCH)
1478 /* Internal error -- should not happen. */
1479 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1480 __FILE__, __func__, r, str);
1487 /* Match a regular expression which contains exactly one capture. If
1488 * the string matches, return the capture, otherwise return NULL. The
1489 * caller must free the result.
1492 guestfs___match1 (guestfs_h *g, const char *str, const pcre *re)
1494 size_t len = strlen (str);
1497 r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
1498 if (r == PCRE_ERROR_NOMATCH)
1501 /* Internal error -- should not happen. */
1502 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1503 __FILE__, __func__, r, str);
1507 return safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
1510 /* Match a regular expression which contains exactly two captures. */
1512 guestfs___match2 (guestfs_h *g, const char *str, const pcre *re,
1513 char **ret1, char **ret2)
1515 size_t len = strlen (str);
1518 r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
1519 if (r == PCRE_ERROR_NOMATCH)
1522 /* Internal error -- should not happen. */
1523 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1524 __FILE__, __func__, r, str);
1528 *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
1529 *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
1534 /* Match a regular expression which contains exactly three captures. */
1536 guestfs___match3 (guestfs_h *g, const char *str, const pcre *re,
1537 char **ret1, char **ret2, char **ret3)
1539 size_t len = strlen (str);
1542 r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
1543 if (r == PCRE_ERROR_NOMATCH)
1546 /* Internal error -- should not happen. */
1547 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1548 __FILE__, __func__, r, str);
1552 *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
1553 *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
1554 *ret3 = safe_strndup (g, &str[vec[6]], vec[7]-vec[6]);
1559 #endif /* HAVE_PCRE */