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.
179 for (i = 0; i < g->nr_fses; ++i)
180 if (g->fses[i].is_root)
183 char **ret = calloc (count+1, sizeof (char *));
185 perrorf (g, "calloc");
186 guestfs___free_inspect_info (g);
191 for (i = 0; i < g->nr_fses; ++i) {
192 if (g->fses[i].is_root) {
193 ret[count] = safe_strdup (g, g->fses[i].device);
202 /* Find out if 'device' contains a filesystem. If it does, add
203 * another entry in g->fses.
205 static int check_filesystem (guestfs_h *g, const char *device);
206 static int check_linux_root (guestfs_h *g, struct inspect_fs *fs);
207 static int check_fstab (guestfs_h *g, struct inspect_fs *fs);
208 static int check_windows_root (guestfs_h *g, struct inspect_fs *fs);
209 static int check_windows_arch (guestfs_h *g, struct inspect_fs *fs);
210 static int check_windows_registry (guestfs_h *g, struct inspect_fs *fs);
211 static char *resolve_windows_path_silently (guestfs_h *g, const char *);
212 static int extend_fses (guestfs_h *g);
213 static int parse_unsigned_int (guestfs_h *g, const char *str);
214 static int add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
215 const char *spec, const char *mp);
216 static char *resolve_fstab_device (guestfs_h *g, const char *spec);
219 check_for_filesystem_on (guestfs_h *g, const char *device)
221 /* Get vfs-type in order to check if it's a Linux(?) swap device.
222 * If there's an error we should ignore it, so to do that we have to
223 * temporarily replace the error handler with a null one.
225 guestfs_error_handler_cb old_error_cb = g->error_cb;
227 char *vfs_type = guestfs_vfs_type (g, device);
228 g->error_cb = old_error_cb;
230 int is_swap = vfs_type && STREQ (vfs_type, "swap");
233 fprintf (stderr, "check_for_filesystem_on: %s (%s)\n",
234 device, vfs_type ? vfs_type : "failed to get vfs type");
238 if (extend_fses (g) == -1)
240 g->fses[g->nr_fses-1].is_swap = 1;
244 /* Try mounting the device. As above, ignore errors. */
246 int r = guestfs_mount_ro (g, device, "/");
247 if (r == -1 && vfs_type && STREQ (vfs_type, "ufs")) /* Hack for the *BSDs. */
248 r = guestfs_mount_vfs (g, "ro,ufstype=ufs2", "ufs", device, "/");
250 g->error_cb = old_error_cb;
254 /* Do the rest of the checks. */
255 r = check_filesystem (g, device);
257 /* Unmount the filesystem. */
258 if (guestfs_umount_all (g) == -1)
265 check_filesystem (guestfs_h *g, const char *device)
267 if (extend_fses (g) == -1)
270 struct inspect_fs *fs = &g->fses[g->nr_fses-1];
272 fs->device = safe_strdup (g, device);
273 fs->is_mountable = 1;
276 if (guestfs_is_file (g, "/grub/menu.lst") > 0 ||
277 guestfs_is_file (g, "/grub/grub.conf") > 0)
278 fs->content = FS_CONTENT_LINUX_BOOT;
280 else if (guestfs_is_dir (g, "/etc") > 0 &&
281 guestfs_is_dir (g, "/bin") > 0 &&
282 guestfs_is_file (g, "/etc/fstab") > 0) {
284 fs->content = FS_CONTENT_LINUX_ROOT;
285 if (check_linux_root (g, fs) == -1)
288 /* Linux /usr/local? */
289 else if (guestfs_is_dir (g, "/etc") > 0 &&
290 guestfs_is_dir (g, "/bin") > 0 &&
291 guestfs_is_dir (g, "/share") > 0 &&
292 guestfs_exists (g, "/local") == 0 &&
293 guestfs_is_file (g, "/etc/fstab") == 0)
294 fs->content = FS_CONTENT_LINUX_USR_LOCAL;
296 else if (guestfs_is_dir (g, "/etc") > 0 &&
297 guestfs_is_dir (g, "/bin") > 0 &&
298 guestfs_is_dir (g, "/share") > 0 &&
299 guestfs_exists (g, "/local") > 0 &&
300 guestfs_is_file (g, "/etc/fstab") == 0)
301 fs->content = FS_CONTENT_LINUX_USR;
303 else if (guestfs_is_dir (g, "/log") > 0 &&
304 guestfs_is_dir (g, "/run") > 0 &&
305 guestfs_is_dir (g, "/spool") > 0)
306 fs->content = FS_CONTENT_LINUX_VAR;
308 else if (guestfs_is_file (g, "/AUTOEXEC.BAT") > 0 ||
309 guestfs_is_file (g, "/autoexec.bat") > 0 ||
310 guestfs_is_dir (g, "/Program Files") > 0 ||
311 guestfs_is_dir (g, "/WINDOWS") > 0 ||
312 guestfs_is_dir (g, "/Windows") > 0 ||
313 guestfs_is_dir (g, "/windows") > 0 ||
314 guestfs_is_dir (g, "/WIN32") > 0 ||
315 guestfs_is_dir (g, "/Win32") > 0 ||
316 guestfs_is_dir (g, "/WINNT") > 0 ||
317 guestfs_is_file (g, "/boot.ini") > 0 ||
318 guestfs_is_file (g, "/ntldr") > 0) {
320 fs->content = FS_CONTENT_WINDOWS_ROOT;
321 if (check_windows_root (g, fs) == -1)
328 /* Set fs->product_name to the first line of the release file. */
330 parse_release_file (guestfs_h *g, struct inspect_fs *fs,
331 const char *release_filename)
333 char **product_name = guestfs_head_n (g, 1, release_filename);
334 if (product_name == NULL)
336 if (product_name[0] == NULL) {
337 error (g, _("%s: file is empty"), release_filename);
338 guestfs___free_string_list (product_name);
342 /* Note that this string becomes owned by the handle and will
343 * be freed by guestfs___free_inspect_info.
345 fs->product_name = product_name[0];
351 /* Parse generic MAJOR.MINOR from the fs->product_name string. */
353 parse_major_minor (guestfs_h *g, struct inspect_fs *fs)
357 if (match2 (g, fs->product_name, re_major_minor, &major, &minor)) {
358 fs->major_version = parse_unsigned_int (g, major);
360 if (fs->major_version == -1) {
364 fs->minor_version = parse_unsigned_int (g, minor);
366 if (fs->minor_version == -1)
372 /* Ubuntu has /etc/lsb-release containing:
373 * DISTRIB_ID=Ubuntu # Distro
374 * DISTRIB_RELEASE=10.04 # Version
375 * DISTRIB_CODENAME=lucid
376 * DISTRIB_DESCRIPTION="Ubuntu 10.04.1 LTS" # Product name
377 * In theory other distros could have this LSB file, but none do.
380 parse_lsb_release (guestfs_h *g, struct inspect_fs *fs)
386 lines = guestfs_head_n (g, 10, "/etc/lsb-release");
390 for (i = 0; lines[i] != NULL; ++i) {
391 if (fs->distro == 0 &&
392 STREQ (lines[i], "DISTRIB_ID=Ubuntu")) {
393 fs->distro = OS_DISTRO_UBUNTU;
396 else if (STRPREFIX (lines[i], "DISTRIB_RELEASE=")) {
398 if (match2 (g, &lines[i][16], re_major_minor, &major, &minor)) {
399 fs->major_version = parse_unsigned_int (g, major);
401 if (fs->major_version == -1) {
403 guestfs___free_string_list (lines);
406 fs->minor_version = parse_unsigned_int (g, minor);
408 if (fs->minor_version == -1) {
409 guestfs___free_string_list (lines);
414 else if (fs->product_name == NULL &&
415 (STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=\"") ||
416 STRPREFIX (lines[i], "DISTRIB_DESCRIPTION='"))) {
417 size_t len = strlen (lines[i]) - 21 - 1;
418 fs->product_name = safe_strndup (g, &lines[i][21], len);
421 else if (fs->product_name == NULL &&
422 STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=")) {
423 size_t len = strlen (lines[i]) - 20;
424 fs->product_name = safe_strndup (g, &lines[i][20], len);
429 guestfs___free_string_list (lines);
433 /* The currently mounted device is known to be a Linux root. Try to
434 * determine from this the distro, version, etc. Also parse
435 * /etc/fstab to determine the arrangement of mountpoints and
436 * associated devices.
439 check_linux_root (guestfs_h *g, struct inspect_fs *fs)
443 fs->type = OS_TYPE_LINUX;
445 if (guestfs_exists (g, "/etc/lsb-release") > 0) {
446 r = parse_lsb_release (g, fs);
447 if (r == -1) /* error */
449 if (r == 1) /* ok - detected the release from this file */
450 goto skip_release_checks;
453 if (guestfs_exists (g, "/etc/redhat-release") > 0) {
454 fs->distro = OS_DISTRO_REDHAT_BASED; /* Something generic Red Hat-like. */
456 if (parse_release_file (g, fs, "/etc/redhat-release") == -1)
460 if ((major = match1 (g, fs->product_name, re_fedora)) != NULL) {
461 fs->distro = OS_DISTRO_FEDORA;
462 fs->major_version = parse_unsigned_int (g, major);
464 if (fs->major_version == -1)
467 else if (match2 (g, fs->product_name, re_rhel_old, &major, &minor) ||
468 match2 (g, fs->product_name, re_rhel, &major, &minor)) {
469 fs->distro = OS_DISTRO_RHEL;
470 fs->major_version = parse_unsigned_int (g, major);
472 if (fs->major_version == -1) {
476 fs->minor_version = parse_unsigned_int (g, minor);
478 if (fs->minor_version == -1)
481 else if ((major = match1 (g, fs->product_name, re_rhel_no_minor)) != NULL) {
482 fs->distro = OS_DISTRO_RHEL;
483 fs->major_version = parse_unsigned_int (g, major);
485 if (fs->major_version == -1)
487 fs->minor_version = 0;
490 else if (guestfs_exists (g, "/etc/debian_version") > 0) {
491 fs->distro = OS_DISTRO_DEBIAN;
493 if (parse_release_file (g, fs, "/etc/debian_version") == -1)
496 if (parse_major_minor (g, fs) == -1)
499 else if (guestfs_exists (g, "/etc/pardus-release") > 0) {
500 fs->distro = OS_DISTRO_PARDUS;
502 if (parse_release_file (g, fs, "/etc/pardus-release") == -1)
505 if (parse_major_minor (g, fs) == -1)
508 else if (guestfs_exists (g, "/etc/arch-release") > 0) {
509 fs->distro = OS_DISTRO_ARCHLINUX;
511 /* /etc/arch-release file is empty and I can't see a way to
512 * determine the actual release or product string.
515 else if (guestfs_exists (g, "/etc/gentoo-release") > 0) {
516 fs->distro = OS_DISTRO_GENTOO;
518 if (parse_release_file (g, fs, "/etc/gentoo-release") == -1)
521 if (parse_major_minor (g, fs) == -1)
524 else if (guestfs_exists (g, "/etc/meego-release") > 0) {
525 fs->distro = OS_DISTRO_MEEGO;
527 if (parse_release_file (g, fs, "/etc/meego-release") == -1)
530 if (parse_major_minor (g, fs) == -1)
534 skip_release_checks:;
536 /* Determine the architecture. */
537 const char *binaries[] =
538 { "/bin/bash", "/bin/ls", "/bin/echo", "/bin/rm", "/bin/sh" };
540 for (i = 0; i < sizeof binaries / sizeof binaries[0]; ++i) {
541 if (guestfs_is_file (g, binaries[i]) > 0) {
542 /* Ignore errors from file_architecture call. */
543 guestfs_error_handler_cb old_error_cb = g->error_cb;
545 char *arch = guestfs_file_architecture (g, binaries[i]);
546 g->error_cb = old_error_cb;
549 /* String will be owned by handle, freed by
550 * guestfs___free_inspect_info.
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
562 * XXX What if !feature_available (g, "augeas")?
564 if (guestfs_aug_init (g, "/", 16|32) == -1)
567 /* Tell Augeas to only load /etc/fstab (thanks Raphaël Pinson). */
568 guestfs_aug_rm (g, "/augeas/load//incl[. != \"/etc/fstab\"]");
569 guestfs_aug_load (g);
571 r = check_fstab (g, fs);
572 guestfs_aug_close (g);
580 check_fstab (guestfs_h *g, struct inspect_fs *fs)
582 char **lines = guestfs_aug_ls (g, "/files/etc/fstab");
586 if (lines[0] == NULL) {
587 error (g, _("could not parse /etc/fstab or empty file"));
588 guestfs___free_string_list (lines);
594 for (i = 0; lines[i] != NULL; ++i) {
595 /* Ignore comments. Only care about sequence lines which
598 if (match (g, lines[i], re_aug_seq)) {
599 snprintf (augpath, sizeof augpath, "%s/spec", lines[i]);
600 char *spec = guestfs_aug_get (g, augpath);
602 guestfs___free_string_list (lines);
606 snprintf (augpath, sizeof augpath, "%s/file", lines[i]);
607 char *mp = guestfs_aug_get (g, augpath);
609 guestfs___free_string_list (lines);
614 int r = add_fstab_entry (g, fs, spec, mp);
619 guestfs___free_string_list (lines);
625 guestfs___free_string_list (lines);
629 /* Add a filesystem and possibly a mountpoint entry for
630 * the root filesystem 'fs'.
632 * 'spec' is the fstab spec field, which might be a device name or a
633 * pseudodevice or 'UUID=...' or 'LABEL=...'.
635 * 'mp' is the mount point, which could also be 'swap' or 'none'.
638 add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
639 const char *spec, const char *mp)
641 /* Ignore certain mountpoints. */
642 if (STRPREFIX (mp, "/dev/") ||
643 STREQ (mp, "/dev") ||
644 STRPREFIX (mp, "/media/") ||
645 STRPREFIX (mp, "/proc/") ||
646 STREQ (mp, "/proc") ||
647 STRPREFIX (mp, "/selinux/") ||
648 STREQ (mp, "/selinux") ||
649 STRPREFIX (mp, "/sys/") ||
653 /* Ignore /dev/fd (floppy disks) (RHBZ#642929) and CD-ROM drives. */
654 if ((STRPREFIX (spec, "/dev/fd") && c_isdigit (spec[7])) ||
655 STREQ (spec, "/dev/floppy") ||
656 STREQ (spec, "/dev/cdrom"))
659 /* Resolve UUID= and LABEL= to the actual device. */
661 if (STRPREFIX (spec, "UUID="))
662 device = guestfs_findfs_uuid (g, &spec[5]);
663 else if (STRPREFIX (spec, "LABEL="))
664 device = guestfs_findfs_label (g, &spec[6]);
665 /* Ignore "/.swap" (Pardus) and pseudo-devices like "tmpfs". */
666 else if (STRPREFIX (spec, "/dev/"))
667 /* Resolve guest block device names. */
668 device = resolve_fstab_device (g, spec);
670 /* If we haven't resolved the device successfully by this point,
671 * we don't care, just ignore it.
676 char *mountpoint = safe_strdup (g, mp);
678 /* Add this to the fstab entry in 'fs'.
679 * Note these are further filtered by guestfs_inspect_get_mountpoints
680 * and guestfs_inspect_get_filesystems.
682 size_t n = fs->nr_fstab + 1;
683 struct inspect_fstab_entry *p;
685 p = realloc (fs->fstab, n * sizeof (struct inspect_fstab_entry));
687 perrorf (g, "realloc");
696 /* These are owned by the handle and freed by guestfs___free_inspect_info. */
697 fs->fstab[n-1].device = device;
698 fs->fstab[n-1].mountpoint = mountpoint;
701 fprintf (stderr, "fstab: device=%s mountpoint=%s\n", device, mountpoint);
706 /* Resolve block device name to the libguestfs device name, eg.
707 * /dev/xvdb1 => /dev/vdb1; and /dev/mapper/VG-LV => /dev/VG/LV. This
708 * assumes that disks were added in the same order as they appear to
709 * the real VM, which is a reasonable assumption to make. Return
710 * anything we don't recognize unchanged.
713 resolve_fstab_device (guestfs_h *g, const char *spec)
718 if (STRPREFIX (spec, "/dev/mapper/")) {
719 /* LVM2 does some strange munging on /dev/mapper paths for VGs and
720 * LVs which contain '-' character:
722 * ><fs> lvcreate LV--test VG--test 32
723 * ><fs> debug ls /dev/mapper
724 * VG----test-LV----test
726 * This makes it impossible to reverse those paths directly, so
727 * we have implemented lvm_canonical_lv_name in the daemon.
729 device = guestfs_lvm_canonical_lv_name (g, spec);
731 else if ((a1 = match1 (g, spec, re_xdev)) != NULL) {
732 char **devices = guestfs_list_devices (g);
737 for (count = 0; devices[count] != NULL; count++)
740 size_t i = a1[0] - 'a'; /* a1[0] is always [a-z] because of regex. */
742 size_t len = strlen (devices[i]) + strlen (a1) + 16;
743 device = safe_malloc (g, len);
744 snprintf (device, len, "%s%s", devices[i], &a1[1]);
748 guestfs___free_string_list (devices);
751 /* Didn't match device pattern, return original spec unchanged. */
752 device = safe_strdup (g, spec);
758 /* XXX Handling of boot.ini in the Perl version was pretty broken. It
759 * essentially didn't do anything for modern Windows guests.
760 * Therefore I've omitted all that code.
763 check_windows_root (guestfs_h *g, struct inspect_fs *fs)
765 fs->type = OS_TYPE_WINDOWS;
766 fs->distro = OS_DISTRO_WINDOWS;
768 /* Try to find Windows systemroot using some common locations. */
769 const char *systemroots[] =
770 { "/windows", "/winnt", "/win32", "/win" };
772 char *systemroot = NULL;
774 systemroot == NULL && i < sizeof systemroots / sizeof systemroots[0];
776 systemroot = resolve_windows_path_silently (g, systemroots[i]);
780 error (g, _("cannot resolve Windows %%SYSTEMROOT%%"));
785 fprintf (stderr, "windows %%SYSTEMROOT%% = %s", systemroot);
787 /* Freed by guestfs___free_inspect_info. */
788 fs->windows_systemroot = systemroot;
790 if (check_windows_arch (g, fs) == -1)
793 if (check_windows_registry (g, fs) == -1)
800 check_windows_arch (guestfs_h *g, struct inspect_fs *fs)
802 size_t len = strlen (fs->windows_systemroot) + 32;
804 snprintf (cmd_exe, len, "%s/system32/cmd.exe", fs->windows_systemroot);
806 char *cmd_exe_path = resolve_windows_path_silently (g, cmd_exe);
810 char *arch = guestfs_file_architecture (g, cmd_exe_path);
814 fs->arch = arch; /* freed by guestfs___free_inspect_info */
819 /* At the moment, pull just the ProductName and version numbers from
820 * the registry. In future there is a case for making many more
821 * registry fields available to callers.
824 check_windows_registry (guestfs_h *g, struct inspect_fs *fs)
826 TMP_TEMPLATE_ON_STACK (dir);
827 #define dir_len (strlen (dir))
828 #define software_hive_len (dir_len + 16)
829 char software_hive[software_hive_len];
830 #define cmd_len (dir_len + 16)
833 size_t len = strlen (fs->windows_systemroot) + 64;
835 snprintf (software, len, "%s/system32/config/software",
836 fs->windows_systemroot);
838 char *software_path = resolve_windows_path_silently (g, software);
840 /* If the software hive doesn't exist, just accept that we cannot
841 * find product_name etc.
847 hive_value_h *values = NULL;
849 if (mkdtemp (dir) == NULL) {
850 perrorf (g, "mkdtemp");
854 snprintf (software_hive, software_hive_len, "%s/software", dir);
856 if (guestfs_download (g, software_path, software_hive) == -1)
859 h = hivex_open (software_hive, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
861 perrorf (g, "hivex_open");
865 hive_node_h node = hivex_root (h);
866 const char *hivepath[] =
867 { "Microsoft", "Windows NT", "CurrentVersion" };
870 node != 0 && i < sizeof hivepath / sizeof hivepath[0];
872 node = hivex_node_get_child (h, node, hivepath[i]);
876 perrorf (g, "hivex: cannot locate HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
880 values = hivex_node_values (h, node);
882 for (i = 0; values[i] != 0; ++i) {
883 char *key = hivex_value_key (h, values[i]);
885 perrorf (g, "hivex_value_key");
889 if (STRCASEEQ (key, "ProductName")) {
890 fs->product_name = hivex_value_string (h, values[i]);
891 if (!fs->product_name) {
892 perrorf (g, "hivex_value_string");
897 else if (STRCASEEQ (key, "CurrentVersion")) {
898 char *version = hivex_value_string (h, values[i]);
900 perrorf (g, "hivex_value_string");
905 if (match2 (g, version, re_windows_version, &major, &minor)) {
906 fs->major_version = parse_unsigned_int (g, major);
908 if (fs->major_version == -1) {
914 fs->minor_version = parse_unsigned_int (g, minor);
916 if (fs->minor_version == -1) {
932 if (h) hivex_close (h);
934 free (software_path);
936 /* Free up the temporary directory. Note the directory name cannot
937 * contain shell meta-characters because of the way it was
940 snprintf (cmd, cmd_len, "rm -rf %s", dir);
941 ignore_value (system (cmd));
943 #undef software_hive_len
950 resolve_windows_path_silently (guestfs_h *g, const char *path)
952 guestfs_error_handler_cb old_error_cb = g->error_cb;
954 char *ret = guestfs_case_sensitive_path (g, path);
955 g->error_cb = old_error_cb;
960 extend_fses (guestfs_h *g)
962 size_t n = g->nr_fses + 1;
963 struct inspect_fs *p;
965 p = realloc (g->fses, n * sizeof (struct inspect_fs));
967 perrorf (g, "realloc");
974 memset (&g->fses[n-1], 0, sizeof (struct inspect_fs));
979 /* Parse small, unsigned ints, as used in version numbers. */
981 parse_unsigned_int (guestfs_h *g, const char *str)
984 int r = xstrtol (str, NULL, 10, &ret, "");
985 if (r != LONGINT_OK) {
986 error (g, _("could not parse integer in version number: %s"), str);
992 static struct inspect_fs *
993 search_for_root (guestfs_h *g, const char *root)
995 if (g->nr_fses == 0) {
996 error (g, _("no inspection data: call guestfs_inspect_os first"));
1001 struct inspect_fs *fs;
1002 for (i = 0; i < g->nr_fses; ++i) {
1004 if (fs->is_root && STREQ (root, fs->device))
1008 error (g, _("%s: root device not found: only call this function with a root device previously returned by guestfs_inspect_os"),
1014 guestfs__inspect_get_type (guestfs_h *g, const char *root)
1016 struct inspect_fs *fs = search_for_root (g, root);
1022 case OS_TYPE_LINUX: ret = safe_strdup (g, "linux"); break;
1023 case OS_TYPE_WINDOWS: ret = safe_strdup (g, "windows"); break;
1024 case OS_TYPE_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1031 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
1033 struct inspect_fs *fs = search_for_root (g, root);
1037 return safe_strdup (g, fs->arch ? : "unknown");
1041 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
1043 struct inspect_fs *fs = search_for_root (g, root);
1048 switch (fs->distro) {
1049 case OS_DISTRO_ARCHLINUX: ret = safe_strdup (g, "archlinux"); break;
1050 case OS_DISTRO_DEBIAN: ret = safe_strdup (g, "debian"); break;
1051 case OS_DISTRO_FEDORA: ret = safe_strdup (g, "fedora"); break;
1052 case OS_DISTRO_GENTOO: ret = safe_strdup (g, "gentoo"); break;
1053 case OS_DISTRO_MEEGO: ret = safe_strdup (g, "meego"); break;
1054 case OS_DISTRO_PARDUS: ret = safe_strdup (g, "pardus"); break;
1055 case OS_DISTRO_REDHAT_BASED: ret = safe_strdup (g, "redhat-based"); break;
1056 case OS_DISTRO_RHEL: ret = safe_strdup (g, "rhel"); break;
1057 case OS_DISTRO_WINDOWS: ret = safe_strdup (g, "windows"); break;
1058 case OS_DISTRO_UBUNTU: ret = safe_strdup (g, "ubuntu"); break;
1059 case OS_DISTRO_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1066 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
1068 struct inspect_fs *fs = search_for_root (g, root);
1072 return fs->major_version;
1076 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
1078 struct inspect_fs *fs = search_for_root (g, root);
1082 return fs->minor_version;
1086 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
1088 struct inspect_fs *fs = search_for_root (g, root);
1092 return safe_strdup (g, fs->product_name ? : "unknown");
1096 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
1098 struct inspect_fs *fs = search_for_root (g, root);
1102 if (!fs->windows_systemroot) {
1103 error (g, _("not a Windows guest, or systemroot could not be determined"));
1107 return safe_strdup (g, fs->windows_systemroot);
1111 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
1113 struct inspect_fs *fs = search_for_root (g, root);
1119 /* If no fstab information (Windows) return just the root. */
1120 if (fs->nr_fstab == 0) {
1121 ret = calloc (3, sizeof (char *));
1122 ret[0] = safe_strdup (g, "/");
1123 ret[1] = safe_strdup (g, root);
1128 #define CRITERION fs->fstab[i].mountpoint[0] == '/'
1129 size_t i, count = 0;
1130 for (i = 0; i < fs->nr_fstab; ++i)
1134 /* Hashtables have 2N+1 entries. */
1135 ret = calloc (2*count+1, sizeof (char *));
1137 perrorf (g, "calloc");
1142 for (i = 0; i < fs->nr_fstab; ++i)
1144 ret[2*count] = safe_strdup (g, fs->fstab[i].mountpoint);
1145 ret[2*count+1] = safe_strdup (g, fs->fstab[i].device);
1154 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
1156 struct inspect_fs *fs = search_for_root (g, root);
1162 /* If no fstab information (Windows) return just the root. */
1163 if (fs->nr_fstab == 0) {
1164 ret = calloc (2, sizeof (char *));
1165 ret[0] = safe_strdup (g, root);
1170 ret = calloc (fs->nr_fstab + 1, sizeof (char *));
1172 perrorf (g, "calloc");
1177 for (i = 0; i < fs->nr_fstab; ++i)
1178 ret[i] = safe_strdup (g, fs->fstab[i].device);
1183 #else /* no PCRE or hivex at compile time */
1185 /* XXX These functions should be in an optgroup. */
1187 #define NOT_IMPL(r) \
1188 error (g, _("inspection API not available since this version of libguestfs was compiled without PCRE or hivex libraries")); \
1192 guestfs__inspect_os (guestfs_h *g)
1198 guestfs__inspect_get_roots (guestfs_h *g)
1204 guestfs__inspect_get_type (guestfs_h *g, const char *root)
1210 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
1216 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
1222 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
1228 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
1234 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
1240 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
1246 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
1252 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
1257 #endif /* no PCRE or hivex at compile time */
1260 guestfs___free_inspect_info (guestfs_h *g)
1263 for (i = 0; i < g->nr_fses; ++i) {
1264 free (g->fses[i].device);
1265 free (g->fses[i].product_name);
1266 free (g->fses[i].arch);
1267 free (g->fses[i].windows_systemroot);
1269 for (j = 0; j < g->fses[i].nr_fstab; ++j) {
1270 free (g->fses[i].fstab[j].device);
1271 free (g->fses[i].fstab[j].mountpoint);
1273 free (g->fses[i].fstab);
1280 /* In the Perl code this is a public function. */
1282 guestfs___feature_available (guestfs_h *g, const char *feature)
1284 /* If there's an error we should ignore it, so to do that we have to
1285 * temporarily replace the error handler with a null one.
1287 guestfs_error_handler_cb old_error_cb = g->error_cb;
1290 const char *groups[] = { feature, NULL };
1291 int r = guestfs_available (g, (char * const *) groups);
1293 g->error_cb = old_error_cb;
1295 return r == 0 ? 1 : 0;
1300 /* Match a regular expression which contains no captures. Returns
1301 * true if it matches or false if it doesn't.
1304 guestfs___match (guestfs_h *g, const char *str, const pcre *re)
1306 size_t len = strlen (str);
1309 r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
1310 if (r == PCRE_ERROR_NOMATCH)
1313 /* Internal error -- should not happen. */
1314 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1315 __FILE__, __func__, r, str);
1322 /* Match a regular expression which contains exactly one capture. If
1323 * the string matches, return the capture, otherwise return NULL. The
1324 * caller must free the result.
1327 guestfs___match1 (guestfs_h *g, const char *str, const pcre *re)
1329 size_t len = strlen (str);
1332 r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
1333 if (r == PCRE_ERROR_NOMATCH)
1336 /* Internal error -- should not happen. */
1337 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1338 __FILE__, __func__, r, str);
1342 return safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
1345 /* Match a regular expression which contains exactly two captures. */
1347 guestfs___match2 (guestfs_h *g, const char *str, const pcre *re,
1348 char **ret1, char **ret2)
1350 size_t len = strlen (str);
1353 r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
1354 if (r == PCRE_ERROR_NOMATCH)
1357 /* Internal error -- should not happen. */
1358 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1359 __FILE__, __func__, r, str);
1363 *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
1364 *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
1369 /* Match a regular expression which contains exactly three captures. */
1371 guestfs___match3 (guestfs_h *g, const char *str, const pcre *re,
1372 char **ret1, char **ret2, char **ret3)
1374 size_t len = strlen (str);
1377 r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
1378 if (r == PCRE_ERROR_NOMATCH)
1381 /* Internal error -- should not happen. */
1382 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1383 __FILE__, __func__, r, str);
1387 *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
1388 *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
1389 *ret3 = safe_strndup (g, &str[vec[6]], vec[7]-vec[6]);
1394 #endif /* HAVE_PCRE */