2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
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.
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.
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
22 * Email: <neilb@suse.de>
30 int Detail(char *dev, int brief, int export, int test, char *homehost)
33 * Print out details for an md array by using
34 * GET_ARRAY_INFO and GET_DISK_INFO ioctl calls
37 int fd = open(dev, O_RDONLY);
39 mdu_array_info_t array;
40 mdu_disk_info_t *disks;
48 int is_26 = get_linux_version() >= 2006000;
49 int is_rebuilding = 0;
52 char *subarray = NULL;
53 int max_disks = MD_SB_DISKS; /* just a default */
54 struct mdinfo *info = NULL;
57 char *container = NULL;
59 int rv = test ? 4 : 1;
64 fprintf(stderr, Name ": cannot open %s: %s\n",
65 dev, strerror(errno));
68 vers = md_get_version(fd);
70 fprintf(stderr, Name ": %s does not appear to be an md device\n",
76 fprintf(stderr, Name ": cannot get detail for md device %s: driver version too old.\n",
81 if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
83 fprintf(stderr, Name ": md device %s does not appear to be active.\n",
86 fprintf(stderr, Name ": cannot get array detail for %s: %s\n",
87 dev, strerror(errno));
91 sra = sysfs_read(fd, 0, GET_VERSION);
92 st = super_by_fd(fd, &subarray);
94 if (fstat(fd, &stb) != 0 && !S_ISBLK(stb.st_mode))
99 max_disks = st->max_devs;
102 /* This is a subarray of some container.
103 * We want the name of the container, and the member
105 int dn = st->container_dev;
108 container = map_dev(dev2major(dn), dev2minor(dn), 1);
111 /* try to load a superblock */
112 if (st) for (d = 0; d < max_disks; d++) {
113 mdu_disk_info_t disk;
118 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
120 if (d >= array.raid_disks &&
125 if (array.raid_disks > 0 &&
126 (disk.state & (1 << MD_DISK_ACTIVE)) == 0)
129 dv = map_dev(disk.major, disk.minor, 1);
133 fd2 = dev_open(dv, O_RDONLY);
138 st->ss->free_super(st);
140 err = st->ss->load_super(st, fd2, NULL);
147 info = st->ss->container_content(st, subarray);
149 info = malloc(sizeof(*info));
150 st->ss->getinfo_super(st, info, NULL);
155 if (array.raid_disks != 0 && /* container */
156 (info->array.ctime != array.ctime ||
157 info->array.level != array.level)) {
158 st->ss->free_super(st);
161 /* some formats (imsm) have free-floating-spares
162 * with a uuid of uuid_zero, they don't
163 * have very good info about the rest of the
164 * container, so keep searching when
165 * encountering such a device. Otherwise, stop
166 * after the first successful call to
169 if (memcmp(uuid_zero,
171 sizeof(uuid_zero)) == 0) {
172 st->ss->free_super(st);
178 /* Ok, we have some info to print... */
179 c = map_num(pers, array.level);
182 if (array.raid_disks) {
184 printf("MD_LEVEL=%s\n", c);
185 printf("MD_DEVICES=%d\n", array.raid_disks);
187 printf("MD_LEVEL=container\n");
188 printf("MD_DEVICES=%d\n", array.nr_disks);
191 printf("MD_CONTAINER=%s\n", container);
192 printf("MD_MEMBER=%s\n", member);
194 if (sra && sra->array.major_version < 0)
195 printf("MD_METADATA=%s\n", sra->text_version);
197 printf("MD_METADATA=%d.%d\n",
198 array.major_version, array.minor_version);
201 if (st && st->sb && info) {
203 struct map_ent *mp, *map = NULL;
205 fname_from_uuid(st, info, nbuf, ':');
206 printf("MD_UUID=%s\n", nbuf+5);
207 mp = map_by_uuid(&map, info->uuid);
208 if (mp && mp->path &&
209 strncmp(mp->path, "/dev/md/", 8) == 0)
210 printf("MD_DEVNAME=%s\n", mp->path+8);
212 if (st->ss->export_detail_super)
213 st->ss->export_detail_super(st);
215 struct map_ent *mp, *map = NULL;
217 mp = map_by_devnum(&map, fd2devnum(fd));
219 __fname_from_uuid(mp->uuid, 0, nbuf, ':');
220 printf("MD_UUID=%s\n", nbuf+5);
222 if (mp && mp->path &&
223 strncmp(mp->path, "/dev/md/", 8) == 0)
224 printf("MD_DEVNAME=%s\n", mp->path+8);
229 disks = malloc(max_disks * sizeof(mdu_disk_info_t));
230 for (d=0; d<max_disks; d++) {
231 disks[d].state = (1<<MD_DISK_REMOVED);
232 disks[d].major = disks[d].minor = 0;
233 disks[d].number = disks[d].raid_disk = d;
236 next = array.raid_disks;
237 for (d=0; d < max_disks; d++) {
238 mdu_disk_info_t disk;
240 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
241 if (d < array.raid_disks)
242 fprintf(stderr, Name ": cannot get device detail for device %d: %s\n",
246 if (disk.major == 0 && disk.minor == 0)
248 if (disk.raid_disk >= 0 && disk.raid_disk < array.raid_disks)
249 disks[disk.raid_disk] = disk;
250 else if (next < max_disks)
251 disks[next++] = disk;
254 avail = calloc(array.raid_disks, 1);
256 for (d= 0; d < array.raid_disks; d++) {
257 mdu_disk_info_t disk = disks[d];
259 if ((disk.state & (1<<MD_DISK_SYNC))) {
266 mdu_bitmap_file_t bmf;
267 printf("ARRAY %s", dev);
269 if (array.raid_disks)
270 printf(" level=%s num-devices=%d",
274 printf(" level=container num-devices=%d",
278 printf(" container=%s", container);
279 printf(" member=%s", member);
281 if (sra && sra->array.major_version < 0)
282 printf(" metadata=%s", sra->text_version);
284 printf(" metadata=%d.%d",
285 array.major_version, array.minor_version);
288 /* Only try GET_BITMAP_FILE for 0.90.01 and later */
290 ioctl(fd, GET_BITMAP_FILE, &bmf) == 0 &&
292 printf(" bitmap=%s", bmf.pathname);
295 mdu_bitmap_file_t bmf;
296 unsigned long long larray_size;
297 struct mdstat_ent *ms = mdstat_read(0, 0);
298 struct mdstat_ent *e;
299 int devnum = array.md_minor;
300 if (major(stb.st_rdev) == (unsigned)get_mdp_major())
301 devnum = -1 - devnum;
303 for (e=ms; e; e=e->next)
304 if (e->devnum == devnum)
306 if (!get_dev_size(fd, NULL, &larray_size))
309 printf("%s:\n", dev);
312 printf(" Container : %s, member %s\n", container, member);
314 if (sra && sra->array.major_version < 0)
315 printf(" Version : %s\n", sra->text_version);
317 printf(" Version : %d.%d\n",
318 array.major_version, array.minor_version);
323 printf(" Creation Time : %.24s\n", ctime(&atime));
324 if (array.raid_disks == 0) c = "container";
325 printf(" Raid Level : %s\n", c?c:"-unknown-");
327 printf(" Array Size : %llu%s\n", (larray_size>>10), human_size(larray_size));
328 if (array.level >= 1) {
329 if (array.major_version != 0 &&
330 (larray_size >= 0xFFFFFFFFULL|| array.size == 0)) {
331 unsigned long long dsize = get_component_size(fd);
333 printf(" Used Dev Size : %llu%s\n",
335 human_size((long long)dsize<<9));
337 printf(" Used Dev Size : unknown\n");
339 printf(" Used Dev Size : %d%s\n", array.size,
340 human_size((long long)array.size<<10));
342 if (array.raid_disks)
343 printf(" Raid Devices : %d\n", array.raid_disks);
344 printf(" Total Devices : %d\n", array.nr_disks);
346 ((sra == NULL && array.major_version == 0) ||
347 (sra && sra->array.major_version == 0)))
348 printf("Preferred Minor : %d\n", array.md_minor);
349 if (sra == NULL || sra->array.major_version >= 0)
350 printf(" Persistence : Superblock is %spersistent\n",
351 array.not_persistent?"not ":"");
353 /* Only try GET_BITMAP_FILE for 0.90.01 and later */
355 ioctl(fd, GET_BITMAP_FILE, &bmf) == 0 &&
357 printf(" Intent Bitmap : %s\n", bmf.pathname);
359 } else if (array.state & (1<<MD_SB_BITMAP_PRESENT))
360 printf(" Intent Bitmap : Internal\n\n");
363 printf(" Update Time : %.24s\n", ctime(&atime));
364 if (array.raid_disks) {
365 static char *sync_action[] = {", recovering",", resyncing",", reshaping",", checking"};
367 if (avail_disks == array.raid_disks)
369 else if (!enough(array.level, array.raid_disks,
370 array.layout, 1, avail, avail_disks))
375 printf(" State : %s%s%s%s%s%s \n",
376 (array.state&(1<<MD_SB_CLEAN))?"clean":"active", st,
377 (!e || (e->percent < 0 && e->percent != PROCESS_PENDING &&
378 e->percent != PROCESS_DELAYED)) ? "" : sync_action[e->resync],
379 larray_size ? "": ", Not Started",
380 e->percent == PROCESS_DELAYED ? " (DELAYED)": "",
381 e->percent == PROCESS_PENDING ? " (PENDING)": "");
383 if (array.raid_disks)
384 printf(" Active Devices : %d\n", array.active_disks);
385 printf("Working Devices : %d\n", array.working_disks);
386 if (array.raid_disks) {
387 printf(" Failed Devices : %d\n", array.failed_disks);
388 printf(" Spare Devices : %d\n", array.spare_disks);
391 if (array.level == 5) {
392 c = map_num(r5layout, array.layout);
393 printf(" Layout : %s\n", c?c:"-unknown-");
395 if (array.level == 6) {
396 c = map_num(r6layout, array.layout);
397 printf(" Layout : %s\n", c?c:"-unknown-");
399 if (array.level == 10) {
401 print_r10_layout(array.layout);
404 switch (array.level) {
410 if (array.chunk_size)
411 printf(" Chunk Size : %dK\n\n",
412 array.chunk_size/1024);
415 printf(" Rounding : %dK\n\n", array.chunk_size/1024);
420 if (e && e->percent >= 0) {
421 static char *sync_action[] = {"Rebuild", "Resync", "Reshape", "Check"};
422 printf(" %7s Status : %d%% complete\n", sync_action[e->resync], e->percent);
427 if (st->sb && info->reshape_active) {
429 This is pretty boring
430 printf(" Reshape pos'n : %llu%s\n", (unsigned long long) info->reshape_progress<<9,
431 human_size((unsigned long long)info->reshape_progress<<9));
433 if (info->delta_disks != 0)
434 printf(" Delta Devices : %d, (%d->%d)\n",
435 info->delta_disks, array.raid_disks - info->delta_disks, array.raid_disks);
436 if (info->new_level != array.level) {
437 char *c = map_num(pers, info->new_level);
438 printf(" New Level : %s\n", c?c:"-unknown-");
440 if (info->new_level != array.level ||
441 info->new_layout != array.layout) {
442 if (info->new_level == 5) {
443 char *c = map_num(r5layout, info->new_layout);
444 printf(" New Layout : %s\n",
447 if (info->new_level == 6) {
448 char *c = map_num(r6layout, info->new_layout);
449 printf(" New Layout : %s\n",
452 if (info->new_level == 10) {
453 printf(" New Layout : near=%d, %s=%d\n",
454 info->new_layout&255,
455 (info->new_layout&0x10000)?"offset":"far",
456 (info->new_layout>>8)&255);
459 if (info->new_chunk != array.chunk_size)
460 printf(" New Chunksize : %dK\n", info->new_chunk/1024);
462 } else if (e && e->percent >= 0)
465 st->ss->detail_super(st, homehost);
467 if (array.raid_disks == 0 && sra && sra->array.major_version == -1
468 && sra->array.minor_version == -2 && sra->text_version[0] != '/') {
469 /* This looks like a container. Find any active arrays
470 * That claim to be a member.
472 DIR *dir = opendir("/sys/block");
475 printf(" Member Arrays :");
477 while (dir && (de = readdir(dir)) != NULL) {
480 int nlen = strlen(sra->sys_name);
482 if (de->d_name[0] == '.')
484 sprintf(path, "/sys/block/%s/md/metadata_version",
486 if (load_sys(path, vbuf) < 0)
488 if (strncmp(vbuf, "external:", 9) != 0 ||
489 !is_subarray(vbuf+9) ||
490 strncmp(vbuf+10, sra->sys_name, nlen) != 0 ||
491 vbuf[10+nlen] != '/')
493 dn = devname2devnum(de->d_name);
494 printf(" %s", map_dev(dev2major(dn),
502 if (array.raid_disks)
503 printf(" Number Major Minor RaidDevice State\n");
505 printf(" Number Major Minor RaidDevice\n");
509 for (d= 0; d < max_disks; d++) {
511 mdu_disk_info_t disk = disks[d];
513 if (d >= array.raid_disks &&
518 if (d == array.raid_disks) printf("\n");
519 if (disk.raid_disk < 0)
520 printf(" %5d %5d %5d - ",
521 disk.number, disk.major, disk.minor);
523 printf(" %5d %5d %5d %5d ",
524 disk.number, disk.major, disk.minor, disk.raid_disk);
526 if (!brief && array.raid_disks) {
528 if (disk.state & (1<<MD_DISK_FAULTY)) {
530 if (disk.raid_disk < array.raid_disks &&
534 if (disk.state & (1<<MD_DISK_ACTIVE)) printf(" active");
535 if (disk.state & (1<<MD_DISK_SYNC)) printf(" sync");
536 if (disk.state & (1<<MD_DISK_REMOVED)) printf(" removed");
537 if (disk.state & (1<<MD_DISK_WRITEMOSTLY)) printf(" writemostly");
539 ((1<<MD_DISK_ACTIVE)|(1<<MD_DISK_SYNC)|(1<<MD_DISK_REMOVED)))
543 if (disk.raid_disk < array.raid_disks && disk.raid_disk >= 0)
544 printf(" rebuilding");
545 } else if (is_rebuilding && failed) {
546 /* Taking a bit of a risk here, we remove the
547 * device from the array, and then put it back.
548 * If this fails, we are rebuilding
550 int err = ioctl(fd, HOT_REMOVE_DISK, makedev(disk.major, disk.minor));
551 if (err == 0) ioctl(fd, HOT_ADD_DISK, makedev(disk.major, disk.minor));
552 if (err && errno == EBUSY)
553 printf(" rebuilding");
557 if (disk.state == 0) spares++;
558 if (test && d < array.raid_disks
559 && !(disk.state & (1<<MD_DISK_SYNC)))
561 if ((dv=map_dev(disk.major, disk.minor, 0))) {
564 devices = realloc(devices,
565 strlen(devices)+1+strlen(dv)+1);
566 strcat(strcat(devices,","),dv);
568 devices = strdup(dv);
572 if (!brief) printf("\n");
574 if (spares && brief && array.raid_disks) printf(" spares=%d", spares);
575 if (brief && st && st->sb)
576 st->ss->brief_detail_super(st);
577 st->ss->free_super(st);
579 if (brief > 1 && devices) printf("\n devices=%s", devices);
580 if (brief) printf("\n");
582 !enough(array.level, array.raid_disks, array.layout,
583 1, avail, avail_disks))
594 int Detail_Platform(struct superswitch *ss, int scan, int verbose)
596 /* display platform capabilities for the given metadata format
597 * 'scan' in this context means iterate over all metadata types
602 if (ss && ss->detail_platform)
603 err = ss->detail_platform(verbose, 0);
606 fprintf(stderr, Name ": %s metadata is platform independent\n",
607 ss->name ? : "[no name]");
610 fprintf(stderr, Name ": specify a metadata type or --scan\n");
616 for (i = 0; superlist[i]; i++) {
617 struct superswitch *meta = superlist[i];
622 fprintf(stderr, Name ": checking metadata %s\n",
623 meta->name ? : "[no name]");
624 if (!meta->detail_platform) {
626 fprintf(stderr, Name ": %s metadata is platform independent\n",
627 meta->name ? : "[no name]");
629 err |= meta->detail_platform(verbose, 0);