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
;
87 ident
.spare_group
= NULL
;
90 while ((option_index
= -1) ,
91 (opt
=getopt_long(argc
, argv
,
92 short_options
, long_options
,
93 &option_index
)) != -1) {
95 /* firstly, so mode-independant options */
99 if (option_index
> 0 &&
100 strcmp(long_options
[option_index
].name
, "help-options")==0)
101 help_text
= OptionHelp
;
104 case ASSEMBLE
: help_text
= Help_assemble
; break;
105 case BUILD
: help_text
= Help_build
; break;
106 case CREATE
: help_text
= Help_create
; break;
107 case MANAGE
: help_text
= Help_manage
; break;
108 case MISC
: help_text
= Help_misc
; break;
109 case MONITOR
: help_text
= Help_monitor
; break;
110 case GROW
: help_text
= Help_grow
; break;
112 fputs(help_text
,stderr
);
116 fputs(Version
, stderr
);
119 case 'v': verbose
= 1;
127 fputs(Usage
, stderr
);
130 /* second, figure out the mode.
131 * Some options force the mode. Others
132 * set the mode if it isn't already
136 case '@': /* just incase they say --manage */
137 newmode
= MANAGE
; break;
141 case 1 : if (!mode
) newmode
= MANAGE
; break;
143 case 'A': newmode
= ASSEMBLE
; break;
144 case 'B': newmode
= BUILD
; break;
145 case 'C': newmode
= CREATE
; break;
146 case 'F': newmode
= MONITOR
;break;
147 case 'G': newmode
= GROW
; break;
152 case 'Q': newmode
= MISC
; break;
157 case 'K': if (!mode
) newmode
= MISC
; break;
159 if (mode
&& newmode
== mode
) {
160 /* everybody happy ! */
161 } else if (mode
&& newmode
!= mode
) {
163 fprintf(stderr
, Name
": ");
164 if (option_index
>= 0)
165 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
167 fprintf(stderr
, "-%c", opt
);
168 fprintf(stderr
, " would set mode to %s, but it is already %s.\n",
169 map_num(modes
, newmode
),
170 map_num(modes
, mode
));
172 } else if (!mode
&& newmode
) {
175 /* special case of -c --help */
177 ( strncmp(optarg
, "--h", 3)==0 ||
178 strncmp(optarg
, "-h", 2)==0)) {
179 fputs(Help_config
, stderr
);
182 if (option_index
>= 0)
183 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
185 fprintf(stderr
, "-%c", opt
);
186 fprintf(stderr
, " does not set the mode, and so cannot be first.\n");
190 /* if we just set the mode, then done */
202 /* an undecorated option - must be a device name.
204 if (devs_found
> 0 && mode
== '@' && !devmode
) {
205 fprintf(stderr
, Name
": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
208 if (devs_found
> 0 && mode
== 'G' && !devmode
) {
209 fprintf(stderr
, Name
": Must give one of -a for devices do add: %s\n", optarg
);
212 dv
= malloc(sizeof(*dv
));
214 fprintf(stderr
, Name
": malloc failed\n");
217 dv
->devname
= optarg
;
218 dv
->disposition
= devmode
;
221 devlistend
= &dv
->next
;
227 /* We've got a mode, and opt is now something else which
228 * could depend on the mode */
229 #define O(a,b) ((a<<8)|b)
230 switch (O(mode
,opt
)) {
232 case O(BUILD
,'c'): /* chunk or rounding */
234 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
235 "Second value is %s.\n", optarg
);
238 chunk
= strtol(optarg
, &c
, 10);
239 if (!optarg
[0] || *c
|| chunk
<4 || ((chunk
-1)&chunk
)) {
240 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
247 case O(CREATE
,'z'): /* size */
249 fprintf(stderr
, Name
": size may only be specified once. "
250 "Second value is %s.\n", optarg
);
253 if (strcmp(optarg
, "max")==0)
256 size
= strtol(optarg
, &c
, 10);
257 if (!optarg
[0] || *c
|| size
< 4) {
258 fprintf(stderr
, Name
": invalid size: %s\n",
265 case O(GROW
,'l'): /* hack - needed to understand layout */
267 case O(BUILD
,'l'): /* set raid level*/
268 if (level
!= UnSet
) {
269 fprintf(stderr
, Name
": raid level may only be set once. "
270 "Second value is %s.\n", optarg
);
273 level
= map_name(pers
, optarg
);
274 if (level
== UnSet
) {
275 fprintf(stderr
, Name
": invalid raid level: %s\n",
279 if (level
!= 0 && level
!= -1 && level
!= 1 && level
!= -4 && level
!= -5 && mode
== BUILD
) {
280 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
284 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
285 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
292 case O(CREATE
,'p'): /* raid5 layout */
293 case O(BUILD
,'p'): /* faulty layout */
294 case O(GROW
, 'p'): /* faulty reconfig */
295 if (layout
!= UnSet
) {
296 fprintf(stderr
,Name
": layout may only be sent once. "
297 "Second value was %s\n", optarg
);
302 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
303 map_num(pers
, level
));
306 fprintf(stderr
, Name
": raid level must be given before layout.\n");
311 layout
= map_name(r5layout
, optarg
);
313 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
320 /* 'f' or 'n' followed by a number <= raid_disks */
321 if ((optarg
[0] != 'n' && optarg
[0] != 'f') ||
322 (copies
= strtoul(optarg
+1, &cp
, 10)) < 1 ||
325 fprintf(stderr
, Name
": layout for raid10 must be 'nNN' or 'fNN' where NN is a number, not %s\n", optarg
);
328 if (optarg
[0] == 'n')
329 layout
= 256 + copies
;
331 layout
= 1 + (copies
<<8);
338 int ln
= strcspn(optarg
, "0123456789");
339 char *m
= strdup(optarg
);
342 mode
= map_name(faultylayout
, m
);
344 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
348 layout
= mode
| (atoi(optarg
+ln
)<< ModeShift
);
354 case O(BUILD
,3): /* assume clean */
360 case O(BUILD
,'n'): /* number of raid disks */
362 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
366 raiddisks
= strtol(optarg
, &c
, 10);
367 if (!optarg
[0] || *c
|| raiddisks
<=0 || raiddisks
> MD_SB_DISKS
) {
368 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
372 if (raiddisks
== 1 && !force
&& level
!= -5) {
373 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
374 " a mistake. If you really mean it you will need to specify --force before\n"
375 " setting the number of drives.\n");
378 ident
.raid_disks
= raiddisks
;
381 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
383 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
387 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
388 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
392 sparedisks
= strtol(optarg
, &c
, 10);
393 if (!optarg
[0] || *c
|| sparedisks
< 0 || sparedisks
> MD_SB_DISKS
- raiddisks
) {
394 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
402 case O(ASSEMBLE
,'a'): /* auto-creation of device node */
405 else if (strcasecmp(optarg
,"no")==0)
407 else if (strcasecmp(optarg
,"yes")==0 || strcasecmp(optarg
,"md")==0)
410 /* There might be digits, and maybe a hypen, at the end */
411 char *e
= optarg
+ strlen(optarg
);
414 while (e
> optarg
&& isdigit(e
[-1]))
418 if (num
<= 0) num
= 1;
420 if (e
> optarg
&& e
[-1] == '-')
423 if ((len
== 3 && strncasecmp(optarg
,"mdp",3)==0) ||
424 (len
== 1 && strncasecmp(optarg
,"p",1)==0) ||
425 (len
>= 4 && strncasecmp(optarg
,"part",4)==0))
428 fprintf(stderr
, Name
": --auto flag arg of \"%s\" unrecognised: use no,yes,md,mdp,part\n"
429 " optionally followed by a number.\n",
436 case O(BUILD
,'f'): /* force honouring '-n 1' */
437 case O(CREATE
,'f'): /* force honouring of device list */
438 case O(ASSEMBLE
,'f'): /* force assembly */
439 case O(MISC
,'f'): /* force zero */
443 /* now for the Assemble options */
444 case O(ASSEMBLE
,'u'): /* uuid of array */
445 if (ident
.uuid_set
) {
446 fprintf(stderr
, Name
": uuid cannot be set twice. "
447 "Second value %s.\n", optarg
);
450 if (parse_uuid(optarg
, ident
.uuid
))
453 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
458 case O(ASSEMBLE
,'m'): /* super-minor for array */
459 if (ident
.super_minor
!= UnSet
) {
460 fprintf(stderr
, Name
": super-minor cannot be set twice. "
461 "Second value: %s.\n", optarg
);
464 if (strcmp(optarg
, "dev")==0)
465 ident
.super_minor
= -2;
467 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
468 if (!optarg
[0] || *cp
) {
469 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
475 case O(ASSEMBLE
,'U'): /* update the superblock */
477 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
482 if (strcmp(update
, "sparc2.2")==0)
484 if (strcmp(update
, "super-minor") == 0)
486 if (strcmp(update
, "summaries")==0)
488 if (strcmp(update
, "resync")==0)
490 fprintf(stderr
, Name
": '--update %s' invalid. Only 'sparc2.2', 'super-minor', 'resync' or 'summaries' supported\n",update
);
493 case O(ASSEMBLE
,'c'): /* config file */
497 fprintf(stderr
, Name
": configfile cannot be set twice. "
498 "Second value is %s.\n", optarg
);
502 /* FIXME possibly check that config file exists. Even parse it */
504 case O(ASSEMBLE
,'s'): /* scan */
510 case O(MONITOR
,'m'): /* mail address */
512 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
518 case O(MONITOR
,'p'): /* alert program */
520 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
526 case O(MONITOR
,'d'): /* delay in seconds */
528 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
531 delay
= strtol(optarg
, &c
, 10);
532 if (!optarg
[0] || *c
|| delay
<1) {
533 fprintf(stderr
, Name
": invalid delay: %s\n",
539 case O(MONITOR
,'f'): /* daemonise */
542 case O(MONITOR
,'i'): /* pid */
544 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
549 case O(MONITOR
,'1'): /* oneshot */
552 case O(MONITOR
,'t'): /* test */
556 /* now the general management options. Some are applicable
557 * to other modes. None have arguments.
560 case O(MANAGE
,'a'): /* add a drive */
563 case O(MANAGE
,'r'): /* remove a drive */
566 case O(MANAGE
,'f'): /* set faulty */
570 case O(ASSEMBLE
,'R'):
572 case O(CREATE
,'R'): /* Run the array */
574 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
581 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
589 fprintf(stderr
, Name
": Cannot have both readonly and readwrite\n");
596 fprintf(stderr
, Name
": Cannot have both readwrite and readonly.\n");
610 if (devmode
&& devmode
!= opt
&&
611 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
612 fprintf(stderr
, Name
": --examine/-E cannot be given with -%c\n",
613 devmode
=='E'?opt
:devmode
);
623 if (devmode
!= 'E') {
624 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
630 /* We have now processed all the valid options. Anything else is
633 fprintf(stderr
, Name
": option %c not valid in %s mode\n",
634 opt
, map_num(modes
, mode
));
640 fputs(Usage
, stderr
);
643 /* Ok, got the option parsing out of the way
644 * hopefully it's mostly right but there might be some stuff
647 * That is mosty checked in the per-mode stuff but...
649 * For @,B,C and A without -s, the first device listed must be an md device
650 * we check that here and open it.
653 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
654 (mode
== ASSEMBLE
&& ! scan
)) {
655 if (devs_found
< 1) {
656 fprintf(stderr
, Name
": an md device must be given in this mode\n");
659 if ((int)ident
.super_minor
== -2 && autof
) {
660 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
663 mdfd
= open_mddev(devlist
->devname
, autof
);
666 if ((int)ident
.super_minor
== -2) {
669 ident
.super_minor
= MINOR(stb
.st_rdev
);
676 /* readonly, add/remove, readwrite, runstop */
678 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
679 if (!rv
&& devs_found
>1)
680 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
682 if (!rv
&& readonly
< 0)
683 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
685 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
);
688 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
689 ident
.super_minor
== UnSet
&& !scan
) {
690 /* Only a device has been given, so get details from config file */
691 mddev_ident_t array_ident
= conf_get_ident(configfile
, devlist
->devname
);
692 if (array_ident
== NULL
) {
693 fprintf(stderr
, Name
": %s not identified in config file.\n",
697 mdfd
= open_mddev(devlist
->devname
, array_ident
->autof
);
701 rv
|= Assemble(devlist
->devname
, mdfd
, array_ident
, configfile
,
703 readonly
, runstop
, update
, verbose
, force
);
708 rv
= Assemble(devlist
->devname
, mdfd
, &ident
, configfile
,
710 readonly
, runstop
, update
, verbose
, force
);
711 else if (devs_found
>0) {
712 if (update
&& devs_found
> 1) {
713 fprintf(stderr
, Name
": can only update a single array at a time\n");
716 for (dv
= devlist
; dv
; dv
=dv
->next
) {
717 mddev_ident_t array_ident
= conf_get_ident(configfile
, dv
->devname
);
718 if (array_ident
== NULL
) {
719 fprintf(stderr
, Name
": %s not identified in config file.\n",
724 mdfd
= open_mddev(dv
->devname
, array_ident
->autof
);
729 rv
|= Assemble(dv
->devname
, mdfd
, array_ident
, configfile
,
731 readonly
, runstop
, update
, verbose
, force
);
735 mddev_ident_t array_list
= conf_get_ident(configfile
, NULL
);
737 fprintf(stderr
, Name
": No arrays found in config file\n");
740 for (; array_list
; array_list
= array_list
->next
) {
741 mdu_array_info_t array
;
742 mdfd
= open_mddev(array_list
->devname
, array_list
->autof
);
747 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
)>=0)
748 /* already assembled, skip */
750 rv
|= Assemble(array_list
->devname
, mdfd
,
751 array_list
, configfile
,
753 readonly
, runstop
, NULL
, verbose
, force
);
758 rv
= Build(devlist
->devname
, mdfd
, chunk
, level
, layout
, raiddisks
, devlist
->next
, assume_clean
);
761 rv
= Create(devlist
->devname
, mdfd
, chunk
, level
, layout
, size
<0 ? 0 : size
,
762 raiddisks
, sparedisks
,
763 devs_found
-1, devlist
->next
, runstop
, verbose
, force
);
767 if (devmode
== 'E') {
768 if (devlist
== NULL
&& !scan
) {
769 fprintf(stderr
, Name
": No devices to examine\n");
773 devlist
= conf_get_devs(configfile
);
774 if (devlist
== NULL
) {
775 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
778 rv
= Examine(devlist
, scan
?!verbose
:brief
, scan
, SparcAdjust
);
780 if (devlist
== NULL
) {
781 if ((devmode
== 'S' ||devmode
=='D') && scan
) {
782 /* apply to all devices in /proc/mdstat */
783 struct mdstat_ent
*ms
= mdstat_read(0);
784 struct mdstat_ent
*e
;
785 for (e
=ms
; e
; e
=e
->next
) {
786 char *name
= get_md_name(e
->devnum
);
789 fprintf(stderr
, Name
": cannot find device file for %s\n",
794 rv
|= Detail(name
, !verbose
, test
);
795 else if (devmode
=='S') {
796 mdfd
= open_mddev(name
, 0);
798 rv
|= Manage_runstop(name
, mdfd
, -1);
803 fprintf(stderr
, Name
": No devices given.\n");
807 for (dv
=devlist
; dv
; dv
=dv
->next
) {
808 switch(dv
->disposition
) {
810 rv
|= Detail(dv
->devname
, brief
, test
); continue;
811 case 'K': /* Zero superblock */
812 rv
|= Kill(dv
->devname
, force
); continue;
814 rv
|= Query(dv
->devname
); continue;
816 mdfd
= open_mddev(dv
->devname
, 0);
818 switch(dv
->disposition
) {
820 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1); break;
822 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1); break;
824 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
826 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
832 if (!devlist
&& !scan
) {
833 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
837 if (pidfile
&& !daemonise
) {
838 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
842 rv
= Monitor(devlist
, mailaddr
, program
,
843 delay
?delay
:60, daemonise
, scan
, oneshot
, configfile
, test
, pidfile
);
847 if (devs_found
> 1) {
850 if (size
>= 0 || raiddisks
) {
851 fprintf(stderr
, Name
": --size, --raiddisks, and --add are exclusing in --grow mode\n");
855 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
856 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
860 } else if ((size
>= 0) + (raiddisks
!= 0) + (layout
!= UnSet
) > 1) {
861 fprintf(stderr
, Name
": can change at most one of size, raiddisks, and layout\n");
864 } else if (layout
!= UnSet
)
865 rv
= Manage_reconfig(devlist
->devname
, mdfd
, layout
);
866 else if (size
>= 0 || raiddisks
)
867 rv
= Manage_resize(devlist
->devname
, mdfd
, size
, raiddisks
);
869 fprintf(stderr
, Name
": no changes to --grow\n");