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"
45 #include "guestfs-internal.h"
46 #include "guestfs-internal-actions.h"
47 #include "guestfs_protocol.h"
49 #if defined(HAVE_HIVEX)
51 /* Compile all the regular expressions once when the shared library is
52 * loaded. PCRE is thread safe so we're supposedly OK here if
53 * multiple threads call into the libguestfs API functions below
56 static pcre *re_fedora;
57 static pcre *re_rhel_old;
59 static pcre *re_rhel_no_minor;
60 static pcre *re_centos_old;
61 static pcre *re_centos;
62 static pcre *re_centos_no_minor;
63 static pcre *re_scientific_linux_old;
64 static pcre *re_scientific_linux;
65 static pcre *re_scientific_linux_no_minor;
66 static pcre *re_major_minor;
67 static pcre *re_aug_seq;
69 static pcre *re_cciss;
71 static pcre *re_first_partition;
72 static pcre *re_freebsd;
73 static pcre *re_netbsd;
75 static void compile_regexps (void) __attribute__((constructor));
76 static void free_regexps (void) __attribute__((destructor));
79 compile_regexps (void)
84 #define COMPILE(re,pattern,options) \
86 re = pcre_compile ((pattern), (options), &err, &offset, NULL); \
88 ignore_value (write (2, err, strlen (err))); \
93 COMPILE (re_fedora, "Fedora release (\\d+)", 0);
95 "Red Hat.*release (\\d+).*Update (\\d+)", 0);
97 "Red Hat.*release (\\d+)\\.(\\d+)", 0);
98 COMPILE (re_rhel_no_minor,
99 "Red Hat.*release (\\d+)", 0);
100 COMPILE (re_centos_old,
101 "CentOS.*release (\\d+).*Update (\\d+)", 0);
103 "CentOS.*release (\\d+)\\.(\\d+)", 0);
104 COMPILE (re_centos_no_minor,
105 "CentOS.*release (\\d+)", 0);
106 COMPILE (re_scientific_linux_old,
107 "Scientific Linux.*release (\\d+).*Update (\\d+)", 0);
108 COMPILE (re_scientific_linux,
109 "Scientific Linux.*release (\\d+)\\.(\\d+)", 0);
110 COMPILE (re_scientific_linux_no_minor,
111 "Scientific Linux.*release (\\d+)", 0);
112 COMPILE (re_major_minor, "(\\d+)\\.(\\d+)", 0);
113 COMPILE (re_xdev, "^/dev/(h|s|v|xv)d([a-z]+)(\\d*)$", 0);
114 COMPILE (re_cciss, "^/dev/(cciss/c\\d+d\\d+)(?:p(\\d+))?$", 0);
115 COMPILE (re_mdN, "^(/dev/md\\d+)$", 0);
116 COMPILE (re_freebsd, "^/dev/ad(\\d+)s(\\d+)([a-z])$", 0);
117 COMPILE (re_netbsd, "^NetBSD (\\d+)\\.(\\d+)", 0);
123 pcre_free (re_fedora);
124 pcre_free (re_rhel_old);
126 pcre_free (re_rhel_no_minor);
127 pcre_free (re_centos_old);
128 pcre_free (re_centos);
129 pcre_free (re_centos_no_minor);
130 pcre_free (re_scientific_linux_old);
131 pcre_free (re_scientific_linux);
132 pcre_free (re_scientific_linux_no_minor);
133 pcre_free (re_major_minor);
135 pcre_free (re_cciss);
137 pcre_free (re_freebsd);
138 pcre_free (re_netbsd);
141 static void check_architecture (guestfs_h *g, struct inspect_fs *fs);
142 static int check_hostname_unix (guestfs_h *g, struct inspect_fs *fs);
143 static int check_hostname_redhat (guestfs_h *g, struct inspect_fs *fs);
144 static int check_hostname_freebsd (guestfs_h *g, struct inspect_fs *fs);
145 static int check_fstab (guestfs_h *g, struct inspect_fs *fs);
146 static int add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
147 const char *spec, const char *mp,
149 static char *resolve_fstab_device (guestfs_h *g, const char *spec,
151 static int inspect_with_augeas (guestfs_h *g, struct inspect_fs *fs, const char **configfiles, int (*f) (guestfs_h *, struct inspect_fs *));
153 /* Hash structure for uuid->path lookups */
154 typedef struct md_uuid {
159 static size_t uuid_hash(const void *x, size_t table_size);
160 static bool uuid_cmp(const void *x, const void *y);
161 static void md_uuid_free(void *x);
163 static int parse_uuid(const char *str, uint32_t *uuid);
165 /* Hash structure for path(mdadm)->path(appliance) lookup */
171 static size_t mdadm_app_hash(const void *x, size_t table_size);
172 static bool mdadm_app_cmp(const void *x, const void *y);
173 static void mdadm_app_free(void *x);
175 static int map_app_md_devices (guestfs_h *g, Hash_table **map);
176 static int map_md_devices(guestfs_h *g, Hash_table **map);
178 /* Set fs->product_name to the first line of the release file. */
180 parse_release_file (guestfs_h *g, struct inspect_fs *fs,
181 const char *release_filename)
183 fs->product_name = guestfs___first_line_of_file (g, release_filename);
184 if (fs->product_name == NULL)
189 /* Ubuntu has /etc/lsb-release containing:
190 * DISTRIB_ID=Ubuntu # Distro
191 * DISTRIB_RELEASE=10.04 # Version
192 * DISTRIB_CODENAME=lucid
193 * DISTRIB_DESCRIPTION="Ubuntu 10.04.1 LTS" # Product name
195 * [Ubuntu-derived ...] Linux Mint was found to have this:
196 * DISTRIB_ID=LinuxMint
198 * DISTRIB_CODENAME=julia
199 * DISTRIB_DESCRIPTION="Linux Mint 10 Julia"
200 * Linux Mint also has /etc/linuxmint/info with more information,
201 * but we can use the LSB file.
204 * LSB_VERSION=lsb-4.0-amd64:lsb-4.0-noarch
205 * DISTRIB_ID=MandrivaLinux
206 * DISTRIB_RELEASE=2010.1
207 * DISTRIB_CODENAME=Henry_Farman
208 * DISTRIB_DESCRIPTION="Mandriva Linux 2010.1"
209 * Mandriva also has a normal release file called /etc/mandriva-release.
212 parse_lsb_release (guestfs_h *g, struct inspect_fs *fs)
214 const char *filename = "/etc/lsb-release";
220 /* Don't trust guestfs_head_n not to break with very large files.
221 * Check the file size is something reasonable first.
223 size = guestfs_filesize (g, filename);
225 /* guestfs_filesize failed and has already set error in handle */
227 if (size > MAX_SMALL_FILE_SIZE) {
228 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
233 lines = guestfs_head_n (g, 10, filename);
237 for (i = 0; lines[i] != NULL; ++i) {
238 if (fs->distro == 0 &&
239 STREQ (lines[i], "DISTRIB_ID=Ubuntu")) {
240 fs->distro = OS_DISTRO_UBUNTU;
243 else if (fs->distro == 0 &&
244 STREQ (lines[i], "DISTRIB_ID=LinuxMint")) {
245 fs->distro = OS_DISTRO_LINUX_MINT;
248 else if (fs->distro == 0 &&
249 STREQ (lines[i], "DISTRIB_ID=MandrivaLinux")) {
250 fs->distro = OS_DISTRO_MANDRIVA;
253 else if (fs->distro == 0 &&
254 STREQ (lines[i], "DISTRIB_ID=\"Mageia\"")) {
255 fs->distro = OS_DISTRO_MAGEIA;
258 else if (STRPREFIX (lines[i], "DISTRIB_RELEASE=")) {
260 if (match2 (g, &lines[i][16], re_major_minor, &major, &minor)) {
261 fs->major_version = guestfs___parse_unsigned_int (g, major);
263 if (fs->major_version == -1) {
265 guestfs___free_string_list (lines);
268 fs->minor_version = guestfs___parse_unsigned_int (g, minor);
270 if (fs->minor_version == -1) {
271 guestfs___free_string_list (lines);
276 else if (fs->product_name == NULL &&
277 (STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=\"") ||
278 STRPREFIX (lines[i], "DISTRIB_DESCRIPTION='"))) {
279 size_t len = strlen (lines[i]) - 21 - 1;
280 fs->product_name = safe_strndup (g, &lines[i][21], len);
283 else if (fs->product_name == NULL &&
284 STRPREFIX (lines[i], "DISTRIB_DESCRIPTION=")) {
285 size_t len = strlen (lines[i]) - 20;
286 fs->product_name = safe_strndup (g, &lines[i][20], len);
291 guestfs___free_string_list (lines);
295 /* The currently mounted device is known to be a Linux root. Try to
296 * determine from this the distro, version, etc. Also parse
297 * /etc/fstab to determine the arrangement of mountpoints and
298 * associated devices.
301 guestfs___check_linux_root (guestfs_h *g, struct inspect_fs *fs)
305 fs->type = OS_TYPE_LINUX;
307 if (guestfs_exists (g, "/etc/lsb-release") > 0) {
308 r = parse_lsb_release (g, fs);
309 if (r == -1) /* error */
311 if (r == 1) /* ok - detected the release from this file */
312 goto skip_release_checks;
315 if (guestfs_exists (g, "/etc/redhat-release") > 0) {
316 fs->distro = OS_DISTRO_REDHAT_BASED; /* Something generic Red Hat-like. */
318 if (parse_release_file (g, fs, "/etc/redhat-release") == -1)
322 if ((major = match1 (g, fs->product_name, re_fedora)) != NULL) {
323 fs->distro = OS_DISTRO_FEDORA;
324 fs->major_version = guestfs___parse_unsigned_int (g, major);
326 if (fs->major_version == -1)
329 else if (match2 (g, fs->product_name, re_rhel_old, &major, &minor) ||
330 match2 (g, fs->product_name, re_rhel, &major, &minor)) {
331 fs->distro = OS_DISTRO_RHEL;
332 fs->major_version = guestfs___parse_unsigned_int (g, major);
334 if (fs->major_version == -1) {
338 fs->minor_version = guestfs___parse_unsigned_int (g, minor);
340 if (fs->minor_version == -1)
343 else if ((major = match1 (g, fs->product_name, re_rhel_no_minor)) != NULL) {
344 fs->distro = OS_DISTRO_RHEL;
345 fs->major_version = guestfs___parse_unsigned_int (g, major);
347 if (fs->major_version == -1)
349 fs->minor_version = 0;
351 else if (match2 (g, fs->product_name, re_centos_old, &major, &minor) ||
352 match2 (g, fs->product_name, re_centos, &major, &minor)) {
353 fs->distro = OS_DISTRO_CENTOS;
354 fs->major_version = guestfs___parse_unsigned_int (g, major);
356 if (fs->major_version == -1) {
360 fs->minor_version = guestfs___parse_unsigned_int (g, minor);
362 if (fs->minor_version == -1)
365 else if ((major = match1 (g, fs->product_name, re_centos_no_minor)) != NULL) {
366 fs->distro = OS_DISTRO_CENTOS;
367 fs->major_version = guestfs___parse_unsigned_int (g, major);
369 if (fs->major_version == -1)
371 fs->minor_version = 0;
373 else if (match2 (g, fs->product_name, re_scientific_linux_old, &major, &minor) ||
374 match2 (g, fs->product_name, re_scientific_linux, &major, &minor)) {
375 fs->distro = OS_DISTRO_SCIENTIFIC_LINUX;
376 fs->major_version = guestfs___parse_unsigned_int (g, major);
378 if (fs->major_version == -1) {
382 fs->minor_version = guestfs___parse_unsigned_int (g, minor);
384 if (fs->minor_version == -1)
387 else if ((major = match1 (g, fs->product_name, re_scientific_linux_no_minor)) != NULL) {
388 fs->distro = OS_DISTRO_SCIENTIFIC_LINUX;
389 fs->major_version = guestfs___parse_unsigned_int (g, major);
391 if (fs->major_version == -1)
393 fs->minor_version = 0;
396 else if (guestfs_exists (g, "/etc/debian_version") > 0) {
397 fs->distro = OS_DISTRO_DEBIAN;
399 if (parse_release_file (g, fs, "/etc/debian_version") == -1)
402 if (guestfs___parse_major_minor (g, fs) == -1)
405 else if (guestfs_exists (g, "/etc/pardus-release") > 0) {
406 fs->distro = OS_DISTRO_PARDUS;
408 if (parse_release_file (g, fs, "/etc/pardus-release") == -1)
411 if (guestfs___parse_major_minor (g, fs) == -1)
414 else if (guestfs_exists (g, "/etc/arch-release") > 0) {
415 fs->distro = OS_DISTRO_ARCHLINUX;
417 /* /etc/arch-release file is empty and I can't see a way to
418 * determine the actual release or product string.
421 else if (guestfs_exists (g, "/etc/gentoo-release") > 0) {
422 fs->distro = OS_DISTRO_GENTOO;
424 if (parse_release_file (g, fs, "/etc/gentoo-release") == -1)
427 if (guestfs___parse_major_minor (g, fs) == -1)
430 else if (guestfs_exists (g, "/etc/meego-release") > 0) {
431 fs->distro = OS_DISTRO_MEEGO;
433 if (parse_release_file (g, fs, "/etc/meego-release") == -1)
436 if (guestfs___parse_major_minor (g, fs) == -1)
439 else if (guestfs_exists (g, "/etc/slackware-version") > 0) {
440 fs->distro = OS_DISTRO_SLACKWARE;
442 if (parse_release_file (g, fs, "/etc/slackware-version") == -1)
445 if (guestfs___parse_major_minor (g, fs) == -1)
448 else if (guestfs_exists (g, "/etc/ttylinux-target") > 0) {
449 fs->distro = OS_DISTRO_TTYLINUX;
451 fs->product_name = guestfs___first_line_of_file (g, "/etc/ttylinux-target");
452 if (fs->product_name == NULL)
455 if (guestfs___parse_major_minor (g, fs) == -1)
458 else if (guestfs_exists (g, "/etc/SuSE-release") > 0) {
459 fs->distro = OS_DISTRO_OPENSUSE;
461 if (parse_release_file (g, fs, "/etc/SuSE-release") == -1)
464 if (guestfs___parse_major_minor (g, fs) == -1)
469 skip_release_checks:;
471 /* Determine the architecture. */
472 check_architecture (g, fs);
474 /* We already know /etc/fstab exists because it's part of the test
475 * for Linux root above. We must now parse this file to determine
476 * which filesystems are used by the operating system and how they
479 const char *configfiles[] = { "/etc/fstab", "/etc/mdadm.conf", NULL };
480 if (inspect_with_augeas (g, fs, configfiles, check_fstab) == -1)
483 /* Determine hostname. */
484 if (check_hostname_unix (g, fs) == -1)
490 /* The currently mounted device is known to be a FreeBSD root. */
492 guestfs___check_freebsd_root (guestfs_h *g, struct inspect_fs *fs)
494 fs->type = OS_TYPE_FREEBSD;
496 /* FreeBSD has no authoritative version file. The version number is
497 * in /etc/motd, which the system administrator might edit, but
498 * we'll use that anyway.
501 if (guestfs_exists (g, "/etc/motd") > 0) {
502 if (parse_release_file (g, fs, "/etc/motd") == -1)
505 if (guestfs___parse_major_minor (g, fs) == -1)
509 /* Determine the architecture. */
510 check_architecture (g, fs);
512 /* We already know /etc/fstab exists because it's part of the test above. */
513 const char *configfiles[] = { "/etc/fstab", NULL };
514 if (inspect_with_augeas (g, fs, configfiles, check_fstab) == -1)
517 /* Determine hostname. */
518 if (check_hostname_unix (g, fs) == -1)
524 /* The currently mounted device is maybe to be a *BSD root. */
526 guestfs___check_netbsd_root (guestfs_h *g, struct inspect_fs *fs)
529 if (guestfs_exists (g, "/etc/release") > 0) {
531 if (parse_release_file (g, fs, "/etc/release") == -1)
534 if (match2 (g, fs->product_name, re_netbsd, &major, &minor)) {
535 fs->type = OS_TYPE_NETBSD;
536 fs->major_version = guestfs___parse_unsigned_int (g, major);
538 if (fs->major_version == -1) {
542 fs->minor_version = guestfs___parse_unsigned_int (g, minor);
544 if (fs->minor_version == -1)
551 /* Determine the architecture. */
552 check_architecture (g, fs);
554 /* We already know /etc/fstab exists because it's part of the test above. */
555 const char *configfiles[] = { "/etc/fstab", NULL };
556 if (inspect_with_augeas (g, fs, configfiles, check_fstab) == -1)
559 /* Determine hostname. */
560 if (check_hostname_unix (g, fs) == -1)
566 /* The currently mounted device may be a Hurd root. Hurd has distros
570 guestfs___check_hurd_root (guestfs_h *g, struct inspect_fs *fs)
574 fs->type = OS_TYPE_HURD;
576 if (guestfs_exists (g, "/etc/debian_version") > 0) {
577 fs->distro = OS_DISTRO_DEBIAN;
579 if (parse_release_file (g, fs, "/etc/debian_version") == -1)
582 if (guestfs___parse_major_minor (g, fs) == -1)
586 /* Arch Hurd also exists, but inconveniently it doesn't have
587 * the normal /etc/arch-release file. XXX
590 /* Determine the architecture. */
591 check_architecture (g, fs);
593 /* XXX Check for /etc/fstab. */
595 /* Determine hostname. */
596 if (check_hostname_unix (g, fs) == -1)
603 check_architecture (guestfs_h *g, struct inspect_fs *fs)
605 const char *binaries[] =
606 { "/bin/bash", "/bin/ls", "/bin/echo", "/bin/rm", "/bin/sh" };
609 for (i = 0; i < sizeof binaries / sizeof binaries[0]; ++i) {
610 if (guestfs_is_file (g, binaries[i]) > 0) {
611 /* Ignore errors from file_architecture call. */
612 guestfs_error_handler_cb old_error_cb = g->error_cb;
614 char *arch = guestfs_file_architecture (g, binaries[i]);
615 g->error_cb = old_error_cb;
618 /* String will be owned by handle, freed by
619 * guestfs___free_inspect_info.
628 /* Try several methods to determine the hostname from a Linux or
629 * FreeBSD guest. Note that type and distro have been set, so we can
630 * use that information to direct the search.
633 check_hostname_unix (guestfs_h *g, struct inspect_fs *fs)
638 /* Red Hat-derived would be in /etc/sysconfig/network, and
639 * Debian-derived in the file /etc/hostname. Very old Debian and
640 * SUSE use /etc/HOSTNAME. It's best to just look for each of
641 * these files in turn, rather than try anything clever based on
644 if (guestfs_is_file (g, "/etc/HOSTNAME")) {
645 fs->hostname = guestfs___first_line_of_file (g, "/etc/HOSTNAME");
646 if (fs->hostname == NULL)
649 else if (guestfs_is_file (g, "/etc/hostname")) {
650 fs->hostname = guestfs___first_line_of_file (g, "/etc/hostname");
651 if (fs->hostname == NULL)
654 else if (guestfs_is_file (g, "/etc/sysconfig/network")) {
655 const char *configfiles[] = { "/etc/sysconfig/network", NULL };
656 if (inspect_with_augeas (g, fs, configfiles,
657 check_hostname_redhat) == -1)
662 case OS_TYPE_FREEBSD:
664 /* /etc/rc.conf contains the hostname, but there is no Augeas lens
667 if (guestfs_is_file (g, "/etc/rc.conf")) {
668 if (check_hostname_freebsd (g, fs) == -1)
673 case OS_TYPE_WINDOWS: /* not here, see check_windows_system_registry */
674 case OS_TYPE_UNKNOWN:
676 /* nothing, keep GCC warnings happy */;
682 /* Parse the hostname from /etc/sysconfig/network. This must be called
683 * from the inspect_with_augeas wrapper.
686 check_hostname_redhat (guestfs_h *g, struct inspect_fs *fs)
690 /* Errors here are not fatal (RHBZ#726739), since it could be
691 * just missing HOSTNAME field in the file.
693 guestfs_error_handler_cb old_error_cb = g->error_cb;
695 hostname = guestfs_aug_get (g, "/files/etc/sysconfig/network/HOSTNAME");
696 g->error_cb = old_error_cb;
698 /* This is freed by guestfs___free_inspect_info. Note that hostname
699 * could be NULL because we ignored errors above.
701 fs->hostname = hostname;
705 /* Parse the hostname from /etc/rc.conf. On FreeBSD this file
706 * contains comments, blank lines and:
707 * hostname="freebsd8.example.com"
708 * ifconfig_re0="DHCP"
713 check_hostname_freebsd (guestfs_h *g, struct inspect_fs *fs)
715 const char *filename = "/etc/rc.conf";
720 /* Don't trust guestfs_read_lines not to break with very large files.
721 * Check the file size is something reasonable first.
723 size = guestfs_filesize (g, filename);
725 /* guestfs_filesize failed and has already set error in handle */
727 if (size > MAX_SMALL_FILE_SIZE) {
728 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
733 lines = guestfs_read_lines (g, filename);
737 for (i = 0; lines[i] != NULL; ++i) {
738 if (STRPREFIX (lines[i], "hostname=\"") ||
739 STRPREFIX (lines[i], "hostname='")) {
740 size_t len = strlen (lines[i]) - 10 - 1;
741 fs->hostname = safe_strndup (g, &lines[i][10], len);
743 } else if (STRPREFIX (lines[i], "hostname=")) {
744 size_t len = strlen (lines[i]) - 9;
745 fs->hostname = safe_strndup (g, &lines[i][9], len);
750 guestfs___free_string_list (lines);
755 check_fstab (guestfs_h *g, struct inspect_fs *fs)
757 char **entries, **entry;
762 /* Generate a map of MD device paths listed in /etc/mdadm.conf to MD device
763 * paths in the guestfs appliance */
765 if (map_md_devices (g, &md_map) == -1) return -1;
767 entries = guestfs_aug_match (g, "/files/etc/fstab/*[label() != '#comment']");
768 if (entries == NULL) goto error;
770 if (entries[0] == NULL) {
771 error (g, _("could not parse /etc/fstab or empty file"));
775 for (entry = entries; *entry != NULL; entry++) {
776 snprintf (augpath, sizeof augpath, "%s/spec", *entry);
777 spec = guestfs_aug_get (g, augpath);
778 if (spec == NULL) goto error;
780 snprintf (augpath, sizeof augpath, "%s/file", *entry);
781 mp = guestfs_aug_get (g, augpath);
787 r = add_fstab_entry (g, fs, spec, mp, md_map);
791 if (r == -1) goto error;
794 if (md_map) hash_free (md_map);
795 guestfs___free_string_list (entries);
799 if (md_map) hash_free (md_map);
800 if (entries) guestfs___free_string_list (entries);
804 /* Add a filesystem and possibly a mountpoint entry for
805 * the root filesystem 'fs'.
807 * 'spec' is the fstab spec field, which might be a device name or a
808 * pseudodevice or 'UUID=...' or 'LABEL=...'.
810 * 'mp' is the mount point, which could also be 'swap' or 'none'.
813 add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
814 const char *spec, const char *mp, Hash_table *md_map)
816 /* Ignore certain mountpoints. */
817 if (STRPREFIX (mp, "/dev/") ||
818 STREQ (mp, "/dev") ||
819 STRPREFIX (mp, "/media/") ||
820 STRPREFIX (mp, "/proc/") ||
821 STREQ (mp, "/proc") ||
822 STRPREFIX (mp, "/selinux/") ||
823 STREQ (mp, "/selinux") ||
824 STRPREFIX (mp, "/sys/") ||
828 /* Ignore /dev/fd (floppy disks) (RHBZ#642929) and CD-ROM drives. */
829 if ((STRPREFIX (spec, "/dev/fd") && c_isdigit (spec[7])) ||
830 STREQ (spec, "/dev/floppy") ||
831 STREQ (spec, "/dev/cdrom"))
834 /* Resolve UUID= and LABEL= to the actual device. */
836 if (STRPREFIX (spec, "UUID="))
837 device = guestfs_findfs_uuid (g, &spec[5]);
838 else if (STRPREFIX (spec, "LABEL="))
839 device = guestfs_findfs_label (g, &spec[6]);
840 /* Ignore "/.swap" (Pardus) and pseudo-devices like "tmpfs". */
841 else if (STREQ (spec, "/dev/root"))
842 /* Resolve /dev/root to the current device. */
843 device = safe_strdup (g, fs->device);
844 else if (STRPREFIX (spec, "/dev/"))
845 /* Resolve guest block device names. */
846 device = resolve_fstab_device (g, spec, md_map);
848 /* If we haven't resolved the device successfully by this point,
849 * we don't care, just ignore it.
854 char *mountpoint = safe_strdup (g, mp);
856 /* Add this to the fstab entry in 'fs'.
857 * Note these are further filtered by guestfs_inspect_get_mountpoints
858 * and guestfs_inspect_get_filesystems.
860 size_t n = fs->nr_fstab + 1;
861 struct inspect_fstab_entry *p;
863 p = realloc (fs->fstab, n * sizeof (struct inspect_fstab_entry));
865 perrorf (g, "realloc");
874 /* These are owned by the handle and freed by guestfs___free_inspect_info. */
875 fs->fstab[n-1].device = device;
876 fs->fstab[n-1].mountpoint = mountpoint;
878 debug (g, "fstab: device=%s mountpoint=%s", device, mountpoint);
883 /* Compute a uuid hash as a simple xor of of its 4 32bit components */
885 uuid_hash(const void *x, size_t table_size)
887 const md_uuid *a = x;
889 size_t h = a->uuid[0];
890 for (size_t i = 1; i < 4; i++) {
894 return h % table_size;
898 uuid_cmp(const void *x, const void *y)
900 const md_uuid *a = x;
901 const md_uuid *b = y;
903 for (size_t i = 0; i < 1; i++) {
904 if (a->uuid[i] != b->uuid[i]) return false;
911 md_uuid_free(void *x)
918 /* Taken from parse_uuid in mdadm */
920 parse_uuid(const char *str, uint32_t *uuid)
922 for (size_t i = 0; i < 4; i++) uuid[i] = 0;
924 int hit = 0; /* number of Hex digIT */
926 while ((c = *str++)) {
928 if (c >= '0' && c <= '9')
930 else if (c >= 'a' && c <= 'f')
932 else if (c >= 'A' && c <= 'F')
934 else if (strchr(":. -", c))
944 if (hit == 32) return 0;
949 /* Create a mapping of uuids to appliance md device names */
951 map_app_md_devices (guestfs_h *g, Hash_table **map)
956 /* A hash mapping uuids to md device names */
957 *map = hash_initialize(16, NULL, uuid_hash, uuid_cmp, md_uuid_free);
958 if (*map == NULL) g->abort_cb();
960 mds = guestfs_list_md_devices(g);
961 if (mds == NULL) goto error;
963 for (char **md = mds; *md != NULL; md++) {
964 char **detail = guestfs_md_detail(g, *md);
965 if (detail == NULL) goto error;
967 /* Iterate over keys until we find uuid */
969 for (i = detail; *i != NULL; i += 2) {
970 if (STREQ(*i, "uuid")) break;
975 /* Next item is the uuid value */
978 md_uuid *entry = safe_malloc(g, sizeof(md_uuid));
979 entry->path = safe_strdup(g, *md);
981 if (parse_uuid(*i, entry->uuid) == -1) {
982 /* Invalid UUID is weird, but not fatal. */
983 debug(g, "inspect-os: guestfs_md_detail returned invalid "
984 "uuid for %s: %s", *md, *i);
985 guestfs___free_string_list(detail);
990 const void *matched = NULL;
991 switch (hash_insert_if_absent(*map, entry, &matched)) {
996 /* Duplicate uuid in for md device is weird, but not fatal. */
997 debug(g, "inspect-os: md devices %s and %s have the same uuid",
998 ((md_uuid *)matched)->path, entry->path);
1007 guestfs___free_string_list(detail);
1010 guestfs___free_string_list(mds);
1015 hash_free(*map); *map = NULL;
1016 guestfs___free_string_list(mds);
1022 mdadm_app_hash(const void *x, size_t table_size)
1024 const mdadm_app *a = x;
1025 return hash_pjw(a->mdadm, table_size);
1029 mdadm_app_cmp(const void *x, const void *y)
1031 const mdadm_app *a = x;
1032 const mdadm_app *b = y;
1034 return strcmp(a->mdadm, b->mdadm) == 0;
1038 mdadm_app_free(void *x)
1046 /* Get a map of md device names in mdadm.conf to their device names in the
1049 map_md_devices(guestfs_h *g, Hash_table **map)
1051 Hash_table *app_map = NULL;
1052 char **matches = NULL;
1055 /* Get a map of md device uuids to their device names in the appliance */
1056 int n_app_md_devices = map_app_md_devices(g, &app_map);
1057 if (n_app_md_devices == -1) goto error;
1059 /* Nothing to do if there are no md devices */
1060 if (n_app_md_devices == 0) {
1065 /* Get all arrays listed in mdadm.conf */
1066 matches = guestfs_aug_match(g, "/files/etc/mdadm.conf/array");
1067 if (!matches) goto error;
1069 /* Log a debug message if we've got md devices, but nothing in mdadm.conf */
1070 if (matches[0] == NULL) {
1071 debug(g, "Appliance has MD devices, but augeas returned no array matches "
1073 guestfs___free_string_list(matches);
1078 *map = hash_initialize(16, NULL, mdadm_app_hash, mdadm_app_cmp,
1080 if (!*map) g->abort_cb();
1082 for (char **match = matches; *match != NULL; match++) {
1083 /* Get device name and uuid for each array */
1084 char *dev_path = safe_asprintf(g, "%s/devicename", *match);
1085 char *dev = guestfs_aug_get(g, dev_path);
1087 if (!dev) goto error;
1089 char *uuid_path = safe_asprintf(g, "%s/uuid", *match);
1090 char *uuid = guestfs_aug_get(g, uuid_path);
1097 /* Parse the uuid into an md_uuid structure so we can look it up in the
1098 * uuid->appliance device map */
1101 if (parse_uuid(uuid, mdadm.uuid) == -1) {
1102 /* Invalid uuid. Weird, but not fatal. */
1103 debug(g, "inspect-os: mdadm.conf contains invalid uuid for %s: %s",
1111 /* If there's a corresponding uuid in the appliance, create a new
1112 * entry in the transitive map */
1113 md_uuid *app = hash_lookup(app_map, &mdadm);
1115 mdadm_app *entry = safe_malloc(g, sizeof(mdadm_app));
1117 entry->app = safe_strdup(g, app->path);
1119 switch (hash_insert_if_absent(*map, entry, NULL)) {
1124 /* Duplicate uuid in for md device is weird, but not fatal. */
1125 debug(g, "inspect-os: mdadm.conf contains multiple entries for %s",
1127 mdadm_app_free(entry);
1139 guestfs___free_string_list(matches);
1144 if (app_map) hash_free(app_map);
1145 if (matches) guestfs___free_string_list(matches);
1146 if (*map) hash_free(*map);
1151 /* Resolve block device name to the libguestfs device name, eg.
1152 * /dev/xvdb1 => /dev/vdb1; and /dev/mapper/VG-LV => /dev/VG/LV. This
1153 * assumes that disks were added in the same order as they appear to
1154 * the real VM, which is a reasonable assumption to make. Return
1155 * anything we don't recognize unchanged.
1158 resolve_fstab_device (guestfs_h *g, const char *spec, Hash_table *md_map)
1160 char *device = NULL;
1161 char *type, *slice, *disk, *part;
1163 if (STRPREFIX (spec, "/dev/mapper/")) {
1164 /* LVM2 does some strange munging on /dev/mapper paths for VGs and
1165 * LVs which contain '-' character:
1167 * ><fs> lvcreate LV--test VG--test 32
1168 * ><fs> debug ls /dev/mapper
1169 * VG----test-LV----test
1171 * This makes it impossible to reverse those paths directly, so
1172 * we have implemented lvm_canonical_lv_name in the daemon.
1174 device = guestfs_lvm_canonical_lv_name (g, spec);
1176 else if (match3 (g, spec, re_xdev, &type, &disk, &part)) {
1180 char **devices = guestfs_list_devices (g);
1181 if (devices == NULL)
1184 /* Check any hints we were passed for a non-heuristic mapping */
1185 char *name = safe_asprintf (g, "%sd%s", type, disk);
1187 struct drive *drive = g->drives;
1189 if (drive->name && STREQ(drive->name, name)) {
1190 device = safe_asprintf (g, "%s%s", devices[i], part);
1194 i++; drive = drive->next;
1198 /* Guess the appliance device name if we didn't find a matching hint */
1200 /* Count how many disks the libguestfs appliance has */
1202 for (count = 0; devices[count] != NULL; count++)
1205 /* Calculate the numerical index of the disk */
1207 for (char *p = disk + 1; *p != '\0'; p++) {
1212 /* Check the index makes sense wrt the number of disks the appliance has.
1213 * If it does, map it to an appliance disk. */
1215 device = safe_asprintf (g, "%s%s", devices[i], part);
1222 guestfs___free_string_list (devices);
1224 else if (match2 (g, spec, re_cciss, &disk, &part)) {
1225 /* disk: (cciss/c\d+d\d+)
1227 char **devices = guestfs_list_devices (g);
1228 if (devices == NULL)
1231 /* Check any hints we were passed for a non-heuristic mapping */
1233 struct drive *drive = g->drives;
1235 if (drive->name && STREQ(drive->name, disk)) {
1237 device = safe_asprintf (g, "%s%s", devices[i], part);
1239 device = safe_strdup (g, devices[i]);
1244 i++; drive = drive->next;
1247 /* We don't try to guess mappings for cciss devices */
1251 guestfs___free_string_list (devices);
1253 else if (md_map && (disk = match1 (g, spec, re_mdN)) != NULL) {
1257 mdadm_app *app = hash_lookup (md_map, &entry);
1258 if (app) device = safe_strdup (g, app->app);
1262 else if (match3 (g, spec, re_freebsd, &disk, &slice, &part)) {
1263 /* FreeBSD disks are organized quite differently. See:
1264 * http://www.freebsd.org/doc/handbook/disk-organization.html
1265 * FreeBSD "partitions" are exposed as quasi-extended partitions
1266 * numbered from 5 in Linux. I have no idea what happens when you
1267 * have multiple "slices" (the FreeBSD term for MBR partitions).
1269 int disk_i = guestfs___parse_unsigned_int (g, disk);
1270 int slice_i = guestfs___parse_unsigned_int (g, slice);
1271 int part_i = part[0] - 'a' /* counting from 0 */;
1276 if (disk_i != -1 && disk_i <= 26 &&
1277 slice_i > 0 && slice_i <= 1 /* > 4 .. see comment above */ &&
1278 part_i >= 0 && part_i < 26) {
1279 device = safe_asprintf (g, "/dev/sd%c%d", disk_i + 'a', part_i + 5);
1283 /* Didn't match device pattern, return original spec unchanged. */
1285 device = safe_strdup (g, spec);
1290 /* Call 'f' with Augeas opened and having parsed 'filename' (this file
1291 * must exist). As a security measure, this bails if the file is too
1292 * large for a reasonable configuration file. After the call to 'f'
1296 inspect_with_augeas (guestfs_h *g, struct inspect_fs *fs,
1297 const char **configfiles,
1298 int (*f) (guestfs_h *, struct inspect_fs *))
1300 /* Security: Refuse to do this if a config file is too large. */
1301 for (const char **i = configfiles; *i != NULL; i++) {
1302 if (guestfs_exists(g, *i) == 0) continue;
1304 int64_t size = guestfs_filesize (g, *i);
1306 /* guestfs_filesize failed and has already set error in handle */
1308 if (size > MAX_AUGEAS_FILE_SIZE) {
1309 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
1315 /* If !feature_available (g, "augeas") then the next call will fail.
1316 * Arguably we might want to fall back to a non-Augeas method in
1319 if (guestfs_aug_init (g, "/", 16|32) == -1)
1324 /* Tell Augeas to only load one file (thanks Raphaƫl Pinson). */
1325 #define AUGEAS_LOAD "/augeas/load//incl[. != \""
1326 #define AUGEAS_LOAD_LEN (strlen(AUGEAS_LOAD))
1327 size_t conflen = strlen(configfiles[0]);
1328 size_t buflen = AUGEAS_LOAD_LEN + conflen + 1 /* Closing " */;
1329 char *buf = safe_malloc(g, buflen + 2 /* Closing ] + null terminator */);
1331 memcpy(buf, AUGEAS_LOAD, AUGEAS_LOAD_LEN);
1332 memcpy(buf + AUGEAS_LOAD_LEN, configfiles[0], conflen);
1333 buf[buflen - 1] = '"';
1334 #undef AUGEAS_LOAD_LEN
1337 #define EXCL " and . != \""
1338 #define EXCL_LEN (strlen(EXCL))
1339 for (const char **i = &configfiles[1]; *i != NULL; i++) {
1340 size_t orig_buflen = buflen;
1341 conflen = strlen(*i);
1342 buflen += EXCL_LEN + conflen + 1 /* Closing " */;
1343 buf = safe_realloc(g, buf, buflen + 2 /* Closing ] + null terminator */);
1344 char *s = buf + orig_buflen;
1346 memcpy(s, EXCL, EXCL_LEN);
1347 memcpy(s + EXCL_LEN, *i, conflen);
1348 buf[buflen - 1] = '"';
1354 buf[buflen + 1] = '\0';
1356 if (guestfs_aug_rm (g, buf) == -1) {
1362 if (guestfs_aug_load (g) == -1)
1368 guestfs_aug_close (g);
1373 #endif /* defined(HAVE_HIVEX) */