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
;
99 while ((option_index
= -1) ,
100 (opt
=getopt_long(argc
, argv
,
101 short_options
, long_options
,
102 &option_index
)) != -1) {
104 /* firstly, so mode-independant options */
108 if (option_index
> 0 &&
109 strcmp(long_options
[option_index
].name
, "help-options")==0)
110 help_text
= OptionHelp
;
113 case ASSEMBLE
: help_text
= Help_assemble
; break;
114 case BUILD
: help_text
= Help_build
; break;
115 case CREATE
: help_text
= Help_create
; break;
116 case MANAGE
: help_text
= Help_manage
; break;
117 case MISC
: help_text
= Help_misc
; break;
118 case MONITOR
: help_text
= Help_monitor
; break;
119 case GROW
: help_text
= Help_grow
; break;
121 fputs(help_text
,stderr
);
125 fputs(Version
, stderr
);
128 case 'v': verbose
= 1;
136 fputs(Usage
, stderr
);
139 /* second, figure out the mode.
140 * Some options force the mode. Others
141 * set the mode if it isn't already
145 case '@': /* just incase they say --manage */
146 newmode
= MANAGE
; break;
150 if (!mode
) newmode
= MANAGE
;
153 case 'A': newmode
= ASSEMBLE
; break;
154 case 'B': newmode
= BUILD
; break;
155 case 'C': newmode
= CREATE
; break;
156 case 'F': newmode
= MONITOR
;break;
157 case 'G': newmode
= GROW
; break;
162 case 'Q': newmode
= MISC
; break;
167 case 'K': if (!mode
) newmode
= MISC
; break;
169 if (mode
&& newmode
== mode
) {
170 /* everybody happy ! */
171 } else if (mode
&& newmode
!= mode
) {
173 fprintf(stderr
, Name
": ");
174 if (option_index
>= 0)
175 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
177 fprintf(stderr
, "-%c", opt
);
178 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
179 map_num(modes
, newmode
),
180 map_num(modes
, mode
));
182 } else if (!mode
&& newmode
) {
185 /* special case of -c --help */
187 ( strncmp(optarg
, "--h", 3)==0 ||
188 strncmp(optarg
, "-h", 2)==0)) {
189 fputs(Help_config
, stderr
);
193 /* If first option is a device, don't force the mode yet */
195 if (devs_found
== 0) {
196 dv
= malloc(sizeof(*dv
));
198 fprintf(stderr
, Name
": malloc failed\n");
201 dv
->devname
= optarg
;
202 dv
->disposition
= devmode
;
205 devlistend
= &dv
->next
;
210 /* No mode yet, and this is the second device ... */
211 fprintf(stderr
, Name
": An option must be given to set the mode before a second device is listed\n");
214 if (option_index
>= 0)
215 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
217 fprintf(stderr
, Name
": -%c", opt
);
218 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
222 /* if we just set the mode, then done */
234 /* an undecorated option - must be a device name.
236 if (devs_found
> 0 && mode
== '@' && !devmode
) {
237 fprintf(stderr
, Name
": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
240 if (devs_found
> 0 && mode
== 'G' && !devmode
) {
241 fprintf(stderr
, Name
": Must give one of -a for devices do add: %s\n", optarg
);
244 dv
= malloc(sizeof(*dv
));
246 fprintf(stderr
, Name
": malloc failed\n");
249 dv
->devname
= optarg
;
250 dv
->disposition
= devmode
;
253 devlistend
= &dv
->next
;
259 /* We've got a mode, and opt is now something else which
260 * could depend on the mode */
261 #define O(a,b) ((a<<8)|b)
262 switch (O(mode
,opt
)) {
264 case O(BUILD
,'c'): /* chunk or rounding */
266 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
267 "Second value is %s.\n", optarg
);
270 chunk
= strtol(optarg
, &c
, 10);
271 if (!optarg
[0] || *c
|| chunk
<4 || ((chunk
-1)&chunk
)) {
272 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
279 case O(ASSEMBLE
,'e'):
280 case O(MISC
,'e'): /* set metadata (superblock) information */
282 fprintf(stderr
, Name
": metadata information already given\n");
285 for(i
=0; !ss
&& superlist
[i
]; i
++)
286 ss
= superlist
[i
]->match_metadata_desc(optarg
);
289 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
295 case O(CREATE
,'z'): /* size */
297 fprintf(stderr
, Name
": size may only be specified once. "
298 "Second value is %s.\n", optarg
);
301 if (strcmp(optarg
, "max")==0)
304 size
= strtol(optarg
, &c
, 10);
305 if (!optarg
[0] || *c
|| size
< 4) {
306 fprintf(stderr
, Name
": invalid size: %s\n",
313 case O(GROW
,'l'): /* hack - needed to understand layout */
315 case O(BUILD
,'l'): /* set raid level*/
316 if (level
!= UnSet
) {
317 fprintf(stderr
, Name
": raid level may only be set once. "
318 "Second value is %s.\n", optarg
);
321 level
= map_name(pers
, optarg
);
322 if (level
== UnSet
) {
323 fprintf(stderr
, Name
": invalid raid level: %s\n",
327 if (level
!= 0 && level
!= -1 && level
!= 1 && level
!= -4 && level
!= -5 && mode
== BUILD
) {
328 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
332 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
333 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
340 case O(CREATE
,'p'): /* raid5 layout */
341 case O(BUILD
,'p'): /* faulty layout */
342 case O(GROW
, 'p'): /* faulty reconfig */
343 if (layout
!= UnSet
) {
344 fprintf(stderr
,Name
": layout may only be sent once. "
345 "Second value was %s\n", optarg
);
350 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
351 map_num(pers
, level
));
354 fprintf(stderr
, Name
": raid level must be given before layout.\n");
359 layout
= map_name(r5layout
, optarg
);
361 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
368 /* 'f' or 'n' followed by a number <= raid_disks */
369 if ((optarg
[0] != 'n' && optarg
[0] != 'f') ||
370 (copies
= strtoul(optarg
+1, &cp
, 10)) < 1 ||
373 fprintf(stderr
, Name
": layout for raid10 must be 'nNN' or 'fNN' where NN is a number, not %s\n", optarg
);
376 if (optarg
[0] == 'n')
377 layout
= 256 + copies
;
379 layout
= 1 + (copies
<<8);
386 int ln
= strcspn(optarg
, "0123456789");
387 char *m
= strdup(optarg
);
390 mode
= map_name(faultylayout
, m
);
392 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
396 layout
= mode
| (atoi(optarg
+ln
)<< ModeShift
);
402 case O(BUILD
,3): /* assume clean */
408 case O(BUILD
,'n'): /* number of raid disks */
410 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
414 raiddisks
= strtol(optarg
, &c
, 10);
415 if (!optarg
[0] || *c
|| raiddisks
<=0 || raiddisks
> MD_SB_DISKS
) {
416 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
420 if (raiddisks
== 1 && !force
&& level
!= -5) {
421 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
422 " a mistake. If you really mean it you will need to specify --force before\n"
423 " setting the number of drives.\n");
426 ident
.raid_disks
= raiddisks
;
429 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
431 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
435 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
436 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
440 sparedisks
= strtol(optarg
, &c
, 10);
441 if (!optarg
[0] || *c
|| sparedisks
< 0 || sparedisks
> MD_SB_DISKS
- raiddisks
) {
442 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
450 case O(ASSEMBLE
,'a'): /* auto-creation of device node */
453 else if (strcasecmp(optarg
,"no")==0)
455 else if (strcasecmp(optarg
,"yes")==0)
457 else if (strcasecmp(optarg
,"md")==0)
460 /* There might be digits, and maybe a hypen, at the end */
461 char *e
= optarg
+ strlen(optarg
);
464 while (e
> optarg
&& isdigit(e
[-1]))
468 if (num
<= 0) num
= 1;
470 if (e
> optarg
&& e
[-1] == '-')
473 if ((len
== 3 && strncasecmp(optarg
,"mdp",3)==0) ||
474 (len
== 1 && strncasecmp(optarg
,"p",1)==0) ||
475 (len
>= 4 && strncasecmp(optarg
,"part",4)==0))
478 fprintf(stderr
, Name
": --auto flag arg of \"%s\" unrecognised: use no,yes,md,mdp,part\n"
479 " optionally followed by a number.\n",
486 case O(BUILD
,'f'): /* force honouring '-n 1' */
487 case O(CREATE
,'f'): /* force honouring of device list */
488 case O(ASSEMBLE
,'f'): /* force assembly */
489 case O(MISC
,'f'): /* force zero */
493 /* now for the Assemble options */
494 case O(ASSEMBLE
,'u'): /* uuid of array */
495 if (ident
.uuid_set
) {
496 fprintf(stderr
, Name
": uuid cannot be set twice. "
497 "Second value %s.\n", optarg
);
500 if (parse_uuid(optarg
, ident
.uuid
))
503 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
508 case O(ASSEMBLE
,'m'): /* super-minor for array */
509 if (ident
.super_minor
!= UnSet
) {
510 fprintf(stderr
, Name
": super-minor cannot be set twice. "
511 "Second value: %s.\n", optarg
);
514 if (strcmp(optarg
, "dev")==0)
515 ident
.super_minor
= -2;
517 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
518 if (!optarg
[0] || *cp
) {
519 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
525 case O(ASSEMBLE
,'U'): /* update the superblock */
527 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
532 if (strcmp(update
, "sparc2.2")==0)
534 if (strcmp(update
, "super-minor") == 0)
536 if (strcmp(update
, "summaries")==0)
538 if (strcmp(update
, "resync")==0)
540 fprintf(stderr
, Name
": '--update %s' invalid. Only 'sparc2.2', 'super-minor', 'resync' or 'summaries' supported\n",update
);
543 case O(ASSEMBLE
,'c'): /* config file */
547 fprintf(stderr
, Name
": configfile cannot be set twice. "
548 "Second value is %s.\n", optarg
);
552 /* FIXME possibly check that config file exists. Even parse it */
554 case O(ASSEMBLE
,'s'): /* scan */
560 case O(MONITOR
,'m'): /* mail address */
562 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
568 case O(MONITOR
,'p'): /* alert program */
570 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
576 case O(MONITOR
,'d'): /* delay in seconds */
578 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
581 delay
= strtol(optarg
, &c
, 10);
582 if (!optarg
[0] || *c
|| delay
<1) {
583 fprintf(stderr
, Name
": invalid delay: %s\n",
589 case O(MONITOR
,'f'): /* daemonise */
592 case O(MONITOR
,'i'): /* pid */
594 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
599 case O(MONITOR
,'1'): /* oneshot */
602 case O(MONITOR
,'t'): /* test */
606 /* now the general management options. Some are applicable
607 * to other modes. None have arguments.
610 case O(MANAGE
,'a'): /* add a drive */
613 case O(MANAGE
,'r'): /* remove a drive */
616 case O(MANAGE
,'f'): /* set faulty */
620 case O(ASSEMBLE
,'R'):
622 case O(CREATE
,'R'): /* Run the array */
624 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
631 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
639 fprintf(stderr
, Name
": Cannot have both readonly and readwrite\n");
646 fprintf(stderr
, Name
": Cannot have both readwrite and readonly.\n");
660 if (devmode
&& devmode
!= opt
&&
661 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
662 fprintf(stderr
, Name
": --examine/-E cannot be given with -%c\n",
663 devmode
=='E'?opt
:devmode
);
673 if (devmode
!= 'E') {
674 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
680 /* We have now processed all the valid options. Anything else is
683 fprintf(stderr
, Name
": option %c not valid in %s mode\n",
684 opt
, map_num(modes
, mode
));
689 if (!mode
&& devs_found
) {
692 if (devlist
->disposition
== 0)
693 devlist
->disposition
= devmode
;
696 fputs(Usage
, stderr
);
699 /* Ok, got the option parsing out of the way
700 * hopefully it's mostly right but there might be some stuff
703 * That is mosty checked in the per-mode stuff but...
705 * For @,B,C and A without -s, the first device listed must be an md device
706 * we check that here and open it.
709 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
710 (mode
== ASSEMBLE
&& ! scan
)) {
711 if (devs_found
< 1) {
712 fprintf(stderr
, Name
": an md device must be given in this mode\n");
715 if ((int)ident
.super_minor
== -2 && autof
) {
716 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
719 mdfd
= open_mddev(devlist
->devname
, autof
);
722 if ((int)ident
.super_minor
== -2) {
725 ident
.super_minor
= minor(stb
.st_rdev
);
732 /* readonly, add/remove, readwrite, runstop */
734 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
735 if (!rv
&& devs_found
>1)
736 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
738 if (!rv
&& readonly
< 0)
739 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
741 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
);
744 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
745 ident
.super_minor
== UnSet
&& !scan
) {
746 /* Only a device has been given, so get details from config file */
747 mddev_ident_t array_ident
= conf_get_ident(configfile
, devlist
->devname
);
748 if (array_ident
== NULL
) {
749 fprintf(stderr
, Name
": %s not identified in config file.\n",
753 mdfd
= open_mddev(devlist
->devname
, array_ident
->autof
);
757 rv
|= Assemble(ss
, devlist
->devname
, mdfd
, array_ident
, configfile
,
759 readonly
, runstop
, update
, verbose
, force
);
764 rv
= Assemble(ss
, devlist
->devname
, mdfd
, &ident
, configfile
,
766 readonly
, runstop
, update
, verbose
, force
);
767 else if (devs_found
>0) {
768 if (update
&& devs_found
> 1) {
769 fprintf(stderr
, Name
": can only update a single array at a time\n");
772 for (dv
= devlist
; dv
; dv
=dv
->next
) {
773 mddev_ident_t array_ident
= conf_get_ident(configfile
, dv
->devname
);
774 if (array_ident
== NULL
) {
775 fprintf(stderr
, Name
": %s not identified in config file.\n",
780 mdfd
= open_mddev(dv
->devname
, array_ident
->autof
);
785 rv
|= Assemble(ss
, dv
->devname
, mdfd
, array_ident
, configfile
,
787 readonly
, runstop
, update
, verbose
, force
);
791 mddev_ident_t array_list
= conf_get_ident(configfile
, NULL
);
793 fprintf(stderr
, Name
": No arrays found in config file\n");
796 for (; array_list
; array_list
= array_list
->next
) {
797 mdu_array_info_t array
;
798 mdfd
= open_mddev(array_list
->devname
, array_list
->autof
);
803 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
)>=0)
804 /* already assembled, skip */
807 rv
|= Assemble(ss
, array_list
->devname
, mdfd
,
808 array_list
, configfile
,
810 readonly
, runstop
, NULL
, verbose
, force
);
816 rv
= Build(devlist
->devname
, mdfd
, chunk
, level
, layout
, raiddisks
, devlist
->next
, assume_clean
);
820 for(i
=0; !ss
&& superlist
[i
]; i
++)
821 ss
= superlist
[i
]->match_metadata_desc("default");
824 fprintf(stderr
, Name
": internal error - no default metadata style\n");
828 rv
= Create(ss
, devlist
->devname
, mdfd
, chunk
, level
, layout
, size
<0 ? 0 : size
,
829 raiddisks
, sparedisks
,
830 devs_found
-1, devlist
->next
, runstop
, verbose
, force
);
834 if (devmode
== 'E') {
835 if (devlist
== NULL
&& !scan
) {
836 fprintf(stderr
, Name
": No devices to examine\n");
840 devlist
= conf_get_devs(configfile
);
841 if (devlist
== NULL
) {
842 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
845 rv
= Examine(devlist
, scan
?!verbose
:brief
, scan
, SparcAdjust
, ss
);
847 if (devlist
== NULL
) {
848 if ((devmode
== 'S' ||devmode
=='D') && scan
) {
849 /* apply to all devices in /proc/mdstat */
850 struct mdstat_ent
*ms
= mdstat_read(0);
851 struct mdstat_ent
*e
;
852 if (devmode
== 'S') {
853 /* reverse order so that arrays made of arrays are stopped properly */
854 struct mdstat_ent
*sm
= NULL
;
855 while ((e
=ms
) != NULL
) {
862 for (e
=ms
; e
; e
=e
->next
) {
863 char *name
= get_md_name(e
->devnum
);
866 fprintf(stderr
, Name
": cannot find device file for %s\n",
871 rv
|= Detail(name
, !verbose
, test
);
872 else if (devmode
=='S') {
873 mdfd
= open_mddev(name
, 0);
875 rv
|= Manage_runstop(name
, mdfd
, -1);
882 fprintf(stderr
, Name
": No devices given.\n");
886 for (dv
=devlist
; dv
; dv
=dv
->next
) {
887 switch(dv
->disposition
) {
889 rv
|= Detail(dv
->devname
, brief
, test
); continue;
890 case 'K': /* Zero superblock */
891 rv
|= Kill(dv
->devname
, force
); continue;
893 rv
|= Query(dv
->devname
); continue;
895 mdfd
= open_mddev(dv
->devname
, 0);
897 switch(dv
->disposition
) {
899 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1); break;
901 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1); break;
903 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
905 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
913 if (!devlist
&& !scan
) {
914 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
918 if (pidfile
&& !daemonise
) {
919 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
923 rv
= Monitor(devlist
, mailaddr
, program
,
924 delay
?delay
:60, daemonise
, scan
, oneshot
, configfile
, test
, pidfile
);
928 if (devs_found
> 1) {
931 if (size
>= 0 || raiddisks
) {
932 fprintf(stderr
, Name
": --size, --raiddisks, and --add are exclusing in --grow mode\n");
936 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
937 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
941 } else if ((size
>= 0) + (raiddisks
!= 0) + (layout
!= UnSet
) > 1) {
942 fprintf(stderr
, Name
": can change at most one of size, raiddisks, and layout\n");
945 } else if (layout
!= UnSet
)
946 rv
= Manage_reconfig(devlist
->devname
, mdfd
, layout
);
947 else if (size
>= 0 || raiddisks
)
948 rv
= Manage_resize(devlist
->devname
, mdfd
, size
, raiddisks
);
950 fprintf(stderr
, Name
": no changes to --grow\n");