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(struct supertype
*ss
,
35 struct mddev_ident
*ident
);
36 static int misc_scan(char devmode
, int verbose
, int export
, int test
,
37 char *homehost
, char *prefer
);
38 static int stop_scan(int verbose
);
39 static int misc_list(struct mddev_dev
*devlist
,
40 struct mddev_ident
*ident
,
41 struct supertype
*ss
, struct context
*c
);
44 int main(int argc
, char *argv
[])
54 long long array_size
= -1;
57 char *layout_str
= NULL
;
60 struct mddev_ident ident
;
61 char *configfile
= NULL
;
65 char *bitmap_file
= NULL
;
66 int bitmap_chunk
= UnSet
;
67 struct mddev_dev
*devlist
= NULL
;
68 struct mddev_dev
**devlistend
= & devlist
;
72 char *symlinks
= NULL
;
73 int grow_continue
= 0;
74 /* autof indicates whether and how to create device node.
75 * bottom 3 bits are style. Rest (when shifted) are number of parts
77 * 1 - don't create (no)
78 * 2 - if is_standard, then create (yes)
79 * 3 - create as 'md' - reject is_standard mdp (md)
80 * 4 - create as 'mdp' - reject is_standard md (mdp)
81 * 5 - default to md if not is_standard (md in config file)
82 * 6 - default to mdp if not is_standard (part, or mdp in config file)
85 .require_homehost
= 1,
88 char sys_hostname
[256];
89 char *mailaddr
= NULL
;
95 int spare_sharing
= 1;
96 struct supertype
*ss
= NULL
;
98 char *shortopt
= short_options
;
101 char *remove_path
= NULL
;
102 char *udev_filename
= NULL
;
109 srandom(time(0) ^ getpid());
113 ident
.raid_disks
= UnSet
;
114 ident
.super_minor
= UnSet
;
116 ident
.spare_group
= NULL
;
119 ident
.bitmap_fd
= -1;
120 ident
.bitmap_file
= NULL
;
122 ident
.container
= NULL
;
125 while ((option_index
= -1) ,
126 (opt
=getopt_long(argc
, argv
,
127 shortopt
, long_options
,
128 &option_index
)) != -1) {
130 /* firstly, some mode-independent options */
140 fputs(Version
, stderr
);
143 case 'v': c
.verbose
++;
146 case 'q': c
.verbose
--;
150 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
151 || mode
== GROW
|| mode
== INCREMENTAL
153 break; /* b means bitmap */
158 case 'Y': c
.export
++;
162 if (strcasecmp(optarg
, "<ignore>") == 0)
163 c
.require_homehost
= 0;
169 * --offroot sets first char of argv[0] to @. This is used
170 * by systemd to signal that the task was launched from
171 * initrd/initramfs and should be preserved during shutdown
181 if (asprintf(&c
.prefer
, "/%s/", optarg
) <= 0)
187 fputs(Usage
, stderr
);
190 /* second, figure out the mode.
191 * Some options force the mode. Others
192 * set the mode if it isn't already
198 shortopt
= short_bitmap_options
;
206 case ReAdd
: /* re-add */
209 shortopt
= short_bitmap_options
;
213 case 'A': newmode
= ASSEMBLE
;
214 shortopt
= short_bitmap_auto_options
;
216 case 'B': newmode
= BUILD
;
217 shortopt
= short_bitmap_auto_options
;
219 case 'C': newmode
= CREATE
;
220 shortopt
= short_bitmap_auto_options
;
222 case 'F': newmode
= MONITOR
;
224 case 'G': newmode
= GROW
;
225 shortopt
= short_bitmap_options
;
227 case 'I': newmode
= INCREMENTAL
;
228 shortopt
= short_bitmap_auto_options
;
231 newmode
= AUTODETECT
;
262 if (mode
&& newmode
== mode
) {
263 /* everybody happy ! */
264 } else if (mode
&& newmode
!= mode
) {
267 if (option_index
>= 0)
268 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
270 fprintf(stderr
, "-%c", opt
);
271 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
272 map_num(modes
, newmode
),
273 map_num(modes
, mode
));
275 } else if (!mode
&& newmode
) {
277 if (mode
== MISC
&& devs_found
) {
278 pr_err("No action given for %s in --misc mode\n",
280 fprintf(stderr
," Action options must come before device names\n");
284 /* special case of -c --help */
285 if ((opt
== 'c' || opt
== ConfigFile
) &&
286 ( strncmp(optarg
, "--h", 3)==0 ||
287 strncmp(optarg
, "-h", 2)==0)) {
288 fputs(Help_config
, stdout
);
292 /* If first option is a device, don't force the mode yet */
294 if (devs_found
== 0) {
295 dv
= xmalloc(sizeof(*dv
));
296 dv
->devname
= optarg
;
297 dv
->disposition
= devmode
;
298 dv
->writemostly
= writemostly
;
302 devlistend
= &dv
->next
;
307 /* No mode yet, and this is the second device ... */
308 pr_err("An option must be given to set the mode before a second device\n"
309 " (%s) is listed\n", optarg
);
312 if (option_index
>= 0)
313 pr_err("--%s", long_options
[option_index
].name
);
316 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
320 /* if we just set the mode, then done */
334 /* an undecorated option - must be a device name.
336 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
337 pr_err("Must give one of -a/-r/-f"
338 " for subsequent devices at %s\n", optarg
);
341 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
342 pr_err("Must give -a/--add for"
343 " devices to add: %s\n", optarg
);
346 dv
= xmalloc(sizeof(*dv
));
347 dv
->devname
= optarg
;
348 dv
->disposition
= devmode
;
349 dv
->writemostly
= writemostly
;
353 devlistend
= &dv
->next
;
359 /* We've got a mode, and opt is now something else which
360 * could depend on the mode */
361 #define O(a,b) ((a<<16)|b)
362 switch (O(mode
,opt
)) {
364 case O(GROW
,ChunkSize
):
366 case O(CREATE
,ChunkSize
):
367 case O(BUILD
,'c'): /* chunk or rounding */
368 case O(BUILD
,ChunkSize
): /* chunk or rounding */
370 pr_err("chunk/rounding may only be specified once. "
371 "Second value is %s.\n", optarg
);
374 chunk
= parse_size(optarg
);
375 if (chunk
< 8 || (chunk
&1)) {
376 pr_err("invalid chunk/rounding value: %s\n",
380 /* Convert sectors to K */
384 case O(INCREMENTAL
, 'e'):
386 case O(ASSEMBLE
,'e'):
387 case O(MISC
,'e'): /* set metadata (superblock) information */
389 pr_err("metadata information already given\n");
392 for(i
=0; !ss
&& superlist
[i
]; i
++)
393 ss
= superlist
[i
]->match_metadata_desc(optarg
);
396 pr_err("unrecognised metadata identifier: %s\n", optarg
);
402 case O(MANAGE
,WriteMostly
):
404 case O(BUILD
,WriteMostly
):
406 case O(CREATE
,WriteMostly
):
407 /* set write-mostly for following devices */
412 /* clear write-mostly for following devices */
419 case O(BUILD
,'z'): /* size */
421 pr_err("size may only be specified once. "
422 "Second value is %s.\n", optarg
);
425 if (strcmp(optarg
, "max")==0)
428 size
= parse_size(optarg
);
430 pr_err("invalid size: %s\n",
434 /* convert sectors to K */
439 case O(GROW
,'Z'): /* array size */
440 if (array_size
>= 0) {
441 pr_err("array-size may only be specified once. "
442 "Second value is %s.\n", optarg
);
445 if (strcmp(optarg
, "max") == 0)
448 array_size
= parse_size(optarg
);
449 if (array_size
<= 0) {
450 pr_err("invalid array size: %s\n",
459 case O(BUILD
,'l'): /* set raid level*/
460 if (level
!= UnSet
) {
461 pr_err("raid level may only be set once. "
462 "Second value is %s.\n", optarg
);
465 level
= map_name(pers
, optarg
);
466 if (level
== UnSet
) {
467 pr_err("invalid raid level: %s\n",
471 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
472 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
475 pr_err("Raid level %s not permitted with --build.\n",
479 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
480 pr_err("raid level %s is incompatible with spare-devices setting.\n",
487 case O(GROW
, 'p'): /* new layout */
488 case O(GROW
, Layout
):
490 pr_err("layout may only be sent once. "
491 "Second value was %s\n", optarg
);
495 /* 'Grow' will parse the value */
498 case O(CREATE
,'p'): /* raid5 layout */
499 case O(CREATE
,Layout
):
500 case O(BUILD
,'p'): /* faulty layout */
501 case O(BUILD
,Layout
):
502 if (layout
!= UnSet
) {
503 pr_err("layout may only be sent once. "
504 "Second value was %s\n", optarg
);
509 pr_err("layout not meaningful for %s arrays.\n",
510 map_num(pers
, level
));
513 pr_err("raid level must be given before layout.\n");
517 layout
= map_name(r5layout
, optarg
);
519 pr_err("layout %s not understood for raid5.\n",
525 layout
= map_name(r6layout
, optarg
);
527 pr_err("layout %s not understood for raid6.\n",
534 layout
= parse_layout_10(optarg
);
536 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
544 layout
= parse_layout_faulty(optarg
);
546 pr_err("layout %s not understood for faulty.\n",
554 case O(CREATE
,AssumeClean
):
555 case O(BUILD
,AssumeClean
): /* assume clean */
556 case O(GROW
,AssumeClean
):
562 case O(BUILD
,'n'): /* number of raid disks */
564 pr_err("raid-devices set twice: %d and %s\n",
568 raiddisks
= parse_num(optarg
);
569 if (raiddisks
<= 0) {
570 pr_err("invalid number of raid devices: %s\n",
574 ident
.raid_disks
= raiddisks
;
577 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
579 pr_err("spare-devices set twice: %d and %s\n",
583 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
584 pr_err("spare-devices setting is incompatible with raid level %d\n",
588 sparedisks
= parse_num(optarg
);
589 if (sparedisks
< 0) {
590 pr_err("invalid number of spare-devices: %s\n",
600 case O(INCREMENTAL
,'a'):
601 case O(INCREMENTAL
,Auto
):
602 case O(ASSEMBLE
,'a'):
603 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
604 c
.autof
= parse_auto(optarg
, "--auto flag", 0);
607 case O(CREATE
,Symlinks
):
608 case O(BUILD
,Symlinks
):
609 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
613 case O(BUILD
,'f'): /* force honouring '-n 1' */
614 case O(BUILD
,Force
): /* force honouring '-n 1' */
615 case O(GROW
,'f'): /* ditto */
616 case O(GROW
,Force
): /* ditto */
617 case O(CREATE
,'f'): /* force honouring of device list */
618 case O(CREATE
,Force
): /* force honouring of device list */
619 case O(ASSEMBLE
,'f'): /* force assembly */
620 case O(ASSEMBLE
,Force
): /* force assembly */
621 case O(MISC
,'f'): /* force zero */
622 case O(MISC
,Force
): /* force zero */
623 case O(MANAGE
,Force
): /* add device which is too large */
626 /* now for the Assemble options */
627 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
629 case O(INCREMENTAL
, FreezeReshape
):
630 c
.freeze_reshape
= 1;
632 case O(CREATE
,'u'): /* uuid of array */
633 case O(ASSEMBLE
,'u'): /* uuid of array */
634 if (ident
.uuid_set
) {
635 pr_err("uuid cannot be set twice. "
636 "Second value %s.\n", optarg
);
639 if (parse_uuid(optarg
, ident
.uuid
))
642 pr_err("Bad uuid: %s\n", optarg
);
648 case O(ASSEMBLE
,'N'):
651 pr_err("name cannot be set twice. "
652 "Second value %s.\n", optarg
);
655 if (mode
== MISC
&& !c
.subarray
) {
656 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
659 if (strlen(optarg
) > 32) {
660 pr_err("name '%s' is too long, 32 chars max.\n",
664 strcpy(ident
.name
, optarg
);
667 case O(ASSEMBLE
,'m'): /* super-minor for array */
668 case O(ASSEMBLE
,SuperMinor
):
669 if (ident
.super_minor
!= UnSet
) {
670 pr_err("super-minor cannot be set twice. "
671 "Second value: %s.\n", optarg
);
674 if (strcmp(optarg
, "dev")==0)
675 ident
.super_minor
= -2;
677 ident
.super_minor
= parse_num(optarg
);
678 if (ident
.super_minor
< 0) {
679 pr_err("Bad super-minor number: %s.\n", optarg
);
685 case O(ASSEMBLE
,'o'):
691 case O(ASSEMBLE
,'U'): /* update the superblock */
694 pr_err("Can only update one aspect"
695 " of superblock, both %s and %s given.\n",
699 if (mode
== MISC
&& !c
.subarray
) {
700 pr_err("Only subarrays can be"
701 " updated in misc mode\n");
705 if (strcmp(c
.update
, "sparc2.2")==0)
707 if (strcmp(c
.update
, "super-minor") == 0)
709 if (strcmp(c
.update
, "summaries")==0)
711 if (strcmp(c
.update
, "resync")==0)
713 if (strcmp(c
.update
, "uuid")==0)
715 if (strcmp(c
.update
, "name")==0)
717 if (strcmp(c
.update
, "homehost")==0)
719 if (strcmp(c
.update
, "devicesize")==0)
721 if (strcmp(c
.update
, "no-bitmap")==0)
723 if (strcmp(c
.update
, "byteorder")==0) {
725 pr_err("must not set metadata"
726 " type with --update=byteorder.\n");
729 for(i
=0; !ss
&& superlist
[i
]; i
++)
730 ss
= superlist
[i
]->match_metadata_desc(
733 pr_err("INTERNAL ERROR"
734 " cannot find 0.swap\n");
740 if (strcmp(c
.update
,"?") == 0 ||
741 strcmp(c
.update
, "help") == 0) {
743 fprintf(outf
, Name
": ");
747 Name
": '--update=%s' is invalid. ",
750 fprintf(outf
, "Valid --update options are:\n"
751 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
752 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
754 exit(outf
== stdout
? 0 : 2);
757 /* update=devicesize is allowed with --re-add */
758 if (devmode
!= 'A') {
759 pr_err("--update in Manage mode only"
760 " allowed with --re-add.\n");
764 pr_err("Can only update one aspect"
765 " of superblock, both %s and %s given.\n",
770 if (strcmp(c
.update
, "devicesize") != 0) {
771 pr_err("only 'devicesize' can be"
772 " updated with --re-add\n");
777 case O(INCREMENTAL
,NoDegraded
):
778 pr_err("--no-degraded is deprecated in Incremental mode\n");
779 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
780 c
.runstop
= -1; /* --stop isn't allowed for --assemble,
781 * so we overload slightly */
784 case O(ASSEMBLE
,'c'):
785 case O(ASSEMBLE
,ConfigFile
):
786 case O(INCREMENTAL
, 'c'):
787 case O(INCREMENTAL
, ConfigFile
):
789 case O(MISC
, ConfigFile
):
791 case O(MONITOR
,ConfigFile
):
793 pr_err("configfile cannot be set twice. "
794 "Second value is %s.\n", optarg
);
798 set_conffile(configfile
);
799 /* FIXME possibly check that config file exists. Even parse it */
801 case O(ASSEMBLE
,'s'): /* scan */
804 case O(INCREMENTAL
,'s'):
808 case O(MONITOR
,'m'): /* mail address */
809 case O(MONITOR
,EMail
):
811 pr_err("only specify one mailaddress. %s ignored.\n",
817 case O(MONITOR
,'p'): /* alert program */
818 case O(MONITOR
,ProgramOpt
): /* alert program */
820 pr_err("only specify one alter program. %s ignored.\n",
826 case O(MONITOR
,'r'): /* rebuild increments */
827 case O(MONITOR
,Increment
):
828 increments
= atoi(optarg
);
829 if (increments
> 99 || increments
< 1) {
830 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
835 case O(MONITOR
,'d'): /* delay in seconds */
837 case O(BUILD
,'d'): /* delay for bitmap updates */
840 pr_err("only specify delay once. %s ignored.\n",
843 c
.delay
= parse_num(optarg
);
845 pr_err("invalid delay: %s\n",
851 case O(MONITOR
,'f'): /* daemonise */
852 case O(MONITOR
,Fork
):
855 case O(MONITOR
,'i'): /* pid */
857 pr_err("only specify one pid file. %s ignored.\n",
862 case O(MONITOR
,'1'): /* oneshot */
866 case O(MONITOR
,'t'): /* test */
869 case O(MONITOR
,'y'): /* log messages to syslog */
870 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
873 case O(MONITOR
, NoSharing
):
877 /* now the general management options. Some are applicable
878 * to other modes. None have arguments.
883 case O(MANAGE
,Add
): /* add a drive */
886 case O(MANAGE
,ReAdd
):
889 case O(MANAGE
,'r'): /* remove a drive */
890 case O(MANAGE
,Remove
):
893 case O(MANAGE
,'f'): /* set faulty */
895 case O(INCREMENTAL
,'f'):
896 case O(INCREMENTAL
,Remove
):
897 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
898 * even though we will both fail and
899 * remove the device */
902 case O(INCREMENTAL
,'R'):
904 case O(ASSEMBLE
,'R'):
906 case O(CREATE
,'R'): /* Run the array */
908 pr_err("Cannot both Stop and Run an array\n");
915 pr_err("Cannot both Run and Stop an array\n");
927 case O(MISC
,KillOpt
):
934 case O(MISC
, WaitOpt
):
935 case O(MISC
, Waitclean
):
936 case O(MISC
, DetailPlatform
):
937 case O(MISC
, KillSubarray
):
938 case O(MISC
, UpdateSubarray
):
939 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
941 pr_err("subarray can only"
942 " be specified once\n");
947 if (devmode
&& devmode
!= opt
&&
948 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
949 pr_err("--examine/-E cannot be given with ");
950 if (devmode
== 'E') {
951 if (option_index
>= 0)
952 fprintf(stderr
, "--%s\n",
953 long_options
[option_index
].name
);
955 fprintf(stderr
, "-%c\n", opt
);
956 } else if (isalpha(devmode
))
957 fprintf(stderr
, "-%c\n", devmode
);
959 fprintf(stderr
, "previous option\n");
964 case O(MISC
, UdevRules
):
965 if (devmode
&& devmode
!= opt
) {
966 pr_err("--udev-rules must"
967 " be the only option.\n");
970 pr_err("only specify one udev "
971 "rule filename. %s ignored.\n",
974 udev_filename
= optarg
;
982 case O(MISC
, Sparc22
):
983 if (devmode
!= 'E') {
984 pr_err("--sparc2.2 only allowed with --examine\n");
990 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
991 case O(ASSEMBLE
,Bitmap
):
993 pr_err("bitmap file needed with -b in --assemble mode\n");
996 if (strcmp(optarg
, "internal")==0) {
997 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1000 bitmap_fd
= open(optarg
, O_RDWR
);
1001 if (!*optarg
|| bitmap_fd
< 0) {
1002 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1005 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1008 case O(ASSEMBLE
, BackupFile
):
1009 case O(GROW
, BackupFile
):
1010 /* Specify a file into which grow might place a backup,
1011 * or from which assemble might recover a backup
1013 if (c
.backup_file
) {
1014 pr_err("backup file already specified, rejecting %s\n", optarg
);
1017 c
.backup_file
= optarg
;
1020 case O(GROW
, Continue
):
1021 /* Continue interrupted grow
1025 case O(ASSEMBLE
, InvalidBackup
):
1026 /* Acknowledge that the backupfile is invalid, but ask
1027 * to continue anyway
1029 c
.invalid_backup
= 1;
1033 case O(BUILD
,Bitmap
):
1035 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1036 if (strcmp(optarg
, "none") == 0) {
1037 pr_err("'--bitmap none' only"
1038 " supported for --grow\n");
1043 case O(GROW
,Bitmap
):
1044 if (strcmp(optarg
, "internal")== 0 ||
1045 strcmp(optarg
, "none")== 0 ||
1046 strchr(optarg
, '/') != NULL
) {
1047 bitmap_file
= optarg
;
1051 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1052 " not '%s'\n", optarg
);
1055 case O(GROW
,BitmapChunk
):
1056 case O(BUILD
,BitmapChunk
):
1057 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1058 bitmap_chunk
= parse_size(optarg
);
1059 if (bitmap_chunk
<= 0 ||
1060 bitmap_chunk
& (bitmap_chunk
- 1)) {
1061 pr_err("invalid bitmap chunksize: %s\n",
1065 bitmap_chunk
= bitmap_chunk
* 512;
1068 case O(GROW
, WriteBehind
):
1069 case O(BUILD
, WriteBehind
):
1070 case O(CREATE
, WriteBehind
): /* write-behind mode */
1071 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1073 write_behind
= parse_num(optarg
);
1074 if (write_behind
< 0 ||
1075 write_behind
> 16383) {
1076 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1082 case O(INCREMENTAL
, 'r'):
1083 case O(INCREMENTAL
, RebuildMapOpt
):
1086 case O(INCREMENTAL
, IncrementalPath
):
1087 remove_path
= optarg
;
1090 /* We have now processed all the valid options. Anything else is
1093 if (option_index
> 0)
1094 pr_err(":option --%s not valid in %s mode\n",
1095 long_options
[option_index
].name
,
1096 map_num(modes
, mode
));
1098 pr_err("option -%c not valid in %s mode\n",
1099 opt
, map_num(modes
, mode
));
1106 if (print_help
== 2)
1107 help_text
= OptionHelp
;
1109 help_text
= mode_help
[mode
];
1110 if (help_text
== NULL
)
1112 fputs(help_text
,stdout
);
1116 if (!mode
&& devs_found
) {
1119 if (devlist
->disposition
== 0)
1120 devlist
->disposition
= devmode
;
1123 fputs(Usage
, stderr
);
1128 struct createinfo
*ci
= conf_get_create_info();
1130 if (strcasecmp(symlinks
, "yes") == 0)
1132 else if (strcasecmp(symlinks
, "no") == 0)
1135 pr_err("option --symlinks must be 'no' or 'yes'\n");
1139 /* Ok, got the option parsing out of the way
1140 * hopefully it's mostly right but there might be some stuff
1143 * That is mosty checked in the per-mode stuff but...
1145 * For @,B,C and A without -s, the first device listed must be
1146 * an md device. We check that here and open it.
1149 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1151 || (mode
== ASSEMBLE
&& ! c
.scan
)) {
1152 if (devs_found
< 1) {
1153 pr_err("an md device must be given in this mode\n");
1156 if ((int)ident
.super_minor
== -2 && c
.autof
) {
1157 pr_err("--super-minor=dev is incompatible with --auto\n");
1160 if (mode
== MANAGE
|| mode
== GROW
) {
1161 mdfd
= open_mddev(devlist
->devname
, 1);
1165 /* non-existent device is OK */
1166 mdfd
= open_mddev(devlist
->devname
, 0);
1168 pr_err("device %s exists but is not an "
1169 "md array.\n", devlist
->devname
);
1172 if ((int)ident
.super_minor
== -2) {
1175 pr_err("--super-minor=dev given, and "
1176 "listed device %s doesn't exist.\n",
1181 ident
.super_minor
= minor(stb
.st_rdev
);
1183 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1184 /* We don't really want this open yet, we just might
1185 * have wanted to check some things
1193 if (raiddisks
== 1 && !c
.force
&& level
!= LEVEL_FAULTY
) {
1194 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1195 " a mistake. If you really mean it you will need to specify --force before\n"
1196 " setting the number of drives.\n");
1201 if (c
.homehost
== NULL
)
1202 c
.homehost
= conf_get_homehost(&c
.require_homehost
);
1203 if (c
.homehost
== NULL
|| strcasecmp(c
.homehost
, "<system>")==0) {
1204 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1205 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1206 c
.homehost
= sys_hostname
;
1209 if (c
.homehost
&& (!c
.homehost
[0] || strcasecmp(c
.homehost
, "<none>") == 0)) {
1211 c
.require_homehost
= 0;
1214 if ((mode
== MISC
&& devmode
== 'E')
1215 || (mode
== MONITOR
&& spare_sharing
== 0))
1216 /* Anyone may try this */;
1217 else if (geteuid() != 0) {
1218 pr_err("must be super-user to perform this action\n");
1222 ident
.autof
= c
.autof
;
1224 if (c
.scan
&& c
.verbose
< 2)
1225 /* --scan implied --brief unless -vv */
1231 /* readonly, add/remove, readwrite, runstop */
1233 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1234 if (!rv
&& devs_found
>1)
1235 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1236 devlist
->next
, c
.verbose
, c
.test
,
1238 if (!rv
&& c
.readonly
< 0)
1239 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1240 if (!rv
&& c
.runstop
)
1241 rv
= Manage_runstop(devlist
->devname
, mdfd
, c
.runstop
, c
.verbose
, 0);
1244 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1245 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !c
.scan
) {
1246 /* Only a device has been given, so get details from config file */
1247 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1248 if (array_ident
== NULL
) {
1249 pr_err("%s not identified in config file.\n",
1255 if (array_ident
->autof
== 0)
1256 array_ident
->autof
= c
.autof
;
1257 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1261 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1263 else if (devs_found
> 0) {
1264 if (c
.update
&& devs_found
> 1) {
1265 pr_err("can only update a single array at a time\n");
1268 if (c
.backup_file
&& devs_found
> 1) {
1269 pr_err("can only assemble a single array when providing a backup file.\n");
1272 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1273 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1274 if (array_ident
== NULL
) {
1275 pr_err("%s not identified in config file.\n",
1280 if (array_ident
->autof
== 0)
1281 array_ident
->autof
= c
.autof
;
1282 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1287 pr_err("--update not meaningful with a --scan assembly.\n");
1290 if (c
.backup_file
) {
1291 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1294 rv
= scan_assemble(ss
, &c
, &ident
);
1300 c
.delay
= DEFAULT_BITMAP_DELAY
;
1301 if (write_behind
&& !bitmap_file
) {
1302 pr_err("write-behind mode requires a bitmap.\n");
1306 if (raiddisks
== 0) {
1307 pr_err("no raid-devices specified.\n");
1313 if (strcmp(bitmap_file
, "internal")==0) {
1314 pr_err("'internal' bitmaps not supported with --build\n");
1319 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1320 raiddisks
, devlist
->next
, assume_clean
,
1321 bitmap_file
, bitmap_chunk
, write_behind
,
1326 c
.delay
= DEFAULT_BITMAP_DELAY
;
1327 if (write_behind
&& !bitmap_file
) {
1328 pr_err("write-behind mode requires a bitmap.\n");
1332 if (raiddisks
== 0) {
1333 pr_err("no raid-devices specified.\n");
1338 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1339 raiddisks
, sparedisks
, ident
.name
,
1340 ident
.uuid_set
? ident
.uuid
: NULL
,
1341 devs_found
-1, devlist
->next
,
1343 bitmap_file
, bitmap_chunk
, write_behind
, &c
);
1346 if (devmode
== 'E') {
1347 if (devlist
== NULL
&& !c
.scan
) {
1348 pr_err("No devices to examine\n");
1351 if (devlist
== NULL
)
1352 devlist
= conf_get_devs();
1353 if (devlist
== NULL
) {
1354 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1357 rv
= Examine(devlist
, &c
, ss
);
1358 } else if (devmode
== DetailPlatform
) {
1359 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? c
.scan
: 1, c
.verbose
);
1360 } else if (devlist
== NULL
) {
1361 if (devmode
== 'S' && c
.scan
)
1362 rv
= stop_scan(c
.verbose
);
1363 else if ((devmode
== 'D' || devmode
== Waitclean
) && c
.scan
)
1364 rv
= misc_scan(devmode
, c
.verbose
, c
.export
,
1365 c
.test
, c
.homehost
, c
.prefer
);
1366 else if (devmode
== UdevRules
)
1367 rv
= Write_rules(udev_filename
);
1369 pr_err("No devices given.\n");
1373 rv
= misc_list(devlist
, &ident
, ss
, &c
);
1376 if (!devlist
&& !c
.scan
) {
1377 pr_err("Cannot monitor: need --scan or at least one device\n");
1381 if (pidfile
&& !daemonise
) {
1382 pr_err("Cannot write a pid file when not in daemon mode\n");
1387 if (get_linux_version() > 2006016)
1388 /* mdstat responds to poll */
1393 rv
= Monitor(devlist
, mailaddr
, program
,
1394 c
.delay
?c
.delay
:60, daemonise
, c
.scan
, oneshot
,
1395 dosyslog
, c
.test
, pidfile
, increments
,
1396 spare_sharing
, c
.prefer
);
1400 if (array_size
>= 0) {
1401 /* alway impose array size first, independent of
1403 * Do not allow level or raid_disks changes at the
1404 * same time as that can be irreversibly destructive.
1408 if (raiddisks
|| level
!= UnSet
) {
1409 pr_err("cannot change array size in same operation "
1410 "as changing raiddisks or level.\n"
1411 " Change size first, then check that data is still intact.\n");
1415 sysfs_init(&sra
, mdfd
, 0);
1416 if (array_size
== 0)
1417 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1419 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1422 pr_err("--array-size setting"
1423 " is too large.\n");
1425 pr_err("current kernel does"
1426 " not support setting --array-size\n");
1431 if (devs_found
> 1 && raiddisks
== 0) {
1433 if (size
>= 0 || chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1434 pr_err("--add cannot be used with "
1435 "other geometry changes in --grow mode\n");
1439 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1440 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1445 } else if (bitmap_file
) {
1446 if (size
>= 0 || raiddisks
|| chunk
||
1447 layout_str
!= NULL
|| devs_found
> 1) {
1448 pr_err("--bitmap changes cannot be "
1449 "used with other geometry changes "
1450 "in --grow mode\n");
1455 c
.delay
= DEFAULT_BITMAP_DELAY
;
1456 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1457 bitmap_chunk
, c
.delay
, write_behind
, c
.force
);
1458 } else if (grow_continue
)
1459 rv
= Grow_continue_command(devlist
->devname
,
1460 mdfd
, c
.backup_file
,
1462 else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1463 || chunk
!= 0 || level
!= UnSet
) {
1464 rv
= Grow_reshape(devlist
->devname
, mdfd
, c
.verbose
, c
.backup_file
,
1465 size
, level
, layout_str
, chunk
, raiddisks
,
1467 assume_clean
, c
.force
);
1468 } else if (array_size
< 0)
1469 pr_err("no changes to --grow\n");
1476 if (c
.runstop
<= 0) {
1477 pr_err("--incremental --scan meaningless without --run.\n");
1480 if (devmode
== 'f') {
1481 pr_err("--incremental --scan --fail not supported.\n");
1484 rv
= IncrementalScan(c
.verbose
);
1487 if (!rebuild_map
&& !c
.scan
) {
1488 pr_err("--incremental requires a device.\n");
1493 if (devlist
->next
) {
1494 pr_err("--incremental can only handle one device.\n");
1499 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1502 rv
= Incremental(devlist
->devname
, c
.verbose
,
1503 c
.runstop
, ss
, c
.homehost
,
1504 c
.require_homehost
, c
.autof
,
1514 static int scan_assemble(struct supertype
*ss
,
1516 struct mddev_ident
*ident
)
1518 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1519 struct mddev_dev
*devlist
= conf_get_devs();
1520 struct map_ent
*map
= NULL
;
1523 int failures
, successes
;
1525 if (conf_verify_devnames(array_list
)) {
1526 pr_err("Duplicate MD device names in "
1527 "conf file were found.\n");
1530 if (devlist
== NULL
) {
1531 pr_err("No devices listed in conf file were found.\n");
1534 for (a
= array_list
; a
; a
= a
->next
) {
1537 a
->autof
= c
->autof
;
1540 pr_err("%s: failed to get "
1541 "exclusive lock on mapfile\n",
1547 for (a
= array_list
; a
; a
= a
->next
) {
1552 strcasecmp(a
->devname
, "<ignore>") == 0)
1555 r
= Assemble(ss
, a
->devname
,
1565 } while (failures
&& successes
);
1566 if (c
->homehost
&& cnt
== 0) {
1567 /* Maybe we can auto-assemble something.
1568 * Repeatedly call Assemble in auto-assemble mode
1573 ident
->autof
= c
->autof
;
1575 struct mddev_dev
*devlist
= conf_get_devs();
1578 rv2
= Assemble(ss
, NULL
,
1586 /* Incase there are stacked devices, we need to go around again */
1588 if (cnt
== 0 && rv
== 0) {
1589 pr_err("No arrays found in config file or automatically\n");
1593 } else if (cnt
== 0 && rv
== 0) {
1594 pr_err("No arrays found in config file\n");
1601 static int misc_scan(char devmode
, int verbose
, int export
, int test
,
1602 char *homehost
, char *prefer
)
1604 /* apply --detail or --wait-clean to
1605 * all devices in /proc/mdstat
1607 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1608 struct mdstat_ent
*e
;
1609 struct map_ent
*map
= NULL
;
1611 int v
= verbose
>1?0:verbose
+1;
1614 for (members
= 0; members
<= 1; members
++) {
1615 for (e
=ms
; e
; e
=e
->next
) {
1618 int member
= e
->metadata_version
&&
1619 strncmp(e
->metadata_version
,
1620 "external:/", 10) == 0;
1621 if (members
!= member
)
1623 me
= map_by_devnum(&map
, e
->devnum
);
1625 && strcmp(me
->path
, "/unknown") != 0)
1628 name
= get_md_name(e
->devnum
);
1631 pr_err("cannot find device file for %s\n",
1636 rv
|= Detail(name
, v
,
1640 rv
|= WaitClean(name
, -1, v
);
1648 static int stop_scan(int verbose
)
1650 /* apply --stop to all devices in /proc/mdstat */
1651 /* Due to possible stacking of devices, repeat until
1652 * nothing more can be stopped
1654 int progress
=1, err
;
1658 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1659 struct mdstat_ent
*e
;
1661 if (!progress
) last
= 1;
1662 progress
= 0; err
= 0;
1663 for (e
=ms
; e
; e
=e
->next
) {
1664 char *name
= get_md_name(e
->devnum
);
1668 pr_err("cannot find device file for %s\n",
1672 mdfd
= open_mddev(name
, 1);
1674 if (Manage_runstop(name
, mdfd
, -1, verbose
, !last
))
1684 } while (!last
&& err
);
1690 static int misc_list(struct mddev_dev
*devlist
,
1691 struct mddev_ident
*ident
,
1692 struct supertype
*ss
, struct context
*c
)
1694 struct mddev_dev
*dv
;
1697 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1700 switch(dv
->disposition
) {
1702 rv
|= Detail(dv
->devname
,
1703 c
->brief
?1+c
->verbose
:0,
1704 c
->export
, c
->test
, c
->homehost
, c
->prefer
);
1706 case KillOpt
: /* Zero superblock */
1708 rv
|= Kill(dv
->devname
, ss
, c
->force
, c
->verbose
,0);
1712 rv
|= Kill(dv
->devname
, NULL
, c
->force
, v
, 0);
1719 rv
|= Query(dv
->devname
); continue;
1721 rv
|= ExamineBitmap(dv
->devname
, c
->brief
, ss
); continue;
1724 rv
|= Wait(dv
->devname
); continue;
1726 rv
|= WaitClean(dv
->devname
, -1, c
->verbose
); continue;
1728 rv
|= Kill_subarray(dv
->devname
, c
->subarray
, c
->verbose
);
1730 case UpdateSubarray
:
1731 if (c
->update
== NULL
) {
1732 pr_err("-U/--update must be specified with --update-subarray\n");
1736 rv
|= Update_subarray(dv
->devname
, c
->subarray
,
1737 c
->update
, ident
, c
->verbose
);
1740 mdfd
= open_mddev(dv
->devname
, 1);
1742 switch(dv
->disposition
) {
1744 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, c
->verbose
, 0); break;
1746 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, c
->verbose
, 0); break;
1748 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1750 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;