Detail: export MD_UUID from mapfile
[thirdparty/mdadm.git] / Detail.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        "md_p.h"
27 #include        "md_u.h"
28 #include        <dirent.h>
29
30 int Detail(char *dev, int brief, int export, int test, char *homehost)
31 {
32         /*
33          * Print out details for an md array by using
34          * GET_ARRAY_INFO and GET_DISK_INFO ioctl calls
35          */
36
37         int fd = open(dev, O_RDONLY);
38         int vers;
39         mdu_array_info_t array;
40         mdu_disk_info_t *disks;
41         int next;
42         int d;
43         time_t atime;
44         char *c;
45         char *devices = NULL;
46         int spares = 0;
47         struct stat stb;
48         int is_26 = get_linux_version() >= 2006000;
49         int is_rebuilding = 0;
50         int failed = 0;
51         struct supertype *st = NULL;
52         int max_disks = MD_SB_DISKS; /* just a default */
53         struct mdinfo info;
54         struct mdinfo *sra;
55         char *member = NULL;
56         char *container = NULL;
57
58         int rv = test ? 4 : 1;
59         int avail_disks = 0;
60         char *avail;
61
62         if (fd < 0) {
63                 fprintf(stderr, Name ": cannot open %s: %s\n",
64                         dev, strerror(errno));
65                 return rv;
66         }
67         vers = md_get_version(fd);
68         if (vers < 0) {
69                 fprintf(stderr, Name ": %s does not appear to be an md device\n",
70                         dev);
71                 close(fd);
72                 return rv;
73         }
74         if (vers < 9000) {
75                 fprintf(stderr, Name ": cannot get detail for md device %s: driver version too old.\n",
76                         dev);
77                 close(fd);
78                 return rv;
79         }
80         if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
81                 if (errno == ENODEV)
82                         fprintf(stderr, Name ": md device %s does not appear to be active.\n",
83                                 dev);
84                 else
85                         fprintf(stderr, Name ": cannot get array detail for %s: %s\n",
86                                 dev, strerror(errno));
87                 close(fd);
88                 return rv;
89         }
90         sra = sysfs_read(fd, 0, GET_VERSION);
91         st = super_by_fd(fd);
92
93         if (fstat(fd, &stb) != 0 && !S_ISBLK(stb.st_mode))
94                 stb.st_rdev = 0;
95         rv = 0;
96
97         if (st)
98                 max_disks = st->max_devs;
99
100         if (sra && is_subarray(sra->text_version) &&
101                 strchr(sra->text_version+1, '/')) {
102                 /* This is a subarray of some container.
103                  * We want the name of the container, and the member
104                  */
105                 char *s = strchr(sra->text_version+1, '/');
106                 int dn;
107                 *s++ = '\0';
108                 member = s;
109                 dn = devname2devnum(sra->text_version+1);
110                 container = map_dev(dev2major(dn), dev2minor(dn), 1);
111         }
112
113         /* try to load a superblock */
114         for (d= 0; d<max_disks; d++) {
115                 mdu_disk_info_t disk;
116                 char *dv;
117                 disk.number = d;
118                 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
119                         continue;
120                 if (d >= array.raid_disks &&
121                     disk.major == 0 &&
122                     disk.minor == 0)
123                         continue;
124                 if ((dv=map_dev(disk.major, disk.minor, 1))) {
125                         /* some formats (imsm) have free-floating-spares
126                          * with a uuid of uuid_match_any, they don't
127                          * have very good info about the rest of the
128                          * container, so keep searching when
129                          * encountering such a device.  Otherwise, stop
130                          * after the first successful call to
131                          * ->load_super.
132                          */
133                         int free_spare = memcmp(uuid_match_any,
134                                                 info.uuid,
135                                                 sizeof(uuid_match_any)) == 0;
136                         if ((!st || !st->sb || free_spare) &&
137                             (array.raid_disks == 0 || 
138                              (disk.state & (1<<MD_DISK_ACTIVE)))) {
139                                 /* try to read the superblock from this device
140                                  * to get more info
141                                  */
142                                 if (free_spare)
143                                         st->ss->free_super(st);
144                                 int fd2 = dev_open(dv, O_RDONLY);
145                                 if (fd2 >=0 && st &&
146                                     st->ss->load_super(st, fd2, NULL) == 0) {
147                                         st->ss->getinfo_super(st, &info);
148                                         if (array.raid_disks != 0 && /* container */
149                                             (info.array.ctime != array.ctime ||
150                                              info.array.level != array.level))
151                                                 st->ss->free_super(st);
152                                 }
153                                 if (fd2 >= 0) close(fd2);
154                         }
155                 }
156         }
157
158         /* Ok, we have some info to print... */
159         c = map_num(pers, array.level);
160
161         if (export) {
162                 if (array.raid_disks) {
163                         if (c)
164                                 printf("MD_LEVEL=%s\n", c);
165                         printf("MD_DEVICES=%d\n", array.raid_disks);
166                 } else {
167                         printf("MD_LEVEL=container\n");
168                         printf("MD_DEVICES=%d\n", array.nr_disks);
169                 }
170                 if (container) {
171                         printf("MD_CONTAINER=%s\n", container);
172                         printf("MD_MEMBER=%s\n", member);
173                 } else {
174                         if (sra && sra->array.major_version < 0)
175                                 printf("MD_METADATA=%s\n", sra->text_version);
176                         else
177                                 printf("MD_METADATA=%d.%02d\n",
178                                        array.major_version, array.minor_version);
179                 }
180                 
181                 if (st && st->sb) {
182                         struct mdinfo info;
183                         char nbuf[64];
184                         struct map_ent *mp, *map = NULL;
185                         st->ss->getinfo_super(st, &info);
186                         fname_from_uuid(st, &info, nbuf, ':');
187                         printf("MD_UUID=%s\n", nbuf+5);
188                         mp = map_by_uuid(&map, info.uuid);
189                         if (mp && mp->path &&
190                             strncmp(mp->path, "/dev/md/", 8) == 0)
191                                 printf("MD_DEVNAME=%s\n", mp->path+8);
192
193                         if (st->ss->export_detail_super)
194                                 st->ss->export_detail_super(st);
195                 } else {
196                         struct map_ent *mp, *map = NULL;
197                         char nbuf[64];
198                         mp = map_by_devnum(&map, fd2devnum(fd));
199                         if (mp) {
200                                 __fname_from_uuid(mp->uuid, 0, nbuf, ':');
201                                 printf("MD_UUID=%s\n", nbuf+5);
202                         }
203                         if (mp && mp->path &&
204                             strncmp(mp->path, "/dev/md/", 8) == 0)
205                                 printf("MD_DEVNAME=%s\n", mp->path+8);
206                 }
207                 goto out;
208         }
209
210         if (brief) {
211                 mdu_bitmap_file_t bmf;
212                 printf("ARRAY %s", dev);
213                 if (brief > 1) {
214                         if (array.raid_disks)
215                                 printf("level=%s num-devices=%d",
216                                        c?c:"-unknown-",
217                                        array.raid_disks );
218                         else
219                                 printf("level=container num-devices=%d",
220                                        array.nr_disks);
221                 }
222                 if (container) {
223                         printf(" container=%s", container);
224                         printf(" member=%s", member);
225                 } else {
226                         if (sra && sra->array.major_version < 0)
227                                 printf(" metadata=%s", sra->text_version);
228                         else
229                                 printf(" metadata=%d.%02d",
230                                        array.major_version, array.minor_version);
231                 }
232
233                 /* Only try GET_BITMAP_FILE for 0.90.01 and later */
234                 if (vers >= 9001 &&
235                     ioctl(fd, GET_BITMAP_FILE, &bmf) == 0 &&
236                     bmf.pathname[0]) {
237                         printf(" bitmap=%s", bmf.pathname);
238                 }
239         } else {
240                 mdu_bitmap_file_t bmf;
241                 unsigned long long larray_size;
242                 struct mdstat_ent *ms = mdstat_read(0, 0);
243                 struct mdstat_ent *e;
244                 int devnum = array.md_minor;
245                 if (major(stb.st_rdev) == get_mdp_major())
246                         devnum = -1 - devnum;
247
248                 for (e=ms; e; e=e->next)
249                         if (e->devnum == devnum)
250                                 break;
251                 if (!get_dev_size(fd, NULL, &larray_size))
252                         larray_size = 0;
253
254                 printf("%s:\n", dev);
255
256                 if (container)
257                         printf("      Container : %s, member %s\n", container, member);
258                 else {
259                 if (sra && sra->array.major_version < 0)
260                         printf("        Version : %s\n", sra->text_version);
261                 else
262                         printf("        Version : %d.%02d\n",
263                                array.major_version, array.minor_version);
264                 }
265
266                 atime = array.ctime;
267                 if (atime)
268                         printf("  Creation Time : %.24s\n", ctime(&atime));
269                 if (array.raid_disks == 0) c = "container";
270                 printf("     Raid Level : %s\n", c?c:"-unknown-");
271                 if (larray_size)
272                         printf("     Array Size : %llu%s\n", (larray_size>>10), human_size(larray_size));
273                 if (array.level >= 1) {
274                         if (array.major_version != 0 &&
275                             (larray_size >= 0xFFFFFFFFULL|| array.size == 0)) {
276                                 unsigned long long dsize = get_component_size(fd);
277                                 if (dsize > 0)
278                                         printf("  Used Dev Size : %llu%s\n",
279                                                dsize/2,
280                                          human_size((long long)dsize<<9));
281                                 else
282                                         printf("  Used Dev Size : unknown\n");
283                         } else
284                                 printf("  Used Dev Size : %d%s\n", array.size,
285                                        human_size((long long)array.size<<10));
286                 }
287                 if (array.raid_disks)
288                         printf("   Raid Devices : %d\n", array.raid_disks);
289                 printf("  Total Devices : %d\n", array.nr_disks);
290                 if (!container && 
291                     ((sra == NULL && array.major_version == 0) ||
292                      (sra && sra->array.major_version == 0)))
293                         printf("Preferred Minor : %d\n", array.md_minor);
294                 if (sra == NULL || sra->array.major_version >= 0)
295                         printf("    Persistence : Superblock is %spersistent\n",
296                                array.not_persistent?"not ":"");
297                 printf("\n");
298                 /* Only try GET_BITMAP_FILE for 0.90.01 and later */
299                 if (vers >= 9001 &&
300                     ioctl(fd, GET_BITMAP_FILE, &bmf) == 0 &&
301                     bmf.pathname[0]) {
302                         printf("  Intent Bitmap : %s\n", bmf.pathname);
303                         printf("\n");
304                 } else if (array.state & (1<<MD_SB_BITMAP_PRESENT))
305                         printf("  Intent Bitmap : Internal\n\n");
306                 atime = array.utime;
307                 if (atime)
308                         printf("    Update Time : %.24s\n", ctime(&atime));
309                 if (array.raid_disks)
310                         printf("          State : %s%s%s%s\n",
311                                (array.state&(1<<MD_SB_CLEAN))?"clean":"active",
312                                array.active_disks < array.raid_disks? ", degraded":"",
313                                (!e || e->percent < 0) ? "" :
314                                (e->resync) ? ", resyncing": ", recovering",
315                                larray_size ? "": ", Not Started");
316                 if (array.raid_disks)
317                         printf(" Active Devices : %d\n", array.active_disks);
318                 printf("Working Devices : %d\n", array.working_disks);
319                 if (array.raid_disks) {
320                         printf(" Failed Devices : %d\n", array.failed_disks);
321                         printf("  Spare Devices : %d\n", array.spare_disks);
322                 }
323                 printf("\n");
324                 if (array.level == 5) {
325                         c = map_num(r5layout, array.layout);
326                         printf("         Layout : %s\n", c?c:"-unknown-");
327                 }
328                 if (array.level == 10) {
329                         printf("         Layout :");
330                         print_r10_layout(array.layout);
331                         printf("\n");
332                 }
333                 switch (array.level) {
334                 case 0:
335                 case 4:
336                 case 5:
337                 case 10:
338                 case 6:
339                         if (array.chunk_size)
340                                 printf("     Chunk Size : %dK\n\n",
341                                        array.chunk_size/1024);
342                         break;
343                 case -1:
344                         printf("       Rounding : %dK\n\n", array.chunk_size/1024);
345                         break;
346                 default: break;
347                 }
348
349                 if (e && e->percent >= 0) {
350                         printf(" Re%s Status : %d%% complete\n",
351                                (st && st->sb && info.reshape_active)?
352                                   "shape":"build",
353                                e->percent);
354                         is_rebuilding = 1;
355                 }
356                 free_mdstat(ms);
357
358                 if (st->sb && info.reshape_active) {
359 #if 0
360 This is pretty boring
361                         printf("  Reshape pos'n : %llu%s\n", (unsigned long long) info.reshape_progress<<9,
362                                human_size(info.reshape_progress<<9));
363 #endif
364                         if (info.delta_disks > 0)
365                                 printf("  Delta Devices : %d, (%d->%d)\n",
366                                        info.delta_disks, array.raid_disks - info.delta_disks, array.raid_disks);
367                         if (info.delta_disks < 0)
368                                 printf("  Delta Devices : %d, (%d->%d)\n",
369                                        info.delta_disks, array.raid_disks, array.raid_disks + info.delta_disks);
370                         if (info.new_level != array.level) {
371                                 char *c = map_num(pers, info.new_level);
372                                 printf("      New Level : %s\n", c?c:"-unknown-");
373                         }
374                         if (info.new_level != array.level ||
375                             info.new_layout != array.layout) {
376                                 if (info.new_level == 5) {
377                                         char *c = map_num(r5layout, info.new_layout);
378                                         printf("     New Layout : %s\n",
379                                                c?c:"-unknown-");
380                                 }
381                                 if (info.new_level == 10) {
382                                         printf("     New Layout : near=%d, %s=%d\n",
383                                                info.new_layout&255,
384                                                (info.new_layout&0x10000)?"offset":"far",
385                                                (info.new_layout>>8)&255);
386                                 }
387                         }
388                         if (info.new_chunk != array.chunk_size)
389                                 printf("  New Chunksize : %dK\n", info.new_chunk/1024);
390                         printf("\n");
391                 } else if (e && e->percent >= 0)
392                         printf("\n");
393                 if (st && st->sb)
394                         st->ss->detail_super(st, homehost);
395
396                 if (array.raid_disks == 0 && sra && sra->array.major_version == -1
397                     && sra->array.minor_version == -2 && sra->text_version[0] != '/') {
398                         /* This looks like a container.  Find any active arrays
399                          * That claim to be a member.
400                          */
401                         DIR *dir = opendir("/sys/block");
402                         struct dirent *de;
403
404                         printf("  Member Arrays :");
405
406                         while (dir && (de = readdir(dir)) != NULL) {
407                                 char path[200];
408                                 char vbuf[1024];
409                                 int nlen = strlen(sra->sys_name);
410                                 int dn;
411                                 if (de->d_name[0] == '.')
412                                         continue;
413                                 sprintf(path, "/sys/block/%s/md/metadata_version",
414                                         de->d_name);
415                                 if (load_sys(path, vbuf) < 0)
416                                         continue;
417                                 if (strncmp(vbuf, "external:", 9) != 0 ||
418                                     !is_subarray(sra->sys_name+9) ||
419                                     strncmp(vbuf+10, sra->sys_name, nlen) != 0 ||
420                                     vbuf[10+nlen] != '/')
421                                         continue;
422                                 dn = devname2devnum(de->d_name);
423                                 printf(" %s", map_dev(dev2major(dn),
424                                                       dev2minor(dn), 1));
425                         }
426                         if (dir)
427                                 closedir(dir);
428                         printf("\n\n");
429                 }
430
431                 if (array.raid_disks)
432                         printf("    Number   Major   Minor   RaidDevice State\n");
433                 else
434                         printf("    Number   Major   Minor   RaidDevice\n");
435         }
436         disks = malloc(max_disks * sizeof(mdu_disk_info_t));
437         for (d=0; d<max_disks; d++) {
438                 disks[d].state = (1<<MD_DISK_REMOVED);
439                 disks[d].major = disks[d].minor = 0;
440                 disks[d].number = disks[d].raid_disk = d;
441         }
442
443         next = array.raid_disks;
444         for (d=0; d < max_disks; d++) {
445                 mdu_disk_info_t disk;
446                 disk.number = d;
447                 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
448                         if (d < array.raid_disks)
449                                 fprintf(stderr, Name ": cannot get device detail for device %d: %s\n",
450                                         d, strerror(errno));
451                         continue;
452                 }
453                 if (disk.major == 0 && disk.minor == 0)
454                         continue;
455                 if (disk.raid_disk >= 0 && disk.raid_disk < array.raid_disks)
456                         disks[disk.raid_disk] = disk;
457                 else if (next < max_disks)
458                         disks[next++] = disk;
459         }
460
461         avail = calloc(array.raid_disks, 1);
462         for (d= 0; d < max_disks; d++) {
463                 char *dv;
464                 mdu_disk_info_t disk = disks[d];
465
466                 if (d >= array.raid_disks &&
467                     disk.major == 0 &&
468                     disk.minor == 0)
469                         continue;
470                 if (!brief) {
471                         if (d == array.raid_disks) printf("\n");
472                         if (disk.raid_disk < 0)
473                                 printf("   %5d   %5d    %5d        -     ",
474                                        disk.number, disk.major, disk.minor);
475                         else
476                                 printf("   %5d   %5d    %5d    %5d     ",
477                                        disk.number, disk.major, disk.minor, disk.raid_disk);
478                 }
479                 if (!brief && array.raid_disks) {
480
481                         if (disk.state & (1<<MD_DISK_FAULTY)) {
482                                 printf(" faulty");
483                                 if (disk.raid_disk < array.raid_disks &&
484                                     disk.raid_disk >= 0)
485                                         failed++;
486                         }
487                         if (disk.state & (1<<MD_DISK_ACTIVE)) printf(" active");
488                         if (disk.state & (1<<MD_DISK_SYNC)) printf(" sync");
489                         if (disk.state & (1<<MD_DISK_REMOVED)) printf(" removed");
490                         if (disk.state & (1<<MD_DISK_WRITEMOSTLY)) printf(" writemostly");
491                         if ((disk.state &
492                              ((1<<MD_DISK_ACTIVE)|(1<<MD_DISK_SYNC)|(1<<MD_DISK_REMOVED)))
493                             == 0) {
494                                 printf(" spare");
495                                 if (is_26) {
496                                         if (disk.raid_disk < array.raid_disks && disk.raid_disk >= 0)
497                                                 printf(" rebuilding");
498                                 } else if (is_rebuilding && failed) {
499                                         /* Taking a bit of a risk here, we remove the
500                                          * device from the array, and then put it back.
501                                          * If this fails, we are rebuilding
502                                          */
503                                         int err = ioctl(fd, HOT_REMOVE_DISK, makedev(disk.major, disk.minor));
504                                         if (err == 0) ioctl(fd, HOT_ADD_DISK, makedev(disk.major, disk.minor));
505                                         if (err && errno ==  EBUSY)
506                                                 printf(" rebuilding");
507                                 }
508                         }
509                 }
510                 if (disk.state == 0) spares++;
511                 if (test && d < array.raid_disks
512                     && !(disk.state & (1<<MD_DISK_SYNC)))
513                         rv |= 1;
514                 if (d < array.raid_disks
515                     && (disk.state & (1<<MD_DISK_SYNC))) {
516                         avail_disks ++;
517                         avail[d] = 1;
518                 }
519                 if ((dv=map_dev(disk.major, disk.minor, 0))) {
520                         if (brief) {
521                                 if (devices) {
522                                         devices = realloc(devices,
523                                                           strlen(devices)+1+strlen(dv)+1);
524                                         strcat(strcat(devices,","),dv);
525                                 } else
526                                         devices = strdup(dv);
527                         } else
528                                 printf("   %s", dv);
529                 }
530                 if (!brief) printf("\n");
531         }
532         if (spares && brief && array.raid_disks) printf(" spares=%d", spares);
533         if (brief && st && st->sb)
534                 st->ss->brief_detail_super(st);
535         st->ss->free_super(st);
536
537         if (brief > 1 && devices) printf("\n   devices=%s", devices);
538         if (brief) printf("\n");
539         if (test &&
540             !enough(array.level, array.raid_disks, array.layout,
541                     1, avail, avail_disks))
542                 rv = 2;
543
544 out:
545         close(fd);
546         return rv;
547 }
548
549 int Detail_Platform(struct superswitch *ss, int scan, int verbose)
550 {
551         /* display platform capabilities for the given metadata format
552          * 'scan' in this context means iterate over all metadata types
553          */
554         int i;
555         int err = 1;
556
557         if (ss && ss->detail_platform)
558                 err = ss->detail_platform(verbose, 0);
559         else if (ss) {
560                 if (verbose)
561                         fprintf(stderr, Name ": %s metadata is platform independent\n",
562                                 ss->name ? : "[no name]");
563         } else if (!scan) {
564                 if (verbose)
565                         fprintf(stderr, Name ": specify a metadata type or --scan\n");
566         }
567
568         if (!scan)
569                 return err;
570
571         for (i = 0; superlist[i]; i++) {
572                 struct superswitch *meta = superlist[i];
573
574                 if (meta == ss)
575                         continue;
576                 if (verbose)
577                         fprintf(stderr, Name ": checking metadata %s\n",
578                                 meta->name ? : "[no name]");
579                 if (!meta->detail_platform) {
580                         if (verbose)
581                                 fprintf(stderr, Name ": %s metadata is platform independent\n",
582                                         meta->name ? : "[no name]");
583                 } else
584                         err |= meta->detail_platform(verbose, 0);
585         }
586
587         return err;
588 }