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