]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * mdctl - 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 "mdctl.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 | /* setting mode - only once */ | |
105 | if (mode) { | |
106 | fprintf(stderr, Name ": --%s/-%c not allowed, mode already set to %s\n", | |
107 | long_options[opt-'A'+1].name, | |
108 | long_options[opt-'A'+1].val, | |
109 | long_options[mode-'A'+1].name); | |
110 | exit(2); | |
111 | } | |
112 | mode = opt; | |
113 | continue; | |
114 | ||
115 | case 'h': | |
116 | help_text = Help; | |
117 | switch (mode) { | |
118 | case 'C': help_text = Help_create; break; | |
119 | case 'B': help_text = Help_build; break; | |
120 | case 'A': help_text = Help_assemble; break; | |
121 | } | |
122 | fputs(help_text,stderr); | |
123 | exit(0); | |
124 | ||
125 | case 'V': | |
126 | fputs(Version, stderr); | |
127 | exit(0); | |
128 | ||
129 | case 'v': verbose = 1; | |
130 | continue; | |
131 | ||
132 | case 'b': brief = 1; | |
133 | continue; | |
134 | ||
135 | case 1: /* an undecorated option - must be a device name. | |
136 | * Depending on mode, it could be that: | |
137 | * All devices listed are "md" devices : --Detail, -As | |
138 | * No devices are "md" devices : --Examine | |
139 | * First device is "md", others are component: -A,-B,-C | |
140 | * Only accept on device before mode is determined. | |
141 | * If mode is @, then require devmode for other devices. | |
142 | */ | |
143 | if (devs_found > 0 && !mode ) { | |
144 | fprintf(stderr, Name ": Must give mode flag before second device name at %s\n", optarg); | |
145 | exit(2); | |
146 | } | |
147 | if (devs_found > 0 && mode == '@' && !devmode) { | |
148 | fprintf(stderr, Name ": Must give on of -a/-r/-f for subsequent devices at %s\n", optarg); | |
149 | exit(2); | |
150 | } | |
151 | dv = malloc(sizeof(*dv)); | |
152 | if (dv == NULL) { | |
153 | fprintf(stderr, Name ": malloc failed\n"); | |
154 | exit(3); | |
155 | } | |
156 | dv->devname = optarg; | |
157 | dv->disposition = devmode; | |
158 | dv->next = NULL; | |
159 | *devlistend = dv; | |
160 | devlistend = &dv->next; | |
161 | ||
162 | devs_found++; | |
163 | continue; | |
164 | ||
165 | case ':': | |
166 | case '?': | |
167 | fputs(Usage, stderr); | |
168 | exit(2); | |
169 | default: | |
170 | /* force mode setting - @==manage if nothing else */ | |
171 | if (!mode) mode = '@'; | |
172 | } | |
173 | ||
174 | /* We've got a mode, and opt is now something else which | |
175 | * could depend on the mode */ | |
176 | #define O(a,b) ((a<<8)|b) | |
177 | switch (O(mode,opt)) { | |
178 | case O('C','c'): | |
179 | case O('B','c'): /* chunk or rounding */ | |
180 | if (chunk) { | |
181 | fprintf(stderr, Name ": chunk/rounding may only be specified once. " | |
182 | "Second value is %s.\n", optarg); | |
183 | exit(2); | |
184 | } | |
185 | chunk = strtol(optarg, &c, 10); | |
186 | if (!optarg[0] || *c || chunk<4 || ((chunk-1)&chunk)) { | |
187 | fprintf(stderr, Name ": invalid chunk/rounding value: %s\n", | |
188 | optarg); | |
189 | exit(2); | |
190 | } | |
191 | continue; | |
192 | ||
193 | case O('C','z'): /* size */ | |
194 | if (size) { | |
195 | fprintf(stderr, Name ": size may only be specified once. " | |
196 | "Second value is %s.\n", optarg); | |
197 | exit(2); | |
198 | } | |
199 | size = strtol(optarg, &c, 10); | |
200 | if (!optarg[0] || *c || size < 4) { | |
201 | fprintf(stderr, Name ": invalid size: %s\n", | |
202 | optarg); | |
203 | exit(2); | |
204 | } | |
205 | continue; | |
206 | ||
207 | case O('C','l'): | |
208 | case O('B','l'): /* set raid level*/ | |
209 | if (level != -10) { | |
210 | fprintf(stderr, Name ": raid level may only be set once. " | |
211 | "Second value is %s.\n", optarg); | |
212 | exit(2); | |
213 | } | |
214 | level = map_name(pers, optarg); | |
215 | if (level == -10) { | |
216 | fprintf(stderr, Name ": invalid raid level: %s\n", | |
217 | optarg); | |
218 | exit(2); | |
219 | } | |
220 | if (level > 0 && mode == 'B') { | |
221 | fprintf(stderr, Name ": Raid level %s not permitted with --build.\n", | |
222 | optarg); | |
223 | exit(2); | |
224 | } | |
225 | if (sparedisks > 0 && level < 1) { | |
226 | fprintf(stderr, Name ": raid level %s is incompatible with spare-disks setting.\n", | |
227 | optarg); | |
228 | exit(2); | |
229 | } | |
230 | ident.level = level; | |
231 | continue; | |
232 | ||
233 | case O('C','p'): /* raid5 layout */ | |
234 | if (layout >= 0) { | |
235 | fprintf(stderr,Name ": layout may only be sent once. " | |
236 | "Second value was %s\n", optarg); | |
237 | exit(2); | |
238 | } | |
239 | switch(level) { | |
240 | default: | |
241 | fprintf(stderr, Name ": layout now meaningful for %s arrays.\n", | |
242 | map_num(pers, level)); | |
243 | exit(2); | |
244 | case -10: | |
245 | fprintf(stderr, Name ": raid level must be given before layout.\n"); | |
246 | exit(2); | |
247 | ||
248 | case 5: | |
249 | layout = map_name(r5layout, optarg); | |
250 | if (layout==-10) { | |
251 | fprintf(stderr, Name ": layout %s not understood for raid5.\n", | |
252 | optarg); | |
253 | exit(2); | |
254 | } | |
255 | break; | |
256 | } | |
257 | continue; | |
258 | ||
259 | case O('C','n'): | |
260 | case O('B','n'): /* number of raid disks */ | |
261 | if (raiddisks) { | |
262 | fprintf(stderr, Name ": raid-disks set twice: %d and %s\n", | |
263 | raiddisks, optarg); | |
264 | exit(2); | |
265 | } | |
266 | raiddisks = strtol(optarg, &c, 10); | |
267 | if (!optarg[0] || *c || raiddisks<=0 || raiddisks > MD_SB_DISKS) { | |
268 | fprintf(stderr, Name ": invalid number of raid disks: %s\n", | |
269 | optarg); | |
270 | exit(2); | |
271 | } | |
272 | ident.raid_disks = raiddisks; | |
273 | continue; | |
274 | ||
275 | case O('C','x'): /* number of spare (eXtra) discs */ | |
276 | if (sparedisks) { | |
277 | fprintf(stderr,Name ": spare-disks set twice: %d and %s\n", | |
278 | sparedisks, optarg); | |
279 | exit(2); | |
280 | } | |
281 | if (level > -10 && level < 1) { | |
282 | fprintf(stderr, Name ": spare-disks setting is incompatible with raid level %d\n", | |
283 | level); | |
284 | exit(2); | |
285 | } | |
286 | sparedisks = strtol(optarg, &c, 10); | |
287 | if (!optarg[0] || *c || sparedisks < 0 || sparedisks > MD_SB_DISKS - raiddisks) { | |
288 | fprintf(stderr, Name ": invalid number of spare disks: %s\n", | |
289 | optarg); | |
290 | exit(2); | |
291 | } | |
292 | continue; | |
293 | case O('C','f'): /* force honouring of device list */ | |
294 | force=1; | |
295 | continue; | |
296 | ||
297 | /* now for the Assemble options */ | |
298 | case O('A','f'): /* force assembly */ | |
299 | force = 1; | |
300 | continue; | |
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 | } | |
539 | exit(rv); | |
540 | } |