summaryrefslogtreecommitdiffstats
path: root/master/debian/no_libzfs.patch
blob: 5b9b7eb226ac84e8944f5ed539d84c19c7c37445 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
--- a/grub-core/kern/emu/getroot.c
+++ b/grub-core/kern/emu/getroot.c
@@ -32,6 +32,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
+#ifdef HAVE_LIMITS_H
+#include <limits.h>
+#endif
 #include <grub/util/misc.h>
 #include <grub/util/lvm.h>
 
@@ -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;