]> git.proxmox.com Git - grub2.git/blame - disk/lvm.c
2008-02-09 Robert Millan <rmh@aybabtu.com>
[grub2.git] / disk / lvm.c
CommitLineData
2b002173 1/* lvm.c - module to read Logical Volumes. */
2/*
3 * GRUB -- GRand Unified Bootloader
5a79f472 4 * Copyright (C) 2006,2007 Free Software Foundation, Inc.
2b002173 5 *
5a79f472 6 * GRUB is free software: you can redistribute it and/or modify
2b002173 7 * it under the terms of the GNU General Public License as published by
5a79f472 8 * the Free Software Foundation, either version 3 of the License, or
2b002173 9 * (at your option) any later version.
10 *
5a79f472 11 * GRUB is distributed in the hope that it will be useful,
2b002173 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
5a79f472 17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2b002173 18 */
19
20#include <grub/dl.h>
21#include <grub/disk.h>
22#include <grub/mm.h>
23#include <grub/err.h>
24#include <grub/misc.h>
25#include <grub/lvm.h>
26
4d42b77f 27static struct grub_lvm_vg *vg_list;
2b002173 28static int lv_count;
29
30\f
31/* Go the string STR and return the number after STR. *P will point
32 at the number. */
33static int
34grub_lvm_getvalue (char **p, char *str)
35{
36 *p = grub_strstr (*p, str) + grub_strlen (str);
37 return grub_strtoul (*p, NULL, 10);
38}
39
40static int
41grub_lvm_iterate (int (*hook) (const char *name))
42{
43 struct grub_lvm_vg *vg;
4d42b77f 44 for (vg = vg_list; vg; vg = vg->next)
2b002173 45 {
46 struct grub_lvm_lv *lv;
4d42b77f 47 for (lv = vg->lvs; lv; lv = lv->next)
2b002173 48 if (hook (lv->name))
49 return 1;
50 }
51
52 return 0;
53}
54
55static grub_err_t
56grub_lvm_open (const char *name, grub_disk_t disk)
57{
58 struct grub_lvm_vg *vg;
59 struct grub_lvm_lv *lv = NULL;
4d42b77f 60 for (vg = vg_list; vg; vg = vg->next)
2b002173 61 {
4d42b77f 62 for (lv = vg->lvs; lv; lv = lv->next)
2b002173 63 if (! grub_strcmp (lv->name, name))
64 break;
65
66 if (lv)
67 break;
68 }
69
70 if (! lv)
71 return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "Unknown device");
72
73 disk->has_partitions = 0;
74 disk->id = lv->number;
75 disk->data = lv;
76 disk->total_sectors = lv->size;
77
78 return 0;
79}
80
81static void
82grub_lvm_close (grub_disk_t disk __attribute ((unused)))
83{
84 return;
85}
86
87static grub_err_t
88grub_lvm_read (grub_disk_t disk, grub_disk_addr_t sector,
89 grub_size_t size, char *buf)
90{
91 grub_err_t err = 0;
92 struct grub_lvm_lv *lv = disk->data;
93 struct grub_lvm_vg *vg = lv->vg;
94 struct grub_lvm_segment *seg = lv->segments;
95 struct grub_lvm_pv *pv;
96 grub_uint64_t offset;
97 grub_uint64_t extent;
98 unsigned int i;
99
100 extent = grub_divmod64 (sector, vg->extent_size, NULL);
101
102 /* Find the right segment. */
103 for (i = 0; i < lv->segment_count; i++)
104 {
105 if ((seg->start_extent <= extent)
106 && ((seg->start_extent + seg->extent_count) > extent))
107 {
108 break;
109 }
110
111 seg++;
112 }
113
114 if (seg->stripe_count == 1)
115 {
116 /* This segment is linear, so that's easy. We just need to find
117 out the offset in the physical volume and read SIZE bytes
118 from that. */
119 struct grub_lvm_stripe *stripe = seg->stripes;
120 grub_uint64_t seg_offset; /* Offset of the segment in PV device. */
121
122 pv = stripe->pv;
123 seg_offset = ((grub_uint64_t) stripe->start
124 * (grub_uint64_t) vg->extent_size) + pv->start;
125
126 offset = sector - ((grub_uint64_t) seg->start_extent
127 * (grub_uint64_t) vg->extent_size) + seg_offset;
128 }
129 else
130 {
131 /* This is a striped segment. We have to find the right PV
132 similar to RAID0. */
133 struct grub_lvm_stripe *stripe = seg->stripes;
134 grub_uint32_t a, b;
135 grub_uint64_t seg_offset; /* Offset of the segment in PV device. */
136 unsigned int stripenr;
137
138 offset = sector - ((grub_uint64_t) seg->start_extent
139 * (grub_uint64_t) vg->extent_size);
140
141 a = grub_divmod64 (offset, seg->stripe_size, NULL);
142 grub_divmod64 (a, seg->stripe_count, &stripenr);
143
144 a = grub_divmod64 (offset, seg->stripe_size * seg->stripe_count, NULL);
145 grub_divmod64 (offset, seg->stripe_size, &b);
146 offset = a * seg->stripe_size + b;
147
148 stripe += stripenr;
149 pv = stripe->pv;
150
151 seg_offset = ((grub_uint64_t) stripe->start
152 * (grub_uint64_t) vg->extent_size) + pv->start;
153
154 offset += seg_offset;
155 }
156
157 /* Check whether we actually know the physical volume we want to
158 read from. */
159 if (pv->disk)
160 err = grub_disk_read (pv->disk, offset, 0,
161 size << GRUB_DISK_SECTOR_BITS, buf);
162 else
163 err = grub_error (GRUB_ERR_UNKNOWN_DEVICE,
164 "Physical volume %s not found", pv->name);
165
166 return err;
167}
168
169static grub_err_t
170grub_lvm_write (grub_disk_t disk __attribute ((unused)),
171 grub_disk_addr_t sector __attribute ((unused)),
172 grub_size_t size __attribute ((unused)),
173 const char *buf __attribute ((unused)))
174{
175 return GRUB_ERR_NOT_IMPLEMENTED_YET;
176}
177
178static int
179grub_lvm_scan_device (const char *name)
180{
181 grub_err_t err;
182 grub_disk_t disk;
183 grub_uint64_t da_offset, da_size, mda_offset, mda_size;
184 char buf[GRUB_LVM_LABEL_SIZE];
185 char vg_id[GRUB_LVM_ID_STRLEN+1];
186 char pv_id[GRUB_LVM_ID_STRLEN+1];
187 char *metadatabuf, *p, *q, *vgname;
188 struct grub_lvm_label_header *lh = (struct grub_lvm_label_header *) buf;
189 struct grub_lvm_pv_header *pvh;
190 struct grub_lvm_disk_locn *dlocn;
191 struct grub_lvm_mda_header *mdah;
192 struct grub_lvm_raw_locn *rlocn;
193 unsigned int i, j, vgname_len;
194 struct grub_lvm_vg *vg;
195 struct grub_lvm_pv *pv;
196
197 disk = grub_disk_open (name);
198 if (!disk)
199 return 0;
200
201 /* Search for label. */
202 for (i = 0; i < GRUB_LVM_LABEL_SCAN_SECTORS; i++)
203 {
204 err = grub_disk_read (disk, i, 0, sizeof(buf), buf);
205 if (err)
206 goto fail;
207
208 if ((! grub_strncmp ((char *)lh->id, GRUB_LVM_LABEL_ID,
209 sizeof (lh->id)))
210 && (! grub_strncmp ((char *)lh->type, GRUB_LVM_LVM2_LABEL,
211 sizeof (lh->type))))
212 break;
213 }
214
215 /* Return if we didn't find a label. */
216 if (i == GRUB_LVM_LABEL_SCAN_SECTORS)
217 goto fail;
218
219 pvh = (struct grub_lvm_pv_header *) (buf + grub_le_to_cpu32(lh->offset_xl));
220
221 for (i = 0, j = 0; i < GRUB_LVM_ID_LEN; i++)
222 {
223 pv_id[j++] = pvh->pv_uuid[i];
224 if ((i != 1) && (i != 29) && (i % 4 == 1))
225 pv_id[j++] = '-';
226 }
227 pv_id[j] = '\0';
228
229 dlocn = pvh->disk_areas_xl;
230 da_offset = grub_le_to_cpu64 (dlocn->offset);
231 da_size = grub_le_to_cpu64 (dlocn->size);
232
233 dlocn++;
234 /* Is it possible to have multiple data/metadata areas? I haven't
235 seen devices that have it. */
236 if (dlocn->offset)
237 {
238 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
239 "We don't support multiple data areas");
240
241 goto fail;
242 }
243
244 dlocn++;
245 mda_offset = grub_le_to_cpu64 (dlocn->offset);
246 mda_size = grub_le_to_cpu64 (dlocn->size);
247 dlocn++;
248
249 if (dlocn->offset)
250 {
251 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
252 "We don't support multiple metadata areas");
253
254 goto fail;
255 }
256
257 metadatabuf = grub_malloc (mda_size);
258 if (! metadatabuf)
259 goto fail;
260
261 err = grub_disk_read (disk, 0, mda_offset, mda_size, metadatabuf);
262 if (err)
263 goto fail2;
264
265 mdah = (struct grub_lvm_mda_header *) metadatabuf;
266 if ((grub_strncmp ((char *)mdah->magic, GRUB_LVM_FMTT_MAGIC,
267 sizeof (mdah->magic)))
268 || (grub_le_to_cpu32 (mdah->version) != GRUB_LVM_FMTT_VERSION))
269 {
270 grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
271 "Unknown metadata header");
272 goto fail2;
273 }
274
275 rlocn = mdah->raw_locns;
276 p = q = metadatabuf + grub_le_to_cpu64 (rlocn->offset);
277
278 while (*q != ' ')
279 q++;
280
281 vgname_len = q - p;
282 vgname = grub_malloc (vgname_len + 1);
283 if (!vgname)
284 goto fail2;
285
286 grub_memcpy (vgname, p, vgname_len);
287 vgname[vgname_len] = '\0';
288
289 p = grub_strstr (q, "id = \"") + sizeof ("id = \"") - 1;
290 grub_memcpy (vg_id, p, GRUB_LVM_ID_STRLEN);
291 vg_id[GRUB_LVM_ID_STRLEN] = '\0';
292
4d42b77f 293 for (vg = vg_list; vg; vg = vg->next)
2b002173 294 {
295 if (! grub_memcmp(vg_id, vg->id, GRUB_LVM_ID_STRLEN))
296 break;
297 }
298
299 if (! vg)
300 {
301 /* First time we see this volume group. We've to create the
302 whole volume group structure. */
303 vg = grub_malloc (sizeof (*vg));
304 if (! vg)
305 {
306 grub_free (vgname);
307 goto fail;
308 }
309 vg->name = vgname;
310 grub_memcpy (vg->id, vg_id, GRUB_LVM_ID_STRLEN+1);
311
312 vg->extent_size = grub_lvm_getvalue (&p, "extent_size = ");
313
314 vg->lvs = NULL;
315 vg->pvs = NULL;
4d42b77f 316 vg->next = vg_list;
317 vg_list = vg;
2b002173 318
319 p = grub_strstr (p, "physical_volumes {")
320 + sizeof ("physical_volumes {") - 1;
321
322 /* Add all the pvs to the volume group. */
323 while (1)
324 {
325 int s;
326 while (grub_isspace (*p))
327 p++;
328
329 if (*p == '}')
330 break;
331
332 pv = grub_malloc (sizeof (*pv));
333 q = p;
334 while (*q != ' ')
335 q++;
336
337 s = q - p;
338 pv->name = grub_malloc (s + 1);
339 grub_memcpy (pv->name, p, s);
340 pv->name[s] = '\0';
341
342 p = grub_strstr (p, "id = \"") + sizeof("id = \"") - 1;
343
344 grub_memcpy (pv->id, p, GRUB_LVM_ID_STRLEN);
345 pv->id[GRUB_LVM_ID_STRLEN] = '\0';
346
347 pv->start = grub_lvm_getvalue (&p, "pe_start = ");
348 pv->disk = NULL;
349 pv->next = vg->pvs;
350 vg->pvs = pv;
351
352 p = grub_strchr (p, '}') + 1;
353 }
354
355 p = grub_strstr (p, "logical_volumes");
356 p += 18;
357
358 /* And add all the lvs to the volume group. */
359 while (1)
360 {
361 int s;
362 struct grub_lvm_lv *lv;
363 struct grub_lvm_segment *seg;
364
365 while (grub_isspace (*p))
366 p++;
367
368 if (*p == '}')
369 break;
370
6555d655 371 lv = grub_malloc (sizeof (*lv));
2b002173 372
373 q = p;
374 while (*q != ' ')
375 q++;
376
377 s = q - p;
378 lv->name = grub_malloc (vgname_len + 1 + s + 1);
379 grub_memcpy (lv->name, vgname, vgname_len);
380 lv->name[vgname_len] = '-';
381 grub_memcpy (lv->name + vgname_len + 1, p, s);
382 lv->name[vgname_len + 1 + s] = '\0';
383
384 lv->size = 0;
385
386 lv->segment_count = grub_lvm_getvalue (&p, "segment_count = ");
387 lv->segments = grub_malloc (sizeof (*seg) * lv->segment_count);
388 seg = lv->segments;
389
390 for (i = 0; i < lv->segment_count; i++)
391 {
392 struct grub_lvm_stripe *stripe;
393
394 p = grub_strstr (p, "segment");
395
396 seg->start_extent = grub_lvm_getvalue (&p, "start_extent = ");
397 seg->extent_count = grub_lvm_getvalue (&p, "extent_count = ");
398 seg->stripe_count = grub_lvm_getvalue (&p, "stripe_count = ");
399
400 lv->size += seg->extent_count * vg->extent_size;
401
402 if (seg->stripe_count != 1)
403 seg->stripe_size = grub_lvm_getvalue (&p, "stripe_size = ");
404
405 seg->stripes = grub_malloc (sizeof (*stripe)
406 * seg->stripe_count);
407 stripe = seg->stripes;
408
409 p = grub_strstr (p, "stripes = [")
410 + sizeof("stripes = [") - 1;
411
412 for (j = 0; j < seg->stripe_count; j++)
413 {
414 char pvname[10];
415
416 q = p = grub_strchr (p, '"') + 1;
417 while (*q != '"')
418 q++;
419
420 s = q - p;
421 grub_memcpy (pvname, p, s);
422 pvname[s] = '\0';
423
424 for (pv = vg->pvs; pv; pv = pv->next)
425 {
426 if (! grub_strcmp (pvname, pv->name))
427 {
428 stripe->pv = pv;
429 break;
430 }
431 }
432
433 p = grub_strchr (p, ',') + 1;
434 stripe->start = grub_strtoul (p, NULL, 10);
435
436 stripe++;
437 }
438
439 seg++;
440 }
441
442 lv->number = lv_count++;
443 lv->vg = vg;
444 lv->next = vg->lvs;
445 vg->lvs = lv;
446
447 p = grub_strchr (p, '}') + 3;
448 }
449 }
450 else
451 {
452 grub_free (vgname);
453 }
454
455 /* Match the device we are currently reading from with the right
456 PV. */
457 for (pv = vg->pvs; pv; pv = pv->next)
458 {
459 if (! grub_memcmp (pv->id, pv_id, GRUB_LVM_ID_STRLEN))
460 {
461 pv->disk = grub_disk_open (name);
462 break;
463 }
464 }
465
466 fail2:
467 grub_free (metadatabuf);
468 fail:
469 grub_disk_close (disk);
470 return 0;
471}
472
473static struct grub_disk_dev grub_lvm_dev =
474 {
475 .name = "lvm",
476 .id = GRUB_DISK_DEVICE_LVM_ID,
477 .iterate = grub_lvm_iterate,
478 .open = grub_lvm_open,
479 .close = grub_lvm_close,
480 .read = grub_lvm_read,
481 .write = grub_lvm_write,
482 .next = 0
483 };
484
485\f
486GRUB_MOD_INIT(lvm)
487{
488 grub_device_iterate (&grub_lvm_scan_device);
489 grub_disk_dev_register (&grub_lvm_dev);
490}
491
492GRUB_MOD_FINI(lvm)
493{
494 grub_disk_dev_unregister (&grub_lvm_dev);
495 /* FIXME: free the lvm list. */
496}