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