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 /* Compile all the regular expressions once when the shared library is
50 * loaded. PCRE is thread safe so we're supposedly OK here if
51 * multiple threads call into the libguestfs API functions below
54 static pcre *re_first_partition;
55 static pcre *re_major_minor;
57 static void compile_regexps (void) __attribute__((constructor));
58 static void free_regexps (void) __attribute__((destructor));
61 compile_regexps (void)
66 #define COMPILE(re,pattern,options) \
68 re = pcre_compile ((pattern), (options), &err, &offset, NULL); \
70 ignore_value (write (2, err, strlen (err))); \
75 COMPILE (re_first_partition, "^/dev/(?:h|s|v)d.1$", 0);
76 COMPILE (re_major_minor, "(\\d+)\\.(\\d+)", 0);
82 pcre_free (re_first_partition);
83 pcre_free (re_major_minor);
86 static int check_filesystem (guestfs_h *g, const char *device, int is_block, int is_partnum);
87 static void check_package_format (guestfs_h *g, struct inspect_fs *fs);
88 static void check_package_management (guestfs_h *g, struct inspect_fs *fs);
89 static int extend_fses (guestfs_h *g);
91 /* Find out if 'device' contains a filesystem. If it does, add
92 * another entry in g->fses.
95 guestfs___check_for_filesystem_on (guestfs_h *g, const char *device,
96 int is_block, int is_partnum)
98 /* Get vfs-type in order to check if it's a Linux(?) swap device.
99 * If there's an error we should ignore it, so to do that we have to
100 * temporarily replace the error handler with a null one.
102 guestfs_error_handler_cb old_error_cb = g->error_cb;
104 char *vfs_type = guestfs_vfs_type (g, device);
105 g->error_cb = old_error_cb;
107 int is_swap = vfs_type && STREQ (vfs_type, "swap");
109 debug (g, "check_for_filesystem_on: %s %d %d (%s)",
110 device, is_block, is_partnum,
111 vfs_type ? vfs_type : "failed to get vfs type");
115 if (extend_fses (g) == -1)
117 g->fses[g->nr_fses-1].is_swap = 1;
121 /* Try mounting the device. As above, ignore errors. */
124 if (vfs_type && STREQ (vfs_type, "ufs")) { /* Hack for the *BSDs. */
125 /* FreeBSD fs is a variant of ufs called ufs2 ... */
126 r = guestfs_mount_vfs (g, "ro,ufstype=ufs2", "ufs", device, "/");
128 /* while NetBSD and OpenBSD use another variant labeled 44bsd */
129 r = guestfs_mount_vfs (g, "ro,ufstype=44bsd", "ufs", device, "/");
131 r = guestfs_mount_ro (g, device, "/");
134 g->error_cb = old_error_cb;
138 /* Do the rest of the checks. */
139 r = check_filesystem (g, device, is_block, is_partnum);
141 /* Unmount the filesystem. */
142 if (guestfs_umount_all (g) == -1)
148 /* is_block and is_partnum are just hints: is_block is true if the
149 * filesystem is a whole block device (eg. /dev/sda). is_partnum
150 * is > 0 if the filesystem is a direct partition, and in this case
151 * it is the partition number counting from 1
152 * (eg. /dev/sda1 => is_partnum == 1).
155 check_filesystem (guestfs_h *g, const char *device,
156 int is_block, int is_partnum)
158 if (extend_fses (g) == -1)
161 struct inspect_fs *fs = &g->fses[g->nr_fses-1];
163 fs->device = safe_strdup (g, device);
164 fs->is_mountable = 1;
166 /* Optimize some of the tests by avoiding multiple tests of the same thing. */
167 int is_dir_etc = guestfs_is_dir (g, "/etc") > 0;
168 int is_dir_bin = guestfs_is_dir (g, "/bin") > 0;
169 int is_dir_share = guestfs_is_dir (g, "/share") > 0;
172 if (guestfs_is_file (g, "/grub/menu.lst") > 0 ||
173 guestfs_is_file (g, "/grub/grub.conf") > 0)
174 fs->content = FS_CONTENT_LINUX_BOOT;
176 else if (is_dir_etc &&
178 guestfs_is_file (g, "/etc/freebsd-update.conf") > 0 &&
179 guestfs_is_file (g, "/etc/fstab") > 0) {
180 /* Ignore /dev/sda1 which is a shadow of the real root filesystem
181 * that is probably /dev/sda5 (see:
182 * http://www.freebsd.org/doc/handbook/disk-organization.html)
184 if (match (g, device, re_first_partition))
188 fs->content = FS_CONTENT_FREEBSD_ROOT;
189 fs->format = OS_FORMAT_INSTALLED;
190 if (guestfs___check_freebsd_root (g, fs) == -1)
193 else if (is_dir_etc &&
195 guestfs_is_file (g, "/etc/fstab") > 0 &&
196 guestfs_is_file (g, "/etc/release") > 0) {
197 /* Ignore /dev/sda1 which is a shadow of the real root filesystem
198 * that is probably /dev/sda5 (see:
199 * http://www.freebsd.org/doc/handbook/disk-organization.html)
201 if (match (g, device, re_first_partition))
205 fs->content = FS_CONTENT_NETBSD_ROOT;
206 fs->format = OS_FORMAT_INSTALLED;
207 if (guestfs___check_netbsd_root (g, fs) == -1)
211 else if (is_dir_etc &&
213 guestfs_is_file (g, "/etc/fstab") > 0) {
215 fs->content = FS_CONTENT_LINUX_ROOT;
216 fs->format = OS_FORMAT_INSTALLED;
217 if (guestfs___check_linux_root (g, fs) == -1)
220 /* Linux /usr/local? */
221 else if (is_dir_etc &&
224 guestfs_exists (g, "/local") == 0 &&
225 guestfs_is_file (g, "/etc/fstab") == 0)
226 fs->content = FS_CONTENT_LINUX_USR_LOCAL;
228 else if (is_dir_etc &&
231 guestfs_exists (g, "/local") > 0 &&
232 guestfs_is_file (g, "/etc/fstab") == 0)
233 fs->content = FS_CONTENT_LINUX_USR;
235 else if (guestfs_is_dir (g, "/log") > 0 &&
236 guestfs_is_dir (g, "/run") > 0 &&
237 guestfs_is_dir (g, "/spool") > 0)
238 fs->content = FS_CONTENT_LINUX_VAR;
240 else if (guestfs___has_windows_systemroot (g) >= 0) {
242 fs->content = FS_CONTENT_WINDOWS_ROOT;
243 fs->format = OS_FORMAT_INSTALLED;
244 if (guestfs___check_windows_root (g, fs) == -1)
247 /* Windows volume with installed applications (but not root)? */
248 else if (guestfs___is_dir_nocase (g, "/System Volume Information") > 0 &&
249 guestfs___is_dir_nocase (g, "/Program Files") > 0)
250 fs->content = FS_CONTENT_WINDOWS_VOLUME_WITH_APPS;
251 /* Windows volume (but not root)? */
252 else if (guestfs___is_dir_nocase (g, "/System Volume Information") > 0)
253 fs->content = FS_CONTENT_WINDOWS_VOLUME;
254 /* Install CD/disk? Skip these checks if it's not a whole device
255 * (eg. CD) or the first partition (eg. bootable USB key).
257 else if ((is_block || is_partnum == 1) &&
258 (guestfs_is_file (g, "/isolinux/isolinux.cfg") > 0 ||
259 guestfs_is_dir (g, "/EFI/BOOT") > 0 ||
260 guestfs_is_file (g, "/images/install.img") > 0 ||
261 guestfs_is_dir (g, "/.disk") > 0 ||
262 guestfs_is_file (g, "/.discinfo") > 0 ||
263 guestfs_is_file (g, "/i386/txtsetup.sif") > 0 ||
264 guestfs_is_file (g, "/amd64/txtsetup.sif")) > 0) {
266 fs->content = FS_CONTENT_INSTALLER;
267 fs->format = OS_FORMAT_INSTALLER;
268 if (guestfs___check_installer_root (g, fs) == -1)
272 /* The above code should have set fs->type and fs->distro fields, so
273 * we can now guess the package management system.
275 check_package_format (g, fs);
276 check_package_management (g, fs);
282 extend_fses (guestfs_h *g)
284 size_t n = g->nr_fses + 1;
285 struct inspect_fs *p;
287 p = realloc (g->fses, n * sizeof (struct inspect_fs));
289 perrorf (g, "realloc");
296 memset (&g->fses[n-1], 0, sizeof (struct inspect_fs));
302 guestfs___is_file_nocase (guestfs_h *g, const char *path)
307 p = guestfs___case_sensitive_path_silently (g, path);
310 r = guestfs_is_file (g, p);
316 guestfs___is_dir_nocase (guestfs_h *g, const char *path)
321 p = guestfs___case_sensitive_path_silently (g, path);
324 r = guestfs_is_dir (g, p);
329 /* Parse small, unsigned ints, as used in version numbers. */
331 guestfs___parse_unsigned_int (guestfs_h *g, const char *str)
334 int r = xstrtol (str, NULL, 10, &ret, "");
335 if (r != LONGINT_OK) {
336 error (g, _("could not parse integer in version number: %s"), str);
342 /* Like parse_unsigned_int, but ignore trailing stuff. */
344 guestfs___parse_unsigned_int_ignore_trailing (guestfs_h *g, const char *str)
347 int r = xstrtol (str, NULL, 10, &ret, NULL);
348 if (r != LONGINT_OK) {
349 error (g, _("could not parse integer in version number: %s"), str);
355 /* Parse generic MAJOR.MINOR from the fs->product_name string. */
357 guestfs___parse_major_minor (guestfs_h *g, struct inspect_fs *fs)
361 if (match2 (g, fs->product_name, re_major_minor, &major, &minor)) {
362 fs->major_version = guestfs___parse_unsigned_int (g, major);
364 if (fs->major_version == -1) {
368 fs->minor_version = guestfs___parse_unsigned_int (g, minor);
370 if (fs->minor_version == -1)
376 /* At the moment, package format and package management is just a
377 * simple function of the distro and major_version fields, so these
378 * can never return an error. We might be cleverer in future.
381 check_package_format (guestfs_h *g, struct inspect_fs *fs)
383 switch (fs->distro) {
384 case OS_DISTRO_FEDORA:
385 case OS_DISTRO_MEEGO:
386 case OS_DISTRO_REDHAT_BASED:
388 case OS_DISTRO_MAGEIA:
389 case OS_DISTRO_MANDRIVA:
390 case OS_DISTRO_OPENSUSE:
391 case OS_DISTRO_CENTOS:
392 case OS_DISTRO_SCIENTIFIC_LINUX:
393 fs->package_format = OS_PACKAGE_FORMAT_RPM;
396 case OS_DISTRO_DEBIAN:
397 case OS_DISTRO_UBUNTU:
398 case OS_DISTRO_LINUX_MINT:
399 fs->package_format = OS_PACKAGE_FORMAT_DEB;
402 case OS_DISTRO_ARCHLINUX:
403 fs->package_format = OS_PACKAGE_FORMAT_PACMAN;
405 case OS_DISTRO_GENTOO:
406 fs->package_format = OS_PACKAGE_FORMAT_EBUILD;
408 case OS_DISTRO_PARDUS:
409 fs->package_format = OS_PACKAGE_FORMAT_PISI;
412 case OS_DISTRO_SLACKWARE:
413 case OS_DISTRO_TTYLINUX:
414 case OS_DISTRO_WINDOWS:
415 case OS_DISTRO_UNKNOWN:
417 fs->package_format = OS_PACKAGE_FORMAT_UNKNOWN;
423 check_package_management (guestfs_h *g, struct inspect_fs *fs)
425 switch (fs->distro) {
426 case OS_DISTRO_FEDORA:
427 case OS_DISTRO_MEEGO:
428 fs->package_management = OS_PACKAGE_MANAGEMENT_YUM;
431 case OS_DISTRO_REDHAT_BASED:
433 case OS_DISTRO_CENTOS:
434 case OS_DISTRO_SCIENTIFIC_LINUX:
435 if (fs->major_version >= 5)
436 fs->package_management = OS_PACKAGE_MANAGEMENT_YUM;
438 fs->package_management = OS_PACKAGE_MANAGEMENT_UP2DATE;
441 case OS_DISTRO_DEBIAN:
442 case OS_DISTRO_UBUNTU:
443 case OS_DISTRO_LINUX_MINT:
444 fs->package_management = OS_PACKAGE_MANAGEMENT_APT;
447 case OS_DISTRO_ARCHLINUX:
448 fs->package_management = OS_PACKAGE_MANAGEMENT_PACMAN;
450 case OS_DISTRO_GENTOO:
451 fs->package_management = OS_PACKAGE_MANAGEMENT_PORTAGE;
453 case OS_DISTRO_PARDUS:
454 fs->package_management = OS_PACKAGE_MANAGEMENT_PISI;
456 case OS_DISTRO_MAGEIA:
457 case OS_DISTRO_MANDRIVA:
458 fs->package_management = OS_PACKAGE_MANAGEMENT_URPMI;
461 case OS_DISTRO_OPENSUSE:
462 fs->package_management = OS_PACKAGE_MANAGEMENT_ZYPPER;
465 case OS_DISTRO_SLACKWARE:
466 case OS_DISTRO_TTYLINUX:
467 case OS_DISTRO_WINDOWS:
468 case OS_DISTRO_UNKNOWN:
470 fs->package_management = OS_PACKAGE_MANAGEMENT_UNKNOWN;
475 /* Get the first line of a small file, without any trailing newline
479 guestfs___first_line_of_file (guestfs_h *g, const char *filename)
485 /* Don't trust guestfs_head_n not to break with very large files.
486 * Check the file size is something reasonable first.
488 size = guestfs_filesize (g, filename);
490 /* guestfs_filesize failed and has already set error in handle */
492 if (size > MAX_SMALL_FILE_SIZE) {
493 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
498 lines = guestfs_head_n (g, 1, filename);
501 if (lines[0] == NULL) {
502 error (g, _("%s: file is empty"), filename);
503 guestfs___free_string_list (lines);
506 /* lines[1] should be NULL because of '1' argument above ... */
508 ret = lines[0]; /* caller frees */
509 free (lines); /* free the array */
514 /* Get the first matching line (using guestfs_egrep{,i}) of a small file,
515 * without any trailing newline character.
517 * Returns: 1 = returned a line (in *ret)
522 guestfs___first_egrep_of_file (guestfs_h *g, const char *filename,
523 const char *eregex, int iflag, char **ret)
529 /* Don't trust guestfs_egrep not to break with very large files.
530 * Check the file size is something reasonable first.
532 size = guestfs_filesize (g, filename);
534 /* guestfs_filesize failed and has already set error in handle */
536 if (size > MAX_SMALL_FILE_SIZE) {
537 error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
542 lines = (!iflag ? guestfs_egrep : guestfs_egrepi) (g, eregex, filename);
545 if (lines[0] == NULL) {
546 guestfs___free_string_list (lines);
550 *ret = lines[0]; /* caller frees */
552 /* free up any other matches and the array itself */
553 for (i = 1; lines[i] != NULL; ++i)
560 #endif /* defined(HAVE_HIVEX) */