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
41 #include "ignore-value.h"
45 #include "guestfs-internal.h"
46 #include "guestfs-internal-actions.h"
47 #include "guestfs_protocol.h"
49 #if defined(HAVE_PCRE) && defined(HAVE_HIVEX)
51 /* The main inspection code. */
53 guestfs__inspect_os (guestfs_h *g)
55 /* Remove any information previously stored in the handle. */
56 guestfs___free_inspect_info (g);
58 if (guestfs_umount_all (g) == -1)
61 /* Iterate over all possible devices. Try to mount each
62 * (read-only). Examine ones which contain filesystems and add that
63 * information to the handle.
65 /* Look to see if any devices directly contain filesystems (RHBZ#590167). */
67 devices = guestfs_list_devices (g);
72 for (i = 0; devices[i] != NULL; ++i) {
73 if (guestfs___check_for_filesystem_on (g, devices[i], 1, 0) == -1) {
74 guestfs___free_string_list (devices);
75 guestfs___free_inspect_info (g);
79 guestfs___free_string_list (devices);
81 /* Look at all partitions. */
83 partitions = guestfs_list_partitions (g);
84 if (partitions == NULL) {
85 guestfs___free_inspect_info (g);
89 for (i = 0; partitions[i] != NULL; ++i) {
90 if (guestfs___check_for_filesystem_on (g, partitions[i], 0, i+1) == -1) {
91 guestfs___free_string_list (partitions);
92 guestfs___free_inspect_info (g);
96 guestfs___free_string_list (partitions);
98 /* Look at all LVs. */
99 if (guestfs___feature_available (g, "lvm2")) {
101 lvs = guestfs_lvs (g);
103 guestfs___free_inspect_info (g);
107 for (i = 0; lvs[i] != NULL; ++i) {
108 if (guestfs___check_for_filesystem_on (g, lvs[i], 0, 0) == -1) {
109 guestfs___free_string_list (lvs);
110 guestfs___free_inspect_info (g);
114 guestfs___free_string_list (lvs);
117 /* At this point we have, in the handle, a list of all filesystems
118 * found and data about each one. Now we assemble the list of
119 * filesystems which are root devices and return that to the user.
120 * Fall through to guestfs__inspect_get_roots to do that.
122 char **ret = guestfs__inspect_get_roots (g);
124 guestfs___free_inspect_info (g);
129 guestfs__inspect_get_roots (guestfs_h *g)
131 /* NB. Doesn't matter if g->nr_fses == 0. We just return an empty
137 for (i = 0; i < g->nr_fses; ++i)
138 if (g->fses[i].is_root)
141 char **ret = calloc (count+1, sizeof (char *));
143 perrorf (g, "calloc");
148 for (i = 0; i < g->nr_fses; ++i) {
149 if (g->fses[i].is_root) {
150 ret[count] = safe_strdup (g, g->fses[i].device);
160 guestfs__inspect_get_type (guestfs_h *g, const char *root)
162 struct inspect_fs *fs = guestfs___search_for_root (g, root);
168 case OS_TYPE_LINUX: ret = safe_strdup (g, "linux"); break;
169 case OS_TYPE_WINDOWS: ret = safe_strdup (g, "windows"); break;
170 case OS_TYPE_FREEBSD: ret = safe_strdup (g, "freebsd"); break;
171 case OS_TYPE_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
178 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
180 struct inspect_fs *fs = guestfs___search_for_root (g, root);
184 return safe_strdup (g, fs->arch ? : "unknown");
188 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
190 struct inspect_fs *fs = guestfs___search_for_root (g, root);
195 switch (fs->distro) {
196 case OS_DISTRO_ARCHLINUX: ret = safe_strdup (g, "archlinux"); break;
197 case OS_DISTRO_CENTOS: ret = safe_strdup (g, "centos"); break;
198 case OS_DISTRO_DEBIAN: ret = safe_strdup (g, "debian"); break;
199 case OS_DISTRO_FEDORA: ret = safe_strdup (g, "fedora"); break;
200 case OS_DISTRO_GENTOO: ret = safe_strdup (g, "gentoo"); break;
201 case OS_DISTRO_LINUX_MINT: ret = safe_strdup (g, "linuxmint"); break;
202 case OS_DISTRO_MANDRIVA: ret = safe_strdup (g, "mandriva"); break;
203 case OS_DISTRO_MEEGO: ret = safe_strdup (g, "meego"); break;
204 case OS_DISTRO_PARDUS: ret = safe_strdup (g, "pardus"); break;
205 case OS_DISTRO_REDHAT_BASED: ret = safe_strdup (g, "redhat-based"); break;
206 case OS_DISTRO_RHEL: ret = safe_strdup (g, "rhel"); break;
207 case OS_DISTRO_SCIENTIFIC_LINUX: ret = safe_strdup (g, "scientificlinux"); break;
208 case OS_DISTRO_SLACKWARE: ret = safe_strdup (g, "slackware"); break;
209 case OS_DISTRO_WINDOWS: ret = safe_strdup (g, "windows"); break;
210 case OS_DISTRO_UBUNTU: ret = safe_strdup (g, "ubuntu"); break;
211 case OS_DISTRO_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
218 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
220 struct inspect_fs *fs = guestfs___search_for_root (g, root);
224 return fs->major_version;
228 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
230 struct inspect_fs *fs = guestfs___search_for_root (g, root);
234 return fs->minor_version;
238 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
240 struct inspect_fs *fs = guestfs___search_for_root (g, root);
244 return safe_strdup (g, fs->product_name ? : "unknown");
248 guestfs__inspect_get_product_variant (guestfs_h *g, const char *root)
250 struct inspect_fs *fs = guestfs___search_for_root (g, root);
254 return safe_strdup (g, fs->product_variant ? : "unknown");
258 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
260 struct inspect_fs *fs = guestfs___search_for_root (g, root);
264 if (!fs->windows_systemroot) {
265 error (g, _("not a Windows guest, or systemroot could not be determined"));
269 return safe_strdup (g, fs->windows_systemroot);
273 guestfs__inspect_get_windows_current_control_set (guestfs_h *g,
276 struct inspect_fs *fs = guestfs___search_for_root (g, root);
280 if (!fs->windows_current_control_set) {
281 error (g, _("not a Windows guest, or CurrentControlSet could not be determined"));
285 return safe_strdup (g, fs->windows_current_control_set);
289 guestfs__inspect_get_format (guestfs_h *g, const char *root)
291 struct inspect_fs *fs = guestfs___search_for_root (g, root);
296 switch (fs->format) {
297 case OS_FORMAT_INSTALLED: ret = safe_strdup (g, "installed"); break;
298 case OS_FORMAT_INSTALLER: ret = safe_strdup (g, "installer"); break;
299 case OS_FORMAT_UNKNOWN: default: ret = safe_strdup (g, "unknown"); break;
306 guestfs__inspect_is_live (guestfs_h *g, const char *root)
308 struct inspect_fs *fs = guestfs___search_for_root (g, root);
312 return fs->is_live_disk;
316 guestfs__inspect_is_netinst (guestfs_h *g, const char *root)
318 struct inspect_fs *fs = guestfs___search_for_root (g, root);
322 return fs->is_netinst_disk;
326 guestfs__inspect_is_multipart (guestfs_h *g, const char *root)
328 struct inspect_fs *fs = guestfs___search_for_root (g, root);
332 return fs->is_multipart_disk;
336 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
338 struct inspect_fs *fs = guestfs___search_for_root (g, root);
344 /* If no fstab information (Windows) return just the root. */
345 if (fs->nr_fstab == 0) {
346 ret = calloc (3, sizeof (char *));
347 ret[0] = safe_strdup (g, "/");
348 ret[1] = safe_strdup (g, root);
353 #define CRITERION fs->fstab[i].mountpoint[0] == '/'
355 for (i = 0; i < fs->nr_fstab; ++i)
359 /* Hashtables have 2N+1 entries. */
360 ret = calloc (2*count+1, sizeof (char *));
362 perrorf (g, "calloc");
367 for (i = 0; i < fs->nr_fstab; ++i)
369 ret[2*count] = safe_strdup (g, fs->fstab[i].mountpoint);
370 ret[2*count+1] = safe_strdup (g, fs->fstab[i].device);
379 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
381 struct inspect_fs *fs = guestfs___search_for_root (g, root);
387 /* If no fstab information (Windows) return just the root. */
388 if (fs->nr_fstab == 0) {
389 ret = calloc (2, sizeof (char *));
390 ret[0] = safe_strdup (g, root);
395 ret = calloc (fs->nr_fstab + 1, sizeof (char *));
397 perrorf (g, "calloc");
402 for (i = 0; i < fs->nr_fstab; ++i)
403 ret[i] = safe_strdup (g, fs->fstab[i].device);
409 guestfs__inspect_get_drive_mappings (guestfs_h *g, const char *root)
413 struct inspect_fs *fs;
415 fs = guestfs___search_for_root (g, root);
419 /* If no drive mappings, return an empty hashtable. */
420 if (!fs->drive_mappings)
423 for (count = 0; fs->drive_mappings[count] != NULL; count++)
427 ret = calloc (count+1, sizeof (char *));
429 perrorf (g, "calloc");
433 /* We need to make a deep copy of the hashtable since the caller
436 for (i = 0; i < count; ++i)
437 ret[i] = safe_strdup (g, fs->drive_mappings[i]);
445 guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
447 struct inspect_fs *fs = guestfs___search_for_root (g, root);
452 switch (fs->package_format) {
453 case OS_PACKAGE_FORMAT_RPM: ret = safe_strdup (g, "rpm"); break;
454 case OS_PACKAGE_FORMAT_DEB: ret = safe_strdup (g, "deb"); break;
455 case OS_PACKAGE_FORMAT_PACMAN: ret = safe_strdup (g, "pacman"); break;
456 case OS_PACKAGE_FORMAT_EBUILD: ret = safe_strdup (g, "ebuild"); break;
457 case OS_PACKAGE_FORMAT_PISI: ret = safe_strdup (g, "pisi"); break;
458 case OS_PACKAGE_FORMAT_UNKNOWN:
460 ret = safe_strdup (g, "unknown");
468 guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
470 struct inspect_fs *fs = guestfs___search_for_root (g, root);
475 switch (fs->package_management) {
476 case OS_PACKAGE_MANAGEMENT_YUM: ret = safe_strdup (g, "yum"); break;
477 case OS_PACKAGE_MANAGEMENT_UP2DATE: ret = safe_strdup (g, "up2date"); break;
478 case OS_PACKAGE_MANAGEMENT_APT: ret = safe_strdup (g, "apt"); break;
479 case OS_PACKAGE_MANAGEMENT_PACMAN: ret = safe_strdup (g, "pacman"); break;
480 case OS_PACKAGE_MANAGEMENT_PORTAGE: ret = safe_strdup (g, "portage"); break;
481 case OS_PACKAGE_MANAGEMENT_PISI: ret = safe_strdup (g, "pisi"); break;
482 case OS_PACKAGE_MANAGEMENT_URPMI: ret = safe_strdup (g, "urpmi"); break;
483 case OS_PACKAGE_MANAGEMENT_UNKNOWN:
485 ret = safe_strdup (g, "unknown");
493 guestfs__inspect_get_hostname (guestfs_h *g, const char *root)
495 struct inspect_fs *fs = guestfs___search_for_root (g, root);
499 return safe_strdup (g, fs->hostname ? : "unknown");
502 /* Download a guest file to a local temporary file. The file is
503 * downloaded into g->tmpdir, unless it already exists in g->tmpdir.
504 * The final name will be g->tmpdir + "/" + basename. Refuse to
505 * download the guest file if it is larger than max_size. The caller
506 * does not need to delete the temporary file after use: it will be
507 * deleted when the handle is cleaned up.
510 guestfs___download_to_tmp (guestfs_h *g, const char *filename,
511 const char *basename, int64_t max_size)
513 int tmpdirfd, fd, r = -1;
517 tmpdirfd = open (g->tmpdir, O_RDONLY);
518 if (tmpdirfd == -1) {
519 perrorf (g, _("%s: temporary directory not found"), g->tmpdir);
523 /* If the file has already been downloaded, return. */
524 if (faccessat (tmpdirfd, basename, R_OK, 0) == 0) {
529 /* Check size of remote file. */
530 size = guestfs_filesize (g, filename);
532 /* guestfs_filesize failed and has already set error in handle */
534 if (size > max_size) {
535 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
540 fd = openat (tmpdirfd, basename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, 0600);
542 perrorf (g, "openat: %s/%s", g->tmpdir, basename);
546 snprintf (buf, sizeof buf, "/dev/fd/%d", fd);
548 if (guestfs_download (g, filename, buf) == -1) {
549 unlinkat (tmpdirfd, basename, 0);
554 if (close (fd) == -1) {
555 perrorf (g, "close: %s/%s", g->tmpdir, basename);
556 unlinkat (tmpdirfd, basename, 0);
569 guestfs___search_for_root (guestfs_h *g, const char *root)
571 if (g->nr_fses == 0) {
572 error (g, _("no inspection data: call guestfs_inspect_os first"));
577 struct inspect_fs *fs;
578 for (i = 0; i < g->nr_fses; ++i) {
580 if (fs->is_root && STREQ (root, fs->device))
584 error (g, _("%s: root device not found: only call this function with a root device previously returned by guestfs_inspect_os"),
589 #else /* no PCRE or hivex at compile time */
591 /* XXX These functions should be in an optgroup. */
593 #define NOT_IMPL(r) \
594 error (g, _("inspection API not available since this version of libguestfs was compiled without PCRE or hivex libraries")); \
598 guestfs__inspect_os (guestfs_h *g)
604 guestfs__inspect_get_roots (guestfs_h *g)
610 guestfs__inspect_get_type (guestfs_h *g, const char *root)
616 guestfs__inspect_get_arch (guestfs_h *g, const char *root)
622 guestfs__inspect_get_distro (guestfs_h *g, const char *root)
628 guestfs__inspect_get_major_version (guestfs_h *g, const char *root)
634 guestfs__inspect_get_minor_version (guestfs_h *g, const char *root)
640 guestfs__inspect_get_product_name (guestfs_h *g, const char *root)
646 guestfs__inspect_get_product_variant (guestfs_h *g, const char *root)
652 guestfs__inspect_get_windows_systemroot (guestfs_h *g, const char *root)
658 guestfs__inspect_get_windows_current_control_set (guestfs_h *g,
665 guestfs__inspect_get_mountpoints (guestfs_h *g, const char *root)
671 guestfs__inspect_get_filesystems (guestfs_h *g, const char *root)
677 guestfs__inspect_get_drive_mappings (guestfs_h *g, const char *root)
683 guestfs__inspect_get_package_format (guestfs_h *g, const char *root)
689 guestfs__inspect_get_package_management (guestfs_h *g, const char *root)
695 guestfs__inspect_get_hostname (guestfs_h *g, const char *root)
701 guestfs__inspect_get_format (guestfs_h *g, const char *root)
707 guestfs__inspect_is_live (guestfs_h *g, const char *root)
713 guestfs__inspect_is_netinst (guestfs_h *g, const char *root)
719 guestfs__inspect_is_multipart (guestfs_h *g, const char *root)
724 #endif /* no PCRE or hivex at compile time */
727 guestfs___free_inspect_info (guestfs_h *g)
730 for (i = 0; i < g->nr_fses; ++i) {
731 free (g->fses[i].device);
732 free (g->fses[i].product_name);
733 free (g->fses[i].product_variant);
734 free (g->fses[i].arch);
735 free (g->fses[i].hostname);
736 free (g->fses[i].windows_systemroot);
737 free (g->fses[i].windows_current_control_set);
739 for (j = 0; j < g->fses[i].nr_fstab; ++j) {
740 free (g->fses[i].fstab[j].device);
741 free (g->fses[i].fstab[j].mountpoint);
743 free (g->fses[i].fstab);
744 if (g->fses[i].drive_mappings)
745 guestfs___free_string_list (g->fses[i].drive_mappings);
752 /* In the Perl code this is a public function. */
754 guestfs___feature_available (guestfs_h *g, const char *feature)
756 /* If there's an error we should ignore it, so to do that we have to
757 * temporarily replace the error handler with a null one.
759 guestfs_error_handler_cb old_error_cb = g->error_cb;
762 const char *groups[] = { feature, NULL };
763 int r = guestfs_available (g, (char * const *) groups);
765 g->error_cb = old_error_cb;
767 return r == 0 ? 1 : 0;