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
33 int open_mddev(char *dev
)
35 int mdfd
= open(dev
, O_RDWR
, 0);
37 fprintf(stderr
, Name
": error opening %s: %s\n",
38 dev
, strerror(errno
));
39 else if (md_get_version(mdfd
) <= 0) {
40 fprintf(stderr
, Name
": %s does not appear to be an md device\n",
50 int main(int argc
, char *argv
[])
65 struct mddev_ident_s ident
;
66 char *configfile
= NULL
;
72 mddev_dev_t devlist
= NULL
;
73 mddev_dev_t
*devlistend
= & devlist
;
80 char *mailaddr
= NULL
;
88 ident
.raid_disks
= -1;
89 ident
.super_minor
= -1;
92 while ((option_index
= -1) ,
93 (opt
=getopt_long(argc
, argv
,
94 short_options
, long_options
,
95 &option_index
)) != -1) {
97 /* firstly, so mode-independant options */
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;
109 fputs(help_text
,stderr
);
113 fputs(Version
, stderr
);
116 case 'v': verbose
= 1;
124 fputs(Usage
, stderr
);
127 /* second, figure out the mode.
128 * Some options force the mode. Others
129 * set the mode if it isn't already
133 case '@': /* just incase they say --manage */
134 newmode
= MANAGE
; break;
138 case 1 : if (!mode
) newmode
= MANAGE
; break;
140 case 'A': newmode
= ASSEMBLE
; break;
141 case 'B': newmode
= BUILD
; break;
142 case 'C': newmode
= CREATE
; break;
143 case 'F': newmode
= MONITOR
;break;
148 case 'Q': newmode
= MISC
; break;
153 case 'K': if (!mode
) newmode
= MISC
; break;
155 if (mode
&& newmode
== mode
) {
156 /* everybody happy ! */
157 } else if (mode
&& newmode
!= mode
) {
159 fprintf(stderr
, Name
": ");
160 if (option_index
>= 0)
161 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
163 fprintf(stderr
, "-%c", opt
);
164 fprintf(stderr
, " would set mode to %s, but it is already %s.\n",
165 map_num(modes
, newmode
),
166 map_num(modes
, mode
));
168 } else if (!mode
&& newmode
) {
171 /* special case of -c --help */
173 ( strncmp(optarg
, "--h", 3)==0 ||
174 strncmp(optarg
, "-h", 2)==0)) {
175 fputs(Help_config
, stderr
);
178 if (option_index
>= 0)
179 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
181 fprintf(stderr
, "-%c", opt
);
182 fprintf(stderr
, " does not set the mode, and so cannot be first.\n");
186 /* if we just set the mode, then done */
197 /* an undecorated option - must be a device name.
199 if (devs_found
> 0 && mode
== '@' && !devmode
) {
200 fprintf(stderr
, Name
": Must give on of -a/-r/-f for subsequent devices at %s\n", optarg
);
203 dv
= malloc(sizeof(*dv
));
205 fprintf(stderr
, Name
": malloc failed\n");
208 dv
->devname
= optarg
;
209 dv
->disposition
= devmode
;
212 devlistend
= &dv
->next
;
218 /* We've got a mode, and opt is now something else which
219 * could depend on the mode */
220 #define O(a,b) ((a<<8)|b)
221 switch (O(mode
,opt
)) {
223 case O(BUILD
,'c'): /* chunk or rounding */
225 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
226 "Second value is %s.\n", optarg
);
229 chunk
= strtol(optarg
, &c
, 10);
230 if (!optarg
[0] || *c
|| chunk
<4 || ((chunk
-1)&chunk
)) {
231 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
237 case O(CREATE
,'z'): /* size */
239 fprintf(stderr
, Name
": size may only be specified once. "
240 "Second value is %s.\n", optarg
);
243 size
= strtol(optarg
, &c
, 10);
244 if (!optarg
[0] || *c
|| size
< 4) {
245 fprintf(stderr
, Name
": invalid size: %s\n",
252 case O(BUILD
,'l'): /* set raid level*/
254 fprintf(stderr
, Name
": raid level may only be set once. "
255 "Second value is %s.\n", optarg
);
258 level
= map_name(pers
, optarg
);
260 fprintf(stderr
, Name
": invalid raid level: %s\n",
264 if (level
!= 0 && level
!= -1 && mode
== BUILD
) {
265 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
269 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
270 fprintf(stderr
, Name
": raid level %s is incompatible with spare-disks setting.\n",
277 case O(CREATE
,'p'): /* raid5 layout */
279 fprintf(stderr
,Name
": layout may only be sent once. "
280 "Second value was %s\n", optarg
);
285 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
286 map_num(pers
, level
));
289 fprintf(stderr
, Name
": raid level must be given before layout.\n");
293 layout
= map_name(r5layout
, optarg
);
295 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
304 case O(BUILD
,'n'): /* number of raid disks */
306 fprintf(stderr
, Name
": raid-disks set twice: %d and %s\n",
310 raiddisks
= strtol(optarg
, &c
, 10);
311 if (!optarg
[0] || *c
|| raiddisks
<=0 || raiddisks
> MD_SB_DISKS
) {
312 fprintf(stderr
, Name
": invalid number of raid disks: %s\n",
316 ident
.raid_disks
= raiddisks
;
319 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
321 fprintf(stderr
,Name
": spare-disks set twice: %d and %s\n",
325 if (level
> -10 && level
<= 0 && level
>= -1) {
326 fprintf(stderr
, Name
": spare-disks setting is incompatible with raid level %d\n",
330 sparedisks
= strtol(optarg
, &c
, 10);
331 if (!optarg
[0] || *c
|| sparedisks
< 0 || sparedisks
> MD_SB_DISKS
- raiddisks
) {
332 fprintf(stderr
, Name
": invalid number of spare disks: %s\n",
337 case O(CREATE
,'f'): /* force honouring of device list */
338 case O(ASSEMBLE
,'f'): /* force assembly */
339 case O(MISC
,'f'): /* force zero */
343 /* now for the Assemble options */
344 case O(ASSEMBLE
,'u'): /* uuid of array */
345 if (ident
.uuid_set
) {
346 fprintf(stderr
, Name
": uuid cannot be set twice. "
347 "Second value %s.\n", optarg
);
350 if (parse_uuid(optarg
, ident
.uuid
))
353 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
358 case O(ASSEMBLE
,'m'): /* super-minor for array */
359 if (ident
.super_minor
>= 0) {
360 fprintf(stderr
, Name
": super-minor cannot be set twice. "
361 "Second value: %s.\n", optarg
);
364 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
365 if (!optarg
[0] || *cp
) {
366 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
371 case O(ASSEMBLE
,'c'): /* config file */
374 fprintf(stderr
, Name
": configfile cannot be set twice. "
375 "Second value is %s.\n", optarg
);
379 /* FIXME possibly check that config file exists. Even parse it */
381 case O(ASSEMBLE
,'s'): /* scan */
387 case O(MONITOR
,'m'): /* mail address */
389 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
395 case O(MONITOR
,'p'): /* alert program */
397 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
403 case O(MONITOR
,'d'): /* delay in seconds */
405 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
408 delay
= strtol(optarg
, &c
, 10);
409 if (!optarg
[0] || *c
|| delay
<1) {
410 fprintf(stderr
, Name
": invalid delay: %s\n",
418 /* now the general management options. Some are applicable
419 * to other modes. None have arguments.
424 case O(ASSEMBLE
,'a'): /* add a drive */
427 case O(MANAGE
,'r'): /* remove a drive */
430 case O(MANAGE
,'f'): /* set faulty */
434 case O(ASSEMBLE
,'R'):
436 case O(CREATE
,'R'): /* Run the array */
438 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
445 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
453 fprintf(stderr
, Name
": Cannot have both readonly and readwrite\n");
460 fprintf(stderr
, Name
": Cannot have both readwrite and readonly.\n");
474 if (devmode
&& devmode
!= opt
&&
475 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
476 fprintf(stderr
, Name
": --examine/-E cannot be given with -%c\n",
477 devmode
=='E'?opt
:devmode
);
484 /* We have now processed all the valid options. Anything else is
487 fprintf(stderr
, Name
": option %c not valid in %s mode\n",
488 opt
, map_num(modes
, mode
));
494 fputs(Usage
, stderr
);
497 /* Ok, got the option parsing out of the way
498 * hopefully it's mostly right but there might be some stuff
501 * That is mosty checked in the per-mode stuff but...
503 * For @,B,C and A without -s, the first device listed must be an md device
504 * we check that here and open it.
507 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| (mode
== ASSEMBLE
&& ! scan
)) {
508 if (devs_found
< 1) {
509 fprintf(stderr
, Name
": an md device must be given in this mode\n");
512 mdfd
= open_mddev(devlist
->devname
);
520 /* readonly, add/remove, readwrite, runstop */
522 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
523 if (!rv
&& devs_found
>1)
524 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
526 if (!rv
&& readonly
< 0)
527 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
529 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
);
533 rv
= Assemble(devlist
->devname
, mdfd
, &ident
, configfile
,
535 readonly
, runstop
, verbose
, force
);
536 else if (devs_found
>0)
537 for (dv
= devlist
; dv
; dv
=dv
->next
) {
538 mddev_ident_t array_ident
= conf_get_ident(configfile
, dv
->devname
);
539 mdfd
= open_mddev(dv
->devname
);
544 if (array_ident
== NULL
) {
545 fprintf(stderr
, Name
": %s not identified in config file.\n",
550 rv
|= Assemble(dv
->devname
, mdfd
, array_ident
, configfile
,
552 readonly
, runstop
, verbose
, force
);
555 mddev_ident_t array_list
= conf_get_ident(configfile
, NULL
);
557 fprintf(stderr
, Name
": No arrays found in config file\n");
560 for (; array_list
; array_list
= array_list
->next
) {
561 mdu_array_info_t array
;
562 mdfd
= open_mddev(array_list
->devname
);
567 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
)>=0)
568 /* already assembled, skip */
570 rv
|= Assemble(array_list
->devname
, mdfd
,
571 array_list
, configfile
,
573 readonly
, runstop
, verbose
, force
);
578 rv
= Build(devlist
->devname
, mdfd
, chunk
, level
, raiddisks
, devlist
->next
);
581 rv
= Create(devlist
->devname
, mdfd
, chunk
, level
, layout
, size
,
582 raiddisks
, sparedisks
,
583 devs_found
-1, devlist
->next
, runstop
, verbose
, force
);
587 if (devmode
== 'E') {
588 if (devlist
== NULL
&& !scan
) {
589 fprintf(stderr
, Name
": No devices to examine\n");
593 devlist
= conf_get_devs(configfile
);
594 if (devlist
== NULL
) {
595 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
);
598 rv
= Examine(devlist
, devlist
?brief
:!verbose
, scan
);
600 if (devlist
== NULL
) {
601 if ((devmode
== 'S' ||devmode
=='D') && scan
) {
602 /* apply to all devices in /proc/mdstat */
603 struct mdstat_ent
*ms
= mdstat_read();
604 struct mdstat_ent
*e
;
605 for (e
=ms
; e
; e
=e
->next
) {
606 char *name
= get_md_name(e
->devnum
);
609 fprintf(stderr
, Name
": cannot find device file for %s\n",
614 rv
|= Detail(name
, !verbose
);
615 else if (devmode
=='S') {
616 mdfd
= open_mddev(name
);
618 rv
|= Manage_runstop(name
, mdfd
, -1);
623 fprintf(stderr
, Name
": No devices given.\n");
627 for (dv
=devlist
; dv
; dv
=dv
->next
) {
628 switch(dv
->disposition
) {
630 rv
|= Detail(dv
->devname
, brief
); continue;
631 case 'K': /* Zero superblock */
632 rv
|= Kill(dv
->devname
, force
); continue;
634 rv
|= Query(dv
->devname
); continue;
636 mdfd
= open_mddev(dv
->devname
);
638 switch(dv
->disposition
) {
640 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1); break;
642 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1); break;
644 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
646 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
653 if (!devlist && !scan) {
654 fprintf(stderr, Name ": Cannot monitor: need --scan or at least one device\n");
658 */ rv
= Monitor(devlist
, mailaddr
, program
,
659 delay
?delay
:60, scan
, configfile
);