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