Detect Mageia distribution
[libguestfs.git] / src / inspect_fs.c
1 /* libguestfs
2  * Copyright (C) 2010-2011 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <inttypes.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <errno.h>
30 #include <endian.h>
31
32 #include <pcre.h>
33
34 #ifdef HAVE_HIVEX
35 #include <hivex.h>
36 #endif
37
38 #include "c-ctype.h"
39 #include "ignore-value.h"
40 #include "xstrtol.h"
41
42 #include "guestfs.h"
43 #include "guestfs-internal.h"
44 #include "guestfs-internal-actions.h"
45 #include "guestfs_protocol.h"
46
47 #if defined(HAVE_HIVEX)
48
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
52  * simultaneously.
53  */
54 static pcre *re_first_partition;
55 static pcre *re_major_minor;
56
57 static void compile_regexps (void) __attribute__((constructor));
58 static void free_regexps (void) __attribute__((destructor));
59
60 static void
61 compile_regexps (void)
62 {
63   const char *err;
64   int offset;
65
66 #define COMPILE(re,pattern,options)                                     \
67   do {                                                                  \
68     re = pcre_compile ((pattern), (options), &err, &offset, NULL);      \
69     if (re == NULL) {                                                   \
70       ignore_value (write (2, err, strlen (err)));                      \
71       abort ();                                                         \
72     }                                                                   \
73   } while (0)
74
75   COMPILE (re_first_partition, "^/dev/(?:h|s|v)d.1$", 0);
76   COMPILE (re_major_minor, "(\\d+)\\.(\\d+)", 0);
77 }
78
79 static void
80 free_regexps (void)
81 {
82   pcre_free (re_first_partition);
83   pcre_free (re_major_minor);
84 }
85
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);
90
91 /* Find out if 'device' contains a filesystem.  If it does, add
92  * another entry in g->fses.
93  */
94 int
95 guestfs___check_for_filesystem_on (guestfs_h *g, const char *device,
96                                    int is_block, int is_partnum)
97 {
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.
101    */
102   guestfs_error_handler_cb old_error_cb = g->error_cb;
103   g->error_cb = NULL;
104   char *vfs_type = guestfs_vfs_type (g, device);
105   g->error_cb = old_error_cb;
106
107   int is_swap = vfs_type && STREQ (vfs_type, "swap");
108
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");
112
113   if (is_swap) {
114     free (vfs_type);
115     if (extend_fses (g) == -1)
116       return -1;
117     g->fses[g->nr_fses-1].is_swap = 1;
118     return 0;
119   }
120
121   /* Try mounting the device.  As above, ignore errors. */
122   g->error_cb = NULL;
123   int r = guestfs_mount_ro (g, device, "/");
124   if (r == -1 && vfs_type && STREQ (vfs_type, "ufs")) /* Hack for the *BSDs. */
125     r = guestfs_mount_vfs (g, "ro,ufstype=ufs2", "ufs", device, "/");
126   free (vfs_type);
127   g->error_cb = old_error_cb;
128   if (r == -1)
129     return 0;
130
131   /* Do the rest of the checks. */
132   r = check_filesystem (g, device, is_block, is_partnum);
133
134   /* Unmount the filesystem. */
135   if (guestfs_umount_all (g) == -1)
136     return -1;
137
138   return r;
139 }
140
141 /* is_block and is_partnum are just hints: is_block is true if the
142  * filesystem is a whole block device (eg. /dev/sda).  is_partnum
143  * is > 0 if the filesystem is a direct partition, and in this case
144  * it is the partition number counting from 1
145  * (eg. /dev/sda1 => is_partnum == 1).
146  */
147 static int
148 check_filesystem (guestfs_h *g, const char *device,
149                   int is_block, int is_partnum)
150 {
151   if (extend_fses (g) == -1)
152     return -1;
153
154   struct inspect_fs *fs = &g->fses[g->nr_fses-1];
155
156   fs->device = safe_strdup (g, device);
157   fs->is_mountable = 1;
158
159   /* Optimize some of the tests by avoiding multiple tests of the same thing. */
160   int is_dir_etc = guestfs_is_dir (g, "/etc") > 0;
161   int is_dir_bin = guestfs_is_dir (g, "/bin") > 0;
162   int is_dir_share = guestfs_is_dir (g, "/share") > 0;
163
164   /* Grub /boot? */
165   if (guestfs_is_file (g, "/grub/menu.lst") > 0 ||
166       guestfs_is_file (g, "/grub/grub.conf") > 0)
167     fs->content = FS_CONTENT_LINUX_BOOT;
168   /* FreeBSD root? */
169   else if (is_dir_etc &&
170            is_dir_bin &&
171            guestfs_is_file (g, "/etc/freebsd-update.conf") > 0 &&
172            guestfs_is_file (g, "/etc/fstab") > 0) {
173     /* Ignore /dev/sda1 which is a shadow of the real root filesystem
174      * that is probably /dev/sda5 (see:
175      * http://www.freebsd.org/doc/handbook/disk-organization.html)
176      */
177     if (match (g, device, re_first_partition))
178       return 0;
179
180     fs->is_root = 1;
181     fs->content = FS_CONTENT_FREEBSD_ROOT;
182     fs->format = OS_FORMAT_INSTALLED;
183     if (guestfs___check_freebsd_root (g, fs) == -1)
184       return -1;
185   }
186   /* Linux root? */
187   else if (is_dir_etc &&
188            is_dir_bin &&
189            guestfs_is_file (g, "/etc/fstab") > 0) {
190     fs->is_root = 1;
191     fs->content = FS_CONTENT_LINUX_ROOT;
192     fs->format = OS_FORMAT_INSTALLED;
193     if (guestfs___check_linux_root (g, fs) == -1)
194       return -1;
195   }
196   /* Linux /usr/local? */
197   else if (is_dir_etc &&
198            is_dir_bin &&
199            is_dir_share &&
200            guestfs_exists (g, "/local") == 0 &&
201            guestfs_is_file (g, "/etc/fstab") == 0)
202     fs->content = FS_CONTENT_LINUX_USR_LOCAL;
203   /* Linux /usr? */
204   else if (is_dir_etc &&
205            is_dir_bin &&
206            is_dir_share &&
207            guestfs_exists (g, "/local") > 0 &&
208            guestfs_is_file (g, "/etc/fstab") == 0)
209     fs->content = FS_CONTENT_LINUX_USR;
210   /* Linux /var? */
211   else if (guestfs_is_dir (g, "/log") > 0 &&
212            guestfs_is_dir (g, "/run") > 0 &&
213            guestfs_is_dir (g, "/spool") > 0)
214     fs->content = FS_CONTENT_LINUX_VAR;
215   /* Windows root? */
216   else if (guestfs___has_windows_systemroot (g) >= 0) {
217     fs->is_root = 1;
218     fs->content = FS_CONTENT_WINDOWS_ROOT;
219     fs->format = OS_FORMAT_INSTALLED;
220     if (guestfs___check_windows_root (g, fs) == -1)
221       return -1;
222   }
223   /* Windows volume with installed applications (but not root)? */
224   else if (guestfs___is_dir_nocase (g, "/System Volume Information") > 0 &&
225            guestfs___is_dir_nocase (g, "/Program Files") > 0)
226     fs->content = FS_CONTENT_WINDOWS_VOLUME_WITH_APPS;
227   /* Windows volume (but not root)? */
228   else if (guestfs___is_dir_nocase (g, "/System Volume Information") > 0)
229     fs->content = FS_CONTENT_WINDOWS_VOLUME;
230   /* Install CD/disk?  Skip these checks if it's not a whole device
231    * (eg. CD) or the first partition (eg. bootable USB key).
232    */
233   else if ((is_block || is_partnum == 1) &&
234            (guestfs_is_file (g, "/isolinux/isolinux.cfg") > 0 ||
235             guestfs_is_dir (g, "/EFI/BOOT") > 0 ||
236             guestfs_is_file (g, "/images/install.img") > 0 ||
237             guestfs_is_dir (g, "/.disk") > 0 ||
238             guestfs_is_file (g, "/.discinfo") > 0 ||
239             guestfs_is_file (g, "/i386/txtsetup.sif") > 0 ||
240             guestfs_is_file (g, "/amd64/txtsetup.sif")) > 0) {
241     fs->is_root = 1;
242     fs->content = FS_CONTENT_INSTALLER;
243     fs->format = OS_FORMAT_INSTALLER;
244     if (guestfs___check_installer_root (g, fs) == -1)
245       return -1;
246   }
247
248   /* The above code should have set fs->type and fs->distro fields, so
249    * we can now guess the package management system.
250    */
251   check_package_format (g, fs);
252   check_package_management (g, fs);
253
254   return 0;
255 }
256
257 static int
258 extend_fses (guestfs_h *g)
259 {
260   size_t n = g->nr_fses + 1;
261   struct inspect_fs *p;
262
263   p = realloc (g->fses, n * sizeof (struct inspect_fs));
264   if (p == NULL) {
265     perrorf (g, "realloc");
266     return -1;
267   }
268
269   g->fses = p;
270   g->nr_fses = n;
271
272   memset (&g->fses[n-1], 0, sizeof (struct inspect_fs));
273
274   return 0;
275 }
276
277 int
278 guestfs___is_file_nocase (guestfs_h *g, const char *path)
279 {
280   char *p;
281   int r;
282
283   p = guestfs___case_sensitive_path_silently (g, path);
284   if (!p)
285     return 0;
286   r = guestfs_is_file (g, p);
287   free (p);
288   return r > 0;
289 }
290
291 int
292 guestfs___is_dir_nocase (guestfs_h *g, const char *path)
293 {
294   char *p;
295   int r;
296
297   p = guestfs___case_sensitive_path_silently (g, path);
298   if (!p)
299     return 0;
300   r = guestfs_is_dir (g, p);
301   free (p);
302   return r > 0;
303 }
304
305 /* Parse small, unsigned ints, as used in version numbers. */
306 int
307 guestfs___parse_unsigned_int (guestfs_h *g, const char *str)
308 {
309   long ret;
310   int r = xstrtol (str, NULL, 10, &ret, "");
311   if (r != LONGINT_OK) {
312     error (g, _("could not parse integer in version number: %s"), str);
313     return -1;
314   }
315   return ret;
316 }
317
318 /* Like parse_unsigned_int, but ignore trailing stuff. */
319 int
320 guestfs___parse_unsigned_int_ignore_trailing (guestfs_h *g, const char *str)
321 {
322   long ret;
323   int r = xstrtol (str, NULL, 10, &ret, NULL);
324   if (r != LONGINT_OK) {
325     error (g, _("could not parse integer in version number: %s"), str);
326     return -1;
327   }
328   return ret;
329 }
330
331 /* Parse generic MAJOR.MINOR from the fs->product_name string. */
332 int
333 guestfs___parse_major_minor (guestfs_h *g, struct inspect_fs *fs)
334 {
335   char *major, *minor;
336
337   if (match2 (g, fs->product_name, re_major_minor, &major, &minor)) {
338     fs->major_version = guestfs___parse_unsigned_int (g, major);
339     free (major);
340     if (fs->major_version == -1) {
341       free (minor);
342       return -1;
343     }
344     fs->minor_version = guestfs___parse_unsigned_int (g, minor);
345     free (minor);
346     if (fs->minor_version == -1)
347       return -1;
348   }
349   return 0;
350 }
351
352 /* At the moment, package format and package management is just a
353  * simple function of the distro and major_version fields, so these
354  * can never return an error.  We might be cleverer in future.
355  */
356 static void
357 check_package_format (guestfs_h *g, struct inspect_fs *fs)
358 {
359   switch (fs->distro) {
360   case OS_DISTRO_FEDORA:
361   case OS_DISTRO_MEEGO:
362   case OS_DISTRO_REDHAT_BASED:
363   case OS_DISTRO_RHEL:
364   case OS_DISTRO_MAGEIA:
365   case OS_DISTRO_MANDRIVA:
366   case OS_DISTRO_CENTOS:
367   case OS_DISTRO_SCIENTIFIC_LINUX:
368     fs->package_format = OS_PACKAGE_FORMAT_RPM;
369     break;
370
371   case OS_DISTRO_DEBIAN:
372   case OS_DISTRO_UBUNTU:
373   case OS_DISTRO_LINUX_MINT:
374     fs->package_format = OS_PACKAGE_FORMAT_DEB;
375     break;
376
377   case OS_DISTRO_ARCHLINUX:
378     fs->package_format = OS_PACKAGE_FORMAT_PACMAN;
379     break;
380   case OS_DISTRO_GENTOO:
381     fs->package_format = OS_PACKAGE_FORMAT_EBUILD;
382     break;
383   case OS_DISTRO_PARDUS:
384     fs->package_format = OS_PACKAGE_FORMAT_PISI;
385     break;
386
387   case OS_DISTRO_SLACKWARE:
388   case OS_DISTRO_TTYLINUX:
389   case OS_DISTRO_WINDOWS:
390   case OS_DISTRO_UNKNOWN:
391   default:
392     fs->package_format = OS_PACKAGE_FORMAT_UNKNOWN;
393     break;
394   }
395 }
396
397 static void
398 check_package_management (guestfs_h *g, struct inspect_fs *fs)
399 {
400   switch (fs->distro) {
401   case OS_DISTRO_FEDORA:
402   case OS_DISTRO_MEEGO:
403     fs->package_management = OS_PACKAGE_MANAGEMENT_YUM;
404     break;
405
406   case OS_DISTRO_REDHAT_BASED:
407   case OS_DISTRO_RHEL:
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;
412     else
413       fs->package_management = OS_PACKAGE_MANAGEMENT_UP2DATE;
414     break;
415
416   case OS_DISTRO_DEBIAN:
417   case OS_DISTRO_UBUNTU:
418   case OS_DISTRO_LINUX_MINT:
419     fs->package_management = OS_PACKAGE_MANAGEMENT_APT;
420     break;
421
422   case OS_DISTRO_ARCHLINUX:
423     fs->package_management = OS_PACKAGE_MANAGEMENT_PACMAN;
424     break;
425   case OS_DISTRO_GENTOO:
426     fs->package_management = OS_PACKAGE_MANAGEMENT_PORTAGE;
427     break;
428   case OS_DISTRO_PARDUS:
429     fs->package_management = OS_PACKAGE_MANAGEMENT_PISI;
430     break;
431   case OS_DISTRO_MAGEIA:
432   case OS_DISTRO_MANDRIVA:
433     fs->package_management = OS_PACKAGE_MANAGEMENT_URPMI;
434     break;
435
436   case OS_DISTRO_SLACKWARE:
437   case OS_DISTRO_TTYLINUX:
438   case OS_DISTRO_WINDOWS:
439   case OS_DISTRO_UNKNOWN:
440   default:
441     fs->package_management = OS_PACKAGE_MANAGEMENT_UNKNOWN;
442     break;
443   }
444 }
445
446 /* Get the first line of a small file, without any trailing newline
447  * character.
448  */
449 char *
450 guestfs___first_line_of_file (guestfs_h *g, const char *filename)
451 {
452   char **lines;
453   int64_t size;
454   char *ret;
455
456   /* Don't trust guestfs_head_n not to break with very large files.
457    * Check the file size is something reasonable first.
458    */
459   size = guestfs_filesize (g, filename);
460   if (size == -1)
461     /* guestfs_filesize failed and has already set error in handle */
462     return NULL;
463   if (size > MAX_SMALL_FILE_SIZE) {
464     error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
465            filename, size);
466     return NULL;
467   }
468
469   lines = guestfs_head_n (g, 1, filename);
470   if (lines == NULL)
471     return NULL;
472   if (lines[0] == NULL) {
473     error (g, _("%s: file is empty"), filename);
474     guestfs___free_string_list (lines);
475     return NULL;
476   }
477   /* lines[1] should be NULL because of '1' argument above ... */
478
479   ret = lines[0];               /* caller frees */
480   free (lines);                 /* free the array */
481
482   return ret;
483 }
484
485 /* Get the first matching line (using guestfs_egrep{,i}) of a small file,
486  * without any trailing newline character.
487  *
488  * Returns: 1 = returned a line (in *ret)
489  *          0 = no match
490  *          -1 = error
491  */
492 int
493 guestfs___first_egrep_of_file (guestfs_h *g, const char *filename,
494                                const char *eregex, int iflag, char **ret)
495 {
496   char **lines;
497   int64_t size;
498   size_t i;
499
500   /* Don't trust guestfs_egrep not to break with very large files.
501    * Check the file size is something reasonable first.
502    */
503   size = guestfs_filesize (g, filename);
504   if (size == -1)
505     /* guestfs_filesize failed and has already set error in handle */
506     return -1;
507   if (size > MAX_SMALL_FILE_SIZE) {
508     error (g, _("size of %s is unreasonably large (%" PRIi64 " bytes)"),
509            filename, size);
510     return -1;
511   }
512
513   lines = (!iflag ? guestfs_egrep : guestfs_egrepi) (g, eregex, filename);
514   if (lines == NULL)
515     return -1;
516   if (lines[0] == NULL) {
517     guestfs___free_string_list (lines);
518     return 0;
519   }
520
521   *ret = lines[0];              /* caller frees */
522
523   /* free up any other matches and the array itself */
524   for (i = 1; lines[i] != NULL; ++i)
525     free (lines[i]);
526   free (lines);
527
528   return 1;
529 }
530
531 #endif /* defined(HAVE_HIVEX) */