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
);
126 case 'v': verbose
= 1;
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(CREATE
,'f'): /* force honouring of device list */
470 case O(ASSEMBLE
,'f'): /* force assembly */
471 case O(MISC
,'f'): /* force zero */
475 /* now for the Assemble options */
476 case O(ASSEMBLE
,'u'): /* uuid of array */
477 if (ident
.uuid_set
) {
478 fprintf(stderr
, Name
": uuid cannot be set twice. "
479 "Second value %s.\n", optarg
);
482 if (parse_uuid(optarg
, ident
.uuid
))
485 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
490 case O(ASSEMBLE
,'m'): /* super-minor for array */
491 if (ident
.super_minor
!= UnSet
) {
492 fprintf(stderr
, Name
": super-minor cannot be set twice. "
493 "Second value: %s.\n", optarg
);
496 if (strcmp(optarg
, "dev")==0)
497 ident
.super_minor
= -2;
499 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
500 if (!optarg
[0] || *cp
) {
501 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
507 case O(ASSEMBLE
,'U'): /* update the superblock */
509 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
514 if (strcmp(update
, "sparc2.2")==0)
516 if (strcmp(update
, "super-minor") == 0)
518 if (strcmp(update
, "summaries")==0)
520 if (strcmp(update
, "resync")==0)
522 fprintf(stderr
, Name
": '--update %s' invalid. Only 'sparc2.2', 'super-minor', 'resync' or 'summaries' supported\n",update
);
525 case O(ASSEMBLE
,'c'): /* config file */
529 fprintf(stderr
, Name
": configfile cannot be set twice. "
530 "Second value is %s.\n", optarg
);
534 /* FIXME possibly check that config file exists. Even parse it */
536 case O(ASSEMBLE
,'s'): /* scan */
542 case O(MONITOR
,'m'): /* mail address */
544 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
550 case O(MONITOR
,'p'): /* alert program */
552 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
558 case O(MONITOR
,'d'): /* delay in seconds */
560 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
563 delay
= strtol(optarg
, &c
, 10);
564 if (!optarg
[0] || *c
|| delay
<1) {
565 fprintf(stderr
, Name
": invalid delay: %s\n",
571 case O(MONITOR
,'f'): /* daemonise */
574 case O(MONITOR
,'i'): /* pid */
576 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
581 case O(MONITOR
,'1'): /* oneshot */
584 case O(MONITOR
,'t'): /* test */
588 /* now the general management options. Some are applicable
589 * to other modes. None have arguments.
592 case O(MANAGE
,'a'): /* add a drive */
595 case O(MANAGE
,'r'): /* remove a drive */
598 case O(MANAGE
,'f'): /* set faulty */
602 case O(ASSEMBLE
,'R'):
604 case O(CREATE
,'R'): /* Run the array */
606 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
613 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
621 fprintf(stderr
, Name
": Cannot have both readonly and readwrite\n");
628 fprintf(stderr
, Name
": Cannot have both readwrite and readonly.\n");
642 if (devmode
&& devmode
!= opt
&&
643 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
644 fprintf(stderr
, Name
": --examine/-E cannot be given with -%c\n",
645 devmode
=='E'?opt
:devmode
);
655 if (devmode
!= 'E') {
656 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
662 /* We have now processed all the valid options. Anything else is
665 fprintf(stderr
, Name
": option %c not valid in %s mode\n",
666 opt
, map_num(modes
, mode
));
671 if (!mode
&& devs_found
) {
674 if (devlist
->disposition
== 0)
675 devlist
->disposition
= devmode
;
678 fputs(Usage
, stderr
);
681 /* Ok, got the option parsing out of the way
682 * hopefully it's mostly right but there might be some stuff
685 * That is mosty checked in the per-mode stuff but...
687 * For @,B,C and A without -s, the first device listed must be an md device
688 * we check that here and open it.
691 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
692 (mode
== ASSEMBLE
&& ! scan
)) {
693 if (devs_found
< 1) {
694 fprintf(stderr
, Name
": an md device must be given in this mode\n");
697 if ((int)ident
.super_minor
== -2 && autof
) {
698 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
701 mdfd
= open_mddev(devlist
->devname
, autof
);
704 if ((int)ident
.super_minor
== -2) {
707 ident
.super_minor
= minor(stb
.st_rdev
);
714 /* readonly, add/remove, readwrite, runstop */
716 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
717 if (!rv
&& devs_found
>1)
718 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
720 if (!rv
&& readonly
< 0)
721 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
723 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
);
726 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
727 ident
.super_minor
== UnSet
&& !scan
) {
728 /* Only a device has been given, so get details from config file */
729 mddev_ident_t array_ident
= conf_get_ident(configfile
, devlist
->devname
);
730 if (array_ident
== NULL
) {
731 fprintf(stderr
, Name
": %s not identified in config file.\n",
735 mdfd
= open_mddev(devlist
->devname
, array_ident
->autof
);
739 rv
|= Assemble(devlist
->devname
, mdfd
, array_ident
, configfile
,
741 readonly
, runstop
, update
, verbose
, force
);
746 rv
= Assemble(devlist
->devname
, mdfd
, &ident
, configfile
,
748 readonly
, runstop
, update
, verbose
, force
);
749 else if (devs_found
>0) {
750 if (update
&& devs_found
> 1) {
751 fprintf(stderr
, Name
": can only update a single array at a time\n");
754 for (dv
= devlist
; dv
; dv
=dv
->next
) {
755 mddev_ident_t array_ident
= conf_get_ident(configfile
, dv
->devname
);
756 if (array_ident
== NULL
) {
757 fprintf(stderr
, Name
": %s not identified in config file.\n",
762 mdfd
= open_mddev(dv
->devname
, array_ident
->autof
);
767 rv
|= Assemble(dv
->devname
, mdfd
, array_ident
, configfile
,
769 readonly
, runstop
, update
, verbose
, force
);
773 mddev_ident_t array_list
= conf_get_ident(configfile
, NULL
);
775 fprintf(stderr
, Name
": No arrays found in config file\n");
778 for (; array_list
; array_list
= array_list
->next
) {
779 mdu_array_info_t array
;
780 mdfd
= open_mddev(array_list
->devname
, array_list
->autof
);
785 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
)>=0)
786 /* already assembled, skip */
789 rv
|= Assemble(array_list
->devname
, mdfd
,
790 array_list
, configfile
,
792 readonly
, runstop
, NULL
, verbose
, force
);
798 rv
= Build(devlist
->devname
, mdfd
, chunk
, level
, layout
, raiddisks
, devlist
->next
, assume_clean
);
801 rv
= Create(devlist
->devname
, mdfd
, chunk
, level
, layout
, size
<0 ? 0 : size
,
802 raiddisks
, sparedisks
,
803 devs_found
-1, devlist
->next
, runstop
, verbose
, force
);
807 if (devmode
== 'E') {
808 if (devlist
== NULL
&& !scan
) {
809 fprintf(stderr
, Name
": No devices to examine\n");
813 devlist
= conf_get_devs(configfile
);
814 if (devlist
== NULL
) {
815 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
818 rv
= Examine(devlist
, scan
?!verbose
:brief
, scan
, SparcAdjust
);
820 if (devlist
== NULL
) {
821 if ((devmode
== 'S' ||devmode
=='D') && scan
) {
822 /* apply to all devices in /proc/mdstat */
823 struct mdstat_ent
*ms
= mdstat_read(0);
824 struct mdstat_ent
*e
;
825 if (devmode
== 'S') {
826 /* reverse order so that arrays made of arrays are stopped properly */
827 struct mdstat_ent
*sm
= NULL
;
828 while ((e
=ms
) != NULL
) {
835 for (e
=ms
; e
; e
=e
->next
) {
836 char *name
= get_md_name(e
->devnum
);
839 fprintf(stderr
, Name
": cannot find device file for %s\n",
844 rv
|= Detail(name
, !verbose
, test
);
845 else if (devmode
=='S') {
846 mdfd
= open_mddev(name
, 0);
848 rv
|= Manage_runstop(name
, mdfd
, -1);
855 fprintf(stderr
, Name
": No devices given.\n");
859 for (dv
=devlist
; dv
; dv
=dv
->next
) {
860 switch(dv
->disposition
) {
862 rv
|= Detail(dv
->devname
, brief
, test
); continue;
863 case 'K': /* Zero superblock */
864 rv
|= Kill(dv
->devname
, force
); continue;
866 rv
|= Query(dv
->devname
); continue;
868 mdfd
= open_mddev(dv
->devname
, 0);
870 switch(dv
->disposition
) {
872 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1); break;
874 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1); break;
876 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
878 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
886 if (!devlist
&& !scan
) {
887 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
891 if (pidfile
&& !daemonise
) {
892 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
896 rv
= Monitor(devlist
, mailaddr
, program
,
897 delay
?delay
:60, daemonise
, scan
, oneshot
, configfile
, test
, pidfile
);
901 if (devs_found
> 1) {
904 if (size
>= 0 || raiddisks
) {
905 fprintf(stderr
, Name
": --size, --raiddisks, and --add are exclusing in --grow mode\n");
909 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
910 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
914 } else if ((size
>= 0) + (raiddisks
!= 0) + (layout
!= UnSet
) > 1) {
915 fprintf(stderr
, Name
": can change at most one of size, raiddisks, and layout\n");
918 } else if (layout
!= UnSet
)
919 rv
= Manage_reconfig(devlist
->devname
, mdfd
, layout
);
920 else if (size
>= 0 || raiddisks
)
921 rv
= Manage_resize(devlist
->devname
, mdfd
, size
, raiddisks
);
923 fprintf(stderr
, Name
": no changes to --grow\n");