Add mbr pseudo metadata handler.
[thirdparty/mdadm.git] / Examine.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
22 * Email: <neilb@suse.de>
23 */
24
25 #include "mdadm.h"
26 #include "dlink.h"
27
28 #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
29 #error no endian defined
30 #endif
31 #include "md_u.h"
32 #include "md_p.h"
33 int Examine(mddev_dev_t devlist, int brief, int export, int scan,
34 int SparcAdjust, struct supertype *forcest,
35 char *homehost)
36 {
37
38 /* Read the raid superblock from a device and
39 * display important content.
40 *
41 * If cannot be found, print reason: too small, bad magic
42 *
43 * Print:
44 * version, ctime, level, size, raid+spare+
45 * prefered minor
46 * uuid
47 *
48 * utime, state etc
49 *
50 * If (brief) gather devices for same array and just print a mdadm.conf line including devices=
51 * if devlist==NULL, use conf_get_devs()
52 */
53 int fd;
54 int rv = 0;
55 int err = 0;
56
57 struct array {
58 struct supertype *st;
59 struct mdinfo info;
60 void *devs;
61 struct array *next;
62 int spares;
63 } *arrays = NULL;
64
65 for (; devlist ; devlist=devlist->next) {
66 struct supertype *st;
67
68 fd = dev_open(devlist->devname, O_RDONLY);
69 if (fd < 0) {
70 if (!scan) {
71 fprintf(stderr,Name ": cannot open %s: %s\n",
72 devlist->devname, strerror(errno));
73 rv = 1;
74 }
75 err = 1;
76 }
77 else {
78 if (forcest)
79 st = dup_super(forcest);
80 else
81 st = guess_super(fd);
82 if (st)
83 err = st->ss->load_super(st, fd,
84 (brief||scan) ? NULL
85 :devlist->devname);
86 else {
87 if (!brief) {
88 fprintf(stderr, Name ": No md superblock detected on %s.\n", devlist->devname);
89 rv = 1;
90 }
91 err = 1;
92 }
93 close(fd);
94 }
95 if (err)
96 continue;
97
98 if (SparcAdjust)
99 st->ss->update_super(st, NULL, "sparc2.2",
100 devlist->devname, 0, 0, NULL);
101 /* Ok, its good enough to try, though the checksum could be wrong */
102
103 if (brief && st->ss->brief_examine_super == NULL) {
104 if (!scan)
105 fprintf(stderr, Name ": No brief listing for %s on %s\n",
106 st->ss->name, devlist->devname);
107 } else if (brief) {
108 struct array *ap;
109 char *d;
110 for (ap=arrays; ap; ap=ap->next) {
111 if (st->ss == ap->st->ss &&
112 st->ss->compare_super(ap->st, st)==0)
113 break;
114 }
115 if (!ap) {
116 ap = malloc(sizeof(*ap));
117 ap->devs = dl_head();
118 ap->next = arrays;
119 ap->spares = 0;
120 ap->st = st;
121 arrays = ap;
122 st->ss->getinfo_super(st, &ap->info);
123 } else
124 st->ss->getinfo_super(st, &ap->info);
125 if (!st->loaded_container &&
126 !(ap->info.disk.state & (1<<MD_DISK_SYNC)))
127 ap->spares++;
128 d = dl_strdup(devlist->devname);
129 dl_add(ap->devs, d);
130 } else if (export) {
131 if (st->ss->export_examine_super)
132 st->ss->export_examine_super(st);
133 } else {
134 printf("%s:\n",devlist->devname);
135 st->ss->examine_super(st, homehost);
136 st->ss->free_super(st);
137 }
138 }
139 if (brief) {
140 struct array *ap;
141 for (ap=arrays; ap; ap=ap->next) {
142 char sep='=';
143 char *d;
144 int newline = 0;
145
146 ap->st->ss->brief_examine_super(ap->st, brief > 1);
147 if (ap->spares)
148 newline += printf(" spares=%d", ap->spares);
149 if (brief > 1) {
150 newline += printf(" devices");
151 for (d=dl_next(ap->devs); d!= ap->devs; d=dl_next(d)) {
152 printf("%c%s", sep, d);
153 sep=',';
154 }
155 }
156 if (ap->st->ss->brief_examine_subarrays) {
157 if (newline)
158 printf("\n");
159 ap->st->ss->brief_examine_subarrays(ap->st, brief > 1);
160 }
161 ap->st->ss->free_super(ap->st);
162 /* FIXME free ap */
163 if (ap->spares || brief > 1)
164 printf("\n");
165 }
166 }
167 return rv;
168 }