]>
Commit | Line | Data |
---|---|---|
64c4757e | 1 | /* |
9a9dab36 | 2 | * mdadm - manage Linux "md" devices aka RAID arrays. |
64c4757e | 3 | * |
4f589ad0 | 4 | * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de> |
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 | 28 | * |
aba69144 | 29 | * Additions for bitmap and write-behind RAID options, Copyright (C) 2003-2004, |
c82f047c | 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 *c; |
44 | int rv; | |
f9ce90ba | 45 | int i; |
52826846 NB |
46 | |
47 | int chunk = 0; | |
5dd497ee | 48 | long long size = -1; |
98c6faba NB |
49 | int level = UnSet; |
50 | int layout = UnSet; | |
52826846 | 51 | int raiddisks = 0; |
e27d562b | 52 | int max_disks = MD_SB_DISKS; /* just a default */ |
52826846 NB |
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; | |
dfd4d8ee | 62 | int write_behind = 0; |
c82f047c NB |
63 | int bitmap_fd = -1; |
64 | char *bitmap_file = NULL; | |
06b0d786 | 65 | char *backup_file = NULL; |
c82f047c | 66 | int bitmap_chunk = UnSet; |
bd526cee | 67 | int SparcAdjust = 0; |
cd29a5c8 NB |
68 | mddev_dev_t devlist = NULL; |
69 | mddev_dev_t *devlistend = & devlist; | |
70 | mddev_dev_t dv; | |
52826846 NB |
71 | int devs_found = 0; |
72 | int verbose = 0; | |
dab6685f | 73 | int quiet = 0; |
cd29a5c8 | 74 | int brief = 0; |
52826846 | 75 | int force = 0; |
feb716e9 | 76 | int test = 0; |
54bad364 | 77 | int export = 0; |
dd0781e5 | 78 | int assume_clean = 0; |
38098016 | 79 | char *symlinks = NULL; |
f1ae21c4 NB |
80 | /* autof indicates whether and how to create device node. |
81 | * bottom 3 bits are style. Rest (when shifted) are number of parts | |
82 | * 0 - unset | |
83 | * 1 - don't create (no) | |
84 | * 2 - if is_standard, then create (yes) | |
85 | * 3 - create as 'md' - reject is_standard mdp (md) | |
86 | * 4 - create as 'mdp' - reject is_standard md (mdp) | |
87 | * 5 - default to md if not is_standard (md in config file) | |
88 | * 6 - default to mdp if not is_standard (part, or mdp in config file) | |
89 | */ | |
90 | int autof = 0; | |
52826846 | 91 | |
997aed5d | 92 | char *homehost = NULL; |
05697ec1 | 93 | char sys_hostname[256]; |
0ac91628 | 94 | int require_homehost = 1; |
52826846 NB |
95 | char *mailaddr = NULL; |
96 | char *program = NULL; | |
97 | int delay = 0; | |
d013a55e | 98 | int daemonise = 0; |
b5e64645 | 99 | char *pidfile = NULL; |
aa88f531 | 100 | int oneshot = 0; |
82d9eba6 | 101 | struct supertype *ss = NULL; |
dfd4d8ee | 102 | int writemostly = 0; |
fe80f49b | 103 | int re_add = 0; |
c06487ce | 104 | char *shortopt = short_options; |
773135f5 | 105 | int dosyslog = 0; |
8382f19b | 106 | int rebuild_map = 0; |
589395d6 | 107 | int auto_update_home = 0; |
52826846 | 108 | |
e5329c37 | 109 | int copies; |
3d4064cc | 110 | int print_help = 0; |
0047d254 | 111 | FILE *outf; |
e5329c37 | 112 | |
52826846 NB |
113 | int mdfd = -1; |
114 | ||
dfe47e00 NB |
115 | srandom(time(0) ^ getpid()); |
116 | ||
52826846 | 117 | ident.uuid_set=0; |
98c6faba NB |
118 | ident.level = UnSet; |
119 | ident.raid_disks = UnSet; | |
120 | ident.super_minor= UnSet; | |
52826846 | 121 | ident.devices=0; |
a75f2fbc NB |
122 | ident.spare_group = NULL; |
123 | ident.autof = 0; | |
a825febc | 124 | ident.st = NULL; |
c82f047c | 125 | ident.bitmap_fd = -1; |
7ef02d01 | 126 | ident.bitmap_file = NULL; |
947fd4dd | 127 | ident.name[0] = 0; |
aef35714 N |
128 | ident.container = NULL; |
129 | ident.member = NULL; | |
52826846 | 130 | |
e0d19036 NB |
131 | while ((option_index = -1) , |
132 | (opt=getopt_long(argc, argv, | |
c06487ce | 133 | shortopt, long_options, |
e0d19036 NB |
134 | &option_index)) != -1) { |
135 | int newmode = mode; | |
35904960 | 136 | /* firstly, some mode-independent options */ |
52826846 | 137 | switch(opt) { |
52826846 | 138 | case 'h': |
aba69144 | 139 | if (option_index > 0 && |
56eedc1a | 140 | strcmp(long_options[option_index].name, "help-options")==0) |
3d4064cc | 141 | print_help = 2; |
56eedc1a | 142 | else |
3d4064cc NB |
143 | print_help = 1; |
144 | continue; | |
52826846 NB |
145 | |
146 | case 'V': | |
147 | fputs(Version, stderr); | |
148 | exit(0); | |
149 | ||
22892d56 | 150 | case 'v': verbose++; |
52826846 NB |
151 | continue; |
152 | ||
dab6685f NB |
153 | case 'q': quiet++; |
154 | continue; | |
155 | ||
c82f047c | 156 | case 'b': |
f5e166fe | 157 | if (mode == ASSEMBLE || mode == BUILD || mode == CREATE || mode == GROW) |
c82f047c NB |
158 | break; /* b means bitmap */ |
159 | brief = 1; | |
c06487ce NB |
160 | if (optarg) { |
161 | fprintf(stderr, Name ": -b cannot have any extra immediately after it, sorry.\n"); | |
162 | exit(2); | |
163 | } | |
cd29a5c8 NB |
164 | continue; |
165 | ||
54bad364 KS |
166 | case 'Y': export++; |
167 | continue; | |
168 | ||
997aed5d | 169 | case HomeHost: |
0ac91628 N |
170 | if (strcasecmp(optarg, "<ignore>") == 0) |
171 | require_homehost = 0; | |
172 | else | |
173 | homehost = optarg; | |
997aed5d NB |
174 | continue; |
175 | ||
e0d19036 NB |
176 | case ':': |
177 | case '?': | |
178 | fputs(Usage, stderr); | |
179 | exit(2); | |
180 | } | |
181 | /* second, figure out the mode. | |
182 | * Some options force the mode. Others | |
aba69144 | 183 | * set the mode if it isn't already |
e0d19036 NB |
184 | */ |
185 | ||
186 | switch(opt) { | |
187 | case '@': /* just incase they say --manage */ | |
c06487ce | 188 | newmode = MANAGE; |
024768c4 | 189 | shortopt = short_bitmap_options; |
c06487ce | 190 | break; |
e0d19036 NB |
191 | case 'a': |
192 | case 'r': | |
193 | case 'f': | |
41a3b72a | 194 | case ReAdd: /* re-add */ |
c06487ce NB |
195 | if (!mode) { |
196 | newmode = MANAGE; | |
024768c4 | 197 | shortopt = short_bitmap_options; |
c06487ce | 198 | } |
0320ea45 | 199 | break; |
e0d19036 | 200 | |
c06487ce NB |
201 | case 'A': newmode = ASSEMBLE; shortopt = short_bitmap_auto_options; break; |
202 | case 'B': newmode = BUILD; shortopt = short_bitmap_auto_options; break; | |
203 | case 'C': newmode = CREATE; shortopt = short_bitmap_auto_options; break; | |
e0d19036 | 204 | case 'F': newmode = MONITOR;break; |
024768c4 DL |
205 | case 'G': newmode = GROW; |
206 | shortopt = short_bitmap_options; | |
207 | break; | |
95b79df0 DW |
208 | case 'I': newmode = INCREMENTAL; |
209 | shortopt = short_bitmap_auto_options; break; | |
1f48664b NB |
210 | case AutoDetect: |
211 | newmode = AUTODETECT; break; | |
e0d19036 NB |
212 | |
213 | case '#': | |
214 | case 'D': | |
215 | case 'E': | |
c82f047c | 216 | case 'X': |
e0d19036 NB |
217 | case 'Q': newmode = MISC; break; |
218 | case 'R': | |
219 | case 'S': | |
220 | case 'o': | |
221 | case 'w': | |
b90c0e9a | 222 | case 'W': |
1770662b | 223 | case Waitclean: |
4cce4069 | 224 | case DetailPlatform: |
e0d19036 NB |
225 | case 'K': if (!mode) newmode = MISC; break; |
226 | } | |
227 | if (mode && newmode == mode) { | |
228 | /* everybody happy ! */ | |
229 | } else if (mode && newmode != mode) { | |
230 | /* not allowed.. */ | |
231 | fprintf(stderr, Name ": "); | |
232 | if (option_index >= 0) | |
233 | fprintf(stderr, "--%s", long_options[option_index].name); | |
234 | else | |
235 | fprintf(stderr, "-%c", opt); | |
e6b64cd0 | 236 | fprintf(stderr, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n", |
e0d19036 NB |
237 | map_num(modes, newmode), |
238 | map_num(modes, mode)); | |
239 | exit(2); | |
240 | } else if (!mode && newmode) { | |
241 | mode = newmode; | |
242 | } else { | |
243 | /* special case of -c --help */ | |
aba69144 | 244 | if (opt == 'c' && |
e0d19036 NB |
245 | ( strncmp(optarg, "--h", 3)==0 || |
246 | strncmp(optarg, "-h", 2)==0)) { | |
0047d254 | 247 | fputs(Help_config, stdout); |
e0d19036 | 248 | exit(0); |
cd29a5c8 | 249 | } |
0320ea45 NB |
250 | |
251 | /* If first option is a device, don't force the mode yet */ | |
252 | if (opt == 1) { | |
253 | if (devs_found == 0) { | |
254 | dv = malloc(sizeof(*dv)); | |
255 | if (dv == NULL) { | |
256 | fprintf(stderr, Name ": malloc failed\n"); | |
257 | exit(3); | |
258 | } | |
259 | dv->devname = optarg; | |
260 | dv->disposition = devmode; | |
dfd4d8ee | 261 | dv->writemostly = writemostly; |
fe80f49b | 262 | dv->re_add = re_add; |
da6b5ca9 | 263 | dv->used = 0; |
9008ed1c | 264 | dv->content = NULL; |
0320ea45 NB |
265 | dv->next = NULL; |
266 | *devlistend = dv; | |
267 | devlistend = &dv->next; | |
aba69144 | 268 | |
0320ea45 NB |
269 | devs_found++; |
270 | continue; | |
271 | } | |
272 | /* No mode yet, and this is the second device ... */ | |
273 | fprintf(stderr, Name ": An option must be given to set the mode before a second device is listed\n"); | |
274 | exit(2); | |
275 | } | |
e0d19036 | 276 | if (option_index >= 0) |
0320ea45 | 277 | fprintf(stderr, Name ": --%s", long_options[option_index].name); |
e0d19036 | 278 | else |
0320ea45 NB |
279 | fprintf(stderr, Name ": -%c", opt); |
280 | fprintf(stderr, " does not set the mode, and so cannot be the first option.\n"); | |
e0d19036 NB |
281 | exit(2); |
282 | } | |
283 | ||
284 | /* if we just set the mode, then done */ | |
285 | switch(opt) { | |
286 | case '@': | |
287 | case '#': | |
288 | case 'A': | |
289 | case 'B': | |
290 | case 'C': | |
291 | case 'F': | |
dd0781e5 | 292 | case 'G': |
8382f19b | 293 | case 'I': |
1f48664b | 294 | case AutoDetect: |
e0d19036 NB |
295 | continue; |
296 | } | |
297 | if (opt == 1) { | |
298 | /* an undecorated option - must be a device name. | |
299 | */ | |
cd29a5c8 | 300 | if (devs_found > 0 && mode == '@' && !devmode) { |
dd0781e5 | 301 | fprintf(stderr, Name ": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg); |
52826846 NB |
302 | exit(2); |
303 | } | |
e5329c37 NB |
304 | if (devs_found > 0 && mode == 'G' && !devmode) { |
305 | fprintf(stderr, Name ": Must give one of -a for devices do add: %s\n", optarg); | |
306 | exit(2); | |
307 | } | |
cd29a5c8 NB |
308 | dv = malloc(sizeof(*dv)); |
309 | if (dv == NULL) { | |
310 | fprintf(stderr, Name ": malloc failed\n"); | |
311 | exit(3); | |
312 | } | |
313 | dv->devname = optarg; | |
314 | dv->disposition = devmode; | |
dfd4d8ee | 315 | dv->writemostly = writemostly; |
fe80f49b | 316 | dv->re_add = re_add; |
9008ed1c N |
317 | dv->used = 0; |
318 | dv->content = NULL; | |
cd29a5c8 NB |
319 | dv->next = NULL; |
320 | *devlistend = dv; | |
321 | devlistend = &dv->next; | |
aba69144 | 322 | |
52826846 NB |
323 | devs_found++; |
324 | continue; | |
682c7051 | 325 | } |
682c7051 | 326 | |
52826846 NB |
327 | /* We've got a mode, and opt is now something else which |
328 | * could depend on the mode */ | |
329 | #define O(a,b) ((a<<8)|b) | |
330 | switch (O(mode,opt)) { | |
e0d19036 NB |
331 | case O(CREATE,'c'): |
332 | case O(BUILD,'c'): /* chunk or rounding */ | |
52826846 NB |
333 | if (chunk) { |
334 | fprintf(stderr, Name ": chunk/rounding may only be specified once. " | |
335 | "Second value is %s.\n", optarg); | |
336 | exit(2); | |
337 | } | |
338 | chunk = strtol(optarg, &c, 10); | |
339 | if (!optarg[0] || *c || chunk<4 || ((chunk-1)&chunk)) { | |
340 | fprintf(stderr, Name ": invalid chunk/rounding value: %s\n", | |
341 | optarg); | |
342 | exit(2); | |
343 | } | |
344 | continue; | |
64c4757e | 345 | |
589395d6 NB |
346 | case O(ASSEMBLE,AutoHomeHost): |
347 | auto_update_home = 1; | |
348 | continue; | |
8382f19b | 349 | case O(INCREMENTAL, 'e'): |
f9ce90ba NB |
350 | case O(CREATE,'e'): |
351 | case O(ASSEMBLE,'e'): | |
352 | case O(MISC,'e'): /* set metadata (superblock) information */ | |
353 | if (ss) { | |
354 | fprintf(stderr, Name ": metadata information already given\n"); | |
355 | exit(2); | |
356 | } | |
aba69144 | 357 | for(i=0; !ss && superlist[i]; i++) |
82d9eba6 NB |
358 | ss = superlist[i]->match_metadata_desc(optarg); |
359 | ||
f9ce90ba NB |
360 | if (!ss) { |
361 | fprintf(stderr, Name ": unrecognised metadata identifier: %s\n", optarg); | |
362 | exit(2); | |
363 | } | |
ea329559 | 364 | max_disks = ss->max_devs; |
f9ce90ba NB |
365 | continue; |
366 | ||
dfd4d8ee NB |
367 | case O(MANAGE,'W'): |
368 | case O(BUILD,'W'): | |
369 | case O(CREATE,'W'): | |
370 | /* set write-mostly for following devices */ | |
371 | writemostly = 1; | |
372 | continue; | |
373 | ||
b3d31955 N |
374 | case O(MANAGE,'w'): |
375 | /* clear write-mostly for following devices */ | |
376 | writemostly = 2; | |
377 | continue; | |
378 | ||
379 | ||
dd0781e5 | 380 | case O(GROW,'z'): |
e0d19036 | 381 | case O(CREATE,'z'): /* size */ |
dd0781e5 | 382 | if (size >= 0) { |
52826846 NB |
383 | fprintf(stderr, Name ": size may only be specified once. " |
384 | "Second value is %s.\n", optarg); | |
385 | exit(2); | |
386 | } | |
dd0781e5 NB |
387 | if (strcmp(optarg, "max")==0) |
388 | size = 0; | |
389 | else { | |
5dd497ee | 390 | size = strtoll(optarg, &c, 10); |
dd0781e5 NB |
391 | if (!optarg[0] || *c || size < 4) { |
392 | fprintf(stderr, Name ": invalid size: %s\n", | |
393 | optarg); | |
394 | exit(2); | |
395 | } | |
52826846 NB |
396 | } |
397 | continue; | |
64c4757e | 398 | |
b5e64645 | 399 | case O(GROW,'l'): /* hack - needed to understand layout */ |
e0d19036 NB |
400 | case O(CREATE,'l'): |
401 | case O(BUILD,'l'): /* set raid level*/ | |
98c6faba | 402 | if (level != UnSet) { |
52826846 NB |
403 | fprintf(stderr, Name ": raid level may only be set once. " |
404 | "Second value is %s.\n", optarg); | |
405 | exit(2); | |
406 | } | |
407 | level = map_name(pers, optarg); | |
98c6faba | 408 | if (level == UnSet) { |
52826846 NB |
409 | fprintf(stderr, Name ": invalid raid level: %s\n", |
410 | optarg); | |
411 | exit(2); | |
412 | } | |
b5e64645 | 413 | if (level != 0 && level != -1 && level != 1 && level != -4 && level != -5 && mode == BUILD) { |
52826846 NB |
414 | fprintf(stderr, Name ": Raid level %s not permitted with --build.\n", |
415 | optarg); | |
416 | exit(2); | |
417 | } | |
e0d19036 | 418 | if (sparedisks > 0 && level < 1 && level >= -1) { |
b83d95f3 | 419 | fprintf(stderr, Name ": raid level %s is incompatible with spare-devices setting.\n", |
52826846 NB |
420 | optarg); |
421 | exit(2); | |
422 | } | |
cd29a5c8 | 423 | ident.level = level; |
52826846 | 424 | continue; |
64c4757e | 425 | |
e0d19036 | 426 | case O(CREATE,'p'): /* raid5 layout */ |
b5e64645 NB |
427 | case O(BUILD,'p'): /* faulty layout */ |
428 | case O(GROW, 'p'): /* faulty reconfig */ | |
e5329c37 | 429 | if (layout != UnSet) { |
52826846 NB |
430 | fprintf(stderr,Name ": layout may only be sent once. " |
431 | "Second value was %s\n", optarg); | |
432 | exit(2); | |
433 | } | |
434 | switch(level) { | |
435 | default: | |
56eb10c0 | 436 | fprintf(stderr, Name ": layout not meaningful for %s arrays.\n", |
52826846 NB |
437 | map_num(pers, level)); |
438 | exit(2); | |
98c6faba | 439 | case UnSet: |
52826846 NB |
440 | fprintf(stderr, Name ": raid level must be given before layout.\n"); |
441 | exit(2); | |
442 | ||
443 | case 5: | |
444 | layout = map_name(r5layout, optarg); | |
98c6faba | 445 | if (layout==UnSet) { |
52826846 NB |
446 | fprintf(stderr, Name ": layout %s not understood for raid5.\n", |
447 | optarg); | |
448 | exit(2); | |
449 | } | |
450 | break; | |
b640a252 N |
451 | case 6: |
452 | layout = map_name(r6layout, optarg); | |
453 | if (layout==UnSet) { | |
454 | fprintf(stderr, Name ": layout %s not understood for raid6.\n", | |
455 | optarg); | |
456 | exit(2); | |
457 | } | |
458 | break; | |
e5329c37 NB |
459 | |
460 | case 10: | |
b578481c NB |
461 | /* 'f', 'o' or 'n' followed by a number <= raid_disks */ |
462 | if ((optarg[0] != 'n' && optarg[0] != 'f' && optarg[0] != 'o') || | |
e5329c37 NB |
463 | (copies = strtoul(optarg+1, &cp, 10)) < 1 || |
464 | copies > 200 || | |
465 | *cp) { | |
b578481c | 466 | fprintf(stderr, Name ": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg); |
e5329c37 NB |
467 | exit(2); |
468 | } | |
469 | if (optarg[0] == 'n') | |
470 | layout = 256 + copies; | |
b578481c NB |
471 | else if (optarg[0] == 'o') |
472 | layout = 0x10000 + (copies<<8) + 1; | |
e5329c37 NB |
473 | else |
474 | layout = 1 + (copies<<8); | |
475 | break; | |
b5e64645 NB |
476 | case -5: /* Faulty |
477 | * modeNNN | |
478 | */ | |
aba69144 | 479 | |
b5e64645 NB |
480 | { |
481 | int ln = strcspn(optarg, "0123456789"); | |
482 | char *m = strdup(optarg); | |
483 | int mode; | |
484 | m[ln] = 0; | |
485 | mode = map_name(faultylayout, m); | |
486 | if (mode == UnSet) { | |
487 | fprintf(stderr, Name ": layout %s not understood for faulty.\n", | |
488 | optarg); | |
489 | exit(2); | |
490 | } | |
491 | layout = mode | (atoi(optarg+ln)<< ModeShift); | |
492 | } | |
52826846 NB |
493 | } |
494 | continue; | |
495 | ||
997aed5d NB |
496 | case O(CREATE,AssumeClean): |
497 | case O(BUILD,AssumeClean): /* assume clean */ | |
dd0781e5 NB |
498 | assume_clean = 1; |
499 | continue; | |
500 | ||
501 | case O(GROW,'n'): | |
e0d19036 NB |
502 | case O(CREATE,'n'): |
503 | case O(BUILD,'n'): /* number of raid disks */ | |
52826846 | 504 | if (raiddisks) { |
b83d95f3 | 505 | fprintf(stderr, Name ": raid-devices set twice: %d and %s\n", |
52826846 NB |
506 | raiddisks, optarg); |
507 | exit(2); | |
508 | } | |
509 | raiddisks = strtol(optarg, &c, 10); | |
e4c4352e | 510 | if (!optarg[0] || *c || raiddisks<=0) { |
b83d95f3 | 511 | fprintf(stderr, Name ": invalid number of raid devices: %s\n", |
52826846 NB |
512 | optarg); |
513 | exit(2); | |
514 | } | |
cd29a5c8 | 515 | ident.raid_disks = raiddisks; |
52826846 NB |
516 | continue; |
517 | ||
e0d19036 | 518 | case O(CREATE,'x'): /* number of spare (eXtra) discs */ |
52826846 | 519 | if (sparedisks) { |
b83d95f3 | 520 | fprintf(stderr,Name ": spare-devices set twice: %d and %s\n", |
52826846 NB |
521 | sparedisks, optarg); |
522 | exit(2); | |
523 | } | |
98c6faba | 524 | if (level != UnSet && level <= 0 && level >= -1) { |
b83d95f3 | 525 | fprintf(stderr, Name ": spare-devices setting is incompatible with raid level %d\n", |
52826846 NB |
526 | level); |
527 | exit(2); | |
528 | } | |
529 | sparedisks = strtol(optarg, &c, 10); | |
e4c4352e | 530 | if (!optarg[0] || *c || sparedisks < 0) { |
b83d95f3 | 531 | fprintf(stderr, Name ": invalid number of spare-devices: %s\n", |
52826846 NB |
532 | optarg); |
533 | exit(2); | |
534 | } | |
535 | continue; | |
dd0781e5 NB |
536 | |
537 | case O(CREATE,'a'): | |
538 | case O(BUILD,'a'): | |
95b79df0 | 539 | case O(INCREMENTAL,'a'): |
dd0781e5 | 540 | case O(ASSEMBLE,'a'): /* auto-creation of device node */ |
f1ae21c4 | 541 | autof = parse_auto(optarg, "--auto flag", 0); |
dd0781e5 NB |
542 | continue; |
543 | ||
38098016 NB |
544 | case O(CREATE,Symlinks): |
545 | case O(BUILD,Symlinks): | |
546 | case O(ASSEMBLE,Symlinks): /* auto creation of symlinks in /dev to /dev/md */ | |
547 | symlinks = optarg; | |
548 | continue; | |
549 | ||
aa88f531 | 550 | case O(BUILD,'f'): /* force honouring '-n 1' */ |
bd72c2b2 | 551 | case O(GROW,'f'): /* ditto */ |
e0d19036 NB |
552 | case O(CREATE,'f'): /* force honouring of device list */ |
553 | case O(ASSEMBLE,'f'): /* force assembly */ | |
554 | case O(MISC,'f'): /* force zero */ | |
52826846 NB |
555 | force=1; |
556 | continue; | |
557 | ||
558 | /* now for the Assemble options */ | |
3d3dd91e | 559 | case O(CREATE,'u'): /* uuid of array */ |
e0d19036 | 560 | case O(ASSEMBLE,'u'): /* uuid of array */ |
52826846 | 561 | if (ident.uuid_set) { |
cd29a5c8 | 562 | fprintf(stderr, Name ": uuid cannot be set twice. " |
52826846 NB |
563 | "Second value %s.\n", optarg); |
564 | exit(2); | |
565 | } | |
566 | if (parse_uuid(optarg, ident.uuid)) | |
567 | ident.uuid_set = 1; | |
568 | else { | |
569 | fprintf(stderr,Name ": Bad uuid: %s\n", optarg); | |
570 | exit(2); | |
571 | } | |
572 | continue; | |
573 | ||
947fd4dd NB |
574 | case O(CREATE,'N'): |
575 | case O(ASSEMBLE,'N'): | |
576 | if (ident.name[0]) { | |
577 | fprintf(stderr, Name ": name cannot be set twice. " | |
578 | "Second value %s.\n", optarg); | |
579 | exit(2); | |
580 | } | |
581 | if (strlen(optarg) > 32) { | |
582 | fprintf(stderr, Name ": name '%s' is too long, 32 chars max.\n", | |
583 | optarg); | |
584 | exit(2); | |
585 | } | |
586 | strcpy(ident.name, optarg); | |
587 | continue; | |
588 | ||
e0d19036 | 589 | case O(ASSEMBLE,'m'): /* super-minor for array */ |
98c6faba | 590 | if (ident.super_minor != UnSet) { |
cd29a5c8 NB |
591 | fprintf(stderr, Name ": super-minor cannot be set twice. " |
592 | "Second value: %s.\n", optarg); | |
593 | exit(2); | |
594 | } | |
d013a55e NB |
595 | if (strcmp(optarg, "dev")==0) |
596 | ident.super_minor = -2; | |
597 | else { | |
598 | ident.super_minor = strtoul(optarg, &cp, 10); | |
599 | if (!optarg[0] || *cp) { | |
600 | fprintf(stderr, Name ": Bad super-minor number: %s.\n", optarg); | |
601 | exit(2); | |
602 | } | |
cd29a5c8 NB |
603 | } |
604 | continue; | |
605 | ||
5787fa49 NB |
606 | case O(ASSEMBLE,'U'): /* update the superblock */ |
607 | if (update) { | |
608 | fprintf(stderr, Name ": Can only update one aspect of superblock, both %s and %s given.\n", | |
609 | update, optarg); | |
610 | exit(2); | |
611 | } | |
612 | update = optarg; | |
aba69144 | 613 | if (strcmp(update, "sparc2.2")==0) |
e5329c37 | 614 | continue; |
5787fa49 NB |
615 | if (strcmp(update, "super-minor") == 0) |
616 | continue; | |
feb716e9 NB |
617 | if (strcmp(update, "summaries")==0) |
618 | continue; | |
e5329c37 NB |
619 | if (strcmp(update, "resync")==0) |
620 | continue; | |
7d99579f NB |
621 | if (strcmp(update, "uuid")==0) |
622 | continue; | |
c4f12c13 NB |
623 | if (strcmp(update, "name")==0) |
624 | continue; | |
0237e0ca NB |
625 | if (strcmp(update, "homehost")==0) |
626 | continue; | |
bee8ec56 NB |
627 | if (strcmp(update, "devicesize")==0) |
628 | continue; | |
586ed405 NB |
629 | if (strcmp(update, "byteorder")==0) { |
630 | if (ss) { | |
631 | fprintf(stderr, Name ": must not set metadata type with --update=byteorder.\n"); | |
632 | exit(2); | |
633 | } | |
aba69144 | 634 | for(i=0; !ss && superlist[i]; i++) |
586ed405 NB |
635 | ss = superlist[i]->match_metadata_desc("0.swap"); |
636 | if (!ss) { | |
637 | fprintf(stderr, Name ": INTERNAL ERROR cannot find 0.swap\n"); | |
638 | exit(2); | |
639 | } | |
640 | ||
641 | continue; | |
642 | } | |
0047d254 NB |
643 | if (strcmp(update,"?") == 0 || |
644 | strcmp(update, "help") == 0) { | |
645 | outf = stdout; | |
646 | fprintf(outf, Name ": "); | |
647 | } else { | |
648 | outf = stderr; | |
649 | fprintf(outf, | |
650 | Name ": '--update=%s' is invalid. ", | |
651 | update); | |
652 | } | |
653 | fprintf(outf, "Valid --update options are:\n" | |
bee8ec56 NB |
654 | " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n" |
655 | " 'summaries', 'homehost', 'byteorder', 'devicesize'.\n"); | |
0047d254 | 656 | exit(outf == stdout ? 0 : 2); |
5787fa49 | 657 | |
fdb482f9 | 658 | case O(INCREMENTAL,NoDegraded): |
997aed5d | 659 | case O(ASSEMBLE,NoDegraded): /* --no-degraded */ |
0047d254 NB |
660 | runstop = -1; /* --stop isn't allowed for --assemble, |
661 | * so we overload slightly */ | |
b8a8ccf9 NB |
662 | continue; |
663 | ||
e0d19036 | 664 | case O(ASSEMBLE,'c'): /* config file */ |
7b187ed7 | 665 | case O(INCREMENTAL, 'c'): |
2d465520 | 666 | case O(MISC, 'c'): |
e0d19036 | 667 | case O(MONITOR,'c'): |
52826846 NB |
668 | if (configfile) { |
669 | fprintf(stderr, Name ": configfile cannot be set twice. " | |
670 | "Second value is %s.\n", optarg); | |
671 | exit(2); | |
672 | } | |
673 | configfile = optarg; | |
8aec876d | 674 | set_conffile(configfile); |
52826846 NB |
675 | /* FIXME possibly check that config file exists. Even parse it */ |
676 | continue; | |
e0d19036 NB |
677 | case O(ASSEMBLE,'s'): /* scan */ |
678 | case O(MISC,'s'): | |
679 | case O(MONITOR,'s'): | |
8382f19b | 680 | case O(INCREMENTAL,'s'): |
52826846 NB |
681 | scan = 1; |
682 | continue; | |
683 | ||
e0d19036 | 684 | case O(MONITOR,'m'): /* mail address */ |
52826846 NB |
685 | if (mailaddr) |
686 | fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n", | |
687 | optarg); | |
688 | else | |
689 | mailaddr = optarg; | |
690 | continue; | |
691 | ||
e0d19036 | 692 | case O(MONITOR,'p'): /* alert program */ |
52826846 NB |
693 | if (program) |
694 | fprintf(stderr, Name ": only specify one alter program. %s ignored.\n", | |
695 | optarg); | |
696 | else | |
697 | program = optarg; | |
698 | continue; | |
64c4757e | 699 | |
e0d19036 | 700 | case O(MONITOR,'d'): /* delay in seconds */ |
f5e166fe | 701 | case O(GROW, 'd'): |
c82f047c NB |
702 | case O(BUILD,'d'): /* delay for bitmap updates */ |
703 | case O(CREATE,'d'): | |
52826846 NB |
704 | if (delay) |
705 | fprintf(stderr, Name ": only specify delay once. %s ignored.\n", | |
706 | optarg); | |
707 | else { | |
708 | delay = strtol(optarg, &c, 10); | |
709 | if (!optarg[0] || *c || delay<1) { | |
710 | fprintf(stderr, Name ": invalid delay: %s\n", | |
711 | optarg); | |
712 | exit(2); | |
713 | } | |
714 | } | |
715 | continue; | |
d013a55e NB |
716 | case O(MONITOR,'f'): /* daemonise */ |
717 | daemonise = 1; | |
718 | continue; | |
b5e64645 NB |
719 | case O(MONITOR,'i'): /* pid */ |
720 | if (pidfile) | |
721 | fprintf(stderr, Name ": only specify one pid file. %s ignored.\n", | |
722 | optarg); | |
723 | else | |
724 | pidfile = optarg; | |
725 | continue; | |
aa88f531 NB |
726 | case O(MONITOR,'1'): /* oneshot */ |
727 | oneshot = 1; | |
728 | continue; | |
98c6faba NB |
729 | case O(MONITOR,'t'): /* test */ |
730 | test = 1; | |
731 | continue; | |
773135f5 | 732 | case O(MONITOR,'y'): /* log messages to syslog */ |
6ac8aac2 | 733 | openlog("mdadm", LOG_PID, SYSLOG_FACILITY); |
773135f5 NB |
734 | dosyslog = 1; |
735 | continue; | |
52826846 NB |
736 | |
737 | /* now the general management options. Some are applicable | |
738 | * to other modes. None have arguments. | |
739 | */ | |
e5329c37 | 740 | case O(GROW,'a'): |
dd0781e5 | 741 | case O(MANAGE,'a'): /* add a drive */ |
52826846 | 742 | devmode = 'a'; |
fe80f49b NB |
743 | re_add = 0; |
744 | continue; | |
997aed5d | 745 | case O(MANAGE,ReAdd): |
fe80f49b NB |
746 | devmode = 'a'; |
747 | re_add = 1; | |
52826846 | 748 | continue; |
e0d19036 | 749 | case O(MANAGE,'r'): /* remove a drive */ |
52826846 NB |
750 | devmode = 'r'; |
751 | continue; | |
e0d19036 | 752 | case O(MANAGE,'f'): /* set faulty */ |
52826846 NB |
753 | devmode = 'f'; |
754 | continue; | |
8382f19b | 755 | case O(INCREMENTAL,'R'): |
e0d19036 NB |
756 | case O(MANAGE,'R'): |
757 | case O(ASSEMBLE,'R'): | |
758 | case O(BUILD,'R'): | |
759 | case O(CREATE,'R'): /* Run the array */ | |
52826846 NB |
760 | if (runstop < 0) { |
761 | fprintf(stderr, Name ": Cannot both Stop and Run an array\n"); | |
762 | exit(2); | |
763 | } | |
764 | runstop = 1; | |
765 | continue; | |
e0d19036 | 766 | case O(MANAGE,'S'): |
52826846 NB |
767 | if (runstop > 0) { |
768 | fprintf(stderr, Name ": Cannot both Run and Stop an array\n"); | |
769 | exit(2); | |
770 | } | |
771 | runstop = -1; | |
772 | continue; | |
773 | ||
e0d19036 NB |
774 | case O(MISC,'Q'): |
775 | case O(MISC,'D'): | |
776 | case O(MISC,'E'): | |
777 | case O(MISC,'K'): | |
778 | case O(MISC,'R'): | |
779 | case O(MISC,'S'): | |
c82f047c | 780 | case O(MISC,'X'): |
e0d19036 NB |
781 | case O(MISC,'o'): |
782 | case O(MISC,'w'): | |
b90c0e9a | 783 | case O(MISC,'W'): |
1770662b | 784 | case O(MISC, Waitclean): |
4cce4069 | 785 | case O(MISC, DetailPlatform): |
e0d19036 NB |
786 | if (devmode && devmode != opt && |
787 | (devmode == 'E' || (opt == 'E' && devmode != 'Q'))) { | |
788 | fprintf(stderr, Name ": --examine/-E cannot be given with -%c\n", | |
789 | devmode =='E'?opt:devmode); | |
790 | exit(2); | |
791 | } | |
792 | devmode = opt; | |
793 | continue; | |
feb716e9 NB |
794 | case O(MISC,'t'): |
795 | test = 1; | |
796 | continue; | |
e0d19036 | 797 | |
997aed5d | 798 | case O(MISC, Sparc22): |
bd526cee NB |
799 | if (devmode != 'E') { |
800 | fprintf(stderr, Name ": --sparc2.2 only allowed with --examine\n"); | |
801 | exit(2); | |
802 | } | |
803 | SparcAdjust = 1; | |
804 | continue; | |
c82f047c NB |
805 | |
806 | case O(ASSEMBLE,'b'): /* here we simply set the bitmap file */ | |
807 | if (!optarg) { | |
808 | fprintf(stderr, Name ": bitmap file needed with -b in --assemble mode\n"); | |
809 | exit(2); | |
810 | } | |
55935d51 NB |
811 | if (strcmp(optarg, "internal")==0) { |
812 | fprintf(stderr, Name ": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n"); | |
813 | continue; | |
814 | } | |
c82f047c NB |
815 | bitmap_fd = open(optarg, O_RDWR); |
816 | if (!*optarg || bitmap_fd < 0) { | |
817 | fprintf(stderr, Name ": cannot open bitmap file %s: %s\n", optarg, strerror(errno)); | |
818 | exit(2); | |
819 | } | |
820 | ident.bitmap_fd = bitmap_fd; /* for Assemble */ | |
821 | continue; | |
f5e166fe | 822 | |
997aed5d NB |
823 | case O(ASSEMBLE, BackupFile): |
824 | case O(GROW, BackupFile): | |
06b0d786 NB |
825 | /* Specify a file into which grow might place a backup, |
826 | * or from which assemble might recover a backup | |
827 | */ | |
828 | if (backup_file) { | |
829 | fprintf(stderr, Name ": backup file already specified, rejecting %s\n", optarg); | |
830 | exit(2); | |
831 | } | |
832 | backup_file = optarg; | |
833 | continue; | |
834 | ||
c82f047c NB |
835 | case O(BUILD,'b'): |
836 | case O(CREATE,'b'): /* here we create the bitmap */ | |
85375d6d NB |
837 | if (strcmp(optarg, "none") == 0) { |
838 | fprintf(stderr, Name ": '--bitmap none' only" | |
839 | " support for --grow\n"); | |
840 | exit(2); | |
841 | } | |
842 | /* FALL THROUGH */ | |
843 | case O(GROW,'b'): | |
1e0d770c NB |
844 | if (strcmp(optarg, "internal")== 0 || |
845 | strcmp(optarg, "none")== 0 || | |
846 | strchr(optarg, '/') != NULL) { | |
847 | bitmap_file = optarg; | |
848 | continue; | |
849 | } | |
850 | /* probable typo */ | |
851 | fprintf(stderr, Name ": bitmap file must contain a '/', or be 'internal', or 'none'\n"); | |
852 | exit(2); | |
c82f047c | 853 | |
997aed5d NB |
854 | case O(GROW,BitmapChunk): |
855 | case O(BUILD,BitmapChunk): | |
856 | case O(CREATE,BitmapChunk): /* bitmap chunksize */ | |
c82f047c NB |
857 | bitmap_chunk = strtol(optarg, &c, 10); |
858 | if (!optarg[0] || *c || bitmap_chunk < 0 || | |
859 | bitmap_chunk & (bitmap_chunk - 1)) { | |
860 | fprintf(stderr, Name ": invalid bitmap chunksize: %s\n", | |
861 | optarg); | |
862 | exit(2); | |
863 | } | |
864 | /* convert K to B, chunk of 0K means 512B */ | |
865 | bitmap_chunk = bitmap_chunk ? bitmap_chunk * 1024 : 512; | |
866 | continue; | |
dfd4d8ee | 867 | |
7d19ad0d | 868 | case O(GROW, WriteBehind): |
997aed5d NB |
869 | case O(BUILD, WriteBehind): |
870 | case O(CREATE, WriteBehind): /* write-behind mode */ | |
dfd4d8ee NB |
871 | write_behind = DEFAULT_MAX_WRITE_BEHIND; |
872 | if (optarg) { | |
873 | write_behind = strtol(optarg, &c, 10); | |
874 | if (write_behind < 0 || *c || | |
875 | write_behind > 16383) { | |
876 | fprintf(stderr, Name ": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg); | |
877 | exit(2); | |
878 | } | |
879 | } | |
880 | continue; | |
8382f19b NB |
881 | |
882 | case O(INCREMENTAL, 'r'): | |
883 | rebuild_map = 1; | |
884 | continue; | |
52826846 NB |
885 | } |
886 | /* We have now processed all the valid options. Anything else is | |
887 | * an error | |
888 | */ | |
06b0d786 NB |
889 | if (option_index > 0) |
890 | fprintf(stderr, Name ":option --%s not valid in %s mode\n", | |
891 | long_options[option_index].name, | |
892 | map_num(modes, mode)); | |
893 | else | |
894 | fprintf(stderr, Name ": option -%c not valid in %s mode\n", | |
895 | opt, map_num(modes, mode)); | |
64c4757e | 896 | exit(2); |
52826846 NB |
897 | |
898 | } | |
899 | ||
3d4064cc NB |
900 | if (print_help) { |
901 | char *help_text = Help; | |
902 | if (print_help == 2) | |
903 | help_text = OptionHelp; | |
904 | else | |
905 | switch (mode) { | |
906 | case ASSEMBLE : help_text = Help_assemble; break; | |
907 | case BUILD : help_text = Help_build; break; | |
908 | case CREATE : help_text = Help_create; break; | |
909 | case MANAGE : help_text = Help_manage; break; | |
910 | case MISC : help_text = Help_misc; break; | |
911 | case MONITOR : help_text = Help_monitor; break; | |
912 | case GROW : help_text = Help_grow; break; | |
8382f19b | 913 | case INCREMENTAL:help_text= Help_incr; break; |
3d4064cc | 914 | } |
0047d254 | 915 | fputs(help_text,stdout); |
3d4064cc NB |
916 | exit(0); |
917 | } | |
918 | ||
0320ea45 NB |
919 | if (!mode && devs_found) { |
920 | mode = MISC; | |
921 | devmode = 'Q'; | |
922 | if (devlist->disposition == 0) | |
923 | devlist->disposition = devmode; | |
924 | } | |
52826846 NB |
925 | if (!mode) { |
926 | fputs(Usage, stderr); | |
64c4757e | 927 | exit(2); |
64c4757e | 928 | } |
38098016 NB |
929 | |
930 | if (symlinks) { | |
931 | struct createinfo *ci = conf_get_create_info(); | |
932 | ||
933 | if (strcasecmp(symlinks, "yes") == 0) | |
934 | ci->symlinks = 1; | |
935 | else if (strcasecmp(symlinks, "no") == 0) | |
936 | ci->symlinks = 0; | |
937 | else { | |
938 | fprintf(stderr, Name ": option --symlinks must be 'no' or 'yes'\n"); | |
939 | exit(2); | |
940 | } | |
941 | } | |
52826846 NB |
942 | /* Ok, got the option parsing out of the way |
943 | * hopefully it's mostly right but there might be some stuff | |
944 | * missing | |
945 | * | |
e0d19036 | 946 | * That is mosty checked in the per-mode stuff but... |
52826846 NB |
947 | * |
948 | * For @,B,C and A without -s, the first device listed must be an md device | |
949 | * we check that here and open it. | |
64c4757e | 950 | */ |
52826846 | 951 | |
dd0781e5 NB |
952 | if (mode==MANAGE || mode == BUILD || mode == CREATE || mode == GROW || |
953 | (mode == ASSEMBLE && ! scan)) { | |
52826846 NB |
954 | if (devs_found < 1) { |
955 | fprintf(stderr, Name ": an md device must be given in this mode\n"); | |
956 | exit(2); | |
957 | } | |
dd0781e5 | 958 | if ((int)ident.super_minor == -2 && autof) { |
aba69144 | 959 | fprintf(stderr, Name ": --super-minor=dev is incompatible with --auto\n"); |
dd0781e5 NB |
960 | exit(2); |
961 | } | |
7f91af49 | 962 | if (mode == MANAGE || mode == GROW) { |
6be1d39d | 963 | mdfd = open_mddev(devlist->devname, 1); |
7f91af49 N |
964 | if (mdfd < 0) |
965 | exit(1); | |
966 | } else | |
967 | /* non-existent device is OK */ | |
968 | mdfd = open_mddev(devlist->devname, 0); | |
969 | if (mdfd == -2) { | |
970 | fprintf(stderr, Name ": device %s exists but is not an " | |
971 | "md array.\n", devlist->devname); | |
52826846 | 972 | exit(1); |
7f91af49 | 973 | } |
98c6faba | 974 | if ((int)ident.super_minor == -2) { |
d013a55e | 975 | struct stat stb; |
7f91af49 N |
976 | if (mdfd < 0) { |
977 | fprintf(stderr, Name ": --super-minor=dev given, and " | |
978 | "listed device %s doesn't exist.\n", | |
979 | devlist->devname); | |
980 | exit(1); | |
981 | } | |
d013a55e | 982 | fstat(mdfd, &stb); |
0df46c2a | 983 | ident.super_minor = minor(stb.st_rdev); |
d013a55e | 984 | } |
7f91af49 N |
985 | if (mdfd >= 0 && mode != MANAGE && mode != GROW) { |
986 | /* We don't really want this open yet, we just might | |
987 | * have wanted to check some things | |
988 | */ | |
989 | close(mdfd); | |
990 | mdfd = -1; | |
991 | } | |
64c4757e | 992 | } |
52826846 | 993 | |
e4c4352e NB |
994 | if (raiddisks) { |
995 | if (raiddisks > max_disks) { | |
6fbba4c9 NB |
996 | fprintf(stderr, Name ": invalid number of raid devices: %d\n", |
997 | raiddisks); | |
e4c4352e NB |
998 | exit(2); |
999 | } | |
1000 | if (raiddisks == 1 && !force && level != -5) { | |
1001 | fprintf(stderr, Name ": '1' is an unusual number of drives for an array, so it is probably\n" | |
1002 | " a mistake. If you really mean it you will need to specify --force before\n" | |
1003 | " setting the number of drives.\n"); | |
1004 | exit(2); | |
1005 | } | |
1006 | } | |
1007 | if (sparedisks) { | |
1008 | if ( sparedisks > max_disks - raiddisks) { | |
6fbba4c9 NB |
1009 | fprintf(stderr, Name ": invalid number of spare-devices: %d\n", |
1010 | sparedisks); | |
e4c4352e NB |
1011 | exit(2); |
1012 | } | |
1013 | } | |
c82f047c | 1014 | |
997aed5d | 1015 | if (homehost == NULL) |
0ac91628 | 1016 | homehost = conf_get_homehost(&require_homehost); |
9180c81b | 1017 | if (homehost == NULL || strcmp(homehost, "<system>")==0) { |
05697ec1 NB |
1018 | if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) { |
1019 | sys_hostname[sizeof(sys_hostname)-1] = 0; | |
1020 | homehost = sys_hostname; | |
1021 | } | |
1022 | } | |
997aed5d | 1023 | |
7f91af49 N |
1024 | ident.autof = autof; |
1025 | ||
52826846 NB |
1026 | rv = 0; |
1027 | switch(mode) { | |
e0d19036 | 1028 | case MANAGE: |
52826846 NB |
1029 | /* readonly, add/remove, readwrite, runstop */ |
1030 | if (readonly>0) | |
cd29a5c8 | 1031 | rv = Manage_ro(devlist->devname, mdfd, readonly); |
52826846 | 1032 | if (!rv && devs_found>1) |
cd29a5c8 | 1033 | rv = Manage_subdevs(devlist->devname, mdfd, |
dab6685f | 1034 | devlist->next, verbose-quiet); |
52826846 | 1035 | if (!rv && readonly < 0) |
cd29a5c8 | 1036 | rv = Manage_ro(devlist->devname, mdfd, readonly); |
52826846 | 1037 | if (!rv && runstop) |
ab56093f | 1038 | rv = Manage_runstop(devlist->devname, mdfd, runstop, quiet); |
52826846 | 1039 | break; |
e0d19036 | 1040 | case ASSEMBLE: |
d013a55e | 1041 | if (devs_found == 1 && ident.uuid_set == 0 && |
947fd4dd | 1042 | ident.super_minor == UnSet && ident.name[0] == 0 && !scan ) { |
d013a55e | 1043 | /* Only a device has been given, so get details from config file */ |
8aec876d | 1044 | mddev_ident_t array_ident = conf_get_ident(devlist->devname); |
b5e64645 NB |
1045 | if (array_ident == NULL) { |
1046 | fprintf(stderr, Name ": %s not identified in config file.\n", | |
1047 | devlist->devname); | |
d013a55e | 1048 | rv |= 1; |
7f91af49 N |
1049 | if (mdfd >= 0) |
1050 | close(mdfd); | |
b5e64645 | 1051 | } else { |
7f91af49 N |
1052 | if (array_ident->autof == 0) |
1053 | array_ident->autof = autof; | |
1054 | rv |= Assemble(ss, devlist->devname, array_ident, | |
1055 | NULL, backup_file, | |
0ac91628 N |
1056 | readonly, runstop, update, |
1057 | homehost, require_homehost, | |
1058 | verbose-quiet, force); | |
d013a55e NB |
1059 | } |
1060 | } else if (!scan) | |
7f91af49 | 1061 | rv = Assemble(ss, devlist->devname, &ident, |
06b0d786 | 1062 | devlist->next, backup_file, |
0ac91628 N |
1063 | readonly, runstop, update, |
1064 | homehost, require_homehost, | |
1065 | verbose-quiet, force); | |
5787fa49 NB |
1066 | else if (devs_found>0) { |
1067 | if (update && devs_found > 1) { | |
1068 | fprintf(stderr, Name ": can only update a single array at a time\n"); | |
1069 | exit(1); | |
1070 | } | |
06b0d786 NB |
1071 | if (backup_file && devs_found > 1) { |
1072 | fprintf(stderr, Name ": can only assemble a single array when providing a backup file.\n"); | |
1073 | exit(1); | |
1074 | } | |
cd29a5c8 | 1075 | for (dv = devlist ; dv ; dv=dv->next) { |
8aec876d | 1076 | mddev_ident_t array_ident = conf_get_ident(dv->devname); |
52826846 NB |
1077 | if (array_ident == NULL) { |
1078 | fprintf(stderr, Name ": %s not identified in config file.\n", | |
cd29a5c8 | 1079 | dv->devname); |
52826846 NB |
1080 | rv |= 1; |
1081 | continue; | |
1082 | } | |
7f91af49 N |
1083 | if (array_ident->autof == 0) |
1084 | array_ident->autof = autof; | |
1085 | rv |= Assemble(ss, dv->devname, array_ident, | |
06b0d786 | 1086 | NULL, backup_file, |
0ac91628 N |
1087 | readonly, runstop, update, |
1088 | homehost, require_homehost, | |
1089 | verbose-quiet, force); | |
52826846 | 1090 | } |
5787fa49 | 1091 | } else { |
8aec876d NB |
1092 | mddev_ident_t array_list = conf_get_ident(NULL); |
1093 | mddev_dev_t devlist = conf_get_devs(); | |
da6b5ca9 NB |
1094 | int cnt = 0; |
1095 | if (devlist == NULL) { | |
1096 | fprintf(stderr, Name ": No devices listed in conf file were found.\n"); | |
1097 | exit(1); | |
1098 | } | |
1099 | if (update) { | |
1100 | fprintf(stderr, Name ": --update not meaningful with a --scan assembly.\n"); | |
1101 | exit(1); | |
1102 | } | |
1103 | if (backup_file) { | |
1104 | fprintf(stderr, Name ": --backup_file not meaningful with a --scan assembly.\n"); | |
1105 | exit(1); | |
1106 | } | |
1107 | for (; array_list; array_list = array_list->next) { | |
112cace6 N |
1108 | if (array_list->devname && |
1109 | strcasecmp(array_list->devname, "<ignore>") == 0) | |
1110 | continue; | |
7f91af49 N |
1111 | if (array_list->autof == 0) |
1112 | array_list->autof = autof; | |
1113 | ||
1114 | rv |= Assemble(ss, array_list->devname, | |
1115 | array_list, | |
1116 | NULL, NULL, | |
0ac91628 N |
1117 | readonly, runstop, NULL, |
1118 | homehost, require_homehost, | |
1119 | verbose-quiet, force); | |
a30b2ecd | 1120 | cnt++; |
da6b5ca9 | 1121 | } |
a30b2ecd | 1122 | if (homehost && cnt == 0) { |
da6b5ca9 | 1123 | /* Maybe we can auto-assemble something. |
7f91af49 | 1124 | * Repeatedly call Assemble in auto-assemble mode |
da6b5ca9 NB |
1125 | * until it fails |
1126 | */ | |
1127 | int rv2; | |
d55e3aef NB |
1128 | int acnt; |
1129 | ident.autof = autof; | |
da6b5ca9 | 1130 | do { |
1c203a4b | 1131 | mddev_dev_t devlist = conf_get_devs(); |
d55e3aef NB |
1132 | acnt = 0; |
1133 | do { | |
7f91af49 | 1134 | rv2 = Assemble(ss, NULL, |
8aec876d | 1135 | &ident, |
1c203a4b | 1136 | devlist, NULL, |
0ac91628 N |
1137 | readonly, runstop, NULL, |
1138 | homehost, require_homehost, | |
1139 | verbose-quiet, force); | |
d55e3aef NB |
1140 | if (rv2==0) { |
1141 | cnt++; | |
1142 | acnt++; | |
1143 | } | |
589395d6 NB |
1144 | if (rv2 == 1) |
1145 | /* found something so even though assembly failed we | |
1146 | * want to avoid auto-updates | |
1147 | */ | |
1148 | auto_update_home = 0; | |
d55e3aef NB |
1149 | } while (rv2!=2); |
1150 | /* Incase there are stacked devices, we need to go around again */ | |
d55e3aef | 1151 | } while (acnt); |
589395d6 NB |
1152 | if (cnt == 0 && auto_update_home && homehost) { |
1153 | /* Nothing found, maybe we need to bootstrap homehost info */ | |
1154 | do { | |
1155 | acnt = 0; | |
1156 | do { | |
7f91af49 | 1157 | rv2 = Assemble(ss, NULL, |
8aec876d | 1158 | &ident, |
43f2372a | 1159 | NULL, NULL, |
0ac91628 N |
1160 | readonly, runstop, "homehost", |
1161 | homehost, require_homehost, | |
1162 | verbose-quiet, force); | |
589395d6 NB |
1163 | if (rv2==0) { |
1164 | cnt++; | |
1165 | acnt++; | |
1166 | } | |
1167 | } while (rv2!=2); | |
1168 | /* Incase there are stacked devices, we need to go around again */ | |
589395d6 NB |
1169 | } while (acnt); |
1170 | } | |
da6b5ca9 NB |
1171 | if (cnt == 0 && rv == 0) { |
1172 | fprintf(stderr, Name ": No arrays found in config file or automatically\n"); | |
1173 | rv = 1; | |
1c203a4b NB |
1174 | } else if (cnt) |
1175 | rv = 0; | |
da6b5ca9 NB |
1176 | } else if (cnt == 0 && rv == 0) { |
1177 | fprintf(stderr, Name ": No arrays found in config file\n"); | |
1178 | rv = 1; | |
1179 | } | |
52826846 NB |
1180 | } |
1181 | break; | |
e0d19036 | 1182 | case BUILD: |
c82f047c | 1183 | if (delay == 0) delay = DEFAULT_BITMAP_DELAY; |
dfd4d8ee NB |
1184 | if (write_behind && !bitmap_file) { |
1185 | fprintf(stderr, Name ": write-behind mode requires a bitmap.\n"); | |
1186 | rv = 1; | |
1187 | break; | |
1188 | } | |
5b28bd56 | 1189 | if (raiddisks == 0) { |
e5669f40 | 1190 | fprintf(stderr, Name ": no raid-devices specified.\n"); |
5b28bd56 NB |
1191 | rv = 1; |
1192 | break; | |
1193 | } | |
dfd4d8ee | 1194 | |
c82f047c | 1195 | if (bitmap_file) { |
55935d51 NB |
1196 | if (strcmp(bitmap_file, "internal")==0) { |
1197 | fprintf(stderr, Name ": 'internal' bitmaps not supported with --build\n"); | |
1198 | rv |= 1; | |
1199 | break; | |
1200 | } | |
c82f047c | 1201 | } |
7f91af49 | 1202 | rv = Build(devlist->devname, chunk, level, layout, |
c82f047c | 1203 | raiddisks, devlist->next, assume_clean, |
7f91af49 N |
1204 | bitmap_file, bitmap_chunk, write_behind, |
1205 | delay, verbose-quiet, autof); | |
52826846 | 1206 | break; |
e0d19036 | 1207 | case CREATE: |
c82f047c | 1208 | if (delay == 0) delay = DEFAULT_BITMAP_DELAY; |
dfd4d8ee NB |
1209 | if (write_behind && !bitmap_file) { |
1210 | fprintf(stderr, Name ": write-behind mode requires a bitmap.\n"); | |
1211 | rv = 1; | |
1212 | break; | |
1213 | } | |
5b28bd56 | 1214 | if (raiddisks == 0) { |
e5669f40 | 1215 | fprintf(stderr, Name ": no raid-devices specified.\n"); |
5b28bd56 NB |
1216 | rv = 1; |
1217 | break; | |
1218 | } | |
f9ce90ba | 1219 | |
7f91af49 | 1220 | rv = Create(ss, devlist->devname, chunk, level, layout, size<0 ? 0 : size, |
05697ec1 | 1221 | raiddisks, sparedisks, ident.name, homehost, |
3d3dd91e | 1222 | ident.uuid_set ? ident.uuid : NULL, |
47d79ef8 | 1223 | devs_found-1, devlist->next, runstop, verbose-quiet, force, assume_clean, |
7f91af49 | 1224 | bitmap_file, bitmap_chunk, write_behind, delay, autof); |
52826846 | 1225 | break; |
e0d19036 | 1226 | case MISC: |
e0d19036 NB |
1227 | if (devmode == 'E') { |
1228 | if (devlist == NULL && !scan) { | |
1229 | fprintf(stderr, Name ": No devices to examine\n"); | |
1230 | exit(2); | |
1231 | } | |
1232 | if (devlist == NULL) | |
8aec876d | 1233 | devlist = conf_get_devs(); |
e0d19036 | 1234 | if (devlist == NULL) { |
c913b90e | 1235 | fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile); |
e0d19036 NB |
1236 | exit(1); |
1237 | } | |
e843d5d7 NB |
1238 | if (brief && verbose) |
1239 | brief = 2; | |
0d726f17 KS |
1240 | rv = Examine(devlist, scan?(verbose>1?0:verbose+1):brief, |
1241 | export, scan, | |
1242 | SparcAdjust, ss, homehost); | |
4cce4069 DW |
1243 | } else if (devmode == DetailPlatform) { |
1244 | rv = Detail_Platform(ss ? ss->ss : NULL, ss ? scan : 1, verbose); | |
e0d19036 NB |
1245 | } else { |
1246 | if (devlist == NULL) { | |
fabbfd48 DW |
1247 | if ((devmode=='D' || devmode == Waitclean) && scan) { |
1248 | /* apply --detail or --wait-clean to | |
1249 | * all devices in /proc/mdstat | |
1250 | */ | |
22a88995 | 1251 | struct mdstat_ent *ms = mdstat_read(0, 1); |
e0d19036 | 1252 | struct mdstat_ent *e; |
35904960 | 1253 | struct map_ent *map = NULL; |
fabbfd48 DW |
1254 | int v = verbose>1?0:verbose+1; |
1255 | ||
e0d19036 | 1256 | for (e=ms ; e ; e=e->next) { |
35904960 N |
1257 | char *name; |
1258 | struct map_ent *me; | |
1259 | me = map_by_devnum(&map, e->devnum); | |
8615dcff N |
1260 | if (me && me->path |
1261 | && strcmp(me->path, "/unknown") != 0) | |
35904960 N |
1262 | name = me->path; |
1263 | else | |
1264 | name = get_md_name(e->devnum); | |
e0d19036 NB |
1265 | |
1266 | if (!name) { | |
1267 | fprintf(stderr, Name ": cannot find device file for %s\n", | |
1268 | e->dev); | |
1269 | continue; | |
1270 | } | |
fabbfd48 DW |
1271 | if (devmode == 'D') |
1272 | rv |= Detail(name, v, | |
1273 | export, test, | |
1274 | homehost); | |
1275 | else | |
1276 | rv |= WaitClean(name, v); | |
91f068bf NB |
1277 | put_md_name(name); |
1278 | } | |
89a10d84 | 1279 | free_mdstat(ms); |
91f068bf NB |
1280 | } else if (devmode == 'S' && scan) { |
1281 | /* apply --stop to all devices in /proc/mdstat */ | |
1282 | /* Due to possible stacking of devices, repeat until | |
1283 | * nothing more can be stopped | |
1284 | */ | |
1285 | int progress=1, err; | |
1286 | int last = 0; | |
1287 | do { | |
22a88995 | 1288 | struct mdstat_ent *ms = mdstat_read(0, 0); |
91f068bf NB |
1289 | struct mdstat_ent *e; |
1290 | ||
1291 | if (!progress) last = 1; | |
1292 | progress = 0; err = 0; | |
1293 | for (e=ms ; e ; e=e->next) { | |
1294 | char *name = get_md_name(e->devnum); | |
1295 | ||
1296 | if (!name) { | |
1297 | fprintf(stderr, Name ": cannot find device file for %s\n", | |
1298 | e->dev); | |
1299 | continue; | |
1300 | } | |
6be1d39d | 1301 | mdfd = open_mddev(name, 1); |
43fc1676 | 1302 | if (mdfd >= 0) { |
ab56093f | 1303 | if (Manage_runstop(name, mdfd, -1, quiet?1:last?0:-1)) |
91f068bf NB |
1304 | err = 1; |
1305 | else | |
1306 | progress = 1; | |
43fc1676 NB |
1307 | close(mdfd); |
1308 | } | |
91f068bf NB |
1309 | |
1310 | put_md_name(name); | |
e0d19036 | 1311 | } |
89a10d84 | 1312 | free_mdstat(ms); |
91f068bf | 1313 | } while (!last && err); |
f9c25f1d | 1314 | if (err) rv |= 1; |
91f068bf | 1315 | } else { |
e0d19036 NB |
1316 | fprintf(stderr, Name ": No devices given.\n"); |
1317 | exit(2); | |
1318 | } | |
1319 | } | |
1320 | for (dv=devlist ; dv; dv=dv->next) { | |
1321 | switch(dv->disposition) { | |
1322 | case 'D': | |
54bad364 KS |
1323 | rv |= Detail(dv->devname, |
1324 | brief?1+verbose:0, | |
1325 | export, test, homehost); | |
1326 | continue; | |
e0d19036 | 1327 | case 'K': /* Zero superblock */ |
111d01fc NB |
1328 | rv |= Kill(dv->devname, force, quiet,0); |
1329 | continue; | |
e0d19036 NB |
1330 | case 'Q': |
1331 | rv |= Query(dv->devname); continue; | |
c82f047c | 1332 | case 'X': |
55935d51 | 1333 | rv |= ExamineBitmap(dv->devname, brief, ss); continue; |
b90c0e9a NB |
1334 | case 'W': |
1335 | rv |= Wait(dv->devname); continue; | |
1770662b | 1336 | case Waitclean: |
27dec8fa | 1337 | rv |= WaitClean(dv->devname, verbose-quiet); continue; |
e0d19036 | 1338 | } |
6be1d39d | 1339 | mdfd = open_mddev(dv->devname, 1); |
43fc1676 | 1340 | if (mdfd>=0) { |
e0d19036 NB |
1341 | switch(dv->disposition) { |
1342 | case 'R': | |
ab56093f | 1343 | rv |= Manage_runstop(dv->devname, mdfd, 1, quiet); break; |
e0d19036 | 1344 | case 'S': |
ab56093f | 1345 | rv |= Manage_runstop(dv->devname, mdfd, -1, quiet); break; |
e0d19036 NB |
1346 | case 'o': |
1347 | rv |= Manage_ro(dv->devname, mdfd, 1); break; | |
1348 | case 'w': | |
1349 | rv |= Manage_ro(dv->devname, mdfd, -1); break; | |
1350 | } | |
43fc1676 | 1351 | close(mdfd); |
7f48e210 NB |
1352 | } else |
1353 | rv |= 1; | |
e0d19036 | 1354 | } |
cd29a5c8 | 1355 | } |
9a9dab36 | 1356 | break; |
e0d19036 | 1357 | case MONITOR: |
e0d19036 NB |
1358 | if (!devlist && !scan) { |
1359 | fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n"); | |
1360 | rv = 1; | |
1361 | break; | |
1362 | } | |
b5e64645 NB |
1363 | if (pidfile && !daemonise) { |
1364 | fprintf(stderr, Name ": Cannot write a pid file when not in daemon mode\n"); | |
1365 | rv = 1; | |
1366 | break; | |
1367 | } | |
d013a55e | 1368 | rv= Monitor(devlist, mailaddr, program, |
773135f5 | 1369 | delay?delay:60, daemonise, scan, oneshot, |
8aec876d | 1370 | dosyslog, test, pidfile); |
9a9dab36 | 1371 | break; |
dd0781e5 NB |
1372 | |
1373 | case GROW: | |
1374 | if (devs_found > 1) { | |
aba69144 | 1375 | |
e5329c37 NB |
1376 | /* must be '-a'. */ |
1377 | if (size >= 0 || raiddisks) { | |
1378 | fprintf(stderr, Name ": --size, --raiddisks, and --add are exclusing in --grow mode\n"); | |
1379 | rv = 1; | |
1380 | break; | |
1381 | } | |
1382 | for (dv=devlist->next; dv ; dv=dv->next) { | |
1383 | rv = Grow_Add_device(devlist->devname, mdfd, dv->devname); | |
1384 | if (rv) | |
1385 | break; | |
1386 | } | |
f5e166fe NB |
1387 | } else if ((size >= 0) + (raiddisks != 0) + (layout != UnSet) + (bitmap_file != NULL)> 1) { |
1388 | fprintf(stderr, Name ": can change at most one of size, raiddisks, bitmap, and layout\n"); | |
dd0781e5 NB |
1389 | rv = 1; |
1390 | break; | |
b5e64645 NB |
1391 | } else if (layout != UnSet) |
1392 | rv = Manage_reconfig(devlist->devname, mdfd, layout); | |
1393 | else if (size >= 0 || raiddisks) | |
06b0d786 | 1394 | rv = Grow_reshape(devlist->devname, mdfd, quiet, backup_file, |
e86c9dd6 | 1395 | size, level, layout, chunk, raiddisks); |
f5e166fe NB |
1396 | else if (bitmap_file) { |
1397 | if (delay == 0) delay = DEFAULT_BITMAP_DELAY; | |
1398 | rv = Grow_addbitmap(devlist->devname, mdfd, bitmap_file, | |
8fac0577 | 1399 | bitmap_chunk, delay, write_behind, force); |
f5e166fe | 1400 | } else |
b5e64645 | 1401 | fprintf(stderr, Name ": no changes to --grow\n"); |
dd0781e5 | 1402 | break; |
8382f19b NB |
1403 | case INCREMENTAL: |
1404 | if (rebuild_map) { | |
1405 | RebuildMap(); | |
1406 | } | |
1407 | if (scan) { | |
1408 | if (runstop <= 0) { | |
1409 | fprintf(stderr, Name | |
1410 | ": --incremental --scan meaningless without --run.\n"); | |
1411 | break; | |
1412 | } | |
1413 | rv = IncrementalScan(verbose); | |
1414 | } | |
1415 | if (!devlist) { | |
1416 | if (!rebuild_map && !scan) { | |
1417 | fprintf(stderr, Name | |
1418 | ": --incremental requires a device.\n"); | |
1419 | rv = 1; | |
1420 | } | |
1421 | break; | |
1422 | } | |
1423 | if (devlist->next) { | |
1424 | fprintf(stderr, Name | |
1425 | ": --incremental can only handle one device.\n"); | |
1426 | rv = 1; | |
1427 | break; | |
1428 | } | |
1429 | rv = Incremental(devlist->devname, verbose-quiet, runstop, | |
0ac91628 | 1430 | ss, homehost, require_homehost, autof); |
1f48664b NB |
1431 | break; |
1432 | case AUTODETECT: | |
1433 | autodetect(); | |
1434 | break; | |
64c4757e | 1435 | } |
52826846 | 1436 | exit(rv); |
64c4757e | 1437 | } |