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