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 quiet
);
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
, int quiet
);
49 int main(int argc
, char *argv
[])
60 long long array_size
= -1;
63 char *layout_str
= NULL
;
66 struct mddev_ident ident
;
67 char *configfile
= NULL
;
76 char *bitmap_file
= NULL
;
77 char *backup_file
= NULL
;
78 int invalid_backup
= 0;
79 int bitmap_chunk
= UnSet
;
81 struct mddev_dev
*devlist
= NULL
;
82 struct mddev_dev
**devlistend
= & devlist
;
93 char *symlinks
= NULL
;
94 int grow_continue
= 0;
95 /* autof indicates whether and how to create device node.
96 * bottom 3 bits are style. Rest (when shifted) are number of parts
98 * 1 - don't create (no)
99 * 2 - if is_standard, then create (yes)
100 * 3 - create as 'md' - reject is_standard mdp (md)
101 * 4 - create as 'mdp' - reject is_standard md (mdp)
102 * 5 - default to md if not is_standard (md in config file)
103 * 6 - default to mdp if not is_standard (part, or mdp in config file)
107 char *homehost
= NULL
;
108 char sys_hostname
[256];
109 int require_homehost
= 1;
110 char *mailaddr
= NULL
;
111 char *program
= NULL
;
115 char *pidfile
= NULL
;
117 int spare_sharing
= 1;
118 struct supertype
*ss
= NULL
;
120 char *shortopt
= short_options
;
123 char *subarray
= NULL
;
124 char *remove_path
= NULL
;
125 char *udev_filename
= NULL
;
132 int freeze_reshape
= 0;
134 srandom(time(0) ^ getpid());
138 ident
.raid_disks
= UnSet
;
139 ident
.super_minor
= UnSet
;
141 ident
.spare_group
= NULL
;
144 ident
.bitmap_fd
= -1;
145 ident
.bitmap_file
= NULL
;
147 ident
.container
= NULL
;
150 while ((option_index
= -1) ,
151 (opt
=getopt_long(argc
, argv
,
152 shortopt
, long_options
,
153 &option_index
)) != -1) {
155 /* firstly, some mode-independent options */
165 fputs(Version
, stderr
);
175 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
176 || mode
== GROW
|| mode
== INCREMENTAL
178 break; /* b means bitmap */
187 if (strcasecmp(optarg
, "<ignore>") == 0)
188 require_homehost
= 0;
194 * --offroot sets first char of argv[0] to @. This is used
195 * by systemd to signal that the task was launched from
196 * initrd/initramfs and should be preserved during shutdown
206 if (asprintf(&prefer
, "/%s/", optarg
) <= 0)
212 fputs(Usage
, stderr
);
215 /* second, figure out the mode.
216 * Some options force the mode. Others
217 * set the mode if it isn't already
223 shortopt
= short_bitmap_options
;
231 case ReAdd
: /* re-add */
234 shortopt
= short_bitmap_options
;
238 case 'A': newmode
= ASSEMBLE
;
239 shortopt
= short_bitmap_auto_options
;
241 case 'B': newmode
= BUILD
;
242 shortopt
= short_bitmap_auto_options
;
244 case 'C': newmode
= CREATE
;
245 shortopt
= short_bitmap_auto_options
;
247 case 'F': newmode
= MONITOR
;
249 case 'G': newmode
= GROW
;
250 shortopt
= short_bitmap_options
;
252 case 'I': newmode
= INCREMENTAL
;
253 shortopt
= short_bitmap_auto_options
;
256 newmode
= AUTODETECT
;
287 if (mode
&& newmode
== mode
) {
288 /* everybody happy ! */
289 } else if (mode
&& newmode
!= mode
) {
292 if (option_index
>= 0)
293 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
295 fprintf(stderr
, "-%c", opt
);
296 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
297 map_num(modes
, newmode
),
298 map_num(modes
, mode
));
300 } else if (!mode
&& newmode
) {
302 if (mode
== MISC
&& devs_found
) {
303 pr_err("No action given for %s in --misc mode\n",
305 fprintf(stderr
," Action options must come before device names\n");
309 /* special case of -c --help */
310 if ((opt
== 'c' || opt
== ConfigFile
) &&
311 ( strncmp(optarg
, "--h", 3)==0 ||
312 strncmp(optarg
, "-h", 2)==0)) {
313 fputs(Help_config
, stdout
);
317 /* If first option is a device, don't force the mode yet */
319 if (devs_found
== 0) {
320 dv
= xmalloc(sizeof(*dv
));
321 dv
->devname
= optarg
;
322 dv
->disposition
= devmode
;
323 dv
->writemostly
= writemostly
;
327 devlistend
= &dv
->next
;
332 /* No mode yet, and this is the second device ... */
333 pr_err("An option must be given to set the mode before a second device\n"
334 " (%s) is listed\n", optarg
);
337 if (option_index
>= 0)
338 pr_err("--%s", long_options
[option_index
].name
);
341 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
345 /* if we just set the mode, then done */
359 /* an undecorated option - must be a device name.
361 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
362 pr_err("Must give one of -a/-r/-f"
363 " for subsequent devices at %s\n", optarg
);
366 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
367 pr_err("Must give -a/--add for"
368 " devices to add: %s\n", optarg
);
371 dv
= xmalloc(sizeof(*dv
));
372 dv
->devname
= optarg
;
373 dv
->disposition
= devmode
;
374 dv
->writemostly
= writemostly
;
378 devlistend
= &dv
->next
;
384 /* We've got a mode, and opt is now something else which
385 * could depend on the mode */
386 #define O(a,b) ((a<<16)|b)
387 switch (O(mode
,opt
)) {
389 case O(GROW
,ChunkSize
):
391 case O(CREATE
,ChunkSize
):
392 case O(BUILD
,'c'): /* chunk or rounding */
393 case O(BUILD
,ChunkSize
): /* chunk or rounding */
395 pr_err("chunk/rounding may only be specified once. "
396 "Second value is %s.\n", optarg
);
399 chunk
= parse_size(optarg
);
400 if (chunk
< 8 || (chunk
&1)) {
401 pr_err("invalid chunk/rounding value: %s\n",
405 /* Convert sectors to K */
409 case O(INCREMENTAL
, 'e'):
411 case O(ASSEMBLE
,'e'):
412 case O(MISC
,'e'): /* set metadata (superblock) information */
414 pr_err("metadata information already given\n");
417 for(i
=0; !ss
&& superlist
[i
]; i
++)
418 ss
= superlist
[i
]->match_metadata_desc(optarg
);
421 pr_err("unrecognised metadata identifier: %s\n", optarg
);
427 case O(MANAGE
,WriteMostly
):
429 case O(BUILD
,WriteMostly
):
431 case O(CREATE
,WriteMostly
):
432 /* set write-mostly for following devices */
437 /* clear write-mostly for following devices */
444 case O(BUILD
,'z'): /* size */
446 pr_err("size may only be specified once. "
447 "Second value is %s.\n", optarg
);
450 if (strcmp(optarg
, "max")==0)
453 size
= parse_size(optarg
);
455 pr_err("invalid size: %s\n",
459 /* convert sectors to K */
464 case O(GROW
,'Z'): /* array size */
465 if (array_size
>= 0) {
466 pr_err("array-size may only be specified once. "
467 "Second value is %s.\n", optarg
);
470 if (strcmp(optarg
, "max") == 0)
473 array_size
= parse_size(optarg
);
474 if (array_size
<= 0) {
475 pr_err("invalid array size: %s\n",
484 case O(BUILD
,'l'): /* set raid level*/
485 if (level
!= UnSet
) {
486 pr_err("raid level may only be set once. "
487 "Second value is %s.\n", optarg
);
490 level
= map_name(pers
, optarg
);
491 if (level
== UnSet
) {
492 pr_err("invalid raid level: %s\n",
496 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
497 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
500 pr_err("Raid level %s not permitted with --build.\n",
504 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
505 pr_err("raid level %s is incompatible with spare-devices setting.\n",
512 case O(GROW
, 'p'): /* new layout */
513 case O(GROW
, Layout
):
515 pr_err("layout may only be sent once. "
516 "Second value was %s\n", optarg
);
520 /* 'Grow' will parse the value */
523 case O(CREATE
,'p'): /* raid5 layout */
524 case O(CREATE
,Layout
):
525 case O(BUILD
,'p'): /* faulty layout */
526 case O(BUILD
,Layout
):
527 if (layout
!= UnSet
) {
528 pr_err("layout may only be sent once. "
529 "Second value was %s\n", optarg
);
534 pr_err("layout not meaningful for %s arrays.\n",
535 map_num(pers
, level
));
538 pr_err("raid level must be given before layout.\n");
542 layout
= map_name(r5layout
, optarg
);
544 pr_err("layout %s not understood for raid5.\n",
550 layout
= map_name(r6layout
, optarg
);
552 pr_err("layout %s not understood for raid6.\n",
559 layout
= parse_layout_10(optarg
);
561 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
569 layout
= parse_layout_faulty(optarg
);
571 pr_err("layout %s not understood for faulty.\n",
579 case O(CREATE
,AssumeClean
):
580 case O(BUILD
,AssumeClean
): /* assume clean */
581 case O(GROW
,AssumeClean
):
587 case O(BUILD
,'n'): /* number of raid disks */
589 pr_err("raid-devices set twice: %d and %s\n",
593 raiddisks
= strtol(optarg
, &c
, 10);
594 if (!optarg
[0] || *c
|| raiddisks
<=0) {
595 pr_err("invalid number of raid devices: %s\n",
599 ident
.raid_disks
= raiddisks
;
602 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
604 pr_err("spare-devices set twice: %d and %s\n",
608 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
609 pr_err("spare-devices setting is incompatible with raid level %d\n",
613 sparedisks
= strtol(optarg
, &c
, 10);
614 if (!optarg
[0] || *c
|| sparedisks
< 0) {
615 pr_err("invalid number of spare-devices: %s\n",
625 case O(INCREMENTAL
,'a'):
626 case O(INCREMENTAL
,Auto
):
627 case O(ASSEMBLE
,'a'):
628 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
629 autof
= parse_auto(optarg
, "--auto flag", 0);
632 case O(CREATE
,Symlinks
):
633 case O(BUILD
,Symlinks
):
634 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
638 case O(BUILD
,'f'): /* force honouring '-n 1' */
639 case O(BUILD
,Force
): /* force honouring '-n 1' */
640 case O(GROW
,'f'): /* ditto */
641 case O(GROW
,Force
): /* ditto */
642 case O(CREATE
,'f'): /* force honouring of device list */
643 case O(CREATE
,Force
): /* force honouring of device list */
644 case O(ASSEMBLE
,'f'): /* force assembly */
645 case O(ASSEMBLE
,Force
): /* force assembly */
646 case O(MISC
,'f'): /* force zero */
647 case O(MISC
,Force
): /* force zero */
648 case O(MANAGE
,Force
): /* add device which is too large */
651 /* now for the Assemble options */
652 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
654 case O(INCREMENTAL
, FreezeReshape
):
657 case O(CREATE
,'u'): /* uuid of array */
658 case O(ASSEMBLE
,'u'): /* uuid of array */
659 if (ident
.uuid_set
) {
660 pr_err("uuid cannot be set twice. "
661 "Second value %s.\n", optarg
);
664 if (parse_uuid(optarg
, ident
.uuid
))
667 pr_err("Bad uuid: %s\n", optarg
);
673 case O(ASSEMBLE
,'N'):
676 pr_err("name cannot be set twice. "
677 "Second value %s.\n", optarg
);
680 if (mode
== MISC
&& !subarray
) {
681 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
684 if (strlen(optarg
) > 32) {
685 pr_err("name '%s' is too long, 32 chars max.\n",
689 strcpy(ident
.name
, optarg
);
692 case O(ASSEMBLE
,'m'): /* super-minor for array */
693 case O(ASSEMBLE
,SuperMinor
):
694 if (ident
.super_minor
!= UnSet
) {
695 pr_err("super-minor cannot be set twice. "
696 "Second value: %s.\n", optarg
);
699 if (strcmp(optarg
, "dev")==0)
700 ident
.super_minor
= -2;
702 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
703 if (!optarg
[0] || *cp
) {
704 pr_err("Bad super-minor number: %s.\n", optarg
);
710 case O(ASSEMBLE
,'U'): /* update the superblock */
713 pr_err("Can only update one aspect"
714 " of superblock, both %s and %s given.\n",
718 if (mode
== MISC
&& !subarray
) {
719 pr_err("Only subarrays can be"
720 " updated in misc mode\n");
724 if (strcmp(update
, "sparc2.2")==0)
726 if (strcmp(update
, "super-minor") == 0)
728 if (strcmp(update
, "summaries")==0)
730 if (strcmp(update
, "resync")==0)
732 if (strcmp(update
, "uuid")==0)
734 if (strcmp(update
, "name")==0)
736 if (strcmp(update
, "homehost")==0)
738 if (strcmp(update
, "devicesize")==0)
740 if (strcmp(update
, "no-bitmap")==0)
742 if (strcmp(update
, "byteorder")==0) {
744 pr_err("must not set metadata"
745 " type with --update=byteorder.\n");
748 for(i
=0; !ss
&& superlist
[i
]; i
++)
749 ss
= superlist
[i
]->match_metadata_desc(
752 pr_err("INTERNAL ERROR"
753 " cannot find 0.swap\n");
759 if (strcmp(update
,"?") == 0 ||
760 strcmp(update
, "help") == 0) {
762 fprintf(outf
, Name
": ");
766 Name
": '--update=%s' is invalid. ",
769 fprintf(outf
, "Valid --update options are:\n"
770 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
771 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
773 exit(outf
== stdout
? 0 : 2);
776 /* update=devicesize is allowed with --re-add */
777 if (devmode
!= 'A') {
778 pr_err("--update in Manage mode only"
779 " allowed with --re-add.\n");
783 pr_err("Can only update one aspect"
784 " of superblock, both %s and %s given.\n",
789 if (strcmp(update
, "devicesize") != 0) {
790 pr_err("only 'devicesize' can be"
791 " updated with --re-add\n");
796 case O(INCREMENTAL
,NoDegraded
):
797 pr_err("--no-degraded is deprecated in Incremental mode\n");
798 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
799 runstop
= -1; /* --stop isn't allowed for --assemble,
800 * so we overload slightly */
803 case O(ASSEMBLE
,'c'):
804 case O(ASSEMBLE
,ConfigFile
):
805 case O(INCREMENTAL
, 'c'):
806 case O(INCREMENTAL
, ConfigFile
):
808 case O(MISC
, ConfigFile
):
810 case O(MONITOR
,ConfigFile
):
812 pr_err("configfile cannot be set twice. "
813 "Second value is %s.\n", optarg
);
817 set_conffile(configfile
);
818 /* FIXME possibly check that config file exists. Even parse it */
820 case O(ASSEMBLE
,'s'): /* scan */
823 case O(INCREMENTAL
,'s'):
827 case O(MONITOR
,'m'): /* mail address */
828 case O(MONITOR
,EMail
):
830 pr_err("only specify one mailaddress. %s ignored.\n",
836 case O(MONITOR
,'p'): /* alert program */
837 case O(MONITOR
,ProgramOpt
): /* alert program */
839 pr_err("only specify one alter program. %s ignored.\n",
845 case O(MONITOR
,'r'): /* rebuild increments */
846 case O(MONITOR
,Increment
):
847 increments
= atoi(optarg
);
848 if (increments
> 99 || increments
< 1) {
849 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
854 case O(MONITOR
,'d'): /* delay in seconds */
856 case O(BUILD
,'d'): /* delay for bitmap updates */
859 pr_err("only specify delay once. %s ignored.\n",
862 delay
= strtol(optarg
, &c
, 10);
863 if (!optarg
[0] || *c
|| delay
<1) {
864 pr_err("invalid delay: %s\n",
870 case O(MONITOR
,'f'): /* daemonise */
871 case O(MONITOR
,Fork
):
874 case O(MONITOR
,'i'): /* pid */
876 pr_err("only specify one pid file. %s ignored.\n",
881 case O(MONITOR
,'1'): /* oneshot */
885 case O(MONITOR
,'t'): /* test */
888 case O(MONITOR
,'y'): /* log messages to syslog */
889 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
892 case O(MONITOR
, NoSharing
):
896 /* now the general management options. Some are applicable
897 * to other modes. None have arguments.
902 case O(MANAGE
,Add
): /* add a drive */
905 case O(MANAGE
,ReAdd
):
908 case O(MANAGE
,'r'): /* remove a drive */
909 case O(MANAGE
,Remove
):
912 case O(MANAGE
,'f'): /* set faulty */
914 case O(INCREMENTAL
,'f'):
915 case O(INCREMENTAL
,Remove
):
916 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
917 * even though we will both fail and
918 * remove the device */
921 case O(INCREMENTAL
,'R'):
923 case O(ASSEMBLE
,'R'):
925 case O(CREATE
,'R'): /* Run the array */
927 pr_err("Cannot both Stop and Run an array\n");
934 pr_err("Cannot both Run and Stop an array\n");
946 case O(MISC
,KillOpt
):
953 case O(MISC
, WaitOpt
):
954 case O(MISC
, Waitclean
):
955 case O(MISC
, DetailPlatform
):
956 case O(MISC
, KillSubarray
):
957 case O(MISC
, UpdateSubarray
):
958 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
960 pr_err("subarray can only"
961 " be specified once\n");
966 if (devmode
&& devmode
!= opt
&&
967 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
968 pr_err("--examine/-E cannot be given with ");
969 if (devmode
== 'E') {
970 if (option_index
>= 0)
971 fprintf(stderr
, "--%s\n",
972 long_options
[option_index
].name
);
974 fprintf(stderr
, "-%c\n", opt
);
975 } else if (isalpha(devmode
))
976 fprintf(stderr
, "-%c\n", devmode
);
978 fprintf(stderr
, "previous option\n");
983 case O(MISC
, UdevRules
):
984 if (devmode
&& devmode
!= opt
) {
985 pr_err("--udev-rules must"
986 " be the only option.\n");
989 pr_err("only specify one udev "
990 "rule filename. %s ignored.\n",
993 udev_filename
= optarg
;
1001 case O(MISC
, Sparc22
):
1002 if (devmode
!= 'E') {
1003 pr_err("--sparc2.2 only allowed with --examine\n");
1009 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
1010 case O(ASSEMBLE
,Bitmap
):
1012 pr_err("bitmap file needed with -b in --assemble mode\n");
1015 if (strcmp(optarg
, "internal")==0) {
1016 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1019 bitmap_fd
= open(optarg
, O_RDWR
);
1020 if (!*optarg
|| bitmap_fd
< 0) {
1021 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1024 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1027 case O(ASSEMBLE
, BackupFile
):
1028 case O(GROW
, BackupFile
):
1029 /* Specify a file into which grow might place a backup,
1030 * or from which assemble might recover a backup
1033 pr_err("backup file already specified, rejecting %s\n", optarg
);
1036 backup_file
= optarg
;
1039 case O(GROW
, Continue
):
1040 /* Continue interrupted grow
1044 case O(ASSEMBLE
, InvalidBackup
):
1045 /* Acknowledge that the backupfile is invalid, but ask
1046 * to continue anyway
1052 case O(BUILD
,Bitmap
):
1054 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1055 if (strcmp(optarg
, "none") == 0) {
1056 pr_err("'--bitmap none' only"
1057 " supported for --grow\n");
1062 case O(GROW
,Bitmap
):
1063 if (strcmp(optarg
, "internal")== 0 ||
1064 strcmp(optarg
, "none")== 0 ||
1065 strchr(optarg
, '/') != NULL
) {
1066 bitmap_file
= optarg
;
1070 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1071 " not '%s'\n", optarg
);
1074 case O(GROW
,BitmapChunk
):
1075 case O(BUILD
,BitmapChunk
):
1076 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1077 bitmap_chunk
= parse_size(optarg
);
1078 if (bitmap_chunk
<= 0 ||
1079 bitmap_chunk
& (bitmap_chunk
- 1)) {
1080 pr_err("invalid bitmap chunksize: %s\n",
1084 bitmap_chunk
= bitmap_chunk
* 512;
1087 case O(GROW
, WriteBehind
):
1088 case O(BUILD
, WriteBehind
):
1089 case O(CREATE
, WriteBehind
): /* write-behind mode */
1090 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1092 write_behind
= strtol(optarg
, &c
, 10);
1093 if (write_behind
< 0 || *c
||
1094 write_behind
> 16383) {
1095 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1101 case O(INCREMENTAL
, 'r'):
1102 case O(INCREMENTAL
, RebuildMapOpt
):
1105 case O(INCREMENTAL
, IncrementalPath
):
1106 remove_path
= optarg
;
1109 /* We have now processed all the valid options. Anything else is
1112 if (option_index
> 0)
1113 pr_err(":option --%s not valid in %s mode\n",
1114 long_options
[option_index
].name
,
1115 map_num(modes
, mode
));
1117 pr_err("option -%c not valid in %s mode\n",
1118 opt
, map_num(modes
, mode
));
1125 if (print_help
== 2)
1126 help_text
= OptionHelp
;
1128 help_text
= mode_help
[mode
];
1129 if (help_text
== NULL
)
1131 fputs(help_text
,stdout
);
1135 if (!mode
&& devs_found
) {
1138 if (devlist
->disposition
== 0)
1139 devlist
->disposition
= devmode
;
1142 fputs(Usage
, stderr
);
1147 struct createinfo
*ci
= conf_get_create_info();
1149 if (strcasecmp(symlinks
, "yes") == 0)
1151 else if (strcasecmp(symlinks
, "no") == 0)
1154 pr_err("option --symlinks must be 'no' or 'yes'\n");
1158 /* Ok, got the option parsing out of the way
1159 * hopefully it's mostly right but there might be some stuff
1162 * That is mosty checked in the per-mode stuff but...
1164 * For @,B,C and A without -s, the first device listed must be
1165 * an md device. We check that here and open it.
1168 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1170 || (mode
== ASSEMBLE
&& ! scan
)) {
1171 if (devs_found
< 1) {
1172 pr_err("an md device must be given in this mode\n");
1175 if ((int)ident
.super_minor
== -2 && autof
) {
1176 pr_err("--super-minor=dev is incompatible with --auto\n");
1179 if (mode
== MANAGE
|| mode
== GROW
) {
1180 mdfd
= open_mddev(devlist
->devname
, 1);
1184 /* non-existent device is OK */
1185 mdfd
= open_mddev(devlist
->devname
, 0);
1187 pr_err("device %s exists but is not an "
1188 "md array.\n", devlist
->devname
);
1191 if ((int)ident
.super_minor
== -2) {
1194 pr_err("--super-minor=dev given, and "
1195 "listed device %s doesn't exist.\n",
1200 ident
.super_minor
= minor(stb
.st_rdev
);
1202 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1203 /* We don't really want this open yet, we just might
1204 * have wanted to check some things
1212 if (raiddisks
== 1 && !force
&& level
!= LEVEL_FAULTY
) {
1213 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1214 " a mistake. If you really mean it you will need to specify --force before\n"
1215 " setting the number of drives.\n");
1220 if (homehost
== NULL
)
1221 homehost
= conf_get_homehost(&require_homehost
);
1222 if (homehost
== NULL
|| strcasecmp(homehost
, "<system>")==0) {
1223 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1224 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1225 homehost
= sys_hostname
;
1228 if (homehost
&& (!homehost
[0] || strcasecmp(homehost
, "<none>") == 0)) {
1230 require_homehost
= 0;
1233 if ((mode
== MISC
&& devmode
== 'E')
1234 || (mode
== MONITOR
&& spare_sharing
== 0))
1235 /* Anyone may try this */;
1236 else if (geteuid() != 0) {
1237 pr_err("must be super-user to perform this action\n");
1241 ident
.autof
= autof
;
1246 /* readonly, add/remove, readwrite, runstop */
1248 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1249 if (!rv
&& devs_found
>1)
1250 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1251 devlist
->next
, verbose
-quiet
, test
,
1253 if (!rv
&& readonly
< 0)
1254 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1256 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1259 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1260 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1261 /* Only a device has been given, so get details from config file */
1262 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1263 if (array_ident
== NULL
) {
1264 pr_err("%s not identified in config file.\n",
1270 if (array_ident
->autof
== 0)
1271 array_ident
->autof
= autof
;
1272 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1273 NULL
, backup_file
, invalid_backup
,
1274 readonly
, runstop
, update
,
1275 homehost
, require_homehost
,
1276 verbose
-quiet
, force
,
1280 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1281 devlist
->next
, backup_file
, invalid_backup
,
1282 readonly
, runstop
, update
,
1283 homehost
, require_homehost
,
1284 verbose
-quiet
, force
,
1286 else if (devs_found
> 0) {
1287 if (update
&& devs_found
> 1) {
1288 pr_err("can only update a single array at a time\n");
1291 if (backup_file
&& devs_found
> 1) {
1292 pr_err("can only assemble a single array when providing a backup file.\n");
1295 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1296 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1297 if (array_ident
== NULL
) {
1298 pr_err("%s not identified in config file.\n",
1303 if (array_ident
->autof
== 0)
1304 array_ident
->autof
= autof
;
1305 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1306 NULL
, backup_file
, invalid_backup
,
1307 readonly
, runstop
, update
,
1308 homehost
, require_homehost
,
1309 verbose
-quiet
, force
,
1314 pr_err("--update not meaningful with a --scan assembly.\n");
1318 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1321 rv
= scan_assemble(autof
, ss
, readonly
, runstop
,
1325 force
, freeze_reshape
);
1330 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1331 if (write_behind
&& !bitmap_file
) {
1332 pr_err("write-behind mode requires a bitmap.\n");
1336 if (raiddisks
== 0) {
1337 pr_err("no raid-devices specified.\n");
1343 if (strcmp(bitmap_file
, "internal")==0) {
1344 pr_err("'internal' bitmaps not supported with --build\n");
1349 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1350 raiddisks
, devlist
->next
, assume_clean
,
1351 bitmap_file
, bitmap_chunk
, write_behind
,
1352 delay
, verbose
-quiet
, autof
, size
);
1355 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1356 if (write_behind
&& !bitmap_file
) {
1357 pr_err("write-behind mode requires a bitmap.\n");
1361 if (raiddisks
== 0) {
1362 pr_err("no raid-devices specified.\n");
1367 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1368 raiddisks
, sparedisks
, ident
.name
, homehost
,
1369 ident
.uuid_set
? ident
.uuid
: NULL
,
1370 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1371 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1374 if (devmode
== 'E') {
1375 if (devlist
== NULL
&& !scan
) {
1376 pr_err("No devices to examine\n");
1379 if (devlist
== NULL
)
1380 devlist
= conf_get_devs();
1381 if (devlist
== NULL
) {
1382 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1385 if (brief
&& verbose
)
1387 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1389 SparcAdjust
, ss
, homehost
);
1390 } else if (devmode
== DetailPlatform
) {
1391 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1392 } else if (devlist
== NULL
) {
1393 if (devmode
== 'S' && scan
)
1394 rv
= stop_scan(quiet
);
1395 else if ((devmode
== 'D' || devmode
== Waitclean
) && scan
)
1396 rv
= misc_scan(devmode
, verbose
, export
,
1397 test
, homehost
, prefer
);
1398 else if (devmode
== UdevRules
)
1399 rv
= Write_rules(udev_filename
);
1401 pr_err("No devices given.\n");
1405 rv
= misc_list(devlist
, brief
, verbose
, export
, test
,
1406 homehost
, prefer
, subarray
, update
,
1411 if (!devlist
&& !scan
) {
1412 pr_err("Cannot monitor: need --scan or at least one device\n");
1416 if (pidfile
&& !daemonise
) {
1417 pr_err("Cannot write a pid file when not in daemon mode\n");
1422 if (get_linux_version() > 2006016)
1423 /* mdstat responds to poll */
1428 rv
= Monitor(devlist
, mailaddr
, program
,
1429 delay
?delay
:60, daemonise
, scan
, oneshot
,
1430 dosyslog
, test
, pidfile
, increments
,
1431 spare_sharing
, prefer
);
1435 if (array_size
>= 0) {
1436 /* alway impose array size first, independent of
1438 * Do not allow level or raid_disks changes at the
1439 * same time as that can be irreversibly destructive.
1443 if (raiddisks
|| level
!= UnSet
) {
1444 pr_err("cannot change array size in same operation "
1445 "as changing raiddisks or level.\n"
1446 " Change size first, then check that data is still intact.\n");
1450 sysfs_init(&sra
, mdfd
, 0);
1451 if (array_size
== 0)
1452 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1454 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1457 pr_err("--array-size setting"
1458 " is too large.\n");
1460 pr_err("current kernel does"
1461 " not support setting --array-size\n");
1466 if (devs_found
> 1 && raiddisks
== 0) {
1468 if (size
>= 0 || chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1469 pr_err("--add cannot be used with "
1470 "other geometry changes in --grow mode\n");
1474 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1475 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1480 } else if (bitmap_file
) {
1481 if (size
>= 0 || raiddisks
|| chunk
||
1482 layout_str
!= NULL
|| devs_found
> 1) {
1483 pr_err("--bitmap changes cannot be "
1484 "used with other geometry changes "
1485 "in --grow mode\n");
1490 delay
= DEFAULT_BITMAP_DELAY
;
1491 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1492 bitmap_chunk
, delay
, write_behind
, force
);
1493 } else if (grow_continue
)
1494 rv
= Grow_continue_command(devlist
->devname
,
1497 else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1498 || chunk
!= 0 || level
!= UnSet
) {
1499 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1500 size
, level
, layout_str
, chunk
, raiddisks
,
1502 assume_clean
, force
);
1503 } else if (array_size
< 0)
1504 pr_err("no changes to --grow\n");
1512 pr_err("--incremental --scan meaningless without --run.\n");
1515 if (devmode
== 'f') {
1516 pr_err("--incremental --scan --fail not supported.\n");
1519 rv
= IncrementalScan(verbose
);
1522 if (!rebuild_map
&& !scan
) {
1523 pr_err("--incremental requires a device.\n");
1528 if (devlist
->next
) {
1529 pr_err("--incremental can only handle one device.\n");
1534 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1537 rv
= Incremental(devlist
->devname
, verbose
-quiet
,
1538 runstop
, ss
, homehost
,
1539 require_homehost
, autof
,
1549 static int scan_assemble(int autof
, struct supertype
*ss
,
1550 int readonly
, int runstop
,
1551 struct mddev_ident
*ident
,
1552 char *homehost
, int require_homehost
,
1553 int verbose
, int force
,
1556 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1557 struct mddev_dev
*devlist
= conf_get_devs();
1558 struct map_ent
*map
= NULL
;
1561 int failures
, successes
;
1563 if (conf_verify_devnames(array_list
)) {
1564 pr_err("Duplicate MD device names in "
1565 "conf file were found.\n");
1568 if (devlist
== NULL
) {
1569 pr_err("No devices listed in conf file were found.\n");
1572 for (a
= array_list
; a
; a
= a
->next
) {
1578 pr_err("%s: failed to get "
1579 "exclusive lock on mapfile\n",
1585 for (a
= array_list
; a
; a
= a
->next
) {
1590 strcasecmp(a
->devname
, "<ignore>") == 0)
1593 r
= Assemble(ss
, a
->devname
,
1596 readonly
, runstop
, NULL
,
1597 homehost
, require_homehost
,
1608 } while (failures
&& successes
);
1609 if (homehost
&& cnt
== 0) {
1610 /* Maybe we can auto-assemble something.
1611 * Repeatedly call Assemble in auto-assemble mode
1616 ident
->autof
= autof
;
1618 struct mddev_dev
*devlist
= conf_get_devs();
1621 rv2
= Assemble(ss
, NULL
,
1636 /* Incase there are stacked devices, we need to go around again */
1638 if (cnt
== 0 && rv
== 0) {
1639 pr_err("No arrays found in config file or automatically\n");
1643 } else if (cnt
== 0 && rv
== 0) {
1644 pr_err("No arrays found in config file\n");
1651 static int misc_scan(char devmode
, int verbose
, int export
, int test
,
1652 char *homehost
, char *prefer
)
1654 /* apply --detail or --wait-clean to
1655 * all devices in /proc/mdstat
1657 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1658 struct mdstat_ent
*e
;
1659 struct map_ent
*map
= NULL
;
1661 int v
= verbose
>1?0:verbose
+1;
1664 for (members
= 0; members
<= 1; members
++) {
1665 for (e
=ms
; e
; e
=e
->next
) {
1668 int member
= e
->metadata_version
&&
1669 strncmp(e
->metadata_version
,
1670 "external:/", 10) == 0;
1671 if (members
!= member
)
1673 me
= map_by_devnum(&map
, e
->devnum
);
1675 && strcmp(me
->path
, "/unknown") != 0)
1678 name
= get_md_name(e
->devnum
);
1681 pr_err("cannot find device file for %s\n",
1686 rv
|= Detail(name
, v
,
1690 rv
|= WaitClean(name
, -1, v
);
1698 static int stop_scan(int quiet
)
1700 /* apply --stop to all devices in /proc/mdstat */
1701 /* Due to possible stacking of devices, repeat until
1702 * nothing more can be stopped
1704 int progress
=1, err
;
1708 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1709 struct mdstat_ent
*e
;
1711 if (!progress
) last
= 1;
1712 progress
= 0; err
= 0;
1713 for (e
=ms
; e
; e
=e
->next
) {
1714 char *name
= get_md_name(e
->devnum
);
1718 pr_err("cannot find device file for %s\n",
1722 mdfd
= open_mddev(name
, 1);
1724 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1734 } while (!last
&& err
);
1740 static int misc_list(struct mddev_dev
*devlist
,
1741 int brief
, int verbose
, int export
, int test
,
1742 char *homehost
, char *prefer
, char *subarray
,
1743 char *update
, struct mddev_ident
*ident
,
1744 struct supertype
*ss
, int force
, int quiet
)
1746 struct mddev_dev
*dv
;
1749 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1752 switch(dv
->disposition
) {
1754 rv
|= Detail(dv
->devname
,
1756 export
, test
, homehost
, prefer
);
1758 case KillOpt
: /* Zero superblock */
1760 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1764 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1771 rv
|= Query(dv
->devname
); continue;
1773 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1776 rv
|= Wait(dv
->devname
); continue;
1778 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1780 rv
|= Kill_subarray(dv
->devname
, subarray
, quiet
);
1782 case UpdateSubarray
:
1783 if (update
== NULL
) {
1784 pr_err("-U/--update must be specified with --update-subarray\n");
1788 rv
|= Update_subarray(dv
->devname
, subarray
,
1789 update
, ident
, quiet
);
1792 mdfd
= open_mddev(dv
->devname
, 1);
1794 switch(dv
->disposition
) {
1796 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1798 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1800 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1802 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;