]>
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 | |
c82f047c NB |
28 | * |
29 | * Additions for bitmap and async RAID options, Copyright (C) 2003-2004, | |
30 | * Paul Clements, SteelEye Technology, Inc. | |
64c4757e NB |
31 | */ |
32 | ||
9a9dab36 | 33 | #include "mdadm.h" |
64c4757e | 34 | #include "md_p.h" |
dd0781e5 | 35 | #include <ctype.h> |
64c4757e | 36 | |
dd0781e5 | 37 | |
52826846 NB |
38 | int main(int argc, char *argv[]) |
39 | { | |
e0d19036 | 40 | int mode = 0; |
52826846 | 41 | int opt; |
e0d19036 | 42 | int option_index; |
52826846 NB |
43 | char *help_text; |
44 | char *c; | |
45 | int rv; | |
f9ce90ba | 46 | int i; |
52826846 NB |
47 | |
48 | int chunk = 0; | |
dd0781e5 | 49 | int size = -1; |
98c6faba NB |
50 | int level = UnSet; |
51 | int layout = UnSet; | |
52826846 NB |
52 | int raiddisks = 0; |
53 | int sparedisks = 0; | |
54 | struct mddev_ident_s ident; | |
55 | char *configfile = NULL; | |
cd29a5c8 | 56 | char *cp; |
5787fa49 | 57 | char *update = NULL; |
52826846 NB |
58 | int scan = 0; |
59 | char devmode = 0; | |
60 | int runstop = 0; | |
61 | int readonly = 0; | |
c82f047c NB |
62 | int bitmap_fd = -1; |
63 | char *bitmap_file = NULL; | |
64 | int bitmap_chunk = UnSet; | |
bd526cee | 65 | int SparcAdjust = 0; |
cd29a5c8 NB |
66 | mddev_dev_t devlist = NULL; |
67 | mddev_dev_t *devlistend = & devlist; | |
68 | mddev_dev_t dv; | |
52826846 NB |
69 | int devs_found = 0; |
70 | int verbose = 0; | |
cd29a5c8 | 71 | int brief = 0; |
52826846 | 72 | int force = 0; |
feb716e9 | 73 | int test = 0; |
dd0781e5 | 74 | int assume_clean = 0; |
8d80900b NB |
75 | int autof = 0; /* -2 means create device based on name: |
76 | * if it ends mdN, then non-partitioned array N | |
77 | * if it ends dN, then partitions array N | |
78 | * -1 means create non-partitioned, choose N | |
79 | * 1 or more to create partitioned | |
80 | * If -1 or 1 and name is a 'standard' name, then | |
81 | * insist on a match of type and number. | |
82 | */ | |
52826846 NB |
83 | |
84 | char *mailaddr = NULL; | |
85 | char *program = NULL; | |
86 | int delay = 0; | |
d013a55e | 87 | int daemonise = 0; |
b5e64645 | 88 | char *pidfile = NULL; |
aa88f531 | 89 | int oneshot = 0; |
82d9eba6 | 90 | struct supertype *ss = NULL; |
52826846 | 91 | |
e5329c37 NB |
92 | int copies; |
93 | ||
52826846 NB |
94 | int mdfd = -1; |
95 | ||
96 | ident.uuid_set=0; | |
98c6faba NB |
97 | ident.level = UnSet; |
98 | ident.raid_disks = UnSet; | |
99 | ident.super_minor= UnSet; | |
52826846 | 100 | ident.devices=0; |
a75f2fbc NB |
101 | ident.spare_group = NULL; |
102 | ident.autof = 0; | |
a825febc | 103 | ident.st = NULL; |
c82f047c | 104 | ident.bitmap_fd = -1; |
52826846 | 105 | |
e0d19036 NB |
106 | while ((option_index = -1) , |
107 | (opt=getopt_long(argc, argv, | |
52826846 | 108 | short_options, long_options, |
e0d19036 NB |
109 | &option_index)) != -1) { |
110 | int newmode = mode; | |
111 | /* firstly, so mode-independant options */ | |
52826846 | 112 | switch(opt) { |
52826846 NB |
113 | case 'h': |
114 | help_text = Help; | |
56eedc1a NB |
115 | if (option_index > 0 && |
116 | strcmp(long_options[option_index].name, "help-options")==0) | |
117 | help_text = OptionHelp; | |
118 | else | |
119 | switch (mode) { | |
120 | case ASSEMBLE : help_text = Help_assemble; break; | |
121 | case BUILD : help_text = Help_build; break; | |
122 | case CREATE : help_text = Help_create; break; | |
123 | case MANAGE : help_text = Help_manage; break; | |
124 | case MISC : help_text = Help_misc; break; | |
125 | case MONITOR : help_text = Help_monitor; break; | |
dd0781e5 | 126 | case GROW : help_text = Help_grow; break; |
56eedc1a | 127 | } |
52826846 NB |
128 | fputs(help_text,stderr); |
129 | exit(0); | |
130 | ||
131 | case 'V': | |
132 | fputs(Version, stderr); | |
133 | exit(0); | |
134 | ||
135 | case 'v': verbose = 1; | |
136 | continue; | |
137 | ||
c82f047c NB |
138 | case 'b': |
139 | if (mode == ASSEMBLE || mode == BUILD || mode == CREATE) | |
140 | break; /* b means bitmap */ | |
141 | brief = 1; | |
cd29a5c8 NB |
142 | continue; |
143 | ||
e0d19036 NB |
144 | case ':': |
145 | case '?': | |
146 | fputs(Usage, stderr); | |
147 | exit(2); | |
148 | } | |
149 | /* second, figure out the mode. | |
150 | * Some options force the mode. Others | |
151 | * set the mode if it isn't already | |
152 | */ | |
153 | ||
154 | switch(opt) { | |
155 | case '@': /* just incase they say --manage */ | |
156 | newmode = MANAGE; break; | |
157 | case 'a': | |
158 | case 'r': | |
159 | case 'f': | |
0320ea45 NB |
160 | if (!mode) newmode = MANAGE; |
161 | break; | |
e0d19036 NB |
162 | |
163 | case 'A': newmode = ASSEMBLE; break; | |
164 | case 'B': newmode = BUILD; break; | |
165 | case 'C': newmode = CREATE; break; | |
166 | case 'F': newmode = MONITOR;break; | |
dd0781e5 | 167 | case 'G': newmode = GROW; break; |
e0d19036 NB |
168 | |
169 | case '#': | |
170 | case 'D': | |
171 | case 'E': | |
c82f047c | 172 | case 'X': |
e0d19036 NB |
173 | case 'Q': newmode = MISC; break; |
174 | case 'R': | |
175 | case 'S': | |
176 | case 'o': | |
177 | case 'w': | |
178 | case 'K': if (!mode) newmode = MISC; break; | |
179 | } | |
180 | if (mode && newmode == mode) { | |
181 | /* everybody happy ! */ | |
182 | } else if (mode && newmode != mode) { | |
183 | /* not allowed.. */ | |
184 | fprintf(stderr, Name ": "); | |
185 | if (option_index >= 0) | |
186 | fprintf(stderr, "--%s", long_options[option_index].name); | |
187 | else | |
188 | fprintf(stderr, "-%c", opt); | |
e6b64cd0 | 189 | fprintf(stderr, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n", |
e0d19036 NB |
190 | map_num(modes, newmode), |
191 | map_num(modes, mode)); | |
192 | exit(2); | |
193 | } else if (!mode && newmode) { | |
194 | mode = newmode; | |
195 | } else { | |
196 | /* special case of -c --help */ | |
197 | if (opt == 'c' && | |
198 | ( strncmp(optarg, "--h", 3)==0 || | |
199 | strncmp(optarg, "-h", 2)==0)) { | |
200 | fputs(Help_config, stderr); | |
201 | exit(0); | |
cd29a5c8 | 202 | } |
0320ea45 NB |
203 | |
204 | /* If first option is a device, don't force the mode yet */ | |
205 | if (opt == 1) { | |
206 | if (devs_found == 0) { | |
207 | dv = malloc(sizeof(*dv)); | |
208 | if (dv == NULL) { | |
209 | fprintf(stderr, Name ": malloc failed\n"); | |
210 | exit(3); | |
211 | } | |
212 | dv->devname = optarg; | |
213 | dv->disposition = devmode; | |
214 | dv->next = NULL; | |
215 | *devlistend = dv; | |
216 | devlistend = &dv->next; | |
217 | ||
218 | devs_found++; | |
219 | continue; | |
220 | } | |
221 | /* No mode yet, and this is the second device ... */ | |
222 | fprintf(stderr, Name ": An option must be given to set the mode before a second device is listed\n"); | |
223 | exit(2); | |
224 | } | |
e0d19036 | 225 | if (option_index >= 0) |
0320ea45 | 226 | fprintf(stderr, Name ": --%s", long_options[option_index].name); |
e0d19036 | 227 | else |
0320ea45 NB |
228 | fprintf(stderr, Name ": -%c", opt); |
229 | fprintf(stderr, " does not set the mode, and so cannot be the first option.\n"); | |
e0d19036 NB |
230 | exit(2); |
231 | } | |
232 | ||
233 | /* if we just set the mode, then done */ | |
234 | switch(opt) { | |
235 | case '@': | |
236 | case '#': | |
237 | case 'A': | |
238 | case 'B': | |
239 | case 'C': | |
240 | case 'F': | |
dd0781e5 | 241 | case 'G': |
e0d19036 NB |
242 | continue; |
243 | } | |
244 | if (opt == 1) { | |
245 | /* an undecorated option - must be a device name. | |
246 | */ | |
cd29a5c8 | 247 | if (devs_found > 0 && mode == '@' && !devmode) { |
dd0781e5 | 248 | fprintf(stderr, Name ": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg); |
52826846 NB |
249 | exit(2); |
250 | } | |
e5329c37 NB |
251 | if (devs_found > 0 && mode == 'G' && !devmode) { |
252 | fprintf(stderr, Name ": Must give one of -a for devices do add: %s\n", optarg); | |
253 | exit(2); | |
254 | } | |
cd29a5c8 NB |
255 | dv = malloc(sizeof(*dv)); |
256 | if (dv == NULL) { | |
257 | fprintf(stderr, Name ": malloc failed\n"); | |
258 | exit(3); | |
259 | } | |
260 | dv->devname = optarg; | |
261 | dv->disposition = devmode; | |
262 | dv->next = NULL; | |
263 | *devlistend = dv; | |
264 | devlistend = &dv->next; | |
265 | ||
52826846 NB |
266 | devs_found++; |
267 | continue; | |
682c7051 | 268 | } |
682c7051 | 269 | |
52826846 NB |
270 | /* We've got a mode, and opt is now something else which |
271 | * could depend on the mode */ | |
272 | #define O(a,b) ((a<<8)|b) | |
273 | switch (O(mode,opt)) { | |
e0d19036 NB |
274 | case O(CREATE,'c'): |
275 | case O(BUILD,'c'): /* chunk or rounding */ | |
52826846 NB |
276 | if (chunk) { |
277 | fprintf(stderr, Name ": chunk/rounding may only be specified once. " | |
278 | "Second value is %s.\n", optarg); | |
279 | exit(2); | |
280 | } | |
281 | chunk = strtol(optarg, &c, 10); | |
282 | if (!optarg[0] || *c || chunk<4 || ((chunk-1)&chunk)) { | |
283 | fprintf(stderr, Name ": invalid chunk/rounding value: %s\n", | |
284 | optarg); | |
285 | exit(2); | |
286 | } | |
287 | continue; | |
64c4757e | 288 | |
f9ce90ba NB |
289 | case O(CREATE,'e'): |
290 | case O(ASSEMBLE,'e'): | |
291 | case O(MISC,'e'): /* set metadata (superblock) information */ | |
292 | if (ss) { | |
293 | fprintf(stderr, Name ": metadata information already given\n"); | |
294 | exit(2); | |
295 | } | |
82d9eba6 NB |
296 | for(i=0; !ss && superlist[i]; i++) |
297 | ss = superlist[i]->match_metadata_desc(optarg); | |
298 | ||
f9ce90ba NB |
299 | if (!ss) { |
300 | fprintf(stderr, Name ": unrecognised metadata identifier: %s\n", optarg); | |
301 | exit(2); | |
302 | } | |
303 | continue; | |
304 | ||
dd0781e5 | 305 | case O(GROW,'z'): |
e0d19036 | 306 | case O(CREATE,'z'): /* size */ |
dd0781e5 | 307 | if (size >= 0) { |
52826846 NB |
308 | fprintf(stderr, Name ": size may only be specified once. " |
309 | "Second value is %s.\n", optarg); | |
310 | exit(2); | |
311 | } | |
dd0781e5 NB |
312 | if (strcmp(optarg, "max")==0) |
313 | size = 0; | |
314 | else { | |
315 | size = strtol(optarg, &c, 10); | |
316 | if (!optarg[0] || *c || size < 4) { | |
317 | fprintf(stderr, Name ": invalid size: %s\n", | |
318 | optarg); | |
319 | exit(2); | |
320 | } | |
52826846 NB |
321 | } |
322 | continue; | |
64c4757e | 323 | |
b5e64645 | 324 | case O(GROW,'l'): /* hack - needed to understand layout */ |
e0d19036 NB |
325 | case O(CREATE,'l'): |
326 | case O(BUILD,'l'): /* set raid level*/ | |
98c6faba | 327 | if (level != UnSet) { |
52826846 NB |
328 | fprintf(stderr, Name ": raid level may only be set once. " |
329 | "Second value is %s.\n", optarg); | |
330 | exit(2); | |
331 | } | |
332 | level = map_name(pers, optarg); | |
98c6faba | 333 | if (level == UnSet) { |
52826846 NB |
334 | fprintf(stderr, Name ": invalid raid level: %s\n", |
335 | optarg); | |
336 | exit(2); | |
337 | } | |
b5e64645 | 338 | if (level != 0 && level != -1 && level != 1 && level != -4 && level != -5 && mode == BUILD) { |
52826846 NB |
339 | fprintf(stderr, Name ": Raid level %s not permitted with --build.\n", |
340 | optarg); | |
341 | exit(2); | |
342 | } | |
e0d19036 | 343 | if (sparedisks > 0 && level < 1 && level >= -1) { |
b83d95f3 | 344 | fprintf(stderr, Name ": raid level %s is incompatible with spare-devices setting.\n", |
52826846 NB |
345 | optarg); |
346 | exit(2); | |
347 | } | |
cd29a5c8 | 348 | ident.level = level; |
52826846 | 349 | continue; |
64c4757e | 350 | |
e0d19036 | 351 | case O(CREATE,'p'): /* raid5 layout */ |
b5e64645 NB |
352 | case O(BUILD,'p'): /* faulty layout */ |
353 | case O(GROW, 'p'): /* faulty reconfig */ | |
e5329c37 | 354 | if (layout != UnSet) { |
52826846 NB |
355 | fprintf(stderr,Name ": layout may only be sent once. " |
356 | "Second value was %s\n", optarg); | |
357 | exit(2); | |
358 | } | |
359 | switch(level) { | |
360 | default: | |
56eb10c0 | 361 | fprintf(stderr, Name ": layout not meaningful for %s arrays.\n", |
52826846 NB |
362 | map_num(pers, level)); |
363 | exit(2); | |
98c6faba | 364 | case UnSet: |
52826846 NB |
365 | fprintf(stderr, Name ": raid level must be given before layout.\n"); |
366 | exit(2); | |
367 | ||
368 | case 5: | |
98c6faba | 369 | case 6: |
52826846 | 370 | layout = map_name(r5layout, optarg); |
98c6faba | 371 | if (layout==UnSet) { |
52826846 NB |
372 | fprintf(stderr, Name ": layout %s not understood for raid5.\n", |
373 | optarg); | |
374 | exit(2); | |
375 | } | |
376 | break; | |
e5329c37 NB |
377 | |
378 | case 10: | |
379 | /* 'f' or 'n' followed by a number <= raid_disks */ | |
380 | if ((optarg[0] != 'n' && optarg[0] != 'f') || | |
381 | (copies = strtoul(optarg+1, &cp, 10)) < 1 || | |
382 | copies > 200 || | |
383 | *cp) { | |
384 | fprintf(stderr, Name ": layout for raid10 must be 'nNN' or 'fNN' where NN is a number, not %s\n", optarg); | |
385 | exit(2); | |
386 | } | |
387 | if (optarg[0] == 'n') | |
388 | layout = 256 + copies; | |
389 | else | |
390 | layout = 1 + (copies<<8); | |
391 | break; | |
b5e64645 NB |
392 | case -5: /* Faulty |
393 | * modeNNN | |
394 | */ | |
395 | ||
396 | { | |
397 | int ln = strcspn(optarg, "0123456789"); | |
398 | char *m = strdup(optarg); | |
399 | int mode; | |
400 | m[ln] = 0; | |
401 | mode = map_name(faultylayout, m); | |
402 | if (mode == UnSet) { | |
403 | fprintf(stderr, Name ": layout %s not understood for faulty.\n", | |
404 | optarg); | |
405 | exit(2); | |
406 | } | |
407 | layout = mode | (atoi(optarg+ln)<< ModeShift); | |
408 | } | |
52826846 NB |
409 | } |
410 | continue; | |
411 | ||
dd0781e5 NB |
412 | case O(CREATE,3): |
413 | case O(BUILD,3): /* assume clean */ | |
414 | assume_clean = 1; | |
415 | continue; | |
416 | ||
417 | case O(GROW,'n'): | |
e0d19036 NB |
418 | case O(CREATE,'n'): |
419 | case O(BUILD,'n'): /* number of raid disks */ | |
52826846 | 420 | if (raiddisks) { |
b83d95f3 | 421 | fprintf(stderr, Name ": raid-devices set twice: %d and %s\n", |
52826846 NB |
422 | raiddisks, optarg); |
423 | exit(2); | |
424 | } | |
425 | raiddisks = strtol(optarg, &c, 10); | |
426 | if (!optarg[0] || *c || raiddisks<=0 || raiddisks > MD_SB_DISKS) { | |
b83d95f3 | 427 | fprintf(stderr, Name ": invalid number of raid devices: %s\n", |
52826846 NB |
428 | optarg); |
429 | exit(2); | |
430 | } | |
b5e64645 | 431 | if (raiddisks == 1 && !force && level != -5) { |
aa88f531 NB |
432 | fprintf(stderr, Name ": '1' is an unusual number of drives for an array, so it is probably\n" |
433 | " a mistake. If you really mean it you will need to specify --force before\n" | |
434 | " setting the number of drives.\n"); | |
435 | exit(2); | |
436 | } | |
cd29a5c8 | 437 | ident.raid_disks = raiddisks; |
52826846 NB |
438 | continue; |
439 | ||
e0d19036 | 440 | case O(CREATE,'x'): /* number of spare (eXtra) discs */ |
52826846 | 441 | if (sparedisks) { |
b83d95f3 | 442 | fprintf(stderr,Name ": spare-devices set twice: %d and %s\n", |
52826846 NB |
443 | sparedisks, optarg); |
444 | exit(2); | |
445 | } | |
98c6faba | 446 | if (level != UnSet && level <= 0 && level >= -1) { |
b83d95f3 | 447 | fprintf(stderr, Name ": spare-devices setting is incompatible with raid level %d\n", |
52826846 NB |
448 | level); |
449 | exit(2); | |
450 | } | |
451 | sparedisks = strtol(optarg, &c, 10); | |
452 | if (!optarg[0] || *c || sparedisks < 0 || sparedisks > MD_SB_DISKS - raiddisks) { | |
b83d95f3 | 453 | fprintf(stderr, Name ": invalid number of spare-devices: %s\n", |
52826846 NB |
454 | optarg); |
455 | exit(2); | |
456 | } | |
457 | continue; | |
dd0781e5 NB |
458 | |
459 | case O(CREATE,'a'): | |
460 | case O(BUILD,'a'): | |
461 | case O(ASSEMBLE,'a'): /* auto-creation of device node */ | |
462 | if (optarg == NULL) | |
8d80900b | 463 | autof = -2; |
dd0781e5 NB |
464 | else if (strcasecmp(optarg,"no")==0) |
465 | autof = 0; | |
8d80900b NB |
466 | else if (strcasecmp(optarg,"yes")==0) |
467 | autof = -2; | |
468 | else if (strcasecmp(optarg,"md")==0) | |
dd0781e5 NB |
469 | autof = -1; |
470 | else { | |
471 | /* There might be digits, and maybe a hypen, at the end */ | |
472 | char *e = optarg + strlen(optarg); | |
473 | int num = 4; | |
474 | int len; | |
475 | while (e > optarg && isdigit(e[-1])) | |
476 | e--; | |
477 | if (*e) { | |
478 | num = atoi(e); | |
479 | if (num <= 0) num = 1; | |
480 | } | |
481 | if (e > optarg && e[-1] == '-') | |
482 | e--; | |
483 | len = e - optarg; | |
484 | if ((len == 3 && strncasecmp(optarg,"mdp",3)==0) || | |
485 | (len == 1 && strncasecmp(optarg,"p",1)==0) || | |
486 | (len >= 4 && strncasecmp(optarg,"part",4)==0)) | |
487 | autof = num; | |
488 | else { | |
489 | fprintf(stderr, Name ": --auto flag arg of \"%s\" unrecognised: use no,yes,md,mdp,part\n" | |
490 | " optionally followed by a number.\n", | |
491 | optarg); | |
492 | exit(2); | |
493 | } | |
494 | } | |
495 | continue; | |
496 | ||
aa88f531 | 497 | case O(BUILD,'f'): /* force honouring '-n 1' */ |
e0d19036 NB |
498 | case O(CREATE,'f'): /* force honouring of device list */ |
499 | case O(ASSEMBLE,'f'): /* force assembly */ | |
500 | case O(MISC,'f'): /* force zero */ | |
52826846 NB |
501 | force=1; |
502 | continue; | |
503 | ||
504 | /* now for the Assemble options */ | |
e0d19036 | 505 | case O(ASSEMBLE,'u'): /* uuid of array */ |
52826846 | 506 | if (ident.uuid_set) { |
cd29a5c8 | 507 | fprintf(stderr, Name ": uuid cannot be set twice. " |
52826846 NB |
508 | "Second value %s.\n", optarg); |
509 | exit(2); | |
510 | } | |
511 | if (parse_uuid(optarg, ident.uuid)) | |
512 | ident.uuid_set = 1; | |
513 | else { | |
514 | fprintf(stderr,Name ": Bad uuid: %s\n", optarg); | |
515 | exit(2); | |
516 | } | |
517 | continue; | |
518 | ||
e0d19036 | 519 | case O(ASSEMBLE,'m'): /* super-minor for array */ |
98c6faba | 520 | if (ident.super_minor != UnSet) { |
cd29a5c8 NB |
521 | fprintf(stderr, Name ": super-minor cannot be set twice. " |
522 | "Second value: %s.\n", optarg); | |
523 | exit(2); | |
524 | } | |
d013a55e NB |
525 | if (strcmp(optarg, "dev")==0) |
526 | ident.super_minor = -2; | |
527 | else { | |
528 | ident.super_minor = strtoul(optarg, &cp, 10); | |
529 | if (!optarg[0] || *cp) { | |
530 | fprintf(stderr, Name ": Bad super-minor number: %s.\n", optarg); | |
531 | exit(2); | |
532 | } | |
cd29a5c8 NB |
533 | } |
534 | continue; | |
535 | ||
5787fa49 NB |
536 | case O(ASSEMBLE,'U'): /* update the superblock */ |
537 | if (update) { | |
538 | fprintf(stderr, Name ": Can only update one aspect of superblock, both %s and %s given.\n", | |
539 | update, optarg); | |
540 | exit(2); | |
541 | } | |
542 | update = optarg; | |
e5329c37 NB |
543 | if (strcmp(update, "sparc2.2")==0) |
544 | continue; | |
5787fa49 NB |
545 | if (strcmp(update, "super-minor") == 0) |
546 | continue; | |
feb716e9 NB |
547 | if (strcmp(update, "summaries")==0) |
548 | continue; | |
e5329c37 NB |
549 | if (strcmp(update, "resync")==0) |
550 | continue; | |
551 | fprintf(stderr, Name ": '--update %s' invalid. Only 'sparc2.2', 'super-minor', 'resync' or 'summaries' supported\n",update); | |
5787fa49 NB |
552 | exit(2); |
553 | ||
e0d19036 | 554 | case O(ASSEMBLE,'c'): /* config file */ |
2d465520 | 555 | case O(MISC, 'c'): |
e0d19036 | 556 | case O(MONITOR,'c'): |
52826846 NB |
557 | if (configfile) { |
558 | fprintf(stderr, Name ": configfile cannot be set twice. " | |
559 | "Second value is %s.\n", optarg); | |
560 | exit(2); | |
561 | } | |
562 | configfile = optarg; | |
563 | /* FIXME possibly check that config file exists. Even parse it */ | |
564 | continue; | |
e0d19036 NB |
565 | case O(ASSEMBLE,'s'): /* scan */ |
566 | case O(MISC,'s'): | |
567 | case O(MONITOR,'s'): | |
52826846 NB |
568 | scan = 1; |
569 | continue; | |
570 | ||
e0d19036 | 571 | case O(MONITOR,'m'): /* mail address */ |
52826846 NB |
572 | if (mailaddr) |
573 | fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n", | |
574 | optarg); | |
575 | else | |
576 | mailaddr = optarg; | |
577 | continue; | |
578 | ||
e0d19036 | 579 | case O(MONITOR,'p'): /* alert program */ |
52826846 NB |
580 | if (program) |
581 | fprintf(stderr, Name ": only specify one alter program. %s ignored.\n", | |
582 | optarg); | |
583 | else | |
584 | program = optarg; | |
585 | continue; | |
64c4757e | 586 | |
e0d19036 | 587 | case O(MONITOR,'d'): /* delay in seconds */ |
c82f047c NB |
588 | case O(BUILD,'d'): /* delay for bitmap updates */ |
589 | case O(CREATE,'d'): | |
52826846 NB |
590 | if (delay) |
591 | fprintf(stderr, Name ": only specify delay once. %s ignored.\n", | |
592 | optarg); | |
593 | else { | |
594 | delay = strtol(optarg, &c, 10); | |
595 | if (!optarg[0] || *c || delay<1) { | |
596 | fprintf(stderr, Name ": invalid delay: %s\n", | |
597 | optarg); | |
598 | exit(2); | |
599 | } | |
600 | } | |
601 | continue; | |
d013a55e NB |
602 | case O(MONITOR,'f'): /* daemonise */ |
603 | daemonise = 1; | |
604 | continue; | |
b5e64645 NB |
605 | case O(MONITOR,'i'): /* pid */ |
606 | if (pidfile) | |
607 | fprintf(stderr, Name ": only specify one pid file. %s ignored.\n", | |
608 | optarg); | |
609 | else | |
610 | pidfile = optarg; | |
611 | continue; | |
aa88f531 NB |
612 | case O(MONITOR,'1'): /* oneshot */ |
613 | oneshot = 1; | |
614 | continue; | |
98c6faba NB |
615 | case O(MONITOR,'t'): /* test */ |
616 | test = 1; | |
617 | continue; | |
52826846 NB |
618 | |
619 | /* now the general management options. Some are applicable | |
620 | * to other modes. None have arguments. | |
621 | */ | |
e5329c37 | 622 | case O(GROW,'a'): |
dd0781e5 | 623 | case O(MANAGE,'a'): /* add a drive */ |
52826846 NB |
624 | devmode = 'a'; |
625 | continue; | |
e0d19036 | 626 | case O(MANAGE,'r'): /* remove a drive */ |
52826846 NB |
627 | devmode = 'r'; |
628 | continue; | |
e0d19036 | 629 | case O(MANAGE,'f'): /* set faulty */ |
52826846 NB |
630 | devmode = 'f'; |
631 | continue; | |
e0d19036 NB |
632 | case O(MANAGE,'R'): |
633 | case O(ASSEMBLE,'R'): | |
634 | case O(BUILD,'R'): | |
635 | case O(CREATE,'R'): /* Run the array */ | |
52826846 NB |
636 | if (runstop < 0) { |
637 | fprintf(stderr, Name ": Cannot both Stop and Run an array\n"); | |
638 | exit(2); | |
639 | } | |
640 | runstop = 1; | |
641 | continue; | |
e0d19036 | 642 | case O(MANAGE,'S'): |
52826846 NB |
643 | if (runstop > 0) { |
644 | fprintf(stderr, Name ": Cannot both Run and Stop an array\n"); | |
645 | exit(2); | |
646 | } | |
647 | runstop = -1; | |
648 | continue; | |
649 | ||
e0d19036 | 650 | case O(MANAGE,'o'): |
52826846 NB |
651 | if (readonly < 0) { |
652 | fprintf(stderr, Name ": Cannot have both readonly and readwrite\n"); | |
653 | exit(2); | |
654 | } | |
655 | readonly = 1; | |
656 | continue; | |
e0d19036 | 657 | case O(MANAGE,'w'): |
52826846 | 658 | if (readonly > 0) { |
e0d19036 | 659 | fprintf(stderr, Name ": Cannot have both readwrite and readonly.\n"); |
52826846 NB |
660 | exit(2); |
661 | } | |
662 | readonly = -1; | |
663 | continue; | |
e0d19036 NB |
664 | |
665 | case O(MISC,'Q'): | |
666 | case O(MISC,'D'): | |
667 | case O(MISC,'E'): | |
668 | case O(MISC,'K'): | |
669 | case O(MISC,'R'): | |
670 | case O(MISC,'S'): | |
c82f047c | 671 | case O(MISC,'X'): |
e0d19036 NB |
672 | case O(MISC,'o'): |
673 | case O(MISC,'w'): | |
674 | if (devmode && devmode != opt && | |
675 | (devmode == 'E' || (opt == 'E' && devmode != 'Q'))) { | |
676 | fprintf(stderr, Name ": --examine/-E cannot be given with -%c\n", | |
677 | devmode =='E'?opt:devmode); | |
678 | exit(2); | |
679 | } | |
680 | devmode = opt; | |
681 | continue; | |
feb716e9 NB |
682 | case O(MISC,'t'): |
683 | test = 1; | |
684 | continue; | |
e0d19036 | 685 | |
bd526cee NB |
686 | case O(MISC, 22): |
687 | if (devmode != 'E') { | |
688 | fprintf(stderr, Name ": --sparc2.2 only allowed with --examine\n"); | |
689 | exit(2); | |
690 | } | |
691 | SparcAdjust = 1; | |
692 | continue; | |
c82f047c NB |
693 | |
694 | case O(ASSEMBLE,'b'): /* here we simply set the bitmap file */ | |
695 | if (!optarg) { | |
696 | fprintf(stderr, Name ": bitmap file needed with -b in --assemble mode\n"); | |
697 | exit(2); | |
698 | } | |
699 | bitmap_fd = open(optarg, O_RDWR); | |
700 | if (!*optarg || bitmap_fd < 0) { | |
701 | fprintf(stderr, Name ": cannot open bitmap file %s: %s\n", optarg, strerror(errno)); | |
702 | exit(2); | |
703 | } | |
704 | ident.bitmap_fd = bitmap_fd; /* for Assemble */ | |
705 | continue; | |
706 | case O(BUILD,'b'): | |
707 | case O(CREATE,'b'): /* here we create the bitmap */ | |
708 | bitmap_file = optarg; | |
709 | continue; | |
710 | ||
711 | case O(BUILD,4): | |
712 | case O(CREATE,4): /* bitmap chunksize */ | |
713 | bitmap_chunk = strtol(optarg, &c, 10); | |
714 | if (!optarg[0] || *c || bitmap_chunk < 0 || | |
715 | bitmap_chunk & (bitmap_chunk - 1)) { | |
716 | fprintf(stderr, Name ": invalid bitmap chunksize: %s\n", | |
717 | optarg); | |
718 | exit(2); | |
719 | } | |
720 | /* convert K to B, chunk of 0K means 512B */ | |
721 | bitmap_chunk = bitmap_chunk ? bitmap_chunk * 1024 : 512; | |
722 | continue; | |
52826846 NB |
723 | } |
724 | /* We have now processed all the valid options. Anything else is | |
725 | * an error | |
726 | */ | |
e0d19036 NB |
727 | fprintf(stderr, Name ": option %c not valid in %s mode\n", |
728 | opt, map_num(modes, mode)); | |
64c4757e | 729 | exit(2); |
52826846 NB |
730 | |
731 | } | |
732 | ||
0320ea45 NB |
733 | if (!mode && devs_found) { |
734 | mode = MISC; | |
735 | devmode = 'Q'; | |
736 | if (devlist->disposition == 0) | |
737 | devlist->disposition = devmode; | |
738 | } | |
52826846 NB |
739 | if (!mode) { |
740 | fputs(Usage, stderr); | |
64c4757e | 741 | exit(2); |
64c4757e | 742 | } |
52826846 NB |
743 | /* Ok, got the option parsing out of the way |
744 | * hopefully it's mostly right but there might be some stuff | |
745 | * missing | |
746 | * | |
e0d19036 | 747 | * That is mosty checked in the per-mode stuff but... |
52826846 NB |
748 | * |
749 | * For @,B,C and A without -s, the first device listed must be an md device | |
750 | * we check that here and open it. | |
64c4757e | 751 | */ |
52826846 | 752 | |
dd0781e5 NB |
753 | if (mode==MANAGE || mode == BUILD || mode == CREATE || mode == GROW || |
754 | (mode == ASSEMBLE && ! scan)) { | |
52826846 NB |
755 | if (devs_found < 1) { |
756 | fprintf(stderr, Name ": an md device must be given in this mode\n"); | |
757 | exit(2); | |
758 | } | |
dd0781e5 NB |
759 | if ((int)ident.super_minor == -2 && autof) { |
760 | fprintf(stderr, Name ": --super-minor=dev is incompatible with --auto\n"); | |
761 | exit(2); | |
762 | } | |
763 | mdfd = open_mddev(devlist->devname, autof); | |
52826846 NB |
764 | if (mdfd < 0) |
765 | exit(1); | |
98c6faba | 766 | if ((int)ident.super_minor == -2) { |
d013a55e NB |
767 | struct stat stb; |
768 | fstat(mdfd, &stb); | |
0df46c2a | 769 | ident.super_minor = minor(stb.st_rdev); |
d013a55e | 770 | } |
64c4757e | 771 | } |
52826846 | 772 | |
c82f047c | 773 | |
52826846 NB |
774 | rv = 0; |
775 | switch(mode) { | |
e0d19036 | 776 | case MANAGE: |
52826846 NB |
777 | /* readonly, add/remove, readwrite, runstop */ |
778 | if (readonly>0) | |
cd29a5c8 | 779 | rv = Manage_ro(devlist->devname, mdfd, readonly); |
52826846 | 780 | if (!rv && devs_found>1) |
cd29a5c8 NB |
781 | rv = Manage_subdevs(devlist->devname, mdfd, |
782 | devlist->next); | |
52826846 | 783 | if (!rv && readonly < 0) |
cd29a5c8 | 784 | rv = Manage_ro(devlist->devname, mdfd, readonly); |
52826846 | 785 | if (!rv && runstop) |
cd29a5c8 | 786 | rv = Manage_runstop(devlist->devname, mdfd, runstop); |
52826846 | 787 | break; |
e0d19036 | 788 | case ASSEMBLE: |
d013a55e | 789 | if (devs_found == 1 && ident.uuid_set == 0 && |
98c6faba | 790 | ident.super_minor == UnSet && !scan ) { |
d013a55e NB |
791 | /* Only a device has been given, so get details from config file */ |
792 | mddev_ident_t array_ident = conf_get_ident(configfile, devlist->devname); | |
b5e64645 NB |
793 | if (array_ident == NULL) { |
794 | fprintf(stderr, Name ": %s not identified in config file.\n", | |
795 | devlist->devname); | |
d013a55e | 796 | rv |= 1; |
b5e64645 NB |
797 | } else { |
798 | mdfd = open_mddev(devlist->devname, array_ident->autof); | |
799 | if (mdfd < 0) | |
d013a55e | 800 | rv |= 1; |
b5e64645 | 801 | else { |
f9ce90ba | 802 | rv |= Assemble(ss, devlist->devname, mdfd, array_ident, configfile, |
d013a55e NB |
803 | NULL, |
804 | readonly, runstop, update, verbose, force); | |
b5e64645 NB |
805 | close(mdfd); |
806 | } | |
d013a55e NB |
807 | } |
808 | } else if (!scan) | |
f9ce90ba | 809 | rv = Assemble(ss, devlist->devname, mdfd, &ident, configfile, |
cd29a5c8 | 810 | devlist->next, |
5787fa49 NB |
811 | readonly, runstop, update, verbose, force); |
812 | else if (devs_found>0) { | |
813 | if (update && devs_found > 1) { | |
814 | fprintf(stderr, Name ": can only update a single array at a time\n"); | |
815 | exit(1); | |
816 | } | |
cd29a5c8 NB |
817 | for (dv = devlist ; dv ; dv=dv->next) { |
818 | mddev_ident_t array_ident = conf_get_ident(configfile, dv->devname); | |
52826846 NB |
819 | if (array_ident == NULL) { |
820 | fprintf(stderr, Name ": %s not identified in config file.\n", | |
cd29a5c8 | 821 | dv->devname); |
52826846 NB |
822 | rv |= 1; |
823 | continue; | |
824 | } | |
b5e64645 NB |
825 | mdfd = open_mddev(dv->devname, array_ident->autof); |
826 | if (mdfd < 0) { | |
827 | rv |= 1; | |
828 | continue; | |
829 | } | |
f9ce90ba | 830 | rv |= Assemble(ss, dv->devname, mdfd, array_ident, configfile, |
cd29a5c8 | 831 | NULL, |
5787fa49 | 832 | readonly, runstop, update, verbose, force); |
b5e64645 | 833 | close(mdfd); |
52826846 | 834 | } |
5787fa49 | 835 | } else { |
52826846 NB |
836 | mddev_ident_t array_list = conf_get_ident(configfile, NULL); |
837 | if (!array_list) { | |
838 | fprintf(stderr, Name ": No arrays found in config file\n"); | |
839 | rv = 1; | |
840 | } else | |
841 | for (; array_list; array_list = array_list->next) { | |
cd29a5c8 | 842 | mdu_array_info_t array; |
dd0781e5 | 843 | mdfd = open_mddev(array_list->devname, array_list->autof); |
52826846 NB |
844 | if (mdfd < 0) { |
845 | rv |= 1; | |
846 | continue; | |
847 | } | |
cd29a5c8 NB |
848 | if (ioctl(mdfd, GET_ARRAY_INFO, &array)>=0) |
849 | /* already assembled, skip */ | |
43fc1676 NB |
850 | ; |
851 | else | |
f9ce90ba | 852 | rv |= Assemble(ss, array_list->devname, mdfd, |
43fc1676 NB |
853 | array_list, configfile, |
854 | NULL, | |
855 | readonly, runstop, NULL, verbose, force); | |
856 | close(mdfd); | |
52826846 NB |
857 | } |
858 | } | |
859 | break; | |
e0d19036 | 860 | case BUILD: |
c82f047c NB |
861 | if (bitmap_chunk == UnSet) bitmap_chunk = DEFAULT_BITMAP_CHUNK; |
862 | if (delay == 0) delay = DEFAULT_BITMAP_DELAY; | |
863 | if (bitmap_file) { | |
864 | bitmap_fd = open(bitmap_file, O_RDWR,0); | |
865 | if (bitmap_fd < 0 && errno != ENOENT) { | |
866 | perror(Name ": cannot create bitmap file"); | |
867 | rv |= 1; | |
868 | break; | |
869 | } | |
870 | if (bitmap_fd < 0) { | |
871 | bitmap_fd = CreateBitmap(bitmap_file, force, NULL, | |
872 | bitmap_chunk, delay, size); | |
873 | } | |
874 | } | |
875 | rv = Build(devlist->devname, mdfd, chunk, level, layout, | |
876 | raiddisks, devlist->next, assume_clean, | |
877 | bitmap_file, bitmap_chunk, delay); | |
52826846 | 878 | break; |
e0d19036 | 879 | case CREATE: |
c82f047c NB |
880 | if (bitmap_chunk == UnSet) bitmap_chunk = DEFAULT_BITMAP_CHUNK; |
881 | if (delay == 0) delay = DEFAULT_BITMAP_DELAY; | |
f9ce90ba | 882 | if (ss == NULL) { |
82d9eba6 NB |
883 | for(i=0; !ss && superlist[i]; i++) |
884 | ss = superlist[i]->match_metadata_desc("default"); | |
f9ce90ba NB |
885 | } |
886 | if (!ss) { | |
887 | fprintf(stderr, Name ": internal error - no default metadata style\n"); | |
888 | exit(2); | |
889 | } | |
890 | ||
891 | rv = Create(ss, devlist->devname, mdfd, chunk, level, layout, size<0 ? 0 : size, | |
52826846 | 892 | raiddisks, sparedisks, |
c82f047c NB |
893 | devs_found-1, devlist->next, runstop, verbose, force, |
894 | bitmap_file, bitmap_chunk, delay); | |
52826846 | 895 | break; |
e0d19036 NB |
896 | case MISC: |
897 | ||
898 | if (devmode == 'E') { | |
899 | if (devlist == NULL && !scan) { | |
900 | fprintf(stderr, Name ": No devices to examine\n"); | |
901 | exit(2); | |
902 | } | |
903 | if (devlist == NULL) | |
904 | devlist = conf_get_devs(configfile); | |
905 | if (devlist == NULL) { | |
c913b90e | 906 | fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile); |
e0d19036 NB |
907 | exit(1); |
908 | } | |
82d9eba6 | 909 | rv = Examine(devlist, scan?!verbose:brief, scan, SparcAdjust, ss); |
e0d19036 NB |
910 | } else { |
911 | if (devlist == NULL) { | |
912 | if ((devmode == 'S' ||devmode=='D') && scan) { | |
913 | /* apply to all devices in /proc/mdstat */ | |
dd0781e5 | 914 | struct mdstat_ent *ms = mdstat_read(0); |
e0d19036 | 915 | struct mdstat_ent *e; |
d17c1f8c NB |
916 | if (devmode == 'S') { |
917 | /* reverse order so that arrays made of arrays are stopped properly */ | |
918 | struct mdstat_ent *sm = NULL; | |
919 | while ((e=ms) != NULL) { | |
920 | ms = e->next; | |
921 | e->next = sm; | |
922 | sm = e; | |
923 | } | |
924 | ms = sm; | |
925 | } | |
e0d19036 NB |
926 | for (e=ms ; e ; e=e->next) { |
927 | char *name = get_md_name(e->devnum); | |
928 | ||
929 | if (!name) { | |
930 | fprintf(stderr, Name ": cannot find device file for %s\n", | |
931 | e->dev); | |
932 | continue; | |
933 | } | |
934 | if (devmode == 'D') | |
feb716e9 | 935 | rv |= Detail(name, !verbose, test); |
e0d19036 | 936 | else if (devmode=='S') { |
dd0781e5 | 937 | mdfd = open_mddev(name, 0); |
43fc1676 | 938 | if (mdfd >= 0) { |
e0d19036 | 939 | rv |= Manage_runstop(name, mdfd, -1); |
43fc1676 NB |
940 | close(mdfd); |
941 | } | |
e0d19036 NB |
942 | } |
943 | put_md_name(name); | |
944 | } | |
945 | } else { | |
946 | fprintf(stderr, Name ": No devices given.\n"); | |
947 | exit(2); | |
948 | } | |
949 | } | |
950 | for (dv=devlist ; dv; dv=dv->next) { | |
951 | switch(dv->disposition) { | |
952 | case 'D': | |
feb716e9 | 953 | rv |= Detail(dv->devname, brief, test); continue; |
e0d19036 NB |
954 | case 'K': /* Zero superblock */ |
955 | rv |= Kill(dv->devname, force); continue; | |
956 | case 'Q': | |
957 | rv |= Query(dv->devname); continue; | |
c82f047c NB |
958 | case 'X': |
959 | rv |= ExamineBitmap(dv->devname, brief); continue; | |
e0d19036 | 960 | } |
dd0781e5 | 961 | mdfd = open_mddev(dv->devname, 0); |
43fc1676 | 962 | if (mdfd>=0) { |
e0d19036 NB |
963 | switch(dv->disposition) { |
964 | case 'R': | |
965 | rv |= Manage_runstop(dv->devname, mdfd, 1); break; | |
966 | case 'S': | |
967 | rv |= Manage_runstop(dv->devname, mdfd, -1); break; | |
968 | case 'o': | |
969 | rv |= Manage_ro(dv->devname, mdfd, 1); break; | |
970 | case 'w': | |
971 | rv |= Manage_ro(dv->devname, mdfd, -1); break; | |
972 | } | |
43fc1676 NB |
973 | close(mdfd); |
974 | } | |
e0d19036 | 975 | } |
cd29a5c8 | 976 | } |
9a9dab36 | 977 | break; |
e0d19036 | 978 | case MONITOR: |
e0d19036 NB |
979 | if (!devlist && !scan) { |
980 | fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n"); | |
981 | rv = 1; | |
982 | break; | |
983 | } | |
b5e64645 NB |
984 | if (pidfile && !daemonise) { |
985 | fprintf(stderr, Name ": Cannot write a pid file when not in daemon mode\n"); | |
986 | rv = 1; | |
987 | break; | |
988 | } | |
d013a55e | 989 | rv= Monitor(devlist, mailaddr, program, |
b5e64645 | 990 | delay?delay:60, daemonise, scan, oneshot, configfile, test, pidfile); |
9a9dab36 | 991 | break; |
dd0781e5 NB |
992 | |
993 | case GROW: | |
994 | if (devs_found > 1) { | |
e5329c37 NB |
995 | |
996 | /* must be '-a'. */ | |
997 | if (size >= 0 || raiddisks) { | |
998 | fprintf(stderr, Name ": --size, --raiddisks, and --add are exclusing in --grow mode\n"); | |
999 | rv = 1; | |
1000 | break; | |
1001 | } | |
1002 | for (dv=devlist->next; dv ; dv=dv->next) { | |
1003 | rv = Grow_Add_device(devlist->devname, mdfd, dv->devname); | |
1004 | if (rv) | |
1005 | break; | |
1006 | } | |
b5e64645 NB |
1007 | } else if ((size >= 0) + (raiddisks != 0) + (layout != UnSet) > 1) { |
1008 | fprintf(stderr, Name ": can change at most one of size, raiddisks, and layout\n"); | |
dd0781e5 NB |
1009 | rv = 1; |
1010 | break; | |
b5e64645 NB |
1011 | } else if (layout != UnSet) |
1012 | rv = Manage_reconfig(devlist->devname, mdfd, layout); | |
1013 | else if (size >= 0 || raiddisks) | |
e5329c37 | 1014 | rv = Manage_resize(devlist->devname, mdfd, size, raiddisks); |
b5e64645 NB |
1015 | else |
1016 | fprintf(stderr, Name ": no changes to --grow\n"); | |
dd0781e5 | 1017 | break; |
64c4757e | 1018 | } |
52826846 | 1019 | exit(rv); |
64c4757e | 1020 | } |