2
* GRUB -- GRand Unified Bootloader
3
* Copyright (C) 2002,2003,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
5
* GRUB is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* GRUB is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19
#include <config-util.h>
29
#include <sys/types.h>
39
#include <grub/types.h>
40
#include <grub/misc.h>
41
#include <grub/i18n.h>
42
#include <grub/time.h>
43
#include <grub/emu/misc.h>
45
#ifdef HAVE_DEVICE_MAPPER
46
# include <libdevmapper.h>
50
# include <grub/util/libzfs.h>
54
# include <grub/util/libnvpair.h>
57
#ifdef HAVE_SYS_PARAM_H
58
# include <sys/param.h>
61
#ifdef HAVE_SYS_MOUNT_H
62
# include <sys/mount.h>
65
#ifdef HAVE_SYS_MNTTAB_H
66
# include <stdio.h> /* Needed by sys/mnttab.h. */
67
# include <sys/mnttab.h>
70
#ifdef HAVE_SYS_MKDEV_H
71
# include <sys/mkdev.h> /* makedev */
77
grub_util_warn (const char *fmt, ...)
81
fprintf (stderr, _("%s: warn:"), program_name);
82
fprintf (stderr, " ");
84
vfprintf (stderr, fmt, ap);
86
fprintf (stderr, ".\n");
91
grub_util_info (const char *fmt, ...)
97
fprintf (stderr, _("%s: info:"), program_name);
98
fprintf (stderr, " ");
100
vfprintf (stderr, fmt, ap);
102
fprintf (stderr, ".\n");
108
grub_util_error (const char *fmt, ...)
112
fprintf (stderr, _("%s: error:"), program_name);
113
fprintf (stderr, " ");
115
vfprintf (stderr, fmt, ap);
117
fprintf (stderr, ".\n");
122
xmalloc (grub_size_t size)
128
grub_util_error ("out of memory");
134
xrealloc (void *ptr, grub_size_t size)
136
ptr = realloc (ptr, size);
138
grub_util_error ("out of memory");
144
xstrdup (const char *str)
150
newstr = (char *) xmalloc (len + 1);
151
memcpy (newstr, str, len + 1);
156
#ifndef HAVE_VASPRINTF
159
vasprintf (char **buf, const char *fmt, va_list ap)
161
/* Should be large enough. */
162
*buf = xmalloc (512);
164
return vsnprintf (*buf, 512, fmt, ap);
169
#ifndef HAVE_ASPRINTF
172
asprintf (char **buf, const char *fmt, ...)
178
status = vasprintf (buf, fmt, ap);
187
xasprintf (const char *fmt, ...)
193
if (vasprintf (&result, fmt, ap) < 0)
196
grub_util_error ("out of memory");
210
grub_get_time_ms (void)
214
gettimeofday (&tv, 0);
216
return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
224
gettimeofday (&tv, 0);
226
return (tv.tv_sec * GRUB_TICKS_PER_SECOND
227
+ (((tv.tv_sec % GRUB_TICKS_PER_SECOND) * 1000000 + tv.tv_usec)
228
* GRUB_TICKS_PER_SECOND / 1000000));
232
canonicalize_file_name (const char *path)
236
ret = xmalloc (PATH_MAX);
237
if (!realpath (path, ret))
240
ret = realpath (path, NULL);
246
/* Convert POSIX path to Win32 path,
247
remove drive letter, replace backslashes. */
249
get_win32_path (const char *path)
251
char winpath[PATH_MAX];
252
if (cygwin_conv_path (CCP_POSIX_TO_WIN_A, path, winpath, sizeof(winpath)))
253
grub_util_error ("cygwin_conv_path() failed");
255
int len = strlen (winpath);
256
int offs = (len > 2 && winpath[1] == ':' ? 2 : 0);
259
for (i = offs; i < len; i++)
260
if (winpath[i] == '\\')
262
return xstrdup (winpath + offs);
267
static libzfs_handle_t *__libzfs_handle;
272
libzfs_fini (__libzfs_handle);
276
grub_get_libzfs_handle (void)
278
if (! __libzfs_handle)
280
__libzfs_handle = libzfs_init ();
283
atexit (fini_libzfs);
286
return __libzfs_handle;
288
#endif /* HAVE_LIBZFS */
290
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
291
/* ZFS has similar problems to those of btrfs (see above). */
293
grub_find_zpool_from_dir (const char *dir, char **poolname, char **poolfs)
297
*poolname = *poolfs = NULL;
299
#if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) && defined(HAVE_STRUCT_STATFS_F_MNTFROMNAME)
300
/* FreeBSD and GNU/kFreeBSD. */
304
if (statfs (dir, &mnt) != 0)
307
if (strcmp (mnt.f_fstypename, "zfs") != 0)
310
*poolname = xstrdup (mnt.f_mntfromname);
312
#elif defined(HAVE_GETEXTMNTENT)
316
struct extmnttab mnt;
318
if (stat (dir, &st) != 0)
321
FILE *mnttab = fopen ("/etc/mnttab", "r");
325
while (getextmntent (mnttab, &mnt, sizeof (mnt)) == 0)
327
if (makedev (mnt.mnt_major, mnt.mnt_minor) == st.st_dev
328
&& !strcmp (mnt.mnt_fstype, "zfs"))
330
*poolname = xstrdup (mnt.mnt_special);
342
slash = strchr (*poolname, '/');
346
*poolfs = xstrdup (slash + 1);
349
*poolfs = xstrdup ("");
353
/* This function never prints trailing slashes (so that its output
354
can be appended a slash unconditionally). */
356
grub_make_system_path_relative_to_its_root (const char *path)
359
char *p, *buf, *buf2, *buf3, *ret;
360
uintptr_t offset = 0;
364
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
369
p = canonicalize_file_name (path);
371
grub_util_error ("failed to get canonical path of %s", path);
373
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
374
/* For ZFS sub-pool filesystems, could be extended to others (btrfs?). */
377
grub_find_zpool_from_dir (p, &dummy, &poolfs);
381
len = strlen (p) + 1;
385
if (stat (buf, &st) < 0)
386
grub_util_error ("cannot stat %s: %s", buf, strerror (errno));
388
buf2 = xstrdup (buf);
391
/* This loop sets offset to the number of chars of the root
392
directory we're inspecting. */
395
p = strrchr (buf, '/');
397
/* This should never happen. */
398
grub_util_error ("FIXME: no / in buf. (make_system_path_relative_to_its_root)");
404
if (stat (buf, &st) < 0)
405
grub_util_error ("cannot stat %s: %s", buf, strerror (errno));
407
/* buf is another filesystem; we found it. */
408
if (st.st_dev != num)
410
/* offset == 0 means path given is the mount point.
411
This works around special-casing of "/" in Un*x. This function never
412
prints trailing slashes (so that its output can be appended a slash
413
unconditionally). Each slash in is considered a preceding slash, and
414
therefore the root directory is an empty string. */
419
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
421
return xasprintf ("/%s/@", poolfs);
430
/* offset == 1 means root directory. */
433
/* Include leading slash. */
439
buf3 = xstrdup (buf2 + offset);
443
if (st.st_dev != (DEV_CYGDRIVE_MAJOR << 16))
445
/* Reached some mount point not below /cygdrive.
446
GRUB does not know Cygwin's emulated mounts,
447
convert to Win32 path. */
448
grub_util_info ("Cygwin path = %s\n", buf3);
449
char * temp = get_win32_path (buf3);
455
/* Remove trailing slashes, return empty string if root directory. */
457
while (len > 0 && buf3[len - 1] == '/')
459
buf3[len - 1] = '\0';
463
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
466
ret = xasprintf ("/%s/@%s", poolfs, buf3);
476
#ifdef HAVE_DEVICE_MAPPER
477
static void device_mapper_null_log (int level __attribute__ ((unused)),
478
const char *file __attribute__ ((unused)),
479
int line __attribute__ ((unused)),
480
int dm_errno __attribute__ ((unused)),
481
const char *f __attribute__ ((unused)),
487
grub_device_mapper_supported (void)
489
static int supported = -1;
495
/* Suppress annoying log messages. */
496
dm_log_with_errno_init (&device_mapper_null_log);
498
dmt = dm_task_create (DM_DEVICE_VERSION);
499
supported = (dmt != NULL);
501
dm_task_destroy (dmt);
503
/* Restore the original logger. */
504
dm_log_with_errno_init (NULL);
509
#endif /* HAVE_DEVICE_MAPPER */