]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mdadm.c
e1a869ebad540bfe3584d4c2fb298e978ebb3343
[thirdparty/mdadm.git] / mdadm.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
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@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
28 */
29
30 #include "mdadm.h"
31 #include "md_p.h"
32
33 int open_mddev(char *dev)
34 {
35 int mdfd = open(dev, O_RDWR, 0);
36 if (mdfd < 0)
37 fprintf(stderr,Name ": error opening %s: %s\n",
38 dev, strerror(errno));
39 else if (md_get_version(mdfd) <= 0) {
40 fprintf(stderr, Name ": %s does not appear to be an md device\n",
41 dev);
42 close(mdfd);
43 mdfd = -1;
44 }
45 return mdfd;
46 }
47
48
49
50 int main(int argc, char *argv[])
51 {
52 char mode = '\0';
53 int opt;
54 char *help_text;
55 char *c;
56 int rv;
57 int i;
58
59 int chunk = 0;
60 int size = 0;
61 int level = -10;
62 int layout = -1;
63 int raiddisks = 0;
64 int sparedisks = 0;
65 struct mddev_ident_s ident;
66 char *configfile = NULL;
67 char *cp;
68 int scan = 0;
69 char devmode = 0;
70 int runstop = 0;
71 int readonly = 0;
72 mddev_dev_t devlist = NULL;
73 mddev_dev_t *devlistend = & devlist;
74 mddev_dev_t dv;
75 int devs_found = 0;
76 int verbose = 0;
77 int brief = 0;
78 int force = 0;
79
80 char *mailaddr = NULL;
81 char *program = NULL;
82 int delay = 0;
83
84 int mdfd = -1;
85
86 ident.uuid_set=0;
87 ident.level = -10;
88 ident.raid_disks = -1;
89 ident.super_minor= -1;
90 ident.devices=0;
91
92 while ((opt=getopt_long(argc, argv,
93 short_options, long_options,
94 NULL)) != -1) {
95
96 switch(opt) {
97 case '@': /* just incase they say --manage */
98 case 'A':
99 case 'B':
100 case 'C':
101 case 'D':
102 case 'E':
103 case 'F':
104 case 'H':
105 /* setting mode - only once */
106 if (mode) {
107 fprintf(stderr, Name ": --%s/-%c not allowed, mode already set to %s\n",
108 long_options[opt-'A'+1].name,
109 long_options[opt-'A'+1].val,
110 long_options[mode-'A'+1].name);
111 exit(2);
112 }
113 mode = opt;
114 continue;
115
116 case 'h':
117 help_text = Help;
118 switch (mode) {
119 case 'C': help_text = Help_create; break;
120 case 'B': help_text = Help_build; break;
121 case 'A': help_text = Help_assemble; break;
122 }
123 fputs(help_text,stderr);
124 exit(0);
125
126 case 'V':
127 fputs(Version, stderr);
128 exit(0);
129
130 case 'v': verbose = 1;
131 continue;
132
133 case 'b': brief = 1;
134 continue;
135
136 case 1: /* an undecorated option - must be a device name.
137 * Depending on mode, it could be that:
138 * All devices listed are "md" devices : --Detail, -As
139 * No devices are "md" devices : --Examine
140 * First device is "md", others are component: -A,-B,-C
141 * Only accept on device before mode is determined.
142 * If mode is @, then require devmode for other devices.
143 */
144 if (devs_found > 0 && !mode ) {
145 fprintf(stderr, Name ": Must give mode flag before second device name at %s\n", optarg);
146 exit(2);
147 }
148 if (devs_found > 0 && mode == '@' && !devmode) {
149 fprintf(stderr, Name ": Must give on of -a/-r/-f for subsequent devices at %s\n", optarg);
150 exit(2);
151 }
152 dv = malloc(sizeof(*dv));
153 if (dv == NULL) {
154 fprintf(stderr, Name ": malloc failed\n");
155 exit(3);
156 }
157 dv->devname = optarg;
158 dv->disposition = devmode;
159 dv->next = NULL;
160 *devlistend = dv;
161 devlistend = &dv->next;
162
163 devs_found++;
164 continue;
165
166 case ':':
167 case '?':
168 fputs(Usage, stderr);
169 exit(2);
170 default:
171 /* force mode setting - @==manage if nothing else */
172 if (!mode) mode = '@';
173 }
174
175 /* We've got a mode, and opt is now something else which
176 * could depend on the mode */
177 #define O(a,b) ((a<<8)|b)
178 switch (O(mode,opt)) {
179 case O('C','c'):
180 case O('B','c'): /* chunk or rounding */
181 if (chunk) {
182 fprintf(stderr, Name ": chunk/rounding may only be specified once. "
183 "Second value is %s.\n", optarg);
184 exit(2);
185 }
186 chunk = strtol(optarg, &c, 10);
187 if (!optarg[0] || *c || chunk<4 || ((chunk-1)&chunk)) {
188 fprintf(stderr, Name ": invalid chunk/rounding value: %s\n",
189 optarg);
190 exit(2);
191 }
192 continue;
193
194 case O('C','z'): /* size */
195 if (size) {
196 fprintf(stderr, Name ": size may only be specified once. "
197 "Second value is %s.\n", optarg);
198 exit(2);
199 }
200 size = strtol(optarg, &c, 10);
201 if (!optarg[0] || *c || size < 4) {
202 fprintf(stderr, Name ": invalid size: %s\n",
203 optarg);
204 exit(2);
205 }
206 continue;
207
208 case O('C','l'):
209 case O('B','l'): /* set raid level*/
210 if (level != -10) {
211 fprintf(stderr, Name ": raid level may only be set once. "
212 "Second value is %s.\n", optarg);
213 exit(2);
214 }
215 level = map_name(pers, optarg);
216 if (level == -10) {
217 fprintf(stderr, Name ": invalid raid level: %s\n",
218 optarg);
219 exit(2);
220 }
221 if (level > 0 && mode == 'B') {
222 fprintf(stderr, Name ": Raid level %s not permitted with --build.\n",
223 optarg);
224 exit(2);
225 }
226 if (sparedisks > 0 && level < 1) {
227 fprintf(stderr, Name ": raid level %s is incompatible with spare-disks setting.\n",
228 optarg);
229 exit(2);
230 }
231 ident.level = level;
232 continue;
233
234 case O('C','p'): /* raid5 layout */
235 if (layout >= 0) {
236 fprintf(stderr,Name ": layout may only be sent once. "
237 "Second value was %s\n", optarg);
238 exit(2);
239 }
240 switch(level) {
241 default:
242 fprintf(stderr, Name ": layout not meaningful for %s arrays.\n",
243 map_num(pers, level));
244 exit(2);
245 case -10:
246 fprintf(stderr, Name ": raid level must be given before layout.\n");
247 exit(2);
248
249 case 5:
250 layout = map_name(r5layout, optarg);
251 if (layout==-10) {
252 fprintf(stderr, Name ": layout %s not understood for raid5.\n",
253 optarg);
254 exit(2);
255 }
256 break;
257 }
258 continue;
259
260 case O('C','n'):
261 case O('B','n'): /* number of raid disks */
262 if (raiddisks) {
263 fprintf(stderr, Name ": raid-disks set twice: %d and %s\n",
264 raiddisks, optarg);
265 exit(2);
266 }
267 raiddisks = strtol(optarg, &c, 10);
268 if (!optarg[0] || *c || raiddisks<=0 || raiddisks > MD_SB_DISKS) {
269 fprintf(stderr, Name ": invalid number of raid disks: %s\n",
270 optarg);
271 exit(2);
272 }
273 ident.raid_disks = raiddisks;
274 continue;
275
276 case O('C','x'): /* number of spare (eXtra) discs */
277 if (sparedisks) {
278 fprintf(stderr,Name ": spare-disks set twice: %d and %s\n",
279 sparedisks, optarg);
280 exit(2);
281 }
282 if (level > -10 && level < 1) {
283 fprintf(stderr, Name ": spare-disks setting is incompatible with raid level %d\n",
284 level);
285 exit(2);
286 }
287 sparedisks = strtol(optarg, &c, 10);
288 if (!optarg[0] || *c || sparedisks < 0 || sparedisks > MD_SB_DISKS - raiddisks) {
289 fprintf(stderr, Name ": invalid number of spare disks: %s\n",
290 optarg);
291 exit(2);
292 }
293 continue;
294 case O('C','f'): /* force honouring of device list */
295 case O('A','f'): /* force assembly */
296 case O('H','f'): /* force zero */
297 force=1;
298 continue;
299
300 /* now for the Assemble options */
301 case O('A','u'): /* uuid of array */
302 if (ident.uuid_set) {
303 fprintf(stderr, Name ": uuid cannot be set twice. "
304 "Second value %s.\n", optarg);
305 exit(2);
306 }
307 if (parse_uuid(optarg, ident.uuid))
308 ident.uuid_set = 1;
309 else {
310 fprintf(stderr,Name ": Bad uuid: %s\n", optarg);
311 exit(2);
312 }
313 continue;
314
315 case O('A','m'): /* super-minor for array */
316 if (ident.super_minor >= 0) {
317 fprintf(stderr, Name ": super-minor cannot be set twice. "
318 "Second value: %s.\n", optarg);
319 exit(2);
320 }
321 ident.super_minor = strtoul(optarg, &cp, 10);
322 if (!optarg[0] || *cp) {
323 fprintf(stderr, Name ": Bad super-minor number: %s.\n", optarg);
324 exit(2);
325 }
326 continue;
327
328 case O('A','c'): /* config file */
329 case O('F','c'):
330 if (configfile) {
331 fprintf(stderr, Name ": configfile cannot be set twice. "
332 "Second value is %s.\n", optarg);
333 exit(2);
334 }
335 configfile = optarg;
336 /* FIXME possibly check that config file exists. Even parse it */
337 continue;
338 case O('A','s'): /* scan */
339 case O('E','s'):
340 scan = 1;
341 continue;
342
343 case O('F','m'): /* mail address */
344 if (mailaddr)
345 fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n",
346 optarg);
347 else
348 mailaddr = optarg;
349 continue;
350
351 case O('F','p'): /* alert program */
352 if (program)
353 fprintf(stderr, Name ": only specify one alter program. %s ignored.\n",
354 optarg);
355 else
356 program = optarg;
357 continue;
358
359 case O('F','d'): /* delay in seconds */
360 if (delay)
361 fprintf(stderr, Name ": only specify delay once. %s ignored.\n",
362 optarg);
363 else {
364 delay = strtol(optarg, &c, 10);
365 if (!optarg[0] || *c || delay<1) {
366 fprintf(stderr, Name ": invalid delay: %s\n",
367 optarg);
368 exit(2);
369 }
370 }
371 continue;
372
373
374 /* now the general management options. Some are applicable
375 * to other modes. None have arguments.
376 */
377 case O('@','a'):
378 case O('C','a'):
379 case O('B','a'):
380 case O('A','a'): /* add a drive */
381 devmode = 'a';
382 continue;
383 case O('@','r'): /* remove a drive */
384 devmode = 'r';
385 continue;
386 case O('@','f'): /* set faulty */
387 devmode = 'f';
388 continue;
389 case O('@','R'):
390 case O('A','R'):
391 case O('B','R'):
392 case O('C','R'): /* Run the array */
393 if (runstop < 0) {
394 fprintf(stderr, Name ": Cannot both Stop and Run an array\n");
395 exit(2);
396 }
397 runstop = 1;
398 continue;
399 case O('@','S'):
400 if (runstop > 0) {
401 fprintf(stderr, Name ": Cannot both Run and Stop an array\n");
402 exit(2);
403 }
404 runstop = -1;
405 continue;
406
407 case O('@','o'):
408 if (readonly < 0) {
409 fprintf(stderr, Name ": Cannot have both readonly and readwrite\n");
410 exit(2);
411 }
412 readonly = 1;
413 continue;
414 case O('@','w'):
415 if (readonly > 0) {
416 fprintf(stderr, "mkdctl: Cannot have both readwrite and readonly.\n");
417 exit(2);
418 }
419 readonly = -1;
420 continue;
421 }
422 /* We have now processed all the valid options. Anything else is
423 * an error
424 */
425 fprintf(stderr, Name ": option %c not valid in mode %c\n",
426 opt, mode);
427 exit(2);
428
429 }
430
431 if (!mode) {
432 fputs(Usage, stderr);
433 exit(2);
434 }
435 /* Ok, got the option parsing out of the way
436 * hopefully it's mostly right but there might be some stuff
437 * missing
438 *
439 * That is mosty checked in ther per-mode stuff but...
440 *
441 * For @,B,C and A without -s, the first device listed must be an md device
442 * we check that here and open it.
443 */
444
445 if (mode=='@' || mode == 'B' || mode == 'C' || (mode == 'A' && ! scan)) {
446 if (devs_found < 1) {
447 fprintf(stderr, Name ": an md device must be given in this mode\n");
448 exit(2);
449 }
450 mdfd = open_mddev(devlist->devname);
451 if (mdfd < 0)
452 exit(1);
453 }
454
455
456 rv = 0;
457 switch(mode) {
458 case '@':/* Management */
459 /* readonly, add/remove, readwrite, runstop */
460 if (readonly>0)
461 rv = Manage_ro(devlist->devname, mdfd, readonly);
462 if (!rv && devs_found>1)
463 rv = Manage_subdevs(devlist->devname, mdfd,
464 devlist->next);
465 if (!rv && readonly < 0)
466 rv = Manage_ro(devlist->devname, mdfd, readonly);
467 if (!rv && runstop)
468 rv = Manage_runstop(devlist->devname, mdfd, runstop);
469 break;
470 case 'A': /* Assemble */
471 if (!scan)
472 rv = Assemble(devlist->devname, mdfd, &ident, configfile,
473 devlist->next,
474 readonly, runstop, verbose, force);
475 else if (devs_found>0)
476 for (dv = devlist ; dv ; dv=dv->next) {
477 mddev_ident_t array_ident = conf_get_ident(configfile, dv->devname);
478 mdfd = open_mddev(dv->devname);
479 if (mdfd < 0) {
480 rv |= 1;
481 continue;
482 }
483 if (array_ident == NULL) {
484 fprintf(stderr, Name ": %s not identified in config file.\n",
485 dv->devname);
486 rv |= 1;
487 continue;
488 }
489 rv |= Assemble(dv->devname, mdfd, array_ident, configfile,
490 NULL,
491 readonly, runstop, verbose, force);
492 }
493 else {
494 mddev_ident_t array_list = conf_get_ident(configfile, NULL);
495 if (!array_list) {
496 fprintf(stderr, Name ": No arrays found in config file\n");
497 rv = 1;
498 } else
499 for (; array_list; array_list = array_list->next) {
500 mdu_array_info_t array;
501 mdfd = open_mddev(array_list->devname);
502 if (mdfd < 0) {
503 rv |= 1;
504 continue;
505 }
506 if (ioctl(mdfd, GET_ARRAY_INFO, &array)>=0)
507 /* already assembled, skip */
508 continue;
509 rv |= Assemble(array_list->devname, mdfd,
510 array_list, configfile,
511 NULL,
512 readonly, runstop, verbose, force);
513 }
514 }
515 break;
516 case 'B': /* Build */
517 rv = Build(devlist->devname, mdfd, chunk, level, raiddisks, devlist->next);
518 break;
519 case 'C': /* Create */
520 rv = Create(devlist->devname, mdfd, chunk, level, layout, size,
521 raiddisks, sparedisks,
522 devs_found-1, devlist->next, runstop, verbose, force);
523 break;
524 case 'D': /* Detail */
525 for (dv=devlist ; dv; dv=dv->next)
526 rv |= Detail(dv->devname, brief);
527 break;
528 case 'E': /* Examine */
529 if (devlist == NULL && scan==0) {
530 fprintf(stderr, Name ": No devices to examine\n");
531 exit(2);
532 }
533 rv = Examine(devlist, devlist?brief:!verbose, configfile);
534 break;
535 case 'F': /* Follow */
536 rv= Monitor(devlist, mailaddr, program,
537 delay?delay:60, configfile);
538 break;
539 case 'H': /* Zero superblock */
540 for (dv=devlist ; dv; dv=dv->next)
541 rv |= Kill(dv->devname, force);
542 break;
543 }
544 exit(rv);
545 }