chiark / gitweb /
volume_id: use PRIu64i, PRIx64 macros
[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
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
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
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef _GNU_SOURCE
21 #define _GNU_SOURCE 1
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <ctype.h>
30
31 #include "libvolume_id.h"
32 #include "libvolume_id-private.h"
33
34 struct lvm1_super_block {
35         uint8_t id[2];
36 } PACKED;
37
38 struct lvm2_super_block {
39         uint8_t         id[8];
40         uint64_t        sector_xl;
41         uint32_t        crc_xl;
42         uint32_t        offset_xl;
43         uint8_t         type[8];
44 } PACKED;
45
46 struct lvm2_pv_header {
47         uint8_t         id[32];
48         uint64_t        devsize_xl;
49 } PACKED;
50
51 #define LVM1_SB_OFF                     0x400
52 #define LVM1_MAGIC                      "HM"
53
54 int volume_id_probe_lvm1(struct volume_id *id, uint64_t off, uint64_t size)
55 {
56         const uint8_t *buf;
57         struct lvm1_super_block *lvm;
58
59         info("probing at offset 0x%" PRIx64 "\n", off);
60
61         buf = volume_id_get_buffer(id, off + LVM1_SB_OFF, 0x800);
62         if (buf == NULL)
63                 return -1;
64
65         lvm = (struct lvm1_super_block *) buf;
66
67         if (memcmp(lvm->id, LVM1_MAGIC, 2) != 0)
68                 return -1;
69
70         volume_id_set_usage(id, VOLUME_ID_RAID);
71         id->type = "LVM1_member";
72
73         return 0;
74 }
75
76 #define LVM2_LABEL_ID                   "LABELONE"
77 #define LVM2LABEL_SCAN_SECTORS          4
78
79 int volume_id_probe_lvm2(struct volume_id *id, uint64_t off, uint64_t size)
80 {
81         const uint8_t *buf;
82         unsigned int soff;
83         struct lvm2_super_block *lvm;
84         struct lvm2_pv_header *pvhdr;
85
86         dbg("probing at offset 0x%" PRIx64 "\n", off);
87
88         buf = volume_id_get_buffer(id, off, LVM2LABEL_SCAN_SECTORS * 0x200);
89         if (buf == NULL)
90                 return -1;
91
92
93         for (soff = 0; soff < LVM2LABEL_SCAN_SECTORS * 0x200; soff += 0x200) {
94                 lvm = (struct lvm2_super_block *) &buf[soff];
95
96                 if (memcmp(lvm->id, LVM2_LABEL_ID, 8) == 0)
97                         goto found;
98         }
99
100         return -1;
101
102 found:
103         dbg("found at offset 0x%x (pv hdr offset 0x%x)\n",
104             soff, cpu_to_le32(lvm->offset_xl));
105         soff += cpu_to_le32(lvm->offset_xl);
106         pvhdr = (struct lvm2_pv_header *) &buf[soff];
107         memcpy(id->type_version, lvm->type, 8);
108         volume_id_set_usage(id, VOLUME_ID_RAID);
109         volume_id_set_uuid(id, pvhdr->id, 0, UUID_LVM);
110         id->type = "LVM2_member";
111
112         return 0;
113 }