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