2 * volume_id - reads filesystem label and uuid
4 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
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.
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.
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/>.
32 #include "libvolume_id.h"
33 #include "libvolume_id-private.h"
35 struct mdp0_super_block {
37 uint32_t major_version;
38 uint32_t minor_version;
39 uint32_t patch_version;
40 uint32_t gvalid_words;
48 uint32_t not_persistent;
54 struct mdp1_super_block {
56 uint32_t major_version;
63 #define MD_RESERVED_BYTES 0x10000
64 #define MD_SB_MAGIC 0xa92b4efc
66 static int volume_id_probe_linux_raid0(struct volume_id *id, uint64_t off, uint64_t size)
69 struct mdp0_super_block *mdp0;
75 info("probing at offset 0x%llx, size 0x%llx\n",
76 (unsigned long long) off, (unsigned long long) size);
80 buf = volume_id_get_buffer(id, off, 0x800);
83 mdp0 = (struct mdp0_super_block *) buf;
85 if (le32_to_cpu(mdp0->md_magic) == MD_SB_MAGIC) {
86 uuid.ints[0] = bswap_32(mdp0->set_uuid0);
87 if (le32_to_cpu(mdp0->minor_version >= 90)) {
88 uuid.ints[1] = bswap_32(mdp0->set_uuid1);
89 uuid.ints[2] = bswap_32(mdp0->set_uuid2);
90 uuid.ints[3] = bswap_32(mdp0->set_uuid3);
96 volume_id_set_uuid(id, uuid.bytes, 0, UUID_MD);
97 snprintf(id->type_version, sizeof(id->type_version)-1, "%u.%u.%u",
98 le32_to_cpu(mdp0->major_version),
99 le32_to_cpu(mdp0->minor_version),
100 le32_to_cpu(mdp0->patch_version));
101 } else if (be32_to_cpu(mdp0->md_magic) == MD_SB_MAGIC) {
102 uuid.ints[0] = mdp0->set_uuid0;
103 if (be32_to_cpu(mdp0->minor_version >= 90)) {
104 uuid.ints[1] = mdp0->set_uuid1;
105 uuid.ints[2] = mdp0->set_uuid2;
106 uuid.ints[3] = mdp0->set_uuid3;
112 volume_id_set_uuid(id, uuid.bytes, 0, UUID_MD);
113 snprintf(id->type_version, sizeof(id->type_version)-1, "%u.%u.%u",
114 be32_to_cpu(mdp0->major_version),
115 be32_to_cpu(mdp0->minor_version),
116 be32_to_cpu(mdp0->patch_version));
120 volume_id_set_usage(id, VOLUME_ID_RAID);
121 id->type = "linux_raid_member";
125 static int volume_id_probe_linux_raid1(struct volume_id *id, uint64_t off, uint64_t size)
128 struct mdp1_super_block *mdp1;
130 info("probing at offset 0x%llx, size 0x%llx\n",
131 (unsigned long long) off, (unsigned long long) size);
133 buf = volume_id_get_buffer(id, off, 0x800);
136 mdp1 = (struct mdp1_super_block *) buf;
138 if (le32_to_cpu(mdp1->magic) != MD_SB_MAGIC)
141 if (le32_to_cpu(mdp1->major_version) != 1)
144 volume_id_set_uuid(id, mdp1->set_uuid, 0, UUID_MD);
145 volume_id_set_label_raw(id, mdp1->set_name, 32);
146 volume_id_set_label_string(id, mdp1->set_name, 32);
147 volume_id_set_usage(id, VOLUME_ID_RAID);
148 id->type = "linux_raid_member";
152 int volume_id_probe_linux_raid(struct volume_id *id, uint64_t off, uint64_t size)
156 if (size > MD_RESERVED_BYTES) {
157 /* version 0 at the end of the device */
158 sboff = (size & ~(MD_RESERVED_BYTES - 1)) - MD_RESERVED_BYTES;
159 if (volume_id_probe_linux_raid0(id, off + sboff, size) == 0)
162 /* version 1.0 at the end of the device */
163 sboff = (size & ~(0x1000 - 1)) - 0x2000;
164 if (volume_id_probe_linux_raid1(id, off + sboff, size) == 0) {
165 strcpy(id->type_version, "1.0");
170 /* version 1.1 at the start of the device */
171 if (volume_id_probe_linux_raid1(id, off, size) == 0) {
172 strcpy(id->type_version, "1.1");
176 /* version 1.2 at 4k offset from the start */
177 if (volume_id_probe_linux_raid1(id, off + 0x1000, size) == 0) {
178 strcpy(id->type_version, "1.2");