2 * Copyright (C) 2010 Red Hat Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
38 #include "ignore-value.h"
42 #include "guestfs-internal.h"
43 #include "guestfs-internal-actions.h"
44 #include "guestfs_protocol.h"
46 #if defined(HAVE_PCRE) && defined(HAVE_HIVEX)
48 /* Compile all the regular expressions once when the shared library is
49 * loaded. PCRE is thread safe so we're supposedly OK here if
50 * multiple threads call into the libguestfs API functions below
53 static pcre *re_fedora;
54 static pcre *re_rhel_old;
56 static pcre *re_rhel_no_minor;
57 static pcre *re_major_minor;
58 static pcre *re_aug_seq;
60 static pcre *re_first_partition;
61 static pcre *re_freebsd;
62 static pcre *re_windows_version;
64 static void compile_regexps (void) __attribute__((constructor));
65 static void free_regexps (void) __attribute__((destructor));
68 compile_regexps (void)
73 #define COMPILE(re,pattern,options) \
75 re = pcre_compile ((pattern), (options), &err, &offset, NULL); \
77 ignore_value (write (2, err, strlen (err))); \
82 COMPILE (re_fedora, "Fedora release (\\d+)", 0);
84 "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+).*Update (\\d+)", 0);
86 "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+)\\.(\\d+)", 0);
87 COMPILE (re_rhel_no_minor,
88 "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+)", 0);
89 COMPILE (re_major_minor, "(\\d+)\\.(\\d+)", 0);
90 COMPILE (re_aug_seq, "/\\d+$", 0);
91 COMPILE (re_xdev, "^/dev/(?:h|s|v|xv)d([a-z]\\d*)$", 0);
92 COMPILE (re_first_partition, "^/dev/(?:h|s|v)d.1$", 0);
93 COMPILE (re_freebsd, "^/dev/ad(\\d+)s(\\d+)([a-z])$", 0);
94 COMPILE (re_windows_version, "^(\\d+)\\.(\\d+)", 0);
100 pcre_free (re_fedora);
101 pcre_free (re_rhel_old);
103 pcre_free (re_rhel_no_minor);
104 pcre_free (re_major_minor);
105 pcre_free (re_aug_seq);
107 pcre_free (re_first_partition);
108 pcre_free (re_freebsd);
109 pcre_free (re_windows_version);
112 /* The main inspection code. */
113 static int check_for_filesystem_on (guestfs_h *g, const char *device);
116 guestfs__inspect_os (guestfs_h *g)
118 /* Remove any information previously stored in the handle. */
119 guestfs___free_inspect_info (g);
121 if (guestfs_umount_all (g) == -1)
124 /* Iterate over all possible devices. Try to mount each
125 * (read-only). Examine ones which contain filesystems and add that
126 * information to the handle.
128 /* Look to see if any devices directly contain filesystems (RHBZ#590167). */
130 devices = guestfs_list_devices (g);
135 for (i = 0; devices[i] != NULL; ++i) {
136 if (check_for_filesystem_on (g, devices[i]) == -1) {
137 guestfs___free_string_list (devices);
138 guestfs___free_inspect_info (g);
142 guestfs___free_string_list (devices);
144 /* Look at all partitions. */
146 partitions = guestfs_list_partitions (g);
147 if (partitions == NULL) {
148 guestfs___free_inspect_info (g);
152 for (i = 0; partitions[i] != NULL; ++i) {
153 if (check_for_filesystem_on (g, partitions[i]) == -1) {
154 guestfs___free_string_list (partitions);
155 guestfs___free_inspect_info (g);
159 guestfs___free_string_list (partitions);
161 /* Look at all LVs. */
162 if (guestfs___feature_available (g, "lvm2")) {
164 lvs = guestfs_lvs (g);
166 guestfs___free_inspect_info (g);
170 for (i = 0; lvs[i] != NULL; ++i) {
171 if (check_for_filesystem_on (g, lvs[i]) == -1) {
172 guestfs___free_string_list (lvs);
173 guestfs___free_inspect_info (g);
177 guestfs___free_string_list (lvs);
180 /* At this point we have, in the handle, a list of all filesystems
181 * found and data about each one. Now we assemble the list of
182 * filesystems which are root devices and return that to the user.
183 * Fall through to guestfs__inspect_get_roots to do that.
185 char **ret = guestfs__inspect_get_roots (g);
187 guestfs___free_inspect_info (g);
191 /* Find out if 'device' contains a filesystem. If it does, add
192 * another entry in g->fses.
194 static int check_filesystem (guestfs_h *g, const char *device);
195 static int check_linux_root (guestfs_h *g, struct inspect_fs *fs);
196 static int check_freebsd_root (guestfs_h *g, struct inspect_fs *fs);
197 static void check_architecture (guestfs_h *g, struct inspect_fs *fs);
198 static int check_hostname_unix (guestfs_h *g, struct inspect_fs *fs);
199 static int check_hostname_redhat (guestfs_h *g, struct inspect_fs *fs);
200 static int check_hostname_freebsd (guestfs_h *g, struct inspect_fs *fs);
201 static int check_fstab (guestfs_h *g, struct inspect_fs *fs);
202 static int check_windows_root (guestfs_h *g, struct inspect_fs *fs);
203 static int check_windows_arch (guestfs_h *g, struct inspect_fs *fs);
204 static int check_windows_software_registry (guestfs_h *g, struct inspect_fs *fs);
205 static int check_windows_system_registry (guestfs_h *g, struct inspect_fs *fs);
206 static char *resolve_windows_path_silently (guestfs_h *g, const char *);
207 static int extend_fses (guestfs_h *g);
208 static int parse_unsigned_int (guestfs_h *g, const char *str);
209 static int add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
210 const char *spec, const char *mp);
211 static char *resolve_fstab_device (guestfs_h *g, const char *spec);
212 static void check_package_format (guestfs_h *g, struct inspect_fs *fs);
213 static void check_package_management (guestfs_h *g, struct inspect_fs *fs);
214 static int download_to_tmp (guestfs_h *g, const char *filename, char *localtmp, int64_t max_size);
215 static int inspect_with_augeas (guestfs_h *g, struct inspect_fs *fs, const char *filename, int (*f) (guestfs_h *, struct inspect_fs *));
216 static char *first_line_of_file (guestfs_h *g, const char *filename);
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;
275 /* Optimize some of the tests by avoiding multiple tests of the same thing. */
276 int is_dir_etc = guestfs_is_dir (g, "/etc") > 0;
277 int is_dir_bin = guestfs_is_dir (g, "/bin") > 0;
278 int is_dir_share = guestfs_is_dir (g, "/share") > 0;
281 if (guestfs_is_file (g, "/grub/menu.lst") > 0 ||
282 guestfs_is_file (g, "/grub/grub.conf") > 0)
283 fs->content = FS_CONTENT_LINUX_BOOT;
285 else if (is_dir_etc &&
287 guestfs_is_file (g, "/etc/freebsd-update.conf") > 0 &&
288 guestfs_is_file (g, "/etc/fstab") > 0) {
289 /* Ignore /dev/sda1 which is a shadow of the real root filesystem
290 * that is probably /dev/sda5 (see:
291 * http://www.freebsd.org/doc/handbook/disk-organization.html)
293 if (match (g, device, re_first_partition))
297 fs->content = FS_CONTENT_FREEBSD_ROOT;
298 if (check_freebsd_root (g, fs) == -1)
302 else if (is_dir_etc &&
304 guestfs_is_file (g, "/etc/fstab") > 0) {
306 fs->content = FS_CONTENT_LINUX_ROOT;
307 if (check_linux_root (g, fs) == -1)
310 /* Linux /usr/local? */
311 else if (is_dir_etc &&
314 guestfs_exists (g, "/local") == 0 &&
315 guestfs_is_file (g, "/etc/fstab") == 0)
316 fs->content = FS_CONTENT_LINUX_USR_LOCAL;
318 else if (is_dir_etc &&
321 guestfs_exists (g, "/local") > 0 &&
322 guestfs_is_file (g, "/etc/fstab") == 0)
323 fs->content = FS_CONTENT_LINUX_USR;
325 else if (guestfs_is_dir (g, "/log") > 0 &&
326 guestfs_is_dir (g, "/run") > 0 &&
327 guestfs_is_dir (g, "/spool") > 0)
328 fs->content = FS_CONTENT_LINUX_VAR;
330 else if (guestfs_is_file (g, "/AUTOEXEC.BAT") > 0 ||
331 guestfs_is_file (g, "/autoexec.bat") > 0 ||
332 guestfs_is_dir (g, "/Program Files") > 0 ||
333 guestfs_is_dir (g, "/WINDOWS") > 0 ||
334 guestfs_is_dir (g, "/Windows") > 0 ||
335 guestfs_is_dir (g, "/windows") > 0 ||
336 guestfs_is_dir (g, "/WIN32") > 0 ||
337 guestfs_is_dir (g, "/Win32") > 0 ||
338 guestfs_is_dir (g, "/WINNT") > 0 ||
339 guestfs_is_file (g, "/boot.ini") > 0 ||
340 guestfs_is_file (g, "/ntldr") > 0) {
342 fs->content = FS_CONTENT_WINDOWS_ROOT;
343 if (check_windows_root (g, fs) == -1)
350 /* Set fs->product_name to the first line of the release file. */
352 parse_release_file (guestfs_h *g, struct inspect_fs *fs,
353 const char *release_filename)
355 fs->product_name = first_line_of_file (g, release_filename);
356 if (fs->product_name == NULL)
361 /* Parse generic MAJOR.MINOR from the fs->product_name string. */
363 parse_major_minor (guestfs_h *g, struct inspect_fs *fs)
367 if (match2 (g, fs->product_name, re_major_minor, &major, &minor)) {
368 fs->major_version = parse_unsigned_int (g, major);
370 if (fs->major_version == -1) {
374 fs->minor_version = parse_unsigned_int (g, minor);
376 if (fs->minor_version == -1)
382 /* Ubuntu has /etc/lsb-release containing:
383 * DISTRIB_ID=Ubuntu # Distro
384 * DISTRIB_RELEASE=10.04 # Version
385 * DISTRIB_CODENAME=lucid
386 * DISTRIB_DESCRIPTION="Ubuntu 10.04.1 LTS" # Product name
388 * [Ubuntu-derived ...] Linux Mint was found to have this:
389 * DISTRIB_ID=LinuxMint
391 * DISTRIB_CODENAME=julia
392 * DISTRIB_DESCRIPTION="Linux Mint 10 Julia"
393 * Linux Mint also has /etc/linuxmint/info with more information,
394 * but we can use the LSB file.
397 * LSB_VERSION=lsb-4.0-amd64:lsb-4.0-noarch
398 * DISTRIB_ID=MandrivaLinux
399 * DISTRIB_RELEASE=2010.1
400 * DISTRIB_CODENAME=Henry_Farman
401 * DISTRIB_DESCRIPTION="Mandriva Linux 2010.1"
402 * Mandriva also has a normal release file called /etc/mandriva-release.
405 parse_lsb_release (guestfs_h *g, struct inspect_fs *fs)
407 const char *filename = "/etc/lsb-release";
413 /* Don't trust guestfs_head_n not to break with very large files.
414 * Check the file size is something reasonable first.
416 size = guestfs_filesize (g, filename);
418 /* guestfs_filesize failed and has already set error in handle */
420 if (size > 1000000) {
421 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
426 lines = guestfs_head_n (g, 10, filename);
430 for (i = 0; lines[i] != NULL; ++i) {
431 if (fs->distro == 0 &&
432 STREQ (lines[i], "DISTRIB_ID=Ubuntu")) {
433 fs->distro = OS_DISTRO_UBUNTU;
436 else if (fs->distro == 0 &&
437 STREQ (lines[i], "DISTRIB_ID=LinuxMint")) {
438 fs->distro = OS_DISTRO_LINUX_MINT;
441 else if (fs->distro == 0 &&
442 STREQ (lines[i], "DISTRIB_ID=MandrivaLinux")) {
443 fs->distro = OS_DISTRO_MANDRIVA;
446 else if (STRPREFIX (lines[i], "DISTRIB_RELEASE=")) {
448 if (match2 (g, &lines[i][16], re_major_minor, &major, &minor)) {
449 fs->major_version = parse_unsigned_int (g, major);
451 if (fs->major_version == -1) {
453 guestfs___free_string_list (lines);
456 fs->minor_version = parse_unsigned_int (g, minor);
458 if (fs->minor_version == -1) {
459 guestfs___free_string_list (lines);
464 else if (fs->product_name == NULL &&
465 (STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=\"") ||
466 STRPREFIX (lines[i], "DISTRIB_DESCRIPTION='"))) {
467 size_t len = strlen (lines[i]) - 21 - 1;
468 fs->product_name = safe_strndup (g, &lines[i][21], len);
471 else if (fs->product_name == NULL &&
472 STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=")) {
473 size_t len = strlen (lines[i]) - 20;
474 fs->product_name = safe_strndup (g, &lines[i][20], len);
479 guestfs___free_string_list (lines);
483 /* The currently mounted device is known to be a Linux root. Try to
484 * determine from this the distro, version, etc. Also parse
485 * /etc/fstab to determine the arrangement of mountpoints and
486 * associated devices.
489 check_linux_root (guestfs_h *g, struct inspect_fs *fs)
493 fs->type = OS_TYPE_LINUX;
495 if (guestfs_exists (g, "/etc/lsb-release") > 0) {
496 r = parse_lsb_release (g, fs);
497 if (r == -1) /* error */
499 if (r == 1) /* ok - detected the release from this file */
500 goto skip_release_checks;
503 if (guestfs_exists (g, "/etc/redhat-release") > 0) {
504 fs->distro = OS_DISTRO_REDHAT_BASED; /* Something generic Red Hat-like. */
506 if (parse_release_file (g, fs, "/etc/redhat-release") == -1)
510 if ((major = match1 (g, fs->product_name, re_fedora)) != NULL) {
511 fs->distro = OS_DISTRO_FEDORA;
512 fs->major_version = parse_unsigned_int (g, major);
514 if (fs->major_version == -1)
517 else if (match2 (g, fs->product_name, re_rhel_old, &major, &minor) ||
518 match2 (g, fs->product_name, re_rhel, &major, &minor)) {
519 fs->distro = OS_DISTRO_RHEL;
520 fs->major_version = parse_unsigned_int (g, major);
522 if (fs->major_version == -1) {
526 fs->minor_version = parse_unsigned_int (g, minor);
528 if (fs->minor_version == -1)
531 else if ((major = match1 (g, fs->product_name, re_rhel_no_minor)) != NULL) {
532 fs->distro = OS_DISTRO_RHEL;
533 fs->major_version = parse_unsigned_int (g, major);
535 if (fs->major_version == -1)
537 fs->minor_version = 0;
540 else if (guestfs_exists (g, "/etc/debian_version") > 0) {
541 fs->distro = OS_DISTRO_DEBIAN;
543 if (parse_release_file (g, fs, "/etc/debian_version") == -1)
546 if (parse_major_minor (g, fs) == -1)
549 else if (guestfs_exists (g, "/etc/pardus-release") > 0) {
550 fs->distro = OS_DISTRO_PARDUS;
552 if (parse_release_file (g, fs, "/etc/pardus-release") == -1)
555 if (parse_major_minor (g, fs) == -1)
558 else if (guestfs_exists (g, "/etc/arch-release") > 0) {
559 fs->distro = OS_DISTRO_ARCHLINUX;
561 /* /etc/arch-release file is empty and I can't see a way to
562 * determine the actual release or product string.
565 else if (guestfs_exists (g, "/etc/gentoo-release") > 0) {
566 fs->distro = OS_DISTRO_GENTOO;
568 if (parse_release_file (g, fs, "/etc/gentoo-release") == -1)
571 if (parse_major_minor (g, fs) == -1)
574 else if (guestfs_exists (g, "/etc/meego-release") > 0) {
575 fs->distro = OS_DISTRO_MEEGO;
577 if (parse_release_file (g, fs, "/etc/meego-release") == -1)
580 if (parse_major_minor (g, fs) == -1)
584 skip_release_checks:;
586 /* If distro test above was successful, work out the package format. */
587 check_package_format (g, fs);
588 check_package_management (g, fs);
590 /* Determine the architecture. */
591 check_architecture (g, fs);
593 /* We already know /etc/fstab exists because it's part of the test
594 * for Linux root above. We must now parse this file to determine
595 * which filesystems are used by the operating system and how they
598 if (inspect_with_augeas (g, fs, "/etc/fstab", check_fstab) == -1)
601 /* Determine hostname. */
602 if (check_hostname_unix (g, fs) == -1)
608 /* The currently mounted device is known to be a FreeBSD root. */
610 check_freebsd_root (guestfs_h *g, struct inspect_fs *fs)
612 fs->type = OS_TYPE_FREEBSD;
614 /* FreeBSD has no authoritative version file. The version number is
615 * in /etc/motd, which the system administrator might edit, but
616 * we'll use that anyway.
619 if (guestfs_exists (g, "/etc/motd") > 0) {
620 if (parse_release_file (g, fs, "/etc/motd") == -1)
623 if (parse_major_minor (g, fs) == -1)
627 /* Determine the architecture. */
628 check_architecture (g, fs);
630 /* We already know /etc/fstab exists because it's part of the test above. */
631 if (inspect_with_augeas (g, fs, "/etc/fstab", check_fstab) == -1)
634 /* Determine hostname. */
635 if (check_hostname_unix (g, fs) == -1)
642 check_architecture (guestfs_h *g, struct inspect_fs *fs)
644 const char *binaries[] =
645 { "/bin/bash", "/bin/ls", "/bin/echo", "/bin/rm", "/bin/sh" };
648 for (i = 0; i < sizeof binaries / sizeof binaries[0]; ++i) {
649 if (guestfs_is_file (g, binaries[i]) > 0) {
650 /* Ignore errors from file_architecture call. */
651 guestfs_error_handler_cb old_error_cb = g->error_cb;
653 char *arch = guestfs_file_architecture (g, binaries[i]);
654 g->error_cb = old_error_cb;
657 /* String will be owned by handle, freed by
658 * guestfs___free_inspect_info.
667 /* Try several methods to determine the hostname from a Linux or
668 * FreeBSD guest. Note that type and distro have been set, so we can
669 * use that information to direct the search.
672 check_hostname_unix (guestfs_h *g, struct inspect_fs *fs)
676 /* Red Hat-derived would be in /etc/sysconfig/network, and
677 * Debian-derived in the file /etc/hostname. Very old Debian and
678 * SUSE use /etc/HOSTNAME. It's best to just look for each of
679 * these files in turn, rather than try anything clever based on
682 if (guestfs_is_file (g, "/etc/HOSTNAME")) {
683 fs->hostname = first_line_of_file (g, "/etc/HOSTNAME");
684 if (fs->hostname == NULL)
687 else if (guestfs_is_file (g, "/etc/hostname")) {
688 fs->hostname = first_line_of_file (g, "/etc/hostname");
689 if (fs->hostname == NULL)
692 else if (guestfs_is_file (g, "/etc/sysconfig/network")) {
693 if (inspect_with_augeas (g, fs, "/etc/sysconfig/network",
694 check_hostname_redhat) == -1)
699 case OS_TYPE_FREEBSD:
700 /* /etc/rc.conf contains the hostname, but there is no Augeas lens
703 if (guestfs_is_file (g, "/etc/rc.conf")) {
704 if (check_hostname_freebsd (g, fs) == -1)
709 case OS_TYPE_WINDOWS: /* not here, see check_windows_system_registry */
710 case OS_TYPE_UNKNOWN:
712 /* nothing, keep GCC warnings happy */;
718 /* Parse the hostname from /etc/sysconfig/network. This must be called
719 * from the inspect_with_augeas wrapper.
722 check_hostname_redhat (guestfs_h *g, struct inspect_fs *fs)
726 hostname = guestfs_aug_get (g, "/files/etc/sysconfig/network/HOSTNAME");
730 fs->hostname = hostname; /* freed by guestfs___free_inspect_info */
734 /* Parse the hostname from /etc/rc.conf. On FreeBSD this file
735 * contains comments, blank lines and:
736 * hostname="freebsd8.example.com"
737 * ifconfig_re0="DHCP"
742 check_hostname_freebsd (guestfs_h *g, struct inspect_fs *fs)
744 const char *filename = "/etc/rc.conf";
749 /* Don't trust guestfs_read_lines not to break with very large files.
750 * Check the file size is something reasonable first.
752 size = guestfs_filesize (g, filename);
754 /* guestfs_filesize failed and has already set error in handle */
756 if (size > 1000000) {
757 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
762 lines = guestfs_read_lines (g, filename);
766 for (i = 0; lines[i] != NULL; ++i) {
767 if (STRPREFIX (lines[i], "hostname=\"") ||
768 STRPREFIX (lines[i], "hostname='")) {
769 size_t len = strlen (lines[i]) - 10 - 1;
770 fs->hostname = safe_strndup (g, &lines[i][10], len);
772 } else if (STRPREFIX (lines[i], "hostname=")) {
773 size_t len = strlen (lines[i]) - 9;
774 fs->hostname = safe_strndup (g, &lines[i][9], len);
779 guestfs___free_string_list (lines);
784 check_fstab (guestfs_h *g, struct inspect_fs *fs)
786 char **lines = guestfs_aug_ls (g, "/files/etc/fstab");
790 if (lines[0] == NULL) {
791 error (g, _("could not parse /etc/fstab or empty file"));
792 guestfs___free_string_list (lines);
798 for (i = 0; lines[i] != NULL; ++i) {
799 /* Ignore comments. Only care about sequence lines which
802 if (match (g, lines[i], re_aug_seq)) {
803 snprintf (augpath, sizeof augpath, "%s/spec", lines[i]);
804 char *spec = guestfs_aug_get (g, augpath);
806 guestfs___free_string_list (lines);
810 snprintf (augpath, sizeof augpath, "%s/file", lines[i]);
811 char *mp = guestfs_aug_get (g, augpath);
813 guestfs___free_string_list (lines);
818 int r = add_fstab_entry (g, fs, spec, mp);
823 guestfs___free_string_list (lines);
829 guestfs___free_string_list (lines);
833 /* Add a filesystem and possibly a mountpoint entry for
834 * the root filesystem 'fs'.
836 * 'spec' is the fstab spec field, which might be a device name or a
837 * pseudodevice or 'UUID=...' or 'LABEL=...'.
839 * 'mp' is the mount point, which could also be 'swap' or 'none'.
842 add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
843 const char *spec, const char *mp)
845 /* Ignore certain mountpoints. */
846 if (STRPREFIX (mp, "/dev/") ||
847 STREQ (mp, "/dev") ||
848 STRPREFIX (mp, "/media/") ||
849 STRPREFIX (mp, "/proc/") ||
850 STREQ (mp, "/proc") ||
851 STRPREFIX (mp, "/selinux/") ||
852 STREQ (mp, "/selinux") ||
853 STRPREFIX (mp, "/sys/") ||
857 /* Ignore /dev/fd (floppy disks) (RHBZ#642929) and CD-ROM drives. */
858 if ((STRPREFIX (spec, "/dev/fd") && c_isdigit (spec[7])) ||
859 STREQ (spec, "/dev/floppy") ||
860 STREQ (spec, "/dev/cdrom"))
863 /* Resolve UUID= and LABEL= to the actual device. */
865 if (STRPREFIX (spec, "UUID="))
866 device = guestfs_findfs_uuid (g, &spec[5]);
867 else if (STRPREFIX (spec, "LABEL="))
868 device = guestfs_findfs_label (g, &spec[6]);
869 /* Ignore "/.swap" (Pardus) and pseudo-devices like "tmpfs". */
870 else if (STRPREFIX (spec, "/dev/"))
871 /* Resolve guest block device names. */
872 device = resolve_fstab_device (g, spec);
874 /* If we haven't resolved the device successfully by this point,
875 * we don't care, just ignore it.
880 char *mountpoint = safe_strdup (g, mp);
882 /* Add this to the fstab entry in 'fs'.
883 * Note these are further filtered by guestfs_inspect_get_mountpoints
884 * and guestfs_inspect_get_filesystems.
886 size_t n = fs->nr_fstab + 1;
887 struct inspect_fstab_entry *p;
889 p = realloc (fs->fstab, n * sizeof (struct inspect_fstab_entry));
891 perrorf (g, "realloc");
900 /* These are owned by the handle and freed by guestfs___free_inspect_info. */
901 fs->fstab[n-1].device = device;
902 fs->fstab[n-1].mountpoint = mountpoint;
905 fprintf (stderr, "fstab: device=%s mountpoint=%s\n", device, mountpoint);
910 /* Resolve block device name to the libguestfs device name, eg.
911 * /dev/xvdb1 => /dev/vdb1; and /dev/mapper/VG-LV => /dev/VG/LV. This
912 * assumes that disks were added in the same order as they appear to
913 * the real VM, which is a reasonable assumption to make. Return
914 * anything we don't recognize unchanged.
917 resolve_fstab_device (guestfs_h *g, const char *spec)
921 char *bsddisk, *bsdslice, *bsdpart;
923 if (STRPREFIX (spec, "/dev/mapper/")) {
924 /* LVM2 does some strange munging on /dev/mapper paths for VGs and
925 * LVs which contain '-' character:
927 * ><fs> lvcreate LV--test VG--test 32
928 * ><fs> debug ls /dev/mapper
929 * VG----test-LV----test
931 * This makes it impossible to reverse those paths directly, so
932 * we have implemented lvm_canonical_lv_name in the daemon.
934 device = guestfs_lvm_canonical_lv_name (g, spec);
936 else if ((a1 = match1 (g, spec, re_xdev)) != NULL) {
937 char **devices = guestfs_list_devices (g);
942 for (count = 0; devices[count] != NULL; count++)
945 size_t i = a1[0] - 'a'; /* a1[0] is always [a-z] because of regex. */
947 size_t len = strlen (devices[i]) + strlen (a1) + 16;
948 device = safe_malloc (g, len);
949 snprintf (device, len, "%s%s", devices[i], &a1[1]);
953 guestfs___free_string_list (devices);
955 else if (match3 (g, spec, re_freebsd, &bsddisk, &bsdslice, &bsdpart)) {
956 /* FreeBSD disks are organized quite differently. See:
957 * http://www.freebsd.org/doc/handbook/disk-organization.html
958 * FreeBSD "partitions" are exposed as quasi-extended partitions
959 * numbered from 5 in Linux. I have no idea what happens when you
960 * have multiple "slices" (the FreeBSD term for MBR partitions).
962 int disk = parse_unsigned_int (g, bsddisk);
963 int slice = parse_unsigned_int (g, bsdslice);
964 int part = bsdpart[0] - 'a' /* counting from 0 */;
969 if (disk == -1 || disk > 26 ||
970 slice <= 0 || slice > 1 /* > 4 .. see comment above */ ||
971 part < 0 || part >= 26)
974 device = safe_asprintf (g, "/dev/sd%c%d", disk + 'a', part + 5);
978 /* Didn't match device pattern, return original spec unchanged. */
980 device = safe_strdup (g, spec);
985 /* XXX Handling of boot.ini in the Perl version was pretty broken. It
986 * essentially didn't do anything for modern Windows guests.
987 * Therefore I've omitted all that code.
990 check_windows_root (guestfs_h *g, struct inspect_fs *fs)
992 fs->type = OS_TYPE_WINDOWS;
993 fs->distro = OS_DISTRO_WINDOWS;
995 /* Try to find Windows systemroot using some common locations. */
996 const char *systemroots[] =
997 { "/windows", "/winnt", "/win32", "/win" };
999 char *systemroot = NULL;
1001 systemroot == NULL && i < sizeof systemroots / sizeof systemroots[0];
1003 systemroot = resolve_windows_path_silently (g, systemroots[i]);
1007 error (g, _("cannot resolve Windows %%SYSTEMROOT%%"));
1012 fprintf (stderr, "windows %%SYSTEMROOT%% = %s", systemroot);
1014 /* Freed by guestfs___free_inspect_info. */
1015 fs->windows_systemroot = systemroot;
1017 if (check_windows_arch (g, fs) == -1)
1020 /* Product name and version. */
1021 if (check_windows_software_registry (g, fs) == -1)
1024 check_package_format (g, fs);
1025 check_package_management (g, fs);
1028 if (check_windows_system_registry (g, fs) == -1)
1035 check_windows_arch (guestfs_h *g, struct inspect_fs *fs)
1037 size_t len = strlen (fs->windows_systemroot) + 32;
1039 snprintf (cmd_exe, len, "%s/system32/cmd.exe", fs->windows_systemroot);
1041 char *cmd_exe_path = resolve_windows_path_silently (g, cmd_exe);
1045 char *arch = guestfs_file_architecture (g, cmd_exe_path);
1046 free (cmd_exe_path);
1049 fs->arch = arch; /* freed by guestfs___free_inspect_info */
1054 /* At the moment, pull just the ProductName and version numbers from
1055 * the registry. In future there is a case for making many more
1056 * registry fields available to callers.
1059 check_windows_software_registry (guestfs_h *g, struct inspect_fs *fs)
1061 TMP_TEMPLATE_ON_STACK (software_local);
1063 size_t len = strlen (fs->windows_systemroot) + 64;
1065 snprintf (software, len, "%s/system32/config/software",
1066 fs->windows_systemroot);
1068 char *software_path = resolve_windows_path_silently (g, software);
1070 /* If the software hive doesn't exist, just accept that we cannot
1071 * find product_name etc.
1077 hive_value_h *values = NULL;
1079 if (download_to_tmp (g, software_path, software_local, 100000000) == -1)
1082 h = hivex_open (software_local, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
1084 perrorf (g, "hivex_open");
1088 hive_node_h node = hivex_root (h);
1089 const char *hivepath[] =
1090 { "Microsoft", "Windows NT", "CurrentVersion" };
1093 node != 0 && i < sizeof hivepath / sizeof hivepath[0];
1095 node = hivex_node_get_child (h, node, hivepath[i]);
1099 perrorf (g, "hivex: cannot locate HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
1103 values = hivex_node_values (h, node);
1105 for (i = 0; values[i] != 0; ++i) {
1106 char *key = hivex_value_key (h, values[i]);
1108 perrorf (g, "hivex_value_key");
1112 if (STRCASEEQ (key, "ProductName")) {
1113 fs->product_name = hivex_value_string (h, values[i]);
1114 if (!fs->product_name) {
1115 perrorf (g, "hivex_value_string");
1120 else if (STRCASEEQ (key, "CurrentVersion")) {
1121 char *version = hivex_value_string (h, values[i]);
1123 perrorf (g, "hivex_value_string");
1127 char *major, *minor;
1128 if (match2 (g, version, re_windows_version, &major, &minor)) {
1129 fs->major_version = parse_unsigned_int (g, major);
1131 if (fs->major_version == -1) {
1137 fs->minor_version = parse_unsigned_int (g, minor);
1139 if (fs->minor_version == -1) {
1155 if (h) hivex_close (h);
1157 free (software_path);
1159 /* Free up the temporary file. */
1160 unlink (software_local);
1161 #undef software_local_len
1167 check_windows_system_registry (guestfs_h *g, struct inspect_fs *fs)
1169 TMP_TEMPLATE_ON_STACK (system_local);
1171 size_t len = strlen (fs->windows_systemroot) + 64;
1173 snprintf (system, len, "%s/system32/config/system",
1174 fs->windows_systemroot);
1176 char *system_path = resolve_windows_path_silently (g, system);
1178 /* If the system hive doesn't exist, just accept that we cannot
1179 * find hostname etc.
1185 hive_value_h *values = NULL;
1187 if (download_to_tmp (g, system_path, system_local, 100000000) == -1)
1190 h = hivex_open (system_local, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
1192 perrorf (g, "hivex_open");
1196 hive_node_h node = hivex_root (h);
1197 /* XXX Don't hard-code ControlSet001. The current control set would
1198 * be another good thing to expose up through the inspection API.
1200 const char *hivepath[] =
1201 { "ControlSet001", "Services", "Tcpip", "Parameters" };
1204 node != 0 && i < sizeof hivepath / sizeof hivepath[0];
1206 node = hivex_node_get_child (h, node, hivepath[i]);
1210 perrorf (g, "hivex: cannot locate HKLM\\SYSTEM\\ControlSet001\\Services\\Tcpip\\Parameters");
1214 values = hivex_node_values (h, node);
1216 for (i = 0; values[i] != 0; ++i) {
1217 char *key = hivex_value_key (h, values[i]);
1219 perrorf (g, "hivex_value_key");
1223 if (STRCASEEQ (key, "Hostname")) {
1224 fs->hostname = hivex_value_string (h, values[i]);
1225 if (!fs->hostname) {
1226 perrorf (g, "hivex_value_string");
1231 /* many other interesting fields here ... */
1239 if (h) hivex_close (h);
1243 /* Free up the temporary file. */
1244 unlink (system_local);
1245 #undef system_local_len
1251 resolve_windows_path_silently (guestfs_h *g, const char *path)
1253 guestfs_error_handler_cb old_error_cb = g->error_cb;
1255 char *ret = guestfs_case_sensitive_path (g, path);
1256 g->error_cb = old_error_cb;
1261 extend_fses (guestfs_h *g)
1263 size_t n = g->nr_fses + 1;
1264 struct inspect_fs *p;
1266 p = realloc (g->fses, n * sizeof (struct inspect_fs));
1268 perrorf (g, "realloc");
1275 memset (&g->fses[n-1], 0, sizeof (struct inspect_fs));
1280 /* Parse small, unsigned ints, as used in version numbers. */
1282 parse_unsigned_int (guestfs_h *g, const char *str)
1285 int r = xstrtol (str, NULL, 10, &ret, "");
1286 if (r != LONGINT_OK) {
1287 error (g, _("could not parse integer in version number: %s"), str);
1293 /* At the moment, package format and package management is just a
1294 * simple function of the distro and major_version fields, so these
1295 * can never return an error. We might be cleverer in future.
1298 check_package_format (guestfs_h *g, struct inspect_fs *fs)
1300 switch (fs->distro) {
1301 case OS_DISTRO_FEDORA:
1302 case OS_DISTRO_MEEGO:
1303 case OS_DISTRO_REDHAT_BASED:
1304 case OS_DISTRO_RHEL:
1305 case OS_DISTRO_MANDRIVA:
1306 fs->package_format = OS_PACKAGE_FORMAT_RPM;
1309 case OS_DISTRO_DEBIAN:
1310 case OS_DISTRO_UBUNTU:
1311 case OS_DISTRO_LINUX_MINT:
1312 fs->package_format = OS_PACKAGE_FORMAT_DEB;
1315 case OS_DISTRO_ARCHLINUX:
1316 fs->package_format = OS_PACKAGE_FORMAT_PACMAN;
1318 case OS_DISTRO_GENTOO:
1319 fs->package_format = OS_PACKAGE_FORMAT_EBUILD;
1321 case OS_DISTRO_PARDUS:
1322 fs->package_format = OS_PACKAGE_FORMAT_PISI;
1325 case OS_DISTRO_WINDOWS:
1326 case OS_DISTRO_UNKNOWN:
1328 fs->package_format = OS_PACKAGE_FORMAT_UNKNOWN;
1334 check_package_management (guestfs_h *g, struct inspect_fs *fs)
1336 switch (fs->distro) {
1337 case OS_DISTRO_FEDORA:
1338 case OS_DISTRO_MEEGO:
1339 fs->package_management = OS_PACKAGE_MANAGEMENT_YUM;
1342 case OS_DISTRO_REDHAT_BASED:
1343 case OS_DISTRO_RHEL:
1344 if (fs->major_version >= 5)
1345 fs->package_management = OS_PACKAGE_MANAGEMENT_YUM;
1347 fs->package_management = OS_PACKAGE_MANAGEMENT_UP2DATE;
1350 case OS_DISTRO_DEBIAN:
1351 case OS_DISTRO_UBUNTU:
1352 case OS_DISTRO_LINUX_MINT:
1353 fs->package_management = OS_PACKAGE_MANAGEMENT_APT;
1356 case OS_DISTRO_ARCHLINUX:
1357 fs->package_management = OS_PACKAGE_MANAGEMENT_PACMAN;
1359 case OS_DISTRO_GENTOO:
1360 fs->package_management = OS_PACKAGE_MANAGEMENT_PORTAGE;
1362 case OS_DISTRO_PARDUS:
1363 fs->package_management = OS_PACKAGE_MANAGEMENT_PISI;
1365 case OS_DISTRO_MANDRIVA:
1366 fs->package_management = OS_PACKAGE_MANAGEMENT_URPMI;
1369 case OS_DISTRO_WINDOWS:
1370 case OS_DISTRO_UNKNOWN:
1372 fs->package_management = OS_PACKAGE_MANAGEMENT_UNKNOWN;
1377 static struct inspect_fs *
1378 search_for_root (guestfs_h *g, const char *root)
1380 if (g->nr_fses == 0) {
1381 error (g, _("no inspection data: call guestfs_inspect_os first"));
1386 struct inspect_fs *fs;
1387 for (i = 0; i < g->nr_fses; ++i) {
1389 if (fs->is_root && STREQ (root, fs->device))
1393 error (g, _("%s: root device not found: only call this function with a root device previously returned by guestfs_inspect_os"),
1399 guestfs__inspect_get_roots (guestfs_h *g)
1401 /* NB. Doesn't matter if g->nr_fses == 0. We just return an empty
1402 * list in this case.
1407 for (i = 0; i < g->nr_fses; ++i)
1408 if (g->fses[i].is_root)
1411 char **ret = calloc (count+1, sizeof (char *));
1413 perrorf (g, "calloc");
1418 for (i = 0; i < g->nr_fses; ++i) {
1419 if (g->fses[i].is_root) {
1420 ret[count] = safe_strdup (g, g->fses[i].device);
1430 guestfs__inspect_get_type (guestfs_h *g, const char *root)
1432 struct inspect_fs *fs = search_for_root (g, root);
1438 case OS_TYPE_LINUX: ret = safe_strdup (g, "linux"); break;
1439 case OS_TYPE_WINDOWS: ret = safe_strdup (g, "windows"); break;
1440 case OS_TYPE_FREEBSD: ret = safe_strdup (g, "freebsd"); break;
1441 case OS_TYPE_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1448 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
1450 struct inspect_fs *fs = search_for_root (g, root);
1454 return safe_strdup (g, fs->arch ? : "unknown");
1458 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
1460 struct inspect_fs *fs = search_for_root (g, root);
1465 switch (fs->distro) {
1466 case OS_DISTRO_ARCHLINUX: ret = safe_strdup (g, "archlinux"); break;
1467 case OS_DISTRO_DEBIAN: ret = safe_strdup (g, "debian"); break;
1468 case OS_DISTRO_FEDORA: ret = safe_strdup (g, "fedora"); break;
1469 case OS_DISTRO_GENTOO: ret = safe_strdup (g, "gentoo"); break;
1470 case OS_DISTRO_LINUX_MINT: ret = safe_strdup (g, "linuxmint"); break;
1471 case OS_DISTRO_MANDRIVA: ret = safe_strdup (g, "mandriva"); break;
1472 case OS_DISTRO_MEEGO: ret = safe_strdup (g, "meego"); break;
1473 case OS_DISTRO_PARDUS: ret = safe_strdup (g, "pardus"); break;
1474 case OS_DISTRO_REDHAT_BASED: ret = safe_strdup (g, "redhat-based"); break;
1475 case OS_DISTRO_RHEL: ret = safe_strdup (g, "rhel"); break;
1476 case OS_DISTRO_WINDOWS: ret = safe_strdup (g, "windows"); break;
1477 case OS_DISTRO_UBUNTU: ret = safe_strdup (g, "ubuntu"); break;
1478 case OS_DISTRO_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1485 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
1487 struct inspect_fs *fs = search_for_root (g, root);
1491 return fs->major_version;
1495 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
1497 struct inspect_fs *fs = search_for_root (g, root);
1501 return fs->minor_version;
1505 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
1507 struct inspect_fs *fs = search_for_root (g, root);
1511 return safe_strdup (g, fs->product_name ? : "unknown");
1515 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
1517 struct inspect_fs *fs = search_for_root (g, root);
1521 if (!fs->windows_systemroot) {
1522 error (g, _("not a Windows guest, or systemroot could not be determined"));
1526 return safe_strdup (g, fs->windows_systemroot);
1530 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
1532 struct inspect_fs *fs = search_for_root (g, root);
1538 /* If no fstab information (Windows) return just the root. */
1539 if (fs->nr_fstab == 0) {
1540 ret = calloc (3, sizeof (char *));
1541 ret[0] = safe_strdup (g, "/");
1542 ret[1] = safe_strdup (g, root);
1547 #define CRITERION fs->fstab[i].mountpoint[0] == '/'
1548 size_t i, count = 0;
1549 for (i = 0; i < fs->nr_fstab; ++i)
1553 /* Hashtables have 2N+1 entries. */
1554 ret = calloc (2*count+1, sizeof (char *));
1556 perrorf (g, "calloc");
1561 for (i = 0; i < fs->nr_fstab; ++i)
1563 ret[2*count] = safe_strdup (g, fs->fstab[i].mountpoint);
1564 ret[2*count+1] = safe_strdup (g, fs->fstab[i].device);
1573 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
1575 struct inspect_fs *fs = search_for_root (g, root);
1581 /* If no fstab information (Windows) return just the root. */
1582 if (fs->nr_fstab == 0) {
1583 ret = calloc (2, sizeof (char *));
1584 ret[0] = safe_strdup (g, root);
1589 ret = calloc (fs->nr_fstab + 1, sizeof (char *));
1591 perrorf (g, "calloc");
1596 for (i = 0; i < fs->nr_fstab; ++i)
1597 ret[i] = safe_strdup (g, fs->fstab[i].device);
1603 guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
1605 struct inspect_fs *fs = search_for_root (g, root);
1610 switch (fs->package_format) {
1611 case OS_PACKAGE_FORMAT_RPM: ret = safe_strdup (g, "rpm"); break;
1612 case OS_PACKAGE_FORMAT_DEB: ret = safe_strdup (g, "deb"); break;
1613 case OS_PACKAGE_FORMAT_PACMAN: ret = safe_strdup (g, "pacman"); break;
1614 case OS_PACKAGE_FORMAT_EBUILD: ret = safe_strdup (g, "ebuild"); break;
1615 case OS_PACKAGE_FORMAT_PISI: ret = safe_strdup (g, "pisi"); break;
1616 case OS_PACKAGE_FORMAT_UNKNOWN:
1618 ret = safe_strdup (g, "unknown");
1626 guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
1628 struct inspect_fs *fs = search_for_root (g, root);
1633 switch (fs->package_management) {
1634 case OS_PACKAGE_MANAGEMENT_YUM: ret = safe_strdup (g, "yum"); break;
1635 case OS_PACKAGE_MANAGEMENT_UP2DATE: ret = safe_strdup (g, "up2date"); break;
1636 case OS_PACKAGE_MANAGEMENT_APT: ret = safe_strdup (g, "apt"); break;
1637 case OS_PACKAGE_MANAGEMENT_PACMAN: ret = safe_strdup (g, "pacman"); break;
1638 case OS_PACKAGE_MANAGEMENT_PORTAGE: ret = safe_strdup (g, "portage"); break;
1639 case OS_PACKAGE_MANAGEMENT_PISI: ret = safe_strdup (g, "pisi"); break;
1640 case OS_PACKAGE_MANAGEMENT_URPMI: ret = safe_strdup (g, "urpmi"); break;
1641 case OS_PACKAGE_MANAGEMENT_UNKNOWN:
1643 ret = safe_strdup (g, "unknown");
1651 guestfs__inspect_get_hostname (guestfs_h *g, const char *root)
1653 struct inspect_fs *fs = search_for_root (g, root);
1657 return safe_strdup (g, fs->hostname ? : "unknown");
1661 static struct guestfs_application_list *list_applications_rpm (guestfs_h *g, struct inspect_fs *fs);
1663 static struct guestfs_application_list *list_applications_deb (guestfs_h *g, struct inspect_fs *fs);
1664 static struct guestfs_application_list *list_applications_windows (guestfs_h *g, struct inspect_fs *fs);
1665 static void add_application (guestfs_h *g, struct guestfs_application_list *, const char *name, const char *display_name, int32_t epoch, const char *version, const char *release, const char *install_path, const char *publisher, const char *url, const char *description);
1666 static void sort_applications (struct guestfs_application_list *);
1668 /* Unlike the simple inspect-get-* calls, this one assumes that the
1669 * disks are mounted up, and reads files from the mounted disks.
1671 struct guestfs_application_list *
1672 guestfs__inspect_list_applications (guestfs_h *g, const char *root)
1674 struct inspect_fs *fs = search_for_root (g, root);
1678 struct guestfs_application_list *ret = NULL;
1682 switch (fs->package_format) {
1683 case OS_PACKAGE_FORMAT_RPM:
1685 ret = list_applications_rpm (g, fs);
1691 case OS_PACKAGE_FORMAT_DEB:
1692 ret = list_applications_deb (g, fs);
1697 case OS_PACKAGE_FORMAT_PACMAN:
1698 case OS_PACKAGE_FORMAT_EBUILD:
1699 case OS_PACKAGE_FORMAT_PISI:
1700 case OS_PACKAGE_FORMAT_UNKNOWN:
1702 /* nothing - keep GCC happy */;
1706 case OS_TYPE_WINDOWS:
1707 ret = list_applications_windows (g, fs);
1712 case OS_TYPE_FREEBSD:
1713 case OS_TYPE_UNKNOWN:
1715 /* nothing - keep GCC happy */;
1719 /* Don't know how to do inspection. Not an error, return an
1722 ret = safe_malloc (g, sizeof *ret);
1727 sort_applications (ret);
1733 static struct guestfs_application_list *
1734 list_applications_rpm (guestfs_h *g, struct inspect_fs *fs)
1736 TMP_TEMPLATE_ON_STACK (tmpfile);
1738 if (download_to_tmp (g, "/var/lib/rpm/Name", tmpfile, 10000000) == -1)
1741 struct guestfs_application_list *apps = NULL, *ret = NULL;
1742 #define cmd_len (strlen (tmpfile) + 64)
1748 snprintf (cmd, cmd_len, DB_DUMP " -p '%s'", tmpfile);
1751 fprintf (stderr, "list_applications_rpm: %s\n", cmd);
1753 pp = popen (cmd, "r");
1755 perrorf (g, "popen: %s", cmd);
1759 /* Ignore everything to end-of-header marker. */
1761 if (fgets (line, sizeof line, pp) == NULL) {
1762 error (g, _("unexpected end of output from db_dump command"));
1766 len = strlen (line);
1767 if (len > 0 && line[len-1] == '\n') {
1772 if (STREQ (line, "HEADER=END"))
1776 /* Allocate 'apps' list. */
1777 apps = safe_malloc (g, sizeof *apps);
1781 /* Read alternate lines until end of data marker. */
1783 if (fgets (line, sizeof line, pp) == NULL) {
1784 error (g, _("unexpected end of output from db_dump command"));
1788 len = strlen (line);
1789 if (len > 0 && line[len-1] == '\n') {
1794 if (STREQ (line, "DATA=END"))
1798 if (len > 0 && line[0] == ' ')
1800 /* Ignore any application name that contains non-printable chars.
1801 * In the db_dump output these would be escaped with backslash, so
1802 * we can just ignore any such line.
1804 if (strchr (p, '\\') == NULL)
1805 add_application (g, apps, p, "", 0, "", "", "", "", "", "");
1807 /* Discard next line. */
1808 if (fgets (line, sizeof line, pp) == NULL) {
1809 error (g, _("unexpected end of output from db_dump command"));
1814 /* Catch errors from the db_dump command. */
1815 if (pclose (pp) == -1) {
1816 perrorf (g, "pclose: %s", cmd);
1824 if (ret == NULL && apps != NULL)
1825 guestfs_free_application_list (apps);
1833 #endif /* defined DB_DUMP */
1835 static struct guestfs_application_list *
1836 list_applications_deb (guestfs_h *g, struct inspect_fs *fs)
1838 TMP_TEMPLATE_ON_STACK (tmpfile);
1840 if (download_to_tmp (g, "/var/lib/dpkg/status", tmpfile, 10000000) == -1)
1843 struct guestfs_application_list *apps = NULL, *ret = NULL;
1847 char *name = NULL, *version = NULL, *release = NULL;
1848 int installed_flag = 0;
1850 fp = fopen (tmpfile, "r");
1852 perrorf (g, "fopen: %s", tmpfile);
1856 /* Allocate 'apps' list. */
1857 apps = safe_malloc (g, sizeof *apps);
1861 /* Read the temporary file. Each package entry is separated by
1863 * XXX Strictly speaking this is in mailbox header format, so it
1864 * would be possible for fields to spread across multiple lines,
1865 * although for the short fields that we are concerned about this is
1866 * unlikely and not seen in practice.
1868 while (fgets (line, sizeof line, fp) != NULL) {
1869 len = strlen (line);
1870 if (len > 0 && line[len-1] == '\n') {
1875 if (STRPREFIX (line, "Package: ")) {
1877 name = safe_strdup (g, &line[9]);
1879 else if (STRPREFIX (line, "Status: ")) {
1880 installed_flag = strstr (&line[8], "installed") != NULL;
1882 else if (STRPREFIX (line, "Version: ")) {
1885 char *p = strchr (&line[9], '-');
1888 version = safe_strdup (g, &line[9]);
1889 release = safe_strdup (g, p+1);
1891 version = safe_strdup (g, &line[9]);
1895 else if (STREQ (line, "")) {
1896 if (installed_flag && name && version)
1897 add_application (g, apps, name, "", 0, version, release ? : "",
1902 name = version = release = NULL;
1907 if (fclose (fp) == -1) {
1908 perrorf (g, "fclose: %s", tmpfile);
1916 if (ret == NULL && apps != NULL)
1917 guestfs_free_application_list (apps);
1927 /* XXX We already download the SOFTWARE hive when doing general
1928 * inspection. We could avoid this second download of the same file
1929 * by caching these entries in the handle.
1931 static struct guestfs_application_list *
1932 list_applications_windows (guestfs_h *g, struct inspect_fs *fs)
1934 TMP_TEMPLATE_ON_STACK (software_local);
1936 size_t len = strlen (fs->windows_systemroot) + 64;
1938 snprintf (software, len, "%s/system32/config/software",
1939 fs->windows_systemroot);
1941 char *software_path = resolve_windows_path_silently (g, software);
1943 /* If the software hive doesn't exist, just accept that we cannot
1944 * find product_name etc.
1948 struct guestfs_application_list *apps = NULL, *ret = NULL;
1950 hive_node_h *children = NULL;
1952 if (download_to_tmp (g, software_path, software_local, 100000000) == -1)
1955 h = hivex_open (software_local, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
1957 perrorf (g, "hivex_open");
1961 hive_node_h node = hivex_root (h);
1962 const char *hivepath[] =
1963 { "Microsoft", "Windows", "CurrentVersion", "Uninstall" };
1966 node != 0 && i < sizeof hivepath / sizeof hivepath[0];
1968 node = hivex_node_get_child (h, node, hivepath[i]);
1972 perrorf (g, "hivex: cannot locate HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
1976 children = hivex_node_children (h, node);
1977 if (children == NULL) {
1978 perrorf (g, "hivex_node_children");
1982 /* Allocate 'apps' list. */
1983 apps = safe_malloc (g, sizeof *apps);
1987 /* Consider any child node that has a DisplayName key.
1989 * http://nsis.sourceforge.net/Add_uninstall_information_to_Add/Remove_Programs#Optional_values
1991 for (i = 0; children[i] != 0; ++i) {
1994 char *display_name = NULL;
1995 char *version = NULL;
1996 char *install_path = NULL;
1997 char *publisher = NULL;
1999 char *comments = NULL;
2001 /* Use the node name as a proxy for the package name in Linux. The
2002 * display name is not language-independent, so it cannot be used.
2004 name = hivex_node_name (h, children[i]);
2006 perrorf (g, "hivex_node_get_name");
2010 value = hivex_node_get_value (h, children[i], "DisplayName");
2012 display_name = hivex_value_string (h, value);
2014 value = hivex_node_get_value (h, children[i], "DisplayVersion");
2016 version = hivex_value_string (h, value);
2017 value = hivex_node_get_value (h, children[i], "InstallLocation");
2019 install_path = hivex_value_string (h, value);
2020 value = hivex_node_get_value (h, children[i], "Publisher");
2022 publisher = hivex_value_string (h, value);
2023 value = hivex_node_get_value (h, children[i], "URLInfoAbout");
2025 url = hivex_value_string (h, value);
2026 value = hivex_node_get_value (h, children[i], "Comments");
2028 comments = hivex_value_string (h, value);
2030 add_application (g, apps, name, display_name, 0,
2033 install_path ? : "",
2041 free (display_name);
2043 free (install_path);
2052 if (ret == NULL && apps != NULL)
2053 guestfs_free_application_list (apps);
2054 if (h) hivex_close (h);
2056 free (software_path);
2058 /* Free up the temporary file. */
2059 unlink (software_local);
2060 #undef software_local_len
2066 add_application (guestfs_h *g, struct guestfs_application_list *apps,
2067 const char *name, const char *display_name, int32_t epoch,
2068 const char *version, const char *release,
2069 const char *install_path,
2070 const char *publisher, const char *url,
2071 const char *description)
2074 apps->val = safe_realloc (g, apps->val,
2075 apps->len * sizeof (struct guestfs_application));
2076 apps->val[apps->len-1].app_name = safe_strdup (g, name);
2077 apps->val[apps->len-1].app_display_name = safe_strdup (g, display_name);
2078 apps->val[apps->len-1].app_epoch = epoch;
2079 apps->val[apps->len-1].app_version = safe_strdup (g, version);
2080 apps->val[apps->len-1].app_release = safe_strdup (g, release);
2081 apps->val[apps->len-1].app_install_path = safe_strdup (g, install_path);
2082 /* XXX Translated path is not implemented yet. */
2083 apps->val[apps->len-1].app_trans_path = safe_strdup (g, "");
2084 apps->val[apps->len-1].app_publisher = safe_strdup (g, publisher);
2085 apps->val[apps->len-1].app_url = safe_strdup (g, url);
2086 /* XXX The next two are not yet implemented for any package
2087 * format, but we could easily support them for rpm and deb.
2089 apps->val[apps->len-1].app_source_package = safe_strdup (g, "");
2090 apps->val[apps->len-1].app_summary = safe_strdup (g, "");
2091 apps->val[apps->len-1].app_description = safe_strdup (g, description);
2094 /* Sort applications by name before returning the list. */
2096 compare_applications (const void *vp1, const void *vp2)
2098 const struct guestfs_application *v1 = vp1;
2099 const struct guestfs_application *v2 = vp2;
2101 return strcmp (v1->app_name, v2->app_name);
2105 sort_applications (struct guestfs_application_list *apps)
2107 if (apps && apps->val)
2108 qsort (apps->val, apps->len, sizeof (struct guestfs_application),
2109 compare_applications);
2112 /* Download to a guest file to a local temporary file. Refuse to
2113 * download the guest file if it is larger than max_size. The caller
2114 * is responsible for deleting the temporary file after use.
2117 download_to_tmp (guestfs_h *g, const char *filename,
2118 char *localtmp, int64_t max_size)
2124 size = guestfs_filesize (g, filename);
2126 /* guestfs_filesize failed and has already set error in handle */
2128 if (size > max_size) {
2129 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
2134 fd = mkstemp (localtmp);
2136 perrorf (g, "mkstemp");
2140 snprintf (buf, sizeof buf, "/dev/fd/%d", fd);
2142 if (guestfs_download (g, filename, buf) == -1) {
2148 if (close (fd) == -1) {
2149 perrorf (g, "close: %s", localtmp);
2157 /* Call 'f' with Augeas opened and having parsed 'filename' (this file
2158 * must exist). As a security measure, this bails if the file is too
2159 * large for a reasonable configuration file. After the call to 'f'
2163 inspect_with_augeas (guestfs_h *g, struct inspect_fs *fs, const char *filename,
2164 int (*f) (guestfs_h *, struct inspect_fs *))
2166 /* Security: Refuse to do this if filename is too large. */
2167 int64_t size = guestfs_filesize (g, filename);
2169 /* guestfs_filesize failed and has already set error in handle */
2171 if (size > 100000) {
2172 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
2177 /* If !feature_available (g, "augeas") then the next call will fail.
2178 * Arguably we might want to fall back to a non-Augeas method in
2181 if (guestfs_aug_init (g, "/", 16|32) == -1)
2186 /* Tell Augeas to only load one file (thanks Raphaël Pinson). */
2187 char buf[strlen (filename) + 64];
2188 snprintf (buf, strlen (filename) + 64, "/augeas/load//incl[. != \"%s\"]",
2190 if (guestfs_aug_rm (g, buf) == -1)
2193 if (guestfs_aug_load (g) == -1)
2199 guestfs_aug_close (g);
2204 /* Get the first line of a small file, without any trailing newline
2208 first_line_of_file (guestfs_h *g, const char *filename)
2214 /* Don't trust guestfs_head_n not to break with very large files.
2215 * Check the file size is something reasonable first.
2217 size = guestfs_filesize (g, filename);
2219 /* guestfs_filesize failed and has already set error in handle */
2221 if (size > 1000000) {
2222 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
2227 lines = guestfs_head_n (g, 1, filename);
2230 if (lines[0] == NULL) {
2231 error (g, _("%s: file is empty"), filename);
2232 guestfs___free_string_list (lines);
2235 /* lines[1] should be NULL because of '1' argument above ... */
2237 ret = lines[0]; /* caller frees */
2238 free (lines); /* free the array */
2243 #else /* no PCRE or hivex at compile time */
2245 /* XXX These functions should be in an optgroup. */
2247 #define NOT_IMPL(r) \
2248 error (g, _("inspection API not available since this version of libguestfs was compiled without PCRE or hivex libraries")); \
2252 guestfs__inspect_os (guestfs_h *g)
2258 guestfs__inspect_get_roots (guestfs_h *g)
2264 guestfs__inspect_get_type (guestfs_h *g, const char *root)
2270 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
2276 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
2282 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
2288 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
2294 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
2300 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
2306 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
2312 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
2318 guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
2324 guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
2330 guestfs__inspect_get_hostname (guestfs_h *g, const char *root)
2335 struct guestfs_application_list *
2336 guestfs__inspect_list_applications (guestfs_h *g, const char *root)
2341 #endif /* no PCRE or hivex at compile time */
2344 guestfs___free_inspect_info (guestfs_h *g)
2347 for (i = 0; i < g->nr_fses; ++i) {
2348 free (g->fses[i].device);
2349 free (g->fses[i].product_name);
2350 free (g->fses[i].arch);
2351 free (g->fses[i].hostname);
2352 free (g->fses[i].windows_systemroot);
2354 for (j = 0; j < g->fses[i].nr_fstab; ++j) {
2355 free (g->fses[i].fstab[j].device);
2356 free (g->fses[i].fstab[j].mountpoint);
2358 free (g->fses[i].fstab);
2365 /* In the Perl code this is a public function. */
2367 guestfs___feature_available (guestfs_h *g, const char *feature)
2369 /* If there's an error we should ignore it, so to do that we have to
2370 * temporarily replace the error handler with a null one.
2372 guestfs_error_handler_cb old_error_cb = g->error_cb;
2375 const char *groups[] = { feature, NULL };
2376 int r = guestfs_available (g, (char * const *) groups);
2378 g->error_cb = old_error_cb;
2380 return r == 0 ? 1 : 0;
2385 /* Match a regular expression which contains no captures. Returns
2386 * true if it matches or false if it doesn't.
2389 guestfs___match (guestfs_h *g, const char *str, const pcre *re)
2391 size_t len = strlen (str);
2394 r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
2395 if (r == PCRE_ERROR_NOMATCH)
2398 /* Internal error -- should not happen. */
2399 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
2400 __FILE__, __func__, r, str);
2407 /* Match a regular expression which contains exactly one capture. If
2408 * the string matches, return the capture, otherwise return NULL. The
2409 * caller must free the result.
2412 guestfs___match1 (guestfs_h *g, const char *str, const pcre *re)
2414 size_t len = strlen (str);
2417 r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
2418 if (r == PCRE_ERROR_NOMATCH)
2421 /* Internal error -- should not happen. */
2422 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
2423 __FILE__, __func__, r, str);
2427 return safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
2430 /* Match a regular expression which contains exactly two captures. */
2432 guestfs___match2 (guestfs_h *g, const char *str, const pcre *re,
2433 char **ret1, char **ret2)
2435 size_t len = strlen (str);
2438 r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
2439 if (r == PCRE_ERROR_NOMATCH)
2442 /* Internal error -- should not happen. */
2443 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
2444 __FILE__, __func__, r, str);
2448 *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
2449 *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
2454 /* Match a regular expression which contains exactly three captures. */
2456 guestfs___match3 (guestfs_h *g, const char *str, const pcre *re,
2457 char **ret1, char **ret2, char **ret3)
2459 size_t len = strlen (str);
2462 r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
2463 if (r == PCRE_ERROR_NOMATCH)
2466 /* Internal error -- should not happen. */
2467 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
2468 __FILE__, __func__, r, str);
2472 *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
2473 *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
2474 *ret3 = safe_strndup (g, &str[vec[6]], vec[7]-vec[6]);
2479 #endif /* HAVE_PCRE */