2
* mdadm - manage Linux "md" devices aka RAID arrays.
4
* Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
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.
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.
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
22
* Email: <neilb@cse.unsw.edu.au>
24
* School of Computer Science and Engineering
25
* The University of New South Wales
36
int main(int argc, char *argv[])
51
struct mddev_ident_s ident;
52
char *configfile = NULL;
60
mddev_dev_t devlist = NULL;
61
mddev_dev_t *devlistend = & devlist;
69
int autof = 0; /* -2 means create device based on name:
70
* if it ends mdN, then non-partitioned array N
71
* if it ends dN, then partitions array N
72
* -1 means create non-partitioned, choose N
73
* 1 or more to create partitioned
74
* If -1 or 1 and name is a 'standard' name, then
75
* insist on a match of type and number.
78
char *mailaddr = NULL;
91
ident.raid_disks = UnSet;
92
ident.super_minor= UnSet;
94
ident.spare_group = NULL;
97
while ((option_index = -1) ,
98
(opt=getopt_long(argc, argv,
99
short_options, long_options,
100
&option_index)) != -1) {
102
/* firstly, so mode-independant options */
106
if (option_index > 0 &&
107
strcmp(long_options[option_index].name, "help-options")==0)
108
help_text = OptionHelp;
111
case ASSEMBLE : help_text = Help_assemble; break;
112
case BUILD : help_text = Help_build; break;
113
case CREATE : help_text = Help_create; break;
114
case MANAGE : help_text = Help_manage; break;
115
case MISC : help_text = Help_misc; break;
116
case MONITOR : help_text = Help_monitor; break;
117
case GROW : help_text = Help_grow; break;
119
fputs(help_text,stderr);
123
fputs(Version, stderr);
134
fputs(Usage, stderr);
137
/* second, figure out the mode.
138
* Some options force the mode. Others
139
* set the mode if it isn't already
143
case '@': /* just incase they say --manage */
144
newmode = MANAGE; break;
148
if (!mode) newmode = MANAGE;
151
case 'A': newmode = ASSEMBLE; break;
152
case 'B': newmode = BUILD; break;
153
case 'C': newmode = CREATE; break;
154
case 'F': newmode = MONITOR;break;
155
case 'G': newmode = GROW; break;
160
case 'Q': newmode = MISC; break;
165
case 'K': if (!mode) newmode = MISC; break;
167
if (mode && newmode == mode) {
168
/* everybody happy ! */
169
} else if (mode && newmode != mode) {
171
fprintf(stderr, Name ": ");
172
if (option_index >= 0)
173
fprintf(stderr, "--%s", long_options[option_index].name);
175
fprintf(stderr, "-%c", opt);
176
fprintf(stderr, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
177
map_num(modes, newmode),
178
map_num(modes, mode));
180
} else if (!mode && newmode) {
183
/* special case of -c --help */
185
( strncmp(optarg, "--h", 3)==0 ||
186
strncmp(optarg, "-h", 2)==0)) {
187
fputs(Help_config, stderr);
191
/* If first option is a device, don't force the mode yet */
193
if (devs_found == 0) {
194
dv = malloc(sizeof(*dv));
196
fprintf(stderr, Name ": malloc failed\n");
199
dv->devname = optarg;
200
dv->disposition = devmode;
203
devlistend = &dv->next;
208
/* No mode yet, and this is the second device ... */
209
fprintf(stderr, Name ": An option must be given to set the mode before a second device is listed\n");
212
if (option_index >= 0)
213
fprintf(stderr, Name ": --%s", long_options[option_index].name);
215
fprintf(stderr, Name ": -%c", opt);
216
fprintf(stderr, " does not set the mode, and so cannot be the first option.\n");
220
/* if we just set the mode, then done */
232
/* an undecorated option - must be a device name.
234
if (devs_found > 0 && mode == '@' && !devmode) {
235
fprintf(stderr, Name ": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg);
238
if (devs_found > 0 && mode == 'G' && !devmode) {
239
fprintf(stderr, Name ": Must give one of -a for devices do add: %s\n", optarg);
242
dv = malloc(sizeof(*dv));
244
fprintf(stderr, Name ": malloc failed\n");
247
dv->devname = optarg;
248
dv->disposition = devmode;
251
devlistend = &dv->next;
257
/* We've got a mode, and opt is now something else which
258
* could depend on the mode */
259
#define O(a,b) ((a<<8)|b)
260
switch (O(mode,opt)) {
262
case O(BUILD,'c'): /* chunk or rounding */
264
fprintf(stderr, Name ": chunk/rounding may only be specified once. "
265
"Second value is %s.\n", optarg);
268
chunk = strtol(optarg, &c, 10);
269
if (!optarg[0] || *c || chunk<4 || ((chunk-1)&chunk)) {
270
fprintf(stderr, Name ": invalid chunk/rounding value: %s\n",
277
case O(CREATE,'z'): /* size */
279
fprintf(stderr, Name ": size may only be specified once. "
280
"Second value is %s.\n", optarg);
283
if (strcmp(optarg, "max")==0)
286
size = strtol(optarg, &c, 10);
287
if (!optarg[0] || *c || size < 4) {
288
fprintf(stderr, Name ": invalid size: %s\n",
295
case O(GROW,'l'): /* hack - needed to understand layout */
297
case O(BUILD,'l'): /* set raid level*/
298
if (level != UnSet) {
299
fprintf(stderr, Name ": raid level may only be set once. "
300
"Second value is %s.\n", optarg);
303
level = map_name(pers, optarg);
304
if (level == UnSet) {
305
fprintf(stderr, Name ": invalid raid level: %s\n",
309
if (level != 0 && level != -1 && level != 1 && level != -4 && level != -5 && mode == BUILD) {
310
fprintf(stderr, Name ": Raid level %s not permitted with --build.\n",
314
if (sparedisks > 0 && level < 1 && level >= -1) {
315
fprintf(stderr, Name ": raid level %s is incompatible with spare-devices setting.\n",
322
case O(CREATE,'p'): /* raid5 layout */
323
case O(BUILD,'p'): /* faulty layout */
324
case O(GROW, 'p'): /* faulty reconfig */
325
if (layout != UnSet) {
326
fprintf(stderr,Name ": layout may only be sent once. "
327
"Second value was %s\n", optarg);
332
fprintf(stderr, Name ": layout not meaningful for %s arrays.\n",
333
map_num(pers, level));
336
fprintf(stderr, Name ": raid level must be given before layout.\n");
341
layout = map_name(r5layout, optarg);
343
fprintf(stderr, Name ": layout %s not understood for raid5.\n",
350
/* 'f' or 'n' followed by a number <= raid_disks */
351
if ((optarg[0] != 'n' && optarg[0] != 'f') ||
352
(copies = strtoul(optarg+1, &cp, 10)) < 1 ||
355
fprintf(stderr, Name ": layout for raid10 must be 'nNN' or 'fNN' where NN is a number, not %s\n", optarg);
358
if (optarg[0] == 'n')
359
layout = 256 + copies;
361
layout = 1 + (copies<<8);
368
int ln = strcspn(optarg, "0123456789");
369
char *m = strdup(optarg);
372
mode = map_name(faultylayout, m);
374
fprintf(stderr, Name ": layout %s not understood for faulty.\n",
378
layout = mode | (atoi(optarg+ln)<< ModeShift);
384
case O(BUILD,3): /* assume clean */
390
case O(BUILD,'n'): /* number of raid disks */
392
fprintf(stderr, Name ": raid-devices set twice: %d and %s\n",
396
raiddisks = strtol(optarg, &c, 10);
397
if (!optarg[0] || *c || raiddisks<=0 || raiddisks > MD_SB_DISKS) {
398
fprintf(stderr, Name ": invalid number of raid devices: %s\n",
402
if (raiddisks == 1 && !force && level != -5) {
403
fprintf(stderr, Name ": '1' is an unusual number of drives for an array, so it is probably\n"
404
" a mistake. If you really mean it you will need to specify --force before\n"
405
" setting the number of drives.\n");
408
ident.raid_disks = raiddisks;
411
case O(CREATE,'x'): /* number of spare (eXtra) discs */
413
fprintf(stderr,Name ": spare-devices set twice: %d and %s\n",
417
if (level != UnSet && level <= 0 && level >= -1) {
418
fprintf(stderr, Name ": spare-devices setting is incompatible with raid level %d\n",
422
sparedisks = strtol(optarg, &c, 10);
423
if (!optarg[0] || *c || sparedisks < 0 || sparedisks > MD_SB_DISKS - raiddisks) {
424
fprintf(stderr, Name ": invalid number of spare-devices: %s\n",
432
case O(ASSEMBLE,'a'): /* auto-creation of device node */
435
else if (strcasecmp(optarg,"no")==0)
437
else if (strcasecmp(optarg,"yes")==0)
439
else if (strcasecmp(optarg,"md")==0)
442
/* There might be digits, and maybe a hypen, at the end */
443
char *e = optarg + strlen(optarg);
446
while (e > optarg && isdigit(e[-1]))
450
if (num <= 0) num = 1;
452
if (e > optarg && e[-1] == '-')
455
if ((len == 3 && strncasecmp(optarg,"mdp",3)==0) ||
456
(len == 1 && strncasecmp(optarg,"p",1)==0) ||
457
(len >= 4 && strncasecmp(optarg,"part",4)==0))
460
fprintf(stderr, Name ": --auto flag arg of \"%s\" unrecognised: use no,yes,md,mdp,part\n"
461
" optionally followed by a number.\n",
468
case O(BUILD,'f'): /* force honouring '-n 1' */
469
case O(GROW,'f'): /* ditto */
470
case O(CREATE,'f'): /* force honouring of device list */
471
case O(ASSEMBLE,'f'): /* force assembly */
472
case O(MISC,'f'): /* force zero */
476
/* now for the Assemble options */
477
case O(ASSEMBLE,'u'): /* uuid of array */
478
if (ident.uuid_set) {
479
fprintf(stderr, Name ": uuid cannot be set twice. "
480
"Second value %s.\n", optarg);
483
if (parse_uuid(optarg, ident.uuid))
486
fprintf(stderr,Name ": Bad uuid: %s\n", optarg);
491
case O(ASSEMBLE,'m'): /* super-minor for array */
492
if (ident.super_minor != UnSet) {
493
fprintf(stderr, Name ": super-minor cannot be set twice. "
494
"Second value: %s.\n", optarg);
497
if (strcmp(optarg, "dev")==0)
498
ident.super_minor = -2;
500
ident.super_minor = strtoul(optarg, &cp, 10);
501
if (!optarg[0] || *cp) {
502
fprintf(stderr, Name ": Bad super-minor number: %s.\n", optarg);
508
case O(ASSEMBLE,'U'): /* update the superblock */
510
fprintf(stderr, Name ": Can only update one aspect of superblock, both %s and %s given.\n",
515
if (strcmp(update, "sparc2.2")==0)
517
if (strcmp(update, "super-minor") == 0)
519
if (strcmp(update, "summaries")==0)
521
if (strcmp(update, "resync")==0)
523
fprintf(stderr, Name ": '--update %s' invalid. Only 'sparc2.2', 'super-minor', 'resync' or 'summaries' supported\n",update);
526
case O(ASSEMBLE,'c'): /* config file */
530
fprintf(stderr, Name ": configfile cannot be set twice. "
531
"Second value is %s.\n", optarg);
535
/* FIXME possibly check that config file exists. Even parse it */
537
case O(ASSEMBLE,'s'): /* scan */
543
case O(MONITOR,'m'): /* mail address */
545
fprintf(stderr, Name ": only specify one mailaddress. %s ignored.\n",
551
case O(MONITOR,'p'): /* alert program */
553
fprintf(stderr, Name ": only specify one alter program. %s ignored.\n",
559
case O(MONITOR,'d'): /* delay in seconds */
561
fprintf(stderr, Name ": only specify delay once. %s ignored.\n",
564
delay = strtol(optarg, &c, 10);
565
if (!optarg[0] || *c || delay<1) {
566
fprintf(stderr, Name ": invalid delay: %s\n",
572
case O(MONITOR,'f'): /* daemonise */
575
case O(MONITOR,'i'): /* pid */
577
fprintf(stderr, Name ": only specify one pid file. %s ignored.\n",
582
case O(MONITOR,'1'): /* oneshot */
585
case O(MONITOR,'t'): /* test */
589
/* now the general management options. Some are applicable
590
* to other modes. None have arguments.
593
case O(MANAGE,'a'): /* add a drive */
596
case O(MANAGE,'r'): /* remove a drive */
599
case O(MANAGE,'f'): /* set faulty */
603
case O(ASSEMBLE,'R'):
605
case O(CREATE,'R'): /* Run the array */
607
fprintf(stderr, Name ": Cannot both Stop and Run an array\n");
614
fprintf(stderr, Name ": Cannot both Run and Stop an array\n");
622
fprintf(stderr, Name ": Cannot have both readonly and readwrite\n");
629
fprintf(stderr, Name ": Cannot have both readwrite and readonly.\n");
643
if (devmode && devmode != opt &&
644
(devmode == 'E' || (opt == 'E' && devmode != 'Q'))) {
645
fprintf(stderr, Name ": --examine/-E cannot be given with -%c\n",
646
devmode =='E'?opt:devmode);
656
if (devmode != 'E') {
657
fprintf(stderr, Name ": --sparc2.2 only allowed with --examine\n");
663
/* We have now processed all the valid options. Anything else is
666
fprintf(stderr, Name ": option %c not valid in %s mode\n",
667
opt, map_num(modes, mode));
672
if (!mode && devs_found) {
675
if (devlist->disposition == 0)
676
devlist->disposition = devmode;
679
fputs(Usage, stderr);
682
/* Ok, got the option parsing out of the way
683
* hopefully it's mostly right but there might be some stuff
686
* That is mosty checked in the per-mode stuff but...
688
* For @,B,C and A without -s, the first device listed must be an md device
689
* we check that here and open it.
692
if (mode==MANAGE || mode == BUILD || mode == CREATE || mode == GROW ||
693
(mode == ASSEMBLE && ! scan)) {
694
if (devs_found < 1) {
695
fprintf(stderr, Name ": an md device must be given in this mode\n");
698
if ((int)ident.super_minor == -2 && autof) {
699
fprintf(stderr, Name ": --super-minor=dev is incompatible with --auto\n");
702
mdfd = open_mddev(devlist->devname, autof);
705
if ((int)ident.super_minor == -2) {
708
ident.super_minor = minor(stb.st_rdev);
715
/* readonly, add/remove, readwrite, runstop */
717
rv = Manage_ro(devlist->devname, mdfd, readonly);
718
if (!rv && devs_found>1)
719
rv = Manage_subdevs(devlist->devname, mdfd,
721
if (!rv && readonly < 0)
722
rv = Manage_ro(devlist->devname, mdfd, readonly);
724
rv = Manage_runstop(devlist->devname, mdfd, runstop, 0);
727
if (devs_found == 1 && ident.uuid_set == 0 &&
728
ident.super_minor == UnSet && !scan ) {
729
/* Only a device has been given, so get details from config file */
730
mddev_ident_t array_ident = conf_get_ident(configfile, devlist->devname);
731
if (array_ident == NULL) {
732
fprintf(stderr, Name ": %s not identified in config file.\n",
736
mdfd = open_mddev(devlist->devname,
737
array_ident->autof ? array_ident->autof : autof);
741
rv |= Assemble(devlist->devname, mdfd, array_ident, configfile,
743
readonly, runstop, update, verbose, force);
748
rv = Assemble(devlist->devname, mdfd, &ident, configfile,
750
readonly, runstop, update, verbose, force);
751
else if (devs_found>0) {
752
if (update && devs_found > 1) {
753
fprintf(stderr, Name ": can only update a single array at a time\n");
756
for (dv = devlist ; dv ; dv=dv->next) {
757
mddev_ident_t array_ident = conf_get_ident(configfile, dv->devname);
758
if (array_ident == NULL) {
759
fprintf(stderr, Name ": %s not identified in config file.\n",
764
mdfd = open_mddev(dv->devname,
765
array_ident->autof ?array_ident->autof : autof);
770
rv |= Assemble(dv->devname, mdfd, array_ident, configfile,
772
readonly, runstop, update, verbose, force);
776
mddev_ident_t array_list = conf_get_ident(configfile, NULL);
778
fprintf(stderr, Name ": No arrays found in config file\n");
781
for (; array_list; array_list = array_list->next) {
782
mdu_array_info_t array;
783
mdfd = open_mddev(array_list->devname,
784
array_list->autof ? array_list->autof : autof);
789
if (ioctl(mdfd, GET_ARRAY_INFO, &array)>=0)
790
/* already assembled, skip */
793
rv |= Assemble(array_list->devname, mdfd,
794
array_list, configfile,
796
readonly, runstop, NULL, verbose, force);
802
rv = Build(devlist->devname, mdfd, chunk, level, layout, raiddisks, devlist->next, assume_clean);
805
rv = Create(devlist->devname, mdfd, chunk, level, layout, size<0 ? 0 : size,
806
raiddisks, sparedisks,
807
devs_found-1, devlist->next, runstop, verbose, force);
811
if (devmode == 'E') {
812
if (devlist == NULL && !scan) {
813
fprintf(stderr, Name ": No devices to examine\n");
817
devlist = conf_get_devs(configfile);
818
if (devlist == NULL) {
819
fprintf(stderr, Name ": No devices listed in %s\n", configfile?configfile:DefaultConfFile);
822
rv = Examine(devlist, scan?(verbose>1?0:verbose+1):brief, scan, SparcAdjust);
824
if (devlist == NULL) {
825
if (devmode=='D' && scan) {
826
/* apply --detail to all devices in /proc/mdstat */
827
struct mdstat_ent *ms = mdstat_read(0);
828
struct mdstat_ent *e;
829
for (e=ms ; e ; e=e->next) {
830
char *name = get_md_name(e->devnum);
833
fprintf(stderr, Name ": cannot find device file for %s\n",
837
rv |= Detail(name, verbose>1?0:verbose+1, test);
840
} else if (devmode == 'S' && scan) {
841
/* apply --stop to all devices in /proc/mdstat */
842
/* Due to possible stacking of devices, repeat until
843
* nothing more can be stopped
848
struct mdstat_ent *ms = mdstat_read(0);
849
struct mdstat_ent *e;
851
if (!progress) last = 1;
852
progress = 0; err = 0;
853
for (e=ms ; e ; e=e->next) {
854
char *name = get_md_name(e->devnum);
857
fprintf(stderr, Name ": cannot find device file for %s\n",
861
mdfd = open_mddev(name, 0);
863
if (Manage_runstop(name, mdfd, -1, !last))
872
} while (!last && err);
874
fprintf(stderr, Name ": No devices given.\n");
878
for (dv=devlist ; dv; dv=dv->next) {
879
switch(dv->disposition) {
881
rv |= Detail(dv->devname, brief?1+verbose:0, test); continue;
882
case 'K': /* Zero superblock */
883
rv |= Kill(dv->devname, force); continue;
885
rv |= Query(dv->devname); continue;
887
mdfd = open_mddev(dv->devname, 0);
889
switch(dv->disposition) {
891
rv |= Manage_runstop(dv->devname, mdfd, 1, 0); break;
893
rv |= Manage_runstop(dv->devname, mdfd, -1, 0); break;
895
rv |= Manage_ro(dv->devname, mdfd, 1); break;
897
rv |= Manage_ro(dv->devname, mdfd, -1); break;
905
if (!devlist && !scan) {
906
fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n");
910
if (pidfile && !daemonise) {
911
fprintf(stderr, Name ": Cannot write a pid file when not in daemon mode\n");
915
rv= Monitor(devlist, mailaddr, program,
916
delay?delay:60, daemonise, scan, oneshot, configfile, test, pidfile);
920
if (devs_found > 1) {
923
if (size >= 0 || raiddisks) {
924
fprintf(stderr, Name ": --size, --raiddisks, and --add are exclusing in --grow mode\n");
928
for (dv=devlist->next; dv ; dv=dv->next) {
929
rv = Grow_Add_device(devlist->devname, mdfd, dv->devname);
933
} else if ((size >= 0) + (raiddisks != 0) + (layout != UnSet) > 1) {
934
fprintf(stderr, Name ": can change at most one of size, raiddisks, and layout\n");
937
} else if (layout != UnSet)
938
rv = Manage_reconfig(devlist->devname, mdfd, layout);
939
else if (size >= 0 || raiddisks)
940
rv = Manage_resize(devlist->devname, mdfd, size, raiddisks);
942
fprintf(stderr, Name ": no changes to --grow\n");