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
34 #include "ignore-value.h"
38 #include "guestfs-internal.h"
39 #include "guestfs-internal-actions.h"
40 #include "guestfs_protocol.h"
42 /* Compile all the regular expressions once when the shared library is
43 * loaded. PCRE is thread safe so we're supposedly OK here if
44 * multiple threads call into the libguestfs API functions below
47 static pcre *re_file_elf;
48 static pcre *re_file_win64;
49 static pcre *re_elf_ppc64;
50 static pcre *re_fedora;
51 static pcre *re_rhel_old;
53 static pcre *re_rhel_no_minor;
54 static pcre *re_debian;
55 static pcre *re_aug_seq;
57 static pcre *re_windows_version;
59 static void compile_regexps (void) __attribute__((constructor));
60 static void free_regexps (void) __attribute__((destructor));
63 compile_regexps (void)
68 #define COMPILE(re,pattern,options) \
70 re = pcre_compile ((pattern), (options), &err, &offset, NULL); \
72 ignore_value (write (2, err, strlen (err))); \
78 "ELF.*(?:executable|shared object|relocatable), (.+?),", 0);
79 COMPILE (re_elf_ppc64, "64.*PowerPC", 0);
80 COMPILE (re_fedora, "Fedora release (\\d+)", 0);
82 "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+).*Update (\\d+)", 0);
84 "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+)\\.(\\d+)", 0);
85 COMPILE (re_rhel_no_minor,
86 "(?:Red Hat Enterprise Linux|CentOS|Scientific Linux).*release (\\d+)", 0);
87 COMPILE (re_debian, "(\\d+)\\.(\\d+)", 0);
88 COMPILE (re_aug_seq, "/\\d+$", 0);
89 COMPILE (re_xdev, "^/dev/(?:h|s|v|xv)d([a-z]\\d*)$", 0);
90 COMPILE (re_windows_version, "^(\\d+)\\.(\\d+)", 0);
96 pcre_free (re_file_elf);
97 pcre_free (re_file_win64);
98 pcre_free (re_elf_ppc64);
99 pcre_free (re_fedora);
100 pcre_free (re_rhel_old);
102 pcre_free (re_rhel_no_minor);
103 pcre_free (re_debian);
104 pcre_free (re_aug_seq);
106 pcre_free (re_windows_version);
109 /* Match a regular expression which contains no captures. Returns
110 * true if it matches or false if it doesn't.
113 match (guestfs_h *g, const char *str, const pcre *re)
115 size_t len = strlen (str);
118 r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
119 if (r == PCRE_ERROR_NOMATCH)
122 /* Internal error -- should not happen. */
123 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
124 __FILE__, __func__, r, str);
131 /* Match a regular expression which contains exactly one capture. If
132 * the string matches, return the capture, otherwise return NULL. The
133 * caller must free the result.
136 match1 (guestfs_h *g, const char *str, const pcre *re)
138 size_t len = strlen (str);
141 r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
142 if (r == PCRE_ERROR_NOMATCH)
145 /* Internal error -- should not happen. */
146 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
147 __FILE__, __func__, r, str);
151 return safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
154 /* Match a regular expression which contains exactly two captures. */
156 match2 (guestfs_h *g, const char *str, const pcre *re, char **ret1, char **ret2)
158 size_t len = strlen (str);
161 r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
162 if (r == PCRE_ERROR_NOMATCH)
165 /* Internal error -- should not happen. */
166 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
167 __FILE__, __func__, r, str);
171 *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
172 *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
177 /* Convert output from 'file' command on ELF files to the canonical
178 * architecture string. Caller must free the result.
181 canonical_elf_arch (guestfs_h *g, const char *elf_arch)
185 if (strstr (elf_arch, "Intel 80386"))
187 else if (strstr (elf_arch, "Intel 80486"))
189 else if (strstr (elf_arch, "x86-64"))
191 else if (strstr (elf_arch, "AMD x86-64"))
193 else if (strstr (elf_arch, "SPARC32"))
195 else if (strstr (elf_arch, "SPARC V9"))
197 else if (strstr (elf_arch, "IA-64"))
199 else if (match (g, elf_arch, re_elf_ppc64))
201 else if (strstr (elf_arch, "PowerPC"))
206 char *ret = safe_strdup (g, r);
211 is_regular_file (const char *filename)
215 return lstat (filename, &statbuf) == 0 && S_ISREG (statbuf.st_mode);
218 /* Download and uncompress the cpio file to find binaries within.
220 * (1) Two lists must be identical.
221 * (2) Implicit limit of 31 bytes for length of each element (see code
224 #define INITRD_BINARIES1 "bin/ls bin/rm bin/modprobe sbin/modprobe bin/sh bin/bash bin/dash bin/nash"
225 #define INITRD_BINARIES2 {"bin/ls", "bin/rm", "bin/modprobe", "sbin/modprobe", "bin/sh", "bin/bash", "bin/dash", "bin/nash"}
228 cpio_arch (guestfs_h *g, const char *file, const char *path)
233 if (strstr (file, "gzip"))
235 else if (strstr (file, "bzip2"))
240 char dir[] = "/tmp/initrd.XXXXXX";
241 #define dir_len (sizeof dir)
242 if (mkdtemp (dir) == NULL) {
243 perrorf (g, "mkdtemp");
247 char dir_initrd[dir_len + 16];
248 snprintf (dir_initrd, dir_len + 16, "%s/initrd", dir);
249 if (guestfs_download (g, path, dir_initrd) == -1)
252 char cmd[dir_len + 256];
253 snprintf (cmd, dir_len + 256,
254 "cd %s && %s initrd | cpio --quiet -id " INITRD_BINARIES1,
256 int r = system (cmd);
257 if (r == -1 || WEXITSTATUS (r) != 0) {
258 perrorf (g, "cpio command failed");
262 char bin[dir_len + 32];
263 const char *bins[] = INITRD_BINARIES2;
265 for (i = 0; i < sizeof bins / sizeof bins[0]; ++i) {
266 snprintf (bin, dir_len + 32, "%s/%s", dir, bins[i]);
268 if (is_regular_file (bin)) {
269 int flags = g->verbose ? MAGIC_DEBUG : 0;
270 flags |= MAGIC_ERROR | MAGIC_RAW;
272 magic_t m = magic_open (flags);
274 perrorf (g, "magic_open");
278 if (magic_load (m, NULL) == -1) {
279 perrorf (g, "magic_load: default magic database file");
284 const char *line = magic_file (m, bin);
286 perrorf (g, "magic_file: %s", bin);
292 if ((elf_arch = match1 (g, line, re_file_elf)) != NULL) {
293 ret = canonical_elf_arch (g, elf_arch);
301 error (g, "file_architecture: could not determine architecture of cpio archive");
304 /* Free up the temporary directory. Note the directory name cannot
305 * contain shell meta-characters because of the way it was
308 snprintf (cmd, dir_len + 256, "rm -rf %s", dir);
309 ignore_value (system (cmd));
316 guestfs__file_architecture (guestfs_h *g, const char *path)
319 char *elf_arch = NULL;
322 /* Get the output of the "file" command. Note that because this
323 * runs in the daemon, LANG=C so it's in English.
325 file = guestfs_file (g, path);
329 if ((elf_arch = match1 (g, file, re_file_elf)) != NULL)
330 ret = canonical_elf_arch (g, elf_arch);
331 else if (strstr (file, "PE32 executable"))
332 ret = safe_strdup (g, "i386");
333 else if (strstr (file, "PE32+ executable"))
334 ret = safe_strdup (g, "x86_64");
335 else if (strstr (file, "cpio archive"))
336 ret = cpio_arch (g, file, path);
338 error (g, "file_architecture: unknown architecture: %s", path);
342 return ret; /* caller frees */
345 /* The main inspection code. */
346 static int feature_available (guestfs_h *g, const char *feature);
347 static void free_string_list (char **);
348 static int check_for_filesystem_on (guestfs_h *g, const char *device);
351 guestfs__inspect_os (guestfs_h *g)
353 /* Remove any information previously stored in the handle. */
354 guestfs___free_inspect_info (g);
356 if (guestfs_umount_all (g) == -1)
359 /* Iterate over all possible devices. Try to mount each
360 * (read-only). Examine ones which contain filesystems and add that
361 * information to the handle.
363 /* Look to see if any devices directly contain filesystems (RHBZ#590167). */
365 devices = guestfs_list_devices (g);
370 for (i = 0; devices[i] != NULL; ++i) {
371 if (check_for_filesystem_on (g, devices[i]) == -1) {
372 free_string_list (devices);
373 guestfs___free_inspect_info (g);
377 free_string_list (devices);
379 /* Look at all partitions. */
381 partitions = guestfs_list_partitions (g);
382 if (partitions == NULL) {
383 guestfs___free_inspect_info (g);
387 for (i = 0; partitions[i] != NULL; ++i) {
388 if (check_for_filesystem_on (g, partitions[i]) == -1) {
389 free_string_list (partitions);
390 guestfs___free_inspect_info (g);
394 free_string_list (partitions);
396 /* Look at all LVs. */
397 if (feature_available (g, "lvm2")) {
399 lvs = guestfs_lvs (g);
401 guestfs___free_inspect_info (g);
405 for (i = 0; lvs[i] != NULL; ++i) {
406 if (check_for_filesystem_on (g, lvs[i]) == -1) {
407 free_string_list (lvs);
408 guestfs___free_inspect_info (g);
412 free_string_list (lvs);
415 /* At this point we have, in the handle, a list of all filesystems
416 * found and data about each one. Now we assemble the list of
417 * filesystems which are root devices and return that to the user.
420 for (i = 0; i < g->nr_fses; ++i)
421 if (g->fses[i].is_root)
424 char **ret = calloc (count+1, sizeof (char *));
426 perrorf (g, "calloc");
427 guestfs___free_inspect_info (g);
432 for (i = 0; i < g->nr_fses; ++i) {
433 if (g->fses[i].is_root) {
434 ret[count] = safe_strdup (g, g->fses[i].device);
444 guestfs___free_inspect_info (guestfs_h *g)
447 for (i = 0; i < g->nr_fses; ++i) {
448 free (g->fses[i].device);
449 free (g->fses[i].product_name);
450 free (g->fses[i].arch);
452 for (j = 0; j < g->fses[i].nr_fstab; ++j) {
453 free (g->fses[i].fstab[j].device);
454 free (g->fses[i].fstab[j].mountpoint);
456 free (g->fses[i].fstab);
464 free_string_list (char **argv)
467 for (i = 0; argv[i] != NULL; ++i)
472 /* In the Perl code this is a public function. */
474 feature_available (guestfs_h *g, const char *feature)
476 /* If there's an error we should ignore it, so to do that we have to
477 * temporarily replace the error handler with a null one.
479 guestfs_error_handler_cb old_error_cb = g->error_cb;
482 const char *groups[] = { feature, NULL };
483 int r = guestfs_available (g, (char * const *) groups);
485 g->error_cb = old_error_cb;
487 return r == 0 ? 1 : 0;
490 /* Find out if 'device' contains a filesystem. If it does, add
491 * another entry in g->fses.
493 static int check_filesystem (guestfs_h *g, const char *device);
494 static int check_linux_root (guestfs_h *g, struct inspect_fs *fs);
495 static int check_fstab (guestfs_h *g, struct inspect_fs *fs);
496 static int check_windows_root (guestfs_h *g, struct inspect_fs *fs);
497 static int check_windows_arch (guestfs_h *g, struct inspect_fs *fs,
498 const char *systemroot);
499 static int check_windows_registry (guestfs_h *g, struct inspect_fs *fs,
500 const char *systemroot);
501 static char *resolve_windows_path_silently (guestfs_h *g, const char *);
502 static int extend_fses (guestfs_h *g);
503 static int parse_unsigned_int (guestfs_h *g, const char *str);
504 static int add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
505 const char *spec, const char *mp);
506 static char *resolve_fstab_device (guestfs_h *g, const char *spec);
509 check_for_filesystem_on (guestfs_h *g, const char *device)
511 /* Get vfs-type in order to check if it's a Linux(?) swap device.
512 * If there's an error we should ignore it, so to do that we have to
513 * temporarily replace the error handler with a null one.
515 guestfs_error_handler_cb old_error_cb = g->error_cb;
517 char *vfs_type = guestfs_vfs_type (g, device);
518 g->error_cb = old_error_cb;
520 int is_swap = vfs_type && STREQ (vfs_type, "swap");
523 fprintf (stderr, "check_for_filesystem_on: %s (%s)\n",
524 device, vfs_type ? vfs_type : "failed to get vfs type");
528 if (extend_fses (g) == -1)
530 g->fses[g->nr_fses-1].is_swap = 1;
534 /* Try mounting the device. As above, ignore errors. */
536 int r = guestfs_mount_ro (g, device, "/");
537 if (r == -1 && vfs_type && STREQ (vfs_type, "ufs")) /* Hack for the *BSDs. */
538 r = guestfs_mount_vfs (g, "ro,ufstype=ufs2", "ufs", device, "/");
540 g->error_cb = old_error_cb;
544 /* Do the rest of the checks. */
545 r = check_filesystem (g, device);
547 /* Unmount the filesystem. */
548 if (guestfs_umount_all (g) == -1)
555 check_filesystem (guestfs_h *g, const char *device)
557 if (extend_fses (g) == -1)
560 struct inspect_fs *fs = &g->fses[g->nr_fses-1];
562 fs->device = safe_strdup (g, device);
563 fs->is_mountable = 1;
566 if (guestfs_is_file (g, "/grub/menu.lst") > 0 ||
567 guestfs_is_file (g, "/grub/grub.conf") > 0)
568 fs->content = FS_CONTENT_LINUX_BOOT;
570 else if (guestfs_is_dir (g, "/etc") > 0 &&
571 guestfs_is_dir (g, "/bin") > 0 &&
572 guestfs_is_file (g, "/etc/fstab") > 0) {
574 fs->content = FS_CONTENT_LINUX_ROOT;
575 if (check_linux_root (g, fs) == -1)
578 /* Linux /usr/local? */
579 else if (guestfs_is_dir (g, "/etc") > 0 &&
580 guestfs_is_dir (g, "/bin") > 0 &&
581 guestfs_is_dir (g, "/share") > 0 &&
582 guestfs_exists (g, "/local") == 0 &&
583 guestfs_is_file (g, "/etc/fstab") == 0)
584 fs->content = FS_CONTENT_LINUX_USR_LOCAL;
586 else if (guestfs_is_dir (g, "/etc") > 0 &&
587 guestfs_is_dir (g, "/bin") > 0 &&
588 guestfs_is_dir (g, "/share") > 0 &&
589 guestfs_exists (g, "/local") > 0 &&
590 guestfs_is_file (g, "/etc/fstab") == 0)
591 fs->content = FS_CONTENT_LINUX_USR;
593 else if (guestfs_is_dir (g, "/log") > 0 &&
594 guestfs_is_dir (g, "/run") > 0 &&
595 guestfs_is_dir (g, "/spool") > 0)
596 fs->content = FS_CONTENT_LINUX_VAR;
598 else if (guestfs_is_file (g, "/AUTOEXEC.BAT") > 0 ||
599 guestfs_is_file (g, "/autoexec.bat") > 0 ||
600 guestfs_is_dir (g, "/Program Files") > 0 ||
601 guestfs_is_dir (g, "/WINDOWS") > 0 ||
602 guestfs_is_dir (g, "/Windows") > 0 ||
603 guestfs_is_dir (g, "/windows") > 0 ||
604 guestfs_is_dir (g, "/WIN32") > 0 ||
605 guestfs_is_dir (g, "/Win32") > 0 ||
606 guestfs_is_dir (g, "/WINNT") > 0 ||
607 guestfs_is_file (g, "/boot.ini") > 0 ||
608 guestfs_is_file (g, "/ntldr") > 0) {
610 fs->content = FS_CONTENT_WINDOWS_ROOT;
611 if (check_windows_root (g, fs) == -1)
618 /* The currently mounted device is known to be a Linux root. Try to
619 * determine from this the distro, version, etc. Also parse
620 * /etc/fstab to determine the arrangement of mountpoints and
621 * associated devices.
624 check_linux_root (guestfs_h *g, struct inspect_fs *fs)
626 fs->type = OS_TYPE_LINUX;
628 if (guestfs_exists (g, "/etc/redhat-release") > 0) {
629 fs->distro = OS_DISTRO_REDHAT_BASED; /* Something generic Red Hat-like. */
631 char **product_name = guestfs_head_n (g, 1, "/etc/redhat-release");
632 if (product_name == NULL)
634 if (product_name[0] == NULL) {
635 error (g, "/etc/redhat-release file is empty");
636 free_string_list (product_name);
640 /* Note that this string becomes owned by the handle and will
641 * be freed by guestfs___free_inspect_info.
643 fs->product_name = product_name[0];
647 if ((major = match1 (g, fs->product_name, re_fedora)) != NULL) {
648 fs->distro = OS_DISTRO_FEDORA;
649 fs->major_version = parse_unsigned_int (g, major);
651 if (fs->major_version == -1)
654 else if (match2 (g, fs->product_name, re_rhel_old, &major, &minor) ||
655 match2 (g, fs->product_name, re_rhel, &major, &minor)) {
656 fs->distro = OS_DISTRO_RHEL;
657 fs->major_version = parse_unsigned_int (g, major);
659 if (fs->major_version == -1) {
663 fs->minor_version = parse_unsigned_int (g, minor);
665 if (fs->minor_version == -1)
668 else if ((major = match1 (g, fs->product_name, re_rhel_no_minor)) != NULL) {
669 fs->distro = OS_DISTRO_RHEL;
670 fs->major_version = parse_unsigned_int (g, major);
672 if (fs->major_version == -1)
674 fs->minor_version = 0;
677 else if (guestfs_exists (g, "/etc/debian_version") > 0) {
678 fs->distro = OS_DISTRO_DEBIAN;
680 char **product_name = guestfs_head_n (g, 1, "/etc/debian_version");
681 if (product_name == NULL)
683 if (product_name[0] == NULL) {
684 error (g, "/etc/debian_version file is empty");
685 free_string_list (product_name);
689 /* Note that this string becomes owned by the handle and will
690 * be freed by guestfs___free_inspect_info.
692 fs->product_name = product_name[0];
696 if (match2 (g, fs->product_name, re_debian, &major, &minor)) {
697 fs->major_version = parse_unsigned_int (g, major);
699 if (fs->major_version == -1) {
703 fs->minor_version = parse_unsigned_int (g, minor);
705 if (fs->minor_version == -1)
710 /* Determine the architecture. */
711 const char *binaries[] =
712 { "/bin/bash", "/bin/ls", "/bin/echo", "/bin/rm", "/bin/sh" };
714 for (i = 0; i < sizeof binaries / sizeof binaries[0]; ++i) {
715 if (guestfs_is_file (g, binaries[i]) > 0) {
716 /* Ignore errors from file_architecture call. */
717 guestfs_error_handler_cb old_error_cb = g->error_cb;
719 char *arch = guestfs_file_architecture (g, binaries[i]);
720 g->error_cb = old_error_cb;
723 /* String will be owned by handle, freed by
724 * guestfs___free_inspect_info.
732 /* We already know /etc/fstab exists because it's part of the test
733 * for Linux root above. We must now parse this file to determine
734 * which filesystems are used by the operating system and how they
736 * XXX What if !feature_available (g, "augeas")?
738 if (guestfs_aug_init (g, "/", AUG_NO_LOAD|AUG_SAVE_NOOP) == -1)
741 /* Tell Augeas to only load /etc/fstab (thanks Raphaël Pinson). */
742 guestfs_aug_rm (g, "/augeas/load//incl[. != \"/etc/fstab\"]");
743 guestfs_aug_load (g);
745 int r = check_fstab (g, fs);
746 guestfs_aug_close (g);
754 check_fstab (guestfs_h *g, struct inspect_fs *fs)
756 char **lines = guestfs_aug_ls (g, "/files/etc/fstab");
760 if (lines[0] == NULL) {
761 error (g, "could not parse /etc/fstab or empty file");
762 free_string_list (lines);
768 for (i = 0; lines[i] != NULL; ++i) {
769 /* Ignore comments. Only care about sequence lines which
772 if (match (g, lines[i], re_aug_seq)) {
773 snprintf (augpath, sizeof augpath, "%s/spec", lines[i]);
774 char *spec = guestfs_aug_get (g, augpath);
776 free_string_list (lines);
780 snprintf (augpath, sizeof augpath, "%s/file", lines[i]);
781 char *mp = guestfs_aug_get (g, augpath);
783 free_string_list (lines);
788 int r = add_fstab_entry (g, fs, spec, mp);
793 free_string_list (lines);
799 free_string_list (lines);
803 /* Add a filesystem and possibly a mountpoint entry for
804 * the root filesystem 'fs'.
806 * 'spec' is the fstab spec field, which might be a device name or a
807 * pseudodevice or 'UUID=...' or 'LABEL=...'.
809 * 'mp' is the mount point, which could also be 'swap' or 'none'.
812 add_fstab_entry (guestfs_h *g, struct inspect_fs *fs,
813 const char *spec, const char *mp)
815 /* Ignore certain mountpoints. */
816 if (STRPREFIX (mp, "/dev/") ||
817 STREQ (mp, "/dev") ||
818 STRPREFIX (mp, "/media/") ||
819 STRPREFIX (mp, "/proc/") ||
820 STREQ (mp, "/proc") ||
821 STRPREFIX (mp, "/selinux/") ||
822 STREQ (mp, "/selinux") ||
823 STRPREFIX (mp, "/sys/") ||
827 /* Resolve UUID= and LABEL= to the actual device. */
829 if (STRPREFIX (spec, "UUID="))
830 device = guestfs_findfs_uuid (g, &spec[5]);
831 else if (STRPREFIX (spec, "LABEL="))
832 device = guestfs_findfs_label (g, &spec[6]);
833 /* Resolve guest block device names. */
834 else if (spec[0] == '/')
835 device = resolve_fstab_device (g, spec);
836 /* Also ignore pseudo-devices completely, like spec == "tmpfs".
837 * If we haven't resolved the device successfully by this point,
838 * we don't care, just ignore it.
843 char *mountpoint = safe_strdup (g, mp);
845 /* Add this to the fstab entry in 'fs'.
846 * Note these are further filtered by guestfs_inspect_get_mountpoints
847 * and guestfs_inspect_get_filesystems.
849 size_t n = fs->nr_fstab + 1;
850 struct inspect_fstab_entry *p;
852 p = realloc (fs->fstab, n * sizeof (struct inspect_fstab_entry));
854 perrorf (g, "realloc");
863 /* These are owned by the handle and freed by guestfs___free_inspect_info. */
864 fs->fstab[n-1].device = device;
865 fs->fstab[n-1].mountpoint = mountpoint;
868 fprintf (stderr, "fstab: device=%s mountpoint=%s\n", device, mountpoint);
873 /* Resolve block device name to the libguestfs device name, eg.
874 * /dev/xvdb1 => /dev/vdb1. This assumes that disks were added in the
875 * same order as they appear to the real VM, which is a reasonable
876 * assumption to make. Return things like LV names unchanged (or
877 * anything we don't recognize).
880 resolve_fstab_device (guestfs_h *g, const char *spec)
882 char **devices = guestfs_list_devices (g);
887 for (count = 0; devices[count] != NULL; count++)
891 char *a1 = match1 (g, spec, re_xdev);
893 size_t i = a1[0] - 'a'; /* a1[0] is always [a-z] because of regex. */
895 size_t len = strlen (devices[i]) + strlen (a1) + 16;
896 device = safe_malloc (g, len);
897 snprintf (device, len, "%s%s", devices[i], &a1[1]);
900 /* Didn't match device pattern, return original spec unchanged. */
901 device = safe_strdup (g, spec);
905 free_string_list (devices);
910 /* XXX Handling of boot.ini in the Perl version was pretty broken. It
911 * essentially didn't do anything for modern Windows guests.
912 * Therefore I've omitted all that code.
915 check_windows_root (guestfs_h *g, struct inspect_fs *fs)
917 fs->type = OS_TYPE_WINDOWS;
918 fs->distro = OS_DISTRO_WINDOWS;
920 /* Try to find Windows systemroot using some common locations. */
921 const char *systemroots[] =
922 { "/windows", "/winnt", "/win32", "/win" };
924 char *systemroot = NULL;
926 systemroot == NULL && i < sizeof systemroots / sizeof systemroots[0];
928 systemroot = resolve_windows_path_silently (g, systemroots[i]);
932 error (g, _("cannot resolve Windows %%SYSTEMROOT%%"));
936 /* XXX There is a case for exposing systemroot and many variables
937 * from the registry through the libguestfs API.
941 fprintf (stderr, "windows %%SYSTEMROOT%% = %s", systemroot);
943 if (check_windows_arch (g, fs, systemroot) == -1) {
948 if (check_windows_registry (g, fs, systemroot) == -1) {
958 check_windows_arch (guestfs_h *g, struct inspect_fs *fs,
959 const char *systemroot)
961 size_t len = strlen (systemroot) + 32;
963 snprintf (cmd_exe, len, "%s/system32/cmd.exe", systemroot);
965 char *cmd_exe_path = resolve_windows_path_silently (g, cmd_exe);
969 char *arch = guestfs_file_architecture (g, cmd_exe_path);
973 fs->arch = arch; /* freed by guestfs___free_inspect_info */
978 /* At the moment, pull just the ProductName and version numbers from
979 * the registry. In future there is a case for making many more
980 * registry fields available to callers.
983 check_windows_registry (guestfs_h *g, struct inspect_fs *fs,
984 const char *systemroot)
986 size_t len = strlen (systemroot) + 64;
988 snprintf (software, len, "%s/system32/config/software", systemroot);
990 char *software_path = resolve_windows_path_silently (g, software);
992 /* If the software hive doesn't exist, just accept that we cannot
993 * find product_name etc.
999 hive_value_h *values = NULL;
1001 char dir[] = "/tmp/winreg.XXXXXX";
1003 if (mkdtemp (dir) == NULL) {
1004 perrorf (g, "mkdtemp");
1008 char software_hive[dir_len + 16];
1009 snprintf (software_hive, dir_len + 16, "%s/software", dir);
1011 if (guestfs_download (g, software_path, software_hive) == -1)
1014 h = hivex_open (software_hive, g->verbose ? HIVEX_OPEN_VERBOSE : 0);
1016 perrorf (g, "hivex_open");
1020 hive_node_h node = hivex_root (h);
1021 const char *hivepath[] =
1022 { "Microsoft", "Windows NT", "CurrentVersion" };
1025 node != 0 && i < sizeof hivepath / sizeof hivepath[0];
1027 node = hivex_node_get_child (h, node, hivepath[i]);
1031 perrorf (g, "hivex: cannot locate HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
1035 values = hivex_node_values (h, node);
1037 for (i = 0; values[i] != 0; ++i) {
1038 char *key = hivex_value_key (h, values[i]);
1040 perrorf (g, "hivex_value_key");
1044 if (STRCASEEQ (key, "ProductName")) {
1045 fs->product_name = hivex_value_string (h, values[i]);
1046 if (!fs->product_name) {
1047 perrorf (g, "hivex_value_string");
1052 else if (STRCASEEQ (key, "CurrentVersion")) {
1053 char *version = hivex_value_string (h, values[i]);
1055 perrorf (g, "hivex_value_string");
1059 char *major, *minor;
1060 if (match2 (g, version, re_windows_version, &major, &minor)) {
1061 fs->major_version = parse_unsigned_int (g, major);
1063 if (fs->major_version == -1) {
1069 fs->minor_version = parse_unsigned_int (g, minor);
1071 if (fs->minor_version == -1) {
1087 if (h) hivex_close (h);
1089 free (software_path);
1091 /* Free up the temporary directory. Note the directory name cannot
1092 * contain shell meta-characters because of the way it was
1093 * constructed above.
1095 char cmd[dir_len + 16];
1096 snprintf (cmd, dir_len + 16, "rm -rf %s", dir);
1097 ignore_value (system (cmd));
1104 resolve_windows_path_silently (guestfs_h *g, const char *path)
1106 guestfs_error_handler_cb old_error_cb = g->error_cb;
1108 char *ret = guestfs_case_sensitive_path (g, path);
1109 g->error_cb = old_error_cb;
1114 extend_fses (guestfs_h *g)
1116 size_t n = g->nr_fses + 1;
1117 struct inspect_fs *p;
1119 p = realloc (g->fses, n * sizeof (struct inspect_fs));
1121 perrorf (g, "realloc");
1128 memset (&g->fses[n-1], 0, sizeof (struct inspect_fs));
1133 /* Parse small, unsigned ints, as used in version numbers. */
1135 parse_unsigned_int (guestfs_h *g, const char *str)
1138 int r = xstrtol (str, NULL, 10, &ret, "");
1139 if (r != LONGINT_OK) {
1140 error (g, "could not parse integer in version number: %s", str);
1146 static struct inspect_fs *
1147 search_for_root (guestfs_h *g, const char *root)
1149 if (g->nr_fses == 0) {
1150 error (g, _("no inspection data: call guestfs_inspect_os first"));
1155 struct inspect_fs *fs;
1156 for (i = 0; i < g->nr_fses; ++i) {
1158 if (fs->is_root && STREQ (root, fs->device))
1162 error (g, _("%s: root device not found: only call this function with a root device previously returned by guestfs_inspect_os"),
1168 guestfs__inspect_get_type (guestfs_h *g, const char *root)
1170 struct inspect_fs *fs = search_for_root (g, root);
1176 case OS_TYPE_LINUX: ret = safe_strdup (g, "linux"); break;
1177 case OS_TYPE_WINDOWS: ret = safe_strdup (g, "windows"); break;
1178 case OS_TYPE_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1185 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
1187 struct inspect_fs *fs = search_for_root (g, root);
1191 return safe_strdup (g, fs->arch ? : "unknown");
1195 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
1197 struct inspect_fs *fs = search_for_root (g, root);
1202 switch (fs->distro) {
1203 case OS_DISTRO_DEBIAN: ret = safe_strdup (g, "debian"); break;
1204 case OS_DISTRO_FEDORA: ret = safe_strdup (g, "fedora"); break;
1205 case OS_DISTRO_REDHAT_BASED: ret = safe_strdup (g, "redhat-based"); break;
1206 case OS_DISTRO_RHEL: ret = safe_strdup (g, "rhel"); break;
1207 case OS_DISTRO_WINDOWS: ret = safe_strdup (g, "windows"); break;
1208 case OS_DISTRO_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
1215 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
1217 struct inspect_fs *fs = search_for_root (g, root);
1221 return fs->major_version;
1225 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
1227 struct inspect_fs *fs = search_for_root (g, root);
1231 return fs->minor_version;
1235 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
1237 struct inspect_fs *fs = search_for_root (g, root);
1241 return safe_strdup (g, fs->product_name ? : "unknown");
1245 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
1247 struct inspect_fs *fs = search_for_root (g, root);
1253 /* If no fstab information (Windows) return just the root. */
1254 if (fs->nr_fstab == 0) {
1255 ret = calloc (3, sizeof (char *));
1256 ret[0] = safe_strdup (g, "/");
1257 ret[1] = safe_strdup (g, root);
1262 #define CRITERION fs->fstab[i].mountpoint[0] == '/'
1263 size_t i, count = 0;
1264 for (i = 0; i < fs->nr_fstab; ++i)
1268 /* Hashtables have 2N+1 entries. */
1269 ret = calloc (2*count+1, sizeof (char *));
1271 perrorf (g, "calloc");
1276 for (i = 0; i < fs->nr_fstab; ++i)
1278 ret[2*count] = safe_strdup (g, fs->fstab[i].mountpoint);
1279 ret[2*count+1] = safe_strdup (g, fs->fstab[i].device);
1288 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
1290 struct inspect_fs *fs = search_for_root (g, root);
1296 /* If no fstab information (Windows) return just the root. */
1297 if (fs->nr_fstab == 0) {
1298 ret = calloc (2, sizeof (char *));
1299 ret[0] = safe_strdup (g, root);
1304 ret = calloc (fs->nr_fstab + 1, sizeof (char *));
1306 perrorf (g, "calloc");
1311 for (i = 0; i < fs->nr_fstab; ++i)
1312 ret[i] = safe_strdup (g, fs->fstab[i].device);
1317 /* List filesystems.
1319 * The current implementation just uses guestfs_vfs_type and doesn't
1320 * try mounting anything, but we reserve the right in future to try
1321 * mounting filesystems.
1324 static void remove_from_list (char **list, const char *item);
1325 static void check_with_vfs_type (guestfs_h *g, const char *dev, char ***ret, size_t *ret_size);
1328 guestfs__list_filesystems (guestfs_h *g)
1334 ret = safe_malloc (g, sizeof (char *));
1338 /* Look to see if any devices directly contain filesystems
1339 * (RHBZ#590167). However vfs-type will fail to tell us anything
1340 * useful about devices which just contain partitions, so we also
1341 * get the list of partitions and exclude the corresponding devices
1342 * by using part-to-dev.
1345 devices = guestfs_list_devices (g);
1346 if (devices == NULL) {
1347 free_string_list (ret);
1351 partitions = guestfs_list_partitions (g);
1352 if (partitions == NULL) {
1353 free_string_list (devices);
1354 free_string_list (ret);
1358 for (i = 0; partitions[i] != NULL; ++i) {
1359 char *dev = guestfs_part_to_dev (g, partitions[i]);
1361 remove_from_list (devices, dev);
1365 /* Use vfs-type to check for filesystems on devices. */
1366 for (i = 0; devices[i] != NULL; ++i)
1367 check_with_vfs_type (g, devices[i], &ret, &ret_size);
1368 free_string_list (devices);
1370 /* Use vfs-type to check for filesystems on partitions. */
1371 for (i = 0; partitions[i] != NULL; ++i)
1372 check_with_vfs_type (g, partitions[i], &ret, &ret_size);
1373 free_string_list (partitions);
1375 if (feature_available (g, "lvm2")) {
1376 /* Use vfs-type to check for filesystems on LVs. */
1378 lvs = guestfs_lvs (g);
1380 free_string_list (ret);
1384 for (i = 0; lvs[i] != NULL; ++i)
1385 check_with_vfs_type (g, lvs[i], &ret, &ret_size);
1386 free_string_list (lvs);
1392 /* If 'item' occurs in 'list', remove and free it. */
1394 remove_from_list (char **list, const char *item)
1398 for (i = 0; list[i] != NULL; ++i)
1399 if (STREQ (list[i], item)) {
1401 for (; list[i+1] != NULL; ++i)
1402 list[i] = list[i+1];
1408 /* Use vfs-type to look for a filesystem of some sort on 'dev'.
1409 * Apart from some types which we ignore, add the result to the
1410 * 'ret' string list.
1413 check_with_vfs_type (guestfs_h *g, const char *device,
1414 char ***ret, size_t *ret_size)
1418 guestfs_error_handler_cb old_error_cb = g->error_cb;
1420 char *vfs_type = guestfs_vfs_type (g, device);
1421 g->error_cb = old_error_cb;
1424 v = safe_strdup (g, "unknown");
1426 /* Ignore all "*_member" strings. In libblkid these are returned
1427 * for things which are members of some RAID or LVM set, most
1428 * importantly "LVM2_member" which is a PV.
1430 size_t n = strlen (vfs_type);
1431 if (n >= 7 && STREQ (&vfs_type[n-7], "_member")) {
1436 /* Ignore LUKS-encrypted partitions. These are also containers. */
1437 if (STREQ (vfs_type, "crypto_LUKS")) {
1445 /* Extend the return array. */
1446 size_t i = *ret_size;
1448 *ret = safe_realloc (g, *ret, (*ret_size + 1) * sizeof (char *));
1449 (*ret)[i] = safe_strdup (g, device);