]>
Commit | Line | Data |
---|---|---|
64c4757e | 1 | /* |
9a9dab36 | 2 | * mdadm - manage Linux "md" devices aka RAID arrays. |
64c4757e | 3 | * |
cd29a5c8 | 4 | * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au> |
64c4757e NB |
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 | ||
9a9dab36 | 30 | #include "mdadm.h" |
64c4757e NB |
31 | #include "md_p.h" |
32 | ||
52826846 | 33 | int open_mddev(char *dev) |
64c4757e | 34 | { |
52826846 NB |
35 | int mdfd = open(dev, O_RDWR, 0); |
36 | if (mdfd < 0) | |
e0d19036 | 37 | fprintf(stderr, Name ": error opening %s: %s\n", |
52826846 NB |
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; | |
64c4757e | 44 | } |
52826846 NB |
45 | return mdfd; |
46 | } | |
64c4757e | 47 | |
64c4757e | 48 | |
52826846 NB |
49 | |
50 | int main(int argc, char *argv[]) | |
51 | { | |
e0d19036 | 52 | int mode = 0; |
52826846 | 53 | int opt; |
e0d19036 | 54 | int option_index; |
52826846 NB |
55 | char *help_text; |
56 | char *c; | |
57 | int rv; | |
52826846 NB |
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; | |
cd29a5c8 | 67 | char *cp; |
5787fa49 | 68 | char *update = NULL; |
52826846 NB |
69 | int scan = 0; |
70 | char devmode = 0; | |
71 | int runstop = 0; | |
72 | int readonly = 0; | |
bd526cee | 73 | int SparcAdjust = 0; |
cd29a5c8 NB |
74 | mddev_dev_t devlist = NULL; |
75 | mddev_dev_t *devlistend = & devlist; | |
76 | mddev_dev_t dv; | |
52826846 NB |
77 | int devs_found = 0; |
78 | int verbose = 0; | |
cd29a5c8 | 79 | int brief = 0; |
52826846 NB |
80 | int force = 0; |
81 | ||
82 | char *mailaddr = NULL; | |
83 | char *program = NULL; | |
84 | int delay = 0; | |
d013a55e | 85 | int daemonise = 0; |
52826846 NB |
86 | |
87 | int mdfd = -1; | |
88 | ||
89 | ident.uuid_set=0; | |
cd29a5c8 NB |
90 | ident.level = -10; |
91 | ident.raid_disks = -1; | |
52826846 NB |
92 | ident.super_minor= -1; |
93 | ident.devices=0; | |
94 | ||
e0d19036 NB |
95 | while ((option_index = -1) , |
96 | (opt=getopt_long(argc, argv, | |
52826846 | 97 | short_options, long_options, |
e0d19036 NB |
98 | &option_index)) != -1) { |
99 | int newmode = mode; | |
100 | /* firstly, so mode-independant options */ | |
52826846 | 101 | switch(opt) { |
52826846 NB |
102 | case 'h': |
103 | help_text = Help; | |
104 | switch (mode) { | |
e0d19036 NB |
105 | case ASSEMBLE : help_text = Help_assemble; break; |
106 | case BUILD : help_text = Help_build; break; | |
107 | case CREATE : help_text = Help_create; break; | |
108 | case MANAGE : help_text = Help_manage; break; | |
109 | case MISC : help_text = Help_misc; break; | |
110 | case MONITOR : help_text = Help_monitor; break; | |
52826846 NB |
111 | } |
112 | fputs(help_text,stderr); | |
113 | exit(0); | |
114 | ||
115 | case 'V': | |
116 | fputs(Version, stderr); | |
117 | exit(0); | |
118 | ||
119 | case 'v': verbose = 1; | |
120 | continue; | |
121 | ||
cd29a5c8 NB |
122 | case 'b': brief = 1; |
123 | continue; | |
124 | ||
e0d19036 NB |
125 | case ':': |
126 | case '?': | |
127 | fputs(Usage, stderr); | |
128 | exit(2); | |
129 | } | |
130 | /* second, figure out the mode. | |
131 | * Some options force the mode. Others | |
132 | * set the mode if it isn't already | |
133 | */ | |
134 | ||
135 | switch(opt) { | |
136 | case '@': /* just incase they say --manage */ | |
137 | newmode = MANAGE; break; | |
138 | case 'a': | |
139 | case 'r': | |
140 | case 'f': | |
141 | case 1 : if (!mode) newmode = MANAGE; break; | |
142 | ||
143 | case 'A': newmode = ASSEMBLE; break; | |
144 | case 'B': newmode = BUILD; break; | |
145 | case 'C': newmode = CREATE; break; | |
146 | case 'F': newmode = MONITOR;break; | |
147 | ||
148 | case '#': | |
149 | case 'D': | |
150 | case 'E': | |
151 | case 'Q': newmode = MISC; break; | |
152 | case 'R': | |
153 | case 'S': | |
154 | case 'o': | |
155 | case 'w': | |
156 | case 'K': if (!mode) newmode = MISC; break; | |
157 | } | |
158 | if (mode && newmode == mode) { | |
159 | /* everybody happy ! */ | |
160 | } else if (mode && newmode != mode) { | |
161 | /* not allowed.. */ | |
162 | fprintf(stderr, Name ": "); | |
163 | if (option_index >= 0) | |
164 | fprintf(stderr, "--%s", long_options[option_index].name); | |
165 | else | |
166 | fprintf(stderr, "-%c", opt); | |
167 | fprintf(stderr, " would set mode to %s, but it is already %s.\n", | |
168 | map_num(modes, newmode), | |
169 | map_num(modes, mode)); | |
170 | exit(2); | |
171 | } else if (!mode && newmode) { | |
172 | mode = newmode; | |
173 | } else { | |
174 | /* special case of -c --help */ | |
175 | if (opt == 'c' && | |
176 | ( strncmp(optarg, "--h", 3)==0 || | |
177 | strncmp(optarg, "-h", 2)==0)) { | |
178 | fputs(Help_config, stderr); | |
179 | exit(0); | |
cd29a5c8 | 180 | } |
e0d19036 NB |
181 | if (option_index >= 0) |
182 | fprintf(stderr, "--%s", long_options[option_index].name); | |
183 | else | |
184 | fprintf(stderr, "-%c", opt); | |
185 | fprintf(stderr, " does not set the mode, and so cannot be first.\n"); | |
186 | exit(2); | |
187 | } | |
188 | ||
189 | /* if we just set the mode, then done */ | |
190 | switch(opt) { | |
191 | case '@': | |
192 | case '#': | |
193 | case 'A': | |
194 | case 'B': | |
195 | case 'C': | |
196 | case 'F': | |
197 | continue; | |
198 | } | |
199 | if (opt == 1) { | |
200 | /* an undecorated option - must be a device name. | |
201 | */ | |
cd29a5c8 NB |
202 | if (devs_found > 0 && mode == '@' && !devmode) { |
203 | fprintf(stderr, Name ": Must give on of -a/-r/-f for subsequent devices at %s\n", optarg); | |
52826846 NB |
204 | exit(2); |
205 | } | |
cd29a5c8 NB |
206 | dv = malloc(sizeof(*dv)); |
207 | if (dv == NULL) { | |
208 | fprintf(stderr, Name ": malloc failed\n"); | |
209 | exit(3); | |
210 | } | |
211 | dv->devname = optarg; | |
212 | dv->disposition = devmode; | |
213 | dv->next = NULL; | |
214 | *devlistend = dv; | |
215 | devlistend = &dv->next; | |
216 | ||
52826846 NB |
217 | devs_found++; |
218 | continue; | |
682c7051 | 219 | } |
682c7051 | 220 | |
52826846 NB |
221 | /* We've got a mode, and opt is now something else which |
222 | * could depend on the mode */ | |
223 | #define O(a,b) ((a<<8)|b) | |
224 | switch (O(mode,opt)) { | |
e0d19036 NB |
225 | case O(CREATE,'c'): |
226 | case O(BUILD,'c'): /* chunk or rounding */ | |
52826846 NB |
227 | if (chunk) { |
228 | fprintf(stderr, Name ": chunk/rounding may only be specified once. " | |
229 | "Second value is %s.\n", optarg); | |
230 | exit(2); | |
231 | } | |
232 | chunk = strtol(optarg, &c, 10); | |
233 | if (!optarg[0] || *c || chunk<4 || ((chunk-1)&chunk)) { | |
234 | fprintf(stderr, Name ": invalid chunk/rounding value: %s\n", | |
235 | optarg); | |
236 | exit(2); | |
237 | } | |
238 | continue; | |
64c4757e | 239 | |
e0d19036 | 240 | case O(CREATE,'z'): /* size */ |
52826846 NB |
241 | if (size) { |
242 | fprintf(stderr, Name ": size may only be specified once. " | |
243 | "Second value is %s.\n", optarg); | |
244 | exit(2); | |
245 | } | |
246 | size = strtol(optarg, &c, 10); | |
247 | if (!optarg[0] || *c || size < 4) { | |
248 | fprintf(stderr, Name ": invalid size: %s\n", | |
249 | optarg); | |
250 | exit(2); | |
251 | } | |
252 | continue; | |
64c4757e | 253 | |
e0d19036 NB |
254 | case O(CREATE,'l'): |
255 | case O(BUILD,'l'): /* set raid level*/ | |
52826846 NB |
256 | if (level != -10) { |
257 | fprintf(stderr, Name ": raid level may only be set once. " | |
258 | "Second value is %s.\n", optarg); | |
259 | exit(2); | |
260 | } | |
261 | level = map_name(pers, optarg); | |
262 | if (level == -10) { | |
263 | fprintf(stderr, Name ": invalid raid level: %s\n", | |
264 | optarg); | |
265 | exit(2); | |
266 | } | |
e0d19036 | 267 | if (level != 0 && level != -1 && mode == BUILD) { |
52826846 NB |
268 | fprintf(stderr, Name ": Raid level %s not permitted with --build.\n", |
269 | optarg); | |
270 | exit(2); | |
271 | } | |
e0d19036 | 272 | if (sparedisks > 0 && level < 1 && level >= -1) { |
b83d95f3 | 273 | fprintf(stderr, Name ": raid level %s is incompatible with spare-devices setting.\n", |
52826846 NB |
274 | optarg); |
275 | exit(2); | |
276 | } | |
cd29a5c8 | 277 | ident.level = level; |
52826846 | 278 | continue; |
64c4757e | 279 | |
e0d19036 | 280 | case O(CREATE,'p'): /* raid5 layout */ |
52826846 NB |
281 | if (layout >= 0) { |
282 | fprintf(stderr,Name ": layout may only be sent once. " | |
283 | "Second value was %s\n", optarg); | |
284 | exit(2); | |
285 | } | |
286 | switch(level) { | |
287 | default: | |
56eb10c0 | 288 | fprintf(stderr, Name ": layout not meaningful for %s arrays.\n", |
52826846 NB |
289 | map_num(pers, level)); |
290 | exit(2); | |
291 | case -10: | |
292 | fprintf(stderr, Name ": raid level must be given before layout.\n"); | |
293 | exit(2); | |
294 | ||
295 | case 5: | |
296 | layout = map_name(r5layout, optarg); | |
297 | if (layout==-10) { | |
298 | fprintf(stderr, Name ": layout %s not understood for raid5.\n", | |
299 | optarg); | |
300 | exit(2); | |
301 | } | |
302 | break; | |
303 | } | |
304 | continue; | |
305 | ||
e0d19036 NB |
306 | case O(CREATE,'n'): |
307 | case O(BUILD,'n'): /* number of raid disks */ | |
52826846 | 308 | if (raiddisks) { |
b83d95f3 | 309 | fprintf(stderr, Name ": raid-devices set twice: %d and %s\n", |
52826846 NB |
310 | raiddisks, optarg); |
311 | exit(2); | |
312 | } | |
313 | raiddisks = strtol(optarg, &c, 10); | |
314 | if (!optarg[0] || *c || raiddisks<=0 || raiddisks > MD_SB_DISKS) { | |
b83d95f3 | 315 | fprintf(stderr, Name ": invalid number of raid devices: %s\n", |
52826846 NB |
316 | optarg); |
317 | exit(2); | |
318 | } | |
cd29a5c8 | 319 | ident.raid_disks = raiddisks; |
52826846 NB |
320 | continue; |
321 | ||
e0d19036 | 322 | case O(CREATE,'x'): /* number of spare (eXtra) discs */ |
52826846 | 323 | if (sparedisks) { |
b83d95f3 | 324 | fprintf(stderr,Name ": spare-devices set twice: %d and %s\n", |
52826846 NB |
325 | sparedisks, optarg); |
326 | exit(2); | |
327 | } | |
e0d19036 | 328 | if (level > -10 && level <= 0 && level >= -1) { |
b83d95f3 | 329 | fprintf(stderr, Name ": spare-devices setting is incompatible with raid level %d\n", |
52826846 NB |
330 | level); |
331 | exit(2); | |
332 | } | |
333 | sparedisks = strtol(optarg, &c, 10); | |
334 | if (!optarg[0] || *c || sparedisks < 0 || sparedisks > MD_SB_DISKS - raiddisks) { | |
b83d95f3 | 335 | fprintf(stderr, Name ": invalid number of spare-devices: %s\n", |
52826846 NB |
336 | optarg); |
337 | exit(2); | |
338 | } | |
339 | continue; | |
e0d19036 NB |
340 | case O(CREATE,'f'): /* force honouring of device list */ |
341 | case O(ASSEMBLE,'f'): /* force assembly */ | |
342 | case O(MISC,'f'): /* force zero */ | |
52826846 NB |
343 | force=1; |
344 | continue; | |
345 | ||
346 | /* now for the Assemble options */ | |
e0d19036 | 347 | case O(ASSEMBLE,'u'): /* uuid of array */ |
52826846 | 348 | if (ident.uuid_set) { |
cd29a5c8 | 349 | fprintf(stderr, Name ": uuid cannot be set twice. " |
52826846 NB |
350 | "Second value %s.\n", optarg); |
351 | exit(2); | |
352 | } | |
353 | if (parse_uuid(optarg, ident.uuid)) | |
354 | ident.uuid_set = 1; | |
355 | else { | |
356 | fprintf(stderr,Name ": Bad uuid: %s\n", optarg); | |
357 | exit(2); | |
358 | } | |
359 | continue; | |
360 | ||
e0d19036 | 361 | case O(ASSEMBLE,'m'): /* super-minor for array */ |
d013a55e | 362 | if (ident.super_minor != -1) { |
cd29a5c8 NB |
363 | fprintf(stderr, Name ": super-minor cannot be set twice. " |
364 | "Second value: %s.\n", optarg); | |
365 | exit(2); | |
366 | } | |
d013a55e NB |
367 | if (strcmp(optarg, "dev")==0) |
368 | ident.super_minor = -2; | |
369 | else { | |
370 | ident.super_minor = strtoul(optarg, &cp, 10); | |
371 | if (!optarg[0] || *cp) { | |
372 | fprintf(stderr, Name ": Bad super-minor number: %s.\n", optarg); | |
373 | exit(2); | |
374 | } | |
cd29a5c8 NB |
375 | } |
376 | continue; | |
377 | ||
5787fa49 NB |
378 | case O(ASSEMBLE,'U'): /* update the superblock */ |
379 | if (update) { | |
380 | fprintf(stderr, Name ": Can only update one aspect of superblock, both %s and %s given.\n", | |
381 | update, optarg); | |
382 | exit(2); | |
383 | } | |
384 | update = optarg; | |
385 | if (strcmp(update, "sparc2.2")==0) continue; | |
386 | if (strcmp(update, "super-minor") == 0) | |
387 | continue; | |
388 | fprintf(stderr, Name ": '--update %s' invalid. Only 'sparc2.2' or 'super-minor' supported\n",update); | |
389 | exit(2); | |
390 | ||
e0d19036 | 391 | case O(ASSEMBLE,'c'): /* config file */ |
2d465520 | 392 | case O(MISC, 'c'): |
e0d19036 | 393 | case O(MONITOR,'c'): |
52826846 NB |
394 | if (configfile) { |
395 | fprintf(stderr, Name ": configfile cannot be set twice. " | |
396 | "Second value is %s.\n", optarg); | |
397 | exit(2); | |
398 | } | |
399 | configfile = optarg; | |
400 | /* FIXME possibly check that config file exists. Even parse it */ | |
401 | continue; | |
e0d19036 NB |
402 | case O(ASSEMBLE,'s'): /* scan */ |
403 | case O(MISC,'s'): | |
404 | case O(MONITOR,'s'): | |
52826846 NB |
405 | scan = 1; |
406 | continue; | |
407 | ||
e0d19036 | 408 | case O(MONITOR,'m'): /* mail address */ |
52826846 NB |
409 | if (mailaddr) |
410 | fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n", | |
411 | optarg); | |
412 | else | |
413 | mailaddr = optarg; | |
414 | continue; | |
415 | ||
e0d19036 | 416 | case O(MONITOR,'p'): /* alert program */ |
52826846 NB |
417 | if (program) |
418 | fprintf(stderr, Name ": only specify one alter program. %s ignored.\n", | |
419 | optarg); | |
420 | else | |
421 | program = optarg; | |
422 | continue; | |
64c4757e | 423 | |
e0d19036 | 424 | case O(MONITOR,'d'): /* delay in seconds */ |
52826846 NB |
425 | if (delay) |
426 | fprintf(stderr, Name ": only specify delay once. %s ignored.\n", | |
427 | optarg); | |
428 | else { | |
429 | delay = strtol(optarg, &c, 10); | |
430 | if (!optarg[0] || *c || delay<1) { | |
431 | fprintf(stderr, Name ": invalid delay: %s\n", | |
432 | optarg); | |
433 | exit(2); | |
434 | } | |
435 | } | |
436 | continue; | |
d013a55e NB |
437 | case O(MONITOR,'f'): /* daemonise */ |
438 | daemonise = 1; | |
439 | continue; | |
52826846 NB |
440 | |
441 | ||
442 | /* now the general management options. Some are applicable | |
443 | * to other modes. None have arguments. | |
444 | */ | |
e0d19036 NB |
445 | case O(MANAGE,'a'): |
446 | case O(CREATE,'a'): | |
447 | case O(BUILD,'a'): | |
448 | case O(ASSEMBLE,'a'): /* add a drive */ | |
52826846 NB |
449 | devmode = 'a'; |
450 | continue; | |
e0d19036 | 451 | case O(MANAGE,'r'): /* remove a drive */ |
52826846 NB |
452 | devmode = 'r'; |
453 | continue; | |
e0d19036 | 454 | case O(MANAGE,'f'): /* set faulty */ |
52826846 NB |
455 | devmode = 'f'; |
456 | continue; | |
e0d19036 NB |
457 | case O(MANAGE,'R'): |
458 | case O(ASSEMBLE,'R'): | |
459 | case O(BUILD,'R'): | |
460 | case O(CREATE,'R'): /* Run the array */ | |
52826846 NB |
461 | if (runstop < 0) { |
462 | fprintf(stderr, Name ": Cannot both Stop and Run an array\n"); | |
463 | exit(2); | |
464 | } | |
465 | runstop = 1; | |
466 | continue; | |
e0d19036 | 467 | case O(MANAGE,'S'): |
52826846 NB |
468 | if (runstop > 0) { |
469 | fprintf(stderr, Name ": Cannot both Run and Stop an array\n"); | |
470 | exit(2); | |
471 | } | |
472 | runstop = -1; | |
473 | continue; | |
474 | ||
e0d19036 | 475 | case O(MANAGE,'o'): |
52826846 NB |
476 | if (readonly < 0) { |
477 | fprintf(stderr, Name ": Cannot have both readonly and readwrite\n"); | |
478 | exit(2); | |
479 | } | |
480 | readonly = 1; | |
481 | continue; | |
e0d19036 | 482 | case O(MANAGE,'w'): |
52826846 | 483 | if (readonly > 0) { |
e0d19036 | 484 | fprintf(stderr, Name ": Cannot have both readwrite and readonly.\n"); |
52826846 NB |
485 | exit(2); |
486 | } | |
487 | readonly = -1; | |
488 | continue; | |
e0d19036 NB |
489 | |
490 | case O(MISC,'Q'): | |
491 | case O(MISC,'D'): | |
492 | case O(MISC,'E'): | |
493 | case O(MISC,'K'): | |
494 | case O(MISC,'R'): | |
495 | case O(MISC,'S'): | |
496 | case O(MISC,'o'): | |
497 | case O(MISC,'w'): | |
498 | if (devmode && devmode != opt && | |
499 | (devmode == 'E' || (opt == 'E' && devmode != 'Q'))) { | |
500 | fprintf(stderr, Name ": --examine/-E cannot be given with -%c\n", | |
501 | devmode =='E'?opt:devmode); | |
502 | exit(2); | |
503 | } | |
504 | devmode = opt; | |
505 | continue; | |
506 | ||
bd526cee NB |
507 | case O(MISC, 22): |
508 | if (devmode != 'E') { | |
509 | fprintf(stderr, Name ": --sparc2.2 only allowed with --examine\n"); | |
510 | exit(2); | |
511 | } | |
512 | SparcAdjust = 1; | |
513 | continue; | |
52826846 NB |
514 | } |
515 | /* We have now processed all the valid options. Anything else is | |
516 | * an error | |
517 | */ | |
e0d19036 NB |
518 | fprintf(stderr, Name ": option %c not valid in %s mode\n", |
519 | opt, map_num(modes, mode)); | |
64c4757e | 520 | exit(2); |
52826846 NB |
521 | |
522 | } | |
523 | ||
524 | if (!mode) { | |
525 | fputs(Usage, stderr); | |
64c4757e | 526 | exit(2); |
64c4757e | 527 | } |
52826846 NB |
528 | /* Ok, got the option parsing out of the way |
529 | * hopefully it's mostly right but there might be some stuff | |
530 | * missing | |
531 | * | |
e0d19036 | 532 | * That is mosty checked in the per-mode stuff but... |
52826846 NB |
533 | * |
534 | * For @,B,C and A without -s, the first device listed must be an md device | |
535 | * we check that here and open it. | |
64c4757e | 536 | */ |
52826846 | 537 | |
e0d19036 | 538 | if (mode==MANAGE || mode == BUILD || mode == CREATE || (mode == ASSEMBLE && ! scan)) { |
52826846 NB |
539 | if (devs_found < 1) { |
540 | fprintf(stderr, Name ": an md device must be given in this mode\n"); | |
541 | exit(2); | |
542 | } | |
cd29a5c8 | 543 | mdfd = open_mddev(devlist->devname); |
52826846 NB |
544 | if (mdfd < 0) |
545 | exit(1); | |
d013a55e NB |
546 | if (ident.super_minor == -2) { |
547 | struct stat stb; | |
548 | fstat(mdfd, &stb); | |
549 | ident.super_minor = MINOR(stb.st_rdev); | |
550 | } | |
64c4757e | 551 | } |
52826846 | 552 | |
52826846 NB |
553 | rv = 0; |
554 | switch(mode) { | |
e0d19036 | 555 | case MANAGE: |
52826846 NB |
556 | /* readonly, add/remove, readwrite, runstop */ |
557 | if (readonly>0) | |
cd29a5c8 | 558 | rv = Manage_ro(devlist->devname, mdfd, readonly); |
52826846 | 559 | if (!rv && devs_found>1) |
cd29a5c8 NB |
560 | rv = Manage_subdevs(devlist->devname, mdfd, |
561 | devlist->next); | |
52826846 | 562 | if (!rv && readonly < 0) |
cd29a5c8 | 563 | rv = Manage_ro(devlist->devname, mdfd, readonly); |
52826846 | 564 | if (!rv && runstop) |
cd29a5c8 | 565 | rv = Manage_runstop(devlist->devname, mdfd, runstop); |
52826846 | 566 | break; |
e0d19036 | 567 | case ASSEMBLE: |
d013a55e NB |
568 | if (devs_found == 1 && ident.uuid_set == 0 && |
569 | ident.super_minor == -1 && !scan ) { | |
570 | /* Only a device has been given, so get details from config file */ | |
571 | mddev_ident_t array_ident = conf_get_ident(configfile, devlist->devname); | |
572 | mdfd = open_mddev(devlist->devname); | |
573 | if (mdfd < 0) | |
574 | rv |= 1; | |
575 | else { | |
576 | if (array_ident == NULL) { | |
577 | fprintf(stderr, Name ": %s not identified in config file.\n", | |
578 | devlist->devname); | |
579 | rv |= 1; | |
580 | } | |
581 | else | |
582 | rv |= Assemble(devlist->devname, mdfd, array_ident, configfile, | |
583 | NULL, | |
584 | readonly, runstop, update, verbose, force); | |
585 | } | |
586 | } else if (!scan) | |
cd29a5c8 NB |
587 | rv = Assemble(devlist->devname, mdfd, &ident, configfile, |
588 | devlist->next, | |
5787fa49 NB |
589 | readonly, runstop, update, verbose, force); |
590 | else if (devs_found>0) { | |
591 | if (update && devs_found > 1) { | |
592 | fprintf(stderr, Name ": can only update a single array at a time\n"); | |
593 | exit(1); | |
594 | } | |
cd29a5c8 NB |
595 | for (dv = devlist ; dv ; dv=dv->next) { |
596 | mddev_ident_t array_ident = conf_get_ident(configfile, dv->devname); | |
597 | mdfd = open_mddev(dv->devname); | |
52826846 NB |
598 | if (mdfd < 0) { |
599 | rv |= 1; | |
600 | continue; | |
601 | } | |
602 | if (array_ident == NULL) { | |
603 | fprintf(stderr, Name ": %s not identified in config file.\n", | |
cd29a5c8 | 604 | dv->devname); |
52826846 NB |
605 | rv |= 1; |
606 | continue; | |
607 | } | |
cd29a5c8 NB |
608 | rv |= Assemble(dv->devname, mdfd, array_ident, configfile, |
609 | NULL, | |
5787fa49 | 610 | readonly, runstop, update, verbose, force); |
52826846 | 611 | } |
5787fa49 | 612 | } else { |
52826846 NB |
613 | mddev_ident_t array_list = conf_get_ident(configfile, NULL); |
614 | if (!array_list) { | |
615 | fprintf(stderr, Name ": No arrays found in config file\n"); | |
616 | rv = 1; | |
617 | } else | |
618 | for (; array_list; array_list = array_list->next) { | |
cd29a5c8 | 619 | mdu_array_info_t array; |
52826846 NB |
620 | mdfd = open_mddev(array_list->devname); |
621 | if (mdfd < 0) { | |
622 | rv |= 1; | |
623 | continue; | |
624 | } | |
cd29a5c8 NB |
625 | if (ioctl(mdfd, GET_ARRAY_INFO, &array)>=0) |
626 | /* already assembled, skip */ | |
627 | continue; | |
52826846 NB |
628 | rv |= Assemble(array_list->devname, mdfd, |
629 | array_list, configfile, | |
cd29a5c8 | 630 | NULL, |
5787fa49 | 631 | readonly, runstop, NULL, verbose, force); |
52826846 NB |
632 | } |
633 | } | |
634 | break; | |
e0d19036 | 635 | case BUILD: |
cd29a5c8 | 636 | rv = Build(devlist->devname, mdfd, chunk, level, raiddisks, devlist->next); |
52826846 | 637 | break; |
e0d19036 | 638 | case CREATE: |
cd29a5c8 | 639 | rv = Create(devlist->devname, mdfd, chunk, level, layout, size, |
52826846 | 640 | raiddisks, sparedisks, |
cd29a5c8 | 641 | devs_found-1, devlist->next, runstop, verbose, force); |
52826846 | 642 | break; |
e0d19036 NB |
643 | case MISC: |
644 | ||
645 | if (devmode == 'E') { | |
646 | if (devlist == NULL && !scan) { | |
647 | fprintf(stderr, Name ": No devices to examine\n"); | |
648 | exit(2); | |
649 | } | |
650 | if (devlist == NULL) | |
651 | devlist = conf_get_devs(configfile); | |
652 | if (devlist == NULL) { | |
c913b90e | 653 | fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile); |
e0d19036 NB |
654 | exit(1); |
655 | } | |
bd526cee | 656 | rv = Examine(devlist, scan?!verbose:brief, scan, SparcAdjust); |
e0d19036 NB |
657 | } else { |
658 | if (devlist == NULL) { | |
659 | if ((devmode == 'S' ||devmode=='D') && scan) { | |
660 | /* apply to all devices in /proc/mdstat */ | |
661 | struct mdstat_ent *ms = mdstat_read(); | |
662 | struct mdstat_ent *e; | |
663 | for (e=ms ; e ; e=e->next) { | |
664 | char *name = get_md_name(e->devnum); | |
665 | ||
666 | if (!name) { | |
667 | fprintf(stderr, Name ": cannot find device file for %s\n", | |
668 | e->dev); | |
669 | continue; | |
670 | } | |
671 | if (devmode == 'D') | |
672 | rv |= Detail(name, !verbose); | |
673 | else if (devmode=='S') { | |
674 | mdfd = open_mddev(name); | |
675 | if (mdfd >= 0) | |
676 | rv |= Manage_runstop(name, mdfd, -1); | |
677 | } | |
678 | put_md_name(name); | |
679 | } | |
680 | } else { | |
681 | fprintf(stderr, Name ": No devices given.\n"); | |
682 | exit(2); | |
683 | } | |
684 | } | |
685 | for (dv=devlist ; dv; dv=dv->next) { | |
686 | switch(dv->disposition) { | |
687 | case 'D': | |
688 | rv |= Detail(dv->devname, brief); continue; | |
689 | case 'K': /* Zero superblock */ | |
690 | rv |= Kill(dv->devname, force); continue; | |
691 | case 'Q': | |
692 | rv |= Query(dv->devname); continue; | |
693 | } | |
694 | mdfd = open_mddev(dv->devname); | |
695 | if (mdfd>=0) | |
696 | switch(dv->disposition) { | |
697 | case 'R': | |
698 | rv |= Manage_runstop(dv->devname, mdfd, 1); break; | |
699 | case 'S': | |
700 | rv |= Manage_runstop(dv->devname, mdfd, -1); break; | |
701 | case 'o': | |
702 | rv |= Manage_ro(dv->devname, mdfd, 1); break; | |
703 | case 'w': | |
704 | rv |= Manage_ro(dv->devname, mdfd, -1); break; | |
705 | } | |
706 | } | |
cd29a5c8 | 707 | } |
9a9dab36 | 708 | break; |
e0d19036 | 709 | case MONITOR: |
e0d19036 NB |
710 | if (!devlist && !scan) { |
711 | fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n"); | |
712 | rv = 1; | |
713 | break; | |
714 | } | |
d013a55e NB |
715 | rv= Monitor(devlist, mailaddr, program, |
716 | delay?delay:60, daemonise, scan, configfile); | |
9a9dab36 | 717 | break; |
64c4757e | 718 | } |
52826846 | 719 | exit(rv); |
64c4757e | 720 | } |