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