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
;
50 struct mddev_ident_s ident
;
51 char *configfile
= NULL
;
60 char *bitmap_file
= NULL
;
61 char *backup_file
= NULL
;
62 int bitmap_chunk
= UnSet
;
64 mddev_dev_t devlist
= NULL
;
65 mddev_dev_t
*devlistend
= & devlist
;
75 char *symlinks
= NULL
;
76 /* autof indicates whether and how to create device node.
77 * bottom 3 bits are style. Rest (when shifted) are number of parts
79 * 1 - don't create (no)
80 * 2 - if is_standard, then create (yes)
81 * 3 - create as 'md' - reject is_standard mdp (md)
82 * 4 - create as 'mdp' - reject is_standard md (mdp)
83 * 5 - default to md if not is_standard (md in config file)
84 * 6 - default to mdp if not is_standard (part, or mdp in config file)
88 char *homehost
= NULL
;
89 char sys_hostname
[256];
90 int require_homehost
= 1;
91 char *mailaddr
= NULL
;
98 struct supertype
*ss
= NULL
;
101 char *shortopt
= short_options
;
104 int auto_update_home
= 0;
105 char *subarray
= NULL
;
112 srandom(time(0) ^ getpid());
116 ident
.raid_disks
= UnSet
;
117 ident
.super_minor
= UnSet
;
119 ident
.spare_group
= NULL
;
122 ident
.bitmap_fd
= -1;
123 ident
.bitmap_file
= NULL
;
125 ident
.container
= NULL
;
128 while ((option_index
= -1) ,
129 (opt
=getopt_long(argc
, argv
,
130 shortopt
, long_options
,
131 &option_index
)) != -1) {
133 /* firstly, some mode-independent options */
136 if (option_index
> 0 &&
137 strcmp(long_options
[option_index
].name
, "help-options")==0)
144 fputs(Version
, stderr
);
154 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
155 mode
== INCREMENTAL
|| mode
== MANAGE
)
156 break; /* b means bitmap */
164 if (strcasecmp(optarg
, "<ignore>") == 0)
165 require_homehost
= 0;
172 fputs(Usage
, stderr
);
175 /* second, figure out the mode.
176 * Some options force the mode. Others
177 * set the mode if it isn't already
181 case '@': /* just incase they say --manage */
183 shortopt
= short_bitmap_options
;
188 case ReAdd
: /* re-add */
191 shortopt
= short_bitmap_options
;
195 case 'A': newmode
= ASSEMBLE
; shortopt
= short_bitmap_auto_options
; break;
196 case 'B': newmode
= BUILD
; shortopt
= short_bitmap_auto_options
; break;
197 case 'C': newmode
= CREATE
; shortopt
= short_bitmap_auto_options
; break;
198 case 'F': newmode
= MONITOR
;break;
199 case 'G': newmode
= GROW
;
200 shortopt
= short_bitmap_options
;
202 case 'I': newmode
= INCREMENTAL
;
203 shortopt
= short_bitmap_auto_options
; break;
205 newmode
= AUTODETECT
; break;
211 case 'Q': newmode
= MISC
; break;
221 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
223 fprintf(stderr
, Name
": subarray can only be specified once\n");
228 case 'K': if (!mode
) newmode
= MISC
; break;
230 if (mode
&& newmode
== mode
) {
231 /* everybody happy ! */
232 } else if (mode
&& newmode
!= mode
) {
234 fprintf(stderr
, Name
": ");
235 if (option_index
>= 0)
236 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
238 fprintf(stderr
, "-%c", opt
);
239 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
240 map_num(modes
, newmode
),
241 map_num(modes
, mode
));
243 } else if (!mode
&& newmode
) {
246 /* special case of -c --help */
248 ( strncmp(optarg
, "--h", 3)==0 ||
249 strncmp(optarg
, "-h", 2)==0)) {
250 fputs(Help_config
, stdout
);
254 /* If first option is a device, don't force the mode yet */
256 if (devs_found
== 0) {
257 dv
= malloc(sizeof(*dv
));
259 fprintf(stderr
, Name
": malloc failed\n");
262 dv
->devname
= optarg
;
263 dv
->disposition
= devmode
;
264 dv
->writemostly
= writemostly
;
270 devlistend
= &dv
->next
;
275 /* No mode yet, and this is the second device ... */
276 fprintf(stderr
, Name
": An option must be given to set the mode before a second device\n"
277 " (%s) is listed\n", optarg
);
280 if (option_index
>= 0)
281 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
283 fprintf(stderr
, Name
": -%c", opt
);
284 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
288 /* if we just set the mode, then done */
302 /* an undecorated option - must be a device name.
304 if (devs_found
> 0 && mode
== '@' && !devmode
) {
305 fprintf(stderr
, Name
": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
308 if (devs_found
> 0 && mode
== 'G' && !devmode
) {
309 fprintf(stderr
, Name
": Must give one of -a for devices do add: %s\n", optarg
);
312 dv
= malloc(sizeof(*dv
));
314 fprintf(stderr
, Name
": malloc failed\n");
317 dv
->devname
= optarg
;
318 dv
->disposition
= devmode
;
319 dv
->writemostly
= writemostly
;
325 devlistend
= &dv
->next
;
331 /* We've got a mode, and opt is now something else which
332 * could depend on the mode */
333 #define O(a,b) ((a<<8)|b)
334 switch (O(mode
,opt
)) {
337 case O(BUILD
,'c'): /* chunk or rounding */
339 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
340 "Second value is %s.\n", optarg
);
343 chunk
= strtol(optarg
, &c
, 10);
344 if (!optarg
[0] || *c
|| chunk
<4 || ((chunk
-1)&chunk
)) {
345 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
352 case O(ASSEMBLE
,AutoHomeHost
):
353 auto_update_home
= 1;
356 case O(INCREMENTAL
, 'e'):
358 case O(ASSEMBLE
,'e'):
359 case O(MISC
,'e'): /* set metadata (superblock) information */
361 fprintf(stderr
, Name
": metadata information already given\n");
364 for(i
=0; !ss
&& superlist
[i
]; i
++)
365 ss
= superlist
[i
]->match_metadata_desc(optarg
);
368 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
376 /* set write-mostly for following devices */
381 /* clear write-mostly for following devices */
388 case O(BUILD
,'z'): /* size */
390 fprintf(stderr
, Name
": size may only be specified once. "
391 "Second value is %s.\n", optarg
);
394 if (strcmp(optarg
, "max")==0)
397 size
= parse_size(optarg
);
399 fprintf(stderr
, Name
": invalid size: %s\n",
403 /* convert sectors to K */
408 case O(GROW
,'Z'): /* array size */
409 if (array_size
>= 0) {
410 fprintf(stderr
, Name
": array-size may only be specified once. "
411 "Second value is %s.\n", optarg
);
414 if (strcmp(optarg
, "max") == 0)
417 array_size
= parse_size(optarg
);
418 if (array_size
<= 0) {
419 fprintf(stderr
, Name
": invalid array size: %s\n",
428 case O(BUILD
,'l'): /* set raid level*/
429 if (level
!= UnSet
) {
430 fprintf(stderr
, Name
": raid level may only be set once. "
431 "Second value is %s.\n", optarg
);
434 level
= map_name(pers
, optarg
);
435 if (level
== UnSet
) {
436 fprintf(stderr
, Name
": invalid raid level: %s\n",
440 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
441 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
444 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
448 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
449 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
456 case O(GROW
, 'p'): /* new layout */
458 fprintf(stderr
,Name
": layout may only be sent once. "
459 "Second value was %s\n", optarg
);
463 /* 'Grow' will parse the value */
466 case O(CREATE
,'p'): /* raid5 layout */
467 case O(BUILD
,'p'): /* faulty layout */
468 if (layout
!= UnSet
) {
469 fprintf(stderr
,Name
": layout may only be sent once. "
470 "Second value was %s\n", optarg
);
475 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
476 map_num(pers
, level
));
479 fprintf(stderr
, Name
": raid level must be given before layout.\n");
483 layout
= map_name(r5layout
, optarg
);
485 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
491 layout
= map_name(r6layout
, optarg
);
493 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
500 layout
= parse_layout_10(optarg
);
502 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
510 layout
= parse_layout_faulty(optarg
);
512 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
520 case O(CREATE
,AssumeClean
):
521 case O(BUILD
,AssumeClean
): /* assume clean */
527 case O(BUILD
,'n'): /* number of raid disks */
529 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
533 raiddisks
= strtol(optarg
, &c
, 10);
534 if (!optarg
[0] || *c
|| raiddisks
<=0) {
535 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
539 ident
.raid_disks
= raiddisks
;
542 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
544 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
548 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
549 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
553 sparedisks
= strtol(optarg
, &c
, 10);
554 if (!optarg
[0] || *c
|| sparedisks
< 0) {
555 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
563 case O(INCREMENTAL
,'a'):
564 case O(ASSEMBLE
,'a'): /* auto-creation of device node */
565 autof
= parse_auto(optarg
, "--auto flag", 0);
568 case O(CREATE
,Symlinks
):
569 case O(BUILD
,Symlinks
):
570 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
574 case O(BUILD
,'f'): /* force honouring '-n 1' */
575 case O(GROW
,'f'): /* ditto */
576 case O(CREATE
,'f'): /* force honouring of device list */
577 case O(ASSEMBLE
,'f'): /* force assembly */
578 case O(MISC
,'f'): /* force zero */
582 /* now for the Assemble options */
583 case O(CREATE
,'u'): /* uuid of array */
584 case O(ASSEMBLE
,'u'): /* uuid of array */
585 if (ident
.uuid_set
) {
586 fprintf(stderr
, Name
": uuid cannot be set twice. "
587 "Second value %s.\n", optarg
);
590 if (parse_uuid(optarg
, ident
.uuid
))
593 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
599 case O(ASSEMBLE
,'N'):
602 fprintf(stderr
, Name
": name cannot be set twice. "
603 "Second value %s.\n", optarg
);
606 if (mode
== MISC
&& !subarray
) {
607 fprintf(stderr
, Name
": -N/--name only valid with --update-subarray in misc mode\n");
610 if (strlen(optarg
) > 32) {
611 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
615 strcpy(ident
.name
, optarg
);
618 case O(ASSEMBLE
,'m'): /* super-minor for array */
619 if (ident
.super_minor
!= UnSet
) {
620 fprintf(stderr
, Name
": super-minor cannot be set twice. "
621 "Second value: %s.\n", optarg
);
624 if (strcmp(optarg
, "dev")==0)
625 ident
.super_minor
= -2;
627 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
628 if (!optarg
[0] || *cp
) {
629 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
635 case O(ASSEMBLE
,'U'): /* update the superblock */
638 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
642 if (mode
== MISC
&& !subarray
) {
643 fprintf(stderr
, Name
": Only subarrays can be updated in misc mode\n");
647 if (strcmp(update
, "sparc2.2")==0)
649 if (strcmp(update
, "super-minor") == 0)
651 if (strcmp(update
, "summaries")==0)
653 if (strcmp(update
, "resync")==0)
655 if (strcmp(update
, "uuid")==0)
657 if (strcmp(update
, "name")==0)
659 if (strcmp(update
, "homehost")==0)
661 if (strcmp(update
, "devicesize")==0)
663 if (strcmp(update
, "byteorder")==0) {
665 fprintf(stderr
, Name
": must not set metadata type with --update=byteorder.\n");
668 for(i
=0; !ss
&& superlist
[i
]; i
++)
669 ss
= superlist
[i
]->match_metadata_desc("0.swap");
671 fprintf(stderr
, Name
": INTERNAL ERROR cannot find 0.swap\n");
677 if (strcmp(update
,"?") == 0 ||
678 strcmp(update
, "help") == 0) {
680 fprintf(outf
, Name
": ");
684 Name
": '--update=%s' is invalid. ",
687 fprintf(outf
, "Valid --update options are:\n"
688 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
689 " 'summaries', 'homehost', 'byteorder', 'devicesize'.\n");
690 exit(outf
== stdout
? 0 : 2);
692 case O(INCREMENTAL
,NoDegraded
):
693 fprintf(stderr
, Name
": --no-degraded is deprecated in Incremental mode\n");
694 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
695 runstop
= -1; /* --stop isn't allowed for --assemble,
696 * so we overload slightly */
699 case O(ASSEMBLE
,'c'): /* config file */
700 case O(INCREMENTAL
, 'c'):
704 fprintf(stderr
, Name
": configfile cannot be set twice. "
705 "Second value is %s.\n", optarg
);
709 set_conffile(configfile
);
710 /* FIXME possibly check that config file exists. Even parse it */
712 case O(ASSEMBLE
,'s'): /* scan */
715 case O(INCREMENTAL
,'s'):
719 case O(MONITOR
,'m'): /* mail address */
721 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
727 case O(MONITOR
,'p'): /* alert program */
729 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
735 case O(MONITOR
,'r'): /* rebuild increments */
736 increments
= atoi(optarg
);
737 if (increments
>99 || increments
<1) {
738 fprintf(stderr
, Name
": please specify positive integer between 1 and 99 as rebuild increments.\n");
743 case O(MONITOR
,'d'): /* delay in seconds */
745 case O(BUILD
,'d'): /* delay for bitmap updates */
748 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
751 delay
= strtol(optarg
, &c
, 10);
752 if (!optarg
[0] || *c
|| delay
<1) {
753 fprintf(stderr
, Name
": invalid delay: %s\n",
759 case O(MONITOR
,'f'): /* daemonise */
762 case O(MONITOR
,'i'): /* pid */
764 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
769 case O(MONITOR
,'1'): /* oneshot */
772 case O(MONITOR
,'t'): /* test */
775 case O(MONITOR
,'y'): /* log messages to syslog */
776 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
780 /* now the general management options. Some are applicable
781 * to other modes. None have arguments.
784 case O(MANAGE
,'a'): /* add a drive */
788 case O(MANAGE
,ReAdd
):
792 case O(MANAGE
,'r'): /* remove a drive */
795 case O(MANAGE
,'f'): /* set faulty */
796 case O(INCREMENTAL
,'f'): /* r for incremental is taken, use f
797 * even though we will both fail and
798 * remove the device */
801 case O(INCREMENTAL
,'R'):
803 case O(ASSEMBLE
,'R'):
805 case O(CREATE
,'R'): /* Run the array */
807 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
814 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
833 case O(MISC
, Waitclean
):
834 case O(MISC
, DetailPlatform
):
835 case O(MISC
, KillSubarray
):
836 case O(MISC
, UpdateSubarray
):
837 if (devmode
&& devmode
!= opt
&&
838 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
839 fprintf(stderr
, Name
": --examine/-E cannot be given with ");
840 if (devmode
== 'E') {
841 if (option_index
>= 0)
842 fprintf(stderr
, "--%s\n",
843 long_options
[option_index
].name
);
845 fprintf(stderr
, "-%c\n", opt
);
846 } else if (isalpha(devmode
))
847 fprintf(stderr
, "-%c\n", devmode
);
849 fprintf(stderr
, "previous option\n");
858 case O(MISC
, Sparc22
):
859 if (devmode
!= 'E') {
860 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
866 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
868 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
871 if (strcmp(optarg
, "internal")==0) {
872 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
875 bitmap_fd
= open(optarg
, O_RDWR
);
876 if (!*optarg
|| bitmap_fd
< 0) {
877 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
880 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
883 case O(ASSEMBLE
, BackupFile
):
884 case O(GROW
, BackupFile
):
885 /* Specify a file into which grow might place a backup,
886 * or from which assemble might recover a backup
889 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
892 backup_file
= optarg
;
896 case O(CREATE
,'b'): /* here we create the bitmap */
897 if (strcmp(optarg
, "none") == 0) {
898 fprintf(stderr
, Name
": '--bitmap none' only"
899 " support for --grow\n");
904 if (strcmp(optarg
, "internal")== 0 ||
905 strcmp(optarg
, "none")== 0 ||
906 strchr(optarg
, '/') != NULL
) {
907 bitmap_file
= optarg
;
911 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n"
912 " not '%s'\n", optarg
);
915 case O(GROW
,BitmapChunk
):
916 case O(BUILD
,BitmapChunk
):
917 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
918 bitmap_chunk
= strtol(optarg
, &c
, 10);
919 if (!optarg
[0] || *c
|| bitmap_chunk
< 0 ||
920 bitmap_chunk
& (bitmap_chunk
- 1)) {
921 fprintf(stderr
, Name
": invalid bitmap chunksize: %s\n",
925 /* convert K to B, chunk of 0K means 512B */
926 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 1024 : 512;
929 case O(GROW
, WriteBehind
):
930 case O(BUILD
, WriteBehind
):
931 case O(CREATE
, WriteBehind
): /* write-behind mode */
932 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
934 write_behind
= strtol(optarg
, &c
, 10);
935 if (write_behind
< 0 || *c
||
936 write_behind
> 16383) {
937 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
943 case O(INCREMENTAL
, 'r'):
947 /* We have now processed all the valid options. Anything else is
950 if (option_index
> 0)
951 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
952 long_options
[option_index
].name
,
953 map_num(modes
, mode
));
955 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
956 opt
, map_num(modes
, mode
));
962 char *help_text
= Help
;
964 help_text
= OptionHelp
;
967 case ASSEMBLE
: help_text
= Help_assemble
; break;
968 case BUILD
: help_text
= Help_build
; break;
969 case CREATE
: help_text
= Help_create
; break;
970 case MANAGE
: help_text
= Help_manage
; break;
971 case MISC
: help_text
= Help_misc
; break;
972 case MONITOR
: help_text
= Help_monitor
; break;
973 case GROW
: help_text
= Help_grow
; break;
974 case INCREMENTAL
:help_text
= Help_incr
; break;
976 fputs(help_text
,stdout
);
980 if (!mode
&& devs_found
) {
983 if (devlist
->disposition
== 0)
984 devlist
->disposition
= devmode
;
987 fputs(Usage
, stderr
);
992 struct createinfo
*ci
= conf_get_create_info();
994 if (strcasecmp(symlinks
, "yes") == 0)
996 else if (strcasecmp(symlinks
, "no") == 0)
999 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
1003 /* Ok, got the option parsing out of the way
1004 * hopefully it's mostly right but there might be some stuff
1007 * That is mosty checked in the per-mode stuff but...
1009 * For @,B,C and A without -s, the first device listed must be an md device
1010 * we check that here and open it.
1013 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
1014 (mode
== ASSEMBLE
&& ! scan
)) {
1015 if (devs_found
< 1) {
1016 fprintf(stderr
, Name
": an md device must be given in this mode\n");
1019 if ((int)ident
.super_minor
== -2 && autof
) {
1020 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
1023 if (mode
== MANAGE
|| mode
== GROW
) {
1024 mdfd
= open_mddev(devlist
->devname
, 1);
1028 /* non-existent device is OK */
1029 mdfd
= open_mddev(devlist
->devname
, 0);
1031 fprintf(stderr
, Name
": device %s exists but is not an "
1032 "md array.\n", devlist
->devname
);
1035 if ((int)ident
.super_minor
== -2) {
1038 fprintf(stderr
, Name
": --super-minor=dev given, and "
1039 "listed device %s doesn't exist.\n",
1044 ident
.super_minor
= minor(stb
.st_rdev
);
1046 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1047 /* We don't really want this open yet, we just might
1048 * have wanted to check some things
1056 if (raiddisks
== 1 && !force
&& level
!= -5) {
1057 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
1058 " a mistake. If you really mean it you will need to specify --force before\n"
1059 " setting the number of drives.\n");
1064 if (homehost
== NULL
)
1065 homehost
= conf_get_homehost(&require_homehost
);
1066 if (homehost
== NULL
|| strcmp(homehost
, "<system>")==0) {
1067 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1068 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1069 homehost
= sys_hostname
;
1073 if ((mode
!= MISC
|| devmode
!= 'E') &&
1075 fprintf(stderr
, Name
": must be super-user to perform this action\n");
1079 ident
.autof
= autof
;
1084 /* readonly, add/remove, readwrite, runstop */
1086 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1087 if (!rv
&& devs_found
>1)
1088 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1089 devlist
->next
, verbose
-quiet
, test
);
1090 if (!rv
&& readonly
< 0)
1091 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1093 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1096 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1097 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1098 /* Only a device has been given, so get details from config file */
1099 mddev_ident_t array_ident
= conf_get_ident(devlist
->devname
);
1100 if (array_ident
== NULL
) {
1101 fprintf(stderr
, Name
": %s not identified in config file.\n",
1107 if (array_ident
->autof
== 0)
1108 array_ident
->autof
= autof
;
1109 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1111 readonly
, runstop
, update
,
1112 homehost
, require_homehost
,
1113 verbose
-quiet
, force
);
1116 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1117 devlist
->next
, backup_file
,
1118 readonly
, runstop
, update
,
1119 homehost
, require_homehost
,
1120 verbose
-quiet
, force
);
1121 else if (devs_found
>0) {
1122 if (update
&& devs_found
> 1) {
1123 fprintf(stderr
, Name
": can only update a single array at a time\n");
1126 if (backup_file
&& devs_found
> 1) {
1127 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1130 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1131 mddev_ident_t array_ident
= conf_get_ident(dv
->devname
);
1132 if (array_ident
== NULL
) {
1133 fprintf(stderr
, Name
": %s not identified in config file.\n",
1138 if (array_ident
->autof
== 0)
1139 array_ident
->autof
= autof
;
1140 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1142 readonly
, runstop
, update
,
1143 homehost
, require_homehost
,
1144 verbose
-quiet
, force
);
1147 mddev_ident_t a
, array_list
= conf_get_ident(NULL
);
1148 mddev_dev_t devlist
= conf_get_devs();
1150 int failures
, successes
;
1151 if (devlist
== NULL
) {
1152 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1156 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1160 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1163 for (a
= array_list
; a
; a
= a
->next
) {
1172 for (a
= array_list
; a
; a
= a
->next
) {
1177 strcasecmp(a
->devname
, "<ignore>") == 0)
1180 r
= Assemble(ss
, a
->devname
,
1183 readonly
, runstop
, NULL
,
1184 homehost
, require_homehost
,
1185 verbose
-quiet
, force
);
1194 } while (failures
&& successes
);
1195 if (homehost
&& cnt
== 0) {
1196 /* Maybe we can auto-assemble something.
1197 * Repeatedly call Assemble in auto-assemble mode
1202 ident
.autof
= autof
;
1204 mddev_dev_t devlist
= conf_get_devs();
1207 rv2
= Assemble(ss
, NULL
,
1210 readonly
, runstop
, NULL
,
1211 homehost
, require_homehost
,
1212 verbose
-quiet
, force
);
1218 /* found something so even though assembly failed we
1219 * want to avoid auto-updates
1221 auto_update_home
= 0;
1223 /* Incase there are stacked devices, we need to go around again */
1226 if (cnt
== 0 && auto_update_home
&& homehost
) {
1227 /* Nothing found, maybe we need to bootstrap homehost info */
1231 rv2
= Assemble(ss
, NULL
,
1234 readonly
, runstop
, "homehost",
1235 homehost
, require_homehost
,
1236 verbose
-quiet
, force
);
1242 /* Incase there are stacked devices, we need to go around again */
1246 if (cnt
== 0 && rv
== 0) {
1247 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1251 } else if (cnt
== 0 && rv
== 0) {
1252 fprintf(stderr
, Name
": No arrays found in config file\n");
1258 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1259 if (write_behind
&& !bitmap_file
) {
1260 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1264 if (raiddisks
== 0) {
1265 fprintf(stderr
, Name
": no raid-devices specified.\n");
1271 if (strcmp(bitmap_file
, "internal")==0) {
1272 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1277 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1278 raiddisks
, devlist
->next
, assume_clean
,
1279 bitmap_file
, bitmap_chunk
, write_behind
,
1280 delay
, verbose
-quiet
, autof
, size
);
1283 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1284 if (write_behind
&& !bitmap_file
) {
1285 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1289 if (raiddisks
== 0) {
1290 fprintf(stderr
, Name
": no raid-devices specified.\n");
1295 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1296 raiddisks
, sparedisks
, ident
.name
, homehost
,
1297 ident
.uuid_set
? ident
.uuid
: NULL
,
1298 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1299 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1302 if (devmode
== 'E') {
1303 if (devlist
== NULL
&& !scan
) {
1304 fprintf(stderr
, Name
": No devices to examine\n");
1307 if (devlist
== NULL
)
1308 devlist
= conf_get_devs();
1309 if (devlist
== NULL
) {
1310 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1313 if (brief
&& verbose
)
1315 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1317 SparcAdjust
, ss
, homehost
);
1318 } else if (devmode
== DetailPlatform
) {
1319 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1321 if (devlist
== NULL
) {
1322 if ((devmode
=='D' || devmode
== Waitclean
) && scan
) {
1323 /* apply --detail or --wait-clean to
1324 * all devices in /proc/mdstat
1326 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1327 struct mdstat_ent
*e
;
1328 struct map_ent
*map
= NULL
;
1330 int v
= verbose
>1?0:verbose
+1;
1332 for (members
= 0; members
<= 1; members
++) {
1333 for (e
=ms
; e
; e
=e
->next
) {
1336 int member
= e
->metadata_version
&&
1337 strncmp(e
->metadata_version
,
1338 "external:/", 10) == 0;
1339 if (members
!= member
)
1341 me
= map_by_devnum(&map
, e
->devnum
);
1343 && strcmp(me
->path
, "/unknown") != 0)
1346 name
= get_md_name(e
->devnum
);
1349 fprintf(stderr
, Name
": cannot find device file for %s\n",
1354 rv
|= Detail(name
, v
,
1358 rv
|= WaitClean(name
, -1, v
);
1363 } else if (devmode
== 'S' && scan
) {
1364 /* apply --stop to all devices in /proc/mdstat */
1365 /* Due to possible stacking of devices, repeat until
1366 * nothing more can be stopped
1368 int progress
=1, err
;
1371 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1372 struct mdstat_ent
*e
;
1374 if (!progress
) last
= 1;
1375 progress
= 0; err
= 0;
1376 for (e
=ms
; e
; e
=e
->next
) {
1377 char *name
= get_md_name(e
->devnum
);
1380 fprintf(stderr
, Name
": cannot find device file for %s\n",
1384 mdfd
= open_mddev(name
, 1);
1386 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1396 } while (!last
&& err
);
1399 fprintf(stderr
, Name
": No devices given.\n");
1403 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1404 switch(dv
->disposition
) {
1406 rv
|= Detail(dv
->devname
,
1408 export
, test
, homehost
);
1410 case 'K': /* Zero superblock */
1412 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1416 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1423 rv
|= Query(dv
->devname
); continue;
1425 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1427 rv
|= Wait(dv
->devname
); continue;
1429 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1431 rv
|= Kill_subarray(dv
->devname
, subarray
, quiet
);
1433 case UpdateSubarray
:
1434 if (update
== NULL
) {
1436 Name
": -U/--update must be specified with --update-subarray\n");
1440 rv
|= Update_subarray(dv
->devname
, subarray
, update
, &ident
, quiet
);
1443 mdfd
= open_mddev(dv
->devname
, 1);
1445 switch(dv
->disposition
) {
1447 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1449 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1451 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1453 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1462 if (!devlist
&& !scan
) {
1463 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1467 if (pidfile
&& !daemonise
) {
1468 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1473 if (get_linux_version() > 20616)
1474 /* mdstat responds to poll */
1479 rv
= Monitor(devlist
, mailaddr
, program
,
1480 delay
?delay
:60, daemonise
, scan
, oneshot
,
1481 dosyslog
, test
, pidfile
, increments
);
1485 if (array_size
>= 0) {
1486 /* alway impose array size first, independent of
1488 * Do not allow level or raid_disks changes at the
1489 * same time as that can be irreversibly destructive.
1493 if (raiddisks
|| level
!= UnSet
) {
1494 fprintf(stderr
, Name
": cannot change array size in same operation "
1495 "as changing raiddisks or level.\n"
1496 " Change size first, then check that data is still intact.\n");
1500 sysfs_init(&sra
, mdfd
, 0);
1501 if (array_size
== 0)
1502 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1504 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1507 fprintf(stderr
, Name
": --array-size setting"
1508 " is too large.\n");
1510 fprintf(stderr
, Name
": current kernel does"
1511 " not support setting --array-size\n");
1516 if (devs_found
> 1) {
1519 if (size
>= 0 || raiddisks
|| chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1520 fprintf(stderr
, Name
": --add cannot be used with other geometry changes in --grow mode\n");
1524 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1525 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
1529 } else if (bitmap_file
) {
1530 if (size
>= 0 || raiddisks
|| chunk
|| layout_str
!= NULL
) {
1531 fprintf(stderr
, Name
": --bitmap changes cannot be used with other geometry changes in --grow mode\n");
1536 delay
= DEFAULT_BITMAP_DELAY
;
1537 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1538 bitmap_chunk
, delay
, write_behind
, force
);
1539 } else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1540 || chunk
!= 0 || level
!= UnSet
) {
1541 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1542 size
, level
, layout_str
, chunk
, raiddisks
);
1543 } else if (array_size
< 0)
1544 fprintf(stderr
, Name
": no changes to --grow\n");
1552 fprintf(stderr
, Name
1553 ": --incremental --scan meaningless without --run.\n");
1556 if (devmode
== 'f') {
1557 fprintf(stderr
, Name
1558 ": --incremental --scan --fail not supported.\n");
1561 rv
= IncrementalScan(verbose
);
1564 if (!rebuild_map
&& !scan
) {
1565 fprintf(stderr
, Name
1566 ": --incremental requires a device.\n");
1571 if (devlist
->next
) {
1572 fprintf(stderr
, Name
1573 ": --incremental can only handle one device.\n");
1577 if (devmode
== 'f') {
1578 rv
= IncrementalRemove(devlist
->devname
, verbose
-quiet
);
1581 rv
= Incremental(devlist
->devname
, verbose
-quiet
, runstop
,
1582 ss
, homehost
, require_homehost
, autof
);