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 /* Some limits on what we will read, for safety. */
50 /* Small text configuration files.
52 * The upper limit is for general files that we grep or download. The
53 * largest such file is probably "txtsetup.sif" from Windows CDs
54 * (~500K). This number has to be larger than any legitimate file and
55 * smaller than the protocol message size.
57 * The lower limit is for files parsed by Augeas on the daemon side,
58 * where Augeas is running in reduced memory and can potentially
59 * create a lot of metadata so we really need to be careful about
62 #define MAX_SMALL_FILE_SIZE (2 * 1000 * 1000)
63 #define MAX_AUGEAS_FILE_SIZE (100 * 1000)
65 /* Maximum Windows Registry hive that we will download to /tmp. Some
66 * registries can be legitimately very large.
68 #define MAX_REGISTRY_SIZE (100 * 1000 * 1000)
70 /* Maximum RPM or dpkg database we will download to /tmp. */
71 #define MAX_PKG_DB_SIZE (10 * 1000 * 1000)
73 /* Compile all the regular expressions once when the shared library is
74 * loaded. PCRE is thread safe so we're supposedly OK here if
75 * multiple threads call into the libguestfs API functions below
78 static pcre *re_fedora;
79 static pcre *re_rhel_old;
81 static pcre *re_rhel_no_minor;
82 static pcre *re_major_minor;
83 static pcre *re_aug_seq;
85 static pcre *re_first_partition;
86 static pcre *re_freebsd;
87 static pcre *re_windows_version;
89 static void compile_regexps (void) __attribute__((constructor));
90 static void free_regexps (void) __attribute__((destructor));
93 compile_regexps (void)
98 #define COMPILE(re,pattern,options) \
100 re = pcre_compile ((pattern), (options), &err, &offset, NULL); \
102 ignore_value (write (2, err, strlen (err))); \
107 COMPILE (re_fedora, "Fedora release (\\d+)", 0);
108 COMPILE (re_rhel_old,
109 "(?:Red Hat|CentOS|Scientific Linux).*release (\\d+).*Update (\\d+)", 0);
111 "(?:Red Hat|CentOS|Scientific Linux).*release (\\d+)\\.(\\d+)", 0);
112 COMPILE (re_rhel_no_minor,
113 "(?:Red Hat|CentOS|Scientific Linux).*release (\\d+)", 0);
114 COMPILE (re_major_minor, "(\\d+)\\.(\\d+)", 0);
115 COMPILE (re_aug_seq, "/\\d+$", 0);
116 COMPILE (re_xdev, "^/dev/(?:h|s|v|xv)d([a-z]\\d*)$", 0);
117 COMPILE (re_first_partition, "^/dev/(?:h|s|v)d.1$", 0);
118 COMPILE (re_freebsd, "^/dev/ad(\\d+)s(\\d+)([a-z])$", 0);
119 COMPILE (re_windows_version, "^(\\d+)\\.(\\d+)", 0);
125 pcre_free (re_fedora);
126 pcre_free (re_rhel_old);
128 pcre_free (re_rhel_no_minor);
129 pcre_free (re_major_minor);
130 pcre_free (re_aug_seq);
132 pcre_free (re_first_partition);
133 pcre_free (re_freebsd);
134 pcre_free (re_windows_version);
137 /* The main inspection code. */
138 static int check_for_filesystem_on (guestfs_h *g, const char *device, int is_block, int is_partnum);
141 guestfs__inspect_os (guestfs_h *g)
143 /* Remove any information previously stored in the handle. */
144 guestfs___free_inspect_info (g);
146 if (guestfs_umount_all (g) == -1)
149 /* Iterate over all possible devices. Try to mount each
150 * (read-only). Examine ones which contain filesystems and add that
151 * information to the handle.
153 /* Look to see if any devices directly contain filesystems (RHBZ#590167). */
155 devices = guestfs_list_devices (g);
160 for (i = 0; devices[i] != NULL; ++i) {
161 if (check_for_filesystem_on (g, devices[i], 1, 0) == -1) {
162 guestfs___free_string_list (devices);
163 guestfs___free_inspect_info (g);
167 guestfs___free_string_list (devices);
169 /* Look at all partitions. */
171 partitions = guestfs_list_partitions (g);
172 if (partitions == NULL) {
173 guestfs___free_inspect_info (g);
177 for (i = 0; partitions[i] != NULL; ++i) {
178 if (check_for_filesystem_on (g, partitions[i], 0, i+1) == -1) {
179 guestfs___free_string_list (partitions);
180 guestfs___free_inspect_info (g);
184 guestfs___free_string_list (partitions);
186 /* Look at all LVs. */
187 if (guestfs___feature_available (g, "lvm2")) {
189 lvs = guestfs_lvs (g);
191 guestfs___free_inspect_info (g);
195 for (i = 0; lvs[i] != NULL; ++i) {
196 if (check_for_filesystem_on (g, lvs[i], 0, 0) == -1) {
197 guestfs___free_string_list (lvs);
198 guestfs___free_inspect_info (g);
202 guestfs___free_string_list (lvs);
205 /* At this point we have, in the handle, a list of all filesystems
206 * found and data about each one. Now we assemble the list of
207 * filesystems which are root devices and return that to the user.
208 * Fall through to guestfs__inspect_get_roots to do that.
210 char **ret = guestfs__inspect_get_roots (g);
212 guestfs___free_inspect_info (g);
216 /* Find out if 'device' contains a filesystem. If it does, add
217 * another entry in g->fses.
219 static int check_filesystem (guestfs_h *g, const char *device, int is_block, int is_partnum);
220 static int check_linux_root (guestfs_h *g, struct inspect_fs *fs);
221 static int check_freebsd_root (guestfs_h *g, struct inspect_fs *fs);
222 static int check_installer_root (guestfs_h *g, struct inspect_fs *fs);
223 static void check_architecture (guestfs_h *g, struct inspect_fs *fs);
224 static int check_hostname_unix (guestfs_h *g, struct inspect_fs *fs);
225 static int check_hostname_redhat (guestfs_h *g, struct inspect_fs *fs);
226 static int check_hostname_freebsd (guestfs_h *g, struct inspect_fs *fs);
227 static int check_fstab (guestfs_h *g, struct inspect_fs *fs);
228 static int check_windows_root (guestfs_h *g, struct inspect_fs *fs);
229 static int check_windows_arch (guestfs_h *g, struct inspect_fs *fs);
230 static int check_windows_software_registry (guestfs_h *g, struct inspect_fs *fs);
231 static int check_windows_system_registry (guestfs_h *g, struct inspect_fs *fs);
232 static char *resolve_windows_path_silently (guestfs_h *g, const char *);
233 static int extend_fses (guestfs_h *g);
234 static int parse_unsigned_int (guestfs_h *g, const char *str);
235 static int parse_unsigned_int_ignore_trailing (guestfs_h *g, const char *str);
236 static int add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
237 const char *spec, const char *mp);
238 static char *resolve_fstab_device (guestfs_h *g, const char *spec);
239 static void check_package_format (guestfs_h *g, struct inspect_fs *fs);
240 static void check_package_management (guestfs_h *g, struct inspect_fs *fs);
241 static int download_to_tmp (guestfs_h *g, const char *filename, char *localtmp, int64_t max_size);
242 static int inspect_with_augeas (guestfs_h *g, struct inspect_fs *fs, const char *filename, int (*f) (guestfs_h *, struct inspect_fs *));
243 static char *first_line_of_file (guestfs_h *g, const char *filename);
244 static int first_egrep_of_file (guestfs_h *g, const char *filename, const char *eregex, int iflag, char **ret);
247 check_for_filesystem_on (guestfs_h *g, const char *device,
248 int is_block, int is_partnum)
250 /* Get vfs-type in order to check if it's a Linux(?) swap device.
251 * If there's an error we should ignore it, so to do that we have to
252 * temporarily replace the error handler with a null one.
254 guestfs_error_handler_cb old_error_cb = g->error_cb;
256 char *vfs_type = guestfs_vfs_type (g, device);
257 g->error_cb = old_error_cb;
259 int is_swap = vfs_type && STREQ (vfs_type, "swap");
261 debug (g, "check_for_filesystem_on: %s %d %d (%s)",
262 device, is_block, is_partnum,
263 vfs_type ? vfs_type : "failed to get vfs type");
267 if (extend_fses (g) == -1)
269 g->fses[g->nr_fses-1].is_swap = 1;
273 /* Try mounting the device. As above, ignore errors. */
275 int r = guestfs_mount_ro (g, device, "/");
276 if (r == -1 && vfs_type && STREQ (vfs_type, "ufs")) /* Hack for the *BSDs. */
277 r = guestfs_mount_vfs (g, "ro,ufstype=ufs2", "ufs", device, "/");
279 g->error_cb = old_error_cb;
283 /* Do the rest of the checks. */
284 r = check_filesystem (g, device, is_block, is_partnum);
286 /* Unmount the filesystem. */
287 if (guestfs_umount_all (g) == -1)
293 /* is_block and is_partnum are just hints: is_block is true if the
294 * filesystem is a whole block device (eg. /dev/sda). is_partnum
295 * is > 0 if the filesystem is a direct partition, and in this case
296 * it is the partition number counting from 1
297 * (eg. /dev/sda1 => is_partnum == 1).
300 check_filesystem (guestfs_h *g, const char *device,
301 int is_block, int is_partnum)
303 if (extend_fses (g) == -1)
306 struct inspect_fs *fs = &g->fses[g->nr_fses-1];
308 fs->device = safe_strdup (g, device);
309 fs->is_mountable = 1;
311 /* Optimize some of the tests by avoiding multiple tests of the same thing. */
312 int is_dir_etc = guestfs_is_dir (g, "/etc") > 0;
313 int is_dir_bin = guestfs_is_dir (g, "/bin") > 0;
314 int is_dir_share = guestfs_is_dir (g, "/share") > 0;
317 if (guestfs_is_file (g, "/grub/menu.lst") > 0 ||
318 guestfs_is_file (g, "/grub/grub.conf") > 0)
319 fs->content = FS_CONTENT_LINUX_BOOT;
321 else if (is_dir_etc &&
323 guestfs_is_file (g, "/etc/freebsd-update.conf") > 0 &&
324 guestfs_is_file (g, "/etc/fstab") > 0) {
325 /* Ignore /dev/sda1 which is a shadow of the real root filesystem
326 * that is probably /dev/sda5 (see:
327 * http://www.freebsd.org/doc/handbook/disk-organization.html)
329 if (match (g, device, re_first_partition))
333 fs->content = FS_CONTENT_FREEBSD_ROOT;
334 fs->format = OS_FORMAT_INSTALLED;
335 if (check_freebsd_root (g, fs) == -1)
339 else if (is_dir_etc &&
341 guestfs_is_file (g, "/etc/fstab") > 0) {
343 fs->content = FS_CONTENT_LINUX_ROOT;
344 fs->format = OS_FORMAT_INSTALLED;
345 if (check_linux_root (g, fs) == -1)
348 /* Linux /usr/local? */
349 else if (is_dir_etc &&
352 guestfs_exists (g, "/local") == 0 &&
353 guestfs_is_file (g, "/etc/fstab") == 0)
354 fs->content = FS_CONTENT_LINUX_USR_LOCAL;
356 else if (is_dir_etc &&
359 guestfs_exists (g, "/local") > 0 &&
360 guestfs_is_file (g, "/etc/fstab") == 0)
361 fs->content = FS_CONTENT_LINUX_USR;
363 else if (guestfs_is_dir (g, "/log") > 0 &&
364 guestfs_is_dir (g, "/run") > 0 &&
365 guestfs_is_dir (g, "/spool") > 0)
366 fs->content = FS_CONTENT_LINUX_VAR;
368 else if (guestfs_is_file (g, "/AUTOEXEC.BAT") > 0 ||
369 guestfs_is_file (g, "/autoexec.bat") > 0 ||
370 guestfs_is_dir (g, "/Program Files") > 0 ||
371 guestfs_is_dir (g, "/WINDOWS") > 0 ||
372 guestfs_is_dir (g, "/Windows") > 0 ||
373 guestfs_is_dir (g, "/windows") > 0 ||
374 guestfs_is_dir (g, "/WIN32") > 0 ||
375 guestfs_is_dir (g, "/Win32") > 0 ||
376 guestfs_is_dir (g, "/WINNT") > 0 ||
377 guestfs_is_file (g, "/boot.ini") > 0 ||
378 guestfs_is_file (g, "/ntldr") > 0) {
380 fs->content = FS_CONTENT_WINDOWS_ROOT;
381 fs->format = OS_FORMAT_INSTALLED;
382 if (check_windows_root (g, fs) == -1)
385 /* Install CD/disk? Skip these checks if it's not a whole device
386 * (eg. CD) or the first partition (eg. bootable USB key).
388 else if ((is_block || is_partnum == 1) &&
389 (guestfs_is_file (g, "/isolinux/isolinux.cfg") > 0 ||
390 guestfs_is_dir (g, "/EFI/BOOT") > 0 ||
391 guestfs_is_file (g, "/images/install.img") > 0 ||
392 guestfs_is_dir (g, "/.disk") > 0 ||
393 guestfs_is_file (g, "/.discinfo") > 0 ||
394 guestfs_is_file (g, "/i386/txtsetup.sif") > 0 ||
395 guestfs_is_file (g, "/amd64/txtsetup.sif")) > 0) {
397 fs->content = FS_CONTENT_INSTALLER;
398 fs->format = OS_FORMAT_INSTALLER;
399 if (check_installer_root (g, fs) == -1)
406 /* Set fs->product_name to the first line of the release file. */
408 parse_release_file (guestfs_h *g, struct inspect_fs *fs,
409 const char *release_filename)
411 fs->product_name = first_line_of_file (g, release_filename);
412 if (fs->product_name == NULL)
417 /* Parse generic MAJOR.MINOR from the fs->product_name string. */
419 parse_major_minor (guestfs_h *g, struct inspect_fs *fs)
423 if (match2 (g, fs->product_name, re_major_minor, &major, &minor)) {
424 fs->major_version = parse_unsigned_int (g, major);
426 if (fs->major_version == -1) {
430 fs->minor_version = parse_unsigned_int (g, minor);
432 if (fs->minor_version == -1)
438 /* Ubuntu has /etc/lsb-release containing:
439 * DISTRIB_ID=Ubuntu # Distro
440 * DISTRIB_RELEASE=10.04 # Version
441 * DISTRIB_CODENAME=lucid
442 * DISTRIB_DESCRIPTION="Ubuntu 10.04.1 LTS" # Product name
444 * [Ubuntu-derived ...] Linux Mint was found to have this:
445 * DISTRIB_ID=LinuxMint
447 * DISTRIB_CODENAME=julia
448 * DISTRIB_DESCRIPTION="Linux Mint 10 Julia"
449 * Linux Mint also has /etc/linuxmint/info with more information,
450 * but we can use the LSB file.
453 * LSB_VERSION=lsb-4.0-amd64:lsb-4.0-noarch
454 * DISTRIB_ID=MandrivaLinux
455 * DISTRIB_RELEASE=2010.1
456 * DISTRIB_CODENAME=Henry_Farman
457 * DISTRIB_DESCRIPTION="Mandriva Linux 2010.1"
458 * Mandriva also has a normal release file called /etc/mandriva-release.
461 parse_lsb_release (guestfs_h *g, struct inspect_fs *fs)
463 const char *filename = "/etc/lsb-release";
469 /* Don't trust guestfs_head_n not to break with very large files.
470 * Check the file size is something reasonable first.
472 size = guestfs_filesize (g, filename);
474 /* guestfs_filesize failed and has already set error in handle */
476 if (size > MAX_SMALL_FILE_SIZE) {
477 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
482 lines = guestfs_head_n (g, 10, filename);
486 for (i = 0; lines[i] != NULL; ++i) {
487 if (fs->distro == 0 &&
488 STREQ (lines[i], "DISTRIB_ID=Ubuntu")) {
489 fs->distro = OS_DISTRO_UBUNTU;
492 else if (fs->distro == 0 &&
493 STREQ (lines[i], "DISTRIB_ID=LinuxMint")) {
494 fs->distro = OS_DISTRO_LINUX_MINT;
497 else if (fs->distro == 0 &&
498 STREQ (lines[i], "DISTRIB_ID=MandrivaLinux")) {
499 fs->distro = OS_DISTRO_MANDRIVA;
502 else if (STRPREFIX (lines[i], "DISTRIB_RELEASE=")) {
504 if (match2 (g, &lines[i][16], re_major_minor, &major, &minor)) {
505 fs->major_version = parse_unsigned_int (g, major);
507 if (fs->major_version == -1) {
509 guestfs___free_string_list (lines);
512 fs->minor_version = parse_unsigned_int (g, minor);
514 if (fs->minor_version == -1) {
515 guestfs___free_string_list (lines);
520 else if (fs->product_name == NULL &&
521 (STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=\"") ||
522 STRPREFIX (lines[i], "DISTRIB_DESCRIPTION='"))) {
523 size_t len = strlen (lines[i]) - 21 - 1;
524 fs->product_name = safe_strndup (g, &lines[i][21], len);
527 else if (fs->product_name == NULL &&
528 STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=")) {
529 size_t len = strlen (lines[i]) - 20;
530 fs->product_name = safe_strndup (g, &lines[i][20], len);
535 guestfs___free_string_list (lines);
539 /* The currently mounted device is known to be a Linux root. Try to
540 * determine from this the distro, version, etc. Also parse
541 * /etc/fstab to determine the arrangement of mountpoints and
542 * associated devices.
545 check_linux_root (guestfs_h *g, struct inspect_fs *fs)
549 fs->type = OS_TYPE_LINUX;
551 if (guestfs_exists (g, "/etc/lsb-release") > 0) {
552 r = parse_lsb_release (g, fs);
553 if (r == -1) /* error */
555 if (r == 1) /* ok - detected the release from this file */
556 goto skip_release_checks;
559 if (guestfs_exists (g, "/etc/redhat-release") > 0) {
560 fs->distro = OS_DISTRO_REDHAT_BASED; /* Something generic Red Hat-like. */
562 if (parse_release_file (g, fs, "/etc/redhat-release") == -1)
566 if ((major = match1 (g, fs->product_name, re_fedora)) != NULL) {
567 fs->distro = OS_DISTRO_FEDORA;
568 fs->major_version = parse_unsigned_int (g, major);
570 if (fs->major_version == -1)
573 else if (match2 (g, fs->product_name, re_rhel_old, &major, &minor) ||
574 match2 (g, fs->product_name, re_rhel, &major, &minor)) {
575 fs->distro = OS_DISTRO_RHEL;
576 fs->major_version = parse_unsigned_int (g, major);
578 if (fs->major_version == -1) {
582 fs->minor_version = parse_unsigned_int (g, minor);
584 if (fs->minor_version == -1)
587 else if ((major = match1 (g, fs->product_name, re_rhel_no_minor)) != NULL) {
588 fs->distro = OS_DISTRO_RHEL;
589 fs->major_version = parse_unsigned_int (g, major);
591 if (fs->major_version == -1)
593 fs->minor_version = 0;
596 else if (guestfs_exists (g, "/etc/debian_version") > 0) {
597 fs->distro = OS_DISTRO_DEBIAN;
599 if (parse_release_file (g, fs, "/etc/debian_version") == -1)
602 if (parse_major_minor (g, fs) == -1)
605 else if (guestfs_exists (g, "/etc/pardus-release") > 0) {
606 fs->distro = OS_DISTRO_PARDUS;
608 if (parse_release_file (g, fs, "/etc/pardus-release") == -1)
611 if (parse_major_minor (g, fs) == -1)
614 else if (guestfs_exists (g, "/etc/arch-release") > 0) {
615 fs->distro = OS_DISTRO_ARCHLINUX;
617 /* /etc/arch-release file is empty and I can't see a way to
618 * determine the actual release or product string.
621 else if (guestfs_exists (g, "/etc/gentoo-release") > 0) {
622 fs->distro = OS_DISTRO_GENTOO;
624 if (parse_release_file (g, fs, "/etc/gentoo-release") == -1)
627 if (parse_major_minor (g, fs) == -1)
630 else if (guestfs_exists (g, "/etc/meego-release") > 0) {
631 fs->distro = OS_DISTRO_MEEGO;
633 if (parse_release_file (g, fs, "/etc/meego-release") == -1)
636 if (parse_major_minor (g, fs) == -1)
640 skip_release_checks:;
642 /* If distro test above was successful, work out the package format. */
643 check_package_format (g, fs);
644 check_package_management (g, fs);
646 /* Determine the architecture. */
647 check_architecture (g, fs);
649 /* We already know /etc/fstab exists because it's part of the test
650 * for Linux root above. We must now parse this file to determine
651 * which filesystems are used by the operating system and how they
654 if (inspect_with_augeas (g, fs, "/etc/fstab", check_fstab) == -1)
657 /* Determine hostname. */
658 if (check_hostname_unix (g, fs) == -1)
664 /* The currently mounted device is known to be a FreeBSD root. */
666 check_freebsd_root (guestfs_h *g, struct inspect_fs *fs)
668 fs->type = OS_TYPE_FREEBSD;
670 /* FreeBSD has no authoritative version file. The version number is
671 * in /etc/motd, which the system administrator might edit, but
672 * we'll use that anyway.
675 if (guestfs_exists (g, "/etc/motd") > 0) {
676 if (parse_release_file (g, fs, "/etc/motd") == -1)
679 if (parse_major_minor (g, fs) == -1)
683 /* Determine the architecture. */
684 check_architecture (g, fs);
686 /* We already know /etc/fstab exists because it's part of the test above. */
687 if (inspect_with_augeas (g, fs, "/etc/fstab", check_fstab) == -1)
690 /* Determine hostname. */
691 if (check_hostname_unix (g, fs) == -1)
697 /* Debian/Ubuntu install disks are easy ...
699 * These files are added by the debian-cd program, and it is worth
700 * looking at the source code to determine exact values, in
701 * particular '/usr/share/debian-cd/tools/start_new_disc'
703 * XXX Architecture? We could parse it out of the product name
704 * string, but that seems quite hairy. We could look for the names
705 * of packages. Also note that some Debian install disks are
709 check_debian_installer_root (guestfs_h *g, struct inspect_fs *fs)
711 fs->product_name = first_line_of_file (g, "/.disk/info");
712 if (!fs->product_name)
715 fs->type = OS_TYPE_LINUX;
716 if (STRPREFIX (fs->product_name, "Ubuntu"))
717 fs->distro = OS_DISTRO_UBUNTU;
718 else if (STRPREFIX (fs->product_name, "Debian"))
719 fs->distro = OS_DISTRO_DEBIAN;
721 (void) parse_major_minor (g, fs);
723 if (guestfs_is_file (g, "/.disk/cd_type") > 0) {
724 char *cd_type = first_line_of_file (g, "/.disk/cd_type");
728 if (STRPREFIX (cd_type, "dvd/single") ||
729 STRPREFIX (cd_type, "full_cd/single")) {
730 fs->is_multipart_disk = 0;
731 fs->is_netinst_disk = 0;
733 else if (STRPREFIX (cd_type, "dvd") ||
734 STRPREFIX (cd_type, "full_cd")) {
735 fs->is_multipart_disk = 1;
736 fs->is_netinst_disk = 0;
738 else if (STRPREFIX (cd_type, "not_complete")) {
739 fs->is_multipart_disk = 0;
740 fs->is_netinst_disk = 1;
749 /* Take string which must look like "key = value" and find the value.
750 * There may or may not be spaces before and after the equals sign.
751 * This function is used by both check_fedora_installer_root and
752 * check_w2k3_installer_root.
755 find_value (const char *kv)
759 p = strchr (kv, '=');
765 } while (c_isspace (*p));
770 /* Fedora CDs and DVD (not netinst). The /.treeinfo file contains
771 * an initial section somewhat like this:
782 check_fedora_installer_root (guestfs_h *g, struct inspect_fs *fs)
787 int discnum = 0, totaldiscs = 0;
789 fs->type = OS_TYPE_LINUX;
791 r = first_egrep_of_file (g, "/.treeinfo",
792 "^family = Fedora$", 0, &str);
796 fs->distro = OS_DISTRO_FEDORA;
800 r = first_egrep_of_file (g, "/.treeinfo",
801 "^family = Red Hat Enterprise Linux$", 0, &str);
805 fs->distro = OS_DISTRO_RHEL;
809 /* XXX should do major.minor before this */
810 r = first_egrep_of_file (g, "/.treeinfo",
811 "^version = [[:digit:]]+", 0, &str);
815 v = find_value (str);
816 fs->major_version = parse_unsigned_int_ignore_trailing (g, v);
818 if (fs->major_version == -1)
822 r = first_egrep_of_file (g, "/.treeinfo",
823 "^arch = [-_[:alnum:]]+$", 0, &str);
827 v = find_value (str);
828 fs->arch = safe_strdup (g, v);
832 r = first_egrep_of_file (g, "/.treeinfo",
833 "^discnum = [[:digit:]]+$", 0, &str);
837 v = find_value (str);
838 discnum = parse_unsigned_int (g, v);
844 r = first_egrep_of_file (g, "/.treeinfo",
845 "^totaldiscs = [[:digit:]]+$", 0, &str);
849 v = find_value (str);
850 totaldiscs = parse_unsigned_int (g, v);
852 if (totaldiscs == -1)
856 fs->is_multipart_disk = totaldiscs > 0;
857 /* and what about discnum? */
862 /* Linux with /isolinux/isolinux.cfg.
864 * This file is not easily parsable so we have to do our best.
865 * Look for the "menu title" line which contains:
866 * menu title Welcome to Fedora 14! # since at least Fedora 10
867 * menu title Welcome to Red Hat Enterprise Linux 6.0!
870 check_isolinux_installer_root (guestfs_h *g, struct inspect_fs *fs)
875 fs->type = OS_TYPE_LINUX;
877 r = first_egrep_of_file (g, "/isolinux/isolinux.cfg",
878 "^menu title Welcome to Fedora [[:digit:]]+",
883 fs->distro = OS_DISTRO_FEDORA;
884 fs->major_version = parse_unsigned_int_ignore_trailing (g, &str[29]);
886 if (fs->major_version == -1)
890 /* XXX parse major.minor */
891 r = first_egrep_of_file (g, "/isolinux/isolinux.cfg",
892 "^menu title Welcome to Red Hat Enterprise Linux [[:digit:]]+",
897 fs->distro = OS_DISTRO_RHEL;
898 fs->major_version = parse_unsigned_int_ignore_trailing (g, &str[47]);
900 if (fs->major_version == -1)
907 /* Windows 2003 and similar versions.
909 * NB: txtsetup file contains Windows \r\n line endings, which guestfs_grep
910 * does not remove. We have to remove them by hand here.
915 size_t n = strlen (str);
916 if (n > 0 && str[n-1] == '\r')
921 trim_quot (char *str)
923 size_t n = strlen (str);
924 if (n > 0 && str[n-1] == '"')
929 check_w2k3_installer_root (guestfs_h *g, struct inspect_fs *fs,
930 const char *txtsetup)
936 fs->type = OS_TYPE_WINDOWS;
937 fs->distro = OS_DISTRO_WINDOWS;
939 r = first_egrep_of_file (g, txtsetup,
940 "^productname[[:space:]]*=[[:space:]]*\"", 1, &str);
946 v = find_value (str);
947 fs->product_name = safe_strdup (g, v+1);
951 r = first_egrep_of_file (g, txtsetup,
952 "^majorversion[[:space:]]*=[[:space:]]*[[:digit:]]+",
958 v = find_value (str);
959 fs->major_version = parse_unsigned_int_ignore_trailing (g, v);
961 if (fs->major_version == -1)
965 r = first_egrep_of_file (g, txtsetup,
966 "^minorversion[[:space:]]*=[[:space:]]*[[:digit:]]+",
972 v = find_value (str);
973 fs->minor_version = parse_unsigned_int_ignore_trailing (g, v);
975 if (fs->minor_version == -1)
979 /* This is the windows systemroot that would be chosen on
980 * installation by default, although not necessarily the one that
981 * the user will finally choose.
983 r = first_egrep_of_file (g, txtsetup, "^defaultpath[[:space:]]*=[[:space:]]*",
989 v = find_value (str);
990 fs->windows_systemroot = safe_strdup (g, v);
997 /* The currently mounted device is very likely to be an installer. */
999 check_installer_root (guestfs_h *g, struct inspect_fs *fs)
1001 /* The presence of certain files indicates a live CD.
1003 * XXX Fedora netinst contains a ~120MB squashfs called
1004 * /images/install.img. However this is not a live CD (unlike the
1005 * Fedora live CDs which contain the same, but larger file). We
1006 * need to unpack this and look inside to tell the difference.
1008 if (guestfs_is_file (g, "/casper/filesystem.squashfs") > 0)
1009 fs->is_live_disk = 1;
1011 /* Debian/Ubuntu. */
1012 if (guestfs_is_file (g, "/.disk/info") > 0) {
1013 if (check_debian_installer_root (g, fs) == -1)
1017 /* Fedora CDs and DVD (not netinst). */
1018 else if (guestfs_is_file (g, "/.treeinfo") > 0) {
1019 if (check_fedora_installer_root (g, fs) == -1)
1023 /* Linux with /isolinux/isolinux.cfg. */
1024 else if (guestfs_is_file (g, "/isolinux/isolinux.cfg") > 0) {
1025 if (check_isolinux_installer_root (g, fs) == -1)
1029 /* Windows 2003 64 bit */
1030 else if (guestfs_is_file (g, "/amd64/txtsetup.sif") > 0) {
1031 fs->arch = safe_strdup (g, "x86_64");
1032 if (check_w2k3_installer_root (g, fs, "/amd64/txtsetup.sif") == -1)
1036 /* Windows 2003 32 bit */
1037 else if (guestfs_is_file (g, "/i386/txtsetup.sif") > 0) {
1038 fs->arch = safe_strdup (g, "i386");
1039 if (check_w2k3_installer_root (g, fs, "/i386/txtsetup.sif") == -1)
1047 check_architecture (guestfs_h *g, struct inspect_fs *fs)
1049 const char *binaries[] =
1050 { "/bin/bash", "/bin/ls", "/bin/echo", "/bin/rm", "/bin/sh" };
1053 for (i = 0; i < sizeof binaries / sizeof binaries[0]; ++i) {
1054 if (guestfs_is_file (g, binaries[i]) > 0) {
1055 /* Ignore errors from file_architecture call. */
1056 guestfs_error_handler_cb old_error_cb = g->error_cb;
1058 char *arch = guestfs_file_architecture (g, binaries[i]);
1059 g->error_cb = old_error_cb;
1062 /* String will be owned by handle, freed by
1063 * guestfs___free_inspect_info.
1072 /* Try several methods to determine the hostname from a Linux or
1073 * FreeBSD guest. Note that type and distro have been set, so we can
1074 * use that information to direct the search.
1077 check_hostname_unix (guestfs_h *g, struct inspect_fs *fs)
1081 /* Red Hat-derived would be in /etc/sysconfig/network, and
1082 * Debian-derived in the file /etc/hostname. Very old Debian and
1083 * SUSE use /etc/HOSTNAME. It's best to just look for each of
1084 * these files in turn, rather than try anything clever based on
1087 if (guestfs_is_file (g, "/etc/HOSTNAME")) {
1088 fs->hostname = first_line_of_file (g, "/etc/HOSTNAME");
1089 if (fs->hostname == NULL)
1092 else if (guestfs_is_file (g, "/etc/hostname")) {
1093 fs->hostname = first_line_of_file (g, "/etc/hostname");
1094 if (fs->hostname == NULL)
1097 else if (guestfs_is_file (g, "/etc/sysconfig/network")) {
1098 if (inspect_with_augeas (g, fs, "/etc/sysconfig/network",
1099 check_hostname_redhat) == -1)
1104 case OS_TYPE_FREEBSD:
1105 /* /etc/rc.conf contains the hostname, but there is no Augeas lens
1108 if (guestfs_is_file (g, "/etc/rc.conf")) {
1109 if (check_hostname_freebsd (g, fs) == -1)
1114 case OS_TYPE_WINDOWS: /* not here, see check_windows_system_registry */
1115 case OS_TYPE_UNKNOWN:
1117 /* nothing, keep GCC warnings happy */;
1123 /* Parse the hostname from /etc/sysconfig/network. This must be called
1124 * from the inspect_with_augeas wrapper.
1127 check_hostname_redhat (guestfs_h *g, struct inspect_fs *fs)
1131 hostname = guestfs_aug_get (g, "/files/etc/sysconfig/network/HOSTNAME");
1135 fs->hostname = hostname; /* freed by guestfs___free_inspect_info */
1139 /* Parse the hostname from /etc/rc.conf. On FreeBSD this file
1140 * contains comments, blank lines and:
1141 * hostname="freebsd8.example.com"
1142 * ifconfig_re0="DHCP"
1147 check_hostname_freebsd (guestfs_h *g, struct inspect_fs *fs)
1149 const char *filename = "/etc/rc.conf";
1154 /* Don't trust guestfs_read_lines not to break with very large files.
1155 * Check the file size is something reasonable first.
1157 size = guestfs_filesize (g, filename);
1159 /* guestfs_filesize failed and has already set error in handle */
1161 if (size > MAX_SMALL_FILE_SIZE) {
1162 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
1167 lines = guestfs_read_lines (g, filename);
1171 for (i = 0; lines[i] != NULL; ++i) {
1172 if (STRPREFIX (lines[i], "hostname=\"") ||
1173 STRPREFIX (lines[i], "hostname='")) {
1174 size_t len = strlen (lines[i]) - 10 - 1;
1175 fs->hostname = safe_strndup (g, &lines[i][10], len);
1177 } else if (STRPREFIX (lines[i], "hostname=")) {
1178 size_t len = strlen (lines[i]) - 9;
1179 fs->hostname = safe_strndup (g, &lines[i][9], len);
1184 guestfs___free_string_list (lines);
1189 check_fstab (guestfs_h *g, struct inspect_fs *fs)
1191 char **lines = guestfs_aug_ls (g, "/files/etc/fstab");
1195 if (lines[0] == NULL) {
1196 error (g, _("could not parse /etc/fstab or empty file"));
1197 guestfs___free_string_list (lines);
1203 for (i = 0; lines[i] != NULL; ++i) {
1204 /* Ignore comments. Only care about sequence lines which
1207 if (match (g, lines[i], re_aug_seq)) {
1208 snprintf (augpath, sizeof augpath, "%s/spec", lines[i]);
1209 char *spec = guestfs_aug_get (g, augpath);
1211 guestfs___free_string_list (lines);
1215 snprintf (augpath, sizeof augpath, "%s/file", lines[i]);
1216 char *mp = guestfs_aug_get (g, augpath);
1218 guestfs___free_string_list (lines);
1223 int r = add_fstab_entry (g, fs, spec, mp);
1228 guestfs___free_string_list (lines);
1234 guestfs___free_string_list (lines);
1238 /* Add a filesystem and possibly a mountpoint entry for
1239 * the root filesystem 'fs'.
1241 * 'spec' is the fstab spec field, which might be a device name or a
1242 * pseudodevice or 'UUID=...' or 'LABEL=...'.
1244 * 'mp' is the mount point, which could also be 'swap' or 'none'.
1247 add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
1248 const char *spec, const char *mp)
1250 /* Ignore certain mountpoints. */
1251 if (STRPREFIX (mp, "/dev/") ||
1252 STREQ (mp, "/dev") ||
1253 STRPREFIX (mp, "/media/") ||
1254 STRPREFIX (mp, "/proc/") ||
1255 STREQ (mp, "/proc") ||
1256 STRPREFIX (mp, "/selinux/") ||
1257 STREQ (mp, "/selinux") ||
1258 STRPREFIX (mp, "/sys/") ||
1262 /* Ignore /dev/fd (floppy disks) (RHBZ#642929) and CD-ROM drives. */
1263 if ((STRPREFIX (spec, "/dev/fd") && c_isdigit (spec[7])) ||
1264 STREQ (spec, "/dev/floppy") ||
1265 STREQ (spec, "/dev/cdrom"))
1268 /* Resolve UUID= and LABEL= to the actual device. */
1269 char *device = NULL;
1270 if (STRPREFIX (spec, "UUID="))
1271 device = guestfs_findfs_uuid (g, &spec[5]);
1272 else if (STRPREFIX (spec, "LABEL="))
1273 device = guestfs_findfs_label (g, &spec[6]);
1274 /* Ignore "/.swap" (Pardus) and pseudo-devices like "tmpfs". */
1275 else if (STRPREFIX (spec, "/dev/"))
1276 /* Resolve guest block device names. */
1277 device = resolve_fstab_device (g, spec);
1279 /* If we haven't resolved the device successfully by this point,
1280 * we don't care, just ignore it.
1285 char *mountpoint = safe_strdup (g, mp);
1287 /* Add this to the fstab entry in 'fs'.
1288 * Note these are further filtered by guestfs_inspect_get_mountpoints
1289 * and guestfs_inspect_get_filesystems.
1291 size_t n = fs->nr_fstab + 1;
1292 struct inspect_fstab_entry *p;
1294 p = realloc (fs->fstab, n * sizeof (struct inspect_fstab_entry));
1296 perrorf (g, "realloc");
1305 /* These are owned by the handle and freed by guestfs___free_inspect_info. */
1306 fs->fstab[n-1].device = device;
1307 fs->fstab[n-1].mountpoint = mountpoint;
1309 debug (g, "fstab: device=%s mountpoint=%s", device, mountpoint);
1314 /* Resolve block device name to the libguestfs device name, eg.
1315 * /dev/xvdb1 => /dev/vdb1; and /dev/mapper/VG-LV => /dev/VG/LV. This
1316 * assumes that disks were added in the same order as they appear to
1317 * the real VM, which is a reasonable assumption to make. Return
1318 * anything we don't recognize unchanged.
1321 resolve_fstab_device (guestfs_h *g, const char *spec)
1324 char *device = NULL;
1325 char *bsddisk, *bsdslice, *bsdpart;
1327 if (STRPREFIX (spec, "/dev/mapper/")) {
1328 /* LVM2 does some strange munging on /dev/mapper paths for VGs and
1329 * LVs which contain '-' character:
1331 * ><fs> lvcreate LV--test VG--test 32
1332 * ><fs> debug ls /dev/mapper
1333 * VG----test-LV----test
1335 * This makes it impossible to reverse those paths directly, so
1336 * we have implemented lvm_canonical_lv_name in the daemon.
1338 device = guestfs_lvm_canonical_lv_name (g, spec);
1340 else if ((a1 = match1 (g, spec, re_xdev)) != NULL) {
1341 char **devices = guestfs_list_devices (g);
1342 if (devices == NULL)
1346 for (count = 0; devices[count] != NULL; count++)
1349 size_t i = a1[0] - 'a'; /* a1[0] is always [a-z] because of regex. */
1351 size_t len = strlen (devices[i]) + strlen (a1) + 16;
1352 device = safe_malloc (g, len);
1353 snprintf (device, len, "%s%s", devices[i], &a1[1]);
1357 guestfs___free_string_list (devices);
1359 else if (match3 (g, spec, re_freebsd, &bsddisk, &bsdslice, &bsdpart)) {
1360 /* FreeBSD disks are organized quite differently. See:
1361 * http://www.freebsd.org/doc/handbook/disk-organization.html
1362 * FreeBSD "partitions" are exposed as quasi-extended partitions
1363 * numbered from 5 in Linux. I have no idea what happens when you
1364 * have multiple "slices" (the FreeBSD term for MBR partitions).
1366 int disk = parse_unsigned_int (g, bsddisk);
1367 int slice = parse_unsigned_int (g, bsdslice);
1368 int part = bsdpart[0] - 'a' /* counting from 0 */;
1373 if (disk == -1 || disk > 26 ||
1374 slice <= 0 || slice > 1 /* > 4 .. see comment above */ ||
1375 part < 0 || part >= 26)
1378 device = safe_asprintf (g, "/dev/sd%c%d", disk + 'a', part + 5);
1382 /* Didn't match device pattern, return original spec unchanged. */
1384 device = safe_strdup (g, spec);
1389 /* XXX Handling of boot.ini in the Perl version was pretty broken. It
1390 * essentially didn't do anything for modern Windows guests.
1391 * Therefore I've omitted all that code.
1394 check_windows_root (guestfs_h *g, struct inspect_fs *fs)
1396 fs->type = OS_TYPE_WINDOWS;
1397 fs->distro = OS_DISTRO_WINDOWS;
1399 /* Try to find Windows systemroot using some common locations. */
1400 const char *systemroots[] =
1401 { "/windows", "/winnt", "/win32", "/win" };
1403 char *systemroot = NULL;
1405 systemroot == NULL && i < sizeof systemroots / sizeof systemroots[0];
1407 systemroot = resolve_windows_path_silently (g, systemroots[i]);
1411 error (g, _("cannot resolve Windows %%SYSTEMROOT%%"));
1415 debug (g, "windows %%SYSTEMROOT%% = %s", systemroot);
1417 /* Freed by guestfs___free_inspect_info. */
1418 fs->windows_systemroot = systemroot;
1420 if (check_windows_arch (g, fs) == -1)
1423 /* Product name and version. */
1424 if (check_windows_software_registry (g, fs) == -1)
1427 check_package_format (g, fs);
1428 check_package_management (g, fs);
1431 if (check_windows_system_registry (g, fs) == -1)
1438 check_windows_arch (guestfs_h *g, struct inspect_fs *fs)
1440 size_t len = strlen (fs->windows_systemroot) + 32;
1442 snprintf (cmd_exe, len, "%s/system32/cmd.exe", fs->windows_systemroot);
1444 char *cmd_exe_path = resolve_windows_path_silently (g, cmd_exe);
1448 char *arch = guestfs_file_architecture (g, cmd_exe_path);
1449 free (cmd_exe_path);
1452 fs->arch = arch; /* freed by guestfs___free_inspect_info */
1457 /* At the moment, pull just the ProductName and version numbers from
1458 * the registry. In future there is a case for making many more
1459 * registry fields available to callers.
1462 check_windows_software_registry (guestfs_h *g, struct inspect_fs *fs)
1464 TMP_TEMPLATE_ON_STACK (software_local);
1466 size_t len = strlen (fs->windows_systemroot) + 64;
1468 snprintf (software, len, "%s/system32/config/software",
1469 fs->windows_systemroot);
1471 char *software_path = resolve_windows_path_silently (g, software);
1473 /* If the software hive doesn't exist, just accept that we cannot
1474 * find product_name etc.
1480 hive_value_h *values = NULL;
1482 if (download_to_tmp (g, software_path, software_local,
1483 MAX_REGISTRY_SIZE) == -1)
1486 h = hivex_open (software_local, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
1488 perrorf (g, "hivex_open");
1492 hive_node_h node = hivex_root (h);
1493 const char *hivepath[] =
1494 { "Microsoft", "Windows NT", "CurrentVersion" };
1497 node != 0 && i < sizeof hivepath / sizeof hivepath[0];
1499 node = hivex_node_get_child (h, node, hivepath[i]);
1503 perrorf (g, "hivex: cannot locate HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
1507 values = hivex_node_values (h, node);
1509 for (i = 0; values[i] != 0; ++i) {
1510 char *key = hivex_value_key (h, values[i]);
1512 perrorf (g, "hivex_value_key");
1516 if (STRCASEEQ (key, "ProductName")) {
1517 fs->product_name = hivex_value_string (h, values[i]);
1518 if (!fs->product_name) {
1519 perrorf (g, "hivex_value_string");
1524 else if (STRCASEEQ (key, "CurrentVersion")) {
1525 char *version = hivex_value_string (h, values[i]);
1527 perrorf (g, "hivex_value_string");
1531 char *major, *minor;
1532 if (match2 (g, version, re_windows_version, &major, &minor)) {
1533 fs->major_version = parse_unsigned_int (g, major);
1535 if (fs->major_version == -1) {
1541 fs->minor_version = parse_unsigned_int (g, minor);
1543 if (fs->minor_version == -1) {
1559 if (h) hivex_close (h);
1561 free (software_path);
1563 /* Free up the temporary file. */
1564 unlink (software_local);
1565 #undef software_local_len
1571 check_windows_system_registry (guestfs_h *g, struct inspect_fs *fs)
1573 TMP_TEMPLATE_ON_STACK (system_local);
1575 size_t len = strlen (fs->windows_systemroot) + 64;
1577 snprintf (system, len, "%s/system32/config/system",
1578 fs->windows_systemroot);
1580 char *system_path = resolve_windows_path_silently (g, system);
1582 /* If the system hive doesn't exist, just accept that we cannot
1583 * find hostname etc.
1589 hive_value_h *values = NULL;
1591 if (download_to_tmp (g, system_path, system_local, MAX_REGISTRY_SIZE) == -1)
1594 h = hivex_open (system_local, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
1596 perrorf (g, "hivex_open");
1600 hive_node_h node = hivex_root (h);
1601 /* XXX Don't hard-code ControlSet001. The current control set would
1602 * be another good thing to expose up through the inspection API.
1604 const char *hivepath[] =
1605 { "ControlSet001", "Services", "Tcpip", "Parameters" };
1608 node != 0 && i < sizeof hivepath / sizeof hivepath[0];
1610 node = hivex_node_get_child (h, node, hivepath[i]);
1614 perrorf (g, "hivex: cannot locate HKLM\\SYSTEM\\ControlSet001\\Services\\Tcpip\\Parameters");
1618 values = hivex_node_values (h, node);
1620 for (i = 0; values[i] != 0; ++i) {
1621 char *key = hivex_value_key (h, values[i]);
1623 perrorf (g, "hivex_value_key");
1627 if (STRCASEEQ (key, "Hostname")) {
1628 fs->hostname = hivex_value_string (h, values[i]);
1629 if (!fs->hostname) {
1630 perrorf (g, "hivex_value_string");
1635 /* many other interesting fields here ... */
1643 if (h) hivex_close (h);
1647 /* Free up the temporary file. */
1648 unlink (system_local);
1649 #undef system_local_len
1655 resolve_windows_path_silently (guestfs_h *g, const char *path)
1657 guestfs_error_handler_cb old_error_cb = g->error_cb;
1659 char *ret = guestfs_case_sensitive_path (g, path);
1660 g->error_cb = old_error_cb;
1665 extend_fses (guestfs_h *g)
1667 size_t n = g->nr_fses + 1;
1668 struct inspect_fs *p;
1670 p = realloc (g->fses, n * sizeof (struct inspect_fs));
1672 perrorf (g, "realloc");
1679 memset (&g->fses[n-1], 0, sizeof (struct inspect_fs));
1684 /* Parse small, unsigned ints, as used in version numbers. */
1686 parse_unsigned_int (guestfs_h *g, const char *str)
1689 int r = xstrtol (str, NULL, 10, &ret, "");
1690 if (r != LONGINT_OK) {
1691 error (g, _("could not parse integer in version number: %s"), str);
1697 /* Like parse_unsigned_int, but ignore trailing stuff. */
1699 parse_unsigned_int_ignore_trailing (guestfs_h *g, const char *str)
1702 int r = xstrtol (str, NULL, 10, &ret, NULL);
1703 if (r != LONGINT_OK) {
1704 error (g, _("could not parse integer in version number: %s"), str);
1710 /* At the moment, package format and package management is just a
1711 * simple function of the distro and major_version fields, so these
1712 * can never return an error. We might be cleverer in future.
1715 check_package_format (guestfs_h *g, struct inspect_fs *fs)
1717 switch (fs->distro) {
1718 case OS_DISTRO_FEDORA:
1719 case OS_DISTRO_MEEGO:
1720 case OS_DISTRO_REDHAT_BASED:
1721 case OS_DISTRO_RHEL:
1722 case OS_DISTRO_MANDRIVA:
1723 fs->package_format = OS_PACKAGE_FORMAT_RPM;
1726 case OS_DISTRO_DEBIAN:
1727 case OS_DISTRO_UBUNTU:
1728 case OS_DISTRO_LINUX_MINT:
1729 fs->package_format = OS_PACKAGE_FORMAT_DEB;
1732 case OS_DISTRO_ARCHLINUX:
1733 fs->package_format = OS_PACKAGE_FORMAT_PACMAN;
1735 case OS_DISTRO_GENTOO:
1736 fs->package_format = OS_PACKAGE_FORMAT_EBUILD;
1738 case OS_DISTRO_PARDUS:
1739 fs->package_format = OS_PACKAGE_FORMAT_PISI;
1742 case OS_DISTRO_WINDOWS:
1743 case OS_DISTRO_UNKNOWN:
1745 fs->package_format = OS_PACKAGE_FORMAT_UNKNOWN;
1751 check_package_management (guestfs_h *g, struct inspect_fs *fs)
1753 switch (fs->distro) {
1754 case OS_DISTRO_FEDORA:
1755 case OS_DISTRO_MEEGO:
1756 fs->package_management = OS_PACKAGE_MANAGEMENT_YUM;
1759 case OS_DISTRO_REDHAT_BASED:
1760 case OS_DISTRO_RHEL:
1761 if (fs->major_version >= 5)
1762 fs->package_management = OS_PACKAGE_MANAGEMENT_YUM;
1764 fs->package_management = OS_PACKAGE_MANAGEMENT_UP2DATE;
1767 case OS_DISTRO_DEBIAN:
1768 case OS_DISTRO_UBUNTU:
1769 case OS_DISTRO_LINUX_MINT:
1770 fs->package_management = OS_PACKAGE_MANAGEMENT_APT;
1773 case OS_DISTRO_ARCHLINUX:
1774 fs->package_management = OS_PACKAGE_MANAGEMENT_PACMAN;
1776 case OS_DISTRO_GENTOO:
1777 fs->package_management = OS_PACKAGE_MANAGEMENT_PORTAGE;
1779 case OS_DISTRO_PARDUS:
1780 fs->package_management = OS_PACKAGE_MANAGEMENT_PISI;
1782 case OS_DISTRO_MANDRIVA:
1783 fs->package_management = OS_PACKAGE_MANAGEMENT_URPMI;
1786 case OS_DISTRO_WINDOWS:
1787 case OS_DISTRO_UNKNOWN:
1789 fs->package_management = OS_PACKAGE_MANAGEMENT_UNKNOWN;
1794 static struct inspect_fs *
1795 search_for_root (guestfs_h *g, const char *root)
1797 if (g->nr_fses == 0) {
1798 error (g, _("no inspection data: call guestfs_inspect_os first"));
1803 struct inspect_fs *fs;
1804 for (i = 0; i < g->nr_fses; ++i) {
1806 if (fs->is_root && STREQ (root, fs->device))
1810 error (g, _("%s: root device not found: only call this function with a root device previously returned by guestfs_inspect_os"),
1816 guestfs__inspect_get_roots (guestfs_h *g)
1818 /* NB. Doesn't matter if g->nr_fses == 0. We just return an empty
1819 * list in this case.
1824 for (i = 0; i < g->nr_fses; ++i)
1825 if (g->fses[i].is_root)
1828 char **ret = calloc (count+1, sizeof (char *));
1830 perrorf (g, "calloc");
1835 for (i = 0; i < g->nr_fses; ++i) {
1836 if (g->fses[i].is_root) {
1837 ret[count] = safe_strdup (g, g->fses[i].device);
1847 guestfs__inspect_get_type (guestfs_h *g, const char *root)
1849 struct inspect_fs *fs = search_for_root (g, root);
1855 case OS_TYPE_LINUX: ret = safe_strdup (g, "linux"); break;
1856 case OS_TYPE_WINDOWS: ret = safe_strdup (g, "windows"); break;
1857 case OS_TYPE_FREEBSD: ret = safe_strdup (g, "freebsd"); break;
1858 case OS_TYPE_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1865 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
1867 struct inspect_fs *fs = search_for_root (g, root);
1871 return safe_strdup (g, fs->arch ? : "unknown");
1875 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
1877 struct inspect_fs *fs = search_for_root (g, root);
1882 switch (fs->distro) {
1883 case OS_DISTRO_ARCHLINUX: ret = safe_strdup (g, "archlinux"); break;
1884 case OS_DISTRO_DEBIAN: ret = safe_strdup (g, "debian"); break;
1885 case OS_DISTRO_FEDORA: ret = safe_strdup (g, "fedora"); break;
1886 case OS_DISTRO_GENTOO: ret = safe_strdup (g, "gentoo"); break;
1887 case OS_DISTRO_LINUX_MINT: ret = safe_strdup (g, "linuxmint"); break;
1888 case OS_DISTRO_MANDRIVA: ret = safe_strdup (g, "mandriva"); break;
1889 case OS_DISTRO_MEEGO: ret = safe_strdup (g, "meego"); break;
1890 case OS_DISTRO_PARDUS: ret = safe_strdup (g, "pardus"); break;
1891 case OS_DISTRO_REDHAT_BASED: ret = safe_strdup (g, "redhat-based"); break;
1892 case OS_DISTRO_RHEL: ret = safe_strdup (g, "rhel"); break;
1893 case OS_DISTRO_WINDOWS: ret = safe_strdup (g, "windows"); break;
1894 case OS_DISTRO_UBUNTU: ret = safe_strdup (g, "ubuntu"); break;
1895 case OS_DISTRO_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1902 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
1904 struct inspect_fs *fs = search_for_root (g, root);
1908 return fs->major_version;
1912 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
1914 struct inspect_fs *fs = search_for_root (g, root);
1918 return fs->minor_version;
1922 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
1924 struct inspect_fs *fs = search_for_root (g, root);
1928 return safe_strdup (g, fs->product_name ? : "unknown");
1932 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
1934 struct inspect_fs *fs = search_for_root (g, root);
1938 if (!fs->windows_systemroot) {
1939 error (g, _("not a Windows guest, or systemroot could not be determined"));
1943 return safe_strdup (g, fs->windows_systemroot);
1947 guestfs__inspect_get_format (guestfs_h *g, const char *root)
1949 struct inspect_fs *fs = search_for_root (g, root);
1954 switch (fs->format) {
1955 case OS_FORMAT_INSTALLED: ret = safe_strdup (g, "installed"); break;
1956 case OS_FORMAT_INSTALLER: ret = safe_strdup (g, "installer"); break;
1957 case OS_FORMAT_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1964 guestfs__inspect_is_live (guestfs_h *g, const char *root)
1966 struct inspect_fs *fs = search_for_root (g, root);
1970 return fs->is_live_disk;
1974 guestfs__inspect_is_netinst (guestfs_h *g, const char *root)
1976 struct inspect_fs *fs = search_for_root (g, root);
1980 return fs->is_netinst_disk;
1984 guestfs__inspect_is_multipart (guestfs_h *g, const char *root)
1986 struct inspect_fs *fs = search_for_root (g, root);
1990 return fs->is_multipart_disk;
1994 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
1996 struct inspect_fs *fs = search_for_root (g, root);
2002 /* If no fstab information (Windows) return just the root. */
2003 if (fs->nr_fstab == 0) {
2004 ret = calloc (3, sizeof (char *));
2005 ret[0] = safe_strdup (g, "/");
2006 ret[1] = safe_strdup (g, root);
2011 #define CRITERION fs->fstab[i].mountpoint[0] == '/'
2012 size_t i, count = 0;
2013 for (i = 0; i < fs->nr_fstab; ++i)
2017 /* Hashtables have 2N+1 entries. */
2018 ret = calloc (2*count+1, sizeof (char *));
2020 perrorf (g, "calloc");
2025 for (i = 0; i < fs->nr_fstab; ++i)
2027 ret[2*count] = safe_strdup (g, fs->fstab[i].mountpoint);
2028 ret[2*count+1] = safe_strdup (g, fs->fstab[i].device);
2037 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
2039 struct inspect_fs *fs = search_for_root (g, root);
2045 /* If no fstab information (Windows) return just the root. */
2046 if (fs->nr_fstab == 0) {
2047 ret = calloc (2, sizeof (char *));
2048 ret[0] = safe_strdup (g, root);
2053 ret = calloc (fs->nr_fstab + 1, sizeof (char *));
2055 perrorf (g, "calloc");
2060 for (i = 0; i < fs->nr_fstab; ++i)
2061 ret[i] = safe_strdup (g, fs->fstab[i].device);
2067 guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
2069 struct inspect_fs *fs = search_for_root (g, root);
2074 switch (fs->package_format) {
2075 case OS_PACKAGE_FORMAT_RPM: ret = safe_strdup (g, "rpm"); break;
2076 case OS_PACKAGE_FORMAT_DEB: ret = safe_strdup (g, "deb"); break;
2077 case OS_PACKAGE_FORMAT_PACMAN: ret = safe_strdup (g, "pacman"); break;
2078 case OS_PACKAGE_FORMAT_EBUILD: ret = safe_strdup (g, "ebuild"); break;
2079 case OS_PACKAGE_FORMAT_PISI: ret = safe_strdup (g, "pisi"); break;
2080 case OS_PACKAGE_FORMAT_UNKNOWN:
2082 ret = safe_strdup (g, "unknown");
2090 guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
2092 struct inspect_fs *fs = search_for_root (g, root);
2097 switch (fs->package_management) {
2098 case OS_PACKAGE_MANAGEMENT_YUM: ret = safe_strdup (g, "yum"); break;
2099 case OS_PACKAGE_MANAGEMENT_UP2DATE: ret = safe_strdup (g, "up2date"); break;
2100 case OS_PACKAGE_MANAGEMENT_APT: ret = safe_strdup (g, "apt"); break;
2101 case OS_PACKAGE_MANAGEMENT_PACMAN: ret = safe_strdup (g, "pacman"); break;
2102 case OS_PACKAGE_MANAGEMENT_PORTAGE: ret = safe_strdup (g, "portage"); break;
2103 case OS_PACKAGE_MANAGEMENT_PISI: ret = safe_strdup (g, "pisi"); break;
2104 case OS_PACKAGE_MANAGEMENT_URPMI: ret = safe_strdup (g, "urpmi"); break;
2105 case OS_PACKAGE_MANAGEMENT_UNKNOWN:
2107 ret = safe_strdup (g, "unknown");
2115 guestfs__inspect_get_hostname (guestfs_h *g, const char *root)
2117 struct inspect_fs *fs = search_for_root (g, root);
2121 return safe_strdup (g, fs->hostname ? : "unknown");
2125 static struct guestfs_application_list *list_applications_rpm (guestfs_h *g, struct inspect_fs *fs);
2127 static struct guestfs_application_list *list_applications_deb (guestfs_h *g, struct inspect_fs *fs);
2128 static struct guestfs_application_list *list_applications_windows (guestfs_h *g, struct inspect_fs *fs);
2129 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);
2130 static void sort_applications (struct guestfs_application_list *);
2132 /* Unlike the simple inspect-get-* calls, this one assumes that the
2133 * disks are mounted up, and reads files from the mounted disks.
2135 struct guestfs_application_list *
2136 guestfs__inspect_list_applications (guestfs_h *g, const char *root)
2138 struct inspect_fs *fs = search_for_root (g, root);
2142 struct guestfs_application_list *ret = NULL;
2144 /* Presently we can only list applications for installed disks. It
2145 * is possible in future to get lists of packages from installers.
2147 if (fs->format == OS_FORMAT_INSTALLED) {
2150 switch (fs->package_format) {
2151 case OS_PACKAGE_FORMAT_RPM:
2153 ret = list_applications_rpm (g, fs);
2159 case OS_PACKAGE_FORMAT_DEB:
2160 ret = list_applications_deb (g, fs);
2165 case OS_PACKAGE_FORMAT_PACMAN:
2166 case OS_PACKAGE_FORMAT_EBUILD:
2167 case OS_PACKAGE_FORMAT_PISI:
2168 case OS_PACKAGE_FORMAT_UNKNOWN:
2170 /* nothing - keep GCC happy */;
2174 case OS_TYPE_WINDOWS:
2175 ret = list_applications_windows (g, fs);
2180 case OS_TYPE_FREEBSD:
2181 case OS_TYPE_UNKNOWN:
2183 /* nothing - keep GCC happy */;
2188 /* Don't know how to do inspection. Not an error, return an
2191 ret = safe_malloc (g, sizeof *ret);
2196 sort_applications (ret);
2202 static struct guestfs_application_list *
2203 list_applications_rpm (guestfs_h *g, struct inspect_fs *fs)
2205 TMP_TEMPLATE_ON_STACK (tmpfile);
2207 if (download_to_tmp (g, "/var/lib/rpm/Name", tmpfile, MAX_PKG_DB_SIZE) == -1)
2210 struct guestfs_application_list *apps = NULL, *ret = NULL;
2211 #define cmd_len (strlen (tmpfile) + 64)
2217 snprintf (cmd, cmd_len, DB_DUMP " -p '%s'", tmpfile);
2219 debug (g, "list_applications_rpm: %s", cmd);
2221 pp = popen (cmd, "r");
2223 perrorf (g, "popen: %s", cmd);
2227 /* Ignore everything to end-of-header marker. */
2229 if (fgets (line, sizeof line, pp) == NULL) {
2230 error (g, _("unexpected end of output from db_dump command"));
2234 len = strlen (line);
2235 if (len > 0 && line[len-1] == '\n') {
2240 if (STREQ (line, "HEADER=END"))
2244 /* Allocate 'apps' list. */
2245 apps = safe_malloc (g, sizeof *apps);
2249 /* Read alternate lines until end of data marker. */
2251 if (fgets (line, sizeof line, pp) == NULL) {
2252 error (g, _("unexpected end of output from db_dump command"));
2256 len = strlen (line);
2257 if (len > 0 && line[len-1] == '\n') {
2262 if (STREQ (line, "DATA=END"))
2266 if (len > 0 && line[0] == ' ')
2268 /* Ignore any application name that contains non-printable chars.
2269 * In the db_dump output these would be escaped with backslash, so
2270 * we can just ignore any such line.
2272 if (strchr (p, '\\') == NULL)
2273 add_application (g, apps, p, "", 0, "", "", "", "", "", "");
2275 /* Discard next line. */
2276 if (fgets (line, sizeof line, pp) == NULL) {
2277 error (g, _("unexpected end of output from db_dump command"));
2282 /* Catch errors from the db_dump command. */
2283 if (pclose (pp) == -1) {
2284 perrorf (g, "pclose: %s", cmd);
2292 if (ret == NULL && apps != NULL)
2293 guestfs_free_application_list (apps);
2301 #endif /* defined DB_DUMP */
2303 static struct guestfs_application_list *
2304 list_applications_deb (guestfs_h *g, struct inspect_fs *fs)
2306 TMP_TEMPLATE_ON_STACK (tmpfile);
2308 if (download_to_tmp (g, "/var/lib/dpkg/status", tmpfile,
2309 MAX_PKG_DB_SIZE) == -1)
2312 struct guestfs_application_list *apps = NULL, *ret = NULL;
2316 char *name = NULL, *version = NULL, *release = NULL;
2317 int installed_flag = 0;
2319 fp = fopen (tmpfile, "r");
2321 perrorf (g, "fopen: %s", tmpfile);
2325 /* Allocate 'apps' list. */
2326 apps = safe_malloc (g, sizeof *apps);
2330 /* Read the temporary file. Each package entry is separated by
2332 * XXX Strictly speaking this is in mailbox header format, so it
2333 * would be possible for fields to spread across multiple lines,
2334 * although for the short fields that we are concerned about this is
2335 * unlikely and not seen in practice.
2337 while (fgets (line, sizeof line, fp) != NULL) {
2338 len = strlen (line);
2339 if (len > 0 && line[len-1] == '\n') {
2344 if (STRPREFIX (line, "Package: ")) {
2346 name = safe_strdup (g, &line[9]);
2348 else if (STRPREFIX (line, "Status: ")) {
2349 installed_flag = strstr (&line[8], "installed") != NULL;
2351 else if (STRPREFIX (line, "Version: ")) {
2354 char *p = strchr (&line[9], '-');
2357 version = safe_strdup (g, &line[9]);
2358 release = safe_strdup (g, p+1);
2360 version = safe_strdup (g, &line[9]);
2364 else if (STREQ (line, "")) {
2365 if (installed_flag && name && version)
2366 add_application (g, apps, name, "", 0, version, release ? : "",
2371 name = version = release = NULL;
2376 if (fclose (fp) == -1) {
2377 perrorf (g, "fclose: %s", tmpfile);
2385 if (ret == NULL && apps != NULL)
2386 guestfs_free_application_list (apps);
2396 /* XXX We already download the SOFTWARE hive when doing general
2397 * inspection. We could avoid this second download of the same file
2398 * by caching these entries in the handle.
2400 static struct guestfs_application_list *
2401 list_applications_windows (guestfs_h *g, struct inspect_fs *fs)
2403 TMP_TEMPLATE_ON_STACK (software_local);
2405 size_t len = strlen (fs->windows_systemroot) + 64;
2407 snprintf (software, len, "%s/system32/config/software",
2408 fs->windows_systemroot);
2410 char *software_path = resolve_windows_path_silently (g, software);
2412 /* If the software hive doesn't exist, just accept that we cannot
2413 * find product_name etc.
2417 struct guestfs_application_list *apps = NULL, *ret = NULL;
2419 hive_node_h *children = NULL;
2421 if (download_to_tmp (g, software_path, software_local,
2422 MAX_REGISTRY_SIZE) == -1)
2425 h = hivex_open (software_local, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
2427 perrorf (g, "hivex_open");
2431 hive_node_h node = hivex_root (h);
2432 const char *hivepath[] =
2433 { "Microsoft", "Windows", "CurrentVersion", "Uninstall" };
2436 node != 0 && i < sizeof hivepath / sizeof hivepath[0];
2438 node = hivex_node_get_child (h, node, hivepath[i]);
2442 perrorf (g, "hivex: cannot locate HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall");
2446 children = hivex_node_children (h, node);
2447 if (children == NULL) {
2448 perrorf (g, "hivex_node_children");
2452 /* Allocate 'apps' list. */
2453 apps = safe_malloc (g, sizeof *apps);
2457 /* Consider any child node that has a DisplayName key.
2459 * http://nsis.sourceforge.net/Add_uninstall_information_to_Add/Remove_Programs#Optional_values
2461 for (i = 0; children[i] != 0; ++i) {
2464 char *display_name = NULL;
2465 char *version = NULL;
2466 char *install_path = NULL;
2467 char *publisher = NULL;
2469 char *comments = NULL;
2471 /* Use the node name as a proxy for the package name in Linux. The
2472 * display name is not language-independent, so it cannot be used.
2474 name = hivex_node_name (h, children[i]);
2476 perrorf (g, "hivex_node_get_name");
2480 value = hivex_node_get_value (h, children[i], "DisplayName");
2482 display_name = hivex_value_string (h, value);
2484 value = hivex_node_get_value (h, children[i], "DisplayVersion");
2486 version = hivex_value_string (h, value);
2487 value = hivex_node_get_value (h, children[i], "InstallLocation");
2489 install_path = hivex_value_string (h, value);
2490 value = hivex_node_get_value (h, children[i], "Publisher");
2492 publisher = hivex_value_string (h, value);
2493 value = hivex_node_get_value (h, children[i], "URLInfoAbout");
2495 url = hivex_value_string (h, value);
2496 value = hivex_node_get_value (h, children[i], "Comments");
2498 comments = hivex_value_string (h, value);
2500 add_application (g, apps, name, display_name, 0,
2503 install_path ? : "",
2511 free (display_name);
2513 free (install_path);
2522 if (ret == NULL && apps != NULL)
2523 guestfs_free_application_list (apps);
2524 if (h) hivex_close (h);
2526 free (software_path);
2528 /* Free up the temporary file. */
2529 unlink (software_local);
2530 #undef software_local_len
2536 add_application (guestfs_h *g, struct guestfs_application_list *apps,
2537 const char *name, const char *display_name, int32_t epoch,
2538 const char *version, const char *release,
2539 const char *install_path,
2540 const char *publisher, const char *url,
2541 const char *description)
2544 apps->val = safe_realloc (g, apps->val,
2545 apps->len * sizeof (struct guestfs_application));
2546 apps->val[apps->len-1].app_name = safe_strdup (g, name);
2547 apps->val[apps->len-1].app_display_name = safe_strdup (g, display_name);
2548 apps->val[apps->len-1].app_epoch = epoch;
2549 apps->val[apps->len-1].app_version = safe_strdup (g, version);
2550 apps->val[apps->len-1].app_release = safe_strdup (g, release);
2551 apps->val[apps->len-1].app_install_path = safe_strdup (g, install_path);
2552 /* XXX Translated path is not implemented yet. */
2553 apps->val[apps->len-1].app_trans_path = safe_strdup (g, "");
2554 apps->val[apps->len-1].app_publisher = safe_strdup (g, publisher);
2555 apps->val[apps->len-1].app_url = safe_strdup (g, url);
2556 /* XXX The next two are not yet implemented for any package
2557 * format, but we could easily support them for rpm and deb.
2559 apps->val[apps->len-1].app_source_package = safe_strdup (g, "");
2560 apps->val[apps->len-1].app_summary = safe_strdup (g, "");
2561 apps->val[apps->len-1].app_description = safe_strdup (g, description);
2564 /* Sort applications by name before returning the list. */
2566 compare_applications (const void *vp1, const void *vp2)
2568 const struct guestfs_application *v1 = vp1;
2569 const struct guestfs_application *v2 = vp2;
2571 return strcmp (v1->app_name, v2->app_name);
2575 sort_applications (struct guestfs_application_list *apps)
2577 if (apps && apps->val)
2578 qsort (apps->val, apps->len, sizeof (struct guestfs_application),
2579 compare_applications);
2582 /* Download to a guest file to a local temporary file. Refuse to
2583 * download the guest file if it is larger than max_size. The caller
2584 * is responsible for deleting the temporary file after use.
2587 download_to_tmp (guestfs_h *g, const char *filename,
2588 char *localtmp, int64_t max_size)
2594 size = guestfs_filesize (g, filename);
2596 /* guestfs_filesize failed and has already set error in handle */
2598 if (size > max_size) {
2599 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
2604 fd = mkstemp (localtmp);
2606 perrorf (g, "mkstemp");
2610 snprintf (buf, sizeof buf, "/dev/fd/%d", fd);
2612 if (guestfs_download (g, filename, buf) == -1) {
2618 if (close (fd) == -1) {
2619 perrorf (g, "close: %s", localtmp);
2627 /* Call 'f' with Augeas opened and having parsed 'filename' (this file
2628 * must exist). As a security measure, this bails if the file is too
2629 * large for a reasonable configuration file. After the call to 'f'
2633 inspect_with_augeas (guestfs_h *g, struct inspect_fs *fs, const char *filename,
2634 int (*f) (guestfs_h *, struct inspect_fs *))
2636 /* Security: Refuse to do this if filename is too large. */
2637 int64_t size = guestfs_filesize (g, filename);
2639 /* guestfs_filesize failed and has already set error in handle */
2641 if (size > MAX_AUGEAS_FILE_SIZE) {
2642 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
2647 /* If !feature_available (g, "augeas") then the next call will fail.
2648 * Arguably we might want to fall back to a non-Augeas method in
2651 if (guestfs_aug_init (g, "/", 16|32) == -1)
2656 /* Tell Augeas to only load one file (thanks Raphaël Pinson). */
2657 char buf[strlen (filename) + 64];
2658 snprintf (buf, strlen (filename) + 64, "/augeas/load//incl[. != \"%s\"]",
2660 if (guestfs_aug_rm (g, buf) == -1)
2663 if (guestfs_aug_load (g) == -1)
2669 guestfs_aug_close (g);
2674 /* Get the first line of a small file, without any trailing newline
2678 first_line_of_file (guestfs_h *g, const char *filename)
2684 /* Don't trust guestfs_head_n not to break with very large files.
2685 * Check the file size is something reasonable first.
2687 size = guestfs_filesize (g, filename);
2689 /* guestfs_filesize failed and has already set error in handle */
2691 if (size > MAX_SMALL_FILE_SIZE) {
2692 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
2697 lines = guestfs_head_n (g, 1, filename);
2700 if (lines[0] == NULL) {
2701 error (g, _("%s: file is empty"), filename);
2702 guestfs___free_string_list (lines);
2705 /* lines[1] should be NULL because of '1' argument above ... */
2707 ret = lines[0]; /* caller frees */
2708 free (lines); /* free the array */
2713 /* Get the first matching line (using guestfs_egrep{,i}) of a small file,
2714 * without any trailing newline character.
2716 * Returns: 1 = returned a line (in *ret)
2721 first_egrep_of_file (guestfs_h *g, const char *filename,
2722 const char *eregex, int iflag, char **ret)
2728 /* Don't trust guestfs_egrep not to break with very large files.
2729 * Check the file size is something reasonable first.
2731 size = guestfs_filesize (g, filename);
2733 /* guestfs_filesize failed and has already set error in handle */
2735 if (size > MAX_SMALL_FILE_SIZE) {
2736 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
2741 lines = (!iflag ? guestfs_egrep : guestfs_egrepi) (g, eregex, filename);
2744 if (lines[0] == NULL) {
2745 guestfs___free_string_list (lines);
2749 *ret = lines[0]; /* caller frees */
2751 /* free up any other matches and the array itself */
2752 for (i = 1; lines[i] != NULL; ++i)
2759 #else /* no PCRE or hivex at compile time */
2761 /* XXX These functions should be in an optgroup. */
2763 #define NOT_IMPL(r) \
2764 error (g, _("inspection API not available since this version of libguestfs was compiled without PCRE or hivex libraries")); \
2768 guestfs__inspect_os (guestfs_h *g)
2774 guestfs__inspect_get_roots (guestfs_h *g)
2780 guestfs__inspect_get_type (guestfs_h *g, const char *root)
2786 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
2792 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
2798 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
2804 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
2810 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
2816 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
2822 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
2828 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
2834 guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
2840 guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
2846 guestfs__inspect_get_hostname (guestfs_h *g, const char *root)
2851 struct guestfs_application_list *
2852 guestfs__inspect_list_applications (guestfs_h *g, const char *root)
2858 guestfs__inspect_get_format (guestfs_h *g, const char *root)
2864 guestfs__inspect_is_live (guestfs_h *g, const char *root)
2870 guestfs__inspect_is_netinst (guestfs_h *g, const char *root)
2876 guestfs__inspect_is_multipart (guestfs_h *g, const char *root)
2881 #endif /* no PCRE or hivex at compile time */
2884 guestfs___free_inspect_info (guestfs_h *g)
2887 for (i = 0; i < g->nr_fses; ++i) {
2888 free (g->fses[i].device);
2889 free (g->fses[i].product_name);
2890 free (g->fses[i].arch);
2891 free (g->fses[i].hostname);
2892 free (g->fses[i].windows_systemroot);
2894 for (j = 0; j < g->fses[i].nr_fstab; ++j) {
2895 free (g->fses[i].fstab[j].device);
2896 free (g->fses[i].fstab[j].mountpoint);
2898 free (g->fses[i].fstab);
2905 /* In the Perl code this is a public function. */
2907 guestfs___feature_available (guestfs_h *g, const char *feature)
2909 /* If there's an error we should ignore it, so to do that we have to
2910 * temporarily replace the error handler with a null one.
2912 guestfs_error_handler_cb old_error_cb = g->error_cb;
2915 const char *groups[] = { feature, NULL };
2916 int r = guestfs_available (g, (char * const *) groups);
2918 g->error_cb = old_error_cb;
2920 return r == 0 ? 1 : 0;
2925 /* Match a regular expression which contains no captures. Returns
2926 * true if it matches or false if it doesn't.
2929 guestfs___match (guestfs_h *g, const char *str, const pcre *re)
2931 size_t len = strlen (str);
2934 r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
2935 if (r == PCRE_ERROR_NOMATCH)
2938 /* Internal error -- should not happen. */
2939 warning (g, "%s: %s: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
2940 __FILE__, __func__, r, str);
2947 /* Match a regular expression which contains exactly one capture. If
2948 * the string matches, return the capture, otherwise return NULL. The
2949 * caller must free the result.
2952 guestfs___match1 (guestfs_h *g, const char *str, const pcre *re)
2954 size_t len = strlen (str);
2957 r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
2958 if (r == PCRE_ERROR_NOMATCH)
2961 /* Internal error -- should not happen. */
2962 warning (g, "%s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"",
2963 __FILE__, __func__, r, str);
2967 return safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
2970 /* Match a regular expression which contains exactly two captures. */
2972 guestfs___match2 (guestfs_h *g, const char *str, const pcre *re,
2973 char **ret1, char **ret2)
2975 size_t len = strlen (str);
2978 r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
2979 if (r == PCRE_ERROR_NOMATCH)
2982 /* Internal error -- should not happen. */
2983 warning (g, "%s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"",
2984 __FILE__, __func__, r, str);
2988 *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
2989 *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
2994 /* Match a regular expression which contains exactly three captures. */
2996 guestfs___match3 (guestfs_h *g, const char *str, const pcre *re,
2997 char **ret1, char **ret2, char **ret3)
2999 size_t len = strlen (str);
3002 r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
3003 if (r == PCRE_ERROR_NOMATCH)
3006 /* Internal error -- should not happen. */
3007 warning (g, "%s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"",
3008 __FILE__, __func__, r, str);
3012 *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
3013 *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
3014 *ret3 = safe_strndup (g, &str[vec[6]], vec[7]-vec[6]);
3019 #endif /* HAVE_PCRE */