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 case 1 : if (!mode
) newmode
= MANAGE
; break;
150 case 'A': newmode
= ASSEMBLE
; break;
151 case 'B': newmode
= BUILD
; break;
152 case 'C': newmode
= CREATE
; break;
153 case 'F': newmode
= MONITOR
;break;
154 case 'G': newmode
= GROW
; break;
159 case 'Q': newmode
= MISC
; break;
164 case 'K': if (!mode
) newmode
= MISC
; break;
166 if (mode
&& newmode
== mode
) {
167 /* everybody happy ! */
168 } else if (mode
&& newmode
!= mode
) {
170 fprintf(stderr
, Name
": ");
171 if (option_index
>= 0)
172 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
174 fprintf(stderr
, "-%c", opt
);
175 fprintf(stderr
, " would set mode to %s, but it is already %s.\n",
176 map_num(modes
, newmode
),
177 map_num(modes
, mode
));
179 } else if (!mode
&& newmode
) {
182 /* special case of -c --help */
184 ( strncmp(optarg
, "--h", 3)==0 ||
185 strncmp(optarg
, "-h", 2)==0)) {
186 fputs(Help_config
, stderr
);
189 if (option_index
>= 0)
190 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
192 fprintf(stderr
, "-%c", opt
);
193 fprintf(stderr
, " does not set the mode, and so cannot be first.\n");
197 /* if we just set the mode, then done */
209 /* an undecorated option - must be a device name.
211 if (devs_found
> 0 && mode
== '@' && !devmode
) {
212 fprintf(stderr
, Name
": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
215 if (devs_found
> 0 && mode
== 'G' && !devmode
) {
216 fprintf(stderr
, Name
": Must give one of -a for devices do add: %s\n", optarg
);
219 dv
= malloc(sizeof(*dv
));
221 fprintf(stderr
, Name
": malloc failed\n");
224 dv
->devname
= optarg
;
225 dv
->disposition
= devmode
;
228 devlistend
= &dv
->next
;
234 /* We've got a mode, and opt is now something else which
235 * could depend on the mode */
236 #define O(a,b) ((a<<8)|b)
237 switch (O(mode
,opt
)) {
239 case O(BUILD
,'c'): /* chunk or rounding */
241 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
242 "Second value is %s.\n", optarg
);
245 chunk
= strtol(optarg
, &c
, 10);
246 if (!optarg
[0] || *c
|| chunk
<4 || ((chunk
-1)&chunk
)) {
247 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
254 case O(CREATE
,'z'): /* size */
256 fprintf(stderr
, Name
": size may only be specified once. "
257 "Second value is %s.\n", optarg
);
260 if (strcmp(optarg
, "max")==0)
263 size
= strtol(optarg
, &c
, 10);
264 if (!optarg
[0] || *c
|| size
< 4) {
265 fprintf(stderr
, Name
": invalid size: %s\n",
272 case O(GROW
,'l'): /* hack - needed to understand layout */
274 case O(BUILD
,'l'): /* set raid level*/
275 if (level
!= UnSet
) {
276 fprintf(stderr
, Name
": raid level may only be set once. "
277 "Second value is %s.\n", optarg
);
280 level
= map_name(pers
, optarg
);
281 if (level
== UnSet
) {
282 fprintf(stderr
, Name
": invalid raid level: %s\n",
286 if (level
!= 0 && level
!= -1 && level
!= 1 && level
!= -4 && level
!= -5 && mode
== BUILD
) {
287 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
291 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
292 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
299 case O(CREATE
,'p'): /* raid5 layout */
300 case O(BUILD
,'p'): /* faulty layout */
301 case O(GROW
, 'p'): /* faulty reconfig */
302 if (layout
!= UnSet
) {
303 fprintf(stderr
,Name
": layout may only be sent once. "
304 "Second value was %s\n", optarg
);
309 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
310 map_num(pers
, level
));
313 fprintf(stderr
, Name
": raid level must be given before layout.\n");
318 layout
= map_name(r5layout
, optarg
);
320 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
327 /* 'f' or 'n' followed by a number <= raid_disks */
328 if ((optarg
[0] != 'n' && optarg
[0] != 'f') ||
329 (copies
= strtoul(optarg
+1, &cp
, 10)) < 1 ||
332 fprintf(stderr
, Name
": layout for raid10 must be 'nNN' or 'fNN' where NN is a number, not %s\n", optarg
);
335 if (optarg
[0] == 'n')
336 layout
= 256 + copies
;
338 layout
= 1 + (copies
<<8);
345 int ln
= strcspn(optarg
, "0123456789");
346 char *m
= strdup(optarg
);
349 mode
= map_name(faultylayout
, m
);
351 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
355 layout
= mode
| (atoi(optarg
+ln
)<< ModeShift
);
361 case O(BUILD
,3): /* assume clean */
367 case O(BUILD
,'n'): /* number of raid disks */
369 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
373 raiddisks
= strtol(optarg
, &c
, 10);
374 if (!optarg
[0] || *c
|| raiddisks
<=0 || raiddisks
> MD_SB_DISKS
) {
375 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
379 if (raiddisks
== 1 && !force
&& level
!= -5) {
380 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
381 " a mistake. If you really mean it you will need to specify --force before\n"
382 " setting the number of drives.\n");
385 ident
.raid_disks
= raiddisks
;
388 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
390 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
394 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
395 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
399 sparedisks
= strtol(optarg
, &c
, 10);
400 if (!optarg
[0] || *c
|| sparedisks
< 0 || sparedisks
> MD_SB_DISKS
- raiddisks
) {
401 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
409 case O(ASSEMBLE
,'a'): /* auto-creation of device node */
412 else if (strcasecmp(optarg
,"no")==0)
414 else if (strcasecmp(optarg
,"yes")==0)
416 else if (strcasecmp(optarg
,"md")==0)
419 /* There might be digits, and maybe a hypen, at the end */
420 char *e
= optarg
+ strlen(optarg
);
423 while (e
> optarg
&& isdigit(e
[-1]))
427 if (num
<= 0) num
= 1;
429 if (e
> optarg
&& e
[-1] == '-')
432 if ((len
== 3 && strncasecmp(optarg
,"mdp",3)==0) ||
433 (len
== 1 && strncasecmp(optarg
,"p",1)==0) ||
434 (len
>= 4 && strncasecmp(optarg
,"part",4)==0))
437 fprintf(stderr
, Name
": --auto flag arg of \"%s\" unrecognised: use no,yes,md,mdp,part\n"
438 " optionally followed by a number.\n",
445 case O(BUILD
,'f'): /* force honouring '-n 1' */
446 case O(CREATE
,'f'): /* force honouring of device list */
447 case O(ASSEMBLE
,'f'): /* force assembly */
448 case O(MISC
,'f'): /* force zero */
452 /* now for the Assemble options */
453 case O(ASSEMBLE
,'u'): /* uuid of array */
454 if (ident
.uuid_set
) {
455 fprintf(stderr
, Name
": uuid cannot be set twice. "
456 "Second value %s.\n", optarg
);
459 if (parse_uuid(optarg
, ident
.uuid
))
462 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
467 case O(ASSEMBLE
,'m'): /* super-minor for array */
468 if (ident
.super_minor
!= UnSet
) {
469 fprintf(stderr
, Name
": super-minor cannot be set twice. "
470 "Second value: %s.\n", optarg
);
473 if (strcmp(optarg
, "dev")==0)
474 ident
.super_minor
= -2;
476 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
477 if (!optarg
[0] || *cp
) {
478 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
484 case O(ASSEMBLE
,'U'): /* update the superblock */
486 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
491 if (strcmp(update
, "sparc2.2")==0)
493 if (strcmp(update
, "super-minor") == 0)
495 if (strcmp(update
, "summaries")==0)
497 if (strcmp(update
, "resync")==0)
499 fprintf(stderr
, Name
": '--update %s' invalid. Only 'sparc2.2', 'super-minor', 'resync' or 'summaries' supported\n",update
);
502 case O(ASSEMBLE
,'c'): /* config file */
506 fprintf(stderr
, Name
": configfile cannot be set twice. "
507 "Second value is %s.\n", optarg
);
511 /* FIXME possibly check that config file exists. Even parse it */
513 case O(ASSEMBLE
,'s'): /* scan */
519 case O(MONITOR
,'m'): /* mail address */
521 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
527 case O(MONITOR
,'p'): /* alert program */
529 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
535 case O(MONITOR
,'d'): /* delay in seconds */
537 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
540 delay
= strtol(optarg
, &c
, 10);
541 if (!optarg
[0] || *c
|| delay
<1) {
542 fprintf(stderr
, Name
": invalid delay: %s\n",
548 case O(MONITOR
,'f'): /* daemonise */
551 case O(MONITOR
,'i'): /* pid */
553 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
558 case O(MONITOR
,'1'): /* oneshot */
561 case O(MONITOR
,'t'): /* test */
565 /* now the general management options. Some are applicable
566 * to other modes. None have arguments.
569 case O(MANAGE
,'a'): /* add a drive */
572 case O(MANAGE
,'r'): /* remove a drive */
575 case O(MANAGE
,'f'): /* set faulty */
579 case O(ASSEMBLE
,'R'):
581 case O(CREATE
,'R'): /* Run the array */
583 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
590 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
598 fprintf(stderr
, Name
": Cannot have both readonly and readwrite\n");
605 fprintf(stderr
, Name
": Cannot have both readwrite and readonly.\n");
619 if (devmode
&& devmode
!= opt
&&
620 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
621 fprintf(stderr
, Name
": --examine/-E cannot be given with -%c\n",
622 devmode
=='E'?opt
:devmode
);
632 if (devmode
!= 'E') {
633 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
639 /* We have now processed all the valid options. Anything else is
642 fprintf(stderr
, Name
": option %c not valid in %s mode\n",
643 opt
, map_num(modes
, mode
));
649 fputs(Usage
, stderr
);
652 /* Ok, got the option parsing out of the way
653 * hopefully it's mostly right but there might be some stuff
656 * That is mosty checked in the per-mode stuff but...
658 * For @,B,C and A without -s, the first device listed must be an md device
659 * we check that here and open it.
662 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
663 (mode
== ASSEMBLE
&& ! scan
)) {
664 if (devs_found
< 1) {
665 fprintf(stderr
, Name
": an md device must be given in this mode\n");
668 if ((int)ident
.super_minor
== -2 && autof
) {
669 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
672 mdfd
= open_mddev(devlist
->devname
, autof
);
675 if ((int)ident
.super_minor
== -2) {
678 ident
.super_minor
= minor(stb
.st_rdev
);
685 /* readonly, add/remove, readwrite, runstop */
687 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
688 if (!rv
&& devs_found
>1)
689 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
691 if (!rv
&& readonly
< 0)
692 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
694 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
);
697 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
698 ident
.super_minor
== UnSet
&& !scan
) {
699 /* Only a device has been given, so get details from config file */
700 mddev_ident_t array_ident
= conf_get_ident(configfile
, devlist
->devname
);
701 if (array_ident
== NULL
) {
702 fprintf(stderr
, Name
": %s not identified in config file.\n",
706 mdfd
= open_mddev(devlist
->devname
, array_ident
->autof
);
710 rv
|= Assemble(devlist
->devname
, mdfd
, array_ident
, configfile
,
712 readonly
, runstop
, update
, verbose
, force
);
717 rv
= Assemble(devlist
->devname
, mdfd
, &ident
, configfile
,
719 readonly
, runstop
, update
, verbose
, force
);
720 else if (devs_found
>0) {
721 if (update
&& devs_found
> 1) {
722 fprintf(stderr
, Name
": can only update a single array at a time\n");
725 for (dv
= devlist
; dv
; dv
=dv
->next
) {
726 mddev_ident_t array_ident
= conf_get_ident(configfile
, dv
->devname
);
727 if (array_ident
== NULL
) {
728 fprintf(stderr
, Name
": %s not identified in config file.\n",
733 mdfd
= open_mddev(dv
->devname
, array_ident
->autof
);
738 rv
|= Assemble(dv
->devname
, mdfd
, array_ident
, configfile
,
740 readonly
, runstop
, update
, verbose
, force
);
744 mddev_ident_t array_list
= conf_get_ident(configfile
, NULL
);
746 fprintf(stderr
, Name
": No arrays found in config file\n");
749 for (; array_list
; array_list
= array_list
->next
) {
750 mdu_array_info_t array
;
751 mdfd
= open_mddev(array_list
->devname
, array_list
->autof
);
756 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
)>=0)
757 /* already assembled, skip */
759 rv
|= Assemble(array_list
->devname
, mdfd
,
760 array_list
, configfile
,
762 readonly
, runstop
, NULL
, verbose
, force
);
767 rv
= Build(devlist
->devname
, mdfd
, chunk
, level
, layout
, raiddisks
, devlist
->next
, assume_clean
);
770 rv
= Create(devlist
->devname
, mdfd
, chunk
, level
, layout
, size
<0 ? 0 : size
,
771 raiddisks
, sparedisks
,
772 devs_found
-1, devlist
->next
, runstop
, verbose
, force
);
776 if (devmode
== 'E') {
777 if (devlist
== NULL
&& !scan
) {
778 fprintf(stderr
, Name
": No devices to examine\n");
782 devlist
= conf_get_devs(configfile
);
783 if (devlist
== NULL
) {
784 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
787 rv
= Examine(devlist
, scan
?!verbose
:brief
, scan
, SparcAdjust
);
789 if (devlist
== NULL
) {
790 if ((devmode
== 'S' ||devmode
=='D') && scan
) {
791 /* apply to all devices in /proc/mdstat */
792 struct mdstat_ent
*ms
= mdstat_read(0);
793 struct mdstat_ent
*e
;
794 for (e
=ms
; e
; e
=e
->next
) {
795 char *name
= get_md_name(e
->devnum
);
798 fprintf(stderr
, Name
": cannot find device file for %s\n",
803 rv
|= Detail(name
, !verbose
, test
);
804 else if (devmode
=='S') {
805 mdfd
= open_mddev(name
, 0);
807 rv
|= Manage_runstop(name
, mdfd
, -1);
812 fprintf(stderr
, Name
": No devices given.\n");
816 for (dv
=devlist
; dv
; dv
=dv
->next
) {
817 switch(dv
->disposition
) {
819 rv
|= Detail(dv
->devname
, brief
, test
); continue;
820 case 'K': /* Zero superblock */
821 rv
|= Kill(dv
->devname
, force
); continue;
823 rv
|= Query(dv
->devname
); continue;
825 mdfd
= open_mddev(dv
->devname
, 0);
827 switch(dv
->disposition
) {
829 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1); break;
831 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1); break;
833 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
835 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
841 if (!devlist
&& !scan
) {
842 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
846 if (pidfile
&& !daemonise
) {
847 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
851 rv
= Monitor(devlist
, mailaddr
, program
,
852 delay
?delay
:60, daemonise
, scan
, oneshot
, configfile
, test
, pidfile
);
856 if (devs_found
> 1) {
859 if (size
>= 0 || raiddisks
) {
860 fprintf(stderr
, Name
": --size, --raiddisks, and --add are exclusing in --grow mode\n");
864 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
865 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
869 } else if ((size
>= 0) + (raiddisks
!= 0) + (layout
!= UnSet
) > 1) {
870 fprintf(stderr
, Name
": can change at most one of size, raiddisks, and layout\n");
873 } else if (layout
!= UnSet
)
874 rv
= Manage_reconfig(devlist
->devname
, mdfd
, layout
);
875 else if (size
>= 0 || raiddisks
)
876 rv
= Manage_resize(devlist
->devname
, mdfd
, size
, raiddisks
);
878 fprintf(stderr
, Name
": no changes to --grow\n");