chiark / gitweb /
volume_id: remove deprecated functions and bump major version
[elogind.git] / extras / volume_id / lib / lvm.c
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  */
10
11 #ifndef _GNU_SOURCE
12 #define _GNU_SOURCE 1
13 #endif
14
15 #ifdef HAVE_CONFIG_H
16 #  include <config.h>
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <ctype.h>
25
26 #include "libvolume_id.h"
27 #include "libvolume_id-private.h"
28 #include "util.h"
29
30 struct lvm1_super_block {
31         uint8_t id[2];
32 } PACKED;
33
34 struct lvm2_super_block {
35         uint8_t         id[8];
36         uint64_t        sector_xl;
37         uint32_t        crc_xl;
38         uint32_t        offset_xl;
39         uint8_t         type[8];
40 } PACKED;
41
42 struct lvm2_pv_header {
43         uint8_t         id[32];
44         uint64_t        devsize_xl;
45 } PACKED;
46
47 #define LVM1_SB_OFF                     0x400
48 #define LVM1_MAGIC                      "HM"
49
50 int volume_id_probe_lvm1(struct volume_id *id, uint64_t off, uint64_t size)
51 {
52         const uint8_t *buf;
53         struct lvm1_super_block *lvm;
54
55         info("probing at offset 0x%llx\n", (unsigned long long) off);
56
57         buf = volume_id_get_buffer(id, off + LVM1_SB_OFF, 0x800);
58         if (buf == NULL)
59                 return -1;
60
61         lvm = (struct lvm1_super_block *) buf;
62
63         if (memcmp(lvm->id, LVM1_MAGIC, 2) != 0)
64                 return -1;
65
66         volume_id_set_usage(id, VOLUME_ID_RAID);
67         id->type = "LVM1_member";
68
69         return 0;
70 }
71
72 #define LVM2_LABEL_ID                   "LABELONE"
73 #define LVM2LABEL_SCAN_SECTORS          4
74
75 int volume_id_probe_lvm2(struct volume_id *id, uint64_t off, uint64_t size)
76 {
77         const uint8_t *buf;
78         unsigned int soff;
79         struct lvm2_super_block *lvm;
80         struct lvm2_pv_header *pvhdr;
81
82         dbg("probing at offset 0x%llx\n", (unsigned long long) off);
83
84         buf = volume_id_get_buffer(id, off, LVM2LABEL_SCAN_SECTORS * 0x200);
85         if (buf == NULL)
86                 return -1;
87
88
89         for (soff = 0; soff < LVM2LABEL_SCAN_SECTORS * 0x200; soff += 0x200) {
90                 lvm = (struct lvm2_super_block *) &buf[soff];
91
92                 if (memcmp(lvm->id, LVM2_LABEL_ID, 8) == 0)
93                         goto found;
94         }
95
96         return -1;
97
98 found:
99         dbg("found at offset 0x%x (pv hdr offset 0x%x)\n",
100             soff, cpu_to_le32(lvm->offset_xl));
101         soff += cpu_to_le32(lvm->offset_xl);
102         pvhdr = (struct lvm2_pv_header *) &buf[soff];
103         memcpy(id->type_version, lvm->type, 8);
104         volume_id_set_usage(id, VOLUME_ID_RAID);
105         volume_id_set_uuid(id, pvhdr->id, 0, UUID_LVM);
106         id->type = "LVM2_member";
107
108         return 0;
109 }