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; /* -1 for non-partitions, 1 or more to create partitions */
71 char *mailaddr
= NULL
;
84 ident
.raid_disks
= UnSet
;
85 ident
.super_minor
= UnSet
;
88 while ((option_index
= -1) ,
89 (opt
=getopt_long(argc
, argv
,
90 short_options
, long_options
,
91 &option_index
)) != -1) {
93 /* firstly, so mode-independant options */
97 if (option_index
> 0 &&
98 strcmp(long_options
[option_index
].name
, "help-options")==0)
99 help_text
= OptionHelp
;
102 case ASSEMBLE
: help_text
= Help_assemble
; break;
103 case BUILD
: help_text
= Help_build
; break;
104 case CREATE
: help_text
= Help_create
; break;
105 case MANAGE
: help_text
= Help_manage
; break;
106 case MISC
: help_text
= Help_misc
; break;
107 case MONITOR
: help_text
= Help_monitor
; break;
108 case GROW
: help_text
= Help_grow
; break;
110 fputs(help_text
,stderr
);
114 fputs(Version
, stderr
);
117 case 'v': verbose
= 1;
125 fputs(Usage
, stderr
);
128 /* second, figure out the mode.
129 * Some options force the mode. Others
130 * set the mode if it isn't already
134 case '@': /* just incase they say --manage */
135 newmode
= MANAGE
; break;
139 case 1 : if (!mode
) newmode
= MANAGE
; break;
141 case 'A': newmode
= ASSEMBLE
; break;
142 case 'B': newmode
= BUILD
; break;
143 case 'C': newmode
= CREATE
; break;
144 case 'F': newmode
= MONITOR
;break;
145 case 'G': newmode
= GROW
; break;
150 case 'Q': newmode
= MISC
; break;
155 case 'K': if (!mode
) newmode
= MISC
; break;
157 if (mode
&& newmode
== mode
) {
158 /* everybody happy ! */
159 } else if (mode
&& newmode
!= mode
) {
161 fprintf(stderr
, Name
": ");
162 if (option_index
>= 0)
163 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
165 fprintf(stderr
, "-%c", opt
);
166 fprintf(stderr
, " would set mode to %s, but it is already %s.\n",
167 map_num(modes
, newmode
),
168 map_num(modes
, mode
));
170 } else if (!mode
&& newmode
) {
173 /* special case of -c --help */
175 ( strncmp(optarg
, "--h", 3)==0 ||
176 strncmp(optarg
, "-h", 2)==0)) {
177 fputs(Help_config
, stderr
);
180 if (option_index
>= 0)
181 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
183 fprintf(stderr
, "-%c", opt
);
184 fprintf(stderr
, " does not set the mode, and so cannot be first.\n");
188 /* if we just set the mode, then done */
200 /* an undecorated option - must be a device name.
202 if (devs_found
> 0 && mode
== '@' && !devmode
) {
203 fprintf(stderr
, Name
": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
206 if (devs_found
> 0 && mode
== 'G' && !devmode
) {
207 fprintf(stderr
, Name
": Must give one of -a for devices do add: %s\n", optarg
);
210 dv
= malloc(sizeof(*dv
));
212 fprintf(stderr
, Name
": malloc failed\n");
215 dv
->devname
= optarg
;
216 dv
->disposition
= devmode
;
219 devlistend
= &dv
->next
;
225 /* We've got a mode, and opt is now something else which
226 * could depend on the mode */
227 #define O(a,b) ((a<<8)|b)
228 switch (O(mode
,opt
)) {
230 case O(BUILD
,'c'): /* chunk or rounding */
232 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
233 "Second value is %s.\n", optarg
);
236 chunk
= strtol(optarg
, &c
, 10);
237 if (!optarg
[0] || *c
|| chunk
<4 || ((chunk
-1)&chunk
)) {
238 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
245 case O(CREATE
,'z'): /* size */
247 fprintf(stderr
, Name
": size may only be specified once. "
248 "Second value is %s.\n", optarg
);
251 if (strcmp(optarg
, "max")==0)
254 size
= strtol(optarg
, &c
, 10);
255 if (!optarg
[0] || *c
|| size
< 4) {
256 fprintf(stderr
, Name
": invalid size: %s\n",
263 case O(GROW
,'l'): /* hack - needed to understand layout */
265 case O(BUILD
,'l'): /* set raid level*/
266 if (level
!= UnSet
) {
267 fprintf(stderr
, Name
": raid level may only be set once. "
268 "Second value is %s.\n", optarg
);
271 level
= map_name(pers
, optarg
);
272 if (level
== UnSet
) {
273 fprintf(stderr
, Name
": invalid raid level: %s\n",
277 if (level
!= 0 && level
!= -1 && level
!= 1 && level
!= -4 && level
!= -5 && mode
== BUILD
) {
278 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
282 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
283 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
290 case O(CREATE
,'p'): /* raid5 layout */
291 case O(BUILD
,'p'): /* faulty layout */
292 case O(GROW
, 'p'): /* faulty reconfig */
293 if (layout
!= UnSet
) {
294 fprintf(stderr
,Name
": layout may only be sent once. "
295 "Second value was %s\n", optarg
);
300 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
301 map_num(pers
, level
));
304 fprintf(stderr
, Name
": raid level must be given before layout.\n");
309 layout
= map_name(r5layout
, optarg
);
311 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
318 /* 'f' or 'n' followed by a number <= raid_disks */
319 if ((optarg
[0] != 'n' && optarg
[0] != 'f') ||
320 (copies
= strtoul(optarg
+1, &cp
, 10)) < 1 ||
323 fprintf(stderr
, Name
": layout for raid10 must be 'nNN' or 'fNN' where NN is a number, not %s\n", optarg
);
326 if (optarg
[0] == 'n')
327 layout
= 256 + copies
;
329 layout
= 1 + (copies
<<8);
336 int ln
= strcspn(optarg
, "0123456789");
337 char *m
= strdup(optarg
);
340 mode
= map_name(faultylayout
, m
);
342 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
346 layout
= mode
| (atoi(optarg
+ln
)<< ModeShift
);
352 case O(BUILD
,3): /* assume clean */
358 case O(BUILD
,'n'): /* number of raid disks */
360 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
364 raiddisks
= strtol(optarg
, &c
, 10);
365 if (!optarg
[0] || *c
|| raiddisks
<=0 || raiddisks
> MD_SB_DISKS
) {
366 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
370 if (raiddisks
== 1 && !force
&& level
!= -5) {
371 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
372 " a mistake. If you really mean it you will need to specify --force before\n"
373 " setting the number of drives.\n");
376 ident
.raid_disks
= raiddisks
;
379 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
381 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
385 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
386 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
390 sparedisks
= strtol(optarg
, &c
, 10);
391 if (!optarg
[0] || *c
|| sparedisks
< 0 || sparedisks
> MD_SB_DISKS
- raiddisks
) {
392 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
400 case O(ASSEMBLE
,'a'): /* auto-creation of device node */
403 else if (strcasecmp(optarg
,"no")==0)
405 else if (strcasecmp(optarg
,"yes")==0 || strcasecmp(optarg
,"md")==0)
408 /* There might be digits, and maybe a hypen, at the end */
409 char *e
= optarg
+ strlen(optarg
);
412 while (e
> optarg
&& isdigit(e
[-1]))
416 if (num
<= 0) num
= 1;
418 if (e
> optarg
&& e
[-1] == '-')
421 if ((len
== 3 && strncasecmp(optarg
,"mdp",3)==0) ||
422 (len
== 1 && strncasecmp(optarg
,"p",1)==0) ||
423 (len
>= 4 && strncasecmp(optarg
,"part",4)==0))
426 fprintf(stderr
, Name
": --auto flag arg of \"%s\" unrecognised: use no,yes,md,mdp,part\n"
427 " optionally followed by a number.\n",
434 case O(BUILD
,'f'): /* force honouring '-n 1' */
435 case O(CREATE
,'f'): /* force honouring of device list */
436 case O(ASSEMBLE
,'f'): /* force assembly */
437 case O(MISC
,'f'): /* force zero */
441 /* now for the Assemble options */
442 case O(ASSEMBLE
,'u'): /* uuid of array */
443 if (ident
.uuid_set
) {
444 fprintf(stderr
, Name
": uuid cannot be set twice. "
445 "Second value %s.\n", optarg
);
448 if (parse_uuid(optarg
, ident
.uuid
))
451 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
456 case O(ASSEMBLE
,'m'): /* super-minor for array */
457 if (ident
.super_minor
!= UnSet
) {
458 fprintf(stderr
, Name
": super-minor cannot be set twice. "
459 "Second value: %s.\n", optarg
);
462 if (strcmp(optarg
, "dev")==0)
463 ident
.super_minor
= -2;
465 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
466 if (!optarg
[0] || *cp
) {
467 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
473 case O(ASSEMBLE
,'U'): /* update the superblock */
475 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
480 if (strcmp(update
, "sparc2.2")==0)
482 if (strcmp(update
, "super-minor") == 0)
484 if (strcmp(update
, "summaries")==0)
486 if (strcmp(update
, "resync")==0)
488 fprintf(stderr
, Name
": '--update %s' invalid. Only 'sparc2.2', 'super-minor', 'resync' or 'summaries' supported\n",update
);
491 case O(ASSEMBLE
,'c'): /* config file */
495 fprintf(stderr
, Name
": configfile cannot be set twice. "
496 "Second value is %s.\n", optarg
);
500 /* FIXME possibly check that config file exists. Even parse it */
502 case O(ASSEMBLE
,'s'): /* scan */
508 case O(MONITOR
,'m'): /* mail address */
510 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
516 case O(MONITOR
,'p'): /* alert program */
518 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
524 case O(MONITOR
,'d'): /* delay in seconds */
526 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
529 delay
= strtol(optarg
, &c
, 10);
530 if (!optarg
[0] || *c
|| delay
<1) {
531 fprintf(stderr
, Name
": invalid delay: %s\n",
537 case O(MONITOR
,'f'): /* daemonise */
540 case O(MONITOR
,'i'): /* pid */
542 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
547 case O(MONITOR
,'1'): /* oneshot */
550 case O(MONITOR
,'t'): /* test */
554 /* now the general management options. Some are applicable
555 * to other modes. None have arguments.
558 case O(MANAGE
,'a'): /* add a drive */
561 case O(MANAGE
,'r'): /* remove a drive */
564 case O(MANAGE
,'f'): /* set faulty */
568 case O(ASSEMBLE
,'R'):
570 case O(CREATE
,'R'): /* Run the array */
572 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
579 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
587 fprintf(stderr
, Name
": Cannot have both readonly and readwrite\n");
594 fprintf(stderr
, Name
": Cannot have both readwrite and readonly.\n");
608 if (devmode
&& devmode
!= opt
&&
609 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
610 fprintf(stderr
, Name
": --examine/-E cannot be given with -%c\n",
611 devmode
=='E'?opt
:devmode
);
621 if (devmode
!= 'E') {
622 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
628 /* We have now processed all the valid options. Anything else is
631 fprintf(stderr
, Name
": option %c not valid in %s mode\n",
632 opt
, map_num(modes
, mode
));
638 fputs(Usage
, stderr
);
641 /* Ok, got the option parsing out of the way
642 * hopefully it's mostly right but there might be some stuff
645 * That is mosty checked in the per-mode stuff but...
647 * For @,B,C and A without -s, the first device listed must be an md device
648 * we check that here and open it.
651 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
652 (mode
== ASSEMBLE
&& ! scan
)) {
653 if (devs_found
< 1) {
654 fprintf(stderr
, Name
": an md device must be given in this mode\n");
657 if ((int)ident
.super_minor
== -2 && autof
) {
658 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
661 mdfd
= open_mddev(devlist
->devname
, autof
);
664 if ((int)ident
.super_minor
== -2) {
667 ident
.super_minor
= MINOR(stb
.st_rdev
);
674 /* readonly, add/remove, readwrite, runstop */
676 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
677 if (!rv
&& devs_found
>1)
678 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
680 if (!rv
&& readonly
< 0)
681 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
683 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
);
686 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
687 ident
.super_minor
== UnSet
&& !scan
) {
688 /* Only a device has been given, so get details from config file */
689 mddev_ident_t array_ident
= conf_get_ident(configfile
, devlist
->devname
);
690 if (array_ident
== NULL
) {
691 fprintf(stderr
, Name
": %s not identified in config file.\n",
695 mdfd
= open_mddev(devlist
->devname
, array_ident
->autof
);
699 rv
|= Assemble(devlist
->devname
, mdfd
, array_ident
, configfile
,
701 readonly
, runstop
, update
, verbose
, force
);
706 rv
= Assemble(devlist
->devname
, mdfd
, &ident
, configfile
,
708 readonly
, runstop
, update
, verbose
, force
);
709 else if (devs_found
>0) {
710 if (update
&& devs_found
> 1) {
711 fprintf(stderr
, Name
": can only update a single array at a time\n");
714 for (dv
= devlist
; dv
; dv
=dv
->next
) {
715 mddev_ident_t array_ident
= conf_get_ident(configfile
, dv
->devname
);
716 if (array_ident
== NULL
) {
717 fprintf(stderr
, Name
": %s not identified in config file.\n",
722 mdfd
= open_mddev(dv
->devname
, array_ident
->autof
);
727 rv
|= Assemble(dv
->devname
, mdfd
, array_ident
, configfile
,
729 readonly
, runstop
, update
, verbose
, force
);
733 mddev_ident_t array_list
= conf_get_ident(configfile
, NULL
);
735 fprintf(stderr
, Name
": No arrays found in config file\n");
738 for (; array_list
; array_list
= array_list
->next
) {
739 mdu_array_info_t array
;
740 mdfd
= open_mddev(array_list
->devname
, array_list
->autof
);
745 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
)>=0)
746 /* already assembled, skip */
748 rv
|= Assemble(array_list
->devname
, mdfd
,
749 array_list
, configfile
,
751 readonly
, runstop
, NULL
, verbose
, force
);
756 rv
= Build(devlist
->devname
, mdfd
, chunk
, level
, layout
, raiddisks
, devlist
->next
, assume_clean
);
759 rv
= Create(devlist
->devname
, mdfd
, chunk
, level
, layout
, size
<0 ? 0 : size
,
760 raiddisks
, sparedisks
,
761 devs_found
-1, devlist
->next
, runstop
, verbose
, force
);
765 if (devmode
== 'E') {
766 if (devlist
== NULL
&& !scan
) {
767 fprintf(stderr
, Name
": No devices to examine\n");
771 devlist
= conf_get_devs(configfile
);
772 if (devlist
== NULL
) {
773 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
776 rv
= Examine(devlist
, scan
?!verbose
:brief
, scan
, SparcAdjust
);
778 if (devlist
== NULL
) {
779 if ((devmode
== 'S' ||devmode
=='D') && scan
) {
780 /* apply to all devices in /proc/mdstat */
781 struct mdstat_ent
*ms
= mdstat_read(0);
782 struct mdstat_ent
*e
;
783 for (e
=ms
; e
; e
=e
->next
) {
784 char *name
= get_md_name(e
->devnum
);
787 fprintf(stderr
, Name
": cannot find device file for %s\n",
792 rv
|= Detail(name
, !verbose
, test
);
793 else if (devmode
=='S') {
794 mdfd
= open_mddev(name
, 0);
796 rv
|= Manage_runstop(name
, mdfd
, -1);
801 fprintf(stderr
, Name
": No devices given.\n");
805 for (dv
=devlist
; dv
; dv
=dv
->next
) {
806 switch(dv
->disposition
) {
808 rv
|= Detail(dv
->devname
, brief
, test
); continue;
809 case 'K': /* Zero superblock */
810 rv
|= Kill(dv
->devname
, force
); continue;
812 rv
|= Query(dv
->devname
); continue;
814 mdfd
= open_mddev(dv
->devname
, 0);
816 switch(dv
->disposition
) {
818 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1); break;
820 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1); break;
822 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
824 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
830 if (!devlist
&& !scan
) {
831 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
835 if (pidfile
&& !daemonise
) {
836 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
840 rv
= Monitor(devlist
, mailaddr
, program
,
841 delay
?delay
:60, daemonise
, scan
, oneshot
, configfile
, test
, pidfile
);
845 if (devs_found
> 1) {
848 if (size
>= 0 || raiddisks
) {
849 fprintf(stderr
, Name
": --size, --raiddisks, and --add are exclusing in --grow mode\n");
853 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
854 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
858 } else if ((size
>= 0) + (raiddisks
!= 0) + (layout
!= UnSet
) > 1) {
859 fprintf(stderr
, Name
": can change at most one of size, raiddisks, and layout\n");
862 } else if (layout
!= UnSet
)
863 rv
= Manage_reconfig(devlist
->devname
, mdfd
, layout
);
864 else if (size
>= 0 || raiddisks
)
865 rv
= Manage_resize(devlist
->devname
, mdfd
, size
, raiddisks
);
867 fprintf(stderr
, Name
": no changes to --grow\n");