2 * Copyright (C) 2010-2011 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
39 #include "ignore-value.h"
43 #include "guestfs-internal.h"
44 #include "guestfs-internal-actions.h"
45 #include "guestfs_protocol.h"
47 #if defined(HAVE_PCRE) && defined(HAVE_HIVEX)
49 /* Some limits on what we will read, for safety. */
51 /* Small text configuration files.
53 * The upper limit is for general files that we grep or download. The
54 * largest such file is probably "txtsetup.sif" from Windows CDs
55 * (~500K). This number has to be larger than any legitimate file and
56 * smaller than the protocol message size.
58 * The lower limit is for files parsed by Augeas on the daemon side,
59 * where Augeas is running in reduced memory and can potentially
60 * create a lot of metadata so we really need to be careful about
63 #define MAX_SMALL_FILE_SIZE (2 * 1000 * 1000)
64 #define MAX_AUGEAS_FILE_SIZE (100 * 1000)
66 /* Maximum Windows Registry hive that we will download to /tmp. Some
67 * registries can be legitimately very large.
69 #define MAX_REGISTRY_SIZE (100 * 1000 * 1000)
71 /* Maximum RPM or dpkg database we will download to /tmp. */
72 #define MAX_PKG_DB_SIZE (10 * 1000 * 1000)
74 /* Compile all the regular expressions once when the shared library is
75 * loaded. PCRE is thread safe so we're supposedly OK here if
76 * multiple threads call into the libguestfs API functions below
79 static pcre *re_fedora;
80 static pcre *re_rhel_old;
82 static pcre *re_rhel_no_minor;
83 static pcre *re_major_minor;
84 static pcre *re_aug_seq;
86 static pcre *re_first_partition;
87 static pcre *re_freebsd;
88 static pcre *re_windows_version;
90 static void compile_regexps (void) __attribute__((constructor));
91 static void free_regexps (void) __attribute__((destructor));
94 compile_regexps (void)
99 #define COMPILE(re,pattern,options) \
101 re = pcre_compile ((pattern), (options), &err, &offset, NULL); \
103 ignore_value (write (2, err, strlen (err))); \
108 COMPILE (re_fedora, "Fedora release (\\d+)", 0);
109 COMPILE (re_rhel_old,
110 "(?:Red Hat|CentOS|Scientific Linux).*release (\\d+).*Update (\\d+)", 0);
112 "(?:Red Hat|CentOS|Scientific Linux).*release (\\d+)\\.(\\d+)", 0);
113 COMPILE (re_rhel_no_minor,
114 "(?:Red Hat|CentOS|Scientific Linux).*release (\\d+)", 0);
115 COMPILE (re_major_minor, "(\\d+)\\.(\\d+)", 0);
116 COMPILE (re_aug_seq, "/\\d+$", 0);
117 COMPILE (re_xdev, "^/dev/(?:h|s|v|xv)d([a-z]\\d*)$", 0);
118 COMPILE (re_first_partition, "^/dev/(?:h|s|v)d.1$", 0);
119 COMPILE (re_freebsd, "^/dev/ad(\\d+)s(\\d+)([a-z])$", 0);
120 COMPILE (re_windows_version, "^(\\d+)\\.(\\d+)", 0);
126 pcre_free (re_fedora);
127 pcre_free (re_rhel_old);
129 pcre_free (re_rhel_no_minor);
130 pcre_free (re_major_minor);
131 pcre_free (re_aug_seq);
133 pcre_free (re_first_partition);
134 pcre_free (re_freebsd);
135 pcre_free (re_windows_version);
138 /* The main inspection code. */
139 static int check_for_filesystem_on (guestfs_h *g, const char *device, int is_block, int is_partnum);
142 guestfs__inspect_os (guestfs_h *g)
144 /* Remove any information previously stored in the handle. */
145 guestfs___free_inspect_info (g);
147 if (guestfs_umount_all (g) == -1)
150 /* Iterate over all possible devices. Try to mount each
151 * (read-only). Examine ones which contain filesystems and add that
152 * information to the handle.
154 /* Look to see if any devices directly contain filesystems (RHBZ#590167). */
156 devices = guestfs_list_devices (g);
161 for (i = 0; devices[i] != NULL; ++i) {
162 if (check_for_filesystem_on (g, devices[i], 1, 0) == -1) {
163 guestfs___free_string_list (devices);
164 guestfs___free_inspect_info (g);
168 guestfs___free_string_list (devices);
170 /* Look at all partitions. */
172 partitions = guestfs_list_partitions (g);
173 if (partitions == NULL) {
174 guestfs___free_inspect_info (g);
178 for (i = 0; partitions[i] != NULL; ++i) {
179 if (check_for_filesystem_on (g, partitions[i], 0, i+1) == -1) {
180 guestfs___free_string_list (partitions);
181 guestfs___free_inspect_info (g);
185 guestfs___free_string_list (partitions);
187 /* Look at all LVs. */
188 if (guestfs___feature_available (g, "lvm2")) {
190 lvs = guestfs_lvs (g);
192 guestfs___free_inspect_info (g);
196 for (i = 0; lvs[i] != NULL; ++i) {
197 if (check_for_filesystem_on (g, lvs[i], 0, 0) == -1) {
198 guestfs___free_string_list (lvs);
199 guestfs___free_inspect_info (g);
203 guestfs___free_string_list (lvs);
206 /* At this point we have, in the handle, a list of all filesystems
207 * found and data about each one. Now we assemble the list of
208 * filesystems which are root devices and return that to the user.
209 * Fall through to guestfs__inspect_get_roots to do that.
211 char **ret = guestfs__inspect_get_roots (g);
213 guestfs___free_inspect_info (g);
217 /* Find out if 'device' contains a filesystem. If it does, add
218 * another entry in g->fses.
220 static int check_filesystem (guestfs_h *g, const char *device, int is_block, int is_partnum);
221 static int check_linux_root (guestfs_h *g, struct inspect_fs *fs);
222 static int check_freebsd_root (guestfs_h *g, struct inspect_fs *fs);
223 static int check_installer_root (guestfs_h *g, struct inspect_fs *fs);
224 static void check_architecture (guestfs_h *g, struct inspect_fs *fs);
225 static int check_hostname_unix (guestfs_h *g, struct inspect_fs *fs);
226 static int check_hostname_redhat (guestfs_h *g, struct inspect_fs *fs);
227 static int check_hostname_freebsd (guestfs_h *g, struct inspect_fs *fs);
228 static int check_fstab (guestfs_h *g, struct inspect_fs *fs);
229 static int check_windows_root (guestfs_h *g, struct inspect_fs *fs);
230 static int check_windows_arch (guestfs_h *g, struct inspect_fs *fs);
231 static int check_windows_software_registry (guestfs_h *g, struct inspect_fs *fs);
232 static int check_windows_system_registry (guestfs_h *g, struct inspect_fs *fs);
233 static char *case_sensitive_path_silently (guestfs_h *g, const char *);
234 static int is_file_nocase (guestfs_h *g, const char *);
235 static int is_dir_nocase (guestfs_h *g, const char *);
236 static int extend_fses (guestfs_h *g);
237 static int parse_unsigned_int (guestfs_h *g, const char *str);
238 static int parse_unsigned_int_ignore_trailing (guestfs_h *g, const char *str);
239 static int add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
240 const char *spec, const char *mp);
241 static char *resolve_fstab_device (guestfs_h *g, const char *spec);
242 static void check_package_format (guestfs_h *g, struct inspect_fs *fs);
243 static void check_package_management (guestfs_h *g, struct inspect_fs *fs);
244 static int download_to_tmp (guestfs_h *g, const char *filename, char *localtmp, int64_t max_size);
245 static int inspect_with_augeas (guestfs_h *g, struct inspect_fs *fs, const char *filename, int (*f) (guestfs_h *, struct inspect_fs *));
246 static char *first_line_of_file (guestfs_h *g, const char *filename);
247 static int first_egrep_of_file (guestfs_h *g, const char *filename, const char *eregex, int iflag, char **ret);
250 check_for_filesystem_on (guestfs_h *g, const char *device,
251 int is_block, int is_partnum)
253 /* Get vfs-type in order to check if it's a Linux(?) swap device.
254 * If there's an error we should ignore it, so to do that we have to
255 * temporarily replace the error handler with a null one.
257 guestfs_error_handler_cb old_error_cb = g->error_cb;
259 char *vfs_type = guestfs_vfs_type (g, device);
260 g->error_cb = old_error_cb;
262 int is_swap = vfs_type && STREQ (vfs_type, "swap");
264 debug (g, "check_for_filesystem_on: %s %d %d (%s)",
265 device, is_block, is_partnum,
266 vfs_type ? vfs_type : "failed to get vfs type");
270 if (extend_fses (g) == -1)
272 g->fses[g->nr_fses-1].is_swap = 1;
276 /* Try mounting the device. As above, ignore errors. */
278 int r = guestfs_mount_ro (g, device, "/");
279 if (r == -1 && vfs_type && STREQ (vfs_type, "ufs")) /* Hack for the *BSDs. */
280 r = guestfs_mount_vfs (g, "ro,ufstype=ufs2", "ufs", device, "/");
282 g->error_cb = old_error_cb;
286 /* Do the rest of the checks. */
287 r = check_filesystem (g, device, is_block, is_partnum);
289 /* Unmount the filesystem. */
290 if (guestfs_umount_all (g) == -1)
296 /* is_block and is_partnum are just hints: is_block is true if the
297 * filesystem is a whole block device (eg. /dev/sda). is_partnum
298 * is > 0 if the filesystem is a direct partition, and in this case
299 * it is the partition number counting from 1
300 * (eg. /dev/sda1 => is_partnum == 1).
303 check_filesystem (guestfs_h *g, const char *device,
304 int is_block, int is_partnum)
306 if (extend_fses (g) == -1)
309 struct inspect_fs *fs = &g->fses[g->nr_fses-1];
311 fs->device = safe_strdup (g, device);
312 fs->is_mountable = 1;
314 /* Optimize some of the tests by avoiding multiple tests of the same thing. */
315 int is_dir_etc = guestfs_is_dir (g, "/etc") > 0;
316 int is_dir_bin = guestfs_is_dir (g, "/bin") > 0;
317 int is_dir_share = guestfs_is_dir (g, "/share") > 0;
320 if (guestfs_is_file (g, "/grub/menu.lst") > 0 ||
321 guestfs_is_file (g, "/grub/grub.conf") > 0)
322 fs->content = FS_CONTENT_LINUX_BOOT;
324 else if (is_dir_etc &&
326 guestfs_is_file (g, "/etc/freebsd-update.conf") > 0 &&
327 guestfs_is_file (g, "/etc/fstab") > 0) {
328 /* Ignore /dev/sda1 which is a shadow of the real root filesystem
329 * that is probably /dev/sda5 (see:
330 * http://www.freebsd.org/doc/handbook/disk-organization.html)
332 if (match (g, device, re_first_partition))
336 fs->content = FS_CONTENT_FREEBSD_ROOT;
337 fs->format = OS_FORMAT_INSTALLED;
338 if (check_freebsd_root (g, fs) == -1)
342 else if (is_dir_etc &&
344 guestfs_is_file (g, "/etc/fstab") > 0) {
346 fs->content = FS_CONTENT_LINUX_ROOT;
347 fs->format = OS_FORMAT_INSTALLED;
348 if (check_linux_root (g, fs) == -1)
351 /* Linux /usr/local? */
352 else if (is_dir_etc &&
355 guestfs_exists (g, "/local") == 0 &&
356 guestfs_is_file (g, "/etc/fstab") == 0)
357 fs->content = FS_CONTENT_LINUX_USR_LOCAL;
359 else if (is_dir_etc &&
362 guestfs_exists (g, "/local") > 0 &&
363 guestfs_is_file (g, "/etc/fstab") == 0)
364 fs->content = FS_CONTENT_LINUX_USR;
366 else if (guestfs_is_dir (g, "/log") > 0 &&
367 guestfs_is_dir (g, "/run") > 0 &&
368 guestfs_is_dir (g, "/spool") > 0)
369 fs->content = FS_CONTENT_LINUX_VAR;
371 * Note that if a Windows guest has multiple disks and applications
372 * are installed on those other disks, then those other disks will
373 * contain "/Program Files" and "/System Volume Information". Those
374 * would *not* be Windows root disks. (RHBZ#674130)
376 else if (is_file_nocase (g, "/AUTOEXEC.BAT") > 0 ||
377 is_dir_nocase (g, "/WINDOWS") > 0 ||
378 is_dir_nocase (g, "/WIN32") > 0 ||
379 is_dir_nocase (g, "/WINNT") > 0 ||
380 is_file_nocase (g, "/boot.ini") > 0 ||
381 is_file_nocase (g, "/ntldr") > 0) {
383 fs->content = FS_CONTENT_WINDOWS_ROOT;
384 fs->format = OS_FORMAT_INSTALLED;
385 if (check_windows_root (g, fs) == -1)
388 /* Windows volume with installed applications (but not root)? */
389 else if (is_dir_nocase (g, "/System Volume Information") > 0 &&
390 is_dir_nocase (g, "/Program Files") > 0)
391 fs->content = FS_CONTENT_WINDOWS_VOLUME_WITH_APPS;
392 /* Windows volume (but not root)? */
393 else if (is_dir_nocase (g, "/System Volume Information") > 0)
394 fs->content = FS_CONTENT_WINDOWS_VOLUME;
395 /* Install CD/disk? Skip these checks if it's not a whole device
396 * (eg. CD) or the first partition (eg. bootable USB key).
398 else if ((is_block || is_partnum == 1) &&
399 (guestfs_is_file (g, "/isolinux/isolinux.cfg") > 0 ||
400 guestfs_is_dir (g, "/EFI/BOOT") > 0 ||
401 guestfs_is_file (g, "/images/install.img") > 0 ||
402 guestfs_is_dir (g, "/.disk") > 0 ||
403 guestfs_is_file (g, "/.discinfo") > 0 ||
404 guestfs_is_file (g, "/i386/txtsetup.sif") > 0 ||
405 guestfs_is_file (g, "/amd64/txtsetup.sif")) > 0) {
407 fs->content = FS_CONTENT_INSTALLER;
408 fs->format = OS_FORMAT_INSTALLER;
409 if (check_installer_root (g, fs) == -1)
416 /* Set fs->product_name to the first line of the release file. */
418 parse_release_file (guestfs_h *g, struct inspect_fs *fs,
419 const char *release_filename)
421 fs->product_name = first_line_of_file (g, release_filename);
422 if (fs->product_name == NULL)
427 /* Parse generic MAJOR.MINOR from the fs->product_name string. */
429 parse_major_minor (guestfs_h *g, struct inspect_fs *fs)
433 if (match2 (g, fs->product_name, re_major_minor, &major, &minor)) {
434 fs->major_version = parse_unsigned_int (g, major);
436 if (fs->major_version == -1) {
440 fs->minor_version = parse_unsigned_int (g, minor);
442 if (fs->minor_version == -1)
448 /* Ubuntu has /etc/lsb-release containing:
449 * DISTRIB_ID=Ubuntu # Distro
450 * DISTRIB_RELEASE=10.04 # Version
451 * DISTRIB_CODENAME=lucid
452 * DISTRIB_DESCRIPTION="Ubuntu 10.04.1 LTS" # Product name
454 * [Ubuntu-derived ...] Linux Mint was found to have this:
455 * DISTRIB_ID=LinuxMint
457 * DISTRIB_CODENAME=julia
458 * DISTRIB_DESCRIPTION="Linux Mint 10 Julia"
459 * Linux Mint also has /etc/linuxmint/info with more information,
460 * but we can use the LSB file.
463 * LSB_VERSION=lsb-4.0-amd64:lsb-4.0-noarch
464 * DISTRIB_ID=MandrivaLinux
465 * DISTRIB_RELEASE=2010.1
466 * DISTRIB_CODENAME=Henry_Farman
467 * DISTRIB_DESCRIPTION="Mandriva Linux 2010.1"
468 * Mandriva also has a normal release file called /etc/mandriva-release.
471 parse_lsb_release (guestfs_h *g, struct inspect_fs *fs)
473 const char *filename = "/etc/lsb-release";
479 /* Don't trust guestfs_head_n not to break with very large files.
480 * Check the file size is something reasonable first.
482 size = guestfs_filesize (g, filename);
484 /* guestfs_filesize failed and has already set error in handle */
486 if (size > MAX_SMALL_FILE_SIZE) {
487 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
492 lines = guestfs_head_n (g, 10, filename);
496 for (i = 0; lines[i] != NULL; ++i) {
497 if (fs->distro == 0 &&
498 STREQ (lines[i], "DISTRIB_ID=Ubuntu")) {
499 fs->distro = OS_DISTRO_UBUNTU;
502 else if (fs->distro == 0 &&
503 STREQ (lines[i], "DISTRIB_ID=LinuxMint")) {
504 fs->distro = OS_DISTRO_LINUX_MINT;
507 else if (fs->distro == 0 &&
508 STREQ (lines[i], "DISTRIB_ID=MandrivaLinux")) {
509 fs->distro = OS_DISTRO_MANDRIVA;
512 else if (STRPREFIX (lines[i], "DISTRIB_RELEASE=")) {
514 if (match2 (g, &lines[i][16], re_major_minor, &major, &minor)) {
515 fs->major_version = parse_unsigned_int (g, major);
517 if (fs->major_version == -1) {
519 guestfs___free_string_list (lines);
522 fs->minor_version = parse_unsigned_int (g, minor);
524 if (fs->minor_version == -1) {
525 guestfs___free_string_list (lines);
530 else if (fs->product_name == NULL &&
531 (STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=\"") ||
532 STRPREFIX (lines[i], "DISTRIB_DESCRIPTION='"))) {
533 size_t len = strlen (lines[i]) - 21 - 1;
534 fs->product_name = safe_strndup (g, &lines[i][21], len);
537 else if (fs->product_name == NULL &&
538 STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=")) {
539 size_t len = strlen (lines[i]) - 20;
540 fs->product_name = safe_strndup (g, &lines[i][20], len);
545 guestfs___free_string_list (lines);
549 /* The currently mounted device is known to be a Linux root. Try to
550 * determine from this the distro, version, etc. Also parse
551 * /etc/fstab to determine the arrangement of mountpoints and
552 * associated devices.
555 check_linux_root (guestfs_h *g, struct inspect_fs *fs)
559 fs->type = OS_TYPE_LINUX;
561 if (guestfs_exists (g, "/etc/lsb-release") > 0) {
562 r = parse_lsb_release (g, fs);
563 if (r == -1) /* error */
565 if (r == 1) /* ok - detected the release from this file */
566 goto skip_release_checks;
569 if (guestfs_exists (g, "/etc/redhat-release") > 0) {
570 fs->distro = OS_DISTRO_REDHAT_BASED; /* Something generic Red Hat-like. */
572 if (parse_release_file (g, fs, "/etc/redhat-release") == -1)
576 if ((major = match1 (g, fs->product_name, re_fedora)) != NULL) {
577 fs->distro = OS_DISTRO_FEDORA;
578 fs->major_version = parse_unsigned_int (g, major);
580 if (fs->major_version == -1)
583 else if (match2 (g, fs->product_name, re_rhel_old, &major, &minor) ||
584 match2 (g, fs->product_name, re_rhel, &major, &minor)) {
585 fs->distro = OS_DISTRO_RHEL;
586 fs->major_version = parse_unsigned_int (g, major);
588 if (fs->major_version == -1) {
592 fs->minor_version = parse_unsigned_int (g, minor);
594 if (fs->minor_version == -1)
597 else if ((major = match1 (g, fs->product_name, re_rhel_no_minor)) != NULL) {
598 fs->distro = OS_DISTRO_RHEL;
599 fs->major_version = parse_unsigned_int (g, major);
601 if (fs->major_version == -1)
603 fs->minor_version = 0;
606 else if (guestfs_exists (g, "/etc/debian_version") > 0) {
607 fs->distro = OS_DISTRO_DEBIAN;
609 if (parse_release_file (g, fs, "/etc/debian_version") == -1)
612 if (parse_major_minor (g, fs) == -1)
615 else if (guestfs_exists (g, "/etc/pardus-release") > 0) {
616 fs->distro = OS_DISTRO_PARDUS;
618 if (parse_release_file (g, fs, "/etc/pardus-release") == -1)
621 if (parse_major_minor (g, fs) == -1)
624 else if (guestfs_exists (g, "/etc/arch-release") > 0) {
625 fs->distro = OS_DISTRO_ARCHLINUX;
627 /* /etc/arch-release file is empty and I can't see a way to
628 * determine the actual release or product string.
631 else if (guestfs_exists (g, "/etc/gentoo-release") > 0) {
632 fs->distro = OS_DISTRO_GENTOO;
634 if (parse_release_file (g, fs, "/etc/gentoo-release") == -1)
637 if (parse_major_minor (g, fs) == -1)
640 else if (guestfs_exists (g, "/etc/meego-release") > 0) {
641 fs->distro = OS_DISTRO_MEEGO;
643 if (parse_release_file (g, fs, "/etc/meego-release") == -1)
646 if (parse_major_minor (g, fs) == -1)
649 else if (guestfs_exists (g, "/etc/slackware-version") > 0) {
650 fs->distro = OS_DISTRO_SLACKWARE;
652 if (parse_release_file (g, fs, "/etc/slackware-version") == -1)
655 if (parse_major_minor (g, fs) == -1)
659 skip_release_checks:;
661 /* If distro test above was successful, work out the package format. */
662 check_package_format (g, fs);
663 check_package_management (g, fs);
665 /* Determine the architecture. */
666 check_architecture (g, fs);
668 /* We already know /etc/fstab exists because it's part of the test
669 * for Linux root above. We must now parse this file to determine
670 * which filesystems are used by the operating system and how they
673 if (inspect_with_augeas (g, fs, "/etc/fstab", check_fstab) == -1)
676 /* Determine hostname. */
677 if (check_hostname_unix (g, fs) == -1)
683 /* The currently mounted device is known to be a FreeBSD root. */
685 check_freebsd_root (guestfs_h *g, struct inspect_fs *fs)
687 fs->type = OS_TYPE_FREEBSD;
689 /* FreeBSD has no authoritative version file. The version number is
690 * in /etc/motd, which the system administrator might edit, but
691 * we'll use that anyway.
694 if (guestfs_exists (g, "/etc/motd") > 0) {
695 if (parse_release_file (g, fs, "/etc/motd") == -1)
698 if (parse_major_minor (g, fs) == -1)
702 /* Determine the architecture. */
703 check_architecture (g, fs);
705 /* We already know /etc/fstab exists because it's part of the test above. */
706 if (inspect_with_augeas (g, fs, "/etc/fstab", check_fstab) == -1)
709 /* Determine hostname. */
710 if (check_hostname_unix (g, fs) == -1)
716 /* Debian/Ubuntu install disks are easy ...
718 * These files are added by the debian-cd program, and it is worth
719 * looking at the source code to determine exact values, in
720 * particular '/usr/share/debian-cd/tools/start_new_disc'
722 * XXX Architecture? We could parse it out of the product name
723 * string, but that seems quite hairy. We could look for the names
724 * of packages. Also note that some Debian install disks are
728 check_debian_installer_root (guestfs_h *g, struct inspect_fs *fs)
730 fs->product_name = first_line_of_file (g, "/.disk/info");
731 if (!fs->product_name)
734 fs->type = OS_TYPE_LINUX;
735 if (STRPREFIX (fs->product_name, "Ubuntu"))
736 fs->distro = OS_DISTRO_UBUNTU;
737 else if (STRPREFIX (fs->product_name, "Debian"))
738 fs->distro = OS_DISTRO_DEBIAN;
740 (void) parse_major_minor (g, fs);
742 if (guestfs_is_file (g, "/.disk/cd_type") > 0) {
743 char *cd_type = first_line_of_file (g, "/.disk/cd_type");
747 if (STRPREFIX (cd_type, "dvd/single") ||
748 STRPREFIX (cd_type, "full_cd/single")) {
749 fs->is_multipart_disk = 0;
750 fs->is_netinst_disk = 0;
752 else if (STRPREFIX (cd_type, "dvd") ||
753 STRPREFIX (cd_type, "full_cd")) {
754 fs->is_multipart_disk = 1;
755 fs->is_netinst_disk = 0;
757 else if (STRPREFIX (cd_type, "not_complete")) {
758 fs->is_multipart_disk = 0;
759 fs->is_netinst_disk = 1;
768 /* Take string which must look like "key = value" and find the value.
769 * There may or may not be spaces before and after the equals sign.
770 * This function is used by both check_fedora_installer_root and
771 * check_w2k3_installer_root.
774 find_value (const char *kv)
778 p = strchr (kv, '=');
784 } while (c_isspace (*p));
789 /* Fedora CDs and DVD (not netinst). The /.treeinfo file contains
790 * an initial section somewhat like this:
801 check_fedora_installer_root (guestfs_h *g, struct inspect_fs *fs)
806 int discnum = 0, totaldiscs = 0;
808 fs->type = OS_TYPE_LINUX;
810 r = first_egrep_of_file (g, "/.treeinfo",
811 "^family = Fedora$", 0, &str);
815 fs->distro = OS_DISTRO_FEDORA;
819 r = first_egrep_of_file (g, "/.treeinfo",
820 "^family = Red Hat Enterprise Linux$", 0, &str);
824 fs->distro = OS_DISTRO_RHEL;
828 /* XXX should do major.minor before this */
829 r = first_egrep_of_file (g, "/.treeinfo",
830 "^version = [[:digit:]]+", 0, &str);
834 v = find_value (str);
835 fs->major_version = parse_unsigned_int_ignore_trailing (g, v);
837 if (fs->major_version == -1)
841 r = first_egrep_of_file (g, "/.treeinfo",
842 "^arch = [-_[:alnum:]]+$", 0, &str);
846 v = find_value (str);
847 fs->arch = safe_strdup (g, v);
851 r = first_egrep_of_file (g, "/.treeinfo",
852 "^discnum = [[:digit:]]+$", 0, &str);
856 v = find_value (str);
857 discnum = parse_unsigned_int (g, v);
863 r = first_egrep_of_file (g, "/.treeinfo",
864 "^totaldiscs = [[:digit:]]+$", 0, &str);
868 v = find_value (str);
869 totaldiscs = parse_unsigned_int (g, v);
871 if (totaldiscs == -1)
875 fs->is_multipart_disk = totaldiscs > 0;
876 /* and what about discnum? */
881 /* Linux with /isolinux/isolinux.cfg.
883 * This file is not easily parsable so we have to do our best.
884 * Look for the "menu title" line which contains:
885 * menu title Welcome to Fedora 14! # since at least Fedora 10
886 * menu title Welcome to Red Hat Enterprise Linux 6.0!
889 check_isolinux_installer_root (guestfs_h *g, struct inspect_fs *fs)
894 fs->type = OS_TYPE_LINUX;
896 r = first_egrep_of_file (g, "/isolinux/isolinux.cfg",
897 "^menu title Welcome to Fedora [[:digit:]]+",
902 fs->distro = OS_DISTRO_FEDORA;
903 fs->major_version = parse_unsigned_int_ignore_trailing (g, &str[29]);
905 if (fs->major_version == -1)
909 /* XXX parse major.minor */
910 r = first_egrep_of_file (g, "/isolinux/isolinux.cfg",
911 "^menu title Welcome to Red Hat Enterprise Linux [[:digit:]]+",
916 fs->distro = OS_DISTRO_RHEL;
917 fs->major_version = parse_unsigned_int_ignore_trailing (g, &str[47]);
919 if (fs->major_version == -1)
926 /* Windows 2003 and similar versions.
928 * NB: txtsetup file contains Windows \r\n line endings, which guestfs_grep
929 * does not remove. We have to remove them by hand here.
934 size_t n = strlen (str);
935 if (n > 0 && str[n-1] == '\r')
940 trim_quot (char *str)
942 size_t n = strlen (str);
943 if (n > 0 && str[n-1] == '"')
948 check_w2k3_installer_root (guestfs_h *g, struct inspect_fs *fs,
949 const char *txtsetup)
955 fs->type = OS_TYPE_WINDOWS;
956 fs->distro = OS_DISTRO_WINDOWS;
958 r = first_egrep_of_file (g, txtsetup,
959 "^productname[[:space:]]*=[[:space:]]*\"", 1, &str);
965 v = find_value (str);
966 fs->product_name = safe_strdup (g, v+1);
970 r = first_egrep_of_file (g, txtsetup,
971 "^majorversion[[:space:]]*=[[:space:]]*[[:digit:]]+",
977 v = find_value (str);
978 fs->major_version = parse_unsigned_int_ignore_trailing (g, v);
980 if (fs->major_version == -1)
984 r = first_egrep_of_file (g, txtsetup,
985 "^minorversion[[:space:]]*=[[:space:]]*[[:digit:]]+",
991 v = find_value (str);
992 fs->minor_version = parse_unsigned_int_ignore_trailing (g, v);
994 if (fs->minor_version == -1)
998 /* This is the windows systemroot that would be chosen on
999 * installation by default, although not necessarily the one that
1000 * the user will finally choose.
1002 r = first_egrep_of_file (g, txtsetup, "^defaultpath[[:space:]]*=[[:space:]]*",
1008 v = find_value (str);
1009 fs->windows_systemroot = safe_strdup (g, v);
1016 /* The currently mounted device is very likely to be an installer. */
1018 check_installer_root (guestfs_h *g, struct inspect_fs *fs)
1020 /* The presence of certain files indicates a live CD.
1022 * XXX Fedora netinst contains a ~120MB squashfs called
1023 * /images/install.img. However this is not a live CD (unlike the
1024 * Fedora live CDs which contain the same, but larger file). We
1025 * need to unpack this and look inside to tell the difference.
1027 if (guestfs_is_file (g, "/casper/filesystem.squashfs") > 0)
1028 fs->is_live_disk = 1;
1030 /* Debian/Ubuntu. */
1031 if (guestfs_is_file (g, "/.disk/info") > 0) {
1032 if (check_debian_installer_root (g, fs) == -1)
1036 /* Fedora CDs and DVD (not netinst). */
1037 else if (guestfs_is_file (g, "/.treeinfo") > 0) {
1038 if (check_fedora_installer_root (g, fs) == -1)
1042 /* Linux with /isolinux/isolinux.cfg. */
1043 else if (guestfs_is_file (g, "/isolinux/isolinux.cfg") > 0) {
1044 if (check_isolinux_installer_root (g, fs) == -1)
1048 /* Windows 2003 64 bit */
1049 else if (guestfs_is_file (g, "/amd64/txtsetup.sif") > 0) {
1050 fs->arch = safe_strdup (g, "x86_64");
1051 if (check_w2k3_installer_root (g, fs, "/amd64/txtsetup.sif") == -1)
1055 /* Windows 2003 32 bit */
1056 else if (guestfs_is_file (g, "/i386/txtsetup.sif") > 0) {
1057 fs->arch = safe_strdup (g, "i386");
1058 if (check_w2k3_installer_root (g, fs, "/i386/txtsetup.sif") == -1)
1066 check_architecture (guestfs_h *g, struct inspect_fs *fs)
1068 const char *binaries[] =
1069 { "/bin/bash", "/bin/ls", "/bin/echo", "/bin/rm", "/bin/sh" };
1072 for (i = 0; i < sizeof binaries / sizeof binaries[0]; ++i) {
1073 if (guestfs_is_file (g, binaries[i]) > 0) {
1074 /* Ignore errors from file_architecture call. */
1075 guestfs_error_handler_cb old_error_cb = g->error_cb;
1077 char *arch = guestfs_file_architecture (g, binaries[i]);
1078 g->error_cb = old_error_cb;
1081 /* String will be owned by handle, freed by
1082 * guestfs___free_inspect_info.
1091 /* Try several methods to determine the hostname from a Linux or
1092 * FreeBSD guest. Note that type and distro have been set, so we can
1093 * use that information to direct the search.
1096 check_hostname_unix (guestfs_h *g, struct inspect_fs *fs)
1100 /* Red Hat-derived would be in /etc/sysconfig/network, and
1101 * Debian-derived in the file /etc/hostname. Very old Debian and
1102 * SUSE use /etc/HOSTNAME. It's best to just look for each of
1103 * these files in turn, rather than try anything clever based on
1106 if (guestfs_is_file (g, "/etc/HOSTNAME")) {
1107 fs->hostname = first_line_of_file (g, "/etc/HOSTNAME");
1108 if (fs->hostname == NULL)
1111 else if (guestfs_is_file (g, "/etc/hostname")) {
1112 fs->hostname = first_line_of_file (g, "/etc/hostname");
1113 if (fs->hostname == NULL)
1116 else if (guestfs_is_file (g, "/etc/sysconfig/network")) {
1117 if (inspect_with_augeas (g, fs, "/etc/sysconfig/network",
1118 check_hostname_redhat) == -1)
1123 case OS_TYPE_FREEBSD:
1124 /* /etc/rc.conf contains the hostname, but there is no Augeas lens
1127 if (guestfs_is_file (g, "/etc/rc.conf")) {
1128 if (check_hostname_freebsd (g, fs) == -1)
1133 case OS_TYPE_WINDOWS: /* not here, see check_windows_system_registry */
1134 case OS_TYPE_UNKNOWN:
1136 /* nothing, keep GCC warnings happy */;
1142 /* Parse the hostname from /etc/sysconfig/network. This must be called
1143 * from the inspect_with_augeas wrapper.
1146 check_hostname_redhat (guestfs_h *g, struct inspect_fs *fs)
1150 hostname = guestfs_aug_get (g, "/files/etc/sysconfig/network/HOSTNAME");
1154 fs->hostname = hostname; /* freed by guestfs___free_inspect_info */
1158 /* Parse the hostname from /etc/rc.conf. On FreeBSD this file
1159 * contains comments, blank lines and:
1160 * hostname="freebsd8.example.com"
1161 * ifconfig_re0="DHCP"
1166 check_hostname_freebsd (guestfs_h *g, struct inspect_fs *fs)
1168 const char *filename = "/etc/rc.conf";
1173 /* Don't trust guestfs_read_lines not to break with very large files.
1174 * Check the file size is something reasonable first.
1176 size = guestfs_filesize (g, filename);
1178 /* guestfs_filesize failed and has already set error in handle */
1180 if (size > MAX_SMALL_FILE_SIZE) {
1181 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
1186 lines = guestfs_read_lines (g, filename);
1190 for (i = 0; lines[i] != NULL; ++i) {
1191 if (STRPREFIX (lines[i], "hostname=\"") ||
1192 STRPREFIX (lines[i], "hostname='")) {
1193 size_t len = strlen (lines[i]) - 10 - 1;
1194 fs->hostname = safe_strndup (g, &lines[i][10], len);
1196 } else if (STRPREFIX (lines[i], "hostname=")) {
1197 size_t len = strlen (lines[i]) - 9;
1198 fs->hostname = safe_strndup (g, &lines[i][9], len);
1203 guestfs___free_string_list (lines);
1208 check_fstab (guestfs_h *g, struct inspect_fs *fs)
1210 char **lines = guestfs_aug_ls (g, "/files/etc/fstab");
1214 if (lines[0] == NULL) {
1215 error (g, _("could not parse /etc/fstab or empty file"));
1216 guestfs___free_string_list (lines);
1222 for (i = 0; lines[i] != NULL; ++i) {
1223 /* Ignore comments. Only care about sequence lines which
1226 if (match (g, lines[i], re_aug_seq)) {
1227 snprintf (augpath, sizeof augpath, "%s/spec", lines[i]);
1228 char *spec = guestfs_aug_get (g, augpath);
1230 guestfs___free_string_list (lines);
1234 snprintf (augpath, sizeof augpath, "%s/file", lines[i]);
1235 char *mp = guestfs_aug_get (g, augpath);
1237 guestfs___free_string_list (lines);
1242 int r = add_fstab_entry (g, fs, spec, mp);
1247 guestfs___free_string_list (lines);
1253 guestfs___free_string_list (lines);
1257 /* Add a filesystem and possibly a mountpoint entry for
1258 * the root filesystem 'fs'.
1260 * 'spec' is the fstab spec field, which might be a device name or a
1261 * pseudodevice or 'UUID=...' or 'LABEL=...'.
1263 * 'mp' is the mount point, which could also be 'swap' or 'none'.
1266 add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
1267 const char *spec, const char *mp)
1269 /* Ignore certain mountpoints. */
1270 if (STRPREFIX (mp, "/dev/") ||
1271 STREQ (mp, "/dev") ||
1272 STRPREFIX (mp, "/media/") ||
1273 STRPREFIX (mp, "/proc/") ||
1274 STREQ (mp, "/proc") ||
1275 STRPREFIX (mp, "/selinux/") ||
1276 STREQ (mp, "/selinux") ||
1277 STRPREFIX (mp, "/sys/") ||
1281 /* Ignore /dev/fd (floppy disks) (RHBZ#642929) and CD-ROM drives. */
1282 if ((STRPREFIX (spec, "/dev/fd") && c_isdigit (spec[7])) ||
1283 STREQ (spec, "/dev/floppy") ||
1284 STREQ (spec, "/dev/cdrom"))
1287 /* Resolve UUID= and LABEL= to the actual device. */
1288 char *device = NULL;
1289 if (STRPREFIX (spec, "UUID="))
1290 device = guestfs_findfs_uuid (g, &spec[5]);
1291 else if (STRPREFIX (spec, "LABEL="))
1292 device = guestfs_findfs_label (g, &spec[6]);
1293 /* Ignore "/.swap" (Pardus) and pseudo-devices like "tmpfs". */
1294 else if (STRPREFIX (spec, "/dev/"))
1295 /* Resolve guest block device names. */
1296 device = resolve_fstab_device (g, spec);
1298 /* If we haven't resolved the device successfully by this point,
1299 * we don't care, just ignore it.
1304 char *mountpoint = safe_strdup (g, mp);
1306 /* Add this to the fstab entry in 'fs'.
1307 * Note these are further filtered by guestfs_inspect_get_mountpoints
1308 * and guestfs_inspect_get_filesystems.
1310 size_t n = fs->nr_fstab + 1;
1311 struct inspect_fstab_entry *p;
1313 p = realloc (fs->fstab, n * sizeof (struct inspect_fstab_entry));
1315 perrorf (g, "realloc");
1324 /* These are owned by the handle and freed by guestfs___free_inspect_info. */
1325 fs->fstab[n-1].device = device;
1326 fs->fstab[n-1].mountpoint = mountpoint;
1328 debug (g, "fstab: device=%s mountpoint=%s", device, mountpoint);
1333 /* Resolve block device name to the libguestfs device name, eg.
1334 * /dev/xvdb1 => /dev/vdb1; and /dev/mapper/VG-LV => /dev/VG/LV. This
1335 * assumes that disks were added in the same order as they appear to
1336 * the real VM, which is a reasonable assumption to make. Return
1337 * anything we don't recognize unchanged.
1340 resolve_fstab_device (guestfs_h *g, const char *spec)
1343 char *device = NULL;
1344 char *bsddisk, *bsdslice, *bsdpart;
1346 if (STRPREFIX (spec, "/dev/mapper/")) {
1347 /* LVM2 does some strange munging on /dev/mapper paths for VGs and
1348 * LVs which contain '-' character:
1350 * ><fs> lvcreate LV--test VG--test 32
1351 * ><fs> debug ls /dev/mapper
1352 * VG----test-LV----test
1354 * This makes it impossible to reverse those paths directly, so
1355 * we have implemented lvm_canonical_lv_name in the daemon.
1357 device = guestfs_lvm_canonical_lv_name (g, spec);
1359 else if ((a1 = match1 (g, spec, re_xdev)) != NULL) {
1360 char **devices = guestfs_list_devices (g);
1361 if (devices == NULL)
1365 for (count = 0; devices[count] != NULL; count++)
1368 size_t i = a1[0] - 'a'; /* a1[0] is always [a-z] because of regex. */
1370 size_t len = strlen (devices[i]) + strlen (a1) + 16;
1371 device = safe_malloc (g, len);
1372 snprintf (device, len, "%s%s", devices[i], &a1[1]);
1376 guestfs___free_string_list (devices);
1378 else if (match3 (g, spec, re_freebsd, &bsddisk, &bsdslice, &bsdpart)) {
1379 /* FreeBSD disks are organized quite differently. See:
1380 * http://www.freebsd.org/doc/handbook/disk-organization.html
1381 * FreeBSD "partitions" are exposed as quasi-extended partitions
1382 * numbered from 5 in Linux. I have no idea what happens when you
1383 * have multiple "slices" (the FreeBSD term for MBR partitions).
1385 int disk = parse_unsigned_int (g, bsddisk);
1386 int slice = parse_unsigned_int (g, bsdslice);
1387 int part = bsdpart[0] - 'a' /* counting from 0 */;
1392 if (disk == -1 || disk > 26 ||
1393 slice <= 0 || slice > 1 /* > 4 .. see comment above */ ||
1394 part < 0 || part >= 26)
1397 device = safe_asprintf (g, "/dev/sd%c%d", disk + 'a', part + 5);
1401 /* Didn't match device pattern, return original spec unchanged. */
1403 device = safe_strdup (g, spec);
1408 /* XXX Handling of boot.ini in the Perl version was pretty broken. It
1409 * essentially didn't do anything for modern Windows guests.
1410 * Therefore I've omitted all that code.
1413 check_windows_root (guestfs_h *g, struct inspect_fs *fs)
1415 fs->type = OS_TYPE_WINDOWS;
1416 fs->distro = OS_DISTRO_WINDOWS;
1418 /* Try to find Windows systemroot using some common locations. */
1419 const char *systemroots[] =
1420 { "/windows", "/winnt", "/win32", "/win" };
1422 char *systemroot = NULL;
1424 systemroot == NULL && i < sizeof systemroots / sizeof systemroots[0];
1426 systemroot = case_sensitive_path_silently (g, systemroots[i]);
1430 error (g, _("cannot resolve Windows %%SYSTEMROOT%%"));
1434 debug (g, "windows %%SYSTEMROOT%% = %s", systemroot);
1436 /* Freed by guestfs___free_inspect_info. */
1437 fs->windows_systemroot = systemroot;
1439 if (check_windows_arch (g, fs) == -1)
1442 /* Product name and version. */
1443 if (check_windows_software_registry (g, fs) == -1)
1446 check_package_format (g, fs);
1447 check_package_management (g, fs);
1450 if (check_windows_system_registry (g, fs) == -1)
1457 check_windows_arch (guestfs_h *g, struct inspect_fs *fs)
1459 size_t len = strlen (fs->windows_systemroot) + 32;
1461 snprintf (cmd_exe, len, "%s/system32/cmd.exe", fs->windows_systemroot);
1463 char *cmd_exe_path = case_sensitive_path_silently (g, cmd_exe);
1467 char *arch = guestfs_file_architecture (g, cmd_exe_path);
1468 free (cmd_exe_path);
1471 fs->arch = arch; /* freed by guestfs___free_inspect_info */
1476 /* At the moment, pull just the ProductName and version numbers from
1477 * the registry. In future there is a case for making many more
1478 * registry fields available to callers.
1481 check_windows_software_registry (guestfs_h *g, struct inspect_fs *fs)
1483 TMP_TEMPLATE_ON_STACK (software_local);
1485 size_t len = strlen (fs->windows_systemroot) + 64;
1487 snprintf (software, len, "%s/system32/config/software",
1488 fs->windows_systemroot);
1490 char *software_path = case_sensitive_path_silently (g, software);
1492 /* If the software hive doesn't exist, just accept that we cannot
1493 * find product_name etc.
1499 hive_value_h *values = NULL;
1501 if (download_to_tmp (g, software_path, software_local,
1502 MAX_REGISTRY_SIZE) == -1)
1505 h = hivex_open (software_local, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
1507 perrorf (g, "hivex_open");
1511 hive_node_h node = hivex_root (h);
1512 const char *hivepath[] =
1513 { "Microsoft", "Windows NT", "CurrentVersion" };
1516 node != 0 && i < sizeof hivepath / sizeof hivepath[0];
1518 node = hivex_node_get_child (h, node, hivepath[i]);
1522 perrorf (g, "hivex: cannot locate HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
1526 values = hivex_node_values (h, node);
1528 for (i = 0; values[i] != 0; ++i) {
1529 char *key = hivex_value_key (h, values[i]);
1531 perrorf (g, "hivex_value_key");
1535 if (STRCASEEQ (key, "ProductName")) {
1536 fs->product_name = hivex_value_string (h, values[i]);
1537 if (!fs->product_name) {
1538 perrorf (g, "hivex_value_string");
1543 else if (STRCASEEQ (key, "CurrentVersion")) {
1544 char *version = hivex_value_string (h, values[i]);
1546 perrorf (g, "hivex_value_string");
1550 char *major, *minor;
1551 if (match2 (g, version, re_windows_version, &major, &minor)) {
1552 fs->major_version = parse_unsigned_int (g, major);
1554 if (fs->major_version == -1) {
1560 fs->minor_version = parse_unsigned_int (g, minor);
1562 if (fs->minor_version == -1) {
1571 else if (STRCASEEQ (key, "InstallationType")) {
1572 fs->product_variant = hivex_value_string (h, values[i]);
1573 if (!fs->product_variant) {
1574 perrorf (g, "hivex_value_string");
1586 if (h) hivex_close (h);
1588 free (software_path);
1590 /* Free up the temporary file. */
1591 unlink (software_local);
1592 #undef software_local_len
1598 check_windows_system_registry (guestfs_h *g, struct inspect_fs *fs)
1600 TMP_TEMPLATE_ON_STACK (system_local);
1602 size_t len = strlen (fs->windows_systemroot) + 64;
1604 snprintf (system, len, "%s/system32/config/system",
1605 fs->windows_systemroot);
1607 char *system_path = case_sensitive_path_silently (g, system);
1609 /* If the system hive doesn't exist, just accept that we cannot
1610 * find hostname etc.
1616 hive_node_h root, node;
1617 hive_value_h value, *values = NULL;
1621 if (download_to_tmp (g, system_path, system_local, MAX_REGISTRY_SIZE) == -1)
1624 h = hivex_open (system_local, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
1626 perrorf (g, "hivex_open");
1630 root = hivex_root (h);
1632 perrorf (g, "hivex_root");
1636 /* Get the CurrentControlSet. */
1638 node = hivex_node_get_child (h, root, "Select");
1641 perrorf (g, "hivex_node_get_child");
1643 error (g, "hivex: could not locate HKLM\\SYSTEM\\Select");
1648 value = hivex_node_get_value (h, node, "Current");
1651 perrorf (g, "hivex_node_get_value");
1653 error (g, "hivex: HKLM\\System\\Select Default entry not found.");
1657 /* XXX Should check the type. */
1658 dword = hivex_value_dword (h, value);
1659 fs->windows_current_control_set = safe_asprintf (g, "ControlSet%03d", dword);
1661 /* Get the hostname. */
1662 const char *hivepath[] =
1663 { fs->windows_current_control_set, "Services", "Tcpip", "Parameters" };
1664 for (node = root, i = 0;
1665 node != 0 && i < sizeof hivepath / sizeof hivepath[0];
1667 node = hivex_node_get_child (h, node, hivepath[i]);
1671 perrorf (g, "hivex: cannot locate HKLM\\SYSTEM\\%s\\Services\\Tcpip\\Parameters",
1672 fs->windows_current_control_set);
1676 values = hivex_node_values (h, node);
1678 for (i = 0; values[i] != 0; ++i) {
1679 char *key = hivex_value_key (h, values[i]);
1681 perrorf (g, "hivex_value_key");
1685 if (STRCASEEQ (key, "Hostname")) {
1686 fs->hostname = hivex_value_string (h, values[i]);
1687 if (!fs->hostname) {
1688 perrorf (g, "hivex_value_string");
1693 /* many other interesting fields here ... */
1701 if (h) hivex_close (h);
1705 /* Free up the temporary file. */
1706 unlink (system_local);
1707 #undef system_local_len
1713 case_sensitive_path_silently (guestfs_h *g, const char *path)
1715 guestfs_error_handler_cb old_error_cb = g->error_cb;
1717 char *ret = guestfs_case_sensitive_path (g, path);
1718 g->error_cb = old_error_cb;
1723 is_file_nocase (guestfs_h *g, const char *path)
1728 p = case_sensitive_path_silently (g, path);
1731 r = guestfs_is_file (g, p);
1737 is_dir_nocase (guestfs_h *g, const char *path)
1742 p = case_sensitive_path_silently (g, path);
1745 r = guestfs_is_dir (g, p);
1751 extend_fses (guestfs_h *g)
1753 size_t n = g->nr_fses + 1;
1754 struct inspect_fs *p;
1756 p = realloc (g->fses, n * sizeof (struct inspect_fs));
1758 perrorf (g, "realloc");
1765 memset (&g->fses[n-1], 0, sizeof (struct inspect_fs));
1770 /* Parse small, unsigned ints, as used in version numbers. */
1772 parse_unsigned_int (guestfs_h *g, const char *str)
1775 int r = xstrtol (str, NULL, 10, &ret, "");
1776 if (r != LONGINT_OK) {
1777 error (g, _("could not parse integer in version number: %s"), str);
1783 /* Like parse_unsigned_int, but ignore trailing stuff. */
1785 parse_unsigned_int_ignore_trailing (guestfs_h *g, const char *str)
1788 int r = xstrtol (str, NULL, 10, &ret, NULL);
1789 if (r != LONGINT_OK) {
1790 error (g, _("could not parse integer in version number: %s"), str);
1796 /* At the moment, package format and package management is just a
1797 * simple function of the distro and major_version fields, so these
1798 * can never return an error. We might be cleverer in future.
1801 check_package_format (guestfs_h *g, struct inspect_fs *fs)
1803 switch (fs->distro) {
1804 case OS_DISTRO_FEDORA:
1805 case OS_DISTRO_MEEGO:
1806 case OS_DISTRO_REDHAT_BASED:
1807 case OS_DISTRO_RHEL:
1808 case OS_DISTRO_MANDRIVA:
1809 fs->package_format = OS_PACKAGE_FORMAT_RPM;
1812 case OS_DISTRO_DEBIAN:
1813 case OS_DISTRO_UBUNTU:
1814 case OS_DISTRO_LINUX_MINT:
1815 fs->package_format = OS_PACKAGE_FORMAT_DEB;
1818 case OS_DISTRO_ARCHLINUX:
1819 fs->package_format = OS_PACKAGE_FORMAT_PACMAN;
1821 case OS_DISTRO_GENTOO:
1822 fs->package_format = OS_PACKAGE_FORMAT_EBUILD;
1824 case OS_DISTRO_PARDUS:
1825 fs->package_format = OS_PACKAGE_FORMAT_PISI;
1828 case OS_DISTRO_SLACKWARE:
1829 case OS_DISTRO_WINDOWS:
1830 case OS_DISTRO_UNKNOWN:
1832 fs->package_format = OS_PACKAGE_FORMAT_UNKNOWN;
1838 check_package_management (guestfs_h *g, struct inspect_fs *fs)
1840 switch (fs->distro) {
1841 case OS_DISTRO_FEDORA:
1842 case OS_DISTRO_MEEGO:
1843 fs->package_management = OS_PACKAGE_MANAGEMENT_YUM;
1846 case OS_DISTRO_REDHAT_BASED:
1847 case OS_DISTRO_RHEL:
1848 if (fs->major_version >= 5)
1849 fs->package_management = OS_PACKAGE_MANAGEMENT_YUM;
1851 fs->package_management = OS_PACKAGE_MANAGEMENT_UP2DATE;
1854 case OS_DISTRO_DEBIAN:
1855 case OS_DISTRO_UBUNTU:
1856 case OS_DISTRO_LINUX_MINT:
1857 fs->package_management = OS_PACKAGE_MANAGEMENT_APT;
1860 case OS_DISTRO_ARCHLINUX:
1861 fs->package_management = OS_PACKAGE_MANAGEMENT_PACMAN;
1863 case OS_DISTRO_GENTOO:
1864 fs->package_management = OS_PACKAGE_MANAGEMENT_PORTAGE;
1866 case OS_DISTRO_PARDUS:
1867 fs->package_management = OS_PACKAGE_MANAGEMENT_PISI;
1869 case OS_DISTRO_MANDRIVA:
1870 fs->package_management = OS_PACKAGE_MANAGEMENT_URPMI;
1873 case OS_DISTRO_SLACKWARE:
1874 case OS_DISTRO_WINDOWS:
1875 case OS_DISTRO_UNKNOWN:
1877 fs->package_management = OS_PACKAGE_MANAGEMENT_UNKNOWN;
1882 static struct inspect_fs *
1883 search_for_root (guestfs_h *g, const char *root)
1885 if (g->nr_fses == 0) {
1886 error (g, _("no inspection data: call guestfs_inspect_os first"));
1891 struct inspect_fs *fs;
1892 for (i = 0; i < g->nr_fses; ++i) {
1894 if (fs->is_root && STREQ (root, fs->device))
1898 error (g, _("%s: root device not found: only call this function with a root device previously returned by guestfs_inspect_os"),
1904 guestfs__inspect_get_roots (guestfs_h *g)
1906 /* NB. Doesn't matter if g->nr_fses == 0. We just return an empty
1907 * list in this case.
1912 for (i = 0; i < g->nr_fses; ++i)
1913 if (g->fses[i].is_root)
1916 char **ret = calloc (count+1, sizeof (char *));
1918 perrorf (g, "calloc");
1923 for (i = 0; i < g->nr_fses; ++i) {
1924 if (g->fses[i].is_root) {
1925 ret[count] = safe_strdup (g, g->fses[i].device);
1935 guestfs__inspect_get_type (guestfs_h *g, const char *root)
1937 struct inspect_fs *fs = search_for_root (g, root);
1943 case OS_TYPE_LINUX: ret = safe_strdup (g, "linux"); break;
1944 case OS_TYPE_WINDOWS: ret = safe_strdup (g, "windows"); break;
1945 case OS_TYPE_FREEBSD: ret = safe_strdup (g, "freebsd"); break;
1946 case OS_TYPE_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1953 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
1955 struct inspect_fs *fs = search_for_root (g, root);
1959 return safe_strdup (g, fs->arch ? : "unknown");
1963 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
1965 struct inspect_fs *fs = search_for_root (g, root);
1970 switch (fs->distro) {
1971 case OS_DISTRO_ARCHLINUX: ret = safe_strdup (g, "archlinux"); break;
1972 case OS_DISTRO_DEBIAN: ret = safe_strdup (g, "debian"); break;
1973 case OS_DISTRO_FEDORA: ret = safe_strdup (g, "fedora"); break;
1974 case OS_DISTRO_GENTOO: ret = safe_strdup (g, "gentoo"); break;
1975 case OS_DISTRO_LINUX_MINT: ret = safe_strdup (g, "linuxmint"); break;
1976 case OS_DISTRO_MANDRIVA: ret = safe_strdup (g, "mandriva"); break;
1977 case OS_DISTRO_MEEGO: ret = safe_strdup (g, "meego"); break;
1978 case OS_DISTRO_PARDUS: ret = safe_strdup (g, "pardus"); break;
1979 case OS_DISTRO_REDHAT_BASED: ret = safe_strdup (g, "redhat-based"); break;
1980 case OS_DISTRO_RHEL: ret = safe_strdup (g, "rhel"); break;
1981 case OS_DISTRO_SLACKWARE: ret = safe_strdup (g, "slackware"); break;
1982 case OS_DISTRO_WINDOWS: ret = safe_strdup (g, "windows"); break;
1983 case OS_DISTRO_UBUNTU: ret = safe_strdup (g, "ubuntu"); break;
1984 case OS_DISTRO_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1991 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
1993 struct inspect_fs *fs = search_for_root (g, root);
1997 return fs->major_version;
2001 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
2003 struct inspect_fs *fs = search_for_root (g, root);
2007 return fs->minor_version;
2011 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
2013 struct inspect_fs *fs = search_for_root (g, root);
2017 return safe_strdup (g, fs->product_name ? : "unknown");
2021 guestfs__inspect_get_product_variant (guestfs_h *g, const char *root)
2023 struct inspect_fs *fs = search_for_root (g, root);
2027 return safe_strdup (g, fs->product_variant ? : "unknown");
2031 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
2033 struct inspect_fs *fs = search_for_root (g, root);
2037 if (!fs->windows_systemroot) {
2038 error (g, _("not a Windows guest, or systemroot could not be determined"));
2042 return safe_strdup (g, fs->windows_systemroot);
2046 guestfs__inspect_get_windows_current_control_set (guestfs_h *g,
2049 struct inspect_fs *fs = search_for_root (g, root);
2053 if (!fs->windows_current_control_set) {
2054 error (g, _("not a Windows guest, or CurrentControlSet could not be determined"));
2058 return safe_strdup (g, fs->windows_current_control_set);
2062 guestfs__inspect_get_format (guestfs_h *g, const char *root)
2064 struct inspect_fs *fs = search_for_root (g, root);
2069 switch (fs->format) {
2070 case OS_FORMAT_INSTALLED: ret = safe_strdup (g, "installed"); break;
2071 case OS_FORMAT_INSTALLER: ret = safe_strdup (g, "installer"); break;
2072 case OS_FORMAT_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
2079 guestfs__inspect_is_live (guestfs_h *g, const char *root)
2081 struct inspect_fs *fs = search_for_root (g, root);
2085 return fs->is_live_disk;
2089 guestfs__inspect_is_netinst (guestfs_h *g, const char *root)
2091 struct inspect_fs *fs = search_for_root (g, root);
2095 return fs->is_netinst_disk;
2099 guestfs__inspect_is_multipart (guestfs_h *g, const char *root)
2101 struct inspect_fs *fs = search_for_root (g, root);
2105 return fs->is_multipart_disk;
2109 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
2111 struct inspect_fs *fs = search_for_root (g, root);
2117 /* If no fstab information (Windows) return just the root. */
2118 if (fs->nr_fstab == 0) {
2119 ret = calloc (3, sizeof (char *));
2120 ret[0] = safe_strdup (g, "/");
2121 ret[1] = safe_strdup (g, root);
2126 #define CRITERION fs->fstab[i].mountpoint[0] == '/'
2127 size_t i, count = 0;
2128 for (i = 0; i < fs->nr_fstab; ++i)
2132 /* Hashtables have 2N+1 entries. */
2133 ret = calloc (2*count+1, sizeof (char *));
2135 perrorf (g, "calloc");
2140 for (i = 0; i < fs->nr_fstab; ++i)
2142 ret[2*count] = safe_strdup (g, fs->fstab[i].mountpoint);
2143 ret[2*count+1] = safe_strdup (g, fs->fstab[i].device);
2152 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
2154 struct inspect_fs *fs = search_for_root (g, root);
2160 /* If no fstab information (Windows) return just the root. */
2161 if (fs->nr_fstab == 0) {
2162 ret = calloc (2, sizeof (char *));
2163 ret[0] = safe_strdup (g, root);
2168 ret = calloc (fs->nr_fstab + 1, sizeof (char *));
2170 perrorf (g, "calloc");
2175 for (i = 0; i < fs->nr_fstab; ++i)
2176 ret[i] = safe_strdup (g, fs->fstab[i].device);
2182 guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
2184 struct inspect_fs *fs = search_for_root (g, root);
2189 switch (fs->package_format) {
2190 case OS_PACKAGE_FORMAT_RPM: ret = safe_strdup (g, "rpm"); break;
2191 case OS_PACKAGE_FORMAT_DEB: ret = safe_strdup (g, "deb"); break;
2192 case OS_PACKAGE_FORMAT_PACMAN: ret = safe_strdup (g, "pacman"); break;
2193 case OS_PACKAGE_FORMAT_EBUILD: ret = safe_strdup (g, "ebuild"); break;
2194 case OS_PACKAGE_FORMAT_PISI: ret = safe_strdup (g, "pisi"); break;
2195 case OS_PACKAGE_FORMAT_UNKNOWN:
2197 ret = safe_strdup (g, "unknown");
2205 guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
2207 struct inspect_fs *fs = search_for_root (g, root);
2212 switch (fs->package_management) {
2213 case OS_PACKAGE_MANAGEMENT_YUM: ret = safe_strdup (g, "yum"); break;
2214 case OS_PACKAGE_MANAGEMENT_UP2DATE: ret = safe_strdup (g, "up2date"); break;
2215 case OS_PACKAGE_MANAGEMENT_APT: ret = safe_strdup (g, "apt"); break;
2216 case OS_PACKAGE_MANAGEMENT_PACMAN: ret = safe_strdup (g, "pacman"); break;
2217 case OS_PACKAGE_MANAGEMENT_PORTAGE: ret = safe_strdup (g, "portage"); break;
2218 case OS_PACKAGE_MANAGEMENT_PISI: ret = safe_strdup (g, "pisi"); break;
2219 case OS_PACKAGE_MANAGEMENT_URPMI: ret = safe_strdup (g, "urpmi"); break;
2220 case OS_PACKAGE_MANAGEMENT_UNKNOWN:
2222 ret = safe_strdup (g, "unknown");
2230 guestfs__inspect_get_hostname (guestfs_h *g, const char *root)
2232 struct inspect_fs *fs = search_for_root (g, root);
2236 return safe_strdup (g, fs->hostname ? : "unknown");
2240 static struct guestfs_application_list *list_applications_rpm (guestfs_h *g, struct inspect_fs *fs);
2242 static struct guestfs_application_list *list_applications_deb (guestfs_h *g, struct inspect_fs *fs);
2243 static struct guestfs_application_list *list_applications_windows (guestfs_h *g, struct inspect_fs *fs);
2244 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);
2245 static void sort_applications (struct guestfs_application_list *);
2247 /* Unlike the simple inspect-get-* calls, this one assumes that the
2248 * disks are mounted up, and reads files from the mounted disks.
2250 struct guestfs_application_list *
2251 guestfs__inspect_list_applications (guestfs_h *g, const char *root)
2253 struct inspect_fs *fs = search_for_root (g, root);
2257 struct guestfs_application_list *ret = NULL;
2259 /* Presently we can only list applications for installed disks. It
2260 * is possible in future to get lists of packages from installers.
2262 if (fs->format == OS_FORMAT_INSTALLED) {
2265 switch (fs->package_format) {
2266 case OS_PACKAGE_FORMAT_RPM:
2268 ret = list_applications_rpm (g, fs);
2274 case OS_PACKAGE_FORMAT_DEB:
2275 ret = list_applications_deb (g, fs);
2280 case OS_PACKAGE_FORMAT_PACMAN:
2281 case OS_PACKAGE_FORMAT_EBUILD:
2282 case OS_PACKAGE_FORMAT_PISI:
2283 case OS_PACKAGE_FORMAT_UNKNOWN:
2285 /* nothing - keep GCC happy */;
2289 case OS_TYPE_WINDOWS:
2290 ret = list_applications_windows (g, fs);
2295 case OS_TYPE_FREEBSD:
2296 case OS_TYPE_UNKNOWN:
2298 /* nothing - keep GCC happy */;
2303 /* Don't know how to do inspection. Not an error, return an
2306 ret = safe_malloc (g, sizeof *ret);
2311 sort_applications (ret);
2317 static struct guestfs_application_list *
2318 list_applications_rpm (guestfs_h *g, struct inspect_fs *fs)
2320 TMP_TEMPLATE_ON_STACK (tmpfile);
2322 if (download_to_tmp (g, "/var/lib/rpm/Name", tmpfile, MAX_PKG_DB_SIZE) == -1)
2325 struct guestfs_application_list *apps = NULL, *ret = NULL;
2326 #define cmd_len (strlen (tmpfile) + 64)
2332 snprintf (cmd, cmd_len, DB_DUMP " -p '%s'", tmpfile);
2334 debug (g, "list_applications_rpm: %s", cmd);
2336 pp = popen (cmd, "r");
2338 perrorf (g, "popen: %s", cmd);
2342 /* Ignore everything to end-of-header marker. */
2344 if (fgets (line, sizeof line, pp) == NULL) {
2345 error (g, _("unexpected end of output from db_dump command"));
2349 len = strlen (line);
2350 if (len > 0 && line[len-1] == '\n') {
2355 if (STREQ (line, "HEADER=END"))
2359 /* Allocate 'apps' list. */
2360 apps = safe_malloc (g, sizeof *apps);
2364 /* Read alternate lines until end of data marker. */
2366 if (fgets (line, sizeof line, pp) == NULL) {
2367 error (g, _("unexpected end of output from db_dump command"));
2371 len = strlen (line);
2372 if (len > 0 && line[len-1] == '\n') {
2377 if (STREQ (line, "DATA=END"))
2381 if (len > 0 && line[0] == ' ')
2383 /* Ignore any application name that contains non-printable chars.
2384 * In the db_dump output these would be escaped with backslash, so
2385 * we can just ignore any such line.
2387 if (strchr (p, '\\') == NULL)
2388 add_application (g, apps, p, "", 0, "", "", "", "", "", "");
2390 /* Discard next line. */
2391 if (fgets (line, sizeof line, pp) == NULL) {
2392 error (g, _("unexpected end of output from db_dump command"));
2397 /* Catch errors from the db_dump command. */
2398 if (pclose (pp) == -1) {
2399 perrorf (g, "pclose: %s", cmd);
2407 if (ret == NULL && apps != NULL)
2408 guestfs_free_application_list (apps);
2416 #endif /* defined DB_DUMP */
2418 static struct guestfs_application_list *
2419 list_applications_deb (guestfs_h *g, struct inspect_fs *fs)
2421 TMP_TEMPLATE_ON_STACK (tmpfile);
2423 if (download_to_tmp (g, "/var/lib/dpkg/status", tmpfile,
2424 MAX_PKG_DB_SIZE) == -1)
2427 struct guestfs_application_list *apps = NULL, *ret = NULL;
2431 char *name = NULL, *version = NULL, *release = NULL;
2432 int installed_flag = 0;
2434 fp = fopen (tmpfile, "r");
2436 perrorf (g, "fopen: %s", tmpfile);
2440 /* Allocate 'apps' list. */
2441 apps = safe_malloc (g, sizeof *apps);
2445 /* Read the temporary file. Each package entry is separated by
2447 * XXX Strictly speaking this is in mailbox header format, so it
2448 * would be possible for fields to spread across multiple lines,
2449 * although for the short fields that we are concerned about this is
2450 * unlikely and not seen in practice.
2452 while (fgets (line, sizeof line, fp) != NULL) {
2453 len = strlen (line);
2454 if (len > 0 && line[len-1] == '\n') {
2459 if (STRPREFIX (line, "Package: ")) {
2461 name = safe_strdup (g, &line[9]);
2463 else if (STRPREFIX (line, "Status: ")) {
2464 installed_flag = strstr (&line[8], "installed") != NULL;
2466 else if (STRPREFIX (line, "Version: ")) {
2469 char *p = strchr (&line[9], '-');
2472 version = safe_strdup (g, &line[9]);
2473 release = safe_strdup (g, p+1);
2475 version = safe_strdup (g, &line[9]);
2479 else if (STREQ (line, "")) {
2480 if (installed_flag && name && version)
2481 add_application (g, apps, name, "", 0, version, release ? : "",
2486 name = version = release = NULL;
2491 if (fclose (fp) == -1) {
2492 perrorf (g, "fclose: %s", tmpfile);
2500 if (ret == NULL && apps != NULL)
2501 guestfs_free_application_list (apps);
2511 static void list_applications_windows_from_path (guestfs_h *g, hive_h *h, struct guestfs_application_list *apps, const char **path, size_t path_len);
2513 static struct guestfs_application_list *
2514 list_applications_windows (guestfs_h *g, struct inspect_fs *fs)
2516 TMP_TEMPLATE_ON_STACK (software_local);
2518 /* XXX We already download the SOFTWARE hive when doing general
2519 * inspection. We could avoid this second download of the same file
2520 * by caching these entries in the handle.
2522 size_t len = strlen (fs->windows_systemroot) + 64;
2524 snprintf (software, len, "%s/system32/config/software",
2525 fs->windows_systemroot);
2527 char *software_path = case_sensitive_path_silently (g, software);
2529 /* If the software hive doesn't exist, just accept that we cannot
2530 * find product_name etc.
2534 struct guestfs_application_list *ret = NULL;
2537 if (download_to_tmp (g, software_path, software_local,
2538 MAX_REGISTRY_SIZE) == -1)
2541 free (software_path);
2542 software_path = NULL;
2544 h = hivex_open (software_local, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
2546 perrorf (g, "hivex_open");
2550 /* Allocate apps list. */
2551 ret = safe_malloc (g, sizeof *ret);
2555 /* Ordinary native applications. */
2556 const char *hivepath[] =
2557 { "Microsoft", "Windows", "CurrentVersion", "Uninstall" };
2558 list_applications_windows_from_path (g, h, ret, hivepath,
2559 sizeof hivepath / sizeof hivepath[0]);
2561 /* 32-bit emulated Windows apps running on the WOW64 emulator.
2562 * http://support.microsoft.com/kb/896459 (RHBZ#692545).
2564 const char *hivepath2[] =
2565 { "WOW6432node", "Microsoft", "Windows", "CurrentVersion", "Uninstall" };
2566 list_applications_windows_from_path (g, h, ret, hivepath2,
2567 sizeof hivepath2 / sizeof hivepath2[0]);
2570 if (h) hivex_close (h);
2571 free (software_path);
2573 /* Delete the temporary file. */
2574 unlink (software_local);
2575 #undef software_local_len
2581 list_applications_windows_from_path (guestfs_h *g, hive_h *h,
2582 struct guestfs_application_list *apps,
2583 const char **path, size_t path_len)
2585 hive_node_h *children = NULL;
2589 node = hivex_root (h);
2591 for (i = 0; node != 0 && i < path_len; ++i)
2592 node = hivex_node_get_child (h, node, path[i]);
2597 children = hivex_node_children (h, node);
2598 if (children == NULL)
2601 /* Consider any child node that has a DisplayName key.
2603 * http://nsis.sourceforge.net/Add_uninstall_information_to_Add/Remove_Programs#Optional_values
2605 for (i = 0; children[i] != 0; ++i) {
2608 char *display_name = NULL;
2609 char *version = NULL;
2610 char *install_path = NULL;
2611 char *publisher = NULL;
2613 char *comments = NULL;
2615 /* Use the node name as a proxy for the package name in Linux. The
2616 * display name is not language-independent, so it cannot be used.
2618 name = hivex_node_name (h, children[i]);
2622 value = hivex_node_get_value (h, children[i], "DisplayName");
2624 display_name = hivex_value_string (h, value);
2626 value = hivex_node_get_value (h, children[i], "DisplayVersion");
2628 version = hivex_value_string (h, value);
2629 value = hivex_node_get_value (h, children[i], "InstallLocation");
2631 install_path = hivex_value_string (h, value);
2632 value = hivex_node_get_value (h, children[i], "Publisher");
2634 publisher = hivex_value_string (h, value);
2635 value = hivex_node_get_value (h, children[i], "URLInfoAbout");
2637 url = hivex_value_string (h, value);
2638 value = hivex_node_get_value (h, children[i], "Comments");
2640 comments = hivex_value_string (h, value);
2642 add_application (g, apps, name, display_name, 0,
2645 install_path ? : "",
2653 free (display_name);
2655 free (install_path);
2665 add_application (guestfs_h *g, struct guestfs_application_list *apps,
2666 const char *name, const char *display_name, int32_t epoch,
2667 const char *version, const char *release,
2668 const char *install_path,
2669 const char *publisher, const char *url,
2670 const char *description)
2673 apps->val = safe_realloc (g, apps->val,
2674 apps->len * sizeof (struct guestfs_application));
2675 apps->val[apps->len-1].app_name = safe_strdup (g, name);
2676 apps->val[apps->len-1].app_display_name = safe_strdup (g, display_name);
2677 apps->val[apps->len-1].app_epoch = epoch;
2678 apps->val[apps->len-1].app_version = safe_strdup (g, version);
2679 apps->val[apps->len-1].app_release = safe_strdup (g, release);
2680 apps->val[apps->len-1].app_install_path = safe_strdup (g, install_path);
2681 /* XXX Translated path is not implemented yet. */
2682 apps->val[apps->len-1].app_trans_path = safe_strdup (g, "");
2683 apps->val[apps->len-1].app_publisher = safe_strdup (g, publisher);
2684 apps->val[apps->len-1].app_url = safe_strdup (g, url);
2685 /* XXX The next two are not yet implemented for any package
2686 * format, but we could easily support them for rpm and deb.
2688 apps->val[apps->len-1].app_source_package = safe_strdup (g, "");
2689 apps->val[apps->len-1].app_summary = safe_strdup (g, "");
2690 apps->val[apps->len-1].app_description = safe_strdup (g, description);
2693 /* Sort applications by name before returning the list. */
2695 compare_applications (const void *vp1, const void *vp2)
2697 const struct guestfs_application *v1 = vp1;
2698 const struct guestfs_application *v2 = vp2;
2700 return strcmp (v1->app_name, v2->app_name);
2704 sort_applications (struct guestfs_application_list *apps)
2706 if (apps && apps->val)
2707 qsort (apps->val, apps->len, sizeof (struct guestfs_application),
2708 compare_applications);
2711 /* Download to a guest file to a local temporary file. Refuse to
2712 * download the guest file if it is larger than max_size. The caller
2713 * is responsible for deleting the temporary file after use.
2716 download_to_tmp (guestfs_h *g, const char *filename,
2717 char *localtmp, int64_t max_size)
2723 size = guestfs_filesize (g, filename);
2725 /* guestfs_filesize failed and has already set error in handle */
2727 if (size > max_size) {
2728 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
2733 fd = mkstemp (localtmp);
2735 perrorf (g, "mkstemp");
2739 snprintf (buf, sizeof buf, "/dev/fd/%d", fd);
2741 if (guestfs_download (g, filename, buf) == -1) {
2747 if (close (fd) == -1) {
2748 perrorf (g, "close: %s", localtmp);
2756 /* Call 'f' with Augeas opened and having parsed 'filename' (this file
2757 * must exist). As a security measure, this bails if the file is too
2758 * large for a reasonable configuration file. After the call to 'f'
2762 inspect_with_augeas (guestfs_h *g, struct inspect_fs *fs, const char *filename,
2763 int (*f) (guestfs_h *, struct inspect_fs *))
2765 /* Security: Refuse to do this if filename is too large. */
2766 int64_t size = guestfs_filesize (g, filename);
2768 /* guestfs_filesize failed and has already set error in handle */
2770 if (size > MAX_AUGEAS_FILE_SIZE) {
2771 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
2776 /* If !feature_available (g, "augeas") then the next call will fail.
2777 * Arguably we might want to fall back to a non-Augeas method in
2780 if (guestfs_aug_init (g, "/", 16|32) == -1)
2785 /* Tell Augeas to only load one file (thanks Raphaël Pinson). */
2786 char buf[strlen (filename) + 64];
2787 snprintf (buf, strlen (filename) + 64, "/augeas/load//incl[. != \"%s\"]",
2789 if (guestfs_aug_rm (g, buf) == -1)
2792 if (guestfs_aug_load (g) == -1)
2798 guestfs_aug_close (g);
2803 /* Get the first line of a small file, without any trailing newline
2807 first_line_of_file (guestfs_h *g, const char *filename)
2813 /* Don't trust guestfs_head_n not to break with very large files.
2814 * Check the file size is something reasonable first.
2816 size = guestfs_filesize (g, filename);
2818 /* guestfs_filesize failed and has already set error in handle */
2820 if (size > MAX_SMALL_FILE_SIZE) {
2821 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
2826 lines = guestfs_head_n (g, 1, filename);
2829 if (lines[0] == NULL) {
2830 error (g, _("%s: file is empty"), filename);
2831 guestfs___free_string_list (lines);
2834 /* lines[1] should be NULL because of '1' argument above ... */
2836 ret = lines[0]; /* caller frees */
2837 free (lines); /* free the array */
2842 /* Get the first matching line (using guestfs_egrep{,i}) of a small file,
2843 * without any trailing newline character.
2845 * Returns: 1 = returned a line (in *ret)
2850 first_egrep_of_file (guestfs_h *g, const char *filename,
2851 const char *eregex, int iflag, char **ret)
2857 /* Don't trust guestfs_egrep not to break with very large files.
2858 * Check the file size is something reasonable first.
2860 size = guestfs_filesize (g, filename);
2862 /* guestfs_filesize failed and has already set error in handle */
2864 if (size > MAX_SMALL_FILE_SIZE) {
2865 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
2870 lines = (!iflag ? guestfs_egrep : guestfs_egrepi) (g, eregex, filename);
2873 if (lines[0] == NULL) {
2874 guestfs___free_string_list (lines);
2878 *ret = lines[0]; /* caller frees */
2880 /* free up any other matches and the array itself */
2881 for (i = 1; lines[i] != NULL; ++i)
2888 #else /* no PCRE or hivex at compile time */
2890 /* XXX These functions should be in an optgroup. */
2892 #define NOT_IMPL(r) \
2893 error (g, _("inspection API not available since this version of libguestfs was compiled without PCRE or hivex libraries")); \
2897 guestfs__inspect_os (guestfs_h *g)
2903 guestfs__inspect_get_roots (guestfs_h *g)
2909 guestfs__inspect_get_type (guestfs_h *g, const char *root)
2915 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
2921 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
2927 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
2933 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
2939 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
2945 guestfs__inspect_get_product_variant (guestfs_h *g, const char *root)
2951 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
2957 guestfs__inspect_get_windows_current_control_set (guestfs_h *g,
2964 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
2970 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
2976 guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
2982 guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
2988 guestfs__inspect_get_hostname (guestfs_h *g, const char *root)
2993 struct guestfs_application_list *
2994 guestfs__inspect_list_applications (guestfs_h *g, const char *root)
3000 guestfs__inspect_get_format (guestfs_h *g, const char *root)
3006 guestfs__inspect_is_live (guestfs_h *g, const char *root)
3012 guestfs__inspect_is_netinst (guestfs_h *g, const char *root)
3018 guestfs__inspect_is_multipart (guestfs_h *g, const char *root)
3023 #endif /* no PCRE or hivex at compile time */
3026 guestfs___free_inspect_info (guestfs_h *g)
3029 for (i = 0; i < g->nr_fses; ++i) {
3030 free (g->fses[i].device);
3031 free (g->fses[i].product_name);
3032 free (g->fses[i].product_variant);
3033 free (g->fses[i].arch);
3034 free (g->fses[i].hostname);
3035 free (g->fses[i].windows_systemroot);
3036 free (g->fses[i].windows_current_control_set);
3038 for (j = 0; j < g->fses[i].nr_fstab; ++j) {
3039 free (g->fses[i].fstab[j].device);
3040 free (g->fses[i].fstab[j].mountpoint);
3042 free (g->fses[i].fstab);
3049 /* In the Perl code this is a public function. */
3051 guestfs___feature_available (guestfs_h *g, const char *feature)
3053 /* If there's an error we should ignore it, so to do that we have to
3054 * temporarily replace the error handler with a null one.
3056 guestfs_error_handler_cb old_error_cb = g->error_cb;
3059 const char *groups[] = { feature, NULL };
3060 int r = guestfs_available (g, (char * const *) groups);
3062 g->error_cb = old_error_cb;
3064 return r == 0 ? 1 : 0;
3069 /* Match a regular expression which contains no captures. Returns
3070 * true if it matches or false if it doesn't.
3073 guestfs___match (guestfs_h *g, const char *str, const pcre *re)
3075 size_t len = strlen (str);
3078 r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
3079 if (r == PCRE_ERROR_NOMATCH)
3082 /* Internal error -- should not happen. */
3083 warning (g, "%s: %s: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
3084 __FILE__, __func__, r, str);
3091 /* Match a regular expression which contains exactly one capture. If
3092 * the string matches, return the capture, otherwise return NULL. The
3093 * caller must free the result.
3096 guestfs___match1 (guestfs_h *g, const char *str, const pcre *re)
3098 size_t len = strlen (str);
3101 r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
3102 if (r == PCRE_ERROR_NOMATCH)
3105 /* Internal error -- should not happen. */
3106 warning (g, "%s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"",
3107 __FILE__, __func__, r, str);
3111 return safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
3114 /* Match a regular expression which contains exactly two captures. */
3116 guestfs___match2 (guestfs_h *g, const char *str, const pcre *re,
3117 char **ret1, char **ret2)
3119 size_t len = strlen (str);
3122 r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
3123 if (r == PCRE_ERROR_NOMATCH)
3126 /* Internal error -- should not happen. */
3127 warning (g, "%s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"",
3128 __FILE__, __func__, r, str);
3132 *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
3133 *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
3138 /* Match a regular expression which contains exactly three captures. */
3140 guestfs___match3 (guestfs_h *g, const char *str, const pcre *re,
3141 char **ret1, char **ret2, char **ret3)
3143 size_t len = strlen (str);
3146 r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
3147 if (r == PCRE_ERROR_NOMATCH)
3150 /* Internal error -- should not happen. */
3151 warning (g, "%s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"",
3152 __FILE__, __func__, r, str);
3156 *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
3157 *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
3158 *ret3 = safe_strndup (g, &str[vec[6]], vec[7]-vec[6]);
3163 #endif /* HAVE_PCRE */