2010-07-30 Robert Millan <rmh@gnu.org>
Enable `grub-probe -t device' resolution on ZFS. * configure.ac: Check for getfsstat(), libzfs and libnvpair. * include/grub/util/libnvpair.h: New file. * include/grub/util/libzfs.h: New file. * kern/emu/getroot.c: Include `<assert.h>' and `<error.h>'. [HAVE_LIBZFS && HAVE_LIBNVPAIR]: Include `<grub/util/libzfs.h>' and `<grub/util/libnvpair.h>'. [HAVE_GETFSSTAT]: Include `<sys/mount.h>'. (find_mount_point_from_dir): New static function. [HAVE_LIBZFS && HAVE_LIBNVPAIR] (find_root_device_from_libzfs): New function. [HAVE_LIBZFS && HAVE_LIBNVPAIR] (grub_guess_root_device): Use find_root_device_from_libzfs() before ressorting to find_root_device(). * include/grub/util/misc.h (grub_util_init_libzfs): New function prototype. * util/misc.c: Include `<grub/util/libzfs.h>'. (grub_util_init_libzfs): New function. [HAVE_LIBZFS] (libzfs_handle): New global variable. [HAVE_LIBZFS] (fini_libzfs): New static function. (grub_util_init_libzfs): New function. * util/grub-probe.c (main): Call grub_util_init_libzfs().
This commit is contained in:
parent
f7790cdd5d
commit
a184f9c801
8 changed files with 285 additions and 1 deletions
|
@ -20,11 +20,13 @@
|
|||
#include <config.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <assert.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <error.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
@ -41,6 +43,15 @@
|
|||
# include <sys/wait.h>
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
|
||||
# include <grub/util/libzfs.h>
|
||||
# include <grub/util/libnvpair.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_GETFSSTAT
|
||||
# include <sys/mount.h>
|
||||
#endif
|
||||
|
||||
#include <grub/mm.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/emu/misc.h>
|
||||
|
@ -86,6 +97,62 @@ xgetcwd (void)
|
|||
return path;
|
||||
}
|
||||
|
||||
static char *
|
||||
find_mount_point_from_dir (const char *dir)
|
||||
{
|
||||
struct stat st;
|
||||
typeof (st.st_dev) fs;
|
||||
char *prev, *next, *slash, *statdir;
|
||||
|
||||
if (stat (dir, &st) == -1)
|
||||
error (1, errno, "stat (%s)", dir);
|
||||
|
||||
fs = st.st_dev;
|
||||
|
||||
prev = xstrdup (dir);
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* Remove last slash. */
|
||||
next = xstrdup (prev);
|
||||
slash = strrchr (next, '/');
|
||||
if (! slash)
|
||||
{
|
||||
free (next);
|
||||
free (prev);
|
||||
return NULL;
|
||||
}
|
||||
*slash = '\0';
|
||||
|
||||
/* A next empty string counts as /. */
|
||||
if (next[0] == '\0')
|
||||
statdir = "/";
|
||||
else
|
||||
statdir = next;
|
||||
|
||||
if (stat (statdir, &st) == -1)
|
||||
error (1, errno, "stat (%s)", next);
|
||||
|
||||
if (st.st_dev != fs)
|
||||
{
|
||||
/* Found mount point. */
|
||||
free (next);
|
||||
return prev;
|
||||
}
|
||||
|
||||
free (prev);
|
||||
prev = next;
|
||||
|
||||
/* We've already seen an empty string, which means we
|
||||
reached /. Nothing left to do. */
|
||||
if (prev[0] == '\0')
|
||||
{
|
||||
free (prev);
|
||||
return xstrdup ("/");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __linux__
|
||||
|
||||
/* Statting something on a btrfs filesystem always returns a virtual device
|
||||
|
@ -166,6 +233,88 @@ find_root_device_from_mountinfo (const char *dir)
|
|||
|
||||
#endif /* __linux__ */
|
||||
|
||||
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
|
||||
|
||||
/* ZFS has similar problems to those of btrfs (see above). */
|
||||
static char *
|
||||
find_root_device_from_libzfs (const char *dir)
|
||||
{
|
||||
char *device = NULL;
|
||||
char *poolname = NULL;
|
||||
char *poolfs = NULL;
|
||||
char *mnt_point;
|
||||
char *slash;
|
||||
|
||||
mnt_point = find_mount_point_from_dir (dir);
|
||||
|
||||
#ifdef HAVE_GETFSSTAT
|
||||
{
|
||||
int mnt_count = getfsstat (NULL, 0, MNT_WAIT);
|
||||
if (mnt_count == -1)
|
||||
error (1, errno, "getfsstat");
|
||||
|
||||
struct statfs *mnt = xmalloc (mnt_count * sizeof (*mnt));
|
||||
|
||||
mnt_count = getfsstat (mnt, mnt_count * sizeof (*mnt), MNT_WAIT);
|
||||
if (mnt_count == -1)
|
||||
error (1, errno, "getfsstat");
|
||||
|
||||
unsigned int i;
|
||||
for (i = 0; i < (unsigned) mnt_count; i++)
|
||||
if (!strcmp (mnt[i].f_fstypename, "zfs")
|
||||
&& !strcmp (mnt[i].f_mntonname, mnt_point))
|
||||
{
|
||||
poolname = xstrdup (mnt[i].f_mntfromname);
|
||||
break;
|
||||
}
|
||||
|
||||
free (mnt);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (! poolname)
|
||||
return NULL;
|
||||
|
||||
slash = strchr (poolname, '/');
|
||||
if (slash)
|
||||
{
|
||||
*slash = '\0';
|
||||
poolfs = slash + 1;
|
||||
}
|
||||
|
||||
{
|
||||
zpool_handle_t *zpool;
|
||||
nvlist_t *nvlist;
|
||||
nvlist_t **nvlist_array;
|
||||
unsigned int nvlist_count;
|
||||
|
||||
zpool = zpool_open (libzfs_handle, poolname);
|
||||
nvlist = zpool_get_config (zpool, NULL);
|
||||
|
||||
if (nvlist_lookup_nvlist (nvlist, "vdev_tree", &nvlist) != 0)
|
||||
error (1, errno, "nvlist_lookup_nvlist (\"vdev_tree\")");
|
||||
|
||||
if (nvlist_lookup_nvlist_array (nvlist, "children", &nvlist_array, &nvlist_count) != 0)
|
||||
error (1, errno, "nvlist_lookup_nvlist_array (\"children\")");
|
||||
|
||||
do
|
||||
{
|
||||
assert (nvlist_count > 0);
|
||||
} while (nvlist_lookup_nvlist_array (nvlist_array[0], "children",
|
||||
&nvlist_array, &nvlist_count) == 0);
|
||||
|
||||
if (nvlist_lookup_string (nvlist_array[0], "path", &device) != 0)
|
||||
error (1, errno, "nvlist_lookup_string (\"path\")");
|
||||
|
||||
zpool_close (zpool);
|
||||
}
|
||||
|
||||
free (poolname);
|
||||
|
||||
return device;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef __MINGW32__
|
||||
|
||||
static char *
|
||||
|
@ -458,6 +607,12 @@ grub_guess_root_device (const char *dir)
|
|||
return os_dev;
|
||||
#endif /* __linux__ */
|
||||
|
||||
#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR)
|
||||
os_dev = find_root_device_from_libzfs (dir);
|
||||
if (os_dev)
|
||||
return os_dev;
|
||||
#endif
|
||||
|
||||
if (stat (dir, &st) < 0)
|
||||
grub_util_error ("cannot stat `%s'", dir);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue