2 * Copyright (C) 2010-2011 Red Hat Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
39 #include "ignore-value.h"
43 #include "guestfs-internal.h"
44 #include "guestfs-internal-actions.h"
45 #include "guestfs_protocol.h"
47 #if defined(HAVE_HIVEX)
49 /* The main inspection code. */
51 guestfs__inspect_os (guestfs_h *g)
53 /* Remove any information previously stored in the handle. */
54 guestfs___free_inspect_info (g);
56 if (guestfs_umount_all (g) == -1)
59 /* Iterate over all possible devices. Try to mount each
60 * (read-only). Examine ones which contain filesystems and add that
61 * information to the handle.
63 /* Look to see if any devices directly contain filesystems (RHBZ#590167). */
64 char **devices = guestfs_list_devices (g);
69 for (i = 0; devices[i] != NULL; ++i) {
70 if (guestfs___check_for_filesystem_on (g, devices[i], 1, 0) == -1) {
71 guestfs___free_string_list (devices);
72 guestfs___free_inspect_info (g);
76 guestfs___free_string_list (devices);
78 /* Look at all partitions. */
79 char **partitions = guestfs_list_partitions (g);
80 if (partitions == NULL) {
81 guestfs___free_inspect_info (g);
85 for (i = 0; partitions[i] != NULL; ++i) {
86 if (guestfs___check_for_filesystem_on (g, partitions[i], 0, i+1) == -1) {
87 guestfs___free_string_list (partitions);
88 guestfs___free_inspect_info (g);
92 guestfs___free_string_list (partitions);
94 /* Look at all LVs. */
95 if (guestfs___feature_available (g, "lvm2")) {
97 lvs = guestfs_lvs (g);
99 guestfs___free_inspect_info (g);
103 for (i = 0; lvs[i] != NULL; ++i) {
104 if (guestfs___check_for_filesystem_on (g, lvs[i], 0, 0) == -1) {
105 guestfs___free_string_list (lvs);
106 guestfs___free_inspect_info (g);
110 guestfs___free_string_list (lvs);
113 /* At this point we have, in the handle, a list of all filesystems
114 * found and data about each one. Now we assemble the list of
115 * filesystems which are root devices and return that to the user.
116 * Fall through to guestfs__inspect_get_roots to do that.
118 char **ret = guestfs__inspect_get_roots (g);
120 guestfs___free_inspect_info (g);
125 compare_strings (const void *vp1, const void *vp2)
127 const char *s1 = * (char * const *) vp1;
128 const char *s2 = * (char * const *) vp2;
130 return strcmp (s1, s2);
134 guestfs__inspect_get_roots (guestfs_h *g)
136 /* NB. Doesn't matter if g->nr_fses == 0. We just return an empty
142 for (i = 0; i < g->nr_fses; ++i)
143 if (g->fses[i].is_root)
146 char **ret = calloc (count+1, sizeof (char *));
148 perrorf (g, "calloc");
153 for (i = 0; i < g->nr_fses; ++i) {
154 if (g->fses[i].is_root) {
155 ret[count] = safe_strdup (g, g->fses[i].device);
161 qsort (ret, count, sizeof (char *), compare_strings);
167 guestfs__inspect_get_type (guestfs_h *g, const char *root)
169 struct inspect_fs *fs = guestfs___search_for_root (g, root);
175 case OS_TYPE_FREEBSD: ret = safe_strdup (g, "freebsd"); break;
176 case OS_TYPE_LINUX: ret = safe_strdup (g, "linux"); break;
177 case OS_TYPE_NETBSD: ret = safe_strdup (g, "netbsd"); break;
178 case OS_TYPE_WINDOWS: ret = safe_strdup (g, "windows"); break;
179 case OS_TYPE_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
186 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
188 struct inspect_fs *fs = guestfs___search_for_root (g, root);
192 return safe_strdup (g, fs->arch ? : "unknown");
196 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
198 struct inspect_fs *fs = guestfs___search_for_root (g, root);
203 switch (fs->distro) {
204 case OS_DISTRO_ARCHLINUX: ret = safe_strdup (g, "archlinux"); break;
205 case OS_DISTRO_CENTOS: ret = safe_strdup (g, "centos"); break;
206 case OS_DISTRO_DEBIAN: ret = safe_strdup (g, "debian"); break;
207 case OS_DISTRO_FEDORA: ret = safe_strdup (g, "fedora"); break;
208 case OS_DISTRO_GENTOO: ret = safe_strdup (g, "gentoo"); break;
209 case OS_DISTRO_LINUX_MINT: ret = safe_strdup (g, "linuxmint"); break;
210 case OS_DISTRO_MAGEIA: ret = safe_strdup (g, "mageia"); break;
211 case OS_DISTRO_MANDRIVA: ret = safe_strdup (g, "mandriva"); break;
212 case OS_DISTRO_MEEGO: ret = safe_strdup (g, "meego"); break;
213 case OS_DISTRO_OPENSUSE: ret = safe_strdup (g, "opensuse"); break;
214 case OS_DISTRO_PARDUS: ret = safe_strdup (g, "pardus"); break;
215 case OS_DISTRO_REDHAT_BASED: ret = safe_strdup (g, "redhat-based"); break;
216 case OS_DISTRO_RHEL: ret = safe_strdup (g, "rhel"); break;
217 case OS_DISTRO_SCIENTIFIC_LINUX: ret = safe_strdup (g, "scientificlinux"); break;
218 case OS_DISTRO_SLACKWARE: ret = safe_strdup (g, "slackware"); break;
219 case OS_DISTRO_TTYLINUX: ret = safe_strdup (g, "ttylinux"); break;
220 case OS_DISTRO_WINDOWS: ret = safe_strdup (g, "windows"); break;
221 case OS_DISTRO_UBUNTU: ret = safe_strdup (g, "ubuntu"); break;
222 case OS_DISTRO_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
229 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
231 struct inspect_fs *fs = guestfs___search_for_root (g, root);
235 return fs->major_version;
239 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
241 struct inspect_fs *fs = guestfs___search_for_root (g, root);
245 return fs->minor_version;
249 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
251 struct inspect_fs *fs = guestfs___search_for_root (g, root);
255 return safe_strdup (g, fs->product_name ? : "unknown");
259 guestfs__inspect_get_product_variant (guestfs_h *g, const char *root)
261 struct inspect_fs *fs = guestfs___search_for_root (g, root);
265 return safe_strdup (g, fs->product_variant ? : "unknown");
269 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
271 struct inspect_fs *fs = guestfs___search_for_root (g, root);
275 if (!fs->windows_systemroot) {
276 error (g, _("not a Windows guest, or systemroot could not be determined"));
280 return safe_strdup (g, fs->windows_systemroot);
284 guestfs__inspect_get_windows_current_control_set (guestfs_h *g,
287 struct inspect_fs *fs = guestfs___search_for_root (g, root);
291 if (!fs->windows_current_control_set) {
292 error (g, _("not a Windows guest, or CurrentControlSet could not be determined"));
296 return safe_strdup (g, fs->windows_current_control_set);
300 guestfs__inspect_get_format (guestfs_h *g, const char *root)
302 struct inspect_fs *fs = guestfs___search_for_root (g, root);
307 switch (fs->format) {
308 case OS_FORMAT_INSTALLED: ret = safe_strdup (g, "installed"); break;
309 case OS_FORMAT_INSTALLER: ret = safe_strdup (g, "installer"); break;
310 case OS_FORMAT_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
317 guestfs__inspect_is_live (guestfs_h *g, const char *root)
319 struct inspect_fs *fs = guestfs___search_for_root (g, root);
323 return fs->is_live_disk;
327 guestfs__inspect_is_netinst (guestfs_h *g, const char *root)
329 struct inspect_fs *fs = guestfs___search_for_root (g, root);
333 return fs->is_netinst_disk;
337 guestfs__inspect_is_multipart (guestfs_h *g, const char *root)
339 struct inspect_fs *fs = guestfs___search_for_root (g, root);
343 return fs->is_multipart_disk;
347 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
349 struct inspect_fs *fs = guestfs___search_for_root (g, root);
355 /* If no fstab information (Windows) return just the root. */
356 if (fs->nr_fstab == 0) {
357 ret = calloc (3, sizeof (char *));
358 ret[0] = safe_strdup (g, "/");
359 ret[1] = safe_strdup (g, root);
364 #define CRITERION fs->fstab[i].mountpoint[0] == '/'
366 for (i = 0; i < fs->nr_fstab; ++i)
370 /* Hashtables have 2N+1 entries. */
371 ret = calloc (2*count+1, sizeof (char *));
373 perrorf (g, "calloc");
378 for (i = 0; i < fs->nr_fstab; ++i)
380 ret[2*count] = safe_strdup (g, fs->fstab[i].mountpoint);
381 ret[2*count+1] = safe_strdup (g, fs->fstab[i].device);
390 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
392 struct inspect_fs *fs = guestfs___search_for_root (g, root);
398 /* If no fstab information (Windows) return just the root. */
399 if (fs->nr_fstab == 0) {
400 ret = calloc (2, sizeof (char *));
401 ret[0] = safe_strdup (g, root);
406 ret = calloc (fs->nr_fstab + 1, sizeof (char *));
408 perrorf (g, "calloc");
413 for (i = 0; i < fs->nr_fstab; ++i)
414 ret[i] = safe_strdup (g, fs->fstab[i].device);
420 guestfs__inspect_get_drive_mappings (guestfs_h *g, const char *root)
424 struct inspect_fs *fs;
426 fs = guestfs___search_for_root (g, root);
430 /* If no drive mappings, return an empty hashtable. */
431 if (!fs->drive_mappings)
434 for (count = 0; fs->drive_mappings[count] != NULL; count++)
438 ret = calloc (count+1, sizeof (char *));
440 perrorf (g, "calloc");
444 /* We need to make a deep copy of the hashtable since the caller
447 for (i = 0; i < count; ++i)
448 ret[i] = safe_strdup (g, fs->drive_mappings[i]);
456 guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
458 struct inspect_fs *fs = guestfs___search_for_root (g, root);
463 switch (fs->package_format) {
464 case OS_PACKAGE_FORMAT_RPM: ret = safe_strdup (g, "rpm"); break;
465 case OS_PACKAGE_FORMAT_DEB: ret = safe_strdup (g, "deb"); break;
466 case OS_PACKAGE_FORMAT_PACMAN: ret = safe_strdup (g, "pacman"); break;
467 case OS_PACKAGE_FORMAT_EBUILD: ret = safe_strdup (g, "ebuild"); break;
468 case OS_PACKAGE_FORMAT_PISI: ret = safe_strdup (g, "pisi"); break;
469 case OS_PACKAGE_FORMAT_PKGSRC: ret = safe_strdup (g, "pkgsrc"); break;
470 case OS_PACKAGE_FORMAT_UNKNOWN:
472 ret = safe_strdup (g, "unknown");
480 guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
482 struct inspect_fs *fs = guestfs___search_for_root (g, root);
487 switch (fs->package_management) {
488 case OS_PACKAGE_MANAGEMENT_YUM: ret = safe_strdup (g, "yum"); break;
489 case OS_PACKAGE_MANAGEMENT_UP2DATE: ret = safe_strdup (g, "up2date"); break;
490 case OS_PACKAGE_MANAGEMENT_APT: ret = safe_strdup (g, "apt"); break;
491 case OS_PACKAGE_MANAGEMENT_PACMAN: ret = safe_strdup (g, "pacman"); break;
492 case OS_PACKAGE_MANAGEMENT_PORTAGE: ret = safe_strdup (g, "portage"); break;
493 case OS_PACKAGE_MANAGEMENT_PISI: ret = safe_strdup (g, "pisi"); break;
494 case OS_PACKAGE_MANAGEMENT_URPMI: ret = safe_strdup (g, "urpmi"); break;
495 case OS_PACKAGE_MANAGEMENT_ZYPPER: ret = safe_strdup (g, "zypper"); break;
496 case OS_PACKAGE_MANAGEMENT_UNKNOWN:
498 ret = safe_strdup (g, "unknown");
506 guestfs__inspect_get_hostname (guestfs_h *g, const char *root)
508 struct inspect_fs *fs = guestfs___search_for_root (g, root);
512 return safe_strdup (g, fs->hostname ? : "unknown");
515 #else /* no hivex at compile time */
517 /* XXX These functions should be in an optgroup. */
519 #define NOT_IMPL(r) \
520 error (g, _("inspection API not available since this version of libguestfs was compiled without the hivex library")); \
524 guestfs__inspect_os (guestfs_h *g)
530 guestfs__inspect_get_roots (guestfs_h *g)
536 guestfs__inspect_get_type (guestfs_h *g, const char *root)
542 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
548 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
554 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
560 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
566 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
572 guestfs__inspect_get_product_variant (guestfs_h *g, const char *root)
578 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
584 guestfs__inspect_get_windows_current_control_set (guestfs_h *g,
591 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
597 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
603 guestfs__inspect_get_drive_mappings (guestfs_h *g, const char *root)
609 guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
615 guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
621 guestfs__inspect_get_hostname (guestfs_h *g, const char *root)
627 guestfs__inspect_get_format (guestfs_h *g, const char *root)
633 guestfs__inspect_is_live (guestfs_h *g, const char *root)
639 guestfs__inspect_is_netinst (guestfs_h *g, const char *root)
645 guestfs__inspect_is_multipart (guestfs_h *g, const char *root)
650 #endif /* no hivex at compile time */
653 guestfs___free_inspect_info (guestfs_h *g)
656 for (i = 0; i < g->nr_fses; ++i) {
657 free (g->fses[i].device);
658 free (g->fses[i].product_name);
659 free (g->fses[i].product_variant);
660 free (g->fses[i].arch);
661 free (g->fses[i].hostname);
662 free (g->fses[i].windows_systemroot);
663 free (g->fses[i].windows_current_control_set);
665 for (j = 0; j < g->fses[i].nr_fstab; ++j) {
666 free (g->fses[i].fstab[j].device);
667 free (g->fses[i].fstab[j].mountpoint);
669 free (g->fses[i].fstab);
670 if (g->fses[i].drive_mappings)
671 guestfs___free_string_list (g->fses[i].drive_mappings);
678 /* In the Perl code this is a public function. */
680 guestfs___feature_available (guestfs_h *g, const char *feature)
682 /* If there's an error we should ignore it, so to do that we have to
683 * temporarily replace the error handler with a null one.
685 guestfs_error_handler_cb old_error_cb = g->error_cb;
688 const char *groups[] = { feature, NULL };
689 int r = guestfs_available (g, (char * const *) groups);
691 g->error_cb = old_error_cb;
693 return r == 0 ? 1 : 0;
696 /* Download a guest file to a local temporary file. The file is
697 * cached in the temporary directory, and is not downloaded again.
699 * The name of the temporary (downloaded) file is returned. The
700 * caller must free the pointer, but does *not* need to delete the
701 * temporary file. It will be deleted when the handle is closed.
703 * Refuse to download the guest file if it is larger than max_size.
704 * On this and other errors, NULL is returned.
706 * There is actually one cache per 'struct inspect_fs *' in order
707 * to handle the case of multiple roots.
710 guestfs___download_to_tmp (guestfs_h *g, struct inspect_fs *fs,
711 const char *filename,
712 const char *basename, int64_t max_size)
719 /* Make the basename unique by prefixing it with the fs number. */
720 if (asprintf (&r, "%s/%td-%s", g->tmpdir, fs - g->fses, basename) == -1) {
721 perrorf (g, "asprintf");
725 /* If the file has already been downloaded, return. */
726 if (access (r, R_OK) == 0)
729 /* Check size of remote file. */
730 size = guestfs_filesize (g, filename);
732 /* guestfs_filesize failed and has already set error in handle */
734 if (size > max_size) {
735 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
740 fd = open (r, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, 0600);
742 perrorf (g, "open: %s", r);
746 snprintf (devfd, sizeof devfd, "/dev/fd/%d", fd);
748 if (guestfs_download (g, filename, devfd) == -1) {
754 if (close (fd) == -1) {
755 perrorf (g, "close: %s", r);
768 guestfs___search_for_root (guestfs_h *g, const char *root)
770 if (g->nr_fses == 0) {
771 error (g, _("no inspection data: call guestfs_inspect_os first"));
776 struct inspect_fs *fs;
777 for (i = 0; i < g->nr_fses; ++i) {
779 if (fs->is_root && STREQ (root, fs->device))
783 error (g, _("%s: root device not found: only call this function with a root device previously returned by guestfs_inspect_os"),