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
35 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
;
84 struct supertype
*ss
= NULL
;
92 ident
.raid_disks
= UnSet
;
93 ident
.super_minor
= UnSet
;
95 ident
.spare_group
= NULL
;
98 while ((option_index
= -1) ,
99 (opt
=getopt_long(argc
, argv
,
100 short_options
, long_options
,
101 &option_index
)) != -1) {
103 /* firstly, so mode-independant options */
107 if (option_index
> 0 &&
108 strcmp(long_options
[option_index
].name
, "help-options")==0)
109 help_text
= OptionHelp
;
112 case ASSEMBLE
: help_text
= Help_assemble
; break;
113 case BUILD
: help_text
= Help_build
; break;
114 case CREATE
: help_text
= Help_create
; break;
115 case MANAGE
: help_text
= Help_manage
; break;
116 case MISC
: help_text
= Help_misc
; break;
117 case MONITOR
: help_text
= Help_monitor
; break;
118 case GROW
: help_text
= Help_grow
; break;
120 fputs(help_text
,stderr
);
124 fputs(Version
, stderr
);
127 case 'v': verbose
= 1;
135 fputs(Usage
, stderr
);
138 /* second, figure out the mode.
139 * Some options force the mode. Others
140 * set the mode if it isn't already
144 case '@': /* just incase they say --manage */
145 newmode
= MANAGE
; break;
149 if (!mode
) newmode
= MANAGE
;
152 case 'A': newmode
= ASSEMBLE
; break;
153 case 'B': newmode
= BUILD
; break;
154 case 'C': newmode
= CREATE
; break;
155 case 'F': newmode
= MONITOR
;break;
156 case 'G': newmode
= GROW
; break;
161 case 'Q': newmode
= MISC
; break;
166 case 'K': if (!mode
) newmode
= MISC
; break;
168 if (mode
&& newmode
== mode
) {
169 /* everybody happy ! */
170 } else if (mode
&& newmode
!= mode
) {
172 fprintf(stderr
, Name
": ");
173 if (option_index
>= 0)
174 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
176 fprintf(stderr
, "-%c", opt
);
177 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
178 map_num(modes
, newmode
),
179 map_num(modes
, mode
));
181 } else if (!mode
&& newmode
) {
184 /* special case of -c --help */
186 ( strncmp(optarg
, "--h", 3)==0 ||
187 strncmp(optarg
, "-h", 2)==0)) {
188 fputs(Help_config
, stderr
);
192 /* If first option is a device, don't force the mode yet */
194 if (devs_found
== 0) {
195 dv
= malloc(sizeof(*dv
));
197 fprintf(stderr
, Name
": malloc failed\n");
200 dv
->devname
= optarg
;
201 dv
->disposition
= devmode
;
204 devlistend
= &dv
->next
;
209 /* No mode yet, and this is the second device ... */
210 fprintf(stderr
, Name
": An option must be given to set the mode before a second device is listed\n");
213 if (option_index
>= 0)
214 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
216 fprintf(stderr
, Name
": -%c", opt
);
217 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
221 /* if we just set the mode, then done */
233 /* an undecorated option - must be a device name.
235 if (devs_found
> 0 && mode
== '@' && !devmode
) {
236 fprintf(stderr
, Name
": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
239 if (devs_found
> 0 && mode
== 'G' && !devmode
) {
240 fprintf(stderr
, Name
": Must give one of -a for devices do add: %s\n", optarg
);
243 dv
= malloc(sizeof(*dv
));
245 fprintf(stderr
, Name
": malloc failed\n");
248 dv
->devname
= optarg
;
249 dv
->disposition
= devmode
;
252 devlistend
= &dv
->next
;
258 /* We've got a mode, and opt is now something else which
259 * could depend on the mode */
260 #define O(a,b) ((a<<8)|b)
261 switch (O(mode
,opt
)) {
263 case O(BUILD
,'c'): /* chunk or rounding */
265 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
266 "Second value is %s.\n", optarg
);
269 chunk
= strtol(optarg
, &c
, 10);
270 if (!optarg
[0] || *c
|| chunk
<4 || ((chunk
-1)&chunk
)) {
271 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
278 case O(ASSEMBLE
,'e'):
279 case O(MISC
,'e'): /* set metadata (superblock) information */
281 fprintf(stderr
, Name
": metadata information already given\n");
284 for(i
=0; !ss
&& superlist
[i
]; i
++)
285 ss
= superlist
[i
]->match_metadata_desc(optarg
);
288 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
294 case O(CREATE
,'z'): /* size */
296 fprintf(stderr
, Name
": size may only be specified once. "
297 "Second value is %s.\n", optarg
);
300 if (strcmp(optarg
, "max")==0)
303 size
= strtol(optarg
, &c
, 10);
304 if (!optarg
[0] || *c
|| size
< 4) {
305 fprintf(stderr
, Name
": invalid size: %s\n",
312 case O(GROW
,'l'): /* hack - needed to understand layout */
314 case O(BUILD
,'l'): /* set raid level*/
315 if (level
!= UnSet
) {
316 fprintf(stderr
, Name
": raid level may only be set once. "
317 "Second value is %s.\n", optarg
);
320 level
= map_name(pers
, optarg
);
321 if (level
== UnSet
) {
322 fprintf(stderr
, Name
": invalid raid level: %s\n",
326 if (level
!= 0 && level
!= -1 && level
!= 1 && level
!= -4 && level
!= -5 && mode
== BUILD
) {
327 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
331 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
332 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
339 case O(CREATE
,'p'): /* raid5 layout */
340 case O(BUILD
,'p'): /* faulty layout */
341 case O(GROW
, 'p'): /* faulty reconfig */
342 if (layout
!= UnSet
) {
343 fprintf(stderr
,Name
": layout may only be sent once. "
344 "Second value was %s\n", optarg
);
349 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
350 map_num(pers
, level
));
353 fprintf(stderr
, Name
": raid level must be given before layout.\n");
358 layout
= map_name(r5layout
, optarg
);
360 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
367 /* 'f' or 'n' followed by a number <= raid_disks */
368 if ((optarg
[0] != 'n' && optarg
[0] != 'f') ||
369 (copies
= strtoul(optarg
+1, &cp
, 10)) < 1 ||
372 fprintf(stderr
, Name
": layout for raid10 must be 'nNN' or 'fNN' where NN is a number, not %s\n", optarg
);
375 if (optarg
[0] == 'n')
376 layout
= 256 + copies
;
378 layout
= 1 + (copies
<<8);
385 int ln
= strcspn(optarg
, "0123456789");
386 char *m
= strdup(optarg
);
389 mode
= map_name(faultylayout
, m
);
391 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
395 layout
= mode
| (atoi(optarg
+ln
)<< ModeShift
);
401 case O(BUILD
,3): /* assume clean */
407 case O(BUILD
,'n'): /* number of raid disks */
409 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
413 raiddisks
= strtol(optarg
, &c
, 10);
414 if (!optarg
[0] || *c
|| raiddisks
<=0 || raiddisks
> MD_SB_DISKS
) {
415 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
419 if (raiddisks
== 1 && !force
&& level
!= -5) {
420 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
421 " a mistake. If you really mean it you will need to specify --force before\n"
422 " setting the number of drives.\n");
425 ident
.raid_disks
= raiddisks
;
428 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
430 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
434 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
435 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
439 sparedisks
= strtol(optarg
, &c
, 10);
440 if (!optarg
[0] || *c
|| sparedisks
< 0 || sparedisks
> MD_SB_DISKS
- raiddisks
) {
441 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
449 case O(ASSEMBLE
,'a'): /* auto-creation of device node */
452 else if (strcasecmp(optarg
,"no")==0)
454 else if (strcasecmp(optarg
,"yes")==0)
456 else if (strcasecmp(optarg
,"md")==0)
459 /* There might be digits, and maybe a hypen, at the end */
460 char *e
= optarg
+ strlen(optarg
);
463 while (e
> optarg
&& isdigit(e
[-1]))
467 if (num
<= 0) num
= 1;
469 if (e
> optarg
&& e
[-1] == '-')
472 if ((len
== 3 && strncasecmp(optarg
,"mdp",3)==0) ||
473 (len
== 1 && strncasecmp(optarg
,"p",1)==0) ||
474 (len
>= 4 && strncasecmp(optarg
,"part",4)==0))
477 fprintf(stderr
, Name
": --auto flag arg of \"%s\" unrecognised: use no,yes,md,mdp,part\n"
478 " optionally followed by a number.\n",
485 case O(BUILD
,'f'): /* force honouring '-n 1' */
486 case O(CREATE
,'f'): /* force honouring of device list */
487 case O(ASSEMBLE
,'f'): /* force assembly */
488 case O(MISC
,'f'): /* force zero */
492 /* now for the Assemble options */
493 case O(ASSEMBLE
,'u'): /* uuid of array */
494 if (ident
.uuid_set
) {
495 fprintf(stderr
, Name
": uuid cannot be set twice. "
496 "Second value %s.\n", optarg
);
499 if (parse_uuid(optarg
, ident
.uuid
))
502 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
507 case O(ASSEMBLE
,'m'): /* super-minor for array */
508 if (ident
.super_minor
!= UnSet
) {
509 fprintf(stderr
, Name
": super-minor cannot be set twice. "
510 "Second value: %s.\n", optarg
);
513 if (strcmp(optarg
, "dev")==0)
514 ident
.super_minor
= -2;
516 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
517 if (!optarg
[0] || *cp
) {
518 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
524 case O(ASSEMBLE
,'U'): /* update the superblock */
526 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
531 if (strcmp(update
, "sparc2.2")==0)
533 if (strcmp(update
, "super-minor") == 0)
535 if (strcmp(update
, "summaries")==0)
537 if (strcmp(update
, "resync")==0)
539 fprintf(stderr
, Name
": '--update %s' invalid. Only 'sparc2.2', 'super-minor', 'resync' or 'summaries' supported\n",update
);
542 case O(ASSEMBLE
,'c'): /* config file */
546 fprintf(stderr
, Name
": configfile cannot be set twice. "
547 "Second value is %s.\n", optarg
);
551 /* FIXME possibly check that config file exists. Even parse it */
553 case O(ASSEMBLE
,'s'): /* scan */
559 case O(MONITOR
,'m'): /* mail address */
561 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
567 case O(MONITOR
,'p'): /* alert program */
569 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
575 case O(MONITOR
,'d'): /* delay in seconds */
577 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
580 delay
= strtol(optarg
, &c
, 10);
581 if (!optarg
[0] || *c
|| delay
<1) {
582 fprintf(stderr
, Name
": invalid delay: %s\n",
588 case O(MONITOR
,'f'): /* daemonise */
591 case O(MONITOR
,'i'): /* pid */
593 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
598 case O(MONITOR
,'1'): /* oneshot */
601 case O(MONITOR
,'t'): /* test */
605 /* now the general management options. Some are applicable
606 * to other modes. None have arguments.
609 case O(MANAGE
,'a'): /* add a drive */
612 case O(MANAGE
,'r'): /* remove a drive */
615 case O(MANAGE
,'f'): /* set faulty */
619 case O(ASSEMBLE
,'R'):
621 case O(CREATE
,'R'): /* Run the array */
623 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
630 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
638 fprintf(stderr
, Name
": Cannot have both readonly and readwrite\n");
645 fprintf(stderr
, Name
": Cannot have both readwrite and readonly.\n");
659 if (devmode
&& devmode
!= opt
&&
660 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
661 fprintf(stderr
, Name
": --examine/-E cannot be given with -%c\n",
662 devmode
=='E'?opt
:devmode
);
672 if (devmode
!= 'E') {
673 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
679 /* We have now processed all the valid options. Anything else is
682 fprintf(stderr
, Name
": option %c not valid in %s mode\n",
683 opt
, map_num(modes
, mode
));
688 if (!mode
&& devs_found
) {
691 if (devlist
->disposition
== 0)
692 devlist
->disposition
= devmode
;
695 fputs(Usage
, stderr
);
698 /* Ok, got the option parsing out of the way
699 * hopefully it's mostly right but there might be some stuff
702 * That is mosty checked in the per-mode stuff but...
704 * For @,B,C and A without -s, the first device listed must be an md device
705 * we check that here and open it.
708 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
709 (mode
== ASSEMBLE
&& ! scan
)) {
710 if (devs_found
< 1) {
711 fprintf(stderr
, Name
": an md device must be given in this mode\n");
714 if ((int)ident
.super_minor
== -2 && autof
) {
715 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
718 mdfd
= open_mddev(devlist
->devname
, autof
);
721 if ((int)ident
.super_minor
== -2) {
724 ident
.super_minor
= minor(stb
.st_rdev
);
731 /* readonly, add/remove, readwrite, runstop */
733 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
734 if (!rv
&& devs_found
>1)
735 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
737 if (!rv
&& readonly
< 0)
738 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
740 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
);
743 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
744 ident
.super_minor
== UnSet
&& !scan
) {
745 /* Only a device has been given, so get details from config file */
746 mddev_ident_t array_ident
= conf_get_ident(configfile
, devlist
->devname
);
747 if (array_ident
== NULL
) {
748 fprintf(stderr
, Name
": %s not identified in config file.\n",
752 mdfd
= open_mddev(devlist
->devname
, array_ident
->autof
);
756 rv
|= Assemble(ss
, devlist
->devname
, mdfd
, array_ident
, configfile
,
758 readonly
, runstop
, update
, verbose
, force
);
763 rv
= Assemble(ss
, devlist
->devname
, mdfd
, &ident
, configfile
,
765 readonly
, runstop
, update
, verbose
, force
);
766 else if (devs_found
>0) {
767 if (update
&& devs_found
> 1) {
768 fprintf(stderr
, Name
": can only update a single array at a time\n");
771 for (dv
= devlist
; dv
; dv
=dv
->next
) {
772 mddev_ident_t array_ident
= conf_get_ident(configfile
, dv
->devname
);
773 if (array_ident
== NULL
) {
774 fprintf(stderr
, Name
": %s not identified in config file.\n",
779 mdfd
= open_mddev(dv
->devname
, array_ident
->autof
);
784 rv
|= Assemble(ss
, dv
->devname
, mdfd
, array_ident
, configfile
,
786 readonly
, runstop
, update
, verbose
, force
);
790 mddev_ident_t array_list
= conf_get_ident(configfile
, NULL
);
792 fprintf(stderr
, Name
": No arrays found in config file\n");
795 for (; array_list
; array_list
= array_list
->next
) {
796 mdu_array_info_t array
;
797 mdfd
= open_mddev(array_list
->devname
, array_list
->autof
);
802 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
)>=0)
803 /* already assembled, skip */
806 rv
|= Assemble(ss
, array_list
->devname
, mdfd
,
807 array_list
, configfile
,
809 readonly
, runstop
, NULL
, verbose
, force
);
815 rv
= Build(devlist
->devname
, mdfd
, chunk
, level
, layout
, raiddisks
, devlist
->next
, assume_clean
);
819 for(i
=0; !ss
&& superlist
[i
]; i
++)
820 ss
= superlist
[i
]->match_metadata_desc("default");
823 fprintf(stderr
, Name
": internal error - no default metadata style\n");
827 rv
= Create(ss
, devlist
->devname
, mdfd
, chunk
, level
, layout
, size
<0 ? 0 : size
,
828 raiddisks
, sparedisks
,
829 devs_found
-1, devlist
->next
, runstop
, verbose
, force
);
833 if (devmode
== 'E') {
834 if (devlist
== NULL
&& !scan
) {
835 fprintf(stderr
, Name
": No devices to examine\n");
839 devlist
= conf_get_devs(configfile
);
840 if (devlist
== NULL
) {
841 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
844 rv
= Examine(devlist
, scan
?!verbose
:brief
, scan
, SparcAdjust
, ss
);
846 if (devlist
== NULL
) {
847 if ((devmode
== 'S' ||devmode
=='D') && scan
) {
848 /* apply to all devices in /proc/mdstat */
849 struct mdstat_ent
*ms
= mdstat_read(0);
850 struct mdstat_ent
*e
;
851 if (devmode
== 'S') {
852 /* reverse order so that arrays made of arrays are stopped properly */
853 struct mdstat_ent
*sm
= NULL
;
854 while ((e
=ms
) != NULL
) {
861 for (e
=ms
; e
; e
=e
->next
) {
862 char *name
= get_md_name(e
->devnum
);
865 fprintf(stderr
, Name
": cannot find device file for %s\n",
870 rv
|= Detail(name
, !verbose
, test
);
871 else if (devmode
=='S') {
872 mdfd
= open_mddev(name
, 0);
874 rv
|= Manage_runstop(name
, mdfd
, -1);
881 fprintf(stderr
, Name
": No devices given.\n");
885 for (dv
=devlist
; dv
; dv
=dv
->next
) {
886 switch(dv
->disposition
) {
888 rv
|= Detail(dv
->devname
, brief
, test
); continue;
889 case 'K': /* Zero superblock */
890 rv
|= Kill(dv
->devname
, force
); continue;
892 rv
|= Query(dv
->devname
); continue;
894 mdfd
= open_mddev(dv
->devname
, 0);
896 switch(dv
->disposition
) {
898 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1); break;
900 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1); break;
902 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
904 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
912 if (!devlist
&& !scan
) {
913 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
917 if (pidfile
&& !daemonise
) {
918 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
922 rv
= Monitor(devlist
, mailaddr
, program
,
923 delay
?delay
:60, daemonise
, scan
, oneshot
, configfile
, test
, pidfile
);
927 if (devs_found
> 1) {
930 if (size
>= 0 || raiddisks
) {
931 fprintf(stderr
, Name
": --size, --raiddisks, and --add are exclusing in --grow mode\n");
935 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
936 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
940 } else if ((size
>= 0) + (raiddisks
!= 0) + (layout
!= UnSet
) > 1) {
941 fprintf(stderr
, Name
": can change at most one of size, raiddisks, and layout\n");
944 } else if (layout
!= UnSet
)
945 rv
= Manage_reconfig(devlist
->devname
, mdfd
, layout
);
946 else if (size
>= 0 || raiddisks
)
947 rv
= Manage_resize(devlist
->devname
, mdfd
, size
, raiddisks
);
949 fprintf(stderr
, Name
": no changes to --grow\n");