2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2012 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 static int scan_assemble(int autof
, struct supertype
*ss
,
34 int readonly
, int runstop
,
35 struct mddev_ident
*ident
,
36 char *homehost
, int require_homehost
,
37 int verbose
, int force
,
39 static int misc_scan(char devmode
, int verbose
, int export
, int test
,
40 char *homehost
, char *prefer
);
41 static int stop_scan(int verbose
);
42 static int misc_list(struct mddev_dev
*devlist
,
43 int brief
, int verbose
, int export
, int test
,
44 char *homehost
, char *prefer
, char *subarray
,
45 char *update
, struct mddev_ident
*ident
,
46 struct supertype
*ss
, int force
);
49 int main(int argc
, char *argv
[])
59 long long array_size
= -1;
62 char *layout_str
= NULL
;
65 struct mddev_ident ident
;
66 char *configfile
= NULL
;
70 char *bitmap_file
= NULL
;
71 int bitmap_chunk
= UnSet
;
72 struct mddev_dev
*devlist
= NULL
;
73 struct mddev_dev
**devlistend
= & devlist
;
77 char *symlinks
= NULL
;
78 int grow_continue
= 0;
79 /* autof indicates whether and how to create device node.
80 * bottom 3 bits are style. Rest (when shifted) are number of parts
82 * 1 - don't create (no)
83 * 2 - if is_standard, then create (yes)
84 * 3 - create as 'md' - reject is_standard mdp (md)
85 * 4 - create as 'mdp' - reject is_standard md (mdp)
86 * 5 - default to md if not is_standard (md in config file)
87 * 6 - default to mdp if not is_standard (part, or mdp in config file)
90 .require_homehost
= 1,
93 char sys_hostname
[256];
94 char *mailaddr
= NULL
;
100 int spare_sharing
= 1;
101 struct supertype
*ss
= NULL
;
103 char *shortopt
= short_options
;
106 char *remove_path
= NULL
;
107 char *udev_filename
= NULL
;
114 srandom(time(0) ^ getpid());
118 ident
.raid_disks
= UnSet
;
119 ident
.super_minor
= UnSet
;
121 ident
.spare_group
= NULL
;
124 ident
.bitmap_fd
= -1;
125 ident
.bitmap_file
= NULL
;
127 ident
.container
= NULL
;
130 while ((option_index
= -1) ,
131 (opt
=getopt_long(argc
, argv
,
132 shortopt
, long_options
,
133 &option_index
)) != -1) {
135 /* firstly, some mode-independent options */
145 fputs(Version
, stderr
);
148 case 'v': c
.verbose
++;
155 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
156 || mode
== GROW
|| mode
== INCREMENTAL
158 break; /* b means bitmap */
163 case 'Y': c
.export
++;
167 if (strcasecmp(optarg
, "<ignore>") == 0)
168 c
.require_homehost
= 0;
174 * --offroot sets first char of argv[0] to @. This is used
175 * by systemd to signal that the task was launched from
176 * initrd/initramfs and should be preserved during shutdown
186 if (asprintf(&c
.prefer
, "/%s/", optarg
) <= 0)
192 fputs(Usage
, stderr
);
195 /* second, figure out the mode.
196 * Some options force the mode. Others
197 * set the mode if it isn't already
203 shortopt
= short_bitmap_options
;
211 case ReAdd
: /* re-add */
214 shortopt
= short_bitmap_options
;
218 case 'A': newmode
= ASSEMBLE
;
219 shortopt
= short_bitmap_auto_options
;
221 case 'B': newmode
= BUILD
;
222 shortopt
= short_bitmap_auto_options
;
224 case 'C': newmode
= CREATE
;
225 shortopt
= short_bitmap_auto_options
;
227 case 'F': newmode
= MONITOR
;
229 case 'G': newmode
= GROW
;
230 shortopt
= short_bitmap_options
;
232 case 'I': newmode
= INCREMENTAL
;
233 shortopt
= short_bitmap_auto_options
;
236 newmode
= AUTODETECT
;
267 if (mode
&& newmode
== mode
) {
268 /* everybody happy ! */
269 } else if (mode
&& newmode
!= mode
) {
272 if (option_index
>= 0)
273 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
275 fprintf(stderr
, "-%c", opt
);
276 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
277 map_num(modes
, newmode
),
278 map_num(modes
, mode
));
280 } else if (!mode
&& newmode
) {
282 if (mode
== MISC
&& devs_found
) {
283 pr_err("No action given for %s in --misc mode\n",
285 fprintf(stderr
," Action options must come before device names\n");
289 /* special case of -c --help */
290 if ((opt
== 'c' || opt
== ConfigFile
) &&
291 ( strncmp(optarg
, "--h", 3)==0 ||
292 strncmp(optarg
, "-h", 2)==0)) {
293 fputs(Help_config
, stdout
);
297 /* If first option is a device, don't force the mode yet */
299 if (devs_found
== 0) {
300 dv
= xmalloc(sizeof(*dv
));
301 dv
->devname
= optarg
;
302 dv
->disposition
= devmode
;
303 dv
->writemostly
= writemostly
;
307 devlistend
= &dv
->next
;
312 /* No mode yet, and this is the second device ... */
313 pr_err("An option must be given to set the mode before a second device\n"
314 " (%s) is listed\n", optarg
);
317 if (option_index
>= 0)
318 pr_err("--%s", long_options
[option_index
].name
);
321 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
325 /* if we just set the mode, then done */
339 /* an undecorated option - must be a device name.
341 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
342 pr_err("Must give one of -a/-r/-f"
343 " for subsequent devices at %s\n", optarg
);
346 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
347 pr_err("Must give -a/--add for"
348 " devices to add: %s\n", optarg
);
351 dv
= xmalloc(sizeof(*dv
));
352 dv
->devname
= optarg
;
353 dv
->disposition
= devmode
;
354 dv
->writemostly
= writemostly
;
358 devlistend
= &dv
->next
;
364 /* We've got a mode, and opt is now something else which
365 * could depend on the mode */
366 #define O(a,b) ((a<<16)|b)
367 switch (O(mode
,opt
)) {
369 case O(GROW
,ChunkSize
):
371 case O(CREATE
,ChunkSize
):
372 case O(BUILD
,'c'): /* chunk or rounding */
373 case O(BUILD
,ChunkSize
): /* chunk or rounding */
375 pr_err("chunk/rounding may only be specified once. "
376 "Second value is %s.\n", optarg
);
379 chunk
= parse_size(optarg
);
380 if (chunk
< 8 || (chunk
&1)) {
381 pr_err("invalid chunk/rounding value: %s\n",
385 /* Convert sectors to K */
389 case O(INCREMENTAL
, 'e'):
391 case O(ASSEMBLE
,'e'):
392 case O(MISC
,'e'): /* set metadata (superblock) information */
394 pr_err("metadata information already given\n");
397 for(i
=0; !ss
&& superlist
[i
]; i
++)
398 ss
= superlist
[i
]->match_metadata_desc(optarg
);
401 pr_err("unrecognised metadata identifier: %s\n", optarg
);
407 case O(MANAGE
,WriteMostly
):
409 case O(BUILD
,WriteMostly
):
411 case O(CREATE
,WriteMostly
):
412 /* set write-mostly for following devices */
417 /* clear write-mostly for following devices */
424 case O(BUILD
,'z'): /* size */
426 pr_err("size may only be specified once. "
427 "Second value is %s.\n", optarg
);
430 if (strcmp(optarg
, "max")==0)
433 size
= parse_size(optarg
);
435 pr_err("invalid size: %s\n",
439 /* convert sectors to K */
444 case O(GROW
,'Z'): /* array size */
445 if (array_size
>= 0) {
446 pr_err("array-size may only be specified once. "
447 "Second value is %s.\n", optarg
);
450 if (strcmp(optarg
, "max") == 0)
453 array_size
= parse_size(optarg
);
454 if (array_size
<= 0) {
455 pr_err("invalid array size: %s\n",
464 case O(BUILD
,'l'): /* set raid level*/
465 if (level
!= UnSet
) {
466 pr_err("raid level may only be set once. "
467 "Second value is %s.\n", optarg
);
470 level
= map_name(pers
, optarg
);
471 if (level
== UnSet
) {
472 pr_err("invalid raid level: %s\n",
476 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
477 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
480 pr_err("Raid level %s not permitted with --build.\n",
484 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
485 pr_err("raid level %s is incompatible with spare-devices setting.\n",
492 case O(GROW
, 'p'): /* new layout */
493 case O(GROW
, Layout
):
495 pr_err("layout may only be sent once. "
496 "Second value was %s\n", optarg
);
500 /* 'Grow' will parse the value */
503 case O(CREATE
,'p'): /* raid5 layout */
504 case O(CREATE
,Layout
):
505 case O(BUILD
,'p'): /* faulty layout */
506 case O(BUILD
,Layout
):
507 if (layout
!= UnSet
) {
508 pr_err("layout may only be sent once. "
509 "Second value was %s\n", optarg
);
514 pr_err("layout not meaningful for %s arrays.\n",
515 map_num(pers
, level
));
518 pr_err("raid level must be given before layout.\n");
522 layout
= map_name(r5layout
, optarg
);
524 pr_err("layout %s not understood for raid5.\n",
530 layout
= map_name(r6layout
, optarg
);
532 pr_err("layout %s not understood for raid6.\n",
539 layout
= parse_layout_10(optarg
);
541 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
549 layout
= parse_layout_faulty(optarg
);
551 pr_err("layout %s not understood for faulty.\n",
559 case O(CREATE
,AssumeClean
):
560 case O(BUILD
,AssumeClean
): /* assume clean */
561 case O(GROW
,AssumeClean
):
567 case O(BUILD
,'n'): /* number of raid disks */
569 pr_err("raid-devices set twice: %d and %s\n",
573 raiddisks
= parse_num(optarg
);
574 if (raiddisks
<= 0) {
575 pr_err("invalid number of raid devices: %s\n",
579 ident
.raid_disks
= raiddisks
;
582 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
584 pr_err("spare-devices set twice: %d and %s\n",
588 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
589 pr_err("spare-devices setting is incompatible with raid level %d\n",
593 sparedisks
= parse_num(optarg
);
594 if (sparedisks
< 0) {
595 pr_err("invalid number of spare-devices: %s\n",
605 case O(INCREMENTAL
,'a'):
606 case O(INCREMENTAL
,Auto
):
607 case O(ASSEMBLE
,'a'):
608 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
609 c
.autof
= parse_auto(optarg
, "--auto flag", 0);
612 case O(CREATE
,Symlinks
):
613 case O(BUILD
,Symlinks
):
614 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
618 case O(BUILD
,'f'): /* force honouring '-n 1' */
619 case O(BUILD
,Force
): /* force honouring '-n 1' */
620 case O(GROW
,'f'): /* ditto */
621 case O(GROW
,Force
): /* ditto */
622 case O(CREATE
,'f'): /* force honouring of device list */
623 case O(CREATE
,Force
): /* force honouring of device list */
624 case O(ASSEMBLE
,'f'): /* force assembly */
625 case O(ASSEMBLE
,Force
): /* force assembly */
626 case O(MISC
,'f'): /* force zero */
627 case O(MISC
,Force
): /* force zero */
628 case O(MANAGE
,Force
): /* add device which is too large */
631 /* now for the Assemble options */
632 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
634 case O(INCREMENTAL
, FreezeReshape
):
635 c
.freeze_reshape
= 1;
637 case O(CREATE
,'u'): /* uuid of array */
638 case O(ASSEMBLE
,'u'): /* uuid of array */
639 if (ident
.uuid_set
) {
640 pr_err("uuid cannot be set twice. "
641 "Second value %s.\n", optarg
);
644 if (parse_uuid(optarg
, ident
.uuid
))
647 pr_err("Bad uuid: %s\n", optarg
);
653 case O(ASSEMBLE
,'N'):
656 pr_err("name cannot be set twice. "
657 "Second value %s.\n", optarg
);
660 if (mode
== MISC
&& !c
.subarray
) {
661 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
664 if (strlen(optarg
) > 32) {
665 pr_err("name '%s' is too long, 32 chars max.\n",
669 strcpy(ident
.name
, optarg
);
672 case O(ASSEMBLE
,'m'): /* super-minor for array */
673 case O(ASSEMBLE
,SuperMinor
):
674 if (ident
.super_minor
!= UnSet
) {
675 pr_err("super-minor cannot be set twice. "
676 "Second value: %s.\n", optarg
);
679 if (strcmp(optarg
, "dev")==0)
680 ident
.super_minor
= -2;
682 ident
.super_minor
= parse_num(optarg
);
683 if (ident
.super_minor
< 0) {
684 pr_err("Bad super-minor number: %s.\n", optarg
);
690 case O(ASSEMBLE
,'o'):
696 case O(ASSEMBLE
,'U'): /* update the superblock */
699 pr_err("Can only update one aspect"
700 " of superblock, both %s and %s given.\n",
704 if (mode
== MISC
&& !c
.subarray
) {
705 pr_err("Only subarrays can be"
706 " updated in misc mode\n");
710 if (strcmp(c
.update
, "sparc2.2")==0)
712 if (strcmp(c
.update
, "super-minor") == 0)
714 if (strcmp(c
.update
, "summaries")==0)
716 if (strcmp(c
.update
, "resync")==0)
718 if (strcmp(c
.update
, "uuid")==0)
720 if (strcmp(c
.update
, "name")==0)
722 if (strcmp(c
.update
, "homehost")==0)
724 if (strcmp(c
.update
, "devicesize")==0)
726 if (strcmp(c
.update
, "no-bitmap")==0)
728 if (strcmp(c
.update
, "byteorder")==0) {
730 pr_err("must not set metadata"
731 " type with --update=byteorder.\n");
734 for(i
=0; !ss
&& superlist
[i
]; i
++)
735 ss
= superlist
[i
]->match_metadata_desc(
738 pr_err("INTERNAL ERROR"
739 " cannot find 0.swap\n");
745 if (strcmp(c
.update
,"?") == 0 ||
746 strcmp(c
.update
, "help") == 0) {
748 fprintf(outf
, Name
": ");
752 Name
": '--update=%s' is invalid. ",
755 fprintf(outf
, "Valid --update options are:\n"
756 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
757 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
759 exit(outf
== stdout
? 0 : 2);
762 /* update=devicesize is allowed with --re-add */
763 if (devmode
!= 'A') {
764 pr_err("--update in Manage mode only"
765 " allowed with --re-add.\n");
769 pr_err("Can only update one aspect"
770 " of superblock, both %s and %s given.\n",
775 if (strcmp(c
.update
, "devicesize") != 0) {
776 pr_err("only 'devicesize' can be"
777 " updated with --re-add\n");
782 case O(INCREMENTAL
,NoDegraded
):
783 pr_err("--no-degraded is deprecated in Incremental mode\n");
784 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
785 c
.runstop
= -1; /* --stop isn't allowed for --assemble,
786 * so we overload slightly */
789 case O(ASSEMBLE
,'c'):
790 case O(ASSEMBLE
,ConfigFile
):
791 case O(INCREMENTAL
, 'c'):
792 case O(INCREMENTAL
, ConfigFile
):
794 case O(MISC
, ConfigFile
):
796 case O(MONITOR
,ConfigFile
):
798 pr_err("configfile cannot be set twice. "
799 "Second value is %s.\n", optarg
);
803 set_conffile(configfile
);
804 /* FIXME possibly check that config file exists. Even parse it */
806 case O(ASSEMBLE
,'s'): /* scan */
809 case O(INCREMENTAL
,'s'):
813 case O(MONITOR
,'m'): /* mail address */
814 case O(MONITOR
,EMail
):
816 pr_err("only specify one mailaddress. %s ignored.\n",
822 case O(MONITOR
,'p'): /* alert program */
823 case O(MONITOR
,ProgramOpt
): /* alert program */
825 pr_err("only specify one alter program. %s ignored.\n",
831 case O(MONITOR
,'r'): /* rebuild increments */
832 case O(MONITOR
,Increment
):
833 increments
= atoi(optarg
);
834 if (increments
> 99 || increments
< 1) {
835 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
840 case O(MONITOR
,'d'): /* delay in seconds */
842 case O(BUILD
,'d'): /* delay for bitmap updates */
845 pr_err("only specify delay once. %s ignored.\n",
848 c
.delay
= parse_num(optarg
);
850 pr_err("invalid delay: %s\n",
856 case O(MONITOR
,'f'): /* daemonise */
857 case O(MONITOR
,Fork
):
860 case O(MONITOR
,'i'): /* pid */
862 pr_err("only specify one pid file. %s ignored.\n",
867 case O(MONITOR
,'1'): /* oneshot */
871 case O(MONITOR
,'t'): /* test */
874 case O(MONITOR
,'y'): /* log messages to syslog */
875 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
878 case O(MONITOR
, NoSharing
):
882 /* now the general management options. Some are applicable
883 * to other modes. None have arguments.
888 case O(MANAGE
,Add
): /* add a drive */
891 case O(MANAGE
,ReAdd
):
894 case O(MANAGE
,'r'): /* remove a drive */
895 case O(MANAGE
,Remove
):
898 case O(MANAGE
,'f'): /* set faulty */
900 case O(INCREMENTAL
,'f'):
901 case O(INCREMENTAL
,Remove
):
902 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
903 * even though we will both fail and
904 * remove the device */
907 case O(INCREMENTAL
,'R'):
909 case O(ASSEMBLE
,'R'):
911 case O(CREATE
,'R'): /* Run the array */
913 pr_err("Cannot both Stop and Run an array\n");
920 pr_err("Cannot both Run and Stop an array\n");
932 case O(MISC
,KillOpt
):
939 case O(MISC
, WaitOpt
):
940 case O(MISC
, Waitclean
):
941 case O(MISC
, DetailPlatform
):
942 case O(MISC
, KillSubarray
):
943 case O(MISC
, UpdateSubarray
):
944 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
946 pr_err("subarray can only"
947 " be specified once\n");
952 if (devmode
&& devmode
!= opt
&&
953 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
954 pr_err("--examine/-E cannot be given with ");
955 if (devmode
== 'E') {
956 if (option_index
>= 0)
957 fprintf(stderr
, "--%s\n",
958 long_options
[option_index
].name
);
960 fprintf(stderr
, "-%c\n", opt
);
961 } else if (isalpha(devmode
))
962 fprintf(stderr
, "-%c\n", devmode
);
964 fprintf(stderr
, "previous option\n");
969 case O(MISC
, UdevRules
):
970 if (devmode
&& devmode
!= opt
) {
971 pr_err("--udev-rules must"
972 " be the only option.\n");
975 pr_err("only specify one udev "
976 "rule filename. %s ignored.\n",
979 udev_filename
= optarg
;
987 case O(MISC
, Sparc22
):
988 if (devmode
!= 'E') {
989 pr_err("--sparc2.2 only allowed with --examine\n");
995 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
996 case O(ASSEMBLE
,Bitmap
):
998 pr_err("bitmap file needed with -b in --assemble mode\n");
1001 if (strcmp(optarg
, "internal")==0) {
1002 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1005 bitmap_fd
= open(optarg
, O_RDWR
);
1006 if (!*optarg
|| bitmap_fd
< 0) {
1007 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1010 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1013 case O(ASSEMBLE
, BackupFile
):
1014 case O(GROW
, BackupFile
):
1015 /* Specify a file into which grow might place a backup,
1016 * or from which assemble might recover a backup
1018 if (c
.backup_file
) {
1019 pr_err("backup file already specified, rejecting %s\n", optarg
);
1022 c
.backup_file
= optarg
;
1025 case O(GROW
, Continue
):
1026 /* Continue interrupted grow
1030 case O(ASSEMBLE
, InvalidBackup
):
1031 /* Acknowledge that the backupfile is invalid, but ask
1032 * to continue anyway
1034 c
.invalid_backup
= 1;
1038 case O(BUILD
,Bitmap
):
1040 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1041 if (strcmp(optarg
, "none") == 0) {
1042 pr_err("'--bitmap none' only"
1043 " supported for --grow\n");
1048 case O(GROW
,Bitmap
):
1049 if (strcmp(optarg
, "internal")== 0 ||
1050 strcmp(optarg
, "none")== 0 ||
1051 strchr(optarg
, '/') != NULL
) {
1052 bitmap_file
= optarg
;
1056 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1057 " not '%s'\n", optarg
);
1060 case O(GROW
,BitmapChunk
):
1061 case O(BUILD
,BitmapChunk
):
1062 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1063 bitmap_chunk
= parse_size(optarg
);
1064 if (bitmap_chunk
<= 0 ||
1065 bitmap_chunk
& (bitmap_chunk
- 1)) {
1066 pr_err("invalid bitmap chunksize: %s\n",
1070 bitmap_chunk
= bitmap_chunk
* 512;
1073 case O(GROW
, WriteBehind
):
1074 case O(BUILD
, WriteBehind
):
1075 case O(CREATE
, WriteBehind
): /* write-behind mode */
1076 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1078 write_behind
= parse_num(optarg
);
1079 if (write_behind
< 0 ||
1080 write_behind
> 16383) {
1081 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1087 case O(INCREMENTAL
, 'r'):
1088 case O(INCREMENTAL
, RebuildMapOpt
):
1091 case O(INCREMENTAL
, IncrementalPath
):
1092 remove_path
= optarg
;
1095 /* We have now processed all the valid options. Anything else is
1098 if (option_index
> 0)
1099 pr_err(":option --%s not valid in %s mode\n",
1100 long_options
[option_index
].name
,
1101 map_num(modes
, mode
));
1103 pr_err("option -%c not valid in %s mode\n",
1104 opt
, map_num(modes
, mode
));
1111 if (print_help
== 2)
1112 help_text
= OptionHelp
;
1114 help_text
= mode_help
[mode
];
1115 if (help_text
== NULL
)
1117 fputs(help_text
,stdout
);
1121 if (!mode
&& devs_found
) {
1124 if (devlist
->disposition
== 0)
1125 devlist
->disposition
= devmode
;
1128 fputs(Usage
, stderr
);
1133 struct createinfo
*ci
= conf_get_create_info();
1135 if (strcasecmp(symlinks
, "yes") == 0)
1137 else if (strcasecmp(symlinks
, "no") == 0)
1140 pr_err("option --symlinks must be 'no' or 'yes'\n");
1144 /* Ok, got the option parsing out of the way
1145 * hopefully it's mostly right but there might be some stuff
1148 * That is mosty checked in the per-mode stuff but...
1150 * For @,B,C and A without -s, the first device listed must be
1151 * an md device. We check that here and open it.
1154 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1156 || (mode
== ASSEMBLE
&& ! c
.scan
)) {
1157 if (devs_found
< 1) {
1158 pr_err("an md device must be given in this mode\n");
1161 if ((int)ident
.super_minor
== -2 && c
.autof
) {
1162 pr_err("--super-minor=dev is incompatible with --auto\n");
1165 if (mode
== MANAGE
|| mode
== GROW
) {
1166 mdfd
= open_mddev(devlist
->devname
, 1);
1170 /* non-existent device is OK */
1171 mdfd
= open_mddev(devlist
->devname
, 0);
1173 pr_err("device %s exists but is not an "
1174 "md array.\n", devlist
->devname
);
1177 if ((int)ident
.super_minor
== -2) {
1180 pr_err("--super-minor=dev given, and "
1181 "listed device %s doesn't exist.\n",
1186 ident
.super_minor
= minor(stb
.st_rdev
);
1188 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1189 /* We don't really want this open yet, we just might
1190 * have wanted to check some things
1198 if (raiddisks
== 1 && !c
.force
&& level
!= LEVEL_FAULTY
) {
1199 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1200 " a mistake. If you really mean it you will need to specify --force before\n"
1201 " setting the number of drives.\n");
1206 if (c
.homehost
== NULL
)
1207 c
.homehost
= conf_get_homehost(&c
.require_homehost
);
1208 if (c
.homehost
== NULL
|| strcasecmp(c
.homehost
, "<system>")==0) {
1209 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1210 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1211 c
.homehost
= sys_hostname
;
1214 if (c
.homehost
&& (!c
.homehost
[0] || strcasecmp(c
.homehost
, "<none>") == 0)) {
1216 c
.require_homehost
= 0;
1219 if ((mode
== MISC
&& devmode
== 'E')
1220 || (mode
== MONITOR
&& spare_sharing
== 0))
1221 /* Anyone may try this */;
1222 else if (geteuid() != 0) {
1223 pr_err("must be super-user to perform this action\n");
1227 ident
.autof
= c
.autof
;
1232 /* readonly, add/remove, readwrite, runstop */
1234 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1235 if (!rv
&& devs_found
>1)
1236 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1237 devlist
->next
, c
.verbose
-c
.quiet
, c
.test
,
1239 if (!rv
&& c
.readonly
< 0)
1240 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1241 if (!rv
&& c
.runstop
)
1242 rv
= Manage_runstop(devlist
->devname
, mdfd
, c
.runstop
, c
.verbose
-c
.quiet
, 0);
1245 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1246 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !c
.scan
) {
1247 /* Only a device has been given, so get details from config file */
1248 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1249 if (array_ident
== NULL
) {
1250 pr_err("%s not identified in config file.\n",
1256 if (array_ident
->autof
== 0)
1257 array_ident
->autof
= c
.autof
;
1258 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1259 NULL
, c
.backup_file
, c
.invalid_backup
,
1260 c
.readonly
, c
.runstop
, c
.update
,
1261 c
.homehost
, c
.require_homehost
,
1262 c
.verbose
-c
.quiet
, c
.force
,
1266 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1267 devlist
->next
, c
.backup_file
, c
.invalid_backup
,
1268 c
.readonly
, c
.runstop
, c
.update
,
1269 c
.homehost
, c
.require_homehost
,
1270 c
.verbose
-c
.quiet
, c
.force
,
1272 else if (devs_found
> 0) {
1273 if (c
.update
&& devs_found
> 1) {
1274 pr_err("can only update a single array at a time\n");
1277 if (c
.backup_file
&& devs_found
> 1) {
1278 pr_err("can only assemble a single array when providing a backup file.\n");
1281 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1282 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1283 if (array_ident
== NULL
) {
1284 pr_err("%s not identified in config file.\n",
1289 if (array_ident
->autof
== 0)
1290 array_ident
->autof
= c
.autof
;
1291 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1292 NULL
, c
.backup_file
, c
.invalid_backup
,
1293 c
.readonly
, c
.runstop
, c
.update
,
1294 c
.homehost
, c
.require_homehost
,
1295 c
.verbose
-c
.quiet
, c
.force
,
1300 pr_err("--update not meaningful with a --scan assembly.\n");
1303 if (c
.backup_file
) {
1304 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1307 rv
= scan_assemble(c
.autof
, ss
, c
.readonly
, c
.runstop
,
1310 c
.verbose
- c
.quiet
,
1311 c
.force
, c
.freeze_reshape
);
1317 c
.delay
= DEFAULT_BITMAP_DELAY
;
1318 if (write_behind
&& !bitmap_file
) {
1319 pr_err("write-behind mode requires a bitmap.\n");
1323 if (raiddisks
== 0) {
1324 pr_err("no raid-devices specified.\n");
1330 if (strcmp(bitmap_file
, "internal")==0) {
1331 pr_err("'internal' bitmaps not supported with --build\n");
1336 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1337 raiddisks
, devlist
->next
, assume_clean
,
1338 bitmap_file
, bitmap_chunk
, write_behind
,
1339 c
.delay
, c
.verbose
-c
.quiet
, c
.autof
, size
);
1343 c
.delay
= DEFAULT_BITMAP_DELAY
;
1344 if (write_behind
&& !bitmap_file
) {
1345 pr_err("write-behind mode requires a bitmap.\n");
1349 if (raiddisks
== 0) {
1350 pr_err("no raid-devices specified.\n");
1355 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1356 raiddisks
, sparedisks
, ident
.name
, c
.homehost
,
1357 ident
.uuid_set
? ident
.uuid
: NULL
,
1358 devs_found
-1, devlist
->next
, c
.runstop
,
1359 c
.readonly
, c
.verbose
-c
.quiet
, c
.force
, assume_clean
,
1360 bitmap_file
, bitmap_chunk
, write_behind
, c
.delay
, c
.autof
);
1363 if (devmode
== 'E') {
1364 if (devlist
== NULL
&& !c
.scan
) {
1365 pr_err("No devices to examine\n");
1368 if (devlist
== NULL
)
1369 devlist
= conf_get_devs();
1370 if (devlist
== NULL
) {
1371 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1374 if (c
.brief
&& c
.verbose
> 0)
1376 rv
= Examine(devlist
, c
.scan
?(c
.verbose
>1?0:c
.verbose
):c
.brief
,
1378 c
.SparcAdjust
, ss
, c
.homehost
);
1379 } else if (devmode
== DetailPlatform
) {
1380 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? c
.scan
: 1, c
.verbose
-c
.quiet
);
1381 } else if (devlist
== NULL
) {
1382 if (devmode
== 'S' && c
.scan
)
1383 rv
= stop_scan(c
.verbose
-c
.quiet
);
1384 else if ((devmode
== 'D' || devmode
== Waitclean
) && c
.scan
)
1385 rv
= misc_scan(devmode
, c
.verbose
-c
.quiet
, c
.export
,
1386 c
.test
, c
.homehost
, c
.prefer
);
1387 else if (devmode
== UdevRules
)
1388 rv
= Write_rules(udev_filename
);
1390 pr_err("No devices given.\n");
1394 rv
= misc_list(devlist
, c
.brief
, c
.verbose
-c
.quiet
, c
.export
, c
.test
,
1395 c
.homehost
, c
.prefer
, c
.subarray
, c
.update
,
1400 if (!devlist
&& !c
.scan
) {
1401 pr_err("Cannot monitor: need --scan or at least one device\n");
1405 if (pidfile
&& !daemonise
) {
1406 pr_err("Cannot write a pid file when not in daemon mode\n");
1411 if (get_linux_version() > 2006016)
1412 /* mdstat responds to poll */
1417 rv
= Monitor(devlist
, mailaddr
, program
,
1418 c
.delay
?c
.delay
:60, daemonise
, c
.scan
, oneshot
,
1419 dosyslog
, c
.test
, pidfile
, increments
,
1420 spare_sharing
, c
.prefer
);
1424 if (array_size
>= 0) {
1425 /* alway impose array size first, independent of
1427 * Do not allow level or raid_disks changes at the
1428 * same time as that can be irreversibly destructive.
1432 if (raiddisks
|| level
!= UnSet
) {
1433 pr_err("cannot change array size in same operation "
1434 "as changing raiddisks or level.\n"
1435 " Change size first, then check that data is still intact.\n");
1439 sysfs_init(&sra
, mdfd
, 0);
1440 if (array_size
== 0)
1441 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1443 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1446 pr_err("--array-size setting"
1447 " is too large.\n");
1449 pr_err("current kernel does"
1450 " not support setting --array-size\n");
1455 if (devs_found
> 1 && raiddisks
== 0) {
1457 if (size
>= 0 || chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1458 pr_err("--add cannot be used with "
1459 "other geometry changes in --grow mode\n");
1463 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1464 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1469 } else if (bitmap_file
) {
1470 if (size
>= 0 || raiddisks
|| chunk
||
1471 layout_str
!= NULL
|| devs_found
> 1) {
1472 pr_err("--bitmap changes cannot be "
1473 "used with other geometry changes "
1474 "in --grow mode\n");
1479 c
.delay
= DEFAULT_BITMAP_DELAY
;
1480 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1481 bitmap_chunk
, c
.delay
, write_behind
, c
.force
);
1482 } else if (grow_continue
)
1483 rv
= Grow_continue_command(devlist
->devname
,
1484 mdfd
, c
.backup_file
,
1486 else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1487 || chunk
!= 0 || level
!= UnSet
) {
1488 rv
= Grow_reshape(devlist
->devname
, mdfd
, c
.verbose
-c
.quiet
, c
.backup_file
,
1489 size
, level
, layout_str
, chunk
, raiddisks
,
1491 assume_clean
, c
.force
);
1492 } else if (array_size
< 0)
1493 pr_err("no changes to --grow\n");
1500 if (c
.runstop
<= 0) {
1501 pr_err("--incremental --scan meaningless without --run.\n");
1504 if (devmode
== 'f') {
1505 pr_err("--incremental --scan --fail not supported.\n");
1508 rv
= IncrementalScan(c
.verbose
-c
.quiet
);
1511 if (!rebuild_map
&& !c
.scan
) {
1512 pr_err("--incremental requires a device.\n");
1517 if (devlist
->next
) {
1518 pr_err("--incremental can only handle one device.\n");
1523 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1526 rv
= Incremental(devlist
->devname
, c
.verbose
-c
.quiet
,
1527 c
.runstop
, ss
, c
.homehost
,
1528 c
.require_homehost
, c
.autof
,
1538 static int scan_assemble(int autof
, struct supertype
*ss
,
1539 int readonly
, int runstop
,
1540 struct mddev_ident
*ident
,
1541 char *homehost
, int require_homehost
,
1542 int verbose
, int force
,
1545 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1546 struct mddev_dev
*devlist
= conf_get_devs();
1547 struct map_ent
*map
= NULL
;
1550 int failures
, successes
;
1552 if (conf_verify_devnames(array_list
)) {
1553 pr_err("Duplicate MD device names in "
1554 "conf file were found.\n");
1557 if (devlist
== NULL
) {
1558 pr_err("No devices listed in conf file were found.\n");
1561 for (a
= array_list
; a
; a
= a
->next
) {
1567 pr_err("%s: failed to get "
1568 "exclusive lock on mapfile\n",
1574 for (a
= array_list
; a
; a
= a
->next
) {
1579 strcasecmp(a
->devname
, "<ignore>") == 0)
1582 r
= Assemble(ss
, a
->devname
,
1585 readonly
, runstop
, NULL
,
1586 homehost
, require_homehost
,
1597 } while (failures
&& successes
);
1598 if (homehost
&& cnt
== 0) {
1599 /* Maybe we can auto-assemble something.
1600 * Repeatedly call Assemble in auto-assemble mode
1605 ident
->autof
= autof
;
1607 struct mddev_dev
*devlist
= conf_get_devs();
1610 rv2
= Assemble(ss
, NULL
,
1625 /* Incase there are stacked devices, we need to go around again */
1627 if (cnt
== 0 && rv
== 0) {
1628 pr_err("No arrays found in config file or automatically\n");
1632 } else if (cnt
== 0 && rv
== 0) {
1633 pr_err("No arrays found in config file\n");
1640 static int misc_scan(char devmode
, int verbose
, int export
, int test
,
1641 char *homehost
, char *prefer
)
1643 /* apply --detail or --wait-clean to
1644 * all devices in /proc/mdstat
1646 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1647 struct mdstat_ent
*e
;
1648 struct map_ent
*map
= NULL
;
1650 int v
= verbose
>1?0:verbose
+1;
1653 for (members
= 0; members
<= 1; members
++) {
1654 for (e
=ms
; e
; e
=e
->next
) {
1657 int member
= e
->metadata_version
&&
1658 strncmp(e
->metadata_version
,
1659 "external:/", 10) == 0;
1660 if (members
!= member
)
1662 me
= map_by_devnum(&map
, e
->devnum
);
1664 && strcmp(me
->path
, "/unknown") != 0)
1667 name
= get_md_name(e
->devnum
);
1670 pr_err("cannot find device file for %s\n",
1675 rv
|= Detail(name
, v
,
1679 rv
|= WaitClean(name
, -1, v
);
1687 static int stop_scan(int verbose
)
1689 /* apply --stop to all devices in /proc/mdstat */
1690 /* Due to possible stacking of devices, repeat until
1691 * nothing more can be stopped
1693 int progress
=1, err
;
1697 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1698 struct mdstat_ent
*e
;
1700 if (!progress
) last
= 1;
1701 progress
= 0; err
= 0;
1702 for (e
=ms
; e
; e
=e
->next
) {
1703 char *name
= get_md_name(e
->devnum
);
1707 pr_err("cannot find device file for %s\n",
1711 mdfd
= open_mddev(name
, 1);
1713 if (Manage_runstop(name
, mdfd
, -1, verbose
, !last
))
1723 } while (!last
&& err
);
1729 static int misc_list(struct mddev_dev
*devlist
,
1730 int brief
, int verbose
, int export
, int test
,
1731 char *homehost
, char *prefer
, char *subarray
,
1732 char *update
, struct mddev_ident
*ident
,
1733 struct supertype
*ss
, int force
)
1735 struct mddev_dev
*dv
;
1738 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1741 switch(dv
->disposition
) {
1743 rv
|= Detail(dv
->devname
,
1745 export
, test
, homehost
, prefer
);
1747 case KillOpt
: /* Zero superblock */
1749 rv
|= Kill(dv
->devname
, ss
, force
, verbose
,0);
1753 rv
|= Kill(dv
->devname
, NULL
, force
, v
, 0);
1760 rv
|= Query(dv
->devname
); continue;
1762 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1765 rv
|= Wait(dv
->devname
); continue;
1767 rv
|= WaitClean(dv
->devname
, -1, verbose
); continue;
1769 rv
|= Kill_subarray(dv
->devname
, subarray
, verbose
);
1771 case UpdateSubarray
:
1772 if (update
== NULL
) {
1773 pr_err("-U/--update must be specified with --update-subarray\n");
1777 rv
|= Update_subarray(dv
->devname
, subarray
,
1778 update
, ident
, verbose
);
1781 mdfd
= open_mddev(dv
->devname
, 1);
1783 switch(dv
->disposition
) {
1785 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, verbose
, 0); break;
1787 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, verbose
, 0); break;
1789 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1791 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;