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