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