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