2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
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@suse.de>
24 * Additions for bitmap and write-behind RAID options, Copyright (C) 2003-2004,
25 * Paul Clements, SteelEye Technology, Inc.
33 int main(int argc
, char *argv
[])
44 long long array_size
= -1;
47 char *layout_str
= NULL
;
49 int max_disks
= MD_SB_DISKS
; /* just a default */
51 struct mddev_ident_s ident
;
52 char *configfile
= NULL
;
61 char *bitmap_file
= NULL
;
62 char *backup_file
= NULL
;
63 int bitmap_chunk
= UnSet
;
65 mddev_dev_t devlist
= NULL
;
66 mddev_dev_t
*devlistend
= & devlist
;
76 char *symlinks
= NULL
;
77 /* autof indicates whether and how to create device node.
78 * bottom 3 bits are style. Rest (when shifted) are number of parts
80 * 1 - don't create (no)
81 * 2 - if is_standard, then create (yes)
82 * 3 - create as 'md' - reject is_standard mdp (md)
83 * 4 - create as 'mdp' - reject is_standard md (mdp)
84 * 5 - default to md if not is_standard (md in config file)
85 * 6 - default to mdp if not is_standard (part, or mdp in config file)
89 char *homehost
= NULL
;
90 char sys_hostname
[256];
91 int require_homehost
= 1;
92 char *mailaddr
= NULL
;
99 struct supertype
*ss
= NULL
;
102 char *shortopt
= short_options
;
105 int auto_update_home
= 0;
106 char *subarray
= NULL
;
113 srandom(time(0) ^ getpid());
117 ident
.raid_disks
= UnSet
;
118 ident
.super_minor
= UnSet
;
120 ident
.spare_group
= NULL
;
123 ident
.bitmap_fd
= -1;
124 ident
.bitmap_file
= NULL
;
126 ident
.container
= NULL
;
129 while ((option_index
= -1) ,
130 (opt
=getopt_long(argc
, argv
,
131 shortopt
, long_options
,
132 &option_index
)) != -1) {
134 /* firstly, some mode-independent options */
137 if (option_index
> 0 &&
138 strcmp(long_options
[option_index
].name
, "help-options")==0)
145 fputs(Version
, stderr
);
155 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
156 mode
== INCREMENTAL
|| mode
== MANAGE
)
157 break; /* b means bitmap */
165 if (strcasecmp(optarg
, "<ignore>") == 0)
166 require_homehost
= 0;
173 fputs(Usage
, stderr
);
176 /* second, figure out the mode.
177 * Some options force the mode. Others
178 * set the mode if it isn't already
182 case '@': /* just incase they say --manage */
184 shortopt
= short_bitmap_options
;
189 case ReAdd
: /* re-add */
192 shortopt
= short_bitmap_options
;
196 case 'A': newmode
= ASSEMBLE
; shortopt
= short_bitmap_auto_options
; break;
197 case 'B': newmode
= BUILD
; shortopt
= short_bitmap_auto_options
; break;
198 case 'C': newmode
= CREATE
; shortopt
= short_bitmap_auto_options
; break;
199 case 'F': newmode
= MONITOR
;break;
200 case 'G': newmode
= GROW
;
201 shortopt
= short_bitmap_options
;
203 case 'I': newmode
= INCREMENTAL
;
204 shortopt
= short_bitmap_auto_options
; break;
206 newmode
= AUTODETECT
; break;
212 case 'Q': newmode
= MISC
; break;
222 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
224 fprintf(stderr
, Name
": subarray can only be specified once\n");
229 case 'K': if (!mode
) newmode
= MISC
; break;
231 if (mode
&& newmode
== mode
) {
232 /* everybody happy ! */
233 } else if (mode
&& newmode
!= mode
) {
235 fprintf(stderr
, Name
": ");
236 if (option_index
>= 0)
237 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
239 fprintf(stderr
, "-%c", opt
);
240 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
241 map_num(modes
, newmode
),
242 map_num(modes
, mode
));
244 } else if (!mode
&& newmode
) {
247 /* special case of -c --help */
249 ( strncmp(optarg
, "--h", 3)==0 ||
250 strncmp(optarg
, "-h", 2)==0)) {
251 fputs(Help_config
, stdout
);
255 /* If first option is a device, don't force the mode yet */
257 if (devs_found
== 0) {
258 dv
= malloc(sizeof(*dv
));
260 fprintf(stderr
, Name
": malloc failed\n");
263 dv
->devname
= optarg
;
264 dv
->disposition
= devmode
;
265 dv
->writemostly
= writemostly
;
271 devlistend
= &dv
->next
;
276 /* No mode yet, and this is the second device ... */
277 fprintf(stderr
, Name
": An option must be given to set the mode before a second device\n"
278 " (%s) is listed\n", optarg
);
281 if (option_index
>= 0)
282 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
284 fprintf(stderr
, Name
": -%c", opt
);
285 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
289 /* if we just set the mode, then done */
303 /* an undecorated option - must be a device name.
305 if (devs_found
> 0 && mode
== '@' && !devmode
) {
306 fprintf(stderr
, Name
": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
309 if (devs_found
> 0 && mode
== 'G' && !devmode
) {
310 fprintf(stderr
, Name
": Must give one of -a for devices do add: %s\n", optarg
);
313 dv
= malloc(sizeof(*dv
));
315 fprintf(stderr
, Name
": malloc failed\n");
318 dv
->devname
= optarg
;
319 dv
->disposition
= devmode
;
320 dv
->writemostly
= writemostly
;
326 devlistend
= &dv
->next
;
332 /* We've got a mode, and opt is now something else which
333 * could depend on the mode */
334 #define O(a,b) ((a<<8)|b)
335 switch (O(mode
,opt
)) {
338 case O(BUILD
,'c'): /* chunk or rounding */
340 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
341 "Second value is %s.\n", optarg
);
344 chunk
= strtol(optarg
, &c
, 10);
345 if (!optarg
[0] || *c
|| chunk
<4 || ((chunk
-1)&chunk
)) {
346 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
353 case O(ASSEMBLE
,AutoHomeHost
):
354 auto_update_home
= 1;
357 case O(INCREMENTAL
, 'e'):
359 case O(ASSEMBLE
,'e'):
360 case O(MISC
,'e'): /* set metadata (superblock) information */
362 fprintf(stderr
, Name
": metadata information already given\n");
365 for(i
=0; !ss
&& superlist
[i
]; i
++)
366 ss
= superlist
[i
]->match_metadata_desc(optarg
);
369 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
372 max_disks
= ss
->max_devs
;
378 /* set write-mostly for following devices */
383 /* clear write-mostly for following devices */
390 case O(BUILD
,'z'): /* size */
392 fprintf(stderr
, Name
": size may only be specified once. "
393 "Second value is %s.\n", optarg
);
396 if (strcmp(optarg
, "max")==0)
399 size
= parse_size(optarg
);
401 fprintf(stderr
, Name
": invalid size: %s\n",
405 /* convert sectors to K */
410 case O(GROW
,'Z'): /* array size */
411 if (array_size
>= 0) {
412 fprintf(stderr
, Name
": array-size may only be specified once. "
413 "Second value is %s.\n", optarg
);
416 if (strcmp(optarg
, "max") == 0)
419 array_size
= parse_size(optarg
);
420 if (array_size
<= 0) {
421 fprintf(stderr
, Name
": invalid array size: %s\n",
430 case O(BUILD
,'l'): /* set raid level*/
431 if (level
!= UnSet
) {
432 fprintf(stderr
, Name
": raid level may only be set once. "
433 "Second value is %s.\n", optarg
);
436 level
= map_name(pers
, optarg
);
437 if (level
== UnSet
) {
438 fprintf(stderr
, Name
": invalid raid level: %s\n",
442 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
443 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
446 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
450 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
451 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
458 case O(GROW
, 'p'): /* new layout */
460 fprintf(stderr
,Name
": layout may only be sent once. "
461 "Second value was %s\n", optarg
);
465 /* 'Grow' will parse the value */
468 case O(CREATE
,'p'): /* raid5 layout */
469 case O(BUILD
,'p'): /* faulty layout */
470 if (layout
!= UnSet
) {
471 fprintf(stderr
,Name
": layout may only be sent once. "
472 "Second value was %s\n", optarg
);
477 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
478 map_num(pers
, level
));
481 fprintf(stderr
, Name
": raid level must be given before layout.\n");
485 layout
= map_name(r5layout
, optarg
);
487 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
493 layout
= map_name(r6layout
, optarg
);
495 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
502 layout
= parse_layout_10(optarg
);
504 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
512 layout
= parse_layout_faulty(optarg
);
514 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
522 case O(CREATE
,AssumeClean
):
523 case O(BUILD
,AssumeClean
): /* assume clean */
529 case O(BUILD
,'n'): /* number of raid disks */
531 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
535 raiddisks
= strtol(optarg
, &c
, 10);
536 if (!optarg
[0] || *c
|| raiddisks
<=0) {
537 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
541 ident
.raid_disks
= raiddisks
;
544 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
546 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
550 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
551 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
555 sparedisks
= strtol(optarg
, &c
, 10);
556 if (!optarg
[0] || *c
|| sparedisks
< 0) {
557 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
565 case O(INCREMENTAL
,'a'):
566 case O(ASSEMBLE
,'a'): /* auto-creation of device node */
567 autof
= parse_auto(optarg
, "--auto flag", 0);
570 case O(CREATE
,Symlinks
):
571 case O(BUILD
,Symlinks
):
572 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
576 case O(BUILD
,'f'): /* force honouring '-n 1' */
577 case O(GROW
,'f'): /* ditto */
578 case O(CREATE
,'f'): /* force honouring of device list */
579 case O(ASSEMBLE
,'f'): /* force assembly */
580 case O(MISC
,'f'): /* force zero */
584 /* now for the Assemble options */
585 case O(CREATE
,'u'): /* uuid of array */
586 case O(ASSEMBLE
,'u'): /* uuid of array */
587 if (ident
.uuid_set
) {
588 fprintf(stderr
, Name
": uuid cannot be set twice. "
589 "Second value %s.\n", optarg
);
592 if (parse_uuid(optarg
, ident
.uuid
))
595 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
601 case O(ASSEMBLE
,'N'):
604 fprintf(stderr
, Name
": name cannot be set twice. "
605 "Second value %s.\n", optarg
);
608 if (mode
== MISC
&& !subarray
) {
609 fprintf(stderr
, Name
": -N/--name only valid with --update-subarray in misc mode\n");
612 if (strlen(optarg
) > 32) {
613 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
617 strcpy(ident
.name
, optarg
);
620 case O(ASSEMBLE
,'m'): /* super-minor for array */
621 if (ident
.super_minor
!= UnSet
) {
622 fprintf(stderr
, Name
": super-minor cannot be set twice. "
623 "Second value: %s.\n", optarg
);
626 if (strcmp(optarg
, "dev")==0)
627 ident
.super_minor
= -2;
629 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
630 if (!optarg
[0] || *cp
) {
631 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
637 case O(ASSEMBLE
,'U'): /* update the superblock */
640 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
644 if (mode
== MISC
&& !subarray
) {
645 fprintf(stderr
, Name
": Only subarrays can be updated in misc mode\n");
649 if (strcmp(update
, "sparc2.2")==0)
651 if (strcmp(update
, "super-minor") == 0)
653 if (strcmp(update
, "summaries")==0)
655 if (strcmp(update
, "resync")==0)
657 if (strcmp(update
, "uuid")==0)
659 if (strcmp(update
, "name")==0)
661 if (strcmp(update
, "homehost")==0)
663 if (strcmp(update
, "devicesize")==0)
665 if (strcmp(update
, "byteorder")==0) {
667 fprintf(stderr
, Name
": must not set metadata type with --update=byteorder.\n");
670 for(i
=0; !ss
&& superlist
[i
]; i
++)
671 ss
= superlist
[i
]->match_metadata_desc("0.swap");
673 fprintf(stderr
, Name
": INTERNAL ERROR cannot find 0.swap\n");
679 if (strcmp(update
,"?") == 0 ||
680 strcmp(update
, "help") == 0) {
682 fprintf(outf
, Name
": ");
686 Name
": '--update=%s' is invalid. ",
689 fprintf(outf
, "Valid --update options are:\n"
690 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
691 " 'summaries', 'homehost', 'byteorder', 'devicesize'.\n");
692 exit(outf
== stdout
? 0 : 2);
694 case O(INCREMENTAL
,NoDegraded
):
695 fprintf(stderr
, Name
": --no-degraded is deprecated in Incremental mode\n");
696 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
697 runstop
= -1; /* --stop isn't allowed for --assemble,
698 * so we overload slightly */
701 case O(ASSEMBLE
,'c'): /* config file */
702 case O(INCREMENTAL
, 'c'):
706 fprintf(stderr
, Name
": configfile cannot be set twice. "
707 "Second value is %s.\n", optarg
);
711 set_conffile(configfile
);
712 /* FIXME possibly check that config file exists. Even parse it */
714 case O(ASSEMBLE
,'s'): /* scan */
717 case O(INCREMENTAL
,'s'):
721 case O(MONITOR
,'m'): /* mail address */
723 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
729 case O(MONITOR
,'p'): /* alert program */
731 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
737 case O(MONITOR
,'r'): /* rebuild increments */
738 increments
= atoi(optarg
);
739 if (increments
>99 || increments
<1) {
740 fprintf(stderr
, Name
": please specify positive integer between 1 and 99 as rebuild increments.\n");
745 case O(MONITOR
,'d'): /* delay in seconds */
747 case O(BUILD
,'d'): /* delay for bitmap updates */
750 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
753 delay
= strtol(optarg
, &c
, 10);
754 if (!optarg
[0] || *c
|| delay
<1) {
755 fprintf(stderr
, Name
": invalid delay: %s\n",
761 case O(MONITOR
,'f'): /* daemonise */
764 case O(MONITOR
,'i'): /* pid */
766 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
771 case O(MONITOR
,'1'): /* oneshot */
774 case O(MONITOR
,'t'): /* test */
777 case O(MONITOR
,'y'): /* log messages to syslog */
778 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
782 /* now the general management options. Some are applicable
783 * to other modes. None have arguments.
786 case O(MANAGE
,'a'): /* add a drive */
790 case O(MANAGE
,ReAdd
):
794 case O(MANAGE
,'r'): /* remove a drive */
797 case O(MANAGE
,'f'): /* set faulty */
798 case O(INCREMENTAL
,'f'): /* r for incremental is taken, use f
799 * even though we will both fail and
800 * remove the device */
803 case O(INCREMENTAL
,'R'):
805 case O(ASSEMBLE
,'R'):
807 case O(CREATE
,'R'): /* Run the array */
809 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
816 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
835 case O(MISC
, Waitclean
):
836 case O(MISC
, DetailPlatform
):
837 case O(MISC
, KillSubarray
):
838 case O(MISC
, UpdateSubarray
):
839 if (devmode
&& devmode
!= opt
&&
840 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
841 fprintf(stderr
, Name
": --examine/-E cannot be given with ");
842 if (devmode
== 'E') {
843 if (option_index
>= 0)
844 fprintf(stderr
, "--%s\n",
845 long_options
[option_index
].name
);
847 fprintf(stderr
, "-%c\n", opt
);
848 } else if (isalpha(devmode
))
849 fprintf(stderr
, "-%c\n", devmode
);
851 fprintf(stderr
, "previous option\n");
860 case O(MISC
, Sparc22
):
861 if (devmode
!= 'E') {
862 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
868 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
870 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
873 if (strcmp(optarg
, "internal")==0) {
874 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
877 bitmap_fd
= open(optarg
, O_RDWR
);
878 if (!*optarg
|| bitmap_fd
< 0) {
879 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
882 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
885 case O(ASSEMBLE
, BackupFile
):
886 case O(GROW
, BackupFile
):
887 /* Specify a file into which grow might place a backup,
888 * or from which assemble might recover a backup
891 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
894 backup_file
= optarg
;
898 case O(CREATE
,'b'): /* here we create the bitmap */
899 if (strcmp(optarg
, "none") == 0) {
900 fprintf(stderr
, Name
": '--bitmap none' only"
901 " support for --grow\n");
906 if (strcmp(optarg
, "internal")== 0 ||
907 strcmp(optarg
, "none")== 0 ||
908 strchr(optarg
, '/') != NULL
) {
909 bitmap_file
= optarg
;
913 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n"
914 " not '%s'\n", optarg
);
917 case O(GROW
,BitmapChunk
):
918 case O(BUILD
,BitmapChunk
):
919 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
920 bitmap_chunk
= strtol(optarg
, &c
, 10);
921 if (!optarg
[0] || *c
|| bitmap_chunk
< 0 ||
922 bitmap_chunk
& (bitmap_chunk
- 1)) {
923 fprintf(stderr
, Name
": invalid bitmap chunksize: %s\n",
927 /* convert K to B, chunk of 0K means 512B */
928 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 1024 : 512;
931 case O(GROW
, WriteBehind
):
932 case O(BUILD
, WriteBehind
):
933 case O(CREATE
, WriteBehind
): /* write-behind mode */
934 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
936 write_behind
= strtol(optarg
, &c
, 10);
937 if (write_behind
< 0 || *c
||
938 write_behind
> 16383) {
939 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
945 case O(INCREMENTAL
, 'r'):
949 /* We have now processed all the valid options. Anything else is
952 if (option_index
> 0)
953 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
954 long_options
[option_index
].name
,
955 map_num(modes
, mode
));
957 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
958 opt
, map_num(modes
, mode
));
964 char *help_text
= Help
;
966 help_text
= OptionHelp
;
969 case ASSEMBLE
: help_text
= Help_assemble
; break;
970 case BUILD
: help_text
= Help_build
; break;
971 case CREATE
: help_text
= Help_create
; break;
972 case MANAGE
: help_text
= Help_manage
; break;
973 case MISC
: help_text
= Help_misc
; break;
974 case MONITOR
: help_text
= Help_monitor
; break;
975 case GROW
: help_text
= Help_grow
; break;
976 case INCREMENTAL
:help_text
= Help_incr
; break;
978 fputs(help_text
,stdout
);
982 if (!mode
&& devs_found
) {
985 if (devlist
->disposition
== 0)
986 devlist
->disposition
= devmode
;
989 fputs(Usage
, stderr
);
994 struct createinfo
*ci
= conf_get_create_info();
996 if (strcasecmp(symlinks
, "yes") == 0)
998 else if (strcasecmp(symlinks
, "no") == 0)
1001 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
1005 /* Ok, got the option parsing out of the way
1006 * hopefully it's mostly right but there might be some stuff
1009 * That is mosty checked in the per-mode stuff but...
1011 * For @,B,C and A without -s, the first device listed must be an md device
1012 * we check that here and open it.
1015 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
1016 (mode
== ASSEMBLE
&& ! scan
)) {
1017 if (devs_found
< 1) {
1018 fprintf(stderr
, Name
": an md device must be given in this mode\n");
1021 if ((int)ident
.super_minor
== -2 && autof
) {
1022 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
1025 if (mode
== MANAGE
|| mode
== GROW
) {
1026 mdfd
= open_mddev(devlist
->devname
, 1);
1030 /* non-existent device is OK */
1031 mdfd
= open_mddev(devlist
->devname
, 0);
1033 fprintf(stderr
, Name
": device %s exists but is not an "
1034 "md array.\n", devlist
->devname
);
1037 if ((int)ident
.super_minor
== -2) {
1040 fprintf(stderr
, Name
": --super-minor=dev given, and "
1041 "listed device %s doesn't exist.\n",
1046 ident
.super_minor
= minor(stb
.st_rdev
);
1048 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1049 /* We don't really want this open yet, we just might
1050 * have wanted to check some things
1058 if (raiddisks
> max_disks
) {
1059 fprintf(stderr
, Name
": invalid number of raid devices: %d\n",
1063 if (raiddisks
== 1 && !force
&& level
!= -5) {
1064 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
1065 " a mistake. If you really mean it you will need to specify --force before\n"
1066 " setting the number of drives.\n");
1071 if ( sparedisks
> max_disks
- raiddisks
) {
1072 fprintf(stderr
, Name
": invalid number of spare-devices: %d\n",
1078 if (homehost
== NULL
)
1079 homehost
= conf_get_homehost(&require_homehost
);
1080 if (homehost
== NULL
|| strcmp(homehost
, "<system>")==0) {
1081 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1082 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1083 homehost
= sys_hostname
;
1087 if ((mode
!= MISC
|| devmode
!= 'E') &&
1089 fprintf(stderr
, Name
": must be super-user to perform this action\n");
1093 ident
.autof
= autof
;
1098 /* readonly, add/remove, readwrite, runstop */
1100 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1101 if (!rv
&& devs_found
>1)
1102 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1103 devlist
->next
, verbose
-quiet
, test
);
1104 if (!rv
&& readonly
< 0)
1105 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1107 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1110 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1111 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1112 /* Only a device has been given, so get details from config file */
1113 mddev_ident_t array_ident
= conf_get_ident(devlist
->devname
);
1114 if (array_ident
== NULL
) {
1115 fprintf(stderr
, Name
": %s not identified in config file.\n",
1121 if (array_ident
->autof
== 0)
1122 array_ident
->autof
= autof
;
1123 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1125 readonly
, runstop
, update
,
1126 homehost
, require_homehost
,
1127 verbose
-quiet
, force
);
1130 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1131 devlist
->next
, backup_file
,
1132 readonly
, runstop
, update
,
1133 homehost
, require_homehost
,
1134 verbose
-quiet
, force
);
1135 else if (devs_found
>0) {
1136 if (update
&& devs_found
> 1) {
1137 fprintf(stderr
, Name
": can only update a single array at a time\n");
1140 if (backup_file
&& devs_found
> 1) {
1141 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1144 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1145 mddev_ident_t array_ident
= conf_get_ident(dv
->devname
);
1146 if (array_ident
== NULL
) {
1147 fprintf(stderr
, Name
": %s not identified in config file.\n",
1152 if (array_ident
->autof
== 0)
1153 array_ident
->autof
= autof
;
1154 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1156 readonly
, runstop
, update
,
1157 homehost
, require_homehost
,
1158 verbose
-quiet
, force
);
1161 mddev_ident_t a
, array_list
= conf_get_ident(NULL
);
1162 mddev_dev_t devlist
= conf_get_devs();
1164 int failures
, successes
;
1165 if (devlist
== NULL
) {
1166 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1170 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1174 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1177 for (a
= array_list
; a
; a
= a
->next
) {
1186 for (a
= array_list
; a
; a
= a
->next
) {
1191 strcasecmp(a
->devname
, "<ignore>") == 0)
1194 r
= Assemble(ss
, a
->devname
,
1197 readonly
, runstop
, NULL
,
1198 homehost
, require_homehost
,
1199 verbose
-quiet
, force
);
1208 } while (failures
&& successes
);
1209 if (homehost
&& cnt
== 0) {
1210 /* Maybe we can auto-assemble something.
1211 * Repeatedly call Assemble in auto-assemble mode
1216 ident
.autof
= autof
;
1218 mddev_dev_t devlist
= conf_get_devs();
1221 rv2
= Assemble(ss
, NULL
,
1224 readonly
, runstop
, NULL
,
1225 homehost
, require_homehost
,
1226 verbose
-quiet
, force
);
1232 /* found something so even though assembly failed we
1233 * want to avoid auto-updates
1235 auto_update_home
= 0;
1237 /* Incase there are stacked devices, we need to go around again */
1240 if (cnt
== 0 && auto_update_home
&& homehost
) {
1241 /* Nothing found, maybe we need to bootstrap homehost info */
1245 rv2
= Assemble(ss
, NULL
,
1248 readonly
, runstop
, "homehost",
1249 homehost
, require_homehost
,
1250 verbose
-quiet
, force
);
1256 /* Incase there are stacked devices, we need to go around again */
1260 if (cnt
== 0 && rv
== 0) {
1261 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1265 } else if (cnt
== 0 && rv
== 0) {
1266 fprintf(stderr
, Name
": No arrays found in config file\n");
1272 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1273 if (write_behind
&& !bitmap_file
) {
1274 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1278 if (raiddisks
== 0) {
1279 fprintf(stderr
, Name
": no raid-devices specified.\n");
1285 if (strcmp(bitmap_file
, "internal")==0) {
1286 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1291 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1292 raiddisks
, devlist
->next
, assume_clean
,
1293 bitmap_file
, bitmap_chunk
, write_behind
,
1294 delay
, verbose
-quiet
, autof
, size
);
1297 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1298 if (write_behind
&& !bitmap_file
) {
1299 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1303 if (raiddisks
== 0) {
1304 fprintf(stderr
, Name
": no raid-devices specified.\n");
1309 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1310 raiddisks
, sparedisks
, ident
.name
, homehost
,
1311 ident
.uuid_set
? ident
.uuid
: NULL
,
1312 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1313 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1316 if (devmode
== 'E') {
1317 if (devlist
== NULL
&& !scan
) {
1318 fprintf(stderr
, Name
": No devices to examine\n");
1321 if (devlist
== NULL
)
1322 devlist
= conf_get_devs();
1323 if (devlist
== NULL
) {
1324 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1327 if (brief
&& verbose
)
1329 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1331 SparcAdjust
, ss
, homehost
);
1332 } else if (devmode
== DetailPlatform
) {
1333 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1335 if (devlist
== NULL
) {
1336 if ((devmode
=='D' || devmode
== Waitclean
) && scan
) {
1337 /* apply --detail or --wait-clean to
1338 * all devices in /proc/mdstat
1340 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1341 struct mdstat_ent
*e
;
1342 struct map_ent
*map
= NULL
;
1344 int v
= verbose
>1?0:verbose
+1;
1346 for (members
= 0; members
<= 1; members
++) {
1347 for (e
=ms
; e
; e
=e
->next
) {
1350 int member
= e
->metadata_version
&&
1351 strncmp(e
->metadata_version
,
1352 "external:/", 10) == 0;
1353 if (members
!= member
)
1355 me
= map_by_devnum(&map
, e
->devnum
);
1357 && strcmp(me
->path
, "/unknown") != 0)
1360 name
= get_md_name(e
->devnum
);
1363 fprintf(stderr
, Name
": cannot find device file for %s\n",
1368 rv
|= Detail(name
, v
,
1372 rv
|= WaitClean(name
, -1, v
);
1377 } else if (devmode
== 'S' && scan
) {
1378 /* apply --stop to all devices in /proc/mdstat */
1379 /* Due to possible stacking of devices, repeat until
1380 * nothing more can be stopped
1382 int progress
=1, err
;
1385 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1386 struct mdstat_ent
*e
;
1388 if (!progress
) last
= 1;
1389 progress
= 0; err
= 0;
1390 for (e
=ms
; e
; e
=e
->next
) {
1391 char *name
= get_md_name(e
->devnum
);
1394 fprintf(stderr
, Name
": cannot find device file for %s\n",
1398 mdfd
= open_mddev(name
, 1);
1400 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1410 } while (!last
&& err
);
1413 fprintf(stderr
, Name
": No devices given.\n");
1417 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1418 switch(dv
->disposition
) {
1420 rv
|= Detail(dv
->devname
,
1422 export
, test
, homehost
);
1424 case 'K': /* Zero superblock */
1426 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1430 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1437 rv
|= Query(dv
->devname
); continue;
1439 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1441 rv
|= Wait(dv
->devname
); continue;
1443 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1445 rv
|= Kill_subarray(dv
->devname
, subarray
, quiet
);
1447 case UpdateSubarray
:
1448 if (update
== NULL
) {
1450 Name
": -U/--update must be specified with --update-subarray\n");
1454 rv
|= Update_subarray(dv
->devname
, subarray
, update
, &ident
, quiet
);
1457 mdfd
= open_mddev(dv
->devname
, 1);
1459 switch(dv
->disposition
) {
1461 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1463 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1465 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1467 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1476 if (!devlist
&& !scan
) {
1477 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1481 if (pidfile
&& !daemonise
) {
1482 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1487 if (get_linux_version() > 20616)
1488 /* mdstat responds to poll */
1493 rv
= Monitor(devlist
, mailaddr
, program
,
1494 delay
?delay
:60, daemonise
, scan
, oneshot
,
1495 dosyslog
, test
, pidfile
, increments
);
1499 if (array_size
>= 0) {
1500 /* alway impose array size first, independent of
1502 * Do not allow level or raid_disks changes at the
1503 * same time as that can be irreversibly destructive.
1507 if (raiddisks
|| level
!= UnSet
) {
1508 fprintf(stderr
, Name
": cannot change array size in same operation "
1509 "as changing raiddisks or level.\n"
1510 " Change size first, then check that data is still intact.\n");
1514 sysfs_init(&sra
, mdfd
, 0);
1515 if (array_size
== 0)
1516 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1518 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1521 fprintf(stderr
, Name
": --array-size setting"
1522 " is too large.\n");
1524 fprintf(stderr
, Name
": current kernel does"
1525 " not support setting --array-size\n");
1530 if (devs_found
> 1) {
1533 if (size
>= 0 || raiddisks
|| chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1534 fprintf(stderr
, Name
": --add cannot be used with other geometry changes in --grow mode\n");
1538 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1539 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
1543 } else if (bitmap_file
) {
1544 if (size
>= 0 || raiddisks
|| chunk
|| layout_str
!= NULL
) {
1545 fprintf(stderr
, Name
": --bitmap changes cannot be used with other geometry changes in --grow mode\n");
1550 delay
= DEFAULT_BITMAP_DELAY
;
1551 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1552 bitmap_chunk
, delay
, write_behind
, force
);
1553 } else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1554 || chunk
!= 0 || level
!= UnSet
) {
1555 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1556 size
, level
, layout_str
, chunk
, raiddisks
);
1557 } else if (array_size
< 0)
1558 fprintf(stderr
, Name
": no changes to --grow\n");
1566 fprintf(stderr
, Name
1567 ": --incremental --scan meaningless without --run.\n");
1570 if (devmode
== 'f') {
1571 fprintf(stderr
, Name
1572 ": --incremental --scan --fail not supported.\n");
1575 rv
= IncrementalScan(verbose
);
1578 if (!rebuild_map
&& !scan
) {
1579 fprintf(stderr
, Name
1580 ": --incremental requires a device.\n");
1585 if (devlist
->next
) {
1586 fprintf(stderr
, Name
1587 ": --incremental can only handle one device.\n");
1591 if (devmode
== 'f') {
1592 rv
= IncrementalRemove(devlist
->devname
, verbose
-quiet
);
1595 rv
= Incremental(devlist
->devname
, verbose
-quiet
, runstop
,
1596 ss
, homehost
, require_homehost
, autof
);