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 /* Compile all the regular expressions once when the shared library is
52 * loaded. PCRE is thread safe so we're supposedly OK here if
53 * multiple threads call into the libguestfs API functions below
56 static pcre *re_first_partition;
57 static pcre *re_major_minor;
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))); \
77 COMPILE (re_first_partition, "^/dev/(?:h|s|v)d.1$", 0);
78 COMPILE (re_major_minor, "(\\d+)\\.(\\d+)", 0);
84 pcre_free (re_first_partition);
85 pcre_free (re_major_minor);
88 static int check_filesystem (guestfs_h *g, const char *device, int is_block, int is_partnum);
89 static void check_package_format (guestfs_h *g, struct inspect_fs *fs);
90 static void check_package_management (guestfs_h *g, struct inspect_fs *fs);
91 static int extend_fses (guestfs_h *g);
93 /* Find out if 'device' contains a filesystem. If it does, add
94 * another entry in g->fses.
97 guestfs___check_for_filesystem_on (guestfs_h *g, const char *device,
98 int is_block, int is_partnum)
100 /* Get vfs-type in order to check if it's a Linux(?) swap device.
101 * If there's an error we should ignore it, so to do that we have to
102 * temporarily replace the error handler with a null one.
104 guestfs_error_handler_cb old_error_cb = g->error_cb;
106 char *vfs_type = guestfs_vfs_type (g, device);
107 g->error_cb = old_error_cb;
109 int is_swap = vfs_type && STREQ (vfs_type, "swap");
111 debug (g, "check_for_filesystem_on: %s %d %d (%s)",
112 device, is_block, is_partnum,
113 vfs_type ? vfs_type : "failed to get vfs type");
117 if (extend_fses (g) == -1)
119 g->fses[g->nr_fses-1].is_swap = 1;
123 /* Try mounting the device. As above, ignore errors. */
125 int r = guestfs_mount_ro (g, device, "/");
126 if (r == -1 && vfs_type && STREQ (vfs_type, "ufs")) /* Hack for the *BSDs. */
127 r = guestfs_mount_vfs (g, "ro,ufstype=ufs2", "ufs", device, "/");
129 g->error_cb = old_error_cb;
133 /* Do the rest of the checks. */
134 r = check_filesystem (g, device, is_block, is_partnum);
136 /* Unmount the filesystem. */
137 if (guestfs_umount_all (g) == -1)
143 /* is_block and is_partnum are just hints: is_block is true if the
144 * filesystem is a whole block device (eg. /dev/sda). is_partnum
145 * is > 0 if the filesystem is a direct partition, and in this case
146 * it is the partition number counting from 1
147 * (eg. /dev/sda1 => is_partnum == 1).
150 check_filesystem (guestfs_h *g, const char *device,
151 int is_block, int is_partnum)
153 if (extend_fses (g) == -1)
156 struct inspect_fs *fs = &g->fses[g->nr_fses-1];
158 fs->device = safe_strdup (g, device);
159 fs->is_mountable = 1;
161 /* Optimize some of the tests by avoiding multiple tests of the same thing. */
162 int is_dir_etc = guestfs_is_dir (g, "/etc") > 0;
163 int is_dir_bin = guestfs_is_dir (g, "/bin") > 0;
164 int is_dir_share = guestfs_is_dir (g, "/share") > 0;
167 if (guestfs_is_file (g, "/grub/menu.lst") > 0 ||
168 guestfs_is_file (g, "/grub/grub.conf") > 0)
169 fs->content = FS_CONTENT_LINUX_BOOT;
171 else if (is_dir_etc &&
173 guestfs_is_file (g, "/etc/freebsd-update.conf") > 0 &&
174 guestfs_is_file (g, "/etc/fstab") > 0) {
175 /* Ignore /dev/sda1 which is a shadow of the real root filesystem
176 * that is probably /dev/sda5 (see:
177 * http://www.freebsd.org/doc/handbook/disk-organization.html)
179 if (match (g, device, re_first_partition))
183 fs->content = FS_CONTENT_FREEBSD_ROOT;
184 fs->format = OS_FORMAT_INSTALLED;
185 if (guestfs___check_freebsd_root (g, fs) == -1)
189 else if (is_dir_etc &&
191 guestfs_is_file (g, "/etc/fstab") > 0) {
193 fs->content = FS_CONTENT_LINUX_ROOT;
194 fs->format = OS_FORMAT_INSTALLED;
195 if (guestfs___check_linux_root (g, fs) == -1)
198 /* Linux /usr/local? */
199 else if (is_dir_etc &&
202 guestfs_exists (g, "/local") == 0 &&
203 guestfs_is_file (g, "/etc/fstab") == 0)
204 fs->content = FS_CONTENT_LINUX_USR_LOCAL;
206 else if (is_dir_etc &&
209 guestfs_exists (g, "/local") > 0 &&
210 guestfs_is_file (g, "/etc/fstab") == 0)
211 fs->content = FS_CONTENT_LINUX_USR;
213 else if (guestfs_is_dir (g, "/log") > 0 &&
214 guestfs_is_dir (g, "/run") > 0 &&
215 guestfs_is_dir (g, "/spool") > 0)
216 fs->content = FS_CONTENT_LINUX_VAR;
218 else if (guestfs___has_windows_systemroot (g) >= 0) {
220 fs->content = FS_CONTENT_WINDOWS_ROOT;
221 fs->format = OS_FORMAT_INSTALLED;
222 if (guestfs___check_windows_root (g, fs) == -1)
225 /* Windows volume with installed applications (but not root)? */
226 else if (guestfs___is_dir_nocase (g, "/System Volume Information") > 0 &&
227 guestfs___is_dir_nocase (g, "/Program Files") > 0)
228 fs->content = FS_CONTENT_WINDOWS_VOLUME_WITH_APPS;
229 /* Windows volume (but not root)? */
230 else if (guestfs___is_dir_nocase (g, "/System Volume Information") > 0)
231 fs->content = FS_CONTENT_WINDOWS_VOLUME;
232 /* Install CD/disk? Skip these checks if it's not a whole device
233 * (eg. CD) or the first partition (eg. bootable USB key).
235 else if ((is_block || is_partnum == 1) &&
236 (guestfs_is_file (g, "/isolinux/isolinux.cfg") > 0 ||
237 guestfs_is_dir (g, "/EFI/BOOT") > 0 ||
238 guestfs_is_file (g, "/images/install.img") > 0 ||
239 guestfs_is_dir (g, "/.disk") > 0 ||
240 guestfs_is_file (g, "/.discinfo") > 0 ||
241 guestfs_is_file (g, "/i386/txtsetup.sif") > 0 ||
242 guestfs_is_file (g, "/amd64/txtsetup.sif")) > 0) {
244 fs->content = FS_CONTENT_INSTALLER;
245 fs->format = OS_FORMAT_INSTALLER;
246 if (guestfs___check_installer_root (g, fs) == -1)
250 /* The above code should have set fs->type and fs->distro fields, so
251 * we can now guess the package management system.
253 check_package_format (g, fs);
254 check_package_management (g, fs);
260 extend_fses (guestfs_h *g)
262 size_t n = g->nr_fses + 1;
263 struct inspect_fs *p;
265 p = realloc (g->fses, n * sizeof (struct inspect_fs));
267 perrorf (g, "realloc");
274 memset (&g->fses[n-1], 0, sizeof (struct inspect_fs));
280 guestfs___is_file_nocase (guestfs_h *g, const char *path)
285 p = guestfs___case_sensitive_path_silently (g, path);
288 r = guestfs_is_file (g, p);
294 guestfs___is_dir_nocase (guestfs_h *g, const char *path)
299 p = guestfs___case_sensitive_path_silently (g, path);
302 r = guestfs_is_dir (g, p);
307 /* Parse small, unsigned ints, as used in version numbers. */
309 guestfs___parse_unsigned_int (guestfs_h *g, const char *str)
312 int r = xstrtol (str, NULL, 10, &ret, "");
313 if (r != LONGINT_OK) {
314 error (g, _("could not parse integer in version number: %s"), str);
320 /* Like parse_unsigned_int, but ignore trailing stuff. */
322 guestfs___parse_unsigned_int_ignore_trailing (guestfs_h *g, const char *str)
325 int r = xstrtol (str, NULL, 10, &ret, NULL);
326 if (r != LONGINT_OK) {
327 error (g, _("could not parse integer in version number: %s"), str);
333 /* Parse generic MAJOR.MINOR from the fs->product_name string. */
335 guestfs___parse_major_minor (guestfs_h *g, struct inspect_fs *fs)
339 if (match2 (g, fs->product_name, re_major_minor, &major, &minor)) {
340 fs->major_version = guestfs___parse_unsigned_int (g, major);
342 if (fs->major_version == -1) {
346 fs->minor_version = guestfs___parse_unsigned_int (g, minor);
348 if (fs->minor_version == -1)
354 /* At the moment, package format and package management is just a
355 * simple function of the distro and major_version fields, so these
356 * can never return an error. We might be cleverer in future.
359 check_package_format (guestfs_h *g, struct inspect_fs *fs)
361 switch (fs->distro) {
362 case OS_DISTRO_FEDORA:
363 case OS_DISTRO_MEEGO:
364 case OS_DISTRO_REDHAT_BASED:
366 case OS_DISTRO_MANDRIVA:
367 case OS_DISTRO_CENTOS:
368 case OS_DISTRO_SCIENTIFIC_LINUX:
369 fs->package_format = OS_PACKAGE_FORMAT_RPM;
372 case OS_DISTRO_DEBIAN:
373 case OS_DISTRO_UBUNTU:
374 case OS_DISTRO_LINUX_MINT:
375 fs->package_format = OS_PACKAGE_FORMAT_DEB;
378 case OS_DISTRO_ARCHLINUX:
379 fs->package_format = OS_PACKAGE_FORMAT_PACMAN;
381 case OS_DISTRO_GENTOO:
382 fs->package_format = OS_PACKAGE_FORMAT_EBUILD;
384 case OS_DISTRO_PARDUS:
385 fs->package_format = OS_PACKAGE_FORMAT_PISI;
388 case OS_DISTRO_SLACKWARE:
389 case OS_DISTRO_WINDOWS:
390 case OS_DISTRO_UNKNOWN:
392 fs->package_format = OS_PACKAGE_FORMAT_UNKNOWN;
398 check_package_management (guestfs_h *g, struct inspect_fs *fs)
400 switch (fs->distro) {
401 case OS_DISTRO_FEDORA:
402 case OS_DISTRO_MEEGO:
403 fs->package_management = OS_PACKAGE_MANAGEMENT_YUM;
406 case OS_DISTRO_REDHAT_BASED:
408 case OS_DISTRO_CENTOS:
409 case OS_DISTRO_SCIENTIFIC_LINUX:
410 if (fs->major_version >= 5)
411 fs->package_management = OS_PACKAGE_MANAGEMENT_YUM;
413 fs->package_management = OS_PACKAGE_MANAGEMENT_UP2DATE;
416 case OS_DISTRO_DEBIAN:
417 case OS_DISTRO_UBUNTU:
418 case OS_DISTRO_LINUX_MINT:
419 fs->package_management = OS_PACKAGE_MANAGEMENT_APT;
422 case OS_DISTRO_ARCHLINUX:
423 fs->package_management = OS_PACKAGE_MANAGEMENT_PACMAN;
425 case OS_DISTRO_GENTOO:
426 fs->package_management = OS_PACKAGE_MANAGEMENT_PORTAGE;
428 case OS_DISTRO_PARDUS:
429 fs->package_management = OS_PACKAGE_MANAGEMENT_PISI;
431 case OS_DISTRO_MANDRIVA:
432 fs->package_management = OS_PACKAGE_MANAGEMENT_URPMI;
435 case OS_DISTRO_SLACKWARE:
436 case OS_DISTRO_WINDOWS:
437 case OS_DISTRO_UNKNOWN:
439 fs->package_management = OS_PACKAGE_MANAGEMENT_UNKNOWN;
444 /* Get the first line of a small file, without any trailing newline
448 guestfs___first_line_of_file (guestfs_h *g, const char *filename)
454 /* Don't trust guestfs_head_n not to break with very large files.
455 * Check the file size is something reasonable first.
457 size = guestfs_filesize (g, filename);
459 /* guestfs_filesize failed and has already set error in handle */
461 if (size > MAX_SMALL_FILE_SIZE) {
462 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
467 lines = guestfs_head_n (g, 1, filename);
470 if (lines[0] == NULL) {
471 error (g, _("%s: file is empty"), filename);
472 guestfs___free_string_list (lines);
475 /* lines[1] should be NULL because of '1' argument above ... */
477 ret = lines[0]; /* caller frees */
478 free (lines); /* free the array */
483 /* Get the first matching line (using guestfs_egrep{,i}) of a small file,
484 * without any trailing newline character.
486 * Returns: 1 = returned a line (in *ret)
491 guestfs___first_egrep_of_file (guestfs_h *g, const char *filename,
492 const char *eregex, int iflag, char **ret)
498 /* Don't trust guestfs_egrep not to break with very large files.
499 * Check the file size is something reasonable first.
501 size = guestfs_filesize (g, filename);
503 /* guestfs_filesize failed and has already set error in handle */
505 if (size > MAX_SMALL_FILE_SIZE) {
506 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
511 lines = (!iflag ? guestfs_egrep : guestfs_egrepi) (g, eregex, filename);
514 if (lines[0] == NULL) {
515 guestfs___free_string_list (lines);
519 *ret = lines[0]; /* caller frees */
521 /* free up any other matches and the array itself */
522 for (i = 1; lines[i] != NULL; ++i)
529 #endif /* defined(HAVE_PCRE) && defined(HAVE_HIVEX) */