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