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/>.
28
#include <sys/types.h>
36
# include <sys/mnttab.h>
42
#include <grub/types.h>
43
#include <grub/misc.h>
44
#include <grub/i18n.h>
45
#include <grub/time.h>
46
#include <grub/emu/misc.h>
48
#ifdef HAVE_DEVICE_MAPPER
49
# include <libdevmapper.h>
53
# include <grub/util/libzfs.h>
57
# include <grub/util/libnvpair.h>
61
# include <sys/mount.h>
67
grub_util_warn (const char *fmt, ...)
71
fprintf (stderr, _("%s: warn:"), program_name);
72
fprintf (stderr, " ");
74
vfprintf (stderr, fmt, ap);
76
fprintf (stderr, ".\n");
81
grub_util_info (const char *fmt, ...)
87
fprintf (stderr, _("%s: info:"), program_name);
88
fprintf (stderr, " ");
90
vfprintf (stderr, fmt, ap);
92
fprintf (stderr, ".\n");
98
grub_util_error (const char *fmt, ...)
102
fprintf (stderr, _("%s: error:"), program_name);
103
fprintf (stderr, " ");
105
vfprintf (stderr, fmt, ap);
107
fprintf (stderr, ".\n");
112
xmalloc (grub_size_t size)
118
grub_util_error ("out of memory");
124
xrealloc (void *ptr, grub_size_t size)
126
ptr = realloc (ptr, size);
128
grub_util_error ("out of memory");
134
xstrdup (const char *str)
140
newstr = (char *) xmalloc (len + 1);
141
memcpy (newstr, str, len + 1);
146
#ifndef HAVE_VASPRINTF
149
vasprintf (char **buf, const char *fmt, va_list ap)
151
/* Should be large enough. */
152
*buf = xmalloc (512);
154
return vsprintf (*buf, fmt, ap);
159
#ifndef HAVE_ASPRINTF
162
asprintf (char **buf, const char *fmt, ...)
168
status = vasprintf (*buf, fmt, ap);
177
xasprintf (const char *fmt, ...)
183
if (vasprintf (&result, fmt, ap) < 0)
186
grub_util_error ("out of memory");
200
grub_get_time_ms (void)
204
gettimeofday (&tv, 0);
206
return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
214
gettimeofday (&tv, 0);
216
return (tv.tv_sec * GRUB_TICKS_PER_SECOND
217
+ (((tv.tv_sec % GRUB_TICKS_PER_SECOND) * 1000000 + tv.tv_usec)
218
* GRUB_TICKS_PER_SECOND / 1000000));
222
canonicalize_file_name (const char *path)
226
ret = xmalloc (PATH_MAX);
227
if (!realpath (path, ret))
230
ret = realpath (path, NULL);
236
/* Convert POSIX path to Win32 path,
237
remove drive letter, replace backslashes. */
239
get_win32_path (const char *path)
241
char winpath[PATH_MAX];
242
if (cygwin_conv_path (CCP_POSIX_TO_WIN_A, path, winpath, sizeof(winpath)))
243
grub_util_error ("cygwin_conv_path() failed");
245
int len = strlen (winpath);
246
int offs = (len > 2 && winpath[1] == ':' ? 2 : 0);
249
for (i = offs; i < len; i++)
250
if (winpath[i] == '\\')
252
return xstrdup (winpath + offs);
257
static libzfs_handle_t *__libzfs_handle;
262
libzfs_fini (__libzfs_handle);
266
grub_get_libzfs_handle (void)
268
if (! __libzfs_handle)
270
__libzfs_handle = libzfs_init ();
271
atexit (fini_libzfs);
274
return __libzfs_handle;
276
#endif /* HAVE_LIBZFS */
278
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
279
/* Not ZFS-specific in itself, but for now it's only used by ZFS-related code. */
281
grub_find_mount_point_from_dir (const char *dir)
284
typeof (st.st_dev) fs;
285
char *prev, *next, *slash, *statdir;
287
if (stat (dir, &st) == -1)
288
error (1, errno, "stat (%s)", dir);
292
prev = xstrdup (dir);
296
/* Remove last slash. */
297
next = xstrdup (prev);
298
slash = strrchr (next, '/');
307
/* A next empty string counts as /. */
313
if (stat (statdir, &st) == -1)
314
error (1, errno, "stat (%s)", next);
318
/* Found mount point. */
326
/* We've already seen an empty string, which means we
327
reached /. Nothing left to do. */
331
return xstrdup ("/");
337
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
339
/* ZFS has similar problems to those of btrfs (see above). */
341
grub_find_zpool_from_mount_point (const char *mnt_point, char **poolname, char **poolfs)
345
*poolname = *poolfs = NULL;
347
#if defined(HAVE_GETFSSTAT) /* FreeBSD and GNU/kFreeBSD */
349
int mnt_count = getfsstat (NULL, 0, MNT_WAIT);
351
error (1, errno, "getfsstat");
353
struct statfs *mnt = xmalloc (mnt_count * sizeof (*mnt));
355
mnt_count = getfsstat (mnt, mnt_count * sizeof (*mnt), MNT_WAIT);
357
error (1, errno, "getfsstat");
360
for (i = 0; i < (unsigned) mnt_count; i++)
361
if (!strcmp (mnt[i].f_fstypename, "zfs")
362
&& !strcmp (mnt[i].f_mntonname, mnt_point))
364
*poolname = xstrdup (mnt[i].f_mntfromname);
370
#elif defined(HAVE_GETMNTANY) /* OpenSolaris */
372
FILE *mnttab = fopen ("/etc/mnttab", "r");
374
struct mnttab mpref =
377
.mnt_mountp = mnt_point,
383
if (getmntany (mnttab, &mp, &mpref) == 0)
384
*poolname = xstrdup (mp.mnt_special);
393
slash = strchr (*poolname, '/');
397
*poolfs = xstrdup (slash + 1);
400
*poolfs = xstrdup ("");
404
/* This function never prints trailing slashes (so that its output
405
can be appended a slash unconditionally). */
407
grub_make_system_path_relative_to_its_root (const char *path)
410
char *p, *buf, *buf2, *buf3;
411
char *mnt_point, *poolname = NULL, *poolfs = NULL, *ret;
412
uintptr_t offset = 0;
417
p = canonicalize_file_name (path);
419
grub_util_error ("failed to get canonical path of %s", path);
421
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
422
/* For ZFS sub-pool filesystems, could be extended to others (btrfs?). */
423
mnt_point = grub_find_mount_point_from_dir (p);
426
grub_find_zpool_from_mount_point (mnt_point, &poolname, &poolfs);
431
len = strlen (p) + 1;
435
if (stat (buf, &st) < 0)
436
grub_util_error ("cannot stat %s: %s", buf, strerror (errno));
438
buf2 = xstrdup (buf);
441
/* This loop sets offset to the number of chars of the root
442
directory we're inspecting. */
445
p = strrchr (buf, '/');
447
/* This should never happen. */
448
grub_util_error ("FIXME: no / in buf. (make_system_path_relative_to_its_root)");
454
if (stat (buf, &st) < 0)
455
grub_util_error ("cannot stat %s: %s", buf, strerror (errno));
457
/* buf is another filesystem; we found it. */
458
if (st.st_dev != num)
460
/* offset == 0 means path given is the mount point.
461
This works around special-casing of "/" in Un*x. This function never
462
prints trailing slashes (so that its output can be appended a slash
463
unconditionally). Each slash in is considered a preceding slash, and
464
therefore the root directory is an empty string. */
476
/* offset == 1 means root directory. */
479
/* Include leading slash. */
485
buf3 = xstrdup (buf2 + offset);
489
if (st.st_dev != (DEV_CYGDRIVE_MAJOR << 16))
491
/* Reached some mount point not below /cygdrive.
492
GRUB does not know Cygwin's emulated mounts,
493
convert to Win32 path. */
494
grub_util_info ("Cygwin path = %s\n", buf3);
495
char * temp = get_win32_path (buf3);
501
/* Remove trailing slashes, return empty string if root directory. */
503
while (len > 0 && buf3[len - 1] == '/')
505
buf3[len - 1] = '\0';
511
ret = xasprintf ("/%s@%s", poolfs, buf3);
520
#ifdef HAVE_DEVICE_MAPPER
521
static void device_mapper_null_log (int level __attribute__ ((unused)),
522
const char *file __attribute__ ((unused)),
523
int line __attribute__ ((unused)),
524
int dm_errno __attribute__ ((unused)),
525
const char *f __attribute__ ((unused)),
531
grub_device_mapper_supported (void)
533
static int supported = -1;
539
/* Suppress annoying log messages. */
540
dm_log_with_errno_init (&device_mapper_null_log);
542
dmt = dm_task_create (DM_DEVICE_VERSION);
543
supported = (dmt != NULL);
545
dm_task_destroy (dmt);
547
/* Restore the original logger. */
548
dm_log_with_errno_init (NULL);
553
#endif /* HAVE_DEVICE_MAPPER */