--- a/grub-core/kern/emu/getroot.c +++ b/grub-core/kern/emu/getroot.c @@ -32,6 +32,9 @@ #include #include #include +#ifdef HAVE_LIMITS_H +#include +#endif #include #include @@ -238,7 +241,67 @@ #endif /* __linux__ */ -#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR) +static char * +find_device_from_pool (const char *poolname) +{ + char *device; + char *cmd; + FILE *fp; + int ret; + char *line; + size_t len; + int st; + + char name[PATH_MAX], state[256], readlen[256], writelen[256], cksum[256], notes[256]; + unsigned int dummy; + + cmd = xasprintf ("zpool status %s", poolname); + fp = popen (cmd, "r"); + free (cmd); + + st = 0; + while (st < 3) + { + line = NULL; + ret = getline (&line, &len, fp); + if (ret == -1) + { + pclose (fp); + return NULL; + } + + if (sscanf (line, " %s %s %s %s %s %s", name, state, readlen, writelen, cksum, notes) >= 5) + switch (st) + { + case 0: + if (!strcmp (name, "NAME") + && !strcmp (state, "STATE") + && !strcmp (readlen, "READ") + && !strcmp (writelen, "WRITE") + && !strcmp (cksum, "CKSUM")) + st++; + break; + case 1: + if (!strcmp (name, poolname)) + st++; + break; + case 2: + if (strcmp (name, "mirror") && !sscanf (name, "mirror-%u", &dummy) + && !sscanf (name, "raidz%u", &dummy) + && !strcmp (state, "ONLINE")) + st++; + break; + } + + free (line); + } + pclose (fp); + + device = xasprintf ("/dev/%s", name); + + return device; +} + static char * find_root_device_from_libzfs (const char *dir) { @@ -250,49 +313,7 @@ if (! poolname) return NULL; - { - zpool_handle_t *zpool; - libzfs_handle_t *libzfs; - nvlist_t *config, *vdev_tree; - nvlist_t **children, **path; - unsigned int nvlist_count; - unsigned int i; - - libzfs = grub_get_libzfs_handle (); - if (! libzfs) - return NULL; - - zpool = zpool_open (libzfs, poolname); - config = zpool_get_config (zpool, NULL); - - if (nvlist_lookup_nvlist (config, "vdev_tree", &vdev_tree) != 0) - error (1, errno, "nvlist_lookup_nvlist (\"vdev_tree\")"); - - if (nvlist_lookup_nvlist_array (vdev_tree, "children", &children, &nvlist_count) != 0) - error (1, errno, "nvlist_lookup_nvlist_array (\"children\")"); - assert (nvlist_count > 0); - - while (nvlist_lookup_nvlist_array (children[0], "children", - &children, &nvlist_count) == 0) - assert (nvlist_count > 0); - - for (i = 0; i < nvlist_count; i++) - { - if (nvlist_lookup_string (children[i], "path", &device) != 0) - error (1, errno, "nvlist_lookup_string (\"path\")"); - - struct stat st; - if (stat (device, &st) == 0) - { - device = xstrdup (device); - break; - } - - device = NULL; - } - - zpool_close (zpool); - } + device = find_device_from_pool (poolname); free (poolname); if (poolfs) @@ -300,7 +321,6 @@ return device; } -#endif #ifdef __MINGW32__ @@ -603,10 +623,8 @@ os_dev = grub_find_root_device_from_mountinfo (dir, NULL); #endif /* __linux__ */ -#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR) if (!os_dev) os_dev = find_root_device_from_libzfs (dir); -#endif if (os_dev) { @@ -708,7 +726,7 @@ } int -grub_util_get_dev_abstraction (const char *os_dev __attribute__((unused))) +grub_util_get_dev_abstraction (const char *os_dev) { #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) /* User explicitly claims that this drive is visible by BIOS. */ --- a/grub-core/kern/emu/misc.c +++ b/grub-core/kern/emu/misc.c @@ -287,7 +287,6 @@ } #endif /* HAVE_LIBZFS */ -#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR) /* ZFS has similar problems to those of btrfs (see above). */ void grub_find_zpool_from_dir (const char *dir, char **poolname, char **poolfs) @@ -348,7 +347,6 @@ else *poolfs = xstrdup (""); } -#endif /* This function never prints trailing slashes (so that its output can be appended a slash unconditionally). */ @@ -361,22 +359,18 @@ dev_t num; size_t len; -#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR) char *poolfs = NULL; -#endif /* canonicalize. */ p = canonicalize_file_name (path); if (p == NULL) grub_util_error ("failed to get canonical path of %s", path); -#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR) /* For ZFS sub-pool filesystems, could be extended to others (btrfs?). */ { char *dummy; grub_find_zpool_from_dir (p, &dummy, &poolfs); } -#endif len = strlen (p) + 1; buf = xstrdup (p); @@ -428,10 +422,8 @@ } #endif free (buf2); -#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR) if (poolfs) return xasprintf ("/%s/@", poolfs); -#endif return xstrdup (""); } else @@ -488,14 +480,12 @@ len--; } -#if defined(HAVE_LIBZFS) && defined(HAVE_LIBNVPAIR) if (poolfs) { ret = xasprintf ("/%s/@%s", poolfs, buf3); free (buf3); } else -#endif ret = buf3; return ret;