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
35 #include "ignore-value.h"
39 #include "guestfs-internal.h"
40 #include "guestfs-internal-actions.h"
41 #include "guestfs_protocol.h"
43 /* Compile all the regular expressions once when the shared library is
44 * loaded. PCRE is thread safe so we're supposedly OK here if
45 * multiple threads call into the libguestfs API functions below
48 static pcre *re_file_elf;
49 static pcre *re_file_win64;
50 static pcre *re_elf_ppc64;
51 static pcre *re_fedora;
52 static pcre *re_rhel_old;
54 static pcre *re_rhel_no_minor;
55 static pcre *re_debian;
56 static pcre *re_aug_seq;
58 static pcre *re_windows_version;
60 static void compile_regexps (void) __attribute__((constructor));
61 static void free_regexps (void) __attribute__((destructor));
64 compile_regexps (void)
69 #define COMPILE(re,pattern,options) \
71 re = pcre_compile ((pattern), (options), &err, &offset, NULL); \
73 ignore_value (write (2, err, strlen (err))); \
79 "ELF.*(?:executable|shared object|relocatable), (.+?),", 0);
80 COMPILE (re_elf_ppc64, "64.*PowerPC", 0);
81 COMPILE (re_fedora, "Fedora release (\\d+)", 0);
83 "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+).*Update (\\d+)", 0);
85 "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+)\\.(\\d+)", 0);
86 COMPILE (re_rhel_no_minor,
87 "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+)", 0);
88 COMPILE (re_debian, "(\\d+)\\.(\\d+)", 0);
89 COMPILE (re_aug_seq, "/\\d+$", 0);
90 COMPILE (re_xdev, "^/dev/(?:h|s|v|xv)d([a-z]\\d*)$", 0);
91 COMPILE (re_windows_version, "^(\\d+)\\.(\\d+)", 0);
97 pcre_free (re_file_elf);
98 pcre_free (re_file_win64);
99 pcre_free (re_elf_ppc64);
100 pcre_free (re_fedora);
101 pcre_free (re_rhel_old);
103 pcre_free (re_rhel_no_minor);
104 pcre_free (re_debian);
105 pcre_free (re_aug_seq);
107 pcre_free (re_windows_version);
110 /* Match a regular expression which contains no captures. Returns
111 * true if it matches or false if it doesn't.
114 match (guestfs_h *g, const char *str, const pcre *re)
116 size_t len = strlen (str);
119 r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
120 if (r == PCRE_ERROR_NOMATCH)
123 /* Internal error -- should not happen. */
124 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
125 __FILE__, __func__, r, str);
132 /* Match a regular expression which contains exactly one capture. If
133 * the string matches, return the capture, otherwise return NULL. The
134 * caller must free the result.
137 match1 (guestfs_h *g, const char *str, const pcre *re)
139 size_t len = strlen (str);
142 r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
143 if (r == PCRE_ERROR_NOMATCH)
146 /* Internal error -- should not happen. */
147 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
148 __FILE__, __func__, r, str);
152 return safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
155 /* Match a regular expression which contains exactly two captures. */
157 match2 (guestfs_h *g, const char *str, const pcre *re, char **ret1, char **ret2)
159 size_t len = strlen (str);
162 r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
163 if (r == PCRE_ERROR_NOMATCH)
166 /* Internal error -- should not happen. */
167 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
168 __FILE__, __func__, r, str);
172 *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
173 *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
178 /* Convert output from 'file' command on ELF files to the canonical
179 * architecture string. Caller must free the result.
182 canonical_elf_arch (guestfs_h *g, const char *elf_arch)
186 if (strstr (elf_arch, "Intel 80386"))
188 else if (strstr (elf_arch, "Intel 80486"))
190 else if (strstr (elf_arch, "x86-64"))
192 else if (strstr (elf_arch, "AMD x86-64"))
194 else if (strstr (elf_arch, "SPARC32"))
196 else if (strstr (elf_arch, "SPARC V9"))
198 else if (strstr (elf_arch, "IA-64"))
200 else if (match (g, elf_arch, re_elf_ppc64))
202 else if (strstr (elf_arch, "PowerPC"))
207 char *ret = safe_strdup (g, r);
212 is_regular_file (const char *filename)
216 return lstat (filename, &statbuf) == 0 && S_ISREG (statbuf.st_mode);
219 /* Download and uncompress the cpio file to find binaries within.
221 * (1) Two lists must be identical.
222 * (2) Implicit limit of 31 bytes for length of each element (see code
225 #define INITRD_BINARIES1 "bin/ls bin/rm bin/modprobe sbin/modprobe bin/sh bin/bash bin/dash bin/nash"
226 #define INITRD_BINARIES2 {"bin/ls", "bin/rm", "bin/modprobe", "sbin/modprobe", "bin/sh", "bin/bash", "bin/dash", "bin/nash"}
229 cpio_arch (guestfs_h *g, const char *file, const char *path)
231 TMP_TEMPLATE_ON_STACK (dir);
232 #define dir_len (strlen (dir))
233 #define initrd_len (dir_len + 16)
234 char initrd[initrd_len];
235 #define cmd_len (dir_len + 256)
237 #define bin_len (dir_len + 32)
243 if (strstr (file, "gzip"))
245 else if (strstr (file, "bzip2"))
250 if (mkdtemp (dir) == NULL) {
251 perrorf (g, "mkdtemp");
255 snprintf (initrd, initrd_len, "%s/initrd", dir);
256 if (guestfs_download (g, path, initrd) == -1)
259 snprintf (cmd, cmd_len,
260 "cd %s && %s initrd | cpio --quiet -id " INITRD_BINARIES1,
262 int r = system (cmd);
263 if (r == -1 || WEXITSTATUS (r) != 0) {
264 perrorf (g, "cpio command failed");
268 const char *bins[] = INITRD_BINARIES2;
270 for (i = 0; i < sizeof bins / sizeof bins[0]; ++i) {
271 snprintf (bin, bin_len, "%s/%s", dir, bins[i]);
273 if (is_regular_file (bin)) {
274 int flags = g->verbose ? MAGIC_DEBUG : 0;
275 flags |= MAGIC_ERROR | MAGIC_RAW;
277 magic_t m = magic_open (flags);
279 perrorf (g, "magic_open");
283 if (magic_load (m, NULL) == -1) {
284 perrorf (g, "magic_load: default magic database file");
289 const char *line = magic_file (m, bin);
291 perrorf (g, "magic_file: %s", bin);
297 if ((elf_arch = match1 (g, line, re_file_elf)) != NULL) {
298 ret = canonical_elf_arch (g, elf_arch);
306 error (g, "file_architecture: could not determine architecture of cpio archive");
309 /* Free up the temporary directory. Note the directory name cannot
310 * contain shell meta-characters because of the way it was
313 snprintf (cmd, cmd_len, "rm -rf %s", dir);
314 ignore_value (system (cmd));
324 guestfs__file_architecture (guestfs_h *g, const char *path)
327 char *elf_arch = NULL;
330 /* Get the output of the "file" command. Note that because this
331 * runs in the daemon, LANG=C so it's in English.
333 file = guestfs_file (g, path);
337 if ((elf_arch = match1 (g, file, re_file_elf)) != NULL)
338 ret = canonical_elf_arch (g, elf_arch);
339 else if (strstr (file, "PE32 executable"))
340 ret = safe_strdup (g, "i386");
341 else if (strstr (file, "PE32+ executable"))
342 ret = safe_strdup (g, "x86_64");
343 else if (strstr (file, "cpio archive"))
344 ret = cpio_arch (g, file, path);
346 error (g, "file_architecture: unknown architecture: %s", path);
350 return ret; /* caller frees */
353 /* The main inspection code. */
354 static int feature_available (guestfs_h *g, const char *feature);
355 static void free_string_list (char **);
356 static int check_for_filesystem_on (guestfs_h *g, const char *device);
359 guestfs__inspect_os (guestfs_h *g)
361 /* Remove any information previously stored in the handle. */
362 guestfs___free_inspect_info (g);
364 if (guestfs_umount_all (g) == -1)
367 /* Iterate over all possible devices. Try to mount each
368 * (read-only). Examine ones which contain filesystems and add that
369 * information to the handle.
371 /* Look to see if any devices directly contain filesystems (RHBZ#590167). */
373 devices = guestfs_list_devices (g);
378 for (i = 0; devices[i] != NULL; ++i) {
379 if (check_for_filesystem_on (g, devices[i]) == -1) {
380 free_string_list (devices);
381 guestfs___free_inspect_info (g);
385 free_string_list (devices);
387 /* Look at all partitions. */
389 partitions = guestfs_list_partitions (g);
390 if (partitions == NULL) {
391 guestfs___free_inspect_info (g);
395 for (i = 0; partitions[i] != NULL; ++i) {
396 if (check_for_filesystem_on (g, partitions[i]) == -1) {
397 free_string_list (partitions);
398 guestfs___free_inspect_info (g);
402 free_string_list (partitions);
404 /* Look at all LVs. */
405 if (feature_available (g, "lvm2")) {
407 lvs = guestfs_lvs (g);
409 guestfs___free_inspect_info (g);
413 for (i = 0; lvs[i] != NULL; ++i) {
414 if (check_for_filesystem_on (g, lvs[i]) == -1) {
415 free_string_list (lvs);
416 guestfs___free_inspect_info (g);
420 free_string_list (lvs);
423 /* At this point we have, in the handle, a list of all filesystems
424 * found and data about each one. Now we assemble the list of
425 * filesystems which are root devices and return that to the user.
428 for (i = 0; i < g->nr_fses; ++i)
429 if (g->fses[i].is_root)
432 char **ret = calloc (count+1, sizeof (char *));
434 perrorf (g, "calloc");
435 guestfs___free_inspect_info (g);
440 for (i = 0; i < g->nr_fses; ++i) {
441 if (g->fses[i].is_root) {
442 ret[count] = safe_strdup (g, g->fses[i].device);
452 guestfs___free_inspect_info (guestfs_h *g)
455 for (i = 0; i < g->nr_fses; ++i) {
456 free (g->fses[i].device);
457 free (g->fses[i].product_name);
458 free (g->fses[i].arch);
460 for (j = 0; j < g->fses[i].nr_fstab; ++j) {
461 free (g->fses[i].fstab[j].device);
462 free (g->fses[i].fstab[j].mountpoint);
464 free (g->fses[i].fstab);
472 free_string_list (char **argv)
475 for (i = 0; argv[i] != NULL; ++i)
480 /* In the Perl code this is a public function. */
482 feature_available (guestfs_h *g, const char *feature)
484 /* If there's an error we should ignore it, so to do that we have to
485 * temporarily replace the error handler with a null one.
487 guestfs_error_handler_cb old_error_cb = g->error_cb;
490 const char *groups[] = { feature, NULL };
491 int r = guestfs_available (g, (char * const *) groups);
493 g->error_cb = old_error_cb;
495 return r == 0 ? 1 : 0;
498 /* Find out if 'device' contains a filesystem. If it does, add
499 * another entry in g->fses.
501 static int check_filesystem (guestfs_h *g, const char *device);
502 static int check_linux_root (guestfs_h *g, struct inspect_fs *fs);
503 static int check_fstab (guestfs_h *g, struct inspect_fs *fs);
504 static int check_windows_root (guestfs_h *g, struct inspect_fs *fs);
505 static int check_windows_arch (guestfs_h *g, struct inspect_fs *fs,
506 const char *systemroot);
507 static int check_windows_registry (guestfs_h *g, struct inspect_fs *fs,
508 const char *systemroot);
509 static char *resolve_windows_path_silently (guestfs_h *g, const char *);
510 static int extend_fses (guestfs_h *g);
511 static int parse_unsigned_int (guestfs_h *g, const char *str);
512 static int add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
513 const char *spec, const char *mp);
514 static char *resolve_fstab_device (guestfs_h *g, const char *spec);
517 check_for_filesystem_on (guestfs_h *g, const char *device)
519 /* Get vfs-type in order to check if it's a Linux(?) swap device.
520 * If there's an error we should ignore it, so to do that we have to
521 * temporarily replace the error handler with a null one.
523 guestfs_error_handler_cb old_error_cb = g->error_cb;
525 char *vfs_type = guestfs_vfs_type (g, device);
526 g->error_cb = old_error_cb;
528 int is_swap = vfs_type && STREQ (vfs_type, "swap");
531 fprintf (stderr, "check_for_filesystem_on: %s (%s)\n",
532 device, vfs_type ? vfs_type : "failed to get vfs type");
536 if (extend_fses (g) == -1)
538 g->fses[g->nr_fses-1].is_swap = 1;
542 /* Try mounting the device. As above, ignore errors. */
544 int r = guestfs_mount_ro (g, device, "/");
545 if (r == -1 && vfs_type && STREQ (vfs_type, "ufs")) /* Hack for the *BSDs. */
546 r = guestfs_mount_vfs (g, "ro,ufstype=ufs2", "ufs", device, "/");
548 g->error_cb = old_error_cb;
552 /* Do the rest of the checks. */
553 r = check_filesystem (g, device);
555 /* Unmount the filesystem. */
556 if (guestfs_umount_all (g) == -1)
563 check_filesystem (guestfs_h *g, const char *device)
565 if (extend_fses (g) == -1)
568 struct inspect_fs *fs = &g->fses[g->nr_fses-1];
570 fs->device = safe_strdup (g, device);
571 fs->is_mountable = 1;
574 if (guestfs_is_file (g, "/grub/menu.lst") > 0 ||
575 guestfs_is_file (g, "/grub/grub.conf") > 0)
576 fs->content = FS_CONTENT_LINUX_BOOT;
578 else if (guestfs_is_dir (g, "/etc") > 0 &&
579 guestfs_is_dir (g, "/bin") > 0 &&
580 guestfs_is_file (g, "/etc/fstab") > 0) {
582 fs->content = FS_CONTENT_LINUX_ROOT;
583 if (check_linux_root (g, fs) == -1)
586 /* Linux /usr/local? */
587 else if (guestfs_is_dir (g, "/etc") > 0 &&
588 guestfs_is_dir (g, "/bin") > 0 &&
589 guestfs_is_dir (g, "/share") > 0 &&
590 guestfs_exists (g, "/local") == 0 &&
591 guestfs_is_file (g, "/etc/fstab") == 0)
592 fs->content = FS_CONTENT_LINUX_USR_LOCAL;
594 else if (guestfs_is_dir (g, "/etc") > 0 &&
595 guestfs_is_dir (g, "/bin") > 0 &&
596 guestfs_is_dir (g, "/share") > 0 &&
597 guestfs_exists (g, "/local") > 0 &&
598 guestfs_is_file (g, "/etc/fstab") == 0)
599 fs->content = FS_CONTENT_LINUX_USR;
601 else if (guestfs_is_dir (g, "/log") > 0 &&
602 guestfs_is_dir (g, "/run") > 0 &&
603 guestfs_is_dir (g, "/spool") > 0)
604 fs->content = FS_CONTENT_LINUX_VAR;
606 else if (guestfs_is_file (g, "/AUTOEXEC.BAT") > 0 ||
607 guestfs_is_file (g, "/autoexec.bat") > 0 ||
608 guestfs_is_dir (g, "/Program Files") > 0 ||
609 guestfs_is_dir (g, "/WINDOWS") > 0 ||
610 guestfs_is_dir (g, "/Windows") > 0 ||
611 guestfs_is_dir (g, "/windows") > 0 ||
612 guestfs_is_dir (g, "/WIN32") > 0 ||
613 guestfs_is_dir (g, "/Win32") > 0 ||
614 guestfs_is_dir (g, "/WINNT") > 0 ||
615 guestfs_is_file (g, "/boot.ini") > 0 ||
616 guestfs_is_file (g, "/ntldr") > 0) {
618 fs->content = FS_CONTENT_WINDOWS_ROOT;
619 if (check_windows_root (g, fs) == -1)
626 /* The currently mounted device is known to be a Linux root. Try to
627 * determine from this the distro, version, etc. Also parse
628 * /etc/fstab to determine the arrangement of mountpoints and
629 * associated devices.
632 check_linux_root (guestfs_h *g, struct inspect_fs *fs)
634 fs->type = OS_TYPE_LINUX;
636 if (guestfs_exists (g, "/etc/redhat-release") > 0) {
637 fs->distro = OS_DISTRO_REDHAT_BASED; /* Something generic Red Hat-like. */
639 char **product_name = guestfs_head_n (g, 1, "/etc/redhat-release");
640 if (product_name == NULL)
642 if (product_name[0] == NULL) {
643 error (g, "/etc/redhat-release file is empty");
644 free_string_list (product_name);
648 /* Note that this string becomes owned by the handle and will
649 * be freed by guestfs___free_inspect_info.
651 fs->product_name = product_name[0];
655 if ((major = match1 (g, fs->product_name, re_fedora)) != NULL) {
656 fs->distro = OS_DISTRO_FEDORA;
657 fs->major_version = parse_unsigned_int (g, major);
659 if (fs->major_version == -1)
662 else if (match2 (g, fs->product_name, re_rhel_old, &major, &minor) ||
663 match2 (g, fs->product_name, re_rhel, &major, &minor)) {
664 fs->distro = OS_DISTRO_RHEL;
665 fs->major_version = parse_unsigned_int (g, major);
667 if (fs->major_version == -1) {
671 fs->minor_version = parse_unsigned_int (g, minor);
673 if (fs->minor_version == -1)
676 else if ((major = match1 (g, fs->product_name, re_rhel_no_minor)) != NULL) {
677 fs->distro = OS_DISTRO_RHEL;
678 fs->major_version = parse_unsigned_int (g, major);
680 if (fs->major_version == -1)
682 fs->minor_version = 0;
685 else if (guestfs_exists (g, "/etc/debian_version") > 0) {
686 fs->distro = OS_DISTRO_DEBIAN;
688 char **product_name = guestfs_head_n (g, 1, "/etc/debian_version");
689 if (product_name == NULL)
691 if (product_name[0] == NULL) {
692 error (g, "/etc/debian_version file is empty");
693 free_string_list (product_name);
697 /* Note that this string becomes owned by the handle and will
698 * be freed by guestfs___free_inspect_info.
700 fs->product_name = product_name[0];
704 if (match2 (g, fs->product_name, re_debian, &major, &minor)) {
705 fs->major_version = parse_unsigned_int (g, major);
707 if (fs->major_version == -1) {
711 fs->minor_version = parse_unsigned_int (g, minor);
713 if (fs->minor_version == -1)
718 /* Determine the architecture. */
719 const char *binaries[] =
720 { "/bin/bash", "/bin/ls", "/bin/echo", "/bin/rm", "/bin/sh" };
722 for (i = 0; i < sizeof binaries / sizeof binaries[0]; ++i) {
723 if (guestfs_is_file (g, binaries[i]) > 0) {
724 /* Ignore errors from file_architecture call. */
725 guestfs_error_handler_cb old_error_cb = g->error_cb;
727 char *arch = guestfs_file_architecture (g, binaries[i]);
728 g->error_cb = old_error_cb;
731 /* String will be owned by handle, freed by
732 * guestfs___free_inspect_info.
740 /* We already know /etc/fstab exists because it's part of the test
741 * for Linux root above. We must now parse this file to determine
742 * which filesystems are used by the operating system and how they
744 * XXX What if !feature_available (g, "augeas")?
746 if (guestfs_aug_init (g, "/", AUG_NO_LOAD|AUG_SAVE_NOOP) == -1)
749 /* Tell Augeas to only load /etc/fstab (thanks Raphaël Pinson). */
750 guestfs_aug_rm (g, "/augeas/load//incl[. != \"/etc/fstab\"]");
751 guestfs_aug_load (g);
753 int r = check_fstab (g, fs);
754 guestfs_aug_close (g);
762 check_fstab (guestfs_h *g, struct inspect_fs *fs)
764 char **lines = guestfs_aug_ls (g, "/files/etc/fstab");
768 if (lines[0] == NULL) {
769 error (g, "could not parse /etc/fstab or empty file");
770 free_string_list (lines);
776 for (i = 0; lines[i] != NULL; ++i) {
777 /* Ignore comments. Only care about sequence lines which
780 if (match (g, lines[i], re_aug_seq)) {
781 snprintf (augpath, sizeof augpath, "%s/spec", lines[i]);
782 char *spec = guestfs_aug_get (g, augpath);
784 free_string_list (lines);
788 snprintf (augpath, sizeof augpath, "%s/file", lines[i]);
789 char *mp = guestfs_aug_get (g, augpath);
791 free_string_list (lines);
796 int r = add_fstab_entry (g, fs, spec, mp);
801 free_string_list (lines);
807 free_string_list (lines);
811 /* Add a filesystem and possibly a mountpoint entry for
812 * the root filesystem 'fs'.
814 * 'spec' is the fstab spec field, which might be a device name or a
815 * pseudodevice or 'UUID=...' or 'LABEL=...'.
817 * 'mp' is the mount point, which could also be 'swap' or 'none'.
820 add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
821 const char *spec, const char *mp)
823 /* Ignore certain mountpoints. */
824 if (STRPREFIX (mp, "/dev/") ||
825 STREQ (mp, "/dev") ||
826 STRPREFIX (mp, "/media/") ||
827 STRPREFIX (mp, "/proc/") ||
828 STREQ (mp, "/proc") ||
829 STRPREFIX (mp, "/selinux/") ||
830 STREQ (mp, "/selinux") ||
831 STRPREFIX (mp, "/sys/") ||
835 /* Ignore /dev/fd (floppy disks) (RHBZ#642929) and CD-ROM drives. */
836 if ((STRPREFIX (spec, "/dev/fd") && c_isdigit (spec[7])) ||
837 STREQ (spec, "/dev/floppy") ||
838 STREQ (spec, "/dev/cdrom"))
841 /* Resolve UUID= and LABEL= to the actual device. */
843 if (STRPREFIX (spec, "UUID="))
844 device = guestfs_findfs_uuid (g, &spec[5]);
845 else if (STRPREFIX (spec, "LABEL="))
846 device = guestfs_findfs_label (g, &spec[6]);
847 /* Resolve guest block device names. */
848 else if (spec[0] == '/')
849 device = resolve_fstab_device (g, spec);
850 /* Also ignore pseudo-devices completely, like spec == "tmpfs".
851 * If we haven't resolved the device successfully by this point,
852 * we don't care, just ignore it.
857 char *mountpoint = safe_strdup (g, mp);
859 /* Add this to the fstab entry in 'fs'.
860 * Note these are further filtered by guestfs_inspect_get_mountpoints
861 * and guestfs_inspect_get_filesystems.
863 size_t n = fs->nr_fstab + 1;
864 struct inspect_fstab_entry *p;
866 p = realloc (fs->fstab, n * sizeof (struct inspect_fstab_entry));
868 perrorf (g, "realloc");
877 /* These are owned by the handle and freed by guestfs___free_inspect_info. */
878 fs->fstab[n-1].device = device;
879 fs->fstab[n-1].mountpoint = mountpoint;
882 fprintf (stderr, "fstab: device=%s mountpoint=%s\n", device, mountpoint);
887 /* Resolve block device name to the libguestfs device name, eg.
888 * /dev/xvdb1 => /dev/vdb1; and /dev/mapper/VG-LV => /dev/VG/LV. This
889 * assumes that disks were added in the same order as they appear to
890 * the real VM, which is a reasonable assumption to make. Return
891 * anything we don't recognize unchanged.
894 resolve_fstab_device (guestfs_h *g, const char *spec)
899 if (STRPREFIX (spec, "/dev/mapper/")) {
900 /* LVM2 does some strange munging on /dev/mapper paths for VGs and
901 * LVs which contain '-' character:
903 * ><fs> lvcreate LV--test VG--test 32
904 * ><fs> debug ls /dev/mapper
905 * VG----test-LV----test
907 * This makes it impossible to reverse those paths directly, so
908 * we have implemented lvm_canonical_lv_name in the daemon.
910 device = guestfs_lvm_canonical_lv_name (g, spec);
912 else if ((a1 = match1 (g, spec, re_xdev)) != NULL) {
913 char **devices = guestfs_list_devices (g);
918 for (count = 0; devices[count] != NULL; count++)
921 size_t i = a1[0] - 'a'; /* a1[0] is always [a-z] because of regex. */
923 size_t len = strlen (devices[i]) + strlen (a1) + 16;
924 device = safe_malloc (g, len);
925 snprintf (device, len, "%s%s", devices[i], &a1[1]);
929 free_string_list (devices);
932 /* Didn't match device pattern, return original spec unchanged. */
933 device = safe_strdup (g, spec);
939 /* XXX Handling of boot.ini in the Perl version was pretty broken. It
940 * essentially didn't do anything for modern Windows guests.
941 * Therefore I've omitted all that code.
944 check_windows_root (guestfs_h *g, struct inspect_fs *fs)
946 fs->type = OS_TYPE_WINDOWS;
947 fs->distro = OS_DISTRO_WINDOWS;
949 /* Try to find Windows systemroot using some common locations. */
950 const char *systemroots[] =
951 { "/windows", "/winnt", "/win32", "/win" };
953 char *systemroot = NULL;
955 systemroot == NULL && i < sizeof systemroots / sizeof systemroots[0];
957 systemroot = resolve_windows_path_silently (g, systemroots[i]);
961 error (g, _("cannot resolve Windows %%SYSTEMROOT%%"));
965 /* XXX There is a case for exposing systemroot and many variables
966 * from the registry through the libguestfs API.
970 fprintf (stderr, "windows %%SYSTEMROOT%% = %s", systemroot);
972 if (check_windows_arch (g, fs, systemroot) == -1) {
977 if (check_windows_registry (g, fs, systemroot) == -1) {
987 check_windows_arch (guestfs_h *g, struct inspect_fs *fs,
988 const char *systemroot)
990 size_t len = strlen (systemroot) + 32;
992 snprintf (cmd_exe, len, "%s/system32/cmd.exe", systemroot);
994 char *cmd_exe_path = resolve_windows_path_silently (g, cmd_exe);
998 char *arch = guestfs_file_architecture (g, cmd_exe_path);
1002 fs->arch = arch; /* freed by guestfs___free_inspect_info */
1007 /* At the moment, pull just the ProductName and version numbers from
1008 * the registry. In future there is a case for making many more
1009 * registry fields available to callers.
1012 check_windows_registry (guestfs_h *g, struct inspect_fs *fs,
1013 const char *systemroot)
1015 TMP_TEMPLATE_ON_STACK (dir);
1016 #define dir_len (strlen (dir))
1017 #define software_hive_len (dir_len + 16)
1018 char software_hive[software_hive_len];
1019 #define cmd_len (dir_len + 16)
1022 size_t len = strlen (systemroot) + 64;
1024 snprintf (software, len, "%s/system32/config/software", systemroot);
1026 char *software_path = resolve_windows_path_silently (g, software);
1028 /* If the software hive doesn't exist, just accept that we cannot
1029 * find product_name etc.
1035 hive_value_h *values = NULL;
1037 if (mkdtemp (dir) == NULL) {
1038 perrorf (g, "mkdtemp");
1042 snprintf (software_hive, software_hive_len, "%s/software", dir);
1044 if (guestfs_download (g, software_path, software_hive) == -1)
1047 h = hivex_open (software_hive, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
1049 perrorf (g, "hivex_open");
1053 hive_node_h node = hivex_root (h);
1054 const char *hivepath[] =
1055 { "Microsoft", "Windows NT", "CurrentVersion" };
1058 node != 0 && i < sizeof hivepath / sizeof hivepath[0];
1060 node = hivex_node_get_child (h, node, hivepath[i]);
1064 perrorf (g, "hivex: cannot locate HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
1068 values = hivex_node_values (h, node);
1070 for (i = 0; values[i] != 0; ++i) {
1071 char *key = hivex_value_key (h, values[i]);
1073 perrorf (g, "hivex_value_key");
1077 if (STRCASEEQ (key, "ProductName")) {
1078 fs->product_name = hivex_value_string (h, values[i]);
1079 if (!fs->product_name) {
1080 perrorf (g, "hivex_value_string");
1085 else if (STRCASEEQ (key, "CurrentVersion")) {
1086 char *version = hivex_value_string (h, values[i]);
1088 perrorf (g, "hivex_value_string");
1092 char *major, *minor;
1093 if (match2 (g, version, re_windows_version, &major, &minor)) {
1094 fs->major_version = parse_unsigned_int (g, major);
1096 if (fs->major_version == -1) {
1102 fs->minor_version = parse_unsigned_int (g, minor);
1104 if (fs->minor_version == -1) {
1120 if (h) hivex_close (h);
1122 free (software_path);
1124 /* Free up the temporary directory. Note the directory name cannot
1125 * contain shell meta-characters because of the way it was
1126 * constructed above.
1128 snprintf (cmd, cmd_len, "rm -rf %s", dir);
1129 ignore_value (system (cmd));
1131 #undef software_hive_len
1138 resolve_windows_path_silently (guestfs_h *g, const char *path)
1140 guestfs_error_handler_cb old_error_cb = g->error_cb;
1142 char *ret = guestfs_case_sensitive_path (g, path);
1143 g->error_cb = old_error_cb;
1148 extend_fses (guestfs_h *g)
1150 size_t n = g->nr_fses + 1;
1151 struct inspect_fs *p;
1153 p = realloc (g->fses, n * sizeof (struct inspect_fs));
1155 perrorf (g, "realloc");
1162 memset (&g->fses[n-1], 0, sizeof (struct inspect_fs));
1167 /* Parse small, unsigned ints, as used in version numbers. */
1169 parse_unsigned_int (guestfs_h *g, const char *str)
1172 int r = xstrtol (str, NULL, 10, &ret, "");
1173 if (r != LONGINT_OK) {
1174 error (g, "could not parse integer in version number: %s", str);
1180 static struct inspect_fs *
1181 search_for_root (guestfs_h *g, const char *root)
1183 if (g->nr_fses == 0) {
1184 error (g, _("no inspection data: call guestfs_inspect_os first"));
1189 struct inspect_fs *fs;
1190 for (i = 0; i < g->nr_fses; ++i) {
1192 if (fs->is_root && STREQ (root, fs->device))
1196 error (g, _("%s: root device not found: only call this function with a root device previously returned by guestfs_inspect_os"),
1202 guestfs__inspect_get_type (guestfs_h *g, const char *root)
1204 struct inspect_fs *fs = search_for_root (g, root);
1210 case OS_TYPE_LINUX: ret = safe_strdup (g, "linux"); break;
1211 case OS_TYPE_WINDOWS: ret = safe_strdup (g, "windows"); break;
1212 case OS_TYPE_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1219 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
1221 struct inspect_fs *fs = search_for_root (g, root);
1225 return safe_strdup (g, fs->arch ? : "unknown");
1229 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
1231 struct inspect_fs *fs = search_for_root (g, root);
1236 switch (fs->distro) {
1237 case OS_DISTRO_DEBIAN: ret = safe_strdup (g, "debian"); break;
1238 case OS_DISTRO_FEDORA: ret = safe_strdup (g, "fedora"); break;
1239 case OS_DISTRO_REDHAT_BASED: ret = safe_strdup (g, "redhat-based"); break;
1240 case OS_DISTRO_RHEL: ret = safe_strdup (g, "rhel"); break;
1241 case OS_DISTRO_WINDOWS: ret = safe_strdup (g, "windows"); break;
1242 case OS_DISTRO_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1249 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
1251 struct inspect_fs *fs = search_for_root (g, root);
1255 return fs->major_version;
1259 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
1261 struct inspect_fs *fs = search_for_root (g, root);
1265 return fs->minor_version;
1269 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
1271 struct inspect_fs *fs = search_for_root (g, root);
1275 return safe_strdup (g, fs->product_name ? : "unknown");
1279 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
1281 struct inspect_fs *fs = search_for_root (g, root);
1287 /* If no fstab information (Windows) return just the root. */
1288 if (fs->nr_fstab == 0) {
1289 ret = calloc (3, sizeof (char *));
1290 ret[0] = safe_strdup (g, "/");
1291 ret[1] = safe_strdup (g, root);
1296 #define CRITERION fs->fstab[i].mountpoint[0] == '/'
1297 size_t i, count = 0;
1298 for (i = 0; i < fs->nr_fstab; ++i)
1302 /* Hashtables have 2N+1 entries. */
1303 ret = calloc (2*count+1, sizeof (char *));
1305 perrorf (g, "calloc");
1310 for (i = 0; i < fs->nr_fstab; ++i)
1312 ret[2*count] = safe_strdup (g, fs->fstab[i].mountpoint);
1313 ret[2*count+1] = safe_strdup (g, fs->fstab[i].device);
1322 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
1324 struct inspect_fs *fs = search_for_root (g, root);
1330 /* If no fstab information (Windows) return just the root. */
1331 if (fs->nr_fstab == 0) {
1332 ret = calloc (2, sizeof (char *));
1333 ret[0] = safe_strdup (g, root);
1338 ret = calloc (fs->nr_fstab + 1, sizeof (char *));
1340 perrorf (g, "calloc");
1345 for (i = 0; i < fs->nr_fstab; ++i)
1346 ret[i] = safe_strdup (g, fs->fstab[i].device);
1351 /* List filesystems.
1353 * The current implementation just uses guestfs_vfs_type and doesn't
1354 * try mounting anything, but we reserve the right in future to try
1355 * mounting filesystems.
1358 static void remove_from_list (char **list, const char *item);
1359 static void check_with_vfs_type (guestfs_h *g, const char *dev, char ***ret, size_t *ret_size);
1362 guestfs__list_filesystems (guestfs_h *g)
1368 ret = safe_malloc (g, sizeof (char *));
1372 /* Look to see if any devices directly contain filesystems
1373 * (RHBZ#590167). However vfs-type will fail to tell us anything
1374 * useful about devices which just contain partitions, so we also
1375 * get the list of partitions and exclude the corresponding devices
1376 * by using part-to-dev.
1379 devices = guestfs_list_devices (g);
1380 if (devices == NULL) {
1381 free_string_list (ret);
1385 partitions = guestfs_list_partitions (g);
1386 if (partitions == NULL) {
1387 free_string_list (devices);
1388 free_string_list (ret);
1392 for (i = 0; partitions[i] != NULL; ++i) {
1393 char *dev = guestfs_part_to_dev (g, partitions[i]);
1395 remove_from_list (devices, dev);
1399 /* Use vfs-type to check for filesystems on devices. */
1400 for (i = 0; devices[i] != NULL; ++i)
1401 check_with_vfs_type (g, devices[i], &ret, &ret_size);
1402 free_string_list (devices);
1404 /* Use vfs-type to check for filesystems on partitions. */
1405 for (i = 0; partitions[i] != NULL; ++i)
1406 check_with_vfs_type (g, partitions[i], &ret, &ret_size);
1407 free_string_list (partitions);
1409 if (feature_available (g, "lvm2")) {
1410 /* Use vfs-type to check for filesystems on LVs. */
1412 lvs = guestfs_lvs (g);
1414 free_string_list (ret);
1418 for (i = 0; lvs[i] != NULL; ++i)
1419 check_with_vfs_type (g, lvs[i], &ret, &ret_size);
1420 free_string_list (lvs);
1426 /* If 'item' occurs in 'list', remove and free it. */
1428 remove_from_list (char **list, const char *item)
1432 for (i = 0; list[i] != NULL; ++i)
1433 if (STREQ (list[i], item)) {
1435 for (; list[i+1] != NULL; ++i)
1436 list[i] = list[i+1];
1442 /* Use vfs-type to look for a filesystem of some sort on 'dev'.
1443 * Apart from some types which we ignore, add the result to the
1444 * 'ret' string list.
1447 check_with_vfs_type (guestfs_h *g, const char *device,
1448 char ***ret, size_t *ret_size)
1452 guestfs_error_handler_cb old_error_cb = g->error_cb;
1454 char *vfs_type = guestfs_vfs_type (g, device);
1455 g->error_cb = old_error_cb;
1458 v = safe_strdup (g, "unknown");
1460 /* Ignore all "*_member" strings. In libblkid these are returned
1461 * for things which are members of some RAID or LVM set, most
1462 * importantly "LVM2_member" which is a PV.
1464 size_t n = strlen (vfs_type);
1465 if (n >= 7 && STREQ (&vfs_type[n-7], "_member")) {
1470 /* Ignore LUKS-encrypted partitions. These are also containers. */
1471 if (STREQ (vfs_type, "crypto_LUKS")) {
1479 /* Extend the return array. */
1480 size_t i = *ret_size;
1482 *ret = safe_realloc (g, *ret, (*ret_size + 1) * sizeof (char *));
1483 (*ret)[i] = safe_strdup (g, device);