2 * Copyright (C) 2010 Red Hat Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
38 #include "ignore-value.h"
42 #include "guestfs-internal.h"
43 #include "guestfs-internal-actions.h"
44 #include "guestfs_protocol.h"
46 #if defined(HAVE_PCRE) && defined(HAVE_HIVEX)
48 /* Compile all the regular expressions once when the shared library is
49 * loaded. PCRE is thread safe so we're supposedly OK here if
50 * multiple threads call into the libguestfs API functions below
53 static pcre *re_fedora;
54 static pcre *re_rhel_old;
56 static pcre *re_rhel_no_minor;
57 static pcre *re_major_minor;
58 static pcre *re_aug_seq;
60 static pcre *re_first_partition;
61 static pcre *re_freebsd;
62 static pcre *re_windows_version;
64 static void compile_regexps (void) __attribute__((constructor));
65 static void free_regexps (void) __attribute__((destructor));
68 compile_regexps (void)
73 #define COMPILE(re,pattern,options) \
75 re = pcre_compile ((pattern), (options), &err, &offset, NULL); \
77 ignore_value (write (2, err, strlen (err))); \
82 COMPILE (re_fedora, "Fedora release (\\d+)", 0);
84 "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+).*Update (\\d+)", 0);
86 "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+)\\.(\\d+)", 0);
87 COMPILE (re_rhel_no_minor,
88 "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+)", 0);
89 COMPILE (re_major_minor, "(\\d+)\\.(\\d+)", 0);
90 COMPILE (re_aug_seq, "/\\d+$", 0);
91 COMPILE (re_xdev, "^/dev/(?:h|s|v|xv)d([a-z]\\d*)$", 0);
92 COMPILE (re_first_partition, "^/dev/(?:h|s|v)d.1$", 0);
93 COMPILE (re_freebsd, "^/dev/ad(\\d+)s(\\d+)([a-z])$", 0);
94 COMPILE (re_windows_version, "^(\\d+)\\.(\\d+)", 0);
100 pcre_free (re_fedora);
101 pcre_free (re_rhel_old);
103 pcre_free (re_rhel_no_minor);
104 pcre_free (re_major_minor);
105 pcre_free (re_aug_seq);
107 pcre_free (re_first_partition);
108 pcre_free (re_freebsd);
109 pcre_free (re_windows_version);
112 /* The main inspection code. */
113 static int check_for_filesystem_on (guestfs_h *g, const char *device);
116 guestfs__inspect_os (guestfs_h *g)
118 /* Remove any information previously stored in the handle. */
119 guestfs___free_inspect_info (g);
121 if (guestfs_umount_all (g) == -1)
124 /* Iterate over all possible devices. Try to mount each
125 * (read-only). Examine ones which contain filesystems and add that
126 * information to the handle.
128 /* Look to see if any devices directly contain filesystems (RHBZ#590167). */
130 devices = guestfs_list_devices (g);
135 for (i = 0; devices[i] != NULL; ++i) {
136 if (check_for_filesystem_on (g, devices[i]) == -1) {
137 guestfs___free_string_list (devices);
138 guestfs___free_inspect_info (g);
142 guestfs___free_string_list (devices);
144 /* Look at all partitions. */
146 partitions = guestfs_list_partitions (g);
147 if (partitions == NULL) {
148 guestfs___free_inspect_info (g);
152 for (i = 0; partitions[i] != NULL; ++i) {
153 if (check_for_filesystem_on (g, partitions[i]) == -1) {
154 guestfs___free_string_list (partitions);
155 guestfs___free_inspect_info (g);
159 guestfs___free_string_list (partitions);
161 /* Look at all LVs. */
162 if (guestfs___feature_available (g, "lvm2")) {
164 lvs = guestfs_lvs (g);
166 guestfs___free_inspect_info (g);
170 for (i = 0; lvs[i] != NULL; ++i) {
171 if (check_for_filesystem_on (g, lvs[i]) == -1) {
172 guestfs___free_string_list (lvs);
173 guestfs___free_inspect_info (g);
177 guestfs___free_string_list (lvs);
180 /* At this point we have, in the handle, a list of all filesystems
181 * found and data about each one. Now we assemble the list of
182 * filesystems which are root devices and return that to the user.
183 * Fall through to guestfs__inspect_get_roots to do that.
185 char **ret = guestfs__inspect_get_roots (g);
187 guestfs___free_inspect_info (g);
191 /* Find out if 'device' contains a filesystem. If it does, add
192 * another entry in g->fses.
194 static int check_filesystem (guestfs_h *g, const char *device);
195 static int check_linux_root (guestfs_h *g, struct inspect_fs *fs);
196 static int check_freebsd_root (guestfs_h *g, struct inspect_fs *fs);
197 static void check_architecture (guestfs_h *g, struct inspect_fs *fs);
198 static int check_fstab (guestfs_h *g, struct inspect_fs *fs);
199 static int check_windows_root (guestfs_h *g, struct inspect_fs *fs);
200 static int check_windows_arch (guestfs_h *g, struct inspect_fs *fs);
201 static int check_windows_registry (guestfs_h *g, struct inspect_fs *fs);
202 static char *resolve_windows_path_silently (guestfs_h *g, const char *);
203 static int extend_fses (guestfs_h *g);
204 static int parse_unsigned_int (guestfs_h *g, const char *str);
205 static int add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
206 const char *spec, const char *mp);
207 static char *resolve_fstab_device (guestfs_h *g, const char *spec);
208 static void check_package_format (guestfs_h *g, struct inspect_fs *fs);
209 static void check_package_management (guestfs_h *g, struct inspect_fs *fs);
210 static int download_to_tmp (guestfs_h *g, const char *filename, char *localtmp, int64_t max_size);
213 check_for_filesystem_on (guestfs_h *g, const char *device)
215 /* Get vfs-type in order to check if it's a Linux(?) swap device.
216 * If there's an error we should ignore it, so to do that we have to
217 * temporarily replace the error handler with a null one.
219 guestfs_error_handler_cb old_error_cb = g->error_cb;
221 char *vfs_type = guestfs_vfs_type (g, device);
222 g->error_cb = old_error_cb;
224 int is_swap = vfs_type && STREQ (vfs_type, "swap");
227 fprintf (stderr, "check_for_filesystem_on: %s (%s)\n",
228 device, vfs_type ? vfs_type : "failed to get vfs type");
232 if (extend_fses (g) == -1)
234 g->fses[g->nr_fses-1].is_swap = 1;
238 /* Try mounting the device. As above, ignore errors. */
240 int r = guestfs_mount_ro (g, device, "/");
241 if (r == -1 && vfs_type && STREQ (vfs_type, "ufs")) /* Hack for the *BSDs. */
242 r = guestfs_mount_vfs (g, "ro,ufstype=ufs2", "ufs", device, "/");
244 g->error_cb = old_error_cb;
248 /* Do the rest of the checks. */
249 r = check_filesystem (g, device);
251 /* Unmount the filesystem. */
252 if (guestfs_umount_all (g) == -1)
259 check_filesystem (guestfs_h *g, const char *device)
261 if (extend_fses (g) == -1)
264 struct inspect_fs *fs = &g->fses[g->nr_fses-1];
266 fs->device = safe_strdup (g, device);
267 fs->is_mountable = 1;
269 /* Optimize some of the tests by avoiding multiple tests of the same thing. */
270 int is_dir_etc = guestfs_is_dir (g, "/etc") > 0;
271 int is_dir_bin = guestfs_is_dir (g, "/bin") > 0;
272 int is_dir_share = guestfs_is_dir (g, "/share") > 0;
275 if (guestfs_is_file (g, "/grub/menu.lst") > 0 ||
276 guestfs_is_file (g, "/grub/grub.conf") > 0)
277 fs->content = FS_CONTENT_LINUX_BOOT;
279 else if (is_dir_etc &&
281 guestfs_is_file (g, "/etc/freebsd-update.conf") > 0 &&
282 guestfs_is_file (g, "/etc/fstab") > 0) {
283 /* Ignore /dev/sda1 which is a shadow of the real root filesystem
284 * that is probably /dev/sda5 (see:
285 * http://www.freebsd.org/doc/handbook/disk-organization.html)
287 if (match (g, device, re_first_partition))
291 fs->content = FS_CONTENT_FREEBSD_ROOT;
292 if (check_freebsd_root (g, fs) == -1)
296 else if (is_dir_etc &&
298 guestfs_is_file (g, "/etc/fstab") > 0) {
300 fs->content = FS_CONTENT_LINUX_ROOT;
301 if (check_linux_root (g, fs) == -1)
304 /* Linux /usr/local? */
305 else if (is_dir_etc &&
308 guestfs_exists (g, "/local") == 0 &&
309 guestfs_is_file (g, "/etc/fstab") == 0)
310 fs->content = FS_CONTENT_LINUX_USR_LOCAL;
312 else if (is_dir_etc &&
315 guestfs_exists (g, "/local") > 0 &&
316 guestfs_is_file (g, "/etc/fstab") == 0)
317 fs->content = FS_CONTENT_LINUX_USR;
319 else if (guestfs_is_dir (g, "/log") > 0 &&
320 guestfs_is_dir (g, "/run") > 0 &&
321 guestfs_is_dir (g, "/spool") > 0)
322 fs->content = FS_CONTENT_LINUX_VAR;
324 else if (guestfs_is_file (g, "/AUTOEXEC.BAT") > 0 ||
325 guestfs_is_file (g, "/autoexec.bat") > 0 ||
326 guestfs_is_dir (g, "/Program Files") > 0 ||
327 guestfs_is_dir (g, "/WINDOWS") > 0 ||
328 guestfs_is_dir (g, "/Windows") > 0 ||
329 guestfs_is_dir (g, "/windows") > 0 ||
330 guestfs_is_dir (g, "/WIN32") > 0 ||
331 guestfs_is_dir (g, "/Win32") > 0 ||
332 guestfs_is_dir (g, "/WINNT") > 0 ||
333 guestfs_is_file (g, "/boot.ini") > 0 ||
334 guestfs_is_file (g, "/ntldr") > 0) {
336 fs->content = FS_CONTENT_WINDOWS_ROOT;
337 if (check_windows_root (g, fs) == -1)
344 /* Set fs->product_name to the first line of the release file. */
346 parse_release_file (guestfs_h *g, struct inspect_fs *fs,
347 const char *release_filename)
349 char **product_name = guestfs_head_n (g, 1, release_filename);
350 if (product_name == NULL)
352 if (product_name[0] == NULL) {
353 error (g, _("%s: file is empty"), release_filename);
354 guestfs___free_string_list (product_name);
358 /* Note that this string becomes owned by the handle and will
359 * be freed by guestfs___free_inspect_info.
361 fs->product_name = product_name[0];
367 /* Parse generic MAJOR.MINOR from the fs->product_name string. */
369 parse_major_minor (guestfs_h *g, struct inspect_fs *fs)
373 if (match2 (g, fs->product_name, re_major_minor, &major, &minor)) {
374 fs->major_version = parse_unsigned_int (g, major);
376 if (fs->major_version == -1) {
380 fs->minor_version = parse_unsigned_int (g, minor);
382 if (fs->minor_version == -1)
388 /* Ubuntu has /etc/lsb-release containing:
389 * DISTRIB_ID=Ubuntu # Distro
390 * DISTRIB_RELEASE=10.04 # Version
391 * DISTRIB_CODENAME=lucid
392 * DISTRIB_DESCRIPTION="Ubuntu 10.04.1 LTS" # Product name
394 * [Ubuntu-derived ...] Linux Mint was found to have this:
395 * DISTRIB_ID=LinuxMint
397 * DISTRIB_CODENAME=julia
398 * DISTRIB_DESCRIPTION="Linux Mint 10 Julia"
399 * Linux Mint also has /etc/linuxmint/info with more information,
400 * but we can use the LSB file.
403 * LSB_VERSION=lsb-4.0-amd64:lsb-4.0-noarch
404 * DISTRIB_ID=MandrivaLinux
405 * DISTRIB_RELEASE=2010.1
406 * DISTRIB_CODENAME=Henry_Farman
407 * DISTRIB_DESCRIPTION="Mandriva Linux 2010.1"
408 * Mandriva also has a normal release file called /etc/mandriva-release.
411 parse_lsb_release (guestfs_h *g, struct inspect_fs *fs)
417 lines = guestfs_head_n (g, 10, "/etc/lsb-release");
421 for (i = 0; lines[i] != NULL; ++i) {
422 if (fs->distro == 0 &&
423 STREQ (lines[i], "DISTRIB_ID=Ubuntu")) {
424 fs->distro = OS_DISTRO_UBUNTU;
427 else if (fs->distro == 0 &&
428 STREQ (lines[i], "DISTRIB_ID=LinuxMint")) {
429 fs->distro = OS_DISTRO_LINUX_MINT;
432 else if (fs->distro == 0 &&
433 STREQ (lines[i], "DISTRIB_ID=MandrivaLinux")) {
434 fs->distro = OS_DISTRO_MANDRIVA;
437 else if (STRPREFIX (lines[i], "DISTRIB_RELEASE=")) {
439 if (match2 (g, &lines[i][16], re_major_minor, &major, &minor)) {
440 fs->major_version = parse_unsigned_int (g, major);
442 if (fs->major_version == -1) {
444 guestfs___free_string_list (lines);
447 fs->minor_version = parse_unsigned_int (g, minor);
449 if (fs->minor_version == -1) {
450 guestfs___free_string_list (lines);
455 else if (fs->product_name == NULL &&
456 (STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=\"") ||
457 STRPREFIX (lines[i], "DISTRIB_DESCRIPTION='"))) {
458 size_t len = strlen (lines[i]) - 21 - 1;
459 fs->product_name = safe_strndup (g, &lines[i][21], len);
462 else if (fs->product_name == NULL &&
463 STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=")) {
464 size_t len = strlen (lines[i]) - 20;
465 fs->product_name = safe_strndup (g, &lines[i][20], len);
470 guestfs___free_string_list (lines);
474 /* The currently mounted device is known to be a Linux root. Try to
475 * determine from this the distro, version, etc. Also parse
476 * /etc/fstab to determine the arrangement of mountpoints and
477 * associated devices.
480 check_linux_root (guestfs_h *g, struct inspect_fs *fs)
484 fs->type = OS_TYPE_LINUX;
486 if (guestfs_exists (g, "/etc/lsb-release") > 0) {
487 r = parse_lsb_release (g, fs);
488 if (r == -1) /* error */
490 if (r == 1) /* ok - detected the release from this file */
491 goto skip_release_checks;
494 if (guestfs_exists (g, "/etc/redhat-release") > 0) {
495 fs->distro = OS_DISTRO_REDHAT_BASED; /* Something generic Red Hat-like. */
497 if (parse_release_file (g, fs, "/etc/redhat-release") == -1)
501 if ((major = match1 (g, fs->product_name, re_fedora)) != NULL) {
502 fs->distro = OS_DISTRO_FEDORA;
503 fs->major_version = parse_unsigned_int (g, major);
505 if (fs->major_version == -1)
508 else if (match2 (g, fs->product_name, re_rhel_old, &major, &minor) ||
509 match2 (g, fs->product_name, re_rhel, &major, &minor)) {
510 fs->distro = OS_DISTRO_RHEL;
511 fs->major_version = parse_unsigned_int (g, major);
513 if (fs->major_version == -1) {
517 fs->minor_version = parse_unsigned_int (g, minor);
519 if (fs->minor_version == -1)
522 else if ((major = match1 (g, fs->product_name, re_rhel_no_minor)) != NULL) {
523 fs->distro = OS_DISTRO_RHEL;
524 fs->major_version = parse_unsigned_int (g, major);
526 if (fs->major_version == -1)
528 fs->minor_version = 0;
531 else if (guestfs_exists (g, "/etc/debian_version") > 0) {
532 fs->distro = OS_DISTRO_DEBIAN;
534 if (parse_release_file (g, fs, "/etc/debian_version") == -1)
537 if (parse_major_minor (g, fs) == -1)
540 else if (guestfs_exists (g, "/etc/pardus-release") > 0) {
541 fs->distro = OS_DISTRO_PARDUS;
543 if (parse_release_file (g, fs, "/etc/pardus-release") == -1)
546 if (parse_major_minor (g, fs) == -1)
549 else if (guestfs_exists (g, "/etc/arch-release") > 0) {
550 fs->distro = OS_DISTRO_ARCHLINUX;
552 /* /etc/arch-release file is empty and I can't see a way to
553 * determine the actual release or product string.
556 else if (guestfs_exists (g, "/etc/gentoo-release") > 0) {
557 fs->distro = OS_DISTRO_GENTOO;
559 if (parse_release_file (g, fs, "/etc/gentoo-release") == -1)
562 if (parse_major_minor (g, fs) == -1)
565 else if (guestfs_exists (g, "/etc/meego-release") > 0) {
566 fs->distro = OS_DISTRO_MEEGO;
568 if (parse_release_file (g, fs, "/etc/meego-release") == -1)
571 if (parse_major_minor (g, fs) == -1)
575 skip_release_checks:;
577 /* If distro test above was successful, work out the package format. */
578 check_package_format (g, fs);
579 check_package_management (g, fs);
581 /* Determine the architecture. */
582 check_architecture (g, fs);
584 /* We already know /etc/fstab exists because it's part of the test
585 * for Linux root above. We must now parse this file to determine
586 * which filesystems are used by the operating system and how they
589 if (check_fstab (g, fs) == -1)
595 /* The currently mounted device is known to be a FreeBSD root. */
597 check_freebsd_root (guestfs_h *g, struct inspect_fs *fs)
601 fs->type = OS_TYPE_FREEBSD;
603 /* FreeBSD has no authoritative version file. The version number is
604 * in /etc/motd, which the system administrator might edit, but
605 * we'll use that anyway.
608 if (guestfs_exists (g, "/etc/motd") > 0) {
609 if (parse_release_file (g, fs, "/etc/motd") == -1)
612 if (parse_major_minor (g, fs) == -1)
616 /* Determine the architecture. */
617 check_architecture (g, fs);
619 /* We already know /etc/fstab exists because it's part of the test above. */
620 if (check_fstab (g, fs) == -1)
627 check_architecture (guestfs_h *g, struct inspect_fs *fs)
629 const char *binaries[] =
630 { "/bin/bash", "/bin/ls", "/bin/echo", "/bin/rm", "/bin/sh" };
633 for (i = 0; i < sizeof binaries / sizeof binaries[0]; ++i) {
634 if (guestfs_is_file (g, binaries[i]) > 0) {
635 /* Ignore errors from file_architecture call. */
636 guestfs_error_handler_cb old_error_cb = g->error_cb;
638 char *arch = guestfs_file_architecture (g, binaries[i]);
639 g->error_cb = old_error_cb;
642 /* String will be owned by handle, freed by
643 * guestfs___free_inspect_info.
652 static int check_fstab_aug_open (guestfs_h *g, struct inspect_fs *fs);
655 check_fstab (guestfs_h *g, struct inspect_fs *fs)
660 /* Security: Refuse to do this if /etc/fstab is huge. */
661 size = guestfs_filesize (g, "/etc/fstab");
662 if (size == -1 || size > 100000) {
663 error (g, _("size of /etc/fstab unreasonable (%" PRIi64 " bytes)"), size);
667 /* XXX What if !feature_available (g, "augeas")? */
668 if (guestfs_aug_init (g, "/", 16|32) == -1)
671 /* Tell Augeas to only load /etc/fstab (thanks Raphaël Pinson). */
672 guestfs_aug_rm (g, "/augeas/load//incl[. != \"/etc/fstab\"]");
673 guestfs_aug_load (g);
675 r = check_fstab_aug_open (g, fs);
676 guestfs_aug_close (g);
684 check_fstab_aug_open (guestfs_h *g, struct inspect_fs *fs)
686 char **lines = guestfs_aug_ls (g, "/files/etc/fstab");
690 if (lines[0] == NULL) {
691 error (g, _("could not parse /etc/fstab or empty file"));
692 guestfs___free_string_list (lines);
698 for (i = 0; lines[i] != NULL; ++i) {
699 /* Ignore comments. Only care about sequence lines which
702 if (match (g, lines[i], re_aug_seq)) {
703 snprintf (augpath, sizeof augpath, "%s/spec", lines[i]);
704 char *spec = guestfs_aug_get (g, augpath);
706 guestfs___free_string_list (lines);
710 snprintf (augpath, sizeof augpath, "%s/file", lines[i]);
711 char *mp = guestfs_aug_get (g, augpath);
713 guestfs___free_string_list (lines);
718 int r = add_fstab_entry (g, fs, spec, mp);
723 guestfs___free_string_list (lines);
729 guestfs___free_string_list (lines);
733 /* Add a filesystem and possibly a mountpoint entry for
734 * the root filesystem 'fs'.
736 * 'spec' is the fstab spec field, which might be a device name or a
737 * pseudodevice or 'UUID=...' or 'LABEL=...'.
739 * 'mp' is the mount point, which could also be 'swap' or 'none'.
742 add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
743 const char *spec, const char *mp)
745 /* Ignore certain mountpoints. */
746 if (STRPREFIX (mp, "/dev/") ||
747 STREQ (mp, "/dev") ||
748 STRPREFIX (mp, "/media/") ||
749 STRPREFIX (mp, "/proc/") ||
750 STREQ (mp, "/proc") ||
751 STRPREFIX (mp, "/selinux/") ||
752 STREQ (mp, "/selinux") ||
753 STRPREFIX (mp, "/sys/") ||
757 /* Ignore /dev/fd (floppy disks) (RHBZ#642929) and CD-ROM drives. */
758 if ((STRPREFIX (spec, "/dev/fd") && c_isdigit (spec[7])) ||
759 STREQ (spec, "/dev/floppy") ||
760 STREQ (spec, "/dev/cdrom"))
763 /* Resolve UUID= and LABEL= to the actual device. */
765 if (STRPREFIX (spec, "UUID="))
766 device = guestfs_findfs_uuid (g, &spec[5]);
767 else if (STRPREFIX (spec, "LABEL="))
768 device = guestfs_findfs_label (g, &spec[6]);
769 /* Ignore "/.swap" (Pardus) and pseudo-devices like "tmpfs". */
770 else if (STRPREFIX (spec, "/dev/"))
771 /* Resolve guest block device names. */
772 device = resolve_fstab_device (g, spec);
774 /* If we haven't resolved the device successfully by this point,
775 * we don't care, just ignore it.
780 char *mountpoint = safe_strdup (g, mp);
782 /* Add this to the fstab entry in 'fs'.
783 * Note these are further filtered by guestfs_inspect_get_mountpoints
784 * and guestfs_inspect_get_filesystems.
786 size_t n = fs->nr_fstab + 1;
787 struct inspect_fstab_entry *p;
789 p = realloc (fs->fstab, n * sizeof (struct inspect_fstab_entry));
791 perrorf (g, "realloc");
800 /* These are owned by the handle and freed by guestfs___free_inspect_info. */
801 fs->fstab[n-1].device = device;
802 fs->fstab[n-1].mountpoint = mountpoint;
805 fprintf (stderr, "fstab: device=%s mountpoint=%s\n", device, mountpoint);
810 /* Resolve block device name to the libguestfs device name, eg.
811 * /dev/xvdb1 => /dev/vdb1; and /dev/mapper/VG-LV => /dev/VG/LV. This
812 * assumes that disks were added in the same order as they appear to
813 * the real VM, which is a reasonable assumption to make. Return
814 * anything we don't recognize unchanged.
817 resolve_fstab_device (guestfs_h *g, const char *spec)
821 char *bsddisk, *bsdslice, *bsdpart;
823 if (STRPREFIX (spec, "/dev/mapper/")) {
824 /* LVM2 does some strange munging on /dev/mapper paths for VGs and
825 * LVs which contain '-' character:
827 * ><fs> lvcreate LV--test VG--test 32
828 * ><fs> debug ls /dev/mapper
829 * VG----test-LV----test
831 * This makes it impossible to reverse those paths directly, so
832 * we have implemented lvm_canonical_lv_name in the daemon.
834 device = guestfs_lvm_canonical_lv_name (g, spec);
836 else if ((a1 = match1 (g, spec, re_xdev)) != NULL) {
837 char **devices = guestfs_list_devices (g);
842 for (count = 0; devices[count] != NULL; count++)
845 size_t i = a1[0] - 'a'; /* a1[0] is always [a-z] because of regex. */
847 size_t len = strlen (devices[i]) + strlen (a1) + 16;
848 device = safe_malloc (g, len);
849 snprintf (device, len, "%s%s", devices[i], &a1[1]);
853 guestfs___free_string_list (devices);
855 else if (match3 (g, spec, re_freebsd, &bsddisk, &bsdslice, &bsdpart)) {
856 /* FreeBSD disks are organized quite differently. See:
857 * http://www.freebsd.org/doc/handbook/disk-organization.html
858 * FreeBSD "partitions" are exposed as quasi-extended partitions
859 * numbered from 5 in Linux. I have no idea what happens when you
860 * have multiple "slices" (the FreeBSD term for MBR partitions).
862 int disk = parse_unsigned_int (g, bsddisk);
863 int slice = parse_unsigned_int (g, bsdslice);
864 int part = bsdpart[0] - 'a' /* counting from 0 */;
869 if (disk == -1 || disk > 26 ||
870 slice <= 0 || slice > 1 /* > 4 .. see comment above */ ||
871 part < 0 || part >= 26)
874 device = safe_asprintf (g, "/dev/sd%c%d", disk + 'a', part + 5);
878 /* Didn't match device pattern, return original spec unchanged. */
880 device = safe_strdup (g, spec);
885 /* XXX Handling of boot.ini in the Perl version was pretty broken. It
886 * essentially didn't do anything for modern Windows guests.
887 * Therefore I've omitted all that code.
890 check_windows_root (guestfs_h *g, struct inspect_fs *fs)
892 fs->type = OS_TYPE_WINDOWS;
893 fs->distro = OS_DISTRO_WINDOWS;
895 /* Try to find Windows systemroot using some common locations. */
896 const char *systemroots[] =
897 { "/windows", "/winnt", "/win32", "/win" };
899 char *systemroot = NULL;
901 systemroot == NULL && i < sizeof systemroots / sizeof systemroots[0];
903 systemroot = resolve_windows_path_silently (g, systemroots[i]);
907 error (g, _("cannot resolve Windows %%SYSTEMROOT%%"));
912 fprintf (stderr, "windows %%SYSTEMROOT%% = %s", systemroot);
914 /* Freed by guestfs___free_inspect_info. */
915 fs->windows_systemroot = systemroot;
917 if (check_windows_arch (g, fs) == -1)
920 if (check_windows_registry (g, fs) == -1)
923 check_package_format (g, fs);
924 check_package_management (g, fs);
930 check_windows_arch (guestfs_h *g, struct inspect_fs *fs)
932 size_t len = strlen (fs->windows_systemroot) + 32;
934 snprintf (cmd_exe, len, "%s/system32/cmd.exe", fs->windows_systemroot);
936 char *cmd_exe_path = resolve_windows_path_silently (g, cmd_exe);
940 char *arch = guestfs_file_architecture (g, cmd_exe_path);
944 fs->arch = arch; /* freed by guestfs___free_inspect_info */
949 /* At the moment, pull just the ProductName and version numbers from
950 * the registry. In future there is a case for making many more
951 * registry fields available to callers.
954 check_windows_registry (guestfs_h *g, struct inspect_fs *fs)
956 TMP_TEMPLATE_ON_STACK (software_local);
958 size_t len = strlen (fs->windows_systemroot) + 64;
960 snprintf (software, len, "%s/system32/config/software",
961 fs->windows_systemroot);
963 char *software_path = resolve_windows_path_silently (g, software);
965 /* If the software hive doesn't exist, just accept that we cannot
966 * find product_name etc.
972 hive_value_h *values = NULL;
974 if (download_to_tmp (g, software_path, software_local, 100000000) == -1)
977 h = hivex_open (software_local, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
979 perrorf (g, "hivex_open");
983 hive_node_h node = hivex_root (h);
984 const char *hivepath[] =
985 { "Microsoft", "Windows NT", "CurrentVersion" };
988 node != 0 && i < sizeof hivepath / sizeof hivepath[0];
990 node = hivex_node_get_child (h, node, hivepath[i]);
994 perrorf (g, "hivex: cannot locate HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
998 values = hivex_node_values (h, node);
1000 for (i = 0; values[i] != 0; ++i) {
1001 char *key = hivex_value_key (h, values[i]);
1003 perrorf (g, "hivex_value_key");
1007 if (STRCASEEQ (key, "ProductName")) {
1008 fs->product_name = hivex_value_string (h, values[i]);
1009 if (!fs->product_name) {
1010 perrorf (g, "hivex_value_string");
1015 else if (STRCASEEQ (key, "CurrentVersion")) {
1016 char *version = hivex_value_string (h, values[i]);
1018 perrorf (g, "hivex_value_string");
1022 char *major, *minor;
1023 if (match2 (g, version, re_windows_version, &major, &minor)) {
1024 fs->major_version = parse_unsigned_int (g, major);
1026 if (fs->major_version == -1) {
1032 fs->minor_version = parse_unsigned_int (g, minor);
1034 if (fs->minor_version == -1) {
1050 if (h) hivex_close (h);
1052 free (software_path);
1054 /* Free up the temporary file. */
1055 unlink (software_local);
1056 #undef software_local_len
1062 resolve_windows_path_silently (guestfs_h *g, const char *path)
1064 guestfs_error_handler_cb old_error_cb = g->error_cb;
1066 char *ret = guestfs_case_sensitive_path (g, path);
1067 g->error_cb = old_error_cb;
1072 extend_fses (guestfs_h *g)
1074 size_t n = g->nr_fses + 1;
1075 struct inspect_fs *p;
1077 p = realloc (g->fses, n * sizeof (struct inspect_fs));
1079 perrorf (g, "realloc");
1086 memset (&g->fses[n-1], 0, sizeof (struct inspect_fs));
1091 /* Parse small, unsigned ints, as used in version numbers. */
1093 parse_unsigned_int (guestfs_h *g, const char *str)
1096 int r = xstrtol (str, NULL, 10, &ret, "");
1097 if (r != LONGINT_OK) {
1098 error (g, _("could not parse integer in version number: %s"), str);
1104 /* At the moment, package format and package management is just a
1105 * simple function of the distro and major_version fields, so these
1106 * can never return an error. We might be cleverer in future.
1109 check_package_format (guestfs_h *g, struct inspect_fs *fs)
1111 switch (fs->distro) {
1112 case OS_DISTRO_FEDORA:
1113 case OS_DISTRO_MEEGO:
1114 case OS_DISTRO_REDHAT_BASED:
1115 case OS_DISTRO_RHEL:
1116 case OS_DISTRO_MANDRIVA:
1117 fs->package_format = OS_PACKAGE_FORMAT_RPM;
1120 case OS_DISTRO_DEBIAN:
1121 case OS_DISTRO_UBUNTU:
1122 case OS_DISTRO_LINUX_MINT:
1123 fs->package_format = OS_PACKAGE_FORMAT_DEB;
1126 case OS_DISTRO_ARCHLINUX:
1127 fs->package_format = OS_PACKAGE_FORMAT_PACMAN;
1129 case OS_DISTRO_GENTOO:
1130 fs->package_format = OS_PACKAGE_FORMAT_EBUILD;
1132 case OS_DISTRO_PARDUS:
1133 fs->package_format = OS_PACKAGE_FORMAT_PISI;
1136 case OS_DISTRO_WINDOWS:
1137 case OS_DISTRO_UNKNOWN:
1139 fs->package_format = OS_PACKAGE_FORMAT_UNKNOWN;
1145 check_package_management (guestfs_h *g, struct inspect_fs *fs)
1147 switch (fs->distro) {
1148 case OS_DISTRO_FEDORA:
1149 case OS_DISTRO_MEEGO:
1150 fs->package_management = OS_PACKAGE_MANAGEMENT_YUM;
1153 case OS_DISTRO_REDHAT_BASED:
1154 case OS_DISTRO_RHEL:
1155 if (fs->major_version >= 5)
1156 fs->package_management = OS_PACKAGE_MANAGEMENT_YUM;
1158 fs->package_management = OS_PACKAGE_MANAGEMENT_UP2DATE;
1161 case OS_DISTRO_DEBIAN:
1162 case OS_DISTRO_UBUNTU:
1163 case OS_DISTRO_LINUX_MINT:
1164 fs->package_management = OS_PACKAGE_MANAGEMENT_APT;
1167 case OS_DISTRO_ARCHLINUX:
1168 fs->package_management = OS_PACKAGE_MANAGEMENT_PACMAN;
1170 case OS_DISTRO_GENTOO:
1171 fs->package_management = OS_PACKAGE_MANAGEMENT_PORTAGE;
1173 case OS_DISTRO_PARDUS:
1174 fs->package_management = OS_PACKAGE_MANAGEMENT_PISI;
1176 case OS_DISTRO_MANDRIVA:
1177 fs->package_management = OS_PACKAGE_MANAGEMENT_URPMI;
1180 case OS_DISTRO_WINDOWS:
1181 case OS_DISTRO_UNKNOWN:
1183 fs->package_management = OS_PACKAGE_MANAGEMENT_UNKNOWN;
1188 static struct inspect_fs *
1189 search_for_root (guestfs_h *g, const char *root)
1191 if (g->nr_fses == 0) {
1192 error (g, _("no inspection data: call guestfs_inspect_os first"));
1197 struct inspect_fs *fs;
1198 for (i = 0; i < g->nr_fses; ++i) {
1200 if (fs->is_root && STREQ (root, fs->device))
1204 error (g, _("%s: root device not found: only call this function with a root device previously returned by guestfs_inspect_os"),
1210 guestfs__inspect_get_roots (guestfs_h *g)
1212 /* NB. Doesn't matter if g->nr_fses == 0. We just return an empty
1213 * list in this case.
1218 for (i = 0; i < g->nr_fses; ++i)
1219 if (g->fses[i].is_root)
1222 char **ret = calloc (count+1, sizeof (char *));
1224 perrorf (g, "calloc");
1229 for (i = 0; i < g->nr_fses; ++i) {
1230 if (g->fses[i].is_root) {
1231 ret[count] = safe_strdup (g, g->fses[i].device);
1241 guestfs__inspect_get_type (guestfs_h *g, const char *root)
1243 struct inspect_fs *fs = search_for_root (g, root);
1249 case OS_TYPE_LINUX: ret = safe_strdup (g, "linux"); break;
1250 case OS_TYPE_WINDOWS: ret = safe_strdup (g, "windows"); break;
1251 case OS_TYPE_FREEBSD: ret = safe_strdup (g, "freebsd"); break;
1252 case OS_TYPE_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1259 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
1261 struct inspect_fs *fs = search_for_root (g, root);
1265 return safe_strdup (g, fs->arch ? : "unknown");
1269 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
1271 struct inspect_fs *fs = search_for_root (g, root);
1276 switch (fs->distro) {
1277 case OS_DISTRO_ARCHLINUX: ret = safe_strdup (g, "archlinux"); break;
1278 case OS_DISTRO_DEBIAN: ret = safe_strdup (g, "debian"); break;
1279 case OS_DISTRO_FEDORA: ret = safe_strdup (g, "fedora"); break;
1280 case OS_DISTRO_GENTOO: ret = safe_strdup (g, "gentoo"); break;
1281 case OS_DISTRO_LINUX_MINT: ret = safe_strdup (g, "linuxmint"); break;
1282 case OS_DISTRO_MANDRIVA: ret = safe_strdup (g, "mandriva"); break;
1283 case OS_DISTRO_MEEGO: ret = safe_strdup (g, "meego"); break;
1284 case OS_DISTRO_PARDUS: ret = safe_strdup (g, "pardus"); break;
1285 case OS_DISTRO_REDHAT_BASED: ret = safe_strdup (g, "redhat-based"); break;
1286 case OS_DISTRO_RHEL: ret = safe_strdup (g, "rhel"); break;
1287 case OS_DISTRO_WINDOWS: ret = safe_strdup (g, "windows"); break;
1288 case OS_DISTRO_UBUNTU: ret = safe_strdup (g, "ubuntu"); break;
1289 case OS_DISTRO_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1296 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
1298 struct inspect_fs *fs = search_for_root (g, root);
1302 return fs->major_version;
1306 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
1308 struct inspect_fs *fs = search_for_root (g, root);
1312 return fs->minor_version;
1316 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
1318 struct inspect_fs *fs = search_for_root (g, root);
1322 return safe_strdup (g, fs->product_name ? : "unknown");
1326 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
1328 struct inspect_fs *fs = search_for_root (g, root);
1332 if (!fs->windows_systemroot) {
1333 error (g, _("not a Windows guest, or systemroot could not be determined"));
1337 return safe_strdup (g, fs->windows_systemroot);
1341 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
1343 struct inspect_fs *fs = search_for_root (g, root);
1349 /* If no fstab information (Windows) return just the root. */
1350 if (fs->nr_fstab == 0) {
1351 ret = calloc (3, sizeof (char *));
1352 ret[0] = safe_strdup (g, "/");
1353 ret[1] = safe_strdup (g, root);
1358 #define CRITERION fs->fstab[i].mountpoint[0] == '/'
1359 size_t i, count = 0;
1360 for (i = 0; i < fs->nr_fstab; ++i)
1364 /* Hashtables have 2N+1 entries. */
1365 ret = calloc (2*count+1, sizeof (char *));
1367 perrorf (g, "calloc");
1372 for (i = 0; i < fs->nr_fstab; ++i)
1374 ret[2*count] = safe_strdup (g, fs->fstab[i].mountpoint);
1375 ret[2*count+1] = safe_strdup (g, fs->fstab[i].device);
1384 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
1386 struct inspect_fs *fs = search_for_root (g, root);
1392 /* If no fstab information (Windows) return just the root. */
1393 if (fs->nr_fstab == 0) {
1394 ret = calloc (2, sizeof (char *));
1395 ret[0] = safe_strdup (g, root);
1400 ret = calloc (fs->nr_fstab + 1, sizeof (char *));
1402 perrorf (g, "calloc");
1407 for (i = 0; i < fs->nr_fstab; ++i)
1408 ret[i] = safe_strdup (g, fs->fstab[i].device);
1414 guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
1416 struct inspect_fs *fs = search_for_root (g, root);
1421 switch (fs->package_format) {
1422 case OS_PACKAGE_FORMAT_RPM: ret = safe_strdup (g, "rpm"); break;
1423 case OS_PACKAGE_FORMAT_DEB: ret = safe_strdup (g, "deb"); break;
1424 case OS_PACKAGE_FORMAT_PACMAN: ret = safe_strdup (g, "pacman"); break;
1425 case OS_PACKAGE_FORMAT_EBUILD: ret = safe_strdup (g, "ebuild"); break;
1426 case OS_PACKAGE_FORMAT_PISI: ret = safe_strdup (g, "pisi"); break;
1427 case OS_PACKAGE_FORMAT_UNKNOWN:
1429 ret = safe_strdup (g, "unknown");
1437 guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
1439 struct inspect_fs *fs = search_for_root (g, root);
1444 switch (fs->package_management) {
1445 case OS_PACKAGE_MANAGEMENT_YUM: ret = safe_strdup (g, "yum"); break;
1446 case OS_PACKAGE_MANAGEMENT_UP2DATE: ret = safe_strdup (g, "up2date"); break;
1447 case OS_PACKAGE_MANAGEMENT_APT: ret = safe_strdup (g, "apt"); break;
1448 case OS_PACKAGE_MANAGEMENT_PACMAN: ret = safe_strdup (g, "pacman"); break;
1449 case OS_PACKAGE_MANAGEMENT_PORTAGE: ret = safe_strdup (g, "portage"); break;
1450 case OS_PACKAGE_MANAGEMENT_PISI: ret = safe_strdup (g, "pisi"); break;
1451 case OS_PACKAGE_MANAGEMENT_URPMI: ret = safe_strdup (g, "urpmi"); break;
1452 case OS_PACKAGE_MANAGEMENT_UNKNOWN:
1454 ret = safe_strdup (g, "unknown");
1461 static struct guestfs_application_list *list_applications_rpm (guestfs_h *g, struct inspect_fs *fs);
1462 static struct guestfs_application_list *list_applications_deb (guestfs_h *g, struct inspect_fs *fs);
1463 static struct guestfs_application_list *list_applications_windows (guestfs_h *g, struct inspect_fs *fs);
1464 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);
1465 static void sort_applications (struct guestfs_application_list *);
1467 /* Unlike the simple inspect-get-* calls, this one assumes that the
1468 * disks are mounted up, and reads files from the mounted disks.
1470 struct guestfs_application_list *
1471 guestfs__inspect_list_applications (guestfs_h *g, const char *root)
1473 struct inspect_fs *fs = search_for_root (g, root);
1477 struct guestfs_application_list *ret = NULL;
1481 switch (fs->package_format) {
1482 case OS_PACKAGE_FORMAT_RPM:
1483 ret = list_applications_rpm (g, fs);
1488 case OS_PACKAGE_FORMAT_DEB:
1489 ret = list_applications_deb (g, fs);
1494 case OS_PACKAGE_FORMAT_PACMAN:
1495 case OS_PACKAGE_FORMAT_EBUILD:
1496 case OS_PACKAGE_FORMAT_PISI:
1497 case OS_PACKAGE_FORMAT_UNKNOWN:
1499 /* nothing - keep GCC happy */;
1503 case OS_TYPE_WINDOWS:
1504 ret = list_applications_windows (g, fs);
1509 case OS_TYPE_FREEBSD:
1510 case OS_TYPE_UNKNOWN:
1512 /* nothing - keep GCC happy */;
1516 /* Don't know how to do inspection. Not an error, return an
1519 ret = safe_malloc (g, sizeof *ret);
1524 sort_applications (ret);
1529 static struct guestfs_application_list *
1530 list_applications_rpm (guestfs_h *g, struct inspect_fs *fs)
1532 TMP_TEMPLATE_ON_STACK (tmpfile);
1534 if (download_to_tmp (g, "/var/lib/rpm/Name", tmpfile, 10000000) == -1)
1537 struct guestfs_application_list *apps = NULL, *ret = NULL;
1538 #define cmd_len (strlen (tmpfile) + 64)
1544 snprintf (cmd, cmd_len, "db_dump -p '%s'", tmpfile);
1547 fprintf (stderr, "list_applications_rpm: %s\n", cmd);
1549 pp = popen (cmd, "r");
1551 perrorf (g, "popen: %s", cmd);
1555 /* Ignore everything to end-of-header marker. */
1557 if (fgets (line, sizeof line, pp) == NULL) {
1558 error (g, _("unexpected end of output from db_dump command"));
1562 len = strlen (line);
1563 if (len > 0 && line[len-1] == '\n') {
1568 if (STREQ (line, "HEADER=END"))
1572 /* Allocate 'apps' list. */
1573 apps = safe_malloc (g, sizeof *apps);
1577 /* Read alternate lines until end of data marker. */
1579 if (fgets (line, sizeof line, pp) == NULL) {
1580 error (g, _("unexpected end of output from db_dump command"));
1584 len = strlen (line);
1585 if (len > 0 && line[len-1] == '\n') {
1590 if (STREQ (line, "DATA=END"))
1594 if (len > 0 && line[0] == ' ')
1596 /* Ignore any application name that contains non-printable chars.
1597 * In the db_dump output these would be escaped with backslash, so
1598 * we can just ignore any such line.
1600 if (strchr (p, '\\') == NULL)
1601 add_application (g, apps, p, "", 0, "", "", "", "", "", "");
1603 /* Discard next line. */
1604 if (fgets (line, sizeof line, pp) == NULL) {
1605 error (g, _("unexpected end of output from db_dump command"));
1610 /* Catch errors from the db_dump command. */
1611 if (pclose (pp) == -1) {
1612 perrorf (g, "pclose: %s", cmd);
1620 if (ret == NULL && apps != NULL)
1621 guestfs_free_application_list (apps);
1630 static struct guestfs_application_list *
1631 list_applications_deb (guestfs_h *g, struct inspect_fs *fs)
1633 TMP_TEMPLATE_ON_STACK (tmpfile);
1635 if (download_to_tmp (g, "/var/lib/dpkg/status", tmpfile, 10000000) == -1)
1638 struct guestfs_application_list *apps = NULL, *ret = NULL;
1642 char *name = NULL, *version = NULL, *release = NULL;
1643 int installed_flag = 0;
1645 fp = fopen (tmpfile, "r");
1647 perrorf (g, "fopen: %s", tmpfile);
1651 /* Allocate 'apps' list. */
1652 apps = safe_malloc (g, sizeof *apps);
1656 /* Read the temporary file. Each package entry is separated by
1658 * XXX Strictly speaking this is in mailbox header format, so it
1659 * would be possible for fields to spread across multiple lines,
1660 * although for the short fields that we are concerned about this is
1661 * unlikely and not seen in practice.
1663 while (fgets (line, sizeof line, fp) != NULL) {
1664 len = strlen (line);
1665 if (len > 0 && line[len-1] == '\n') {
1670 if (STRPREFIX (line, "Package: ")) {
1672 name = safe_strdup (g, &line[9]);
1674 else if (STRPREFIX (line, "Status: ")) {
1675 installed_flag = strstr (&line[8], "installed") != NULL;
1677 else if (STRPREFIX (line, "Version: ")) {
1680 char *p = strchr (&line[9], '-');
1683 version = safe_strdup (g, &line[9]);
1684 release = safe_strdup (g, p+1);
1686 version = safe_strdup (g, &line[9]);
1690 else if (STREQ (line, "")) {
1691 if (installed_flag && name && version)
1692 add_application (g, apps, name, "", 0, version, release ? : "",
1697 name = version = release = NULL;
1702 if (fclose (fp) == -1) {
1703 perrorf (g, "fclose: %s", tmpfile);
1711 if (ret == NULL && apps != NULL)
1712 guestfs_free_application_list (apps);
1722 /* XXX We already download the SOFTWARE hive when doing general
1723 * inspection. We could avoid this second download of the same file
1724 * by caching these entries in the handle.
1726 static struct guestfs_application_list *
1727 list_applications_windows (guestfs_h *g, struct inspect_fs *fs)
1729 TMP_TEMPLATE_ON_STACK (software_local);
1731 size_t len = strlen (fs->windows_systemroot) + 64;
1733 snprintf (software, len, "%s/system32/config/software",
1734 fs->windows_systemroot);
1736 char *software_path = resolve_windows_path_silently (g, software);
1738 /* If the software hive doesn't exist, just accept that we cannot
1739 * find product_name etc.
1743 struct guestfs_application_list *apps = NULL, *ret = NULL;
1745 hive_node_h *children = NULL;
1747 if (download_to_tmp (g, software_path, software_local, 100000000) == -1)
1750 h = hivex_open (software_local, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
1752 perrorf (g, "hivex_open");
1756 hive_node_h node = hivex_root (h);
1757 const char *hivepath[] =
1758 { "Microsoft", "Windows", "CurrentVersion", "Uninstall" };
1761 node != 0 && i < sizeof hivepath / sizeof hivepath[0];
1763 node = hivex_node_get_child (h, node, hivepath[i]);
1767 perrorf (g, "hivex: cannot locate HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
1771 children = hivex_node_children (h, node);
1772 if (children == NULL) {
1773 perrorf (g, "hivex_node_children");
1777 /* Allocate 'apps' list. */
1778 apps = safe_malloc (g, sizeof *apps);
1782 /* Consider any child node that has a DisplayName key.
1784 * http://nsis.sourceforge.net/Add_uninstall_information_to_Add/Remove_Programs#Optional_values
1786 for (i = 0; children[i] != 0; ++i) {
1789 char *display_name = NULL;
1790 char *version = NULL;
1791 char *install_path = NULL;
1792 char *publisher = NULL;
1794 char *comments = NULL;
1796 /* Use the node name as a proxy for the package name in Linux. The
1797 * display name is not language-independent, so it cannot be used.
1799 name = hivex_node_name (h, children[i]);
1801 perrorf (g, "hivex_node_get_name");
1805 value = hivex_node_get_value (h, children[i], "DisplayName");
1807 display_name = hivex_value_string (h, value);
1809 value = hivex_node_get_value (h, children[i], "DisplayVersion");
1811 version = hivex_value_string (h, value);
1812 value = hivex_node_get_value (h, children[i], "InstallLocation");
1814 install_path = hivex_value_string (h, value);
1815 value = hivex_node_get_value (h, children[i], "Publisher");
1817 publisher = hivex_value_string (h, value);
1818 value = hivex_node_get_value (h, children[i], "URLInfoAbout");
1820 url = hivex_value_string (h, value);
1821 value = hivex_node_get_value (h, children[i], "Comments");
1823 comments = hivex_value_string (h, value);
1825 add_application (g, apps, name, display_name, 0,
1828 install_path ? : "",
1836 free (display_name);
1838 free (install_path);
1847 if (ret == NULL && apps != NULL)
1848 guestfs_free_application_list (apps);
1849 if (h) hivex_close (h);
1851 free (software_path);
1853 /* Free up the temporary file. */
1854 unlink (software_local);
1855 #undef software_local_len
1861 add_application (guestfs_h *g, struct guestfs_application_list *apps,
1862 const char *name, const char *display_name, int32_t epoch,
1863 const char *version, const char *release,
1864 const char *install_path,
1865 const char *publisher, const char *url,
1866 const char *description)
1869 apps->val = safe_realloc (g, apps->val,
1870 apps->len * sizeof (struct guestfs_application));
1871 apps->val[apps->len-1].app_name = safe_strdup (g, name);
1872 apps->val[apps->len-1].app_display_name = safe_strdup (g, display_name);
1873 apps->val[apps->len-1].app_epoch = epoch;
1874 apps->val[apps->len-1].app_version = safe_strdup (g, version);
1875 apps->val[apps->len-1].app_release = safe_strdup (g, release);
1876 apps->val[apps->len-1].app_install_path = safe_strdup (g, install_path);
1877 /* XXX Translated path is not implemented yet. */
1878 apps->val[apps->len-1].app_trans_path = safe_strdup (g, "");
1879 apps->val[apps->len-1].app_publisher = safe_strdup (g, publisher);
1880 apps->val[apps->len-1].app_url = safe_strdup (g, url);
1881 /* XXX The next two are not yet implemented for any package
1882 * format, but we could easily support them for rpm and deb.
1884 apps->val[apps->len-1].app_source_package = safe_strdup (g, "");
1885 apps->val[apps->len-1].app_summary = safe_strdup (g, "");
1886 apps->val[apps->len-1].app_description = safe_strdup (g, description);
1889 /* Sort applications by name before returning the list. */
1891 compare_applications (const void *vp1, const void *vp2)
1893 const struct guestfs_application *v1 = vp1;
1894 const struct guestfs_application *v2 = vp2;
1896 return strcmp (v1->app_name, v2->app_name);
1900 sort_applications (struct guestfs_application_list *apps)
1902 if (apps && apps->val)
1903 qsort (apps->val, apps->len, sizeof (struct guestfs_application),
1904 compare_applications);
1907 /* Download to a guest file to a local temporary file. Refuse to
1908 * download the guest file if it is larger than max_size. The caller
1909 * is responsible for deleting the temporary file after use.
1912 download_to_tmp (guestfs_h *g, const char *filename,
1913 char *localtmp, int64_t max_size)
1919 size = guestfs_filesize (g, filename);
1921 /* guestfs_filesize failed and has already set error in handle */
1923 if (size > max_size) {
1924 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
1929 fd = mkstemp (localtmp);
1931 perrorf (g, "mkstemp");
1935 snprintf (buf, sizeof buf, "/dev/fd/%d", fd);
1937 if (guestfs_download (g, filename, buf) == -1) {
1943 if (close (fd) == -1) {
1944 perrorf (g, "close: %s", localtmp);
1952 #else /* no PCRE or hivex at compile time */
1954 /* XXX These functions should be in an optgroup. */
1956 #define NOT_IMPL(r) \
1957 error (g, _("inspection API not available since this version of libguestfs was compiled without PCRE or hivex libraries")); \
1961 guestfs__inspect_os (guestfs_h *g)
1967 guestfs__inspect_get_roots (guestfs_h *g)
1973 guestfs__inspect_get_type (guestfs_h *g, const char *root)
1979 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
1985 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
1991 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
1997 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
2003 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
2009 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
2015 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
2021 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
2027 guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
2033 guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
2038 struct guestfs_application_list *
2039 guestfs__inspect_list_applications (guestfs_h *g, const char *root)
2044 #endif /* no PCRE or hivex at compile time */
2047 guestfs___free_inspect_info (guestfs_h *g)
2050 for (i = 0; i < g->nr_fses; ++i) {
2051 free (g->fses[i].device);
2052 free (g->fses[i].product_name);
2053 free (g->fses[i].arch);
2054 free (g->fses[i].windows_systemroot);
2056 for (j = 0; j < g->fses[i].nr_fstab; ++j) {
2057 free (g->fses[i].fstab[j].device);
2058 free (g->fses[i].fstab[j].mountpoint);
2060 free (g->fses[i].fstab);
2067 /* In the Perl code this is a public function. */
2069 guestfs___feature_available (guestfs_h *g, const char *feature)
2071 /* If there's an error we should ignore it, so to do that we have to
2072 * temporarily replace the error handler with a null one.
2074 guestfs_error_handler_cb old_error_cb = g->error_cb;
2077 const char *groups[] = { feature, NULL };
2078 int r = guestfs_available (g, (char * const *) groups);
2080 g->error_cb = old_error_cb;
2082 return r == 0 ? 1 : 0;
2087 /* Match a regular expression which contains no captures. Returns
2088 * true if it matches or false if it doesn't.
2091 guestfs___match (guestfs_h *g, const char *str, const pcre *re)
2093 size_t len = strlen (str);
2096 r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
2097 if (r == PCRE_ERROR_NOMATCH)
2100 /* Internal error -- should not happen. */
2101 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
2102 __FILE__, __func__, r, str);
2109 /* Match a regular expression which contains exactly one capture. If
2110 * the string matches, return the capture, otherwise return NULL. The
2111 * caller must free the result.
2114 guestfs___match1 (guestfs_h *g, const char *str, const pcre *re)
2116 size_t len = strlen (str);
2119 r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
2120 if (r == PCRE_ERROR_NOMATCH)
2123 /* Internal error -- should not happen. */
2124 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
2125 __FILE__, __func__, r, str);
2129 return safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
2132 /* Match a regular expression which contains exactly two captures. */
2134 guestfs___match2 (guestfs_h *g, const char *str, const pcre *re,
2135 char **ret1, char **ret2)
2137 size_t len = strlen (str);
2140 r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
2141 if (r == PCRE_ERROR_NOMATCH)
2144 /* Internal error -- should not happen. */
2145 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
2146 __FILE__, __func__, r, str);
2150 *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
2151 *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
2156 /* Match a regular expression which contains exactly three captures. */
2158 guestfs___match3 (guestfs_h *g, const char *str, const pcre *re,
2159 char **ret1, char **ret2, char **ret3)
2161 size_t len = strlen (str);
2164 r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
2165 if (r == PCRE_ERROR_NOMATCH)
2168 /* Internal error -- should not happen. */
2169 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
2170 __FILE__, __func__, r, str);
2174 *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
2175 *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
2176 *ret3 = safe_strndup (g, &str[vec[6]], vec[7]-vec[6]);
2181 #endif /* HAVE_PCRE */