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
35 void make_parts(char *dev
, int cnt
)
37 /* make 'cnt' partition devices for 'dev'
38 * We use the major/minor from dev and add 1..cnt
39 * If dev ends with a digit, we add "_p%d" else "%d"
40 * If the name exists, we use it's owner/mode,
46 char *name
= malloc(strlen(dev
) + 20);
47 int dig
= isdigit(dev
[strlen(dev
)-1]);
49 if (stat(dev
, &stb
)!= 0)
51 if (!S_ISBLK(stb
.st_mode
))
53 major
= MAJOR(stb
.st_rdev
);
54 minor
= MINOR(stb
.st_rdev
);
55 for (i
=1; i
<= cnt
; i
++) {
57 sprintf(name
, "%s%s%d", dev
, dig
?"_p":"", i
);
58 if (stat(name
, &stb2
)==0) {
59 if (!S_ISBLK(stb2
.st_mode
))
61 if (stb2
.st_rdev
== MKDEV(major
, minor
+i
))
67 mknod(name
, S_IFBLK
| 0600, MKDEV(major
, minor
+i
));
68 chown(name
, stb2
.st_uid
, stb2
.st_gid
);
69 chmod(name
, stb2
.st_mode
& 07777);
74 * Open a given md device, and check that it really is one.
75 * If 'autof' is given, then we need to create, or recreate, the md device.
76 * If the name already exists, and is not a block device, we fail.
77 * If it exists and is not an md device, is not the right type (partitioned or not),
78 * or is currently in-use, we remove the device, but remember the owner and mode.
79 * If it now doesn't exist, we find a few md array and create the device.
80 * Default ownership is user=0, group=0 perm=0600
82 int open_mddev(char *dev
, int autof
)
89 struct mdstat_ent
*mdlist
;
93 /* autof is set, so we need to check that the name is ok,
94 * and possibly create one if not
97 if (lstat(dev
, &stb
)==0 && ! S_ISBLK(stb
.st_mode
)) {
98 fprintf(stderr
, Name
": %s is not a block device.\n",
102 /* check major number is correct */
104 major
= get_mdp_major();
105 if (stb
.st_mode
&& MAJOR(stb
.st_rdev
) != major
)
107 if (stb
.st_mode
&& !must_remove
) {
108 mdu_array_info_t array
;
109 /* looks ok, see if it is available */
110 mdfd
= open(dev
, O_RDWR
, 0);
112 fprintf(stderr
, Name
": error opening %s: %s\n",
113 dev
, strerror(errno
));
115 } else if (md_get_version(mdfd
) <= 0) {
116 fprintf(stderr
, Name
": %s does not appear to be an md device\n",
121 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
)==0) {
127 make_parts(dev
, autof
);
131 /* Ok, need to find a minor that is not in use.
132 * Easiest to read /proc/mdstat, and hunt through for
135 mdlist
= mdstat_read(0);
136 for (num
= (autof
>0)?-1:0 ; ; num
+= (autof
>2)?-1:1) {
137 struct mdstat_ent
*me
;
138 for (me
=mdlist
; me
; me
=me
->next
)
139 if (me
->devnum
== num
)
142 /* doesn't exist if mdstat.
143 * make sure it is new to /dev too
147 minor
= (-1-num
) << MdpMinorShift
;
150 dn
= map_dev(major
,minor
);
151 if (dn
==NULL
|| is_standard(dn
)) {
152 /* this number only used by a 'standard' name,
153 * so it is safe to use
159 /* 'num' is the number to use, >=0 for md, <0 for mdp */
161 /* never remove a device name that ends /mdNN or /dNN,
162 * that would be confusing
164 if (is_standard(dev
)) {
165 fprintf(stderr
, Name
": --auto refusing to remove %s as it looks like a standard name.\n",
172 if (mknod(dev
, S_IFBLK
|0600, MKDEV(major
, minor
))!= 0) {
173 fprintf(stderr
, Name
": failed to create %s\n", dev
);
177 chown(dev
, stb
.st_uid
, stb
.st_gid
);
178 chmod(dev
, stb
.st_mode
& 07777);
180 make_parts(dev
,autof
);
182 mdfd
= open(dev
, O_RDWR
, 0);
184 fprintf(stderr
, Name
": error opening %s: %s\n",
185 dev
, strerror(errno
));
186 else if (md_get_version(mdfd
) <= 0) {
187 fprintf(stderr
, Name
": %s does not appear to be an md device\n",
197 int main(int argc
, char *argv
[])
212 struct mddev_ident_s ident
;
213 char *configfile
= NULL
;
221 mddev_dev_t devlist
= NULL
;
222 mddev_dev_t
*devlistend
= & devlist
;
229 int assume_clean
= 0;
230 int autof
= 0; /* -1 for non-partitions, 1 or more to create partitions */
232 char *mailaddr
= NULL
;
233 char *program
= NULL
;
242 ident
.raid_disks
= UnSet
;
243 ident
.super_minor
= UnSet
;
246 while ((option_index
= -1) ,
247 (opt
=getopt_long(argc
, argv
,
248 short_options
, long_options
,
249 &option_index
)) != -1) {
251 /* firstly, so mode-independant options */
255 if (option_index
> 0 &&
256 strcmp(long_options
[option_index
].name
, "help-options")==0)
257 help_text
= OptionHelp
;
260 case ASSEMBLE
: help_text
= Help_assemble
; break;
261 case BUILD
: help_text
= Help_build
; break;
262 case CREATE
: help_text
= Help_create
; break;
263 case MANAGE
: help_text
= Help_manage
; break;
264 case MISC
: help_text
= Help_misc
; break;
265 case MONITOR
: help_text
= Help_monitor
; break;
266 case GROW
: help_text
= Help_grow
; break;
268 fputs(help_text
,stderr
);
272 fputs(Version
, stderr
);
275 case 'v': verbose
= 1;
283 fputs(Usage
, stderr
);
286 /* second, figure out the mode.
287 * Some options force the mode. Others
288 * set the mode if it isn't already
292 case '@': /* just incase they say --manage */
293 newmode
= MANAGE
; break;
297 case 1 : if (!mode
) newmode
= MANAGE
; break;
299 case 'A': newmode
= ASSEMBLE
; break;
300 case 'B': newmode
= BUILD
; break;
301 case 'C': newmode
= CREATE
; break;
302 case 'F': newmode
= MONITOR
;break;
303 case 'G': newmode
= GROW
; break;
308 case 'Q': newmode
= MISC
; break;
313 case 'K': if (!mode
) newmode
= MISC
; break;
315 if (mode
&& newmode
== mode
) {
316 /* everybody happy ! */
317 } else if (mode
&& newmode
!= mode
) {
319 fprintf(stderr
, Name
": ");
320 if (option_index
>= 0)
321 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
323 fprintf(stderr
, "-%c", opt
);
324 fprintf(stderr
, " would set mode to %s, but it is already %s.\n",
325 map_num(modes
, newmode
),
326 map_num(modes
, mode
));
328 } else if (!mode
&& newmode
) {
331 /* special case of -c --help */
333 ( strncmp(optarg
, "--h", 3)==0 ||
334 strncmp(optarg
, "-h", 2)==0)) {
335 fputs(Help_config
, stderr
);
338 if (option_index
>= 0)
339 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
341 fprintf(stderr
, "-%c", opt
);
342 fprintf(stderr
, " does not set the mode, and so cannot be first.\n");
346 /* if we just set the mode, then done */
358 /* an undecorated option - must be a device name.
360 if (devs_found
> 0 && mode
== '@' && !devmode
) {
361 fprintf(stderr
, Name
": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
364 dv
= malloc(sizeof(*dv
));
366 fprintf(stderr
, Name
": malloc failed\n");
369 dv
->devname
= optarg
;
370 dv
->disposition
= devmode
;
373 devlistend
= &dv
->next
;
379 /* We've got a mode, and opt is now something else which
380 * could depend on the mode */
381 #define O(a,b) ((a<<8)|b)
382 switch (O(mode
,opt
)) {
384 case O(BUILD
,'c'): /* chunk or rounding */
386 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
387 "Second value is %s.\n", optarg
);
390 chunk
= strtol(optarg
, &c
, 10);
391 if (!optarg
[0] || *c
|| chunk
<4 || ((chunk
-1)&chunk
)) {
392 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
399 case O(CREATE
,'z'): /* size */
401 fprintf(stderr
, Name
": size may only be specified once. "
402 "Second value is %s.\n", optarg
);
405 if (strcmp(optarg
, "max")==0)
408 size
= strtol(optarg
, &c
, 10);
409 if (!optarg
[0] || *c
|| size
< 4) {
410 fprintf(stderr
, Name
": invalid size: %s\n",
418 case O(BUILD
,'l'): /* set raid level*/
419 if (level
!= UnSet
) {
420 fprintf(stderr
, Name
": raid level may only be set once. "
421 "Second value is %s.\n", optarg
);
424 level
= map_name(pers
, optarg
);
425 if (level
== UnSet
) {
426 fprintf(stderr
, Name
": invalid raid level: %s\n",
430 if (level
!= 0 && level
!= -1 && level
!= 1 && level
!= -4 && mode
== BUILD
) {
431 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
435 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
436 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
443 case O(CREATE
,'p'): /* raid5 layout */
445 fprintf(stderr
,Name
": layout may only be sent once. "
446 "Second value was %s\n", optarg
);
451 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
452 map_num(pers
, level
));
455 fprintf(stderr
, Name
": raid level must be given before layout.\n");
460 layout
= map_name(r5layout
, optarg
);
462 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
471 case O(BUILD
,3): /* assume clean */
477 case O(BUILD
,'n'): /* number of raid disks */
479 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
483 raiddisks
= strtol(optarg
, &c
, 10);
484 if (!optarg
[0] || *c
|| raiddisks
<=0 || raiddisks
> MD_SB_DISKS
) {
485 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
489 if (raiddisks
== 1 && !force
) {
490 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
491 " a mistake. If you really mean it you will need to specify --force before\n"
492 " setting the number of drives.\n");
495 ident
.raid_disks
= raiddisks
;
498 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
500 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
504 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
505 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
509 sparedisks
= strtol(optarg
, &c
, 10);
510 if (!optarg
[0] || *c
|| sparedisks
< 0 || sparedisks
> MD_SB_DISKS
- raiddisks
) {
511 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
519 case O(ASSEMBLE
,'a'): /* auto-creation of device node */
522 else if (strcasecmp(optarg
,"no")==0)
524 else if (strcasecmp(optarg
,"yes")==0 || strcasecmp(optarg
,"md")==0)
527 /* There might be digits, and maybe a hypen, at the end */
528 char *e
= optarg
+ strlen(optarg
);
531 while (e
> optarg
&& isdigit(e
[-1]))
535 if (num
<= 0) num
= 1;
537 if (e
> optarg
&& e
[-1] == '-')
540 if ((len
== 3 && strncasecmp(optarg
,"mdp",3)==0) ||
541 (len
== 1 && strncasecmp(optarg
,"p",1)==0) ||
542 (len
>= 4 && strncasecmp(optarg
,"part",4)==0))
545 fprintf(stderr
, Name
": --auto flag arg of \"%s\" unrecognised: use no,yes,md,mdp,part\n"
546 " optionally followed by a number.\n",
553 case O(BUILD
,'f'): /* force honouring '-n 1' */
554 case O(CREATE
,'f'): /* force honouring of device list */
555 case O(ASSEMBLE
,'f'): /* force assembly */
556 case O(MISC
,'f'): /* force zero */
560 /* now for the Assemble options */
561 case O(ASSEMBLE
,'u'): /* uuid of array */
562 if (ident
.uuid_set
) {
563 fprintf(stderr
, Name
": uuid cannot be set twice. "
564 "Second value %s.\n", optarg
);
567 if (parse_uuid(optarg
, ident
.uuid
))
570 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
575 case O(ASSEMBLE
,'m'): /* super-minor for array */
576 if (ident
.super_minor
!= UnSet
) {
577 fprintf(stderr
, Name
": super-minor cannot be set twice. "
578 "Second value: %s.\n", optarg
);
581 if (strcmp(optarg
, "dev")==0)
582 ident
.super_minor
= -2;
584 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
585 if (!optarg
[0] || *cp
) {
586 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
592 case O(ASSEMBLE
,'U'): /* update the superblock */
594 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
599 if (strcmp(update
, "sparc2.2")==0) continue;
600 if (strcmp(update
, "super-minor") == 0)
602 if (strcmp(update
, "summaries")==0)
604 fprintf(stderr
, Name
": '--update %s' invalid. Only 'sparc2.2', 'super-minor' or 'summaries' supported\n",update
);
607 case O(ASSEMBLE
,'c'): /* config file */
611 fprintf(stderr
, Name
": configfile cannot be set twice. "
612 "Second value is %s.\n", optarg
);
616 /* FIXME possibly check that config file exists. Even parse it */
618 case O(ASSEMBLE
,'s'): /* scan */
624 case O(MONITOR
,'m'): /* mail address */
626 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
632 case O(MONITOR
,'p'): /* alert program */
634 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
640 case O(MONITOR
,'d'): /* delay in seconds */
642 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
645 delay
= strtol(optarg
, &c
, 10);
646 if (!optarg
[0] || *c
|| delay
<1) {
647 fprintf(stderr
, Name
": invalid delay: %s\n",
653 case O(MONITOR
,'f'): /* daemonise */
656 case O(MONITOR
,'1'): /* oneshot */
659 case O(MONITOR
,'t'): /* test */
663 /* now the general management options. Some are applicable
664 * to other modes. None have arguments.
666 case O(MANAGE
,'a'): /* add a drive */
669 case O(MANAGE
,'r'): /* remove a drive */
672 case O(MANAGE
,'f'): /* set faulty */
676 case O(ASSEMBLE
,'R'):
678 case O(CREATE
,'R'): /* Run the array */
680 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
687 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
695 fprintf(stderr
, Name
": Cannot have both readonly and readwrite\n");
702 fprintf(stderr
, Name
": Cannot have both readwrite and readonly.\n");
716 if (devmode
&& devmode
!= opt
&&
717 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
718 fprintf(stderr
, Name
": --examine/-E cannot be given with -%c\n",
719 devmode
=='E'?opt
:devmode
);
729 if (devmode
!= 'E') {
730 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
736 /* We have now processed all the valid options. Anything else is
739 fprintf(stderr
, Name
": option %c not valid in %s mode\n",
740 opt
, map_num(modes
, mode
));
746 fputs(Usage
, stderr
);
749 /* Ok, got the option parsing out of the way
750 * hopefully it's mostly right but there might be some stuff
753 * That is mosty checked in the per-mode stuff but...
755 * For @,B,C and A without -s, the first device listed must be an md device
756 * we check that here and open it.
759 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
760 (mode
== ASSEMBLE
&& ! scan
)) {
761 if (devs_found
< 1) {
762 fprintf(stderr
, Name
": an md device must be given in this mode\n");
765 if ((int)ident
.super_minor
== -2 && autof
) {
766 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
769 mdfd
= open_mddev(devlist
->devname
, autof
);
772 if ((int)ident
.super_minor
== -2) {
775 ident
.super_minor
= MINOR(stb
.st_rdev
);
782 /* readonly, add/remove, readwrite, runstop */
784 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
785 if (!rv
&& devs_found
>1)
786 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
788 if (!rv
&& readonly
< 0)
789 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
791 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
);
794 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
795 ident
.super_minor
== UnSet
&& !scan
) {
796 /* Only a device has been given, so get details from config file */
797 mddev_ident_t array_ident
= conf_get_ident(configfile
, devlist
->devname
);
798 mdfd
= open_mddev(devlist
->devname
, array_ident
->autof
);
802 if (array_ident
== NULL
) {
803 fprintf(stderr
, Name
": %s not identified in config file.\n",
808 rv
|= Assemble(devlist
->devname
, mdfd
, array_ident
, configfile
,
810 readonly
, runstop
, update
, verbose
, force
);
813 rv
= Assemble(devlist
->devname
, mdfd
, &ident
, configfile
,
815 readonly
, runstop
, update
, verbose
, force
);
816 else if (devs_found
>0) {
817 if (update
&& devs_found
> 1) {
818 fprintf(stderr
, Name
": can only update a single array at a time\n");
821 for (dv
= devlist
; dv
; dv
=dv
->next
) {
822 mddev_ident_t array_ident
= conf_get_ident(configfile
, dv
->devname
);
823 mdfd
= open_mddev(dv
->devname
, array_ident
->autof
);
828 if (array_ident
== NULL
) {
829 fprintf(stderr
, Name
": %s not identified in config file.\n",
834 rv
|= Assemble(dv
->devname
, mdfd
, array_ident
, configfile
,
836 readonly
, runstop
, update
, verbose
, force
);
839 mddev_ident_t array_list
= conf_get_ident(configfile
, NULL
);
841 fprintf(stderr
, Name
": No arrays found in config file\n");
844 for (; array_list
; array_list
= array_list
->next
) {
845 mdu_array_info_t array
;
846 mdfd
= open_mddev(array_list
->devname
, array_list
->autof
);
851 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
)>=0)
852 /* already assembled, skip */
854 rv
|= Assemble(array_list
->devname
, mdfd
,
855 array_list
, configfile
,
857 readonly
, runstop
, NULL
, verbose
, force
);
862 rv
= Build(devlist
->devname
, mdfd
, chunk
, level
, raiddisks
, devlist
->next
, assume_clean
);
865 rv
= Create(devlist
->devname
, mdfd
, chunk
, level
, layout
, size
<0 ? 0 : size
,
866 raiddisks
, sparedisks
,
867 devs_found
-1, devlist
->next
, runstop
, verbose
, force
);
871 if (devmode
== 'E') {
872 if (devlist
== NULL
&& !scan
) {
873 fprintf(stderr
, Name
": No devices to examine\n");
877 devlist
= conf_get_devs(configfile
);
878 if (devlist
== NULL
) {
879 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
882 rv
= Examine(devlist
, scan
?!verbose
:brief
, scan
, SparcAdjust
);
884 if (devlist
== NULL
) {
885 if ((devmode
== 'S' ||devmode
=='D') && scan
) {
886 /* apply to all devices in /proc/mdstat */
887 struct mdstat_ent
*ms
= mdstat_read(0);
888 struct mdstat_ent
*e
;
889 for (e
=ms
; e
; e
=e
->next
) {
890 char *name
= get_md_name(e
->devnum
);
893 fprintf(stderr
, Name
": cannot find device file for %s\n",
898 rv
|= Detail(name
, !verbose
, test
);
899 else if (devmode
=='S') {
900 mdfd
= open_mddev(name
, 0);
902 rv
|= Manage_runstop(name
, mdfd
, -1);
907 fprintf(stderr
, Name
": No devices given.\n");
911 for (dv
=devlist
; dv
; dv
=dv
->next
) {
912 switch(dv
->disposition
) {
914 rv
|= Detail(dv
->devname
, brief
, test
); continue;
915 case 'K': /* Zero superblock */
916 rv
|= Kill(dv
->devname
, force
); continue;
918 rv
|= Query(dv
->devname
); continue;
920 mdfd
= open_mddev(dv
->devname
, 0);
922 switch(dv
->disposition
) {
924 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1); break;
926 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1); break;
928 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
930 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
936 if (!devlist
&& !scan
) {
937 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
941 rv
= Monitor(devlist
, mailaddr
, program
,
942 delay
?delay
:60, daemonise
, scan
, oneshot
, configfile
, test
);
946 if (devs_found
> 1) {
947 fprintf(stderr
, Name
": Only one device may be given for --grow\n");
951 if (size
>= 0 && raiddisks
) {
952 fprintf(stderr
, Name
": can only grow size OR raiddisks, not both\n");
956 rv
= Manage_resize(devlist
->devname
, mdfd
, size
, raiddisks
);