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 void check_architecture (guestfs_h *g, struct inspect_fs *fs);
208 static int check_fstab (guestfs_h *g, struct inspect_fs *fs);
209 static int check_windows_root (guestfs_h *g, struct inspect_fs *fs);
210 static int check_windows_arch (guestfs_h *g, struct inspect_fs *fs);
211 static int check_windows_registry (guestfs_h *g, struct inspect_fs *fs);
212 static char *resolve_windows_path_silently (guestfs_h *g, const char *);
213 static int extend_fses (guestfs_h *g);
214 static int parse_unsigned_int (guestfs_h *g, const char *str);
215 static int add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
216 const char *spec, const char *mp);
217 static char *resolve_fstab_device (guestfs_h *g, const char *spec);
218 static int download_to_tmp (guestfs_h *g, const char *filename, char *localtmp, int64_t max_size);
219 static int inspect_with_augeas (guestfs_h *g, struct inspect_fs *fs, const char *filename, int (*f) (guestfs_h *, struct inspect_fs *));
220 static char *first_line_of_file (guestfs_h *g, const char *filename);
223 check_for_filesystem_on (guestfs_h *g, const char *device)
225 /* Get vfs-type in order to check if it's a Linux(?) swap device.
226 * If there's an error we should ignore it, so to do that we have to
227 * temporarily replace the error handler with a null one.
229 guestfs_error_handler_cb old_error_cb = g->error_cb;
231 char *vfs_type = guestfs_vfs_type (g, device);
232 g->error_cb = old_error_cb;
234 int is_swap = vfs_type && STREQ (vfs_type, "swap");
237 fprintf (stderr, "check_for_filesystem_on: %s (%s)\n",
238 device, vfs_type ? vfs_type : "failed to get vfs type");
242 if (extend_fses (g) == -1)
244 g->fses[g->nr_fses-1].is_swap = 1;
248 /* Try mounting the device. As above, ignore errors. */
250 int r = guestfs_mount_ro (g, device, "/");
251 if (r == -1 && vfs_type && STREQ (vfs_type, "ufs")) /* Hack for the *BSDs. */
252 r = guestfs_mount_vfs (g, "ro,ufstype=ufs2", "ufs", device, "/");
254 g->error_cb = old_error_cb;
258 /* Do the rest of the checks. */
259 r = check_filesystem (g, device);
261 /* Unmount the filesystem. */
262 if (guestfs_umount_all (g) == -1)
269 check_filesystem (guestfs_h *g, const char *device)
271 if (extend_fses (g) == -1)
274 struct inspect_fs *fs = &g->fses[g->nr_fses-1];
276 fs->device = safe_strdup (g, device);
277 fs->is_mountable = 1;
280 if (guestfs_is_file (g, "/grub/menu.lst") > 0 ||
281 guestfs_is_file (g, "/grub/grub.conf") > 0)
282 fs->content = FS_CONTENT_LINUX_BOOT;
284 else if (guestfs_is_dir (g, "/etc") > 0 &&
285 guestfs_is_dir (g, "/bin") > 0 &&
286 guestfs_is_file (g, "/etc/fstab") > 0) {
288 fs->content = FS_CONTENT_LINUX_ROOT;
289 if (check_linux_root (g, fs) == -1)
292 /* Linux /usr/local? */
293 else if (guestfs_is_dir (g, "/etc") > 0 &&
294 guestfs_is_dir (g, "/bin") > 0 &&
295 guestfs_is_dir (g, "/share") > 0 &&
296 guestfs_exists (g, "/local") == 0 &&
297 guestfs_is_file (g, "/etc/fstab") == 0)
298 fs->content = FS_CONTENT_LINUX_USR_LOCAL;
300 else if (guestfs_is_dir (g, "/etc") > 0 &&
301 guestfs_is_dir (g, "/bin") > 0 &&
302 guestfs_is_dir (g, "/share") > 0 &&
303 guestfs_exists (g, "/local") > 0 &&
304 guestfs_is_file (g, "/etc/fstab") == 0)
305 fs->content = FS_CONTENT_LINUX_USR;
307 else if (guestfs_is_dir (g, "/log") > 0 &&
308 guestfs_is_dir (g, "/run") > 0 &&
309 guestfs_is_dir (g, "/spool") > 0)
310 fs->content = FS_CONTENT_LINUX_VAR;
312 else if (guestfs_is_file (g, "/AUTOEXEC.BAT") > 0 ||
313 guestfs_is_file (g, "/autoexec.bat") > 0 ||
314 guestfs_is_dir (g, "/Program Files") > 0 ||
315 guestfs_is_dir (g, "/WINDOWS") > 0 ||
316 guestfs_is_dir (g, "/Windows") > 0 ||
317 guestfs_is_dir (g, "/windows") > 0 ||
318 guestfs_is_dir (g, "/WIN32") > 0 ||
319 guestfs_is_dir (g, "/Win32") > 0 ||
320 guestfs_is_dir (g, "/WINNT") > 0 ||
321 guestfs_is_file (g, "/boot.ini") > 0 ||
322 guestfs_is_file (g, "/ntldr") > 0) {
324 fs->content = FS_CONTENT_WINDOWS_ROOT;
325 if (check_windows_root (g, fs) == -1)
332 /* Set fs->product_name to the first line of the release file. */
334 parse_release_file (guestfs_h *g, struct inspect_fs *fs,
335 const char *release_filename)
337 fs->product_name = first_line_of_file (g, release_filename);
338 if (fs->product_name == NULL)
343 /* Parse generic MAJOR.MINOR from the fs->product_name string. */
345 parse_major_minor (guestfs_h *g, struct inspect_fs *fs)
349 if (match2 (g, fs->product_name, re_major_minor, &major, &minor)) {
350 fs->major_version = parse_unsigned_int (g, major);
352 if (fs->major_version == -1) {
356 fs->minor_version = parse_unsigned_int (g, minor);
358 if (fs->minor_version == -1)
364 /* Ubuntu has /etc/lsb-release containing:
365 * DISTRIB_ID=Ubuntu # Distro
366 * DISTRIB_RELEASE=10.04 # Version
367 * DISTRIB_CODENAME=lucid
368 * DISTRIB_DESCRIPTION="Ubuntu 10.04.1 LTS" # Product name
369 * In theory other distros could have this LSB file, but none do.
372 parse_lsb_release (guestfs_h *g, struct inspect_fs *fs)
374 const char *filename = "/etc/lsb-release";
380 /* Don't trust guestfs_head_n not to break with very large files.
381 * Check the file size is something reasonable first.
383 size = guestfs_filesize (g, filename);
385 /* guestfs_filesize failed and has already set error in handle */
387 if (size > 1000000) {
388 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
393 lines = guestfs_head_n (g, 10, filename);
397 for (i = 0; lines[i] != NULL; ++i) {
398 if (fs->distro == 0 &&
399 STREQ (lines[i], "DISTRIB_ID=Ubuntu")) {
400 fs->distro = OS_DISTRO_UBUNTU;
403 else if (STRPREFIX (lines[i], "DISTRIB_RELEASE=")) {
405 if (match2 (g, &lines[i][16], re_major_minor, &major, &minor)) {
406 fs->major_version = parse_unsigned_int (g, major);
408 if (fs->major_version == -1) {
410 guestfs___free_string_list (lines);
413 fs->minor_version = parse_unsigned_int (g, minor);
415 if (fs->minor_version == -1) {
416 guestfs___free_string_list (lines);
421 else if (fs->product_name == NULL &&
422 (STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=\"") ||
423 STRPREFIX (lines[i], "DISTRIB_DESCRIPTION='"))) {
424 size_t len = strlen (lines[i]) - 21 - 1;
425 fs->product_name = safe_strndup (g, &lines[i][21], len);
428 else if (fs->product_name == NULL &&
429 STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=")) {
430 size_t len = strlen (lines[i]) - 20;
431 fs->product_name = safe_strndup (g, &lines[i][20], len);
436 guestfs___free_string_list (lines);
440 /* The currently mounted device is known to be a Linux root. Try to
441 * determine from this the distro, version, etc. Also parse
442 * /etc/fstab to determine the arrangement of mountpoints and
443 * associated devices.
446 check_linux_root (guestfs_h *g, struct inspect_fs *fs)
450 fs->type = OS_TYPE_LINUX;
452 if (guestfs_exists (g, "/etc/lsb-release") > 0) {
453 r = parse_lsb_release (g, fs);
454 if (r == -1) /* error */
456 if (r == 1) /* ok - detected the release from this file */
457 goto skip_release_checks;
460 if (guestfs_exists (g, "/etc/redhat-release") > 0) {
461 fs->distro = OS_DISTRO_REDHAT_BASED; /* Something generic Red Hat-like. */
463 if (parse_release_file (g, fs, "/etc/redhat-release") == -1)
467 if ((major = match1 (g, fs->product_name, re_fedora)) != NULL) {
468 fs->distro = OS_DISTRO_FEDORA;
469 fs->major_version = parse_unsigned_int (g, major);
471 if (fs->major_version == -1)
474 else if (match2 (g, fs->product_name, re_rhel_old, &major, &minor) ||
475 match2 (g, fs->product_name, re_rhel, &major, &minor)) {
476 fs->distro = OS_DISTRO_RHEL;
477 fs->major_version = parse_unsigned_int (g, major);
479 if (fs->major_version == -1) {
483 fs->minor_version = parse_unsigned_int (g, minor);
485 if (fs->minor_version == -1)
488 else if ((major = match1 (g, fs->product_name, re_rhel_no_minor)) != NULL) {
489 fs->distro = OS_DISTRO_RHEL;
490 fs->major_version = parse_unsigned_int (g, major);
492 if (fs->major_version == -1)
494 fs->minor_version = 0;
497 else if (guestfs_exists (g, "/etc/debian_version") > 0) {
498 fs->distro = OS_DISTRO_DEBIAN;
500 if (parse_release_file (g, fs, "/etc/debian_version") == -1)
503 if (parse_major_minor (g, fs) == -1)
506 else if (guestfs_exists (g, "/etc/pardus-release") > 0) {
507 fs->distro = OS_DISTRO_PARDUS;
509 if (parse_release_file (g, fs, "/etc/pardus-release") == -1)
512 if (parse_major_minor (g, fs) == -1)
515 else if (guestfs_exists (g, "/etc/arch-release") > 0) {
516 fs->distro = OS_DISTRO_ARCHLINUX;
518 /* /etc/arch-release file is empty and I can't see a way to
519 * determine the actual release or product string.
522 else if (guestfs_exists (g, "/etc/gentoo-release") > 0) {
523 fs->distro = OS_DISTRO_GENTOO;
525 if (parse_release_file (g, fs, "/etc/gentoo-release") == -1)
528 if (parse_major_minor (g, fs) == -1)
531 else if (guestfs_exists (g, "/etc/meego-release") > 0) {
532 fs->distro = OS_DISTRO_MEEGO;
534 if (parse_release_file (g, fs, "/etc/meego-release") == -1)
537 if (parse_major_minor (g, fs) == -1)
541 skip_release_checks:;
543 /* Determine the architecture. */
544 check_architecture (g, fs);
546 /* We already know /etc/fstab exists because it's part of the test
547 * for Linux root above. We must now parse this file to determine
548 * which filesystems are used by the operating system and how they
551 if (inspect_with_augeas (g, fs, "/etc/fstab", check_fstab) == -1)
558 check_architecture (guestfs_h *g, struct inspect_fs *fs)
560 const char *binaries[] =
561 { "/bin/bash", "/bin/ls", "/bin/echo", "/bin/rm", "/bin/sh" };
564 for (i = 0; i < sizeof binaries / sizeof binaries[0]; ++i) {
565 if (guestfs_is_file (g, binaries[i]) > 0) {
566 /* Ignore errors from file_architecture call. */
567 guestfs_error_handler_cb old_error_cb = g->error_cb;
569 char *arch = guestfs_file_architecture (g, binaries[i]);
570 g->error_cb = old_error_cb;
573 /* String will be owned by handle, freed by
574 * guestfs___free_inspect_info.
584 check_fstab (guestfs_h *g, struct inspect_fs *fs)
586 char **lines = guestfs_aug_ls (g, "/files/etc/fstab");
590 if (lines[0] == NULL) {
591 error (g, _("could not parse /etc/fstab or empty file"));
592 guestfs___free_string_list (lines);
598 for (i = 0; lines[i] != NULL; ++i) {
599 /* Ignore comments. Only care about sequence lines which
602 if (match (g, lines[i], re_aug_seq)) {
603 snprintf (augpath, sizeof augpath, "%s/spec", lines[i]);
604 char *spec = guestfs_aug_get (g, augpath);
606 guestfs___free_string_list (lines);
610 snprintf (augpath, sizeof augpath, "%s/file", lines[i]);
611 char *mp = guestfs_aug_get (g, augpath);
613 guestfs___free_string_list (lines);
618 int r = add_fstab_entry (g, fs, spec, mp);
623 guestfs___free_string_list (lines);
629 guestfs___free_string_list (lines);
633 /* Add a filesystem and possibly a mountpoint entry for
634 * the root filesystem 'fs'.
636 * 'spec' is the fstab spec field, which might be a device name or a
637 * pseudodevice or 'UUID=...' or 'LABEL=...'.
639 * 'mp' is the mount point, which could also be 'swap' or 'none'.
642 add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
643 const char *spec, const char *mp)
645 /* Ignore certain mountpoints. */
646 if (STRPREFIX (mp, "/dev/") ||
647 STREQ (mp, "/dev") ||
648 STRPREFIX (mp, "/media/") ||
649 STRPREFIX (mp, "/proc/") ||
650 STREQ (mp, "/proc") ||
651 STRPREFIX (mp, "/selinux/") ||
652 STREQ (mp, "/selinux") ||
653 STRPREFIX (mp, "/sys/") ||
657 /* Ignore /dev/fd (floppy disks) (RHBZ#642929) and CD-ROM drives. */
658 if ((STRPREFIX (spec, "/dev/fd") && c_isdigit (spec[7])) ||
659 STREQ (spec, "/dev/floppy") ||
660 STREQ (spec, "/dev/cdrom"))
663 /* Resolve UUID= and LABEL= to the actual device. */
665 if (STRPREFIX (spec, "UUID="))
666 device = guestfs_findfs_uuid (g, &spec[5]);
667 else if (STRPREFIX (spec, "LABEL="))
668 device = guestfs_findfs_label (g, &spec[6]);
669 /* Ignore "/.swap" (Pardus) and pseudo-devices like "tmpfs". */
670 else if (STRPREFIX (spec, "/dev/"))
671 /* Resolve guest block device names. */
672 device = resolve_fstab_device (g, spec);
674 /* If we haven't resolved the device successfully by this point,
675 * we don't care, just ignore it.
680 char *mountpoint = safe_strdup (g, mp);
682 /* Add this to the fstab entry in 'fs'.
683 * Note these are further filtered by guestfs_inspect_get_mountpoints
684 * and guestfs_inspect_get_filesystems.
686 size_t n = fs->nr_fstab + 1;
687 struct inspect_fstab_entry *p;
689 p = realloc (fs->fstab, n * sizeof (struct inspect_fstab_entry));
691 perrorf (g, "realloc");
700 /* These are owned by the handle and freed by guestfs___free_inspect_info. */
701 fs->fstab[n-1].device = device;
702 fs->fstab[n-1].mountpoint = mountpoint;
705 fprintf (stderr, "fstab: device=%s mountpoint=%s\n", device, mountpoint);
710 /* Resolve block device name to the libguestfs device name, eg.
711 * /dev/xvdb1 => /dev/vdb1; and /dev/mapper/VG-LV => /dev/VG/LV. This
712 * assumes that disks were added in the same order as they appear to
713 * the real VM, which is a reasonable assumption to make. Return
714 * anything we don't recognize unchanged.
717 resolve_fstab_device (guestfs_h *g, const char *spec)
722 if (STRPREFIX (spec, "/dev/mapper/")) {
723 /* LVM2 does some strange munging on /dev/mapper paths for VGs and
724 * LVs which contain '-' character:
726 * ><fs> lvcreate LV--test VG--test 32
727 * ><fs> debug ls /dev/mapper
728 * VG----test-LV----test
730 * This makes it impossible to reverse those paths directly, so
731 * we have implemented lvm_canonical_lv_name in the daemon.
733 device = guestfs_lvm_canonical_lv_name (g, spec);
735 else if ((a1 = match1 (g, spec, re_xdev)) != NULL) {
736 char **devices = guestfs_list_devices (g);
741 for (count = 0; devices[count] != NULL; count++)
744 size_t i = a1[0] - 'a'; /* a1[0] is always [a-z] because of regex. */
746 size_t len = strlen (devices[i]) + strlen (a1) + 16;
747 device = safe_malloc (g, len);
748 snprintf (device, len, "%s%s", devices[i], &a1[1]);
752 guestfs___free_string_list (devices);
755 /* Didn't match device pattern, return original spec unchanged. */
756 device = safe_strdup (g, spec);
762 /* XXX Handling of boot.ini in the Perl version was pretty broken. It
763 * essentially didn't do anything for modern Windows guests.
764 * Therefore I've omitted all that code.
767 check_windows_root (guestfs_h *g, struct inspect_fs *fs)
769 fs->type = OS_TYPE_WINDOWS;
770 fs->distro = OS_DISTRO_WINDOWS;
772 /* Try to find Windows systemroot using some common locations. */
773 const char *systemroots[] =
774 { "/windows", "/winnt", "/win32", "/win" };
776 char *systemroot = NULL;
778 systemroot == NULL && i < sizeof systemroots / sizeof systemroots[0];
780 systemroot = resolve_windows_path_silently (g, systemroots[i]);
784 error (g, _("cannot resolve Windows %%SYSTEMROOT%%"));
789 fprintf (stderr, "windows %%SYSTEMROOT%% = %s", systemroot);
791 /* Freed by guestfs___free_inspect_info. */
792 fs->windows_systemroot = systemroot;
794 if (check_windows_arch (g, fs) == -1)
797 if (check_windows_registry (g, fs) == -1)
804 check_windows_arch (guestfs_h *g, struct inspect_fs *fs)
806 size_t len = strlen (fs->windows_systemroot) + 32;
808 snprintf (cmd_exe, len, "%s/system32/cmd.exe", fs->windows_systemroot);
810 char *cmd_exe_path = resolve_windows_path_silently (g, cmd_exe);
814 char *arch = guestfs_file_architecture (g, cmd_exe_path);
818 fs->arch = arch; /* freed by guestfs___free_inspect_info */
823 /* At the moment, pull just the ProductName and version numbers from
824 * the registry. In future there is a case for making many more
825 * registry fields available to callers.
828 check_windows_registry (guestfs_h *g, struct inspect_fs *fs)
830 TMP_TEMPLATE_ON_STACK (software_local);
832 size_t len = strlen (fs->windows_systemroot) + 64;
834 snprintf (software, len, "%s/system32/config/software",
835 fs->windows_systemroot);
837 char *software_path = resolve_windows_path_silently (g, software);
839 /* If the software hive doesn't exist, just accept that we cannot
840 * find product_name etc.
846 hive_value_h *values = NULL;
848 if (download_to_tmp (g, software_path, software_local, 100000000) == -1)
851 h = hivex_open (software_local, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
853 perrorf (g, "hivex_open");
857 hive_node_h node = hivex_root (h);
858 const char *hivepath[] =
859 { "Microsoft", "Windows NT", "CurrentVersion" };
862 node != 0 && i < sizeof hivepath / sizeof hivepath[0];
864 node = hivex_node_get_child (h, node, hivepath[i]);
868 perrorf (g, "hivex: cannot locate HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
872 values = hivex_node_values (h, node);
874 for (i = 0; values[i] != 0; ++i) {
875 char *key = hivex_value_key (h, values[i]);
877 perrorf (g, "hivex_value_key");
881 if (STRCASEEQ (key, "ProductName")) {
882 fs->product_name = hivex_value_string (h, values[i]);
883 if (!fs->product_name) {
884 perrorf (g, "hivex_value_string");
889 else if (STRCASEEQ (key, "CurrentVersion")) {
890 char *version = hivex_value_string (h, values[i]);
892 perrorf (g, "hivex_value_string");
897 if (match2 (g, version, re_windows_version, &major, &minor)) {
898 fs->major_version = parse_unsigned_int (g, major);
900 if (fs->major_version == -1) {
906 fs->minor_version = parse_unsigned_int (g, minor);
908 if (fs->minor_version == -1) {
924 if (h) hivex_close (h);
926 free (software_path);
928 /* Free up the temporary file. */
929 unlink (software_local);
930 #undef software_local_len
936 resolve_windows_path_silently (guestfs_h *g, const char *path)
938 guestfs_error_handler_cb old_error_cb = g->error_cb;
940 char *ret = guestfs_case_sensitive_path (g, path);
941 g->error_cb = old_error_cb;
946 extend_fses (guestfs_h *g)
948 size_t n = g->nr_fses + 1;
949 struct inspect_fs *p;
951 p = realloc (g->fses, n * sizeof (struct inspect_fs));
953 perrorf (g, "realloc");
960 memset (&g->fses[n-1], 0, sizeof (struct inspect_fs));
965 /* Parse small, unsigned ints, as used in version numbers. */
967 parse_unsigned_int (guestfs_h *g, const char *str)
970 int r = xstrtol (str, NULL, 10, &ret, "");
971 if (r != LONGINT_OK) {
972 error (g, _("could not parse integer in version number: %s"), str);
978 static struct inspect_fs *
979 search_for_root (guestfs_h *g, const char *root)
981 if (g->nr_fses == 0) {
982 error (g, _("no inspection data: call guestfs_inspect_os first"));
987 struct inspect_fs *fs;
988 for (i = 0; i < g->nr_fses; ++i) {
990 if (fs->is_root && STREQ (root, fs->device))
994 error (g, _("%s: root device not found: only call this function with a root device previously returned by guestfs_inspect_os"),
1000 guestfs__inspect_get_type (guestfs_h *g, const char *root)
1002 struct inspect_fs *fs = search_for_root (g, root);
1008 case OS_TYPE_LINUX: ret = safe_strdup (g, "linux"); break;
1009 case OS_TYPE_WINDOWS: ret = safe_strdup (g, "windows"); break;
1010 case OS_TYPE_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1017 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
1019 struct inspect_fs *fs = search_for_root (g, root);
1023 return safe_strdup (g, fs->arch ? : "unknown");
1027 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
1029 struct inspect_fs *fs = search_for_root (g, root);
1034 switch (fs->distro) {
1035 case OS_DISTRO_ARCHLINUX: ret = safe_strdup (g, "archlinux"); break;
1036 case OS_DISTRO_DEBIAN: ret = safe_strdup (g, "debian"); break;
1037 case OS_DISTRO_FEDORA: ret = safe_strdup (g, "fedora"); break;
1038 case OS_DISTRO_GENTOO: ret = safe_strdup (g, "gentoo"); break;
1039 case OS_DISTRO_MEEGO: ret = safe_strdup (g, "meego"); break;
1040 case OS_DISTRO_PARDUS: ret = safe_strdup (g, "pardus"); break;
1041 case OS_DISTRO_REDHAT_BASED: ret = safe_strdup (g, "redhat-based"); break;
1042 case OS_DISTRO_RHEL: ret = safe_strdup (g, "rhel"); break;
1043 case OS_DISTRO_WINDOWS: ret = safe_strdup (g, "windows"); break;
1044 case OS_DISTRO_UBUNTU: ret = safe_strdup (g, "ubuntu"); break;
1045 case OS_DISTRO_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1052 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
1054 struct inspect_fs *fs = search_for_root (g, root);
1058 return fs->major_version;
1062 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
1064 struct inspect_fs *fs = search_for_root (g, root);
1068 return fs->minor_version;
1072 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
1074 struct inspect_fs *fs = search_for_root (g, root);
1078 return safe_strdup (g, fs->product_name ? : "unknown");
1082 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
1084 struct inspect_fs *fs = search_for_root (g, root);
1088 if (!fs->windows_systemroot) {
1089 error (g, _("not a Windows guest, or systemroot could not be determined"));
1093 return safe_strdup (g, fs->windows_systemroot);
1097 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
1099 struct inspect_fs *fs = search_for_root (g, root);
1105 /* If no fstab information (Windows) return just the root. */
1106 if (fs->nr_fstab == 0) {
1107 ret = calloc (3, sizeof (char *));
1108 ret[0] = safe_strdup (g, "/");
1109 ret[1] = safe_strdup (g, root);
1114 #define CRITERION fs->fstab[i].mountpoint[0] == '/'
1115 size_t i, count = 0;
1116 for (i = 0; i < fs->nr_fstab; ++i)
1120 /* Hashtables have 2N+1 entries. */
1121 ret = calloc (2*count+1, sizeof (char *));
1123 perrorf (g, "calloc");
1128 for (i = 0; i < fs->nr_fstab; ++i)
1130 ret[2*count] = safe_strdup (g, fs->fstab[i].mountpoint);
1131 ret[2*count+1] = safe_strdup (g, fs->fstab[i].device);
1140 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
1142 struct inspect_fs *fs = search_for_root (g, root);
1148 /* If no fstab information (Windows) return just the root. */
1149 if (fs->nr_fstab == 0) {
1150 ret = calloc (2, sizeof (char *));
1151 ret[0] = safe_strdup (g, root);
1156 ret = calloc (fs->nr_fstab + 1, sizeof (char *));
1158 perrorf (g, "calloc");
1163 for (i = 0; i < fs->nr_fstab; ++i)
1164 ret[i] = safe_strdup (g, fs->fstab[i].device);
1169 /* Download to a guest file to a local temporary file. Refuse to
1170 * download the guest file if it is larger than max_size. The caller
1171 * is responsible for deleting the temporary file after use.
1174 download_to_tmp (guestfs_h *g, const char *filename,
1175 char *localtmp, int64_t max_size)
1181 size = guestfs_filesize (g, filename);
1183 /* guestfs_filesize failed and has already set error in handle */
1185 if (size > max_size) {
1186 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
1191 fd = mkstemp (localtmp);
1193 perrorf (g, "mkstemp");
1197 snprintf (buf, sizeof buf, "/dev/fd/%d", fd);
1199 if (guestfs_download (g, filename, buf) == -1) {
1205 if (close (fd) == -1) {
1206 perrorf (g, "close: %s", localtmp);
1214 /* Call 'f' with Augeas opened and having parsed 'filename' (this file
1215 * must exist). As a security measure, this bails if the file is too
1216 * large for a reasonable configuration file. After the call to 'f'
1220 inspect_with_augeas (guestfs_h *g, struct inspect_fs *fs, const char *filename,
1221 int (*f) (guestfs_h *, struct inspect_fs *))
1223 /* Security: Refuse to do this if filename is too large. */
1224 int64_t size = guestfs_filesize (g, filename);
1226 /* guestfs_filesize failed and has already set error in handle */
1228 if (size > 100000) {
1229 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
1234 /* If !feature_available (g, "augeas") then the next call will fail.
1235 * Arguably we might want to fall back to a non-Augeas method in
1238 if (guestfs_aug_init (g, "/", 16|32) == -1)
1243 /* Tell Augeas to only load one file (thanks Raphaël Pinson). */
1244 char buf[strlen (filename) + 64];
1245 snprintf (buf, strlen (filename) + 64, "/augeas/load//incl[. != \"%s\"]",
1247 if (guestfs_aug_rm (g, buf) == -1)
1250 if (guestfs_aug_load (g) == -1)
1256 guestfs_aug_close (g);
1261 /* Get the first line of a small file, without any trailing newline
1265 first_line_of_file (guestfs_h *g, const char *filename)
1271 /* Don't trust guestfs_head_n not to break with very large files.
1272 * Check the file size is something reasonable first.
1274 size = guestfs_filesize (g, filename);
1276 /* guestfs_filesize failed and has already set error in handle */
1278 if (size > 1000000) {
1279 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
1284 lines = guestfs_head_n (g, 1, filename);
1287 if (lines[0] == NULL) {
1288 error (g, _("%s: file is empty"), filename);
1289 guestfs___free_string_list (lines);
1292 /* lines[1] should be NULL because of '1' argument above ... */
1294 ret = lines[0]; /* caller frees */
1295 free (lines); /* free the array */
1300 #else /* no PCRE or hivex at compile time */
1302 /* XXX These functions should be in an optgroup. */
1304 #define NOT_IMPL(r) \
1305 error (g, _("inspection API not available since this version of libguestfs was compiled without PCRE or hivex libraries")); \
1309 guestfs__inspect_os (guestfs_h *g)
1315 guestfs__inspect_get_roots (guestfs_h *g)
1321 guestfs__inspect_get_type (guestfs_h *g, const char *root)
1327 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
1333 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
1339 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
1345 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
1351 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
1357 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
1363 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
1369 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
1374 #endif /* no PCRE or hivex at compile time */
1377 guestfs___free_inspect_info (guestfs_h *g)
1380 for (i = 0; i < g->nr_fses; ++i) {
1381 free (g->fses[i].device);
1382 free (g->fses[i].product_name);
1383 free (g->fses[i].arch);
1384 free (g->fses[i].windows_systemroot);
1386 for (j = 0; j < g->fses[i].nr_fstab; ++j) {
1387 free (g->fses[i].fstab[j].device);
1388 free (g->fses[i].fstab[j].mountpoint);
1390 free (g->fses[i].fstab);
1397 /* In the Perl code this is a public function. */
1399 guestfs___feature_available (guestfs_h *g, const char *feature)
1401 /* If there's an error we should ignore it, so to do that we have to
1402 * temporarily replace the error handler with a null one.
1404 guestfs_error_handler_cb old_error_cb = g->error_cb;
1407 const char *groups[] = { feature, NULL };
1408 int r = guestfs_available (g, (char * const *) groups);
1410 g->error_cb = old_error_cb;
1412 return r == 0 ? 1 : 0;
1417 /* Match a regular expression which contains no captures. Returns
1418 * true if it matches or false if it doesn't.
1421 guestfs___match (guestfs_h *g, const char *str, const pcre *re)
1423 size_t len = strlen (str);
1426 r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
1427 if (r == PCRE_ERROR_NOMATCH)
1430 /* Internal error -- should not happen. */
1431 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1432 __FILE__, __func__, r, str);
1439 /* Match a regular expression which contains exactly one capture. If
1440 * the string matches, return the capture, otherwise return NULL. The
1441 * caller must free the result.
1444 guestfs___match1 (guestfs_h *g, const char *str, const pcre *re)
1446 size_t len = strlen (str);
1449 r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
1450 if (r == PCRE_ERROR_NOMATCH)
1453 /* Internal error -- should not happen. */
1454 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1455 __FILE__, __func__, r, str);
1459 return safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
1462 /* Match a regular expression which contains exactly two captures. */
1464 guestfs___match2 (guestfs_h *g, const char *str, const pcre *re,
1465 char **ret1, char **ret2)
1467 size_t len = strlen (str);
1470 r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
1471 if (r == PCRE_ERROR_NOMATCH)
1474 /* Internal error -- should not happen. */
1475 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1476 __FILE__, __func__, r, str);
1480 *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
1481 *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
1486 /* Match a regular expression which contains exactly three captures. */
1488 guestfs___match3 (guestfs_h *g, const char *str, const pcre *re,
1489 char **ret1, char **ret2, char **ret3)
1491 size_t len = strlen (str);
1494 r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
1495 if (r == PCRE_ERROR_NOMATCH)
1498 /* Internal error -- should not happen. */
1499 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
1500 __FILE__, __func__, r, str);
1504 *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
1505 *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
1506 *ret3 = safe_strndup (g, &str[vec[6]], vec[7]-vec[6]);
1511 #endif /* HAVE_PCRE */